/** * 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; }
/** * Saves a block type to the database. * * @param Neo_BlockTypeModel $blockType * @param bool|true $validate * @return bool * @throws \Exception */ public function saveBlockType(Neo_BlockTypeModel $blockType, $validate = true) { if (!$validate || $this->validateBlockType($blockType)) { // Save this for use by plugins (eg. Reasons) $this->currentSavingBlockType = $blockType; $transaction = $this->beginTransaction(); try { // Get the block type record $blockTypeRecord = $this->_getBlockTypeRecord($blockType); $isNewBlockType = $blockType->isNew(); $oldBlockType = $isNewBlockType ? null : Neo_BlockTypeModel::populateModel($blockTypeRecord); // Is there a new field layout? $fieldLayout = $blockType->getFieldLayout(); if (!$fieldLayout->id) { // Delete the old one if (!$isNewBlockType && $oldBlockType->fieldLayoutId) { craft()->fields->deleteLayoutById($oldBlockType->fieldLayoutId); } // Save the new one craft()->fields->saveLayout($fieldLayout); // Update the entry type record/model with the new layout ID $blockType->fieldLayoutId = $fieldLayout->id; $blockTypeRecord->fieldLayoutId = $fieldLayout->id; } // Set the basic info on the new block type record $blockTypeRecord->fieldId = $blockType->fieldId; $blockTypeRecord->name = $blockType->name; $blockTypeRecord->handle = $blockType->handle; $blockTypeRecord->sortOrder = $blockType->sortOrder; $blockTypeRecord->maxBlocks = $blockType->maxBlocks; $blockTypeRecord->childBlocks = $blockType->childBlocks; $blockTypeRecord->topLevel = $blockType->topLevel; // Save it, minus the field layout for now $blockTypeRecord->save(false); if ($isNewBlockType) { // Set the new ID on the model $blockType->id = $blockTypeRecord->id; } // Update the block type with the field layout ID $blockTypeRecord->save(false); $this->commitTransaction($transaction); } catch (\Exception $e) { $this->rollbackTransaction($transaction); throw $e; } // Dereference the block type just for good measure $this->currentSavingBlockType = null; return true; } return false; }