/** * Saves a block type. * * @param MatrixBlockTypeModel $blockType The block type to be saved. * @param bool $validate Whether the block type should be validated before being saved. * Defaults to `true`. * * @throws \Exception * @return bool Whether the block type was saved successfully. */ public function saveBlockType(MatrixBlockTypeModel $blockType, $validate = true) { if (!$validate || $this->validateBlockType($blockType)) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { $contentService = craft()->content; $fieldsService = craft()->fields; $originalFieldContext = $contentService->fieldContext; $originalFieldColumnPrefix = $contentService->fieldColumnPrefix; $originalOldFieldColumnPrefix = $fieldsService->oldFieldColumnPrefix; // Get the block type record $blockTypeRecord = $this->_getBlockTypeRecord($blockType); $isNewBlockType = $blockType->isNew(); if (!$isNewBlockType) { // Get the old block type fields $oldBlockTypeRecord = MatrixBlockTypeRecord::model()->findById($blockType->id); $oldBlockType = MatrixBlockTypeModel::populateModel($oldBlockTypeRecord); $contentService->fieldContext = 'matrixBlockType:' . $blockType->id; $contentService->fieldColumnPrefix = 'field_' . $oldBlockType->handle . '_'; $fieldsService->oldFieldColumnPrefix = 'field_' . $oldBlockType->handle . '_'; $oldFieldsById = array(); foreach ($oldBlockType->getFields() as $field) { $oldFieldsById[$field->id] = $field; } // Figure out which ones are still around foreach ($blockType->getFields() as $field) { if (!$field->isNew()) { unset($oldFieldsById[$field->id]); } } // Drop the old fields that aren't around anymore foreach ($oldFieldsById as $field) { $fieldsService->deleteField($field); } // Refresh the schema cache craft()->db->getSchema()->refresh(); } // 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; // Save it, minus the field layout for now $blockTypeRecord->save(false); if ($isNewBlockType) { // Set the new ID on the model $blockType->id = $blockTypeRecord->id; } // Save the fields and field layout // ------------------------------------------------------------- $fieldLayoutFields = array(); $sortOrder = 0; // Resetting the fieldContext here might be redundant if this isn't a new blocktype but whatever $contentService->fieldContext = 'matrixBlockType:' . $blockType->id; $contentService->fieldColumnPrefix = 'field_' . $blockType->handle . '_'; foreach ($blockType->getFields() as $field) { // Hack to allow blank field names if (!$field->name) { $field->name = '__blank__'; } if (!$fieldsService->saveField($field, false)) { throw new Exception(Craft::t('An error occurred while saving this Matrix block type.')); } $fieldLayoutField = new FieldLayoutFieldModel(); $fieldLayoutField->fieldId = $field->id; $fieldLayoutField->required = $field->required; $fieldLayoutField->sortOrder = ++$sortOrder; $fieldLayoutFields[] = $fieldLayoutField; } $contentService->fieldContext = $originalFieldContext; $contentService->fieldColumnPrefix = $originalFieldColumnPrefix; $fieldsService->oldFieldColumnPrefix = $originalOldFieldColumnPrefix; $fieldLayoutTab = new FieldLayoutTabModel(); $fieldLayoutTab->name = 'Content'; $fieldLayoutTab->sortOrder = 1; $fieldLayoutTab->setFields($fieldLayoutFields); $fieldLayout = new FieldLayoutModel(); $fieldLayout->type = ElementType::MatrixBlock; $fieldLayout->setTabs(array($fieldLayoutTab)); $fieldLayout->setFields($fieldLayoutFields); $fieldsService->saveLayout($fieldLayout); // Update the block type model & record with our new field layout ID $blockType->setFieldLayout($fieldLayout); $blockType->fieldLayoutId = $fieldLayout->id; $blockTypeRecord->fieldLayoutId = $fieldLayout->id; // Update the block type with the field layout ID $blockTypeRecord->save(false); if (!$isNewBlockType) { // Delete the old field layout $fieldsService->deleteLayoutById($oldBlockType->fieldLayoutId); } if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } return true; } else { return false; } }
/** * Assembles a field layout from post data. * * @param bool $createTabs Whether to create tabs, or just assign the fields directly to the layout. * @return FieldLayoutModel */ public function assembleLayoutFromPost($createTabs = true) { $postedFieldLayout = craft()->request->getPost('fieldLayout', array()); $requiredFields = craft()->request->getPost('requiredFields', array()); $tabs = array(); $fields = array(); $tabSortOrder = 0; foreach ($postedFieldLayout as $tabName => $fieldIds) { $tabFields = array(); foreach ($fieldIds as $fieldSortOrder => $fieldId) { $field = array('fieldId' => $fieldId, 'required' => in_array($fieldId, $requiredFields), 'sortOrder' => $fieldSortOrder + 1); $tabFields[] = $field; } $fields = array_merge($fields, $tabFields); if ($createTabs) { $tabSortOrder++; $tabs[] = array('name' => urldecode($tabName), 'sortOrder' => $tabSortOrder, 'fields' => $tabFields); } } $layout = new FieldLayoutModel(); $layout->setTabs($tabs); $layout->setFields($fields); return $layout; }
/** * Creates initial database content for the install. * * @access private * @param $inputs * @return null */ private function _createDefaultContent($inputs) { // Default tag set Craft::log('Creating the Default tag set.'); $tagSet = new TagSetModel(); $tagSet->name = Craft::t('Default'); $tagSet->handle = 'default'; // Save it if (craft()->tags->saveTagSet($tagSet)) { Craft::log('Default tag set created successfully.'); } else { Craft::log('Could not save the Default tag set.', LogLevel::Warning); } // Default field group Craft::log('Creating the Default field group.'); $group = new FieldGroupModel(); $group->name = Craft::t('Default'); if (craft()->fields->saveGroup($group)) { Craft::log('Default field group created successfully.'); } else { Craft::log('Could not save the Default field group.', LogLevel::Warning); } // Heading field Craft::log('Creating the Heading field.'); $headingField = new FieldModel(); $headingField->groupId = $group->id; $headingField->name = Craft::t('Heading'); $headingField->handle = 'heading'; $headingField->translatable = true; $headingField->type = 'PlainText'; if (craft()->fields->saveField($headingField)) { Craft::log('Heading field created successfully.'); } else { Craft::log('Could not save the Heading field.', LogLevel::Warning); } // Body field Craft::log('Creating the Body field.'); $bodyField = new FieldModel(); $bodyField->groupId = $group->id; $bodyField->name = Craft::t('Body'); $bodyField->handle = 'body'; $bodyField->translatable = true; $bodyField->type = 'RichText'; $bodyField->settings = array('configFile' => 'Standard.json'); if (craft()->fields->saveField($bodyField)) { Craft::log('Body field created successfully.'); } else { Craft::log('Could not save the Body field.', LogLevel::Warning); } // Tags field Craft::log('Creating the Tags field.'); $tagsField = new FieldModel(); $tagsField->groupId = $group->id; $tagsField->name = Craft::t('Tags'); $tagsField->handle = 'tags'; $tagsField->type = 'Tags'; $tagsField->settings = array('source' => 'tagset:' . $tagSet->id); if (craft()->fields->saveField($tagsField)) { Craft::log('Tags field created successfully.'); } else { Craft::log('Could not save the Tags field.', LogLevel::Warning); } // Homepage global set Craft::log('Creating the Homepage global set.'); $homepageLayoutFields = array(array('fieldId' => $headingField->id, 'sortOrder' => 1), array('fieldId' => $bodyField->id, 'sortOrder' => 2)); $homepageLayout = new FieldLayoutModel(); $homepageLayout->type = ElementType::GlobalSet; $homepageLayout->setFields($homepageLayoutFields); $homepageGlobalSet = new GlobalSetModel(); $homepageGlobalSet->name = Craft::t('Homepage'); $homepageGlobalSet->handle = 'homepage'; $homepageGlobalSet->setFieldLayout($homepageLayout); if (craft()->globals->saveSet($homepageGlobalSet)) { Craft::log('Homepage global set created successfully.'); } else { Craft::log('Could not save the Homepage global set.', LogLevel::Warning); } // Homepage content $vars = array('siteName' => ucfirst(craft()->request->getServerName())); Craft::log('Setting the Homepage content.'); $homepageGlobalSet->locale = $inputs['locale']; $homepageGlobalSet->getContent()->setAttributes(array('heading' => Craft::t('Welcome to {siteName}!', $vars), 'body' => '<p>' . Craft::t('It’s true, this site doesn’t have a whole lot of content yet, but don’t worry. Our web developers have just installed the CMS, and they’re setting things up for the content editors this very moment. Soon {siteName} will be an oasis of fresh perspectives, sharp analyses, and astute opinions that will keep you coming back again and again.', $vars) . '</p>')); if (craft()->globals->saveContent($homepageGlobalSet)) { Craft::log('Homepage content set successfully.'); } else { Craft::log('Could not set the Homepage content.', LogLevel::Warning); } // News section Craft::log('Creating the News section.'); $newsLayoutFields = array(array('fieldId' => $bodyField->id, 'required' => true, 'sortOrder' => 1), array('fieldId' => $tagsField->id, 'sortOrder' => 2)); $newsLayoutTabs = array(array('name' => Craft::t('Content'), 'sortOrder' => 1, 'fields' => $newsLayoutFields)); $newsLayout = new FieldLayoutModel(); $newsLayout->type = ElementType::Entry; $newsLayout->setTabs($newsLayoutTabs); $newsLayout->setFields($newsLayoutFields); $newsSection = new SectionModel(); $newsSection->name = Craft::t('News'); $newsSection->handle = 'news'; $newsSection->hasUrls = true; $newsSection->template = 'news/_entry'; $newsSection->setLocales(array($inputs['locale'] => SectionLocaleModel::populateModel(array('locale' => $inputs['locale'], 'urlFormat' => 'news/{postDate.year}/{slug}')))); $newsSection->setFieldLayout($newsLayout); if (craft()->sections->saveSection($newsSection)) { Craft::log('News section created successfully.'); } else { Craft::log('Could not save the News section.', LogLevel::Warning); } // News entry Craft::log('Creating a News entry.'); $newsEntry = new EntryModel(); $newsEntry->sectionId = $newsSection->id; $newsEntry->locale = $inputs['locale']; $newsEntry->authorId = $this->_user->id; $newsEntry->enabled = true; $newsEntry->getContent()->title = Craft::t('We just installed Craft!'); $newsEntry->getContent()->setAttributes(array('body' => '<p>' . Craft::t('Craft is the CMS that’s powering {siteName}. It’s beautiful, powerful, flexible, and easy-to-use, and it’s made by Pixel & Tonic. We can’t wait to dive in and see what it’s capable of!', $vars) . '</p><!--pagebreak--><p>' . Craft::t('This is even more captivating content, which you couldn’t see on the News index page because it was entered after a Page Break, and the News index template only likes to show the content on the first page.') . '</p><p>' . Craft::t('Craft: a nice alternative to Word, if you’re making a website.') . '</p>')); if (craft()->entries->saveEntry($newsEntry)) { Craft::log('News entry created successfully.'); } else { Craft::log('Could not save the News entry.', LogLevel::Warning); } }
/** * Assembles a field layout. * * @param array $postedFieldLayout * @param array|null $requiredFields * * @return FieldLayoutModel */ public function assembleLayout($postedFieldLayout, $requiredFields) { $tabs = array(); $fields = array(); $tabSortOrder = 0; foreach ($postedFieldLayout as $tabName => $fieldIds) { $tabFields = array(); $tabSortOrder++; foreach ($fieldIds as $fieldSortOrder => $fieldId) { $field = new FieldLayoutFieldModel(); $field->fieldId = $fieldId; $field->required = in_array($fieldId, $requiredFields); $field->sortOrder = $fieldSortOrder + 1; $fields[] = $field; $tabFields[] = $field; } $tab = new FieldLayoutTabModel(); $tab->name = urldecode($tabName); $tab->sortOrder = $tabSortOrder; $tab->setFields($tabFields); $tabs[] = $tab; } $layout = new FieldLayoutModel(); $layout->setTabs($tabs); $layout->setFields($fields); return $layout; }
/** * Attempt to import a field layout. * * @param array $fieldLayoutDef * * @return FieldLayoutModel */ private function _importFieldLayout(array $fieldLayoutDef) { $layoutTabs = array(); $layoutFields = array(); if (array_key_exists('tabs', $fieldLayoutDef)) { $tabSortOrder = 0; foreach ($fieldLayoutDef['tabs'] as $tabName => $tabDef) { $layoutTabFields = array(); foreach ($tabDef as $fieldHandle => $required) { $fieldSortOrder = 0; $field = craft()->fields->getFieldByHandle($fieldHandle); if ($field) { $layoutField = new FieldLayoutFieldModel(); $layoutField->setAttributes(array('fieldId' => $field->id, 'required' => $required, 'sortOrder' => ++$fieldSortOrder)); $layoutTabFields[] = $layoutField; $layoutFields[] = $layoutField; } } $layoutTab = new FieldLayoutTabModel(); $layoutTab->setAttributes(array('name' => $tabName, 'sortOrder' => ++$tabSortOrder)); $layoutTab->setFields($layoutTabFields); $layoutTabs[] = $layoutTab; } } else { if (array_key_exists('fields', $fieldLayoutDef)) { $fieldSortOrder = 0; foreach ($fieldLayoutDef['fields'] as $fieldHandle => $required) { $field = craft()->fields->getFieldByHandle($fieldHandle); if ($field) { $layoutField = new FieldLayoutFieldModel(); $layoutField->setAttributes(array('fieldId' => $field->id, 'required' => $required, 'sortOrder' => ++$fieldSortOrder)); $layoutFields[] = $layoutField; } } } } $fieldLayout = new FieldLayoutModel(); $fieldLayout->type = ElementType::Entry; $fieldLayout->setTabs($layoutTabs); $fieldLayout->setFields($layoutFields); return $fieldLayout; }