Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OrderBundle, Pimcore] add data-loader to 2.0 branch and fix for href and multihref #782

Merged
merged 2 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

namespace CoreShop\Bundle\OrderBundle\Controller;

use CoreShop\Bundle\MoneyBundle\CoreExtension\Money;
use CoreShop\Bundle\ResourceBundle\Controller\PimcoreController;
use CoreShop\Component\Pimcore\BCLayer\Href;
use CoreShop\Component\Pimcore\BCLayer\Multihref;
use CoreShop\Component\Pimcore\DataObject\DataLoader;
use Pimcore\Model\DataObject;

abstract class AbstractSaleController extends PimcoreController
Expand All @@ -28,63 +26,8 @@ abstract class AbstractSaleController extends PimcoreController
*/
protected function getDataForObject(DataObject\Concrete $data, $loadedObjects = [])
{
if (!$data instanceof DataObject\AbstractObject) {
return [];
}
$dataLoader = new DataLoader();

$objectData = [];
DataObject\Service::loadAllObjectFields($data);

$loadedObjects[] = $data->getId();

foreach ($data->getClass()->getFieldDefinitions() as $key => $def) {
$getter = 'get' . ucfirst($key);

if (!method_exists($data, $getter)) {
continue;
}

$fieldData = $data->$getter();

if ($def instanceof Href) {
if ($fieldData instanceof DataObject\Concrete) {
if (!in_array($fieldData->getId(), $loadedObjects)) {
$objectData[$key] = $this->getDataForObject($fieldData, $loadedObjects);
}
}
} elseif ($def instanceof Multihref) {
$objectData[$key] = [];

if (!is_array($fieldData)) {
continue;
}

foreach ($fieldData as $object) {
if ($object instanceof DataObject\Concrete) {
if (!in_array($object->getId(), $loadedObjects)) {
$objectData[$key][] = $this->getDataForObject($object, $loadedObjects);
}
}
}
} elseif ($def instanceof DataObject\ClassDefinition\Data) {
if ($def instanceof Money) {
$value = $fieldData;
} else {
$value = $def->getDataForEditmode($fieldData, $data, false);
}

$objectData[$key] = $value;
} else {
$objectData[$key] = null;
}
}

$loadedObjects[] = $data->getId();

$objectData['o_id'] = $data->getId();
$objectData['o_creationDate'] = $data->getCreationDate();
$objectData['o_modificationDate'] = $data->getModificationDate();

return $objectData;
return $dataLoader->getDataForObject($data, $loadedObjects);
}
}
84 changes: 84 additions & 0 deletions src/CoreShop/Component/Pimcore/DataObject/DataLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Component\Pimcore\DataObject;

use CoreShop\Bundle\MoneyBundle\CoreExtension\Money;
use Pimcore\Model\DataObject;

class DataLoader implements DataLoaderInterface
{
/**
* {@inheritdoc}
*/
public function getDataForObject(DataObject\Concrete $data, $loadedObjects = [])
{
if (!$data instanceof DataObject\AbstractObject) {
return [];
}

$objectData = [];
DataObject\Service::loadAllObjectFields($data);

$loadedObjects[] = $data->getId();

foreach ($data->getClass()->getFieldDefinitions() as $key => $def) {
$getter = 'get'.ucfirst($key);

if (!method_exists($data, $getter)) {
continue;
}

$fieldData = $data->$getter();

if ($def instanceof DataObject\ClassDefinition\Data\ManyToOneRelation || $def instanceof DataObject\ClassDefinition\Data\Href) {
if ($fieldData instanceof DataObject\Concrete) {
if (!in_array($fieldData->getId(), $loadedObjects)) {
$objectData[$key] = $this->getDataForObject($fieldData, $loadedObjects);
}
}
} elseif ($def instanceof DataObject\ClassDefinition\Data\ManyToManyRelation || $def instanceof DataObject\ClassDefinition\Data\Multihref) {
$objectData[$key] = [];

if (!is_array($fieldData)) {
continue;
}

foreach ($fieldData as $object) {
if ($object instanceof DataObject\Concrete) {
if (!in_array($object->getId(), $loadedObjects)) {
$objectData[$key][] = $this->getDataForObject($object, $loadedObjects);
}
}
}
} elseif ($def instanceof DataObject\ClassDefinition\Data) {
if ($def instanceof Money) {
$value = $fieldData;
} else {
$value = $def->getDataForEditmode($fieldData, $data, false);
}

$objectData[$key] = $value;
} else {
$objectData[$key] = null;
}
}

$loadedObjects[] = $data->getId();

$objectData['o_id'] = $data->getId();
$objectData['o_creationDate'] = $data->getCreationDate();
$objectData['o_modificationDate'] = $data->getModificationDate();

return $objectData;
}
}
25 changes: 25 additions & 0 deletions src/CoreShop/Component/Pimcore/DataObject/DataLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2019 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Component\Pimcore\DataObject;

use Pimcore\Model\DataObject;

interface DataLoaderInterface
{
/**
* @param DataObject\Concrete $data
* @param array $loadedObjects
* @return mixed
*/
public function getDataForObject(DataObject\Concrete $data, $loadedObjects = []);
}