/**
  * Saves a global set.
  */
 public function actionSaveSet()
 {
     $this->requirePostRequest();
     $globalSet = new GlobalSetModel();
     // Set the simple stuff
     $globalSet->id = craft()->request->getPost('setId');
     $globalSet->name = craft()->request->getPost('name');
     $globalSet->handle = craft()->request->getPost('handle');
     // Set the field layout
     $fieldLayout = craft()->fields->assembleLayoutFromPost(false);
     $fieldLayout->type = ElementType::GlobalSet;
     $globalSet->setFieldLayout($fieldLayout);
     // Save it
     if (craft()->globals->saveSet($globalSet)) {
         craft()->userSession->setNotice(Craft::t('Global set saved.'));
         // TODO: Remove for 2.0
         if (isset($_POST['redirect']) && strpos($_POST['redirect'], '{setId}') !== false) {
             Craft::log('The {setId} token within the ‘redirect’ param on globals/saveSet requests has been deprecated. Use {id} instead.', LogLevel::Warning);
             $_POST['redirect'] = str_replace('{setId}', '{id}', $_POST['redirect']);
         }
         $this->redirectToPostedUrl($globalSet);
     } else {
         craft()->userSession->setError(Craft::t('Couldn’t save global set.'));
     }
     // Send the global set back to the template
     craft()->urlManager->setRouteVariables(array('globalSet' => $globalSet));
 }
 /**
  * Saves a global set.
  *
  * @param GlobalSetModel $globalSet
  *
  * @throws \Exception
  * @return bool
  */
 public function saveSet(GlobalSetModel $globalSet)
 {
     $isNewSet = !$globalSet->id;
     if (!$isNewSet) {
         $globalSetRecord = GlobalSetRecord::model()->findById($globalSet->id);
         if (!$globalSetRecord) {
             throw new Exception(Craft::t('No global set exists with the ID “{id}”.', array('id' => $globalSet->id)));
         }
         $oldSet = GlobalSetModel::populateModel($globalSetRecord);
     } else {
         $globalSetRecord = new GlobalSetRecord();
     }
     $globalSetRecord->name = $globalSet->name;
     $globalSetRecord->handle = $globalSet->handle;
     $globalSetRecord->validate();
     $globalSet->addErrors($globalSetRecord->getErrors());
     if (!$globalSet->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if (craft()->elements->saveElement($globalSet, false)) {
                 // Now that we have an element ID, save it on the other stuff
                 if ($isNewSet) {
                     $globalSetRecord->id = $globalSet->id;
                 }
                 // Is there a new field layout?
                 $fieldLayout = $globalSet->getFieldLayout();
                 if (!$fieldLayout->id) {
                     // Delete the old one
                     if (!$isNewSet && $oldSet->fieldLayoutId) {
                         craft()->fields->deleteLayoutById($oldSet->fieldLayoutId);
                     }
                     // Save the new one
                     craft()->fields->saveLayout($fieldLayout);
                     // Update the global set record/model with the new layout ID
                     $globalSet->fieldLayoutId = $fieldLayout->id;
                     $globalSetRecord->fieldLayoutId = $fieldLayout->id;
                 }
                 $globalSetRecord->save(false);
                 if ($transaction !== null) {
                     $transaction->commit();
                 }
                 return true;
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
     }
     return false;
 }
 /**
  * Populates an element model based on a query result.
  *
  * @param array $row
  * @return array
  */
 public function populateElementModel($row)
 {
     return GlobalSetModel::populateModel($row);
 }
 /**
  * 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 &amp; 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);
     }
 }
 /**
  * @param GlobalSetModel $globalSet
  * @param array $globalSetDefinition
  * @param string $globalSetHandle
  */
 private function populateGlobalSet(GlobalSetModel $globalSet, array $globalSetDefinition, $globalSetHandle)
 {
     $globalSet->setAttributes(array('handle' => $globalSetHandle, 'name' => $globalSetDefinition['name']));
     $fieldLayout = craft()->artVandelay_fields->getFieldLayout($globalSetDefinition['fieldLayout']);
     $globalSet->setFieldLayout($fieldLayout);
 }
 /**
  * Saves a global set.
  *
  * @param GlobalSetModel $globalSet
  * @throws \Exception
  * @return bool
  */
 public function saveSet(GlobalSetModel $globalSet)
 {
     $isNewSet = empty($globalSet->id);
     if (!$isNewSet) {
         $globalSetRecord = GlobalSetRecord::model()->with('element')->findById($globalSet->id);
         if (!$globalSetRecord) {
             throw new Exception(Craft::t('No global set exists with the ID “{id}”', array('id' => $globalSet->id)));
         }
         $oldSet = GlobalSetModel::populateModel($globalSetRecord);
         $elementRecord = $globalSetRecord->element;
     } else {
         $globalSetRecord = new GlobalSetRecord();
         $elementRecord = new ElementRecord();
         $elementRecord->type = ElementType::GlobalSet;
     }
     $globalSetRecord->name = $globalSet->name;
     $globalSetRecord->handle = $globalSet->handle;
     $globalSetRecord->validate();
     $globalSet->addErrors($globalSetRecord->getErrors());
     $elementRecord->enabled = $globalSet->enabled;
     $elementRecord->validate();
     $globalSet->addErrors($elementRecord->getErrors());
     if (!$globalSet->hasErrors()) {
         $transaction = craft()->db->beginTransaction();
         try {
             if (!$isNewSet && $oldSet->fieldLayoutId) {
                 // Drop the old field layout
                 craft()->fields->deleteLayoutById($oldSet->fieldLayoutId);
             }
             // Save the new one
             $fieldLayout = $globalSet->getFieldLayout();
             craft()->fields->saveLayout($fieldLayout, false);
             // Update the set record/model with the new layout ID
             $globalSet->fieldLayoutId = $fieldLayout->id;
             $globalSetRecord->fieldLayoutId = $fieldLayout->id;
             // Save the element record first
             $elementRecord->save(false);
             // Now that we have an element ID, save it on the other stuff
             if (!$globalSet->id) {
                 $globalSet->id = $elementRecord->id;
                 $globalSetRecord->id = $globalSet->id;
             }
             $globalSetRecord->save(false);
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }