protected function load($linkName, $entityName) { $parentRelation = parent::load($linkName, $entityName); $relation = array($entityName => array('fields' => array($linkName . 'Types' => array('type' => 'jsonObject', 'notStorable' => true)))); $relation = \Espo\Core\Utils\Util::merge($parentRelation, $relation); return $relation; }
protected function load($linkName, $entityName) { $parentRelation = parent::load($linkName, $entityName); $foreignEntityName = $this->getForeignEntityName(); $relation = array($entityName => array('relations' => array($linkName => array('midKeys' => array(lcfirst($entityName) . 'Id', lcfirst($foreignEntityName) . 'Id'))))); $relation = \Espo\Core\Utils\Util::merge($parentRelation, $relation); return $relation; }
/** * Unite file content to the file * * @param string $name * @param array $paths * @param boolean $recursively Note: only for first level of sub directory, other levels of sub directories will be ignored * * @return array */ public function unify($name, $paths, $recursively = false) { $content = $this->unifySingle($paths['corePath'], $name, $recursively); if (!empty($paths['modulePath'])) { $customDir = strstr($paths['modulePath'], '{*}', true); $moduleList = isset($this->metadata) ? $this->getMetadata()->getModuleList() : $this->getFileManager()->getFileList($customDir, false, '', false); foreach ($moduleList as $moduleName) { $curPath = str_replace('{*}', $moduleName, $paths['modulePath']); $content = Utils\Util::merge($content, $this->unifySingle($curPath, $name, $recursively, $moduleName)); } } if (!empty($paths['customPath'])) { $content = Utils\Util::merge($content, $this->unifySingle($paths['customPath'], $name, $recursively)); } return $content; }
/** * Unite file content to the file * * @param [type] $name [description] * @param [type] $paths [description] * @param boolean $recursively Note: only for first level of sub directory, other levels of sub directories will be ignored * @param [type] $mergeLevel - merge level, see Espo\Core\Utils\Util::merge() * * @return array */ public function unify($name, $paths, $recursively = false, $mergeLevel = null, $mergeKeyName = null) { $content = $this->unifySingle($paths['corePath'], $name, $recursively); if (!empty($paths['modulePath'])) { $customDir = strstr($paths['modulePath'], '{*}', true); $dirList = $this->getFileManager()->getFileList($customDir, false, '', 'dir'); foreach ($dirList as $dirName) { $curPath = str_replace('{*}', $dirName, $paths['modulePath']); $content = Utils\Util::merge($content, $this->unifySingle($curPath, $name, $recursively, $dirName), $mergeLevel, $mergeKeyName); } } if (!empty($paths['customPath'])) { $content = Utils\Util::merge($content, $this->unifySingle($paths['customPath'], $name, $recursively), $mergeLevel, $mergeKeyName); } return $content; }
/** * Unite files content * * @param array $paths * @param bool $isReturnModuleNames - If need to return data with module names * * @return array */ public function unify(array $paths, $isReturnModuleNames = false) { $data = $this->loadData($paths['corePath']); if (!empty($paths['modulePath'])) { $moduleDir = strstr($paths['modulePath'], '{*}', true); $moduleList = isset($this->metadata) ? $this->getMetadata()->getModuleList() : $this->getFileManager()->getFileList($moduleDir, false, '', false); foreach ($moduleList as $moduleName) { $moduleFilePath = str_replace('{*}', $moduleName, $paths['modulePath']); if ($isReturnModuleNames) { if (!isset($data[$moduleName])) { $data[$moduleName] = array(); } $data[$moduleName] = Util::merge($data[$moduleName], $this->loadData($moduleFilePath)); continue; } $data = Util::merge($data, $this->loadData($moduleFilePath)); } } if (!empty($paths['customPath'])) { $data = Util::merge($data, $this->loadData($paths['customPath'])); } return $data; }
/** * Unset some fields and other stuff in metadat * * @param string $key1 * @param string $key2 * @param array | string $unsets Ex. 'fields.name' * * @return bool */ public function delete($key1, $key2, $unsets) { if (!is_array($unsets)) { $unsets = (array) $unsets; } $normalizedData = array('__APPEND__'); $metaUnsetData = array(); foreach ($unsets as $unsetItem) { $normalizedData[] = $unsetItem; $metaUnsetData[] = implode('.', array($key1, $key2, $unsetItem)); } $unsetData = array($key1 => array($key2 => $normalizedData)); $this->deletedData = Util::merge($this->deletedData, $unsetData); $this->deletedData = Util::unsetInArrayByValue('__APPEND__', $this->deletedData); $this->meta = Util::unsetInArray($this->getData(), $metaUnsetData); }
/** * Get custom table defenition in "application/Espo/Core/Utils/Database/Schema/tables/" and in metadata 'additionalTables' * * @param array $ormMeta * * @return array */ protected function getCustomTables(array $ormMeta) { $customTables = array(); $fileList = $this->getFileManager()->getFileList($this->customTablePath, false, '\\.php$', true); foreach ($fileList as $fileName) { $fileData = $this->getFileManager()->getPhpContents(array($this->customTablePath, $fileName)); if (is_array($fileData)) { $customTables = Util::merge($customTables, $fileData); } } //get custom tables from metdata 'additionalTables' foreach ($ormMeta as $entityName => $entityParams) { if (isset($entityParams['additionalTables']) && is_array($entityParams['additionalTables'])) { $customTables = Util::merge($customTables, $entityParams['additionalTables']); } } return $customTables; }
/** * Add all needed block for a field defenition * * @param string $fieldName * @param array $fieldDefs * @param string $scope * @return array */ protected function normalizeDefs($fieldName, array $fieldDefs, $scope) { $fieldDefs = $this->prepareFieldDefs($fieldName, $fieldDefs, $scope); $metaFieldDefs = $this->getMetadataHelper()->getFieldDefsInFieldMeta($fieldDefs); if (isset($metaFieldDefs)) { $fieldDefs = Util::merge($metaFieldDefs, $fieldDefs); } if (isset($fieldDefs['linkDefs'])) { $linkDefs = $fieldDefs['linkDefs']; unset($fieldDefs['linkDefs']); } $defs = array('fields' => array($fieldName => $fieldDefs)); /** Save links for a field. */ $metaLinkDefs = $this->getMetadataHelper()->getLinkDefsInFieldMeta($scope, $fieldDefs); if (isset($linkDefs) || isset($metaLinkDefs)) { $linkDefs = Util::merge((array) $metaLinkDefs, (array) $linkDefs); $defs['links'] = array($fieldName => $linkDefs); } return $defs; }
protected function getInitValues(array $fieldParams) { $values = array(); foreach ($this->fieldAccordances as $espoType => $ormType) { if (isset($fieldParams[$espoType])) { if (is_array($ormType)) { $conditionRes = false; if (!is_array($fieldParams[$espoType])) { $conditionRes = preg_match('/' . $ormType['condition'] . '/i', $fieldParams[$espoType]); } if (!$conditionRes || $conditionRes && $conditionRes === $ormType['conditionEquals']) { $value = is_array($fieldParams[$espoType]) ? json_encode($fieldParams[$espoType]) : $fieldParams[$espoType]; $values = Util::merge($values, Util::replaceInArray('{0}', $value, $ormType['value'])); } } else { $values[$ormType] = $fieldParams[$espoType]; } } } return $values; }
/** * Merge file content and save it to a file * * @param string | array $path * @param string $content JSON string * @param bool $isJSON * @param string | array $mergeOptions * @param string | array $removeOptions - List of unset keys from content * @param bool $isReturn - Is result to be returned or stored * * @return bool | array */ public function mergeContents($path, $content, $isJSON = false, $mergeOptions = null, $removeOptions = null, $isReturn = false) { $fileContent = $this->getContents($path); $savedDataArray = Utils\Json::getArrayData($fileContent); $newDataArray = Utils\Json::getArrayData($content); if (isset($removeOptions)) { $savedDataArray = Utils\Util::unsetInArray($savedDataArray, $removeOptions); $newDataArray = Utils\Util::unsetInArray($newDataArray, $removeOptions); } $data = Utils\Util::merge($savedDataArray, $newDataArray, $mergeOptions); if ($isJSON) { $data = Utils\Json::encode($data, JSON_PRETTY_PRINT); } if ($isReturn) { return $data; } return $this->putContents($path, $data); }
/** * Helpful method for get content from files for unite Files * * @param string | array $paths * @param string | array() $defaults - It can be a string like ["metadata","layouts"] OR an array with default values * * @return array */ protected function unifyGetContents($paths, $defaults) { $fileContent = $this->getFileManager()->getContents($paths); $decoded = Utils\Json::getArrayData($fileContent); if (empty($decoded) && !is_array($decoded)) { $GLOBALS['log']->emergency('Syntax error or empty file - ' . Utils\Util::concatPath($folderPath, $fileName)); } else { //Default values if (is_string($defaults) && !empty($defaults)) { $defType = $defaults; unset($defaults); $name = $this->getFileManager()->getFileName($fileName, '.json'); $defaults = $this->loadDefaultValues($name, $defType); } $mergedValues = Utils\Util::merge($defaults, $decoded); //END: Default values return $mergedValues; } return array(); }
protected function getCustomTables() { $customTables = array(); $fileList = $this->getFileManager()->getFileList($this->customTablePath, false, '\\.php$', 'file'); foreach ($fileList as $fileName) { $fileData = $this->getFileManager()->getContents(array($this->customTablePath, $fileName)); if (is_array($fileData)) { $customTables = Util::merge($customTables, $fileData); } } return $customTables; }
/** * Merge PHP content and save it to a file * * @param string | array $path * @param string $content * @param bool $onlyFirstLevel - Merge only first level. Ex. current: array('test'=>array('item1', 'item2')). $content= array('test'=>array('item1'),). Result will be array('test'=>array('item1')). * * @return bool */ public function mergeContentsPHP($path, $content, $onlyFirstLevel = false, $mergeOptions = null) { $fileContent = $this->getContents($path); $savedDataArray = Utils\Json::getArrayData($fileContent); $newDataArray = Utils\Json::getArrayData($content); if ($onlyFirstLevel) { foreach ($newDataArray as $key => $val) { $setVal = is_array($val) ? array() : ''; $savedDataArray[$key] = $setVal; } } $data = Utils\Util::merge($savedDataArray, $newDataArray, $mergeOptions); return $this->putContentsPHP($path, $data); }
/** * Merge file content and save it to a file * * @param string | array $path * @param string $content JSON string * @param bool $isReturnJson * @param string | array $removeOptions - List of unset keys from content * @param bool $isPhp - Is merge php files * * @return bool | array */ public function mergeContents($path, $content, $isReturnJson = false, $removeOptions = null, $isPhp = false) { if ($isPhp) { $fileContent = $this->getPhpContents($path); } else { $fileContent = $this->getContents($path); } $fullPath = $this->concatPaths($path); if (file_exists($fullPath) && ($fileContent === false || empty($fileContent))) { throw new Error('FileManager: Failed to read file [' . $fullPath . '].'); } $savedDataArray = Utils\Json::getArrayData($fileContent); $newDataArray = Utils\Json::getArrayData($content); if (isset($removeOptions)) { $savedDataArray = Utils\Util::unsetInArray($savedDataArray, $removeOptions); $newDataArray = Utils\Util::unsetInArray($newDataArray, $removeOptions); } $data = Utils\Util::merge($savedDataArray, $newDataArray); if ($isReturnJson) { $data = Utils\Json::encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } if ($isPhp) { return $this->putPhpContents($path, $data); } return $this->putContents($path, $data); }
/** * Add all needed block for a field defenition * * @param string $fieldName * @param array $fieldDef * @param string $scope * @return array */ protected function normalizeDefs($fieldName, array $fieldDef, $scope) { if (isset($fieldDef['name'])) { unset($fieldDef['name']); } if (isset($fieldDef['linkDefs'])) { $linkDefs = $fieldDef['linkDefs']; unset($fieldDef['linkDefs']); } foreach ($fieldDef as $defName => $defValue) { if (!isset($defValue) || is_string($defValue) && $defValue == '') { unset($fieldDef[$defName]); } } $metaFieldDef = $this->getMetadataUtils()->getFieldDefsInFieldMeta($fieldDef); if (isset($metaFieldDef)) { $fieldDef = Util::merge($metaFieldDef, $fieldDef); } $defs = array('fields' => array($fieldName => $fieldDef)); /** Save links for a field. */ $metaLinkDef = $this->getMetadataUtils()->getLinkDefsInFieldMeta($scope, $fieldDef); if (isset($linkDefs) || isset($metaLinkDef)) { $linkDefs = Util::merge((array) $metaLinkDef, (array) $linkDefs); $defs['links'] = array($fieldName => $linkDefs); } return $defs; }
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; foreach ($this->fullData as $i18nName => $i18nData) { $i18nCacheFile = str_replace('{*}', $i18nName, $this->cacheFile); if ($i18nName != $this->defaultLanguage) { $i18nData = Util::merge($this->fullData[$this->defaultLanguage], $i18nData); } $result &= $this->getFileManager()->putContentsPHP($i18nCacheFile, $i18nData); } if ($result == false) { throw new Error('Language::init() - Cannot save data to a cache'); } } $this->data = $this->getFileManager()->getContents($this->getLangCacheFile()); }
private function getAllowedAdditionalParams($allowedItemName) { $linkParams = $this->getLinkParams(); $foreignLinkParams = $this->getForeignLinkParams(); $itemLinkParams = isset($linkParams[$allowedItemName]) ? $linkParams[$allowedItemName] : null; $itemForeignLinkParams = isset($foreignLinkParams[$allowedItemName]) ? $foreignLinkParams[$allowedItemName] : null; $additionalParrams = null; if (isset($itemLinkParams) && isset($itemForeignLinkParams)) { $additionalParrams = Util::merge($itemLinkParams, $itemForeignLinkParams); } else { if (isset($itemLinkParams)) { $additionalParrams = $itemLinkParams; } else { if (isset($itemForeignLinkParams)) { $additionalParrams = $itemForeignLinkParams; } } } return $additionalParrams; }
public function testMergeCompleteTest() { $currentArray = array('fields' => array('aaa1' => array('type' => 'enum', 'required' => false, 'options' => array(0 => 'a1', 1 => 'a3', 2 => 'a3'), 'isCustom' => true), 'append' => array('type' => 'enum', 'required' => false, 'options' => array('b1', 'b3', 'b3')), 't1111' => array('type' => 'varchar', 'required' => false, 'isCustom' => true, 'default' => '11111'), 't2222' => array('type' => 'varchar', 'required' => false, 'isCustom' => true, 'default' => '2222'), 't3333' => array('type' => 'varchar', 'required' => false, 'isCustom' => true, 'default' => '3333', 'maxLength' => 70))); $newArray = array('fields' => array('aaa1' => array('type' => 'enum', 'required' => false, 'options' => array('a1'), 'isCustom' => false, 'newValue' => 'NNNNN'), 'new111' => array('type' => 'varchar', 'required' => false), 'append' => array('type' => 'enum', 'required' => false, 'options' => array('__APPEND__', 'b4', 'b5')), 'aloneAppend' => array('type' => 'enum', 'required' => false, 'options' => array('__APPEND__', 'c1', 'c2')))); $result = array('fields' => array('aaa1' => array('type' => 'enum', 'required' => false, 'options' => array(0 => 'a1'), 'isCustom' => false, 'newValue' => 'NNNNN'), 'append' => array('type' => 'enum', 'required' => false, 'options' => array('b1', 'b3', 'b3', 'b4', 'b5')), 't1111' => array('type' => 'varchar', 'required' => false, 'isCustom' => true, 'default' => '11111'), 't2222' => array('type' => 'varchar', 'required' => false, 'isCustom' => true, 'default' => '2222'), 't3333' => array('type' => 'varchar', 'required' => false, 'isCustom' => true, 'default' => '3333', 'maxLength' => 70), 'new111' => array('type' => 'varchar', 'required' => false), 'aloneAppend' => array('type' => 'enum', 'required' => false, 'options' => array('c1', 'c2')))); $this->assertEquals($result, Util::merge($currentArray, $newArray)); }
private function getAllowedAdditionalParam($allowedItemName) { $linkParams = $this->getLinkParams(); $foreignLinkParams = $this->getForeignLinkParams(); $itemLinkParams = isset($linkParams[$allowedItemName]) ? $linkParams[$allowedItemName] : null; $itemForeignLinkParams = isset($foreignLinkParams[$allowedItemName]) ? $foreignLinkParams[$allowedItemName] : null; $additionalParam = null; $linkName = $this->getLinkName(); $entityName = $this->getEntityName(); if (isset($itemLinkParams) && isset($itemForeignLinkParams)) { if (!empty($itemLinkParams) && !is_array($itemLinkParams)) { $additionalParam = $itemLinkParams; } else { if (!empty($itemForeignLinkParams) && !is_array($itemForeignLinkParams)) { $additionalParam = $itemForeignLinkParams; } else { $additionalParam = Util::merge($itemLinkParams, $itemForeignLinkParams); } } } else { if (isset($itemLinkParams)) { $additionalParam = $itemLinkParams; } else { if (isset($itemForeignLinkParams)) { $additionalParam = $itemForeignLinkParams; } } } return $additionalParam; }