metadata = $metadata; $this->language = $language; $this->container = $container; $this->metadataHelper = new \Espo\Core\Utils\Metadata\Helper($this->metadata); } protected function getMetadata() { return $this->metadata; } protected function getLanguage() { return $this->language; } protected function getMetadataHelper() { return $this->metadataHelper; } public function read($name, $scope) { $fieldDefs = $this->getFieldDefs($name, $scope); $fieldDefs['label'] = $this->getLanguage()->translate($name, 'fields', $scope); $type = $this->getMetadata()->get(['entityDefs', $scope, 'fields', $name, 'type']); $this->processHook('onRead', $type, $scope, $name, $fieldDefs); return $fieldDefs; } public function create($name, $fieldDefs, $scope) { $existingField = $this->getFieldDefs($name, $scope); if (isset($existingField)) { throw new Conflict('Field ['.$name.'] exists in '.$scope); } return $this->update($name, $fieldDefs, $scope); } public function update($name, $fieldDefs, $scope) { $name = trim($name); /*Add option to metadata to identify the custom field*/ if (!$this->isCore($name, $scope)) { $fieldDefs[$this->customOptionName] = true; } $res = true; if (isset($fieldDefs['label'])) { $this->setLabel($name, $fieldDefs['label'], $scope); } $type = isset($fieldDefs['type']) ? $fieldDefs['type'] : $type = $this->getMetadata()->get(['entityDefs', $scope, 'fields', $name, 'type']); $this->processHook('beforeSave', $type, $scope, $name, $fieldDefs); if ($this->getMetadata()->get(['fields', $type, 'translatedOptions'])) { if (isset($fieldDefs['translatedOptions'])) { $this->setTranslatedOptions($name, $fieldDefs['translatedOptions'], $scope); } } if (isset($fieldDefs['label']) || isset($fieldDefs['translatedOptions'])) { $res &= $this->getLanguage()->save(); $this->processHook('afterSave', $type, $scope, $name, $fieldDefs); } if ($this->isDefsChanged($name, $fieldDefs, $scope)) { $res &= $this->setEntityDefs($name, $fieldDefs, $scope); } return (bool) $res; } public function delete($name, $scope) { if ($this->isCore($name, $scope)) { throw new Error('Cannot delete core field ['.$name.'] in '.$scope); } $type = $this->getMetadata()->get(['entityDefs', $scope, 'fields', $name, 'type']); $this->processHook('beforeRemove', $type, $scope, $name); $unsets = array( 'fields.'.$name, 'links.'.$name, ); $this->getMetadata()->delete($this->metadataType, $scope, $unsets); $res = $this->getMetadata()->save(); $res &= $this->deleteLabel($name, $scope); $this->processHook('afterRemove', $type, $scope, $name); return (bool) $res; } protected function setEntityDefs($name, $fieldDefs, $scope) { $fieldDefs = $this->normalizeDefs($name, $fieldDefs, $scope); $this->getMetadata()->set($this->metadataType, $scope, $fieldDefs); $res = $this->getMetadata()->save(); return $res; } protected function setTranslatedOptions($name, $value, $scope) { return $this->getLanguage()->set($scope, 'options', $name, $value); } protected function setLabel($name, $value, $scope) { return $this->getLanguage()->set($scope, 'fields', $name, $value); } protected function deleteLabel($name, $scope) { $this->getLanguage()->delete($scope, 'fields', $name); $this->getLanguage()->delete($scope, 'options', $name); return $this->getLanguage()->save(); } protected function getFieldDefs($name, $scope) { return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.fields.'.$name); } protected function getLinkDefs($name, $scope) { return $this->getMetadata()->get($this->metadataType.'.'.$scope.'.links.'.$name); } /** * Prepare input fieldDefs, remove unnecessary fields * * @param string $fieldName * @param array $fieldDefs * @param string $scope * @return array */ protected function prepareFieldDefs($name, $fieldDefs, $scope) { $unnecessaryFields = array( 'name', 'label', 'translatedOptions', ); foreach ($unnecessaryFields as $fieldName) { if (isset($fieldDefs[$fieldName])) { unset($fieldDefs[$fieldName]); } } $currentOptionList = array_keys((array) $this->getFieldDefs($name, $scope)); foreach ($fieldDefs as $defName => $defValue) { if ( (!isset($defValue) || $defValue === '') && !in_array($defName, $currentOptionList) ) { unset($fieldDefs[$defName]); } } return $fieldDefs; } /** * 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; } /** * Check if changed metadata defenition for a field except 'label' * * @return boolean */ protected function isDefsChanged($name, $fieldDefs, $scope) { $fieldDefs = $this->prepareFieldDefs($name, $fieldDefs, $scope); $currentFieldDefs = $this->getFieldDefs($name, $scope); $this->isChanged = Util::isEquals($fieldDefs, $currentFieldDefs) ? false : true; return $this->isChanged; } /** * Only for update method * * @return boolean */ public function isChanged() { return $this->isChanged; } /** * Check if a field is core field * * @param string $name * @param string $scope * @return boolean */ protected function isCore($name, $scope) { $existingField = $this->getFieldDefs($name, $scope); if (isset($existingField) && (!isset($existingField[$this->customOptionName]) || !$existingField[$this->customOptionName])) { return true; } return false; } private function getAttributeListByType($scope, $name, $type) { $fieldType = $this->getMetadata()->get('entityDefs.' . $scope . '.fields.' . $name . '.type'); if (!$fieldType) return []; $defs = $this->getMetadata()->get('fields.' . $fieldType); if (!$defs) return []; if (is_object($defs)) { $defs = get_object_vars($defs); } $fieldList = []; if (isset($defs[$type . 'Fields'])) { $list = $defs[$type . 'Fields']; $naming = 'suffix'; if (isset($defs['naming'])) { $naming = $defs['naming']; } if ($naming == 'prefix') { foreach ($list as $f) { $fieldList[] = $f . ucfirst($name); } } else { foreach ($list as $f) { $fieldList[] = $name . ucfirst($f); } } } else { if ($type == 'actual') { $fieldList[] = $name; } } return $fieldList; } public function getActualAttributeList($scope, $name) { return $this->getAttributeListByType($scope, $name, 'actual'); } public function getNotActualAttributeList($scope, $name) { return $this->getAttributeListByType($scope, $name, 'notActual'); } public function getAttributeList($scope, $name) { return array_merge($this->getActualAttributeList($scope, $name), $this->getNotActualAttributeList($scope, $name)); } protected function processHook($methodName, $type, $scope, $name, &$defs = null) { $hook = $this->getHook($type); if (!$hook) return; if (!method_exists($hook, $methodName)) return; $hook->$methodName($scope, $name, $defs); } protected function getHook($type) { $className = $this->getMetadata()->get(['fields', $type, 'hookClassName']); if (!$className) return; if (class_exists($className)) { $hook = new $className(); foreach ($hook->getDependencyList() as $name) { $hook->inject($name, $this->container->get($name)); } return $hook; } $GLOBALS['log']->error("Field Manager hook class '{$className}' does not exist."); return; } }