Skip to content

Commit

Permalink
Solve problem that customlayouts are not able created
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisros committed May 28, 2019
1 parent c79b72a commit 7e1ad8c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,28 @@ public function addAction(Request $request)
*/
public function addCustomLayoutAction(Request $request)
{
$layoutId = $request->get('layoutIdentifier');

if ($layoutId) {
$existingLayout = DataObject\ClassDefinition\CustomLayout::getById($layoutId);
if ($existingLayout) {
throw new \Exception('Custom Layout identifier already exists');
}
}

$customLayout = DataObject\ClassDefinition\CustomLayout::create(
['name' => $request->get('name'),
[
'name' => $request->get('layoutName'),
'userOwner' => $this->getAdminUser()->getId(),
'classId' => $request->get('classId')]
'classId' => $request->get('classId')
]
);

// $customLayout->setId($layoutId);
$customLayout->save();

return $this->adminJson(['success' => true, 'id' => $customLayout->getId(), 'name' => $customLayout->getName(),
'data' => $customLayout]);
'data' => $customLayout]);
}

/**
Expand Down Expand Up @@ -316,6 +328,7 @@ public function deleteCustomLayoutAction(Request $request)
public function saveCustomLayoutAction(Request $request)
{
$customLayout = DataObject\ClassDefinition\CustomLayout::getById($request->get('id'));

$class = DataObject\ClassDefinition::getById($customLayout->getClassId());

$configuration = $this->decodeJson($request->get('configuration'));
Expand Down Expand Up @@ -345,7 +358,6 @@ public function saveCustomLayoutAction(Request $request)
return $this->adminJson(['success' => false, 'message' => $e->getMessage()]);
}
}

/**
* @Route("/save", methods={"PUT"})
*
Expand Down Expand Up @@ -1658,7 +1670,7 @@ public function suggestClassIdentifierAction()
$result = [
'suggestedIdentifier' => $maxId ? $maxId + 1 : 1,
'existingIds' => $existingIds
];
];

return $this->adminJson($result);
}
Expand Down
90 changes: 69 additions & 21 deletions models/DataObject/ClassDefinition/CustomLayout/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace Pimcore\Model\DataObject\ClassDefinition\CustomLayout;

use Pimcore\File;
use Pimcore\Model;
use Pimcore\Tool\Serialize;

Expand All @@ -30,11 +29,6 @@ class Dao extends Model\Dao\AbstractDao
*/
protected $model;

/**
* @var array
*/
protected $_sqlChangeLog = [];

/**
* @param null $id
*
Expand All @@ -45,7 +39,6 @@ public function getById($id = null)
if (!$id) {
$id = $this->model->getId();
}

$layoutRaw = $this->db->fetchRow('SELECT * FROM custom_layouts WHERE id = ?', $id);

if ($layoutRaw['id']) {
Expand All @@ -57,35 +50,97 @@ public function getById($id = null)
}
}

/**
* @param null $name
*
* @return int|null
*/
public function getIdByName($name = null)
{
$name = $this->db->fetchOne('SELECT id FROM custom_layouts WHERE name = ?', $name);

return $name;
}

/**
* @param null $id
*
* @return string
*/
public function getNameById($id = null)
{
$name = $this->db->fetchOne('SELECT name FROM custom_layouts WHERE id = ?', $id);

return $name;
}

/**
* @return int|mixed
*/
public function getNewId()
{
$maxId = $this->db->fetchOne('SELECT MAX(CAST(id AS SIGNED)) FROM custom_layouts;');
$newId = $maxId ? $maxId + 1 : 1;
$this->model->setId($newId);

return $newId;
}

/**
* Save object to database
*
* @return string|null
*/
protected function getLayoutData()
{
$file = PIMCORE_CUSTOMLAYOUT_DIRECTORY . '/custom_definition_'. $this->model->getId() .'.psf';
$file = PIMCORE_CUSTOMLAYOUT_DIRECTORY . '/custom_definition_'. $this->model->getId() .'.php';
if (is_file($file)) {
return Serialize::unserialize(file_get_contents($file));
$layout = @include $file;
if ($layout instanceof \Pimcore\Model\DataObject\ClassDefinition\CustomLayout) {
return $layout->getLayoutDefinitions();
}
}

return;
}

/**
* Get latest identifier
*
* @param int $classId
*
* @return int
*/
public function getLatestIdentifier($classId)
{
$maxId = $this->db->fetchOne('SELECT MAX(CAST(id AS SIGNED)) FROM custom_layouts');

return $maxId ? $maxId + 1 : 1;
}

/**
* Save layout to database
*
* @return bool
*
* @todo: update() and create() don't return anything
*/
public function save()
public function save($isUpdate = true)
{
if ($this->model->getId()) {
if (!$this->model->getId()) {
$maxId = $this->db->fetchOne('SELECT MAX(CAST(id AS SIGNED)) FROM custom_layouts;');
$this->model->setId($maxId ? $maxId + 1 : 1);
} else {
return $this->update();
}

return $this->create();

if (!$isUpdate) {

} else {

}
}

/**
Expand All @@ -109,23 +164,16 @@ public function update()
}

$this->db->update('custom_layouts', $data, ['id' => $this->model->getId()]);

// save definition as a serialized file
$definitionFile = PIMCORE_CUSTOMLAYOUT_DIRECTORY.'/custom_definition_'. $this->model->getId() .'.psf';
if (!is_writable(dirname($definitionFile)) || (is_file($definitionFile) && !is_writable($definitionFile))) {
throw new \Exception('Cannot write definition file in: ' . $definitionFile . ' please check write permission on this directory.');
}
File::put($definitionFile, Serialize::serialize($this->model->layoutDefinitions));
}

/**
* Create a new record for the object in database
*/
public function create()
{
$this->db->insert('custom_layouts', ['name' => $this->model->getName(), 'classId' => $this->model->getClassId()]);

$this->model->setId($this->db->lastInsertId());
$this->db->insert('custom_layouts', ['id' => $this->model->getId(), 'name' => $this->model->getName(), 'classId' => $this->model->getClassId()]);

$this->model->setCreationDate(time());
$this->model->setModificationDate(time());

Expand All @@ -138,6 +186,6 @@ public function create()
public function delete()
{
$this->db->delete('custom_layouts', ['id' => $this->model->getId()]);
@unlink(PIMCORE_CUSTOMLAYOUT_DIRECTORY.'/custom_definition_'. $this->model->getId() .'.psf');
@unlink(PIMCORE_CUSTOMLAYOUT_DIRECTORY.'/custom_definition_'. $this->model->getId() .'.php');
}
}

0 comments on commit 7e1ad8c

Please sign in to comment.