diff --git a/application/Espo/Controllers/FieldManager.php b/application/Espo/Controllers/FieldManager.php index e6edaf0c07..6eff62b089 100644 --- a/application/Espo/Controllers/FieldManager.php +++ b/application/Espo/Controllers/FieldManager.php @@ -72,7 +72,11 @@ class FieldManager extends \Espo\Core\Controllers\Base public function actionDelete($params, $data) { - return $this->getContainer()->get('fieldManager')->delete($params['name'], $params['scope']); + $res = $this->getContainer()->get('fieldManager')->delete($params['name'], $params['scope']); + + $this->getContainer()->get('dataManager')->rebuildMetadata(); + + return $res; } } diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 40dae10ada..9fe1d7499d 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -212,7 +212,8 @@ class Container private function loadFieldManager() { return new \Espo\Core\Utils\FieldManager( - $this->get('metadata') + $this->get('metadata'), + $this->get('language') ); } diff --git a/application/Espo/Core/Utils/FieldManager.php b/application/Espo/Core/Utils/FieldManager.php index 2d736c54f7..afe924ed46 100644 --- a/application/Espo/Core/Utils/FieldManager.php +++ b/application/Espo/Core/Utils/FieldManager.php @@ -28,14 +28,17 @@ class FieldManager { private $metadata; + private $language; + protected $metadataType = 'entityDefs'; protected $customOptionName = 'isCustom'; - public function __construct(Metadata $metadata) + public function __construct(Metadata $metadata, Language $language) { $this->metadata = $metadata; + $this->language = $language; } protected function getMetadata() @@ -43,15 +46,24 @@ class FieldManager return $this->metadata; } + protected function getLanguage() + { + return $this->language; + } + public function read($name, $scope) { - return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.fields.'.$name); + $fieldDef = $this->getFieldDef($name, $scope); + + $fieldDef['label'] = $this->getLanguage()->translate($name, 'fields', $scope); + + return $fieldDef; } public function create($name, $fieldDef, $scope) { - $existingField = $this->read($name, $scope); + $existingField = $this->getFieldDef($name, $scope); if (isset($existingField)) { throw new Error('Field ['.$name.'] exists in '.$scope); } @@ -66,7 +78,15 @@ class FieldManager $fieldDef[$this->customOptionName] = true; } - return $this->setEntityDefs($name, $fieldDef, $scope); + $res = true; + if (isset($fieldDef['label'])) { + $res &= $this->setLabel($name, $fieldDef['label'], $scope); + unset($fieldDef['label']); + } + + $res &= $this->setEntityDefs($name, $fieldDef, $scope); + + return (bool) $res; } public function delete($name, $scope) @@ -76,8 +96,11 @@ class FieldManager } $unsets = 'fields.'.$name; + $res = $this->getMetadata()->delete($unsets, $this->metadataType, $scope); - return $this->getMetadata()->unsets($unsets, $this->metadataType, $scope); + $this->deleteLabel($name, $scope); + + return $res; } protected function setEntityDefs($name, $fieldDef, $scope) @@ -85,9 +108,24 @@ class FieldManager $fieldDef = $this->normalizeDefs($name, $fieldDef); $data = Json::encode($fieldDef); - $result = $this->getMetadata()->set($data, $this->metadataType, $scope); + $res = $this->getMetadata()->set($data, $this->metadataType, $scope); - return $result; + return $res; + } + + protected function setLabel($name, $value, $scope) + { + return $this->getLanguage()->set($name, $value, 'fields', $scope); + } + + protected function deleteLabel($name, $scope) + { + return $this->getLanguage()->delete($name, 'fields', $scope); + } + + protected function getFieldDef($name, $scope) + { + return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.fields.'.$name); } /** @@ -118,7 +156,7 @@ class FieldManager protected function isCore($name, $scope) { - $existingField = $this->read($name, $scope); + $existingField = $this->getFieldDef($name, $scope); if (isset($existingField) && (!isset($existingField[$this->customOptionName]) || !$existingField[$this->customOptionName])) { return true; } diff --git a/application/Espo/Core/Utils/Language.php b/application/Espo/Core/Utils/Language.php index aa33efbe02..18ba764b71 100644 --- a/application/Espo/Core/Utils/Language.php +++ b/application/Espo/Core/Utils/Language.php @@ -184,10 +184,43 @@ class Language return $this->data; } - - protected function init() + public function set($label, $value, $category = 'labels', $scope = 'Global') { - if (!file_exists($this->getLangCacheFile()) || !$this->getConfig()->get('useCache')) { + $path = $this->paths['customPath']; + $currentLanguage = $this->getLanguage(); + + $data = $this->normalizeDefs($label, $value, $category); + + $result = $this->getFileManager()->mergeContents(array($path, $currentLanguage, $scope.'.json'), $data, true); + if ($result === false) { + throw new Error("Error saving languages. See log file for details."); + } + + $this->init(true); + + return $result; + } + + public function delete($label, $category = 'labels', $scope = 'Global') + { + $path = $this->paths['customPath']; + $currentLanguage = $this->getLanguage(); + + $unsets = array( + $category => $label, + ); + + $result = $this->getFileManager()->unsetContents(array($path, $currentLanguage, $scope.'.json'), $unsets, true); + + $this->init(true); + + return $result; + } + + + protected function init($reload = false) + { + if ($reload || !file_exists($this->getLangCacheFile()) || !$this->getConfig()->get('useCache')) { $this->fullData = $this->getUnifier()->unify($this->name, $this->paths, true); $result = true; @@ -204,6 +237,21 @@ class Language $this->data = $this->getFileManager()->getContents($this->getLangCacheFile()); } + protected function normalizeDefs($label, $value, $category) + { + if (!is_array($label)) { + $label = array( + $label => $value, + ); + } + + $data = array( + $category => $label, + ); + + return $data; + } + diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index af8243a9d9..51278eeaff 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -264,14 +264,14 @@ class Metadata * @param string $scope * @return bool */ - public function unsets($unsets, $type, $scope) + public function delete($unsets, $type, $scope) { $path = $this->paths['customPath']; $result = $this->getFileManager()->unsetContents(array($path, $type, $scope.'.json'), $unsets, true); if ($result == false) { - $GLOBALS['log']->warning('Metadata unsets available only for custom code.'); + $GLOBALS['log']->warning('Delete metadata items available only for custom code.'); } $this->init(true); diff --git a/tests/Espo/Core/Utils/FieldManagerTest.php b/tests/Espo/Core/Utils/FieldManagerTest.php index f3f0fa6a91..0c51b17d45 100644 --- a/tests/Espo/Core/Utils/FieldManagerTest.php +++ b/tests/Espo/Core/Utils/FieldManagerTest.php @@ -16,8 +16,9 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->objects['metadata'] = $this->getMockBuilder('\Espo\Core\Utils\Metadata')->disableOriginalConstructor()->getMock(); + $this->objects['language'] = $this->getMockBuilder('\Espo\Core\Utils\Language')->disableOriginalConstructor()->getMock(); - $this->object = new \Espo\Core\Utils\FieldManager($this->objects['metadata']); + $this->object = new \Espo\Core\Utils\FieldManager($this->objects['metadata'], $this->objects['language']); $this->reflection = new ReflectionHelper($this->object); } @@ -52,9 +53,15 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase ->method('set') ->will($this->returnValue(true)); + $this->objects['language'] + ->expects($this->once()) + ->method('set') + ->will($this->returnValue(true)); + $data = array( "type" => "varchar", "maxLength" => "50", + "label" => "Name", ); $this->objects['metadata'] @@ -93,6 +100,7 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase "type" => "varchar", "maxLength" => "50", "isCustom" => true, + "label" => 'Var Name', ); $this->objects['metadata'] @@ -100,6 +108,11 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase ->method('get') ->will($this->returnValue($data)); + $this->objects['language'] + ->expects($this->once()) + ->method('translate') + ->will($this->returnValue('Var Name')); + $this->assertEquals($data, $this->object->read('varName', 'Account')); }