/** * Saves a section */ public function actionSaveSection() { $this->requirePostRequest(); $section = new SectionModel(); // Set the simple stuff $section->id = craft()->request->getPost('sectionId'); $section->name = craft()->request->getPost('name'); $section->handle = craft()->request->getPost('handle'); $section->titleLabel = craft()->request->getPost('titleLabel'); $section->hasUrls = (bool) craft()->request->getPost('hasUrls'); $section->template = craft()->request->getPost('template'); // Set the locales and URL formats $locales = array(); $urlFormats = craft()->request->getPost('urlFormat'); if (Craft::hasPackage(CraftPackage::Localize)) { $localeIds = craft()->request->getPost('locales'); } else { $primaryLocaleId = craft()->i18n->getPrimarySiteLocaleId(); $localeIds = array($primaryLocaleId); } foreach ($localeIds as $localeId) { $locales[$localeId] = SectionLocaleModel::populateModel(array('locale' => $localeId, 'urlFormat' => isset($urlFormats[$localeId]) ? $urlFormats[$localeId] : null)); } $section->setLocales($locales); // Set the field layout $fieldLayout = craft()->fields->assembleLayoutFromPost(); $fieldLayout->type = ElementType::Entry; $section->setFieldLayout($fieldLayout); // Save it if (craft()->sections->saveSection($section)) { craft()->userSession->setNotice(Craft::t('Section saved.')); // TODO: Remove for 2.0 if (isset($_POST['redirect']) && strpos($_POST['redirect'], '{sectionId}') !== false) { Craft::log('The {sectionId} token within the ‘redirect’ param on sections/saveSection requests has been deprecated. Use {id} instead.', LogLevel::Warning); $_POST['redirect'] = str_replace('{sectionId}', '{id}', $_POST['redirect']); } $this->redirectToPostedUrl($section); } else { craft()->userSession->setError(Craft::t('Couldn’t save section.')); } // Send the section back to the template craft()->urlManager->setRouteVariables(array('section' => $section)); }
/** * 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); } }