/** * 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 InstaBlog. * * @return null */ private function _createInstaBlogContent() { // InstaBlog tag group Craft::log('Creating the InstaBlog tag group.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $tagGroup = new TagGroupModel(); $tagGroup->name = 'InstaBlog Tags'; $tagGroup->handle = 'instaBlogTags'; // Save it if (craft()->tags->saveTagGroup($tagGroup)) { Craft::log('InstaBlog tag group created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog tag group.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // InstaBlog field group Craft::log('Creating the InstaBlog field group.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $group = new FieldGroupModel(); $group->name = 'InstaBlog'; if (craft()->fields->saveGroup($group)) { Craft::log('InstaBlog field group created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog field group.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Body field Craft::log('Creating the InstaBlog Body field.'); $bodyField = new FieldModel(); $bodyField->groupId = $group->id; $bodyField->name = 'InstaBlog Body'; $bodyField->handle = 'instaBlogBody'; $bodyField->translatable = true; $bodyField->type = 'RichText'; $bodyField->settings = array('configFile' => 'Standard.json', 'columnType' => ColumnType::Text); if (craft()->fields->saveField($bodyField)) { Craft::log('InstaBlog Body field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog Body field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Facebook field Craft::log('Creating the InstaBlog Facebook field.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $facebookField = new FieldModel(); $facebookField->groupId = $group->id; $facebookField->name = 'Facebook'; $facebookField->handle = 'instaBlogFacebook'; $facebookField->translatable = false; $facebookField->type = 'PlainText'; $facebookField->instructions = 'Add your personal Facebook profile link. Example: https://www.facebook.com/xxxxxxxxxx'; if (craft()->fields->saveField($facebookField)) { Craft::log('Facebook field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the Facebook field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Twitter Handle field Craft::log('Creating the InstaBlog Twitter field.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $twitterField = new FieldModel(); $twitterField->groupId = $group->id; $twitterField->name = 'Twitter'; $twitterField->handle = 'instaBlogTwitter'; $twitterField->translatable = false; $twitterField->type = 'PlainText'; $twitterField->instructions = 'Add your personal Twitter handle. Example: @johndoe'; if (craft()->fields->saveField($twitterField)) { Craft::log('Twitter field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the Twitter field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Google+ field Craft::log('Creating the InstaBlog Google+ field.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $googlePlusField = new FieldModel(); $googlePlusField->groupId = $group->id; $googlePlusField->name = 'Google+'; $googlePlusField->handle = 'instaBlogGooglePlus'; $googlePlusField->translatable = false; $googlePlusField->type = 'PlainText'; $googlePlusField->instructions = 'Add your personal Google+ profile link. Example: https://plus.google.com/+JohnDoe'; if (craft()->fields->saveField($googlePlusField)) { Craft::log('Google+ field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the Google+ field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // LinkedIn field Craft::log('Creating the InstaBlog LinkedIn field.'); $linkedinField = new FieldModel(); $linkedinField->groupId = $group->id; $linkedinField->name = 'LinkedIn'; $linkedinField->handle = 'instaBlogLinkedin'; $linkedinField->translatable = false; $linkedinField->type = 'PlainText'; $linkedinField->instructions = 'Add your personal LinkedIn profile link. Example: https://www.linkedin.com/pub/john-doe/3/7aa/91b'; if (craft()->fields->saveField($linkedinField)) { Craft::log('LinkedIn field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the LinkedIn field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Create the new user field layout Craft::log('Creating the new user profile layout.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $userFieldLayout = craft()->fields->getLayoutByType(ElementType::User); $fieldsIds = $userFieldLayout->getFieldIds(); $fieldsIds[] = $facebookField->id; $fieldsIds[] = $twitterField->id; $fieldsIds[] = $googlePlusField->id; $fieldsIds[] = $linkedinField->id; craft()->fields->deleteLayoutsByType(ElementType::User); $userFieldLayout = craft()->fields->assembleLayout(array(Craft::t('Profile') => $fieldsIds), array(), false); $userFieldLayout->type = ElementType::User; if (craft()->fields->saveLayout($userFieldLayout, false)) { Craft::log('User profile layout saved successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the user profile layout.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Tags field Craft::log('Creating the Tags field.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $tagsField = new FieldModel(); $tagsField->groupId = $group->id; $tagsField->name = 'InstaBlog Tags'; $tagsField->handle = 'instaBlogTags'; $tagsField->type = 'Tags'; $tagsField->settings = array('source' => 'taggroup:' . $tagGroup->id); if (craft()->fields->saveField($tagsField)) { Craft::log('InstaBlog Tags field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog Tags field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // InstaBlog category group Craft::log('Creating the InstaBlog category group.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $categoryGroup = new CategoryGroupModel(); $categoryGroup->name = 'InstaBlog Categories'; $categoryGroup->handle = 'instaBlogCategories'; $categoryGroup->template = 'blog/category'; $categoryGroup->maxLevels = 1; // Locale-specific URL formats $locales = array(); foreach (craft()->i18n->getSiteLocaleIds() as $localeId) { $locales[$localeId] = new CategoryGroupLocaleModel(array('locale' => $localeId, 'urlFormat' => 'blog/category/{slug}', 'nestedUrlFormat' => null)); } $categoryGroup->setLocales($locales); // Group the field layout $categoryFieldLayout = craft()->fields->assembleLayout(array('Content' => array($bodyField->id)), array()); $categoryFieldLayout->type = ElementType::Category; $categoryGroup->setFieldLayout($categoryFieldLayout); // Save it if (craft()->categories->saveGroup($categoryGroup)) { Craft::log('InstaBlog category group created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog category group.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Categories field Craft::log('Creating the InstaBlog Category field.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $categoriesField = new FieldModel(); $categoriesField->groupId = $group->id; $categoriesField->name = 'InstaBlog Categories'; $categoriesField->handle = 'instaBlogCategories'; $categoriesField->type = 'Categories'; $categoriesField->settings = array('source' => 'group:' . $categoryGroup->id); if (craft()->fields->saveField($categoriesField)) { Craft::log('InstaBlog Category field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog Category field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // Asset field Craft::log('Creating the InstaBlog Asset field.'); $assetField = new FieldModel(); $assetField->groupId = $group->id; $assetField->name = 'Featured Image'; $assetField->handle = 'instaBlogImage'; $assetField->translatable = false; $assetField->type = 'Assets'; $assetField->settings = array('sources' => '*'); if (craft()->fields->saveField($assetField)) { Craft::log('Asset field created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the Asset field.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // InstaBlog section Craft::log('Creating the InstaBlog section.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $instaBlogSection = new SectionModel(); $instaBlogSection->type = SectionType::Channel; $instaBlogSection->name = 'InstaBlog'; $instaBlogSection->handle = 'instaBlog'; $instaBlogSection->hasUrls = true; $instaBlogSection->template = 'blog/_entry'; // Locale-specific URL formats $locales = array(); if (craft()->isLocalized()) { $localeIds = craft()->i18n->getSiteLocaleIds(); } else { $primaryLocaleId = craft()->i18n->getPrimarySiteLocaleId(); $localeIds = array($primaryLocaleId); } foreach ($localeIds as $localeId) { $locales[$localeId] = new SectionLocaleModel(array('locale' => $localeId, 'enabledByDefault' => true, 'urlFormat' => 'blog/{slug}')); } $instaBlogSection->setLocales($locales); if (craft()->sections->saveSection($instaBlogSection)) { Craft::log('InstaBlog section created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog section.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // InstaBlog section entry type layout Craft::log('Saving the InstaBlog entry type.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); $instaBlogLayout = craft()->fields->assembleLayout(array('Content' => array($bodyField->id, $assetField->id, $categoriesField->id, $tagsField->id)), array($bodyField->id)); $instaBlogLayout->type = ElementType::Entry; $instaBlogEntryTypes = $instaBlogSection->getEntryTypes(); $instaBlogEntryType = $instaBlogEntryTypes[0]; $instaBlogEntryType->setFieldLayout($instaBlogLayout); if (craft()->sections->saveEntryType($instaBlogEntryType)) { Craft::log('InstaBlog entry type saved successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog entry type.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } // InstaBlog entry Craft::log('Creating a InstaBlog entry.'); $instaBlogEntry = new EntryModel(); $instaBlogEntry->sectionId = $instaBlogSection->id; $instaBlogEntry->typeId = $instaBlogEntryType->id; $instaBlogEntry->locale = $primaryLocaleId; $instaBlogEntry->authorId = craft()->userSession->getId(); $instaBlogEntry->enabled = true; $instaBlogEntry->getContent()->title = 'We just installed InstaBlog!'; $instaBlogEntry->getContent()->setAttributes(array('instaBlogBody' => '<p>' . 'Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without revolutionary ROI.' . '</p><p>Efficiently unleash cross-media information without cross-media value. Quickly maximize timely deliverables for real-time schemas. Dramatically maintain clicks-and-mortar solutions without functional solutions.' . '</p><p>Completely synergize resource taxing relationships via premier niche markets. Professionally cultivate one-to-one customer service with robust ideas. Dynamically innovate resource-leveling customer service for state of the art customer service.' . '</p>')); if (craft()->entries->saveEntry($instaBlogEntry)) { Craft::log('InstaBlog entry created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not save the InstaBlog entry.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } //Create Route for Tags $urlParts = array('blog/tag/', array('*', '[^\\/]+')); $template = 'blog/tag'; if (craft()->routes->saveRoute($urlParts, $template)) { Craft::log('InstaBlog tag route created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not create the InstaBlog tag route.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } //Create Route for Tags $urlParts = array('blog/author/', array('*', '[^\\/]+')); $template = 'blog/author'; if (craft()->routes->saveRoute($urlParts, $template)) { Craft::log('InstaBlog author route created successfully.', LogLevel::Info, true, '_createInstaBlogContent', 'InstaBlog'); } else { Craft::log('Could not create the InstaBlog author route.', LogLevel::Error, true, '_createInstaBlogContent', 'InstaBlog'); } }
/** * Creates initial database content for the install. * * @param $inputs * * @return null */ private function _createDefaultContent($inputs) { // Default tag group Craft::log('Creating the Default tag group.'); $tagGroup = new TagGroupModel(); $tagGroup->name = 'Default'; $tagGroup->handle = 'default'; // Save it if (craft()->tags->saveTagGroup($tagGroup)) { Craft::log('Default tag group created successfully.'); } else { Craft::log('Could not save the Default tag group.', LogLevel::Warning); } // Default field group Craft::log('Creating the Default field group.'); $group = new FieldGroupModel(); $group->name = '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); } // Body field Craft::log('Creating the Body field.'); $bodyField = new FieldModel(); $bodyField->groupId = $group->id; $bodyField->name = 'Body'; $bodyField->handle = 'body'; $bodyField->translatable = true; $bodyField->type = 'RichText'; $bodyField->settings = array('configFile' => 'Standard.json', 'columnType' => ColumnType::Text); 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 = 'Tags'; $tagsField->handle = 'tags'; $tagsField->type = 'Tags'; $tagsField->settings = array('source' => 'taggroup:' . $tagGroup->id); if (craft()->fields->saveField($tagsField)) { Craft::log('Tags field created successfully.'); } else { Craft::log('Could not save the Tags field.', LogLevel::Warning); } // Homepage single section Craft::log('Creating the Homepage single section.'); $homepageLayout = craft()->fields->assembleLayout(array('Content' => array($bodyField->id)), array($bodyField->id)); $homepageLayout->type = ElementType::Entry; $homepageSingleSection = new SectionModel(); $homepageSingleSection->name = 'Homepage'; $homepageSingleSection->handle = 'homepage'; $homepageSingleSection->type = SectionType::Single; $homepageSingleSection->hasUrls = false; $homepageSingleSection->template = 'index'; $primaryLocaleId = craft()->i18n->getPrimarySiteLocaleId(); $locales[$primaryLocaleId] = new SectionLocaleModel(array('locale' => $primaryLocaleId, 'urlFormat' => '__home__')); $homepageSingleSection->setLocales($locales); // Save it if (craft()->sections->saveSection($homepageSingleSection)) { Craft::log('Homepage single section created successfully.'); } else { Craft::log('Could not save the Homepage single section.', LogLevel::Warning); } $homepageEntryTypes = $homepageSingleSection->getEntryTypes(); $homepageEntryType = $homepageEntryTypes[0]; $homepageEntryType->hasTitleField = true; $homepageEntryType->titleLabel = 'Title'; $homepageEntryType->setFieldLayout($homepageLayout); if (craft()->sections->saveEntryType($homepageEntryType)) { Craft::log('Homepage single section entry type saved successfully.'); } else { Craft::log('Could not save the Homepage single section entry type.', LogLevel::Warning); } // Homepage content $siteName = ucfirst(craft()->request->getServerName()); Craft::log('Setting the Homepage content.'); $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->sectionId = $homepageSingleSection->id; $entryModel = $criteria->first(); $entryModel->locale = $inputs['locale']; $entryModel->getContent()->title = 'Welcome to ' . $siteName . '!'; $entryModel->setContentFromPost(array('body' => '<p>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.</p>')); // Save the content if (craft()->entries->saveEntry($entryModel)) { Craft::log('Homepage an entry to the Homepage single section.'); } else { Craft::log('Could not save an entry to the Homepage single section.', LogLevel::Warning); } // News section Craft::log('Creating the News section.'); $newsSection = new SectionModel(); $newsSection->type = SectionType::Channel; $newsSection->name = '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}')))); if (craft()->sections->saveSection($newsSection)) { Craft::log('News section created successfully.'); } else { Craft::log('Could not save the News section.', LogLevel::Warning); } Craft::log('Saving the News entry type.'); $newsLayout = craft()->fields->assembleLayout(array('Content' => array($bodyField->id, $tagsField->id)), array($bodyField->id)); $newsLayout->type = ElementType::Entry; $newsEntryTypes = $newsSection->getEntryTypes(); $newsEntryType = $newsEntryTypes[0]; $newsEntryType->setFieldLayout($newsLayout); if (craft()->sections->saveEntryType($newsEntryType)) { Craft::log('News entry type saved successfully.'); } else { Craft::log('Could not save the News entry type.', LogLevel::Warning); } // News entry Craft::log('Creating a News entry.'); $newsEntry = new EntryModel(); $newsEntry->sectionId = $newsSection->id; $newsEntry->typeId = $newsEntryType->id; $newsEntry->locale = $inputs['locale']; $newsEntry->authorId = $this->_user->id; $newsEntry->enabled = true; $newsEntry->getContent()->title = 'We just installed Craft!'; $newsEntry->getContent()->setAttributes(array('body' => '<p>' . '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!' . '</p><!--pagebreak--><p>' . '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: 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); } }
/** * addSection * @param String $jsonSection [input string] * @return Boolean [success] */ private function addSection($jsonSection) { $section = new SectionModel(); $section->name = $jsonSection->name; // Set handle if it was provided if (isset($jsonSection->handle)) { $section->handle = $jsonSection->handle; } else { $section->handle = $this->generateHandle($jsonSection->name); } $section->type = $jsonSection->type; // Set enableVersioning if it was provided if (isset($jsonSection->typeSettings->enableVersioning)) { $section->enableVersioning = $jsonSection->typeSettings->enableVersioning; } else { $section->enableVersioning = 1; } // Set hasUrls if it was provided if (isset($jsonSection->typeSettings->hasUrls)) { $section->hasUrls = $jsonSection->typeSettings->hasUrls; } // Set template if it was provided if (isset($jsonSection->typeSettings->template)) { $section->template = $jsonSection->typeSettings->template; } // Set maxLevels if it was provided if (isset($jsonSection->typeSettings->maxLevels)) { $section->maxLevels = $jsonSection->typeSettings->maxLevels; } $locales = array(); $primaryLocaleId = craft()->i18n->getPrimarySiteLocaleId(); $localeIds = array($primaryLocaleId); foreach ($localeIds as $localeId) { if (isset($jsonSection->typeSettings->urlFormat)) { $urlFormat = $jsonSection->typeSettings->urlFormat; } else { $urlFormat = null; } if (isset($jsonSection->typeSettings->nestedUrlFormat)) { $nestedUrlFormat = $jsonSection->typeSettings->nestedUrlFormat; } else { $nestedUrlFormat = null; } $locales[$localeId] = new SectionLocaleModel(array('locale' => $localeId, 'enabledByDefault' => null, 'urlFormat' => $urlFormat, 'nestedUrlFormat' => $nestedUrlFormat)); } $section->setLocales($locales); if (craft()->sections->saveSection($section)) { return true; } else { return false; } }
/** * Saves a section. * * @return null */ public function actionSaveSection() { $this->requirePostRequest(); $section = new SectionModel(); // Shared attributes $section->id = craft()->request->getPost('sectionId'); $section->name = craft()->request->getPost('name'); $section->handle = craft()->request->getPost('handle'); $section->type = craft()->request->getPost('type'); $section->enableVersioning = craft()->request->getPost('enableVersioning', true); // Type-specific attributes $section->hasUrls = (bool) craft()->request->getPost('types.' . $section->type . '.hasUrls', true); $section->template = craft()->request->getPost('types.' . $section->type . '.template'); $section->maxLevels = craft()->request->getPost('types.' . $section->type . '.maxLevels'); // Locale-specific attributes $locales = array(); if (craft()->isLocalized()) { $localeIds = craft()->request->getPost('locales'); } else { $primaryLocaleId = craft()->i18n->getPrimarySiteLocaleId(); $localeIds = array($primaryLocaleId); } $isHomepage = $section->type == SectionType::Single && craft()->request->getPost('types.' . $section->type . '.homepage'); foreach ($localeIds as $localeId) { if ($isHomepage) { $urlFormat = '__home__'; $nestedUrlFormat = null; } else { $urlFormat = craft()->request->getPost('types.' . $section->type . '.urlFormat.' . $localeId); $nestedUrlFormat = craft()->request->getPost('types.' . $section->type . '.nestedUrlFormat.' . $localeId); } $locales[$localeId] = new SectionLocaleModel(array('locale' => $localeId, 'enabledByDefault' => (bool) craft()->request->getPost('defaultLocaleStatuses.' . $localeId), 'urlFormat' => $urlFormat, 'nestedUrlFormat' => $nestedUrlFormat)); } $section->setLocales($locales); $section->hasUrls = (bool) craft()->request->getPost('types.' . $section->type . '.hasUrls', true); // Save it if (craft()->sections->saveSection($section)) { craft()->userSession->setNotice(Craft::t('Section saved.')); if (isset($_POST['redirect']) && mb_strpos($_POST['redirect'], '{sectionId}') !== false) { craft()->deprecator->log('SectionsController::saveSection():sectionId_redirect', 'The {sectionId} token within the ‘redirect’ param on sections/saveSection requests has been deprecated. Use {id} instead.'); $_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); } }
public function actionSaveTeam() { $illegalSlugCharacters = array_merge(range(chr(0), chr(47)), range(chr(58), chr(64)), range(chr(91), chr(96)), range(chr(123), chr(127))); $this->requirePostRequest(); $id = craft()->request->getPost('teamId'); $teamName = craft()->request->getPost('teamName'); $slug = rtrim(strtolower(str_replace($illegalSlugCharacters, '_', $teamName)), '_'); if (!$id) { $counter = 1; $slugPrefix = ""; while (craft()->teamManager->getTeamBySlug($slug . $slugPrefix)) { $slugPrefix = '_' . $counter++; if ($counter > 10000) { break; } } $slug .= $slugPrefix; if ($counter > 1) { craft()->userSession->setError(Craft::t('Team "' . $teamName . '" already exists, so it was renamed to "' . ($teamName .= str_replace('_', ' ', $slugPrefix)) . '"')); } } $values = array('teamName' => $teamName, 'slug' => $slug, 'gender' => craft()->request->getPost('gender'), 'thumbnail' => craft()->request->getPost('thumbnail') == null ? null : craft()->request->getPost('thumbnail')[0], 'description' => craft()->request->getPost('description'), 'images' => craft()->request->getPost('images')); //TODO check for preconditions if ($id) { $model = craft()->teamManager->getTeam($id); $model->setAttributes($values); } else { $model = craft()->teamManager->newTeam($values); } //Create a new section $section = new SectionModel(); // Shared attributes $section->id = craft()->request->getPost('sectionId'); $section->name = $teamName; $section->handle = $slug; $section->type = SectionType::Channel; $section->enableVersioning = true; $section->hasUrls = true; $section->template = 'teams/entries/_entry'; $locales = array(); $primaryLocaleId = craft()->i18n->getPrimarySiteLocaleId(); $localeIds = array($primaryLocaleId); foreach ($localeIds as $localeId) { $locales[$localeId] = new SectionLocaleModel(array('locale' => $localeId, 'enabledByDefault' => true, 'urlFormat' => 'teams/' . $slug . '/entries/{slug}', 'nestedUrlFormat' => null)); } $section->setLocales($locales); //Create a new user group $group = new UserGroupModel(); $group->id = craft()->request->getPost('groupId'); $group->name = $teamName; $group->handle = $slug; if (craft()->sections->saveSection($section)) { $model->setAttribute('sectionId', $section->id); if (!craft()->userGroups->saveGroup($group)) { craft()->userSession->setError(Craft::t('Team group could not be saved.')); return; } $model->setAttribute('groupId', $group->id); $sectionId = $section->id; $permissions = array('accessCp', 'editEntries:' . $sectionId, 'createEntries:' . $sectionId, 'publishEntries:' . $sectionId, 'deleteEntries:' . $sectionId, 'editPeerEntries:' . $sectionId, 'publishPeerEntries:' . $sectionId, 'deletePeerEntries:' . $sectionId, 'editPeerEntryDrafts:' . $sectionId, 'publishPeerEntryDrafts:' . $sectionId, 'deletePeerEntryDrafts:' . $sectionId); if (!craft()->userPermissions->saveGroupPermissions($group->id, $permissions)) { craft()->userSession->setNotice(Craft::t('Team permissions could not be saved.')); return; } if (craft()->teamManager->saveTeam($model)) { //Apply the field layout $structure = craft()->sections->getEntryTypesByHandle('structure')[0]; //Must be the only one $model->updateEntryType($structure->fieldLayoutId); craft()->userSession->setNotice(Craft::t('Team successfully saved.')); } else { craft()->userSession->setError(Craft::t('Team could not be saved.')); } } else { craft()->userSession->setError(Craft::t('Team section could not be saved.')); } $this->redirect('teammanager'); }
/** * @param SectionModel $section * @param $localeDefinitions */ private function populateSectionLocales(SectionModel $section, $localeDefinitions) { $locales = $section->getLocales(); foreach ($localeDefinitions as $localeId => $localeDef) { $locale = array_key_exists($localeId, $locales) ? $locales[$localeId] : new SectionLocaleModel(); $locale->setAttributes(array('locale' => $localeId, 'enabledByDefault' => $localeDef['enabledByDefault'], 'urlFormat' => $localeDef['urlFormat'], 'nestedUrlFormat' => $localeDef['nestedUrlFormat'])); // Todo: Is this a hack? I don't see another way. // Todo: Might need a sorting order as well? It's NULL at the moment. craft()->db->createCommand()->insertOrUpdate('locales', array('locale' => $locale->locale), array()); $locales[$localeId] = $locale; } $section->setLocales($locales); }