chg: [component:CRUD] Small refactoring to improve re-usability
parent
b6cf9e39dd
commit
d71f48fc9f
|
@ -117,7 +117,7 @@ class CRUDComponent extends Component
|
||||||
private function getMetaTemplates()
|
private function getMetaTemplates()
|
||||||
{
|
{
|
||||||
$metaTemplates = [];
|
$metaTemplates = [];
|
||||||
if (!$this->Table->hasBehavior('MetaFields')) {
|
if (!$this->metaFieldsSupported()) {
|
||||||
throw new \Exception(__("Table {$this->TableAlias} does not support meta_fields"));
|
throw new \Exception(__("Table {$this->TableAlias} does not support meta_fields"));
|
||||||
}
|
}
|
||||||
$metaFieldsBehavior = $this->Table->getBehavior('MetaFields');
|
$metaFieldsBehavior = $this->Table->getBehavior('MetaFields');
|
||||||
|
@ -255,7 +255,7 @@ class CRUDComponent extends Component
|
||||||
// prune empty values and marshall fields
|
// prune empty values and marshall fields
|
||||||
private function massageMetaFields($entity, $input, $allMetaTemplates=[])
|
private function massageMetaFields($entity, $input, $allMetaTemplates=[])
|
||||||
{
|
{
|
||||||
if (empty($input['MetaTemplates'] || !$this->Table->hasBehavior('MetaFields'))) {
|
if (empty($input['MetaTemplates'] || !$this->metaFieldsSupported())) {
|
||||||
return ['entity' => $entity, 'metafields_to_delete' => []];
|
return ['entity' => $entity, 'metafields_to_delete' => []];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,7 +365,7 @@ class CRUDComponent extends Component
|
||||||
$this->setAllTags();
|
$this->setAllTags();
|
||||||
}
|
}
|
||||||
$getParam = isset($params['get']) ? $params['get'] : $params;
|
$getParam = isset($params['get']) ? $params['get'] : $params;
|
||||||
if ($this->Table->hasBehavior('MetaFields')) {
|
if ($this->metaFieldsSupported()) {
|
||||||
if (empty($getParam['contain'])) {
|
if (empty($getParam['contain'])) {
|
||||||
$getParam['contain'] = [];
|
$getParam['contain'] = [];
|
||||||
}
|
}
|
||||||
|
@ -376,7 +376,8 @@ class CRUDComponent extends Component
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$data = $this->Table->get($id, $getParam);
|
$data = $this->Table->get($id, $getParam);
|
||||||
$data = $this->attachMetaTemplatesIfNeeded($data);
|
$metaTemplates = $this->getMetaTemplates();
|
||||||
|
$data = $this->attachMetaTemplatesIfNeeded($data, $metaTemplates);
|
||||||
if (!empty($params['fields'])) {
|
if (!empty($params['fields'])) {
|
||||||
$this->Controller->set('fields', $params['fields']);
|
$this->Controller->set('fields', $params['fields']);
|
||||||
}
|
}
|
||||||
|
@ -388,7 +389,7 @@ class CRUDComponent extends Component
|
||||||
if (!empty($params['fields'])) {
|
if (!empty($params['fields'])) {
|
||||||
$patchEntityParams['fields'] = $params['fields'];
|
$patchEntityParams['fields'] = $params['fields'];
|
||||||
}
|
}
|
||||||
if ($this->Table->hasBehavior('MetaFields')) {
|
if ($this->metaFieldsSupported()) {
|
||||||
$massagedData = $this->massageMetaFields($data, $input, $metaTemplates);
|
$massagedData = $this->massageMetaFields($data, $input, $metaTemplates);
|
||||||
unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity
|
unset($input['MetaTemplates']); // Avoid MetaTemplates to be overriden when patching entity
|
||||||
$data = $massagedData['entity'];
|
$data = $massagedData['entity'];
|
||||||
|
@ -400,7 +401,7 @@ class CRUDComponent extends Component
|
||||||
}
|
}
|
||||||
$savedData = $this->Table->save($data);
|
$savedData = $this->Table->save($data);
|
||||||
if ($savedData !== false) {
|
if ($savedData !== false) {
|
||||||
if ($this->Table->hasBehavior('MetaFields') && !empty($metaFieldsToDelete)) {
|
if ($this->metaFieldsSupported() && !empty($metaFieldsToDelete)) {
|
||||||
$this->Table->MetaFields->unlink($savedData, $metaFieldsToDelete);
|
$this->Table->MetaFields->unlink($savedData, $metaFieldsToDelete);
|
||||||
}
|
}
|
||||||
if (isset($params['afterSave'])) {
|
if (isset($params['afterSave'])) {
|
||||||
|
@ -441,7 +442,7 @@ class CRUDComponent extends Component
|
||||||
|
|
||||||
public function attachMetaData($id, $data)
|
public function attachMetaData($id, $data)
|
||||||
{
|
{
|
||||||
if (!$this->Table->hasBehavior('MetaFields')) {
|
if (!$this->metaFieldsSupported()) {
|
||||||
throw new \Exception(__("Table {$this->TableAlias} does not support meta_fields"));
|
throw new \Exception(__("Table {$this->TableAlias} does not support meta_fields"));
|
||||||
}
|
}
|
||||||
$metaFieldScope = $this->Table->getBehavior('MetaFields')->getScope();
|
$metaFieldScope = $this->Table->getBehavior('MetaFields')->getScope();
|
||||||
|
@ -475,7 +476,7 @@ class CRUDComponent extends Component
|
||||||
|
|
||||||
public function getMetaFields($id)
|
public function getMetaFields($id)
|
||||||
{
|
{
|
||||||
if (!$this->Table->hasBehavior('MetaFields')) {
|
if (!$this->metaFieldsSupported()) {
|
||||||
throw new \Exception(__("Table {$this->TableAlias} does not support meta_fields"));
|
throw new \Exception(__("Table {$this->TableAlias} does not support meta_fields"));
|
||||||
}
|
}
|
||||||
$query = $this->MetaFields->find();
|
$query = $this->MetaFields->find();
|
||||||
|
@ -522,7 +523,7 @@ class CRUDComponent extends Component
|
||||||
$params['contain'][] = 'Tags';
|
$params['contain'][] = 'Tags';
|
||||||
$this->setAllTags();
|
$this->setAllTags();
|
||||||
}
|
}
|
||||||
if ($this->Table->hasBehavior('MetaFields')) {
|
if ($this->metaFieldsSupported()) {
|
||||||
if (!empty($this->request->getQuery('full'))) {
|
if (!empty($this->request->getQuery('full'))) {
|
||||||
$params['contain']['MetaFields'] = ['MetaTemplateFields' => 'MetaTemplates'];
|
$params['contain']['MetaFields'] = ['MetaTemplateFields' => 'MetaTemplates'];
|
||||||
} else {
|
} else {
|
||||||
|
@ -544,7 +545,7 @@ class CRUDComponent extends Component
|
||||||
|
|
||||||
public function attachMetaTemplatesIfNeeded($data)
|
public function attachMetaTemplatesIfNeeded($data)
|
||||||
{
|
{
|
||||||
if (!$this->Table->hasBehavior('MetaFields')) {
|
if (!$this->metaFieldsSupported()) {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
$metaTemplates = $this->getMetaTemplates();
|
$metaTemplates = $this->getMetaTemplates();
|
||||||
|
@ -1062,6 +1063,11 @@ class CRUDComponent extends Component
|
||||||
return $this->Table->behaviors()->has('Tag');
|
return $this->Table->behaviors()->has('Tag');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function metaFieldsSupported()
|
||||||
|
{
|
||||||
|
return $this->Table->hasBehavior('MetaFields');
|
||||||
|
}
|
||||||
|
|
||||||
public function setAllTags()
|
public function setAllTags()
|
||||||
{
|
{
|
||||||
$this->Tags = TableRegistry::getTableLocator()->get('Tags.Tags');
|
$this->Tags = TableRegistry::getTableLocator()->get('Tags.Tags');
|
||||||
|
|
Loading…
Reference in New Issue