FieldManager fixes

This commit is contained in:
Taras Machyshyn
2014-04-04 15:37:18 +03:00
parent 7cc12ff8e1
commit 6c3c2183fe
3 changed files with 47 additions and 21 deletions

View File

@@ -52,19 +52,22 @@ class FieldManager extends \Espo\Core\Controllers\Base
throw new Error("Field 'name' cannnot be empty");
}
$name = $data['name'];
unset($data['name']);
$fieldManager = $this->getContainer()->get('fieldManager');
$fieldManager->create($data['name'], $data, $params['scope']);
$this->getContainer()->get('fieldManager')->create($name, $data, $params['scope']);
$this->getContainer()->get('dataManager')->rebuild($params['scope']);
return $this->getContainer()->get('fieldManager')->read($name, $params['scope']);
return $fieldManager->read($data['name'], $params['scope']);
}
public function actionUpdate($params, $data)
{
$this->getContainer()->get('fieldManager')->update($params['name'], $data, $params['scope']);
$fieldManager = $this->getContainer()->get('fieldManager');
$fieldManager->update($params['name'], $data, $params['scope']);
return $this->getContainer()->get('fieldManager')->read($params['name'], $params['scope']);
$this->getContainer()->get('dataManager')->rebuild($params['scope']);
return $fieldManager->read($params['name'], $params['scope']);
}
public function actionDelete($params, $data)

View File

@@ -61,28 +61,35 @@ class FieldManager
public function update($name, $fieldDef, $scope)
{
$existingField = $this->read($name, $scope);
if (isset($existingField) && (!isset($existingField['isCustom']) || !$existingField['isCustom'])) {
throw new Error('Core field ['.$name.'] cannot be changed in '.$scope);
}
/*Add option to metadata that identify the custom field*/
if (!isset($fieldDef[$this->customOptionName]) || !$fieldDef[$this->customOptionName]) {
/*Add option to metadata to identify the custom field*/
if (!$this->isCore($name, $scope)) {
$fieldDef[$this->customOptionName] = true;
}
$defs = $this->normalizeDefs($name, $fieldDef);
return $this->setEntityDefs($defs, $scope);
return $this->setEntityDefs($name, $fieldDef, $scope);
}
public function delete($name, $scope)
{
if ($this->isCore($name, $scope)) {
throw new Error('Cannot delete core field ['.$name.'] in '.$scope);
}
$unsets = 'fields.'.$name;
return $this->getMetadata()->unsets($unsets, $this->metadataType, $scope);
}
protected function setEntityDefs($name, $fieldDef, $scope)
{
$fieldDef = $this->normalizeDefs($name, $fieldDef);
$data = Json::encode($fieldDef);
$result = $this->getMetadata()->set($data, $this->metadataType, $scope);
return $result;
}
/**
* Add all needed block for a field defenition
*
@@ -92,6 +99,16 @@ class FieldManager
*/
protected function normalizeDefs($fieldName, array $fieldDef)
{
if (isset($fieldDef['name'])) {
unset($fieldDef['name']);
}
foreach ($fieldDef as $defName => $defValue) {
if (!isset($defValue)) {
unset($fieldDef[$defName]);
}
}
return array(
'fields' => array(
$fieldName => $fieldDef,
@@ -99,13 +116,14 @@ class FieldManager
);
}
protected function setEntityDefs($defs, $scope)
protected function isCore($name, $scope)
{
$data = Json::encode($defs);
$result = $this->getMetadata()->set($data, $this->metadataType, $scope);
$existingField = $this->read($name, $scope);
if (isset($existingField) && (!isset($existingField[$this->customOptionName]) || !$existingField[$this->customOptionName])) {
return true;
}
return $result;
return false;
}

View File

@@ -22,6 +22,8 @@
namespace Espo\Core\Utils;
use Espo\Core\Exceptions\Error;
class Metadata
{
protected $meta = null;
@@ -245,6 +247,9 @@ class Metadata
$path = $this->paths['customPath'];
$result = $this->getFileManager()->mergeContents(array($path, $type, $scope.'.json'), $data, true);
if ($result === false) {
throw new Error("Error saving metadata. See log file for details.");
}
$this->init(true);