/** * @param SectionModel[] $sections * @param EntryTypeModel $entryType * @param string $tabName * @return array */ public function exportTabFields(array $sections, EntryTypeModel $entryType, $tabName) { //get list of fieldnames in selected $fieldNames = array(); foreach ($this->getFieldLayoutDefinition($entryType->getFieldLayout()) as $contentType) { foreach ($contentType as $key => $value) { if ($key == $tabName) { $entryTypeDefs[$key] = $value; foreach (array_keys($value) as $fieldName) { $fieldNames[] = $fieldName; } } } } return $this->exportFieldNames($fieldNames); }
public function getEntryTypeIds() { if ($entryTypeRecords = EntryTypeRecord::model()->ordered()->findAll()) { $entryTypes = EntryTypeModel::populateModels($entryTypeRecords); $sectionsWithEntryTypeIds = array(); foreach ($entryTypes as $entryType) { if (!isset($sectionsWithEntryTypeIds[$entryType->sectionId])) { $sectionsWithEntryTypeIds[$entryType->sectionId] = array(); } $sectionsWithEntryTypeIds[$entryType->sectionId][] = $entryType->id; } return !empty($sectionsWithEntryTypeIds) ? $sectionsWithEntryTypeIds : false; } return false; }
/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { /* * Craft 2.5+ required – abort migration if not, making it possible to roll back to 2.3 or 2.4 * */ if (!version_compare(craft()->getVersion(), '2.5', '>=')) { throw new Exception(Craft::t('Reasons 1.0 requires Craft 2.5 or newer and is unable to complete this update. Please restore Reasons to v. 0.2.2 or older, or update your Craft install.')); return false; } $table = 'reasons'; /* * Add FK fieldLayoutId column * */ $this->addColumnAfter($table, 'fieldLayoutId', ColumnType::Int, 'id'); $this->addForeignKey($table, 'fieldLayoutId', 'fieldlayouts', 'id', 'CASCADE', 'CASCADE'); /* * Set fieldLayoutId for existing rows * */ $entryTypeRecords = EntryTypeRecord::model()->findAll(); if ($entryTypeRecords) { $entryTypes = EntryTypeModel::populateModels($entryTypeRecords); foreach ($entryTypes as $entryType) { if (!isset($entryType->fieldLayoutId) || !$entryType->fieldLayoutId) { continue; } $this->update($table, array('fieldLayoutId' => $entryType->fieldLayoutId), 'typeId=:typeId', array(':typeId' => $entryType->id)); } } /* * Delete typeId column * */ $this->dropForeignKey($table, 'typeId'); $this->dropColumn($table, 'typeId'); /* * Delete sectionId column * */ $this->dropForeignKey($table, 'sectionId'); $this->dropColumn($table, 'sectionId'); return true; }
/** * Saves an entry type. * * @param EntryTypeModel $entryType * * @throws Exception * @throws \CDbException * @throws \Exception * @return bool */ public function saveEntryType(EntryTypeModel $entryType) { if ($entryType->id) { $entryTypeRecord = EntryTypeRecord::model()->findById($entryType->id); if (!$entryTypeRecord) { throw new Exception(Craft::t('No entry type exists with the ID “{id}”.', array('id' => $entryType->id))); } $isNewEntryType = false; $oldEntryType = EntryTypeModel::populateModel($entryTypeRecord); } else { $entryTypeRecord = new EntryTypeRecord(); $isNewEntryType = true; } $entryTypeRecord->sectionId = $entryType->sectionId; $entryTypeRecord->name = $entryType->name; $entryTypeRecord->handle = $entryType->handle; $entryTypeRecord->hasTitleField = $entryType->hasTitleField; $entryTypeRecord->titleLabel = $entryType->hasTitleField ? $entryType->titleLabel : null; $entryTypeRecord->titleFormat = !$entryType->hasTitleField ? $entryType->titleFormat : null; $entryTypeRecord->validate(); $entryType->addErrors($entryTypeRecord->getErrors()); if (!$entryType->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // Fire an 'onBeforeSaveEntryType' event $event = new Event($this, array('entryType' => $entryType, 'isNewEntryType' => $isNewEntryType)); $this->onBeforeSaveEntryType($event); // Is the event giving us the go-ahead? if ($event->performAction) { if (!$isNewEntryType && $oldEntryType->fieldLayoutId) { // Drop the old field layout craft()->fields->deleteLayoutById($oldEntryType->fieldLayoutId); } // Save the new one $fieldLayout = $entryType->getFieldLayout(); craft()->fields->saveLayout($fieldLayout); // Update the entry type record/model with the new layout ID $entryType->fieldLayoutId = $fieldLayout->id; $entryTypeRecord->fieldLayoutId = $fieldLayout->id; $entryTypeRecord->save(false); // Now that we have an entry type ID, save it on the model if (!$entryType->id) { $entryType->id = $entryTypeRecord->id; } // Might as well update our cache of the entry type while we have it. $this->_entryTypesById[$entryType->id] = $entryType; $success = true; } else { $success = false; } // Commit the transaction regardless of whether we saved the user, in case something changed // in onBeforeSaveEntryType if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } else { $success = false; } if ($success) { // Fire an 'onSaveEntryType' event $this->onSaveEntryType(new Event($this, array('entryType' => $entryType, 'isNewEntryType' => $isNewEntryType))); } return $success; }
/** * Saves an entry type. * * @return null */ public function actionSaveEntryType() { $this->requirePostRequest(); $entryTypeId = craft()->request->getPost('entryTypeId'); if ($entryTypeId) { $entryType = craft()->sections->getEntryTypeById($entryTypeId); if (!$entryType) { throw new Exception(Craft::t('No entry type exists with the ID “{id}”', array('id' => $entryTypeId))); } } else { $entryType = new EntryTypeModel(); } // Set the simple stuff $entryType->sectionId = craft()->request->getRequiredPost('sectionId', $entryType->sectionId); $entryType->name = craft()->request->getPost('name', $entryType->name); $entryType->handle = craft()->request->getPost('handle', $entryType->handle); $entryType->hasTitleField = (bool) craft()->request->getPost('hasTitleField', $entryType->hasTitleField); $entryType->titleLabel = craft()->request->getPost('titleLabel', $entryType->titleLabel); $entryType->titleFormat = craft()->request->getPost('titleFormat', $entryType->titleFormat); // Set the field layout $fieldLayout = craft()->fields->assembleLayoutFromPost(); $fieldLayout->type = ElementType::Entry; $entryType->setFieldLayout($fieldLayout); // Save it if (craft()->sections->saveEntryType($entryType)) { craft()->userSession->setNotice(Craft::t('Entry type saved.')); $this->redirectToPostedUrl($entryType); } else { craft()->userSession->setError(Craft::t('Couldn’t save entry type.')); } // Send the entry type back to the template craft()->urlManager->setRouteVariables(array('entryType' => $entryType)); }
private function _getLayouts() { $assetSources = craft()->assetSources->getAllSources(); $categoryGroups = craft()->categories->getAllGroups(); $globalSets = craft()->globals->getAllSets(); $entryTypes = EntryTypeModel::populateModels(EntryTypeRecord::model()->ordered()->findAll()); $tagGroups = craft()->tags->getAllTagGroups(); //$userFields = FieldLayoutModel::populateModel(FieldLayoutRecord::model()->findByAttributes('type', ElementType::User)); $sections = craft()->sections->getAllSections(); $singleSections = array(); foreach ($sections as $section) { $entryType = $section->getEntryTypes()[0]; $singleSections[$section->id] = (int) $entryType->fieldLayoutId; } return array('assetSource' => $this->_mapLayouts($assetSources), 'categoryGroup' => $this->_mapLayouts($categoryGroups), 'globalSet' => $this->_mapLayouts($globalSets), 'entryType' => $this->_mapLayouts($entryTypes), 'tagGroup' => $this->_mapLayouts($tagGroups), 'singleSection' => $singleSections); }
/** * @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; }
/** * @param EntryTypeModel $entryType * @param array $entryTypeDefinition * @param string $entryTypeHandle * @param int $sectionId */ private function populateEntryType(EntryTypeModel $entryType, array $entryTypeDefinition, $entryTypeHandle, $sectionId) { $entryType->setAttributes(array('handle' => $entryTypeHandle, 'sectionId' => $sectionId, 'name' => $entryTypeDefinition['name'], 'hasTitleField' => $entryTypeDefinition['hasTitleField'], 'titleLabel' => $entryTypeDefinition['titleLabel'], 'titleFormat' => $entryTypeDefinition['titleFormat'])); $fieldLayout = craft()->artVandelay_fields->getFieldLayout($entryTypeDefinition['fieldLayout']); $entryType->setFieldLayout($fieldLayout); }
/** * Saves an entry type. * * @param EntryTypeModel $entryType * * @throws \Exception * @return bool */ public function saveEntryType(EntryTypeModel $entryType) { if ($entryType->id) { $entryTypeRecord = EntryTypeRecord::model()->findById($entryType->id); if (!$entryTypeRecord) { throw new Exception(Craft::t('No entry type exists with the ID “{id}”', array('id' => $entryType->id))); } $isNewEntryType = false; $oldEntryType = EntryTypeModel::populateModel($entryTypeRecord); } else { $entryTypeRecord = new EntryTypeRecord(); $isNewEntryType = true; } $entryTypeRecord->sectionId = $entryType->sectionId; $entryTypeRecord->name = $entryType->name; $entryTypeRecord->handle = $entryType->handle; $entryTypeRecord->hasTitleField = $entryType->hasTitleField; $entryTypeRecord->titleLabel = $entryType->hasTitleField ? $entryType->titleLabel : null; $entryTypeRecord->titleFormat = !$entryType->hasTitleField ? $entryType->titleFormat : null; $entryTypeRecord->validate(); $entryType->addErrors($entryTypeRecord->getErrors()); if (!$entryType->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { if (!$isNewEntryType && $oldEntryType->fieldLayoutId) { // Drop the old field layout craft()->fields->deleteLayoutById($oldEntryType->fieldLayoutId); } // Save the new one $fieldLayout = $entryType->getFieldLayout(); craft()->fields->saveLayout($fieldLayout); // Update the entry type record/model with the new layout ID $entryType->fieldLayoutId = $fieldLayout->id; $entryTypeRecord->fieldLayoutId = $fieldLayout->id; $entryTypeRecord->save(false); // Now that we have an entry type ID, save it on the model if (!$entryType->id) { $entryType->id = $entryTypeRecord->id; } // Might as well update our cache of the entry type while we have it. $this->_entryTypesById[$entryType->id] = $entryType; if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } return true; } else { return false; } }