Skip to content

Commit

Permalink
Merge pull request #5230 in SW/shopware from sw-19223/5.3/refactor-cu…
Browse files Browse the repository at this point in the history
…stomer-stream-ui to 5.3

* commit '5b621fc3a9767066000ec5a222e5c71ddf369bf0':
  SW-19233 - Add translations, fix cs and disable date field for dynamic streams
  SW-19223 - Refactor customer stream backend ui
  • Loading branch information
janbuecker committed Jul 24, 2017
2 parents e5bea84 + 5b621fc commit aaf24e7
Show file tree
Hide file tree
Showing 23 changed files with 678 additions and 511 deletions.
33 changes: 33 additions & 0 deletions _sql/migrations/946-change-customer-stream-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/

class Migrations_Migration946 extends Shopware\Components\Migrations\AbstractMigration
{
public function up($modus)
{
$this->addSql("ALTER TABLE `s_customer_streams` ADD `static` int(1) NULL DEFAULT '0';");
$this->addSql("UPDATE s_customer_streams SET `static` = 1 WHERE `type` = 'static' OR freeze_up IS NOT NULL;");
$this->addSql('ALTER TABLE `s_customer_streams` DROP COLUMN `type`;');
}
}
70 changes: 35 additions & 35 deletions engine/Shopware/Components/Api/Resource/CustomerStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ public function getList($offset = 0, $limit = 25, array $criteria = [], array $o
$row = array_merge($row, $counts[$id]);
}

$row['freezeUp'] = $this->updateFreezeUp($id, $row['freezeUp']);
if ($this->updateFrozenState($id, $row['freezeUp'], $row['conditions'])) {
$row['freezeUp'] = null;
$row['static'] = false;
}
}

return ['data' => $data, 'total' => $total];
Expand All @@ -203,7 +206,7 @@ public function create(array $data, $index = false)
$this->indexStream($stream);
}

if (array_key_exists('customers', $data) && $stream->getType() === CustomerStreamEntity::TYPE_STATIC) {
if (array_key_exists('customers', $data) && $stream->isStatic()) {
$this->insertCustomers($data['customers'], $stream->getId());
}

Expand Down Expand Up @@ -231,11 +234,11 @@ public function update($id, array $data, $index = false)
throw new ValidationException($violations);
}

if ($stream->getType() === CustomerStreamEntity::TYPE_DYNAMIC && $index) {
if (!$stream->isStatic() && $index) {
$this->indexStream($stream);
}

if (array_key_exists('customers', $data) && $stream->getType() === CustomerStreamEntity::TYPE_STATIC) {
if (array_key_exists('customers', $data) && $stream->isStatic()) {
$this->insertCustomers($data['customers'], $stream->getId());
}

Expand Down Expand Up @@ -287,16 +290,12 @@ public function indexStream(CustomerStreamEntity $stream, $offset = null, $limit
{
$this->checkPrivilege('save');

$now = new \DateTime();
if ($stream->getFreezeUp() < $now) {
if ($this->updateFrozenState($stream->getId(), $stream->getFreezeUp(), $stream->getConditions())) {
$stream->setStatic(false);
$stream->setFreezeUp(null);
$this->manager->flush($stream);
}

if ($stream->getFreezeUp() !== null) {
return;
}
if ($stream->getType() === CustomerStreamEntity::TYPE_STATIC) {
if ($stream->getFreezeUp() !== null || $stream->isStatic()) {
return;
}

Expand Down Expand Up @@ -330,18 +329,14 @@ private function getConditions($streamId, $conditions = [])
}
$stream = $this->manager->find(CustomerStreamEntity::class, $streamId);

switch ($stream->getType()) {
case CustomerStreamEntity::TYPE_DYNAMIC:
return $this->reflectionHelper->unserialize(
json_decode($stream->getConditions(), true),
'Serialization error in Customer Stream'
);

case CustomerStreamEntity::TYPE_STATIC:
return [new AssignedToStreamCondition($streamId)];
if ($stream->isStatic() || $stream->getFreezeUp()) {
return [new AssignedToStreamCondition($streamId)];
}

return [];
return $this->reflectionHelper->unserialize(
json_decode($stream->getConditions(), true),
'Serialization error in Customer Stream'
);
}

/**
Expand Down Expand Up @@ -371,28 +366,33 @@ private function insertCustomers(array $customerIds, $streamId)
}

/**
* @param int $id
* @param string|null $freezeUp
* Returns true if frozen state has changed
*
* @return string|null
* @param $streamId
* @param \DateTime|null $freezeUp
* @param string $conditions
*
* @return bool
*/
private function updateFreezeUp($id, $freezeUp)
private function updateFrozenState($streamId, \DateTime $freezeUp = null, $conditions)
{
if (!$freezeUp) {
return $freezeUp;
$now = new \DateTime();
if (!$freezeUp || $freezeUp >= $now) {
return false;
}

$now = new \DateTime();
$params = [
'id' => $streamId,
'freeze_up' => null,
'static' => (int) strlen($conditions) > 2,
];

if ($freezeUp >= $now) {
return $freezeUp;
}
$this->connection->executeUpdate(
'UPDATE s_customer_streams SET freeze_up = NULL WHERE id = :id',
[':id' => $id]
$this->manager->getConnection()->executeUpdate(
'UPDATE s_customer_streams SET static = :static, freeze_up = :freeze_up WHERE id = :id',
$params
);

return null;
return true;
}
}

Expand Down
15 changes: 9 additions & 6 deletions engine/Shopware/Controllers/Backend/CustomerStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,15 @@ public function addCustomerToStreamAction()
$customerId = (int) $this->Request()->getParam('customerId');
$connection = $this->container->get('dbal_connection');

$connection->executeUpdate(
'INSERT IGNORE INTO s_customer_streams_mapping (stream_id, customer_id) VALUES (:streamId, :customerId)',
[':streamId' => $streamId, ':customerId' => $customerId]
);

$this->View()->assign('success', true);
try {
$connection->executeUpdate(
'INSERT INTO s_customer_streams_mapping (stream_id, customer_id) VALUES (:streamId, :customerId)',
[':streamId' => $streamId, ':customerId' => $customerId]
);
$this->View()->assign('success', true);
} catch (Exception $e) {
$this->View()->assign('success', false);
}
}

public function removeCustomerFromStreamAction()
Expand Down
48 changes: 25 additions & 23 deletions engine/Shopware/Models/CustomerStream/CustomerStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
*/
class CustomerStream extends ModelEntity
{
const TYPE_DYNAMIC = 'dynamic';
const TYPE_STATIC = 'static';

/**
* INVERSE SIDE
*
Expand Down Expand Up @@ -69,17 +66,16 @@ class CustomerStream extends ModelEntity
private $description;

/**
* @var array
* @var string
* @ORM\Column(name="conditions", type="string", nullable=true)
*/
private $conditions;

/**
* @var string
* @Assert\NotBlank
* @ORM\Column(name="type", type="string", nullable=true)
* @var bool
* @ORM\Column(name="static", type="boolean", nullable=false)
*/
private $type = self::TYPE_DYNAMIC;
private $static = false;

/**
* @var \DateTime
Expand Down Expand Up @@ -129,34 +125,21 @@ public function setDescription($description)
}

/**
* @return array
* @return string
*/
public function getConditions()
{
return $this->conditions;
}

/**
* @param array $conditions
* @param string $conditions
*/
public function setConditions($conditions)
{
$this->conditions = $conditions;
}

/**
* @return string
*/
public function getType()
{
return $this->type;
}

public function setType($type)
{
$this->type = $type;
}

/**
* @return \DateTime
*/
Expand All @@ -165,11 +148,30 @@ public function getFreezeUp()
return $this->freezeUp;
}

/**
* @param $freezeUp string|\DateTime
*/
public function setFreezeUp($freezeUp)
{
if (is_string($freezeUp)) {
$freezeUp = new \DateTime($freezeUp);
}
$this->freezeUp = $freezeUp;
}

/**
* @return bool
*/
public function isStatic()
{
return $this->static;
}

/**
* @param $static bool
*/
public function setStatic($static)
{
$this->static = $static;
}
}
Loading

0 comments on commit aaf24e7

Please sign in to comment.