| @@ 51-172 (lines=122) @@ | ||
| 48 | * @throws ParentEntityNotFound |
|
| 49 | * @throws \samsoncms\api\exception\AdditionalFieldTypeNotFound |
|
| 50 | */ |
|
| 51 | public function analyze($type, $filter = null) |
|
| 52 | { |
|
| 53 | $metadataCollection = []; |
|
| 54 | ||
| 55 | // Iterate all structures, parents first |
|
| 56 | foreach ($this->entityNavigations($type) as $structureRow) { |
|
| 57 | // If filter is the function and filter return false then skip this structure |
|
| 58 | if (is_callable($filter) && (false === $filter($structureRow))) { |
|
| 59 | continue; |
|
| 60 | } |
|
| 61 | ||
| 62 | // Fill in entity metadata |
|
| 63 | $metadata = new Metadata($type); |
|
| 64 | ||
| 65 | // Get CapsCase and transliterated entity name |
|
| 66 | $metadata->entity = $this->entityName($structureRow['Name']); |
|
| 67 | $metadata->entityClassName = $this->fullEntityName($metadata->entity); |
|
| 68 | $metadata->entityRealName = $structureRow['Name']; |
|
| 69 | $metadata->entityID = $structureRow['StructureID']; |
|
| 70 | ||
| 71 | // Try to find entity parent identifier for building future relations |
|
| 72 | $metadata->parentID = $this->entityParent($structureRow['StructureID']); |
|
| 73 | // Generate application from current entity |
|
| 74 | $metadata->generateApplication = $structureRow['applicationGenerate']; |
|
| 75 | // Show application from current entity |
|
| 76 | $metadata->showApplication = $structureRow['applicationOutput']; |
|
| 77 | // Icon for application from current entity |
|
| 78 | $metadata->iconApplication = $structureRow['applicationIcon']; |
|
| 79 | // Render application on main page |
|
| 80 | $metadata->renderMainApplication = $structureRow['applicationRenderMain']; |
|
| 81 | ||
| 82 | // TODO: Add multiple parent and fetching their data in a loop |
|
| 83 | ||
| 84 | // Set pointer to parent entity |
|
| 85 | if (null !== $metadata->parentID && $metadata->type !== Metadata::TYPE_TABLE) { |
|
| 86 | if (array_key_exists($metadata->parentID, $this->metadata)) { |
|
| 87 | $metadata->parent = $this->metadata[$metadata->parentID]; |
|
| 88 | // Add all parent metadata to current object |
|
| 89 | $metadata->defaultValues = $metadata->parent->defaultValues; |
|
| 90 | $metadata->realNames = $metadata->parent->realNames; |
|
| 91 | $metadata->allFieldIDs = $metadata->parent->allFieldIDs; |
|
| 92 | $metadata->allFieldNames = $metadata->parent->allFieldNames; |
|
| 93 | $metadata->allFieldValueColumns = $metadata->parent->allFieldValueColumns; |
|
| 94 | $metadata->allFieldTypes = $metadata->parent->allFieldTypes; |
|
| 95 | $metadata->fieldDescriptions = $metadata->parent->fieldDescriptions; |
|
| 96 | $metadata->localizedFieldIDs = $metadata->parent->localizedFieldIDs; |
|
| 97 | $metadata->notLocalizedFieldIDs = $metadata->parent->notLocalizedFieldIDs; |
|
| 98 | } else { |
|
| 99 | throw new ParentEntityNotFound($metadata->parentID); |
|
| 100 | } |
|
| 101 | } |
|
| 102 | ||
| 103 | // Get old AR collections of metadata |
|
| 104 | $metadata->arSelect = \samson\activerecord\material::$_sql_select; |
|
| 105 | $metadata->arAttributes = \samson\activerecord\material::$_attributes; |
|
| 106 | $metadata->arMap = \samson\activerecord\material::$_map; |
|
| 107 | $metadata->arFrom = \samson\activerecord\material::$_sql_from; |
|
| 108 | $metadata->arGroup = \samson\activerecord\material::$_own_group; |
|
| 109 | $metadata->arRelationAlias = \samson\activerecord\material::$_relation_alias; |
|
| 110 | $metadata->arRelationType = \samson\activerecord\material::$_relation_type; |
|
| 111 | $metadata->arRelations = \samson\activerecord\material::$_relations; |
|
| 112 | ||
| 113 | // Add SamsonCMS material needed data |
|
| 114 | $metadata->arSelect['this'] = ' STRAIGHT_JOIN ' . $metadata->arSelect['this']; |
|
| 115 | $metadata->arFrom['this'] .= "\n" . |
|
| 116 | 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf |
|
| 117 | ON ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
|
| 118 | $metadata->arGroup[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
|
| 119 | ||
| 120 | // Iterate entity fields |
|
| 121 | foreach ($this->navigationFields($structureRow['StructureID']) as $fieldID => $fieldRow) { |
|
| 122 | // Get camelCase and transliterated field name |
|
| 123 | $fieldName = $this->fieldName($fieldRow['Name']); |
|
| 124 | ||
| 125 | // TODO: Set default for additional field storing type accordingly. |
|
| 126 | ||
| 127 | // Store field metadata |
|
| 128 | $metadata->realNames[$fieldRow['Name']] = $fieldName; |
|
| 129 | $metadata->allFieldIDs[$fieldID] = $fieldName; |
|
| 130 | $metadata->allFieldNames[$fieldName] = $fieldID; |
|
| 131 | $metadata->allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]); |
|
| 132 | $metadata->allFieldTypes[$fieldID] = Field::phpType($fieldRow['Type']); |
|
| 133 | $metadata->allFieldCmsTypes[$fieldID] = (int)$fieldRow['Type']; |
|
| 134 | $metadata->fieldDescriptions[$fieldID] = $fieldRow['Description'] . ', ' . $fieldRow['Name'] . '#' . $fieldID; |
|
| 135 | $metadata->fieldRawDescriptions[$fieldID] = $fieldRow['Description']; |
|
| 136 | ||
| 137 | // Fill localization fields collections |
|
| 138 | if ($fieldRow[Field::F_LOCALIZED] == 1) { |
|
| 139 | $metadata->localizedFieldIDs[$fieldID] = $fieldName; |
|
| 140 | } else { |
|
| 141 | $metadata->notLocalizedFieldIDs[$fieldID] = $fieldName; |
|
| 142 | } |
|
| 143 | ||
| 144 | // Fill all fields which should display in list |
|
| 145 | if ($fieldRow['showInList'] == 1) { |
|
| 146 | $metadata->showFieldsInList[] = $fieldID; |
|
| 147 | } |
|
| 148 | ||
| 149 | // Save custom type |
|
| 150 | $metadata->customTypeFields[$fieldID] = $fieldRow['customTypeName']; |
|
| 151 | ||
| 152 | // Set old AR collections of metadata |
|
| 153 | $metadata->arAttributes[$fieldName] = $fieldName; |
|
| 154 | $metadata->arMap[$fieldName] = dbMySQLConnector::$prefix . 'material.' . $fieldName; |
|
| 155 | ||
| 156 | // Add additional field column to entity query |
|
| 157 | $equal = '((_mf.FieldID = ' . $fieldID . ')&&(_mf.locale ' . ($fieldRow['local'] ? ' = "@locale"' : 'IS NULL') . '))'; |
|
| 158 | $metadata->arSelect['this'] .= "\n\t\t" . ',MAX(IF(' . $equal . ', _mf.`' . Field::valueColumn($fieldRow['Type']) . '`, NULL)) as `' . $fieldName . '`'; |
|
| 159 | } |
|
| 160 | ||
| 161 | // Get id of child navigation |
|
| 162 | foreach ($this->entityChildNavigation($structureRow['StructureID']) as $childNavigation) { |
|
| 163 | $metadata->childNavigationIDs[] = $childNavigation['StructureID']; |
|
| 164 | } |
|
| 165 | ||
| 166 | // Store metadata by entity identifier |
|
| 167 | $metadataCollection[$structureRow['StructureID']] = $metadata; |
|
| 168 | } |
|
| 169 | ||
| 170 | ||
| 171 | return $metadataCollection; |
|
| 172 | } |
|
| 173 | ||
| 174 | /** @return array Get collection of navigation objects */ |
|
| 175 | protected function entityNavigations($type = 0) |
|
| @@ 50-172 (lines=123) @@ | ||
| 47 | * @throws ParentEntityNotFound |
|
| 48 | * @throws \samsoncms\api\exception\AdditionalFieldTypeNotFound |
|
| 49 | */ |
|
| 50 | public function fillMetadata($type, $filter = null) |
|
| 51 | { |
|
| 52 | $metadataCollection = []; |
|
| 53 | ||
| 54 | // Iterate all structures, parents first |
|
| 55 | foreach ($this->entityNavigations($type) as $structureRow) { |
|
| 56 | ||
| 57 | // If filter is the function and filter return false then skip this structure |
|
| 58 | if (is_callable($filter) && (false === $filter($structureRow))) { |
|
| 59 | continue; |
|
| 60 | } |
|
| 61 | ||
| 62 | // Fill in entity metadata |
|
| 63 | $metadata = new Metadata($type); |
|
| 64 | ||
| 65 | // Get CapsCase and transliterated entity name |
|
| 66 | $metadata->entity = $this->entityName($structureRow['Name']); |
|
| 67 | $metadata->entityClassName = $this->fullEntityName($metadata->entity); |
|
| 68 | $metadata->entityRealName = $structureRow['Name']; |
|
| 69 | $metadata->entityID = $structureRow['StructureID']; |
|
| 70 | ||
| 71 | // Try to find entity parent identifier for building future relations |
|
| 72 | $metadata->parentID = $this->entityParent($structureRow['StructureID']); |
|
| 73 | // Generate application from current entity |
|
| 74 | $metadata->generateApplication = $structureRow['applicationGenerate']; |
|
| 75 | // Show application from current entity |
|
| 76 | $metadata->showApplication = $structureRow['applicationOutput']; |
|
| 77 | // Icon for application from current entity |
|
| 78 | $metadata->iconApplication = $structureRow['applicationIcon']; |
|
| 79 | // Render application on main page |
|
| 80 | $metadata->renderMainApplication = $structureRow['applicationRenderMain']; |
|
| 81 | ||
| 82 | // TODO: Add multiple parent and fetching their data in a loop |
|
| 83 | ||
| 84 | // Set pointer to parent entity |
|
| 85 | if (null !== $metadata->parentID && $metadata->type !== Metadata::TYPE_TABLE) { |
|
| 86 | if (array_key_exists($metadata->parentID, $this->metadata)) { |
|
| 87 | $metadata->parent = $this->metadata[$metadata->parentID]; |
|
| 88 | // Add all parent metadata to current object |
|
| 89 | $metadata->defaultValues = $metadata->parent->defaultValues; |
|
| 90 | $metadata->realNames = $metadata->parent->realNames; |
|
| 91 | $metadata->allFieldIDs = $metadata->parent->allFieldIDs; |
|
| 92 | $metadata->allFieldNames = $metadata->parent->allFieldNames; |
|
| 93 | $metadata->allFieldValueColumns = $metadata->parent->allFieldValueColumns; |
|
| 94 | $metadata->allFieldTypes = $metadata->parent->allFieldTypes; |
|
| 95 | $metadata->fieldDescriptions = $metadata->parent->fieldDescriptions; |
|
| 96 | $metadata->localizedFieldIDs = $metadata->parent->localizedFieldIDs; |
|
| 97 | $metadata->notLocalizedFieldIDs = $metadata->parent->notLocalizedFieldIDs; |
|
| 98 | } else { |
|
| 99 | throw new ParentEntityNotFound($metadata->parentID); |
|
| 100 | } |
|
| 101 | } |
|
| 102 | ||
| 103 | // Get old AR collections of metadata |
|
| 104 | $metadata->arSelect = \samson\activerecord\material::$_sql_select; |
|
| 105 | $metadata->arAttributes = \samson\activerecord\material::$_attributes; |
|
| 106 | $metadata->arMap = \samson\activerecord\material::$_map; |
|
| 107 | $metadata->arFrom = \samson\activerecord\material::$_sql_from; |
|
| 108 | $metadata->arGroup = \samson\activerecord\material::$_own_group; |
|
| 109 | $metadata->arRelationAlias = \samson\activerecord\material::$_relation_alias; |
|
| 110 | $metadata->arRelationType = \samson\activerecord\material::$_relation_type; |
|
| 111 | $metadata->arRelations = \samson\activerecord\material::$_relations; |
|
| 112 | ||
| 113 | // Add SamsonCMS material needed data |
|
| 114 | $metadata->arSelect['this'] = ' STRAIGHT_JOIN ' . $metadata->arSelect['this']; |
|
| 115 | $metadata->arFrom['this'] .= "\n" . |
|
| 116 | 'LEFT JOIN ' . dbMySQLConnector::$prefix . 'materialfield as _mf |
|
| 117 | ON ' . dbMySQLConnector::$prefix . 'material.MaterialID = _mf.MaterialID'; |
|
| 118 | $metadata->arGroup[] = dbMySQLConnector::$prefix . 'material.MaterialID'; |
|
| 119 | ||
| 120 | // Iterate entity fields |
|
| 121 | foreach ($this->navigationFields($structureRow['StructureID']) as $fieldID => $fieldRow) { |
|
| 122 | // Get camelCase and transliterated field name |
|
| 123 | $fieldName = $this->fieldName($fieldRow['Name']); |
|
| 124 | ||
| 125 | // TODO: Set default for additional field storing type accordingly. |
|
| 126 | ||
| 127 | // Store field metadata |
|
| 128 | $metadata->realNames[$fieldRow['Name']] = $fieldName; |
|
| 129 | $metadata->allFieldIDs[$fieldID] = $fieldName; |
|
| 130 | $metadata->allFieldNames[$fieldName] = $fieldID; |
|
| 131 | $metadata->allFieldValueColumns[$fieldID] = Field::valueColumn($fieldRow[Field::F_TYPE]); |
|
| 132 | $metadata->allFieldTypes[$fieldID] = Field::phpType($fieldRow['Type']); |
|
| 133 | $metadata->allFieldCmsTypes[$fieldID] = (int)$fieldRow['Type']; |
|
| 134 | $metadata->fieldDescriptions[$fieldID] = $fieldRow['Description'] . ', ' . $fieldRow['Name'] . '#' . $fieldID; |
|
| 135 | $metadata->fieldRawDescriptions[$fieldID] = $fieldRow['Description']; |
|
| 136 | ||
| 137 | // Fill localization fields collections |
|
| 138 | if ($fieldRow[Field::F_LOCALIZED] == 1) { |
|
| 139 | $metadata->localizedFieldIDs[$fieldID] = $fieldName; |
|
| 140 | } else { |
|
| 141 | $metadata->notLocalizedFieldIDs[$fieldID] = $fieldName; |
|
| 142 | } |
|
| 143 | ||
| 144 | // Fill all fields which should display in list |
|
| 145 | if ($fieldRow['showInList'] == 1) { |
|
| 146 | $metadata->showFieldsInList[] = $fieldID; |
|
| 147 | } |
|
| 148 | ||
| 149 | // Save custom type |
|
| 150 | $metadata->customTypeFields[$fieldID] = $fieldRow['customTypeName']; |
|
| 151 | ||
| 152 | // Set old AR collections of metadata |
|
| 153 | $metadata->arAttributes[$fieldName] = $fieldName; |
|
| 154 | $metadata->arMap[$fieldName] = dbMySQLConnector::$prefix . 'material.' . $fieldName; |
|
| 155 | ||
| 156 | // Add additional field column to entity query |
|
| 157 | $equal = '((_mf.FieldID = ' . $fieldID . ')&&(_mf.locale ' . ($fieldRow['local'] ? ' = "@locale"' : 'IS NULL') . '))'; |
|
| 158 | $metadata->arSelect['this'] .= "\n\t\t" . ',MAX(IF(' . $equal . ', _mf.`' . Field::valueColumn($fieldRow['Type']) . '`, NULL)) as `' . $fieldName . '`'; |
|
| 159 | } |
|
| 160 | ||
| 161 | // Get id of child navigation |
|
| 162 | foreach ($this->entityChildNavigation($structureRow['StructureID']) as $childNavigation) { |
|
| 163 | $metadata->childNavigationIDs[] = $childNavigation['StructureID']; |
|
| 164 | } |
|
| 165 | ||
| 166 | // Store metadata by entity identifier |
|
| 167 | $metadataCollection[$structureRow['StructureID']] = $metadata; |
|
| 168 | } |
|
| 169 | ||
| 170 | ||
| 171 | return $metadataCollection; |
|
| 172 | } |
|
| 173 | ||
| 174 | /** @return array Get collection of navigation objects */ |
|
| 175 | protected function entityNavigations($type = 0) |
|