public function saveConditionals(Reasons_ConditionalsModel $conditionalsModel) { if ($conditionalsRecord = Reasons_ConditionalsRecord::model()->findByAttributes(array('typeId' => $conditionalsModel->typeId))) { $conditionalsModel->id = $conditionalsRecord->id; } else { $conditionalsRecord = new Reasons_ConditionalsRecord(); } $conditionalsRecord->sectionId = $conditionalsModel->sectionId; $conditionalsRecord->typeId = $conditionalsModel->typeId; $conditionalsRecord->conditionals = $conditionalsModel->conditionals; $conditionalsRecord->validate(); $conditionalsModel->addErrors($conditionalsRecord->getErrors()); if (!$conditionalsModel->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { if (!$conditionalsModel->id) { $conditionalsRecord->save(); } else { $conditionalsRecord->update(); } $conditionalsModel->id = $conditionalsRecord->id; if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } return true; } return false; }
/** * 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; }
/** * @param Reasons_ConditionalsModel $model * @return bool * @throws \Exception */ public function saveConditionals(Reasons_ConditionalsModel $model) { $record = new Reasons_ConditionalsRecord(); $record->fieldLayoutId = $model->fieldLayoutId; $record->conditionals = $model->conditionals; $record->validate(); $model->addErrors($record->getErrors()); if (!$model->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { $record->save(); if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } return true; } return false; }
/** * @return array */ protected function getConditionals() { $r = array(); $sources = array(); // Entry types $entryTypeRecords = EntryTypeRecord::model()->findAll(); if ($entryTypeRecords) { foreach ($entryTypeRecords as $entryTypeRecord) { $entryType = EntryTypeModel::populateModel($entryTypeRecord); $sources['entryType:' . $entryType->id] = $entryType->fieldLayoutId; $sources['section:' . $entryType->sectionId] = $entryType->fieldLayoutId; } } // Category groups $allCategoryGroups = craft()->categories->getAllGroups(); foreach ($allCategoryGroups as $categoryGroup) { $sources['categoryGroup:' . $categoryGroup->id] = $categoryGroup->fieldLayoutId; } // Tag groups $allTagGroups = craft()->tags->getAllTagGroups(); foreach ($allTagGroups as $tagGroup) { $sources['tagGroup:' . $tagGroup->id] = $tagGroup->fieldLayoutId; } // Asset sources $allAssetSources = craft()->assetSources->getAllSources(); foreach ($allAssetSources as $assetSource) { $sources['assetSource:' . $assetSource->id] = $assetSource->fieldLayoutId; } // Global sets $allGlobalSets = craft()->globals->getAllSets(); foreach ($allGlobalSets as $globalSet) { $sources['globalSet:' . $globalSet->id] = $globalSet->fieldLayoutId; } // Matrix blocks – TODO // $matrixBlockTypeRecords = MatrixBlockTypeRecord::model()->findAll(); // if ($matrixBlockTypeRecords) { // foreach ($matrixBlockTypeRecords as $matrixBlockTypeRecord) { // $matrixBlockType = MatrixBlockTypeModel::populateModel($matrixBlockTypeRecord); // $sources['matrixBlockType:' . $matrixBlockType->id] = $matrixBlockType->fieldLayoutId; // } // } // Users $usersFieldLayout = craft()->fields->getLayoutByType(ElementType::User); if ($usersFieldLayout) { $sources['users'] = $usersFieldLayout->id; } // Solspace Calendar $solspaceCalendarPlugin = craft()->plugins->getPlugin('calendar'); if ($solspaceCalendarPlugin && $solspaceCalendarPlugin->getDeveloper() === 'Solspace') { $solspaceCalendarFieldLayout = craft()->fields->getLayoutByType('Calendar_Event'); if ($solspaceCalendarFieldLayout) { $sources['solspaceCalendar'] = $solspaceCalendarFieldLayout->id; } } // Commerce – TODO // $commercePlugin = craft()->plugins->getPlugin('commerce'); // if ($commercePlugin && $commercePlugin->getDeveloper() === 'Pixel & Tonic') { // // Product types // $productTypes = craft()->commerce_productTypes->getAllProductTypes(); // if ($productTypes) { // foreach ($productTypes as $productType) { // $sources['commerceProductType:'.$productType->id] = // } // } // } // Get all conditionals $conditionals = array(); $conditionalsRecords = Reasons_ConditionalsRecord::model()->findAll(); if ($conditionalsRecords) { foreach ($conditionalsRecords as $conditionalsRecord) { $conditionalsModel = Reasons_ConditionalsModel::populateModel($conditionalsRecord); if ($conditionalsModel->conditionals && $conditionalsModel->conditionals != '') { $conditionals['fieldLayout:' . $conditionalsModel->fieldLayoutId] = $conditionalsModel->conditionals; } } } // Map conditionals to sources foreach ($sources as $sourceId => $fieldLayoutId) { if (isset($conditionals['fieldLayout:' . $fieldLayoutId])) { $r[$sourceId] = $conditionals['fieldLayout:' . $fieldLayoutId]; } } return $r; }