diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index aa524e4832..2bf3d55e1f 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -114,7 +114,7 @@ class Container $this->get('config') ); } - + private function loadDateTime() { return new \Espo\Core\Utils\DateTime( @@ -209,6 +209,13 @@ class Container ); } + private function loadFieldManager() + { + return new \Espo\Core\Utils\FieldManager( + $this->get('metadata') + ); + } + public function setUser($user) { $this->data['user'] = $user; diff --git a/application/Espo/Core/Utils/FieldManager.php b/application/Espo/Core/Utils/FieldManager.php new file mode 100644 index 0000000000..043863d942 --- /dev/null +++ b/application/Espo/Core/Utils/FieldManager.php @@ -0,0 +1,93 @@ +metadata = $metadata; + } + + protected function getMetadata() + { + return $this->metadata; + } + + + public function read($name, $scope) + { + return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.fields.'.$name); + } + + public function create($name, $fieldDef, $scope) + { + return $this->update($name, $fieldDef, $scope); + } + + public function update($name, $fieldDef, $scope) + { + $defs = $this->normalizeDefs($name, $fieldDef); + + return $this->setEntityDefs($defs, $scope); + } + + public function delete($name, $scope) + { + $unsets = 'fields.'.$name; + + return $this->getMetadata()->unsets($unsets, $this->metadataType, $scope); + } + + /** + * Add all needed block for a field defenition + * + * @param string $fieldName + * @param array $fieldDef + * @return array + */ + protected function normalizeDefs($fieldName, array $fieldDef) + { + return array( + 'fields' => array( + $fieldName => $fieldDef, + ), + ); + } + + + protected function setEntityDefs($defs, $scope) + { + $data = Json::encode($defs); + $result = $this->getMetadata()->set($data, $this->metadataType, $scope); + + return $result; + } + + +} \ No newline at end of file diff --git a/application/Espo/Core/Utils/File/Manager.php b/application/Espo/Core/Utils/File/Manager.php index 5e846ac239..2ad99d0e03 100644 --- a/application/Espo/Core/Utils/File/Manager.php +++ b/application/Espo/Core/Utils/File/Manager.php @@ -199,13 +199,13 @@ class Manager * * @return bool */ - public function putContentsJSON($path, $data) + public function putContentsJson($path, $data) { if (!Utils\Json::isJSON($data)) { - $data = Utils\Json::encode($data); + $data = Utils\Json::encode($data, JSON_PRETTY_PRINT); } - return $this->putContents($path, $data, JSON_PRETTY_PRINT); + return $this->putContents($path, $data); } /** @@ -226,10 +226,10 @@ class Manager $data= Utils\Util::merge($savedDataArray, $newDataArray); if ($isJSON) { - $data= Utils\Json::encode($data); + $data= Utils\Json::encode($data, JSON_PRETTY_PRINT); } - return $this->putContents($path, $data, JSON_PRETTY_PRINT); + return $this->putContents($path, $data); } /** @@ -278,18 +278,23 @@ class Manager * * @param string | array $path * @param array | string $unsets [description] - * @return [type] [description] + * @return bool */ - public function unsetContents($path, $unsets) + public function unsetContents($path, $unsets, $isJSON = true) { $currentData = $this->getContents($path); - - $currentDataArray= $this->getArrayData($currentData); - if (!is_array($currentDataArray)) { + if ($currentData == false) { + $GLOBALS['log']->notice('FileManager::unsetContents: File ['.$this->concatPaths($path).'] does not exist.'); return false; } - $unsettedData = Utils\Util::unsetInArray($currentData, $unsets); + $currentDataArray = $this->getArrayData($currentData); + + $unsettedData = Utils\Util::unsetInArray($currentDataArray, $unsets); + + if ($isJSON) { + return $this->putContentsJson($path, $unsettedData); + } return $this->putContents($path, $unsettedData); } diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index 65ed5ee1b7..98588c0c2d 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -244,17 +244,35 @@ class Metadata { $path = $this->paths['customPath']; - if (file_exists($path)) { - $result = $this->getFileManager()->mergeContents(array($path, $type, $scope.'.json'), $data, true); - } else { - $result = $this->getFileManager()->putContentsJSON(array($path, $type, $scope.'.json'), $data); - } + $result = $this->getFileManager()->mergeContents(array($path, $type, $scope.'.json'), $data, true); $this->init(true); return $result; } + /** + * Unset some fields and other stuff in metadat + * + * @param array | string $unsets Ex. 'fields.name' + * @param string $type Ex. 'entityDefs' + * @param string $scope + * @return bool + */ + public function unsets($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.'); + } + + return $result; + } + + public function getOrmMetadata($reload = false) { if (!empty($this->ormMeta) && !$reload) { diff --git a/custom/Espo/Custom/Resources/metadata/entityDefs/Account.json b/custom/Espo/Custom/Resources/metadata/entityDefs/Account.json new file mode 100644 index 0000000000..8cc5063a7f --- /dev/null +++ b/custom/Espo/Custom/Resources/metadata/entityDefs/Account.json @@ -0,0 +1,16 @@ +{ + "fields": { + "someName": { + "type": "varchar", + "maxLength": 40 + }, + "someName2": { + "type": "varchar", + "maxLength": 40 + }, + "some": { + "type": "varchar", + "maxLength": 40 + } + } +} \ No newline at end of file diff --git a/tests/Espo/Core/Utils/File/ManagerTest.php b/tests/Espo/Core/Utils/File/ManagerTest.php index 040a456688..0a7d5cab7b 100644 --- a/tests/Espo/Core/Utils/File/ManagerTest.php +++ b/tests/Espo/Core/Utils/File/ManagerTest.php @@ -8,7 +8,7 @@ use tests\ReflectionHelper; class ManagerTest extends \PHPUnit_Framework_TestCase { protected $object; - + protected $objects; protected $filesPath= 'tests/testData/FileManager'; @@ -16,7 +16,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase protected $reflection; protected function setUp() - { + { $this->object = new \Espo\Core\Utils\File\Manager( array( 'defaultPermissions' => array ( @@ -36,8 +36,8 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $this->object = NULL; } - - function testGetFileName() + + public function testGetFileName() { $this->assertEquals('Donwload', $this->object->getFileName('Donwload.php')); @@ -48,14 +48,14 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Donwload', $this->object->getFileName('application/Espo/EntryPoints/Donwload.php')); } - function testGetContents() + public function testGetContents() { $result = file_get_contents($this->filesPath.'/getContent/test.json'); $this->assertEquals($result, $this->object->getContents( array($this->filesPath, 'getContent/test.json') )); } - function testPutContents() + public function testPutContents() { $testPath= $this->filesPath.'/setContent'; @@ -68,7 +68,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase } - function testConcatPaths() + public function testConcatPaths() { $input = 'application/Espo/Resources/metadata/app/panel.json'; $result = 'application/Espo/Resources/metadata/app/panel.json'; @@ -88,7 +88,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $input = array( - 'application/Espo/Resources/metadata/app', + 'application/Espo/Resources/metadata/app', 'panel.json', ); $result = 'application/Espo/Resources/metadata/app/panel.json'; @@ -97,7 +97,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $input = array( - 'application/Espo/Resources/metadata/app/', + 'application/Espo/Resources/metadata/app/', 'panel.json', ); $result = 'application/Espo/Resources/metadata/app/panel.json'; @@ -105,7 +105,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->reflection->invokeMethod('concatPaths', array($input)) ); } - function testGetDirName() + public function testGetDirName() { $input = 'data/logs/espo.log'; $result = 'logs'; @@ -133,7 +133,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase } - function testGetDirNameFullPath() + public function testGetDirNameFullPath() { $input = 'data/logs/espo.log'; $result = 'data/logs'; @@ -156,10 +156,24 @@ class ManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->object->getDirName($input)); $input = 'notRealPath/logs/espo.log'; - $result = 'notRealPath/logs'; + $result = 'notRealPath/logs'; $this->assertEquals($result, $this->object->getDirName($input)); } + public function testUnsetContents() + { + $testPath = $this->filesPath.'/unsets/test.json'; + + $initData = '{"fields":{"someName":{"type":"varchar","maxLength":40},"someName2":{"type":"varchar","maxLength":36}}}'; + $this->object->putContents($testPath, $initData); + + $unsets = 'fields.someName2'; + $this->assertTrue($this->object->unsetContents($testPath, $unsets)); + + $result = '{"fields":{"someName":{"type":"varchar","maxLength":40}}}'; + $this->assertJsonStringEqualsJsonFile($testPath, $result); + } + } diff --git a/tests/Espo/Core/Utils/UtilTest.php b/tests/Espo/Core/Utils/UtilTest.php index 336d203dfa..f96a572ee8 100644 --- a/tests/Espo/Core/Utils/UtilTest.php +++ b/tests/Espo/Core/Utils/UtilTest.php @@ -230,25 +230,25 @@ class UtilTest extends \PHPUnit_Framework_TestCase 'subOV' => '125', 'subOV2' => '125', ), - ), - ), + ), + ), ); $unsets = array( - 'Account' => array( + 'Account' => array( 'sub.subO.subOV', 'sub.subV', - ), + ), ); $result = array( 'Account' => array( 'useCache' => true, - 'sub' => array ( + 'sub' => array ( 'subO' => array( 'subOV2' => '125', ), - ), - ), + ), + ), ); $this->assertEquals($result, Util::unsetInArray($input, $unsets)); @@ -265,23 +265,23 @@ class UtilTest extends \PHPUnit_Framework_TestCase 'subOV' => '125', 'subOV2' => '125', ), - ), - ), + ), + ), ); - $unsets = array( - 'Account.sub.subO.subOV', 'Account.sub.subV', + $unsets = array( + 'Account.sub.subO.subOV', 'Account.sub.subV', ); $result = array( 'Account' => array( 'useCache' => true, - 'sub' => array ( + 'sub' => array ( 'subO' => array( 'subOV2' => '125', ), - ), - ), + ), + ), ); $this->assertEquals($result, Util::unsetInArray($input, $unsets)); @@ -299,26 +299,26 @@ class UtilTest extends \PHPUnit_Framework_TestCase 'subOV' => '125', 'subOV2' => '125', ), - ), - ), + ), + ), ); - $unsets = array( + $unsets = array( 'Account' => array( - 'sub.subO.subOV', - ), - 'Account.sub.subV', + 'sub.subO.subOV', + ), + 'Account.sub.subV', ); $result = array( 'Account' => array( 'useCache' => true, - 'sub' => array ( + 'sub' => array ( 'subO' => array( 'subOV2' => '125', ), - ), - ), + ), + ), ); $this->assertEquals($result, Util::unsetInArray($input, $unsets)); @@ -336,21 +336,45 @@ class UtilTest extends \PHPUnit_Framework_TestCase 'subOV' => '125', 'subOV2' => '125', ), - ), + ), ), 'Contact' => array( - 'useCache' => true, - ), + 'useCache' => true, + ), ); - $unsets = array( - 'Account', + $unsets = array( + 'Account', ); $result = array( 'Contact' => array( - 'useCache' => true, - ), + 'useCache' => true, + ), + ); + + $this->assertEquals($result, Util::unsetInArray($input, $unsets)); + } + + public function testUnsetInArrayByString() + { + $input = array( + 'Account' => array( + 'useCache' => true, + ), + 'Contact' => array( + 'useCache' => true, + ), + ); + + $unsets = 'Account.useCache'; + + $result = array( + 'Account' => array( + ), + 'Contact' => array( + 'useCache' => true, + ), ); $this->assertEquals($result, Util::unsetInArray($input, $unsets)); @@ -367,13 +391,13 @@ class UtilTest extends \PHPUnit_Framework_TestCase 'subOV' => '125', 'subOV2' => '125', ), - ), + ), ), 'Contact' => array( - 'useCache' => true, - ), - ); - + 'useCache' => true, + ), + ); + $this->assertEquals($inputArray, Util::getValueByKey($inputArray)); $this->assertEquals($inputArray, Util::getValueByKey($inputArray, '')); diff --git a/tests/testData/FileManager/unsets/test.json b/tests/testData/FileManager/unsets/test.json new file mode 100644 index 0000000000..1c9b8937c2 --- /dev/null +++ b/tests/testData/FileManager/unsets/test.json @@ -0,0 +1,8 @@ +{ + "fields": { + "someName": { + "type": "varchar", + "maxLength": 40 + } + } +} \ No newline at end of file