/** * Prepares the Neo field settings before they're saved to the database. * Handles preparing block types, field layouts and groups. * * @param array $settings * @return Neo_SettingsModel */ public function prepSettings($settings) { if ($settings instanceof Neo_SettingsModel) { return $settings; } $neoSettings = new Neo_SettingsModel(); $neoSettings->setField($this->model); $blockTypes = []; $groups = []; if (!empty($settings['blockTypes'])) { foreach ($settings['blockTypes'] as $blockTypeId => $blockTypeSettings) { $blockType = new Neo_BlockTypeModel(); $blockType->id = $blockTypeId; $blockType->fieldId = $this->model->id; $blockType->name = $blockTypeSettings['name']; $blockType->handle = $blockTypeSettings['handle']; $blockType->maxBlocks = $blockTypeSettings['maxBlocks']; $blockType->sortOrder = $blockTypeSettings['sortOrder']; $blockType->childBlocks = $blockTypeSettings['childBlocks']; $blockType->topLevel = (bool) $blockTypeSettings['topLevel']; if (!empty($blockTypeSettings['fieldLayout'])) { $fieldLayoutPost = $blockTypeSettings['fieldLayout']; $requiredFieldPost = empty($blockTypeSettings['requiredFields']) ? [] : $blockTypeSettings['requiredFields']; // Add support for blank tabs foreach ($fieldLayoutPost as $tabName => $fieldIds) { if (!is_array($fieldIds)) { $fieldLayoutPost[$tabName] = []; } } $fieldLayout = craft()->fields->assembleLayout($fieldLayoutPost, $requiredFieldPost); $fieldLayout->type = Neo_ElementType::NeoBlock; $blockType->setFieldLayout($fieldLayout); } $blockTypes[] = $blockType; } } if (!empty($settings['groups'])) { $names = $settings['groups']['name']; $sortOrders = $settings['groups']['sortOrder']; for ($i = 0; $i < count($names); $i++) { $group = new Neo_GroupModel(); $group->name = $names[$i]; $group->sortOrder = $sortOrders[$i]; $groups[] = $group; } } $neoSettings->setBlockTypes($blockTypes); $neoSettings->setGroups($groups); if (!empty($settings['maxBlocks'])) { $neoSettings->maxBlocks = $settings['maxBlocks']; } return $neoSettings; }
/** * Returns the conditionals just for Neo block types. * * @return array */ public function getConditionals() { craft()->neo->requirePlugin('reasons'); // TODO Reduce database impact $blockTypeConditionals = []; $sources = []; $neoBlockTypeRecords = Neo_BlockTypeRecord::model()->findAll(); if ($neoBlockTypeRecords) { foreach ($neoBlockTypeRecords as $neoBlockTypeRecord) { $neoBlockType = Neo_BlockTypeModel::populateModel($neoBlockTypeRecord); $sources[$neoBlockType->id] = $neoBlockType->fieldLayoutId; } } $conditionals = []; $conditionalsRecords = Reasons_ConditionalsRecord::model()->findAll(); if ($conditionalsRecords) { foreach ($conditionalsRecords as $conditionalsRecord) { $conditionalsModel = Reasons_ConditionalsModel::populateModel($conditionalsRecord); if ($conditionalsModel->conditionals && $conditionalsModel->conditionals != '') { $conditionals[$conditionalsModel->fieldLayoutId] = $conditionalsModel->conditionals; } } } foreach ($sources as $blockTypeId => $fieldLayoutId) { if (isset($conditionals[$fieldLayoutId])) { $blockTypeConditionals[$blockTypeId] = $conditionals[$fieldLayoutId]; } } return $blockTypeConditionals; }
/** * Finds and returns the block type record from a block type model. * If the block type is just newly created, a fresh record will be returned. * * @param Neo_BlockTypeModel $blockType * @return Neo_BlockTypeRecord * @throws Exception */ private function _getBlockTypeRecord(Neo_BlockTypeModel $blockType) { if (!$blockType->isNew()) { $blockTypeId = $blockType->id; if (!isset($this->_blockTypeRecordsById) || !array_key_exists($blockTypeId, $this->_blockTypeRecordsById)) { $this->_blockTypeRecordsById[$blockTypeId] = Neo_BlockTypeRecord::model()->findById($blockTypeId); if (!$this->_blockTypeRecordsById[$blockTypeId]) { throw new Exception(Craft::t('No block type exists with the ID “{id}”.', ['id' => $blockTypeId])); } } return $this->_blockTypeRecordsById[$blockTypeId]; } return new Neo_BlockTypeRecord(); }