/**
  * 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));
 }
 /**
  * Saves a section.
  *
  * @param SectionModel $section
  *
  * @throws \Exception
  * @return bool
  */
 public function saveSection(SectionModel $section)
 {
     if ($section->id) {
         $sectionRecord = SectionRecord::model()->with('structure')->findById($section->id);
         if (!$sectionRecord) {
             throw new Exception(Craft::t('No section exists with the ID “{id}”.', array('id' => $section->id)));
         }
         $oldSection = SectionModel::populateModel($sectionRecord);
         $isNewSection = false;
     } else {
         $sectionRecord = new SectionRecord();
         $isNewSection = true;
     }
     // Shared attributes
     $sectionRecord->name = $section->name;
     $sectionRecord->handle = $section->handle;
     $sectionRecord->type = $section->type;
     $sectionRecord->enableVersioning = $section->enableVersioning;
     // Type-specific attributes
     if ($section->type == SectionType::Single) {
         $sectionRecord->hasUrls = $section->hasUrls = true;
     } else {
         $sectionRecord->hasUrls = $section->hasUrls;
     }
     if ($section->hasUrls) {
         $sectionRecord->template = $section->template;
     } else {
         $sectionRecord->template = $section->template = null;
     }
     $sectionRecord->validate();
     $section->addErrors($sectionRecord->getErrors());
     // Make sure that all of the URL formats are set properly
     $sectionLocales = $section->getLocales();
     if (!$sectionLocales) {
         $section->addError('localeErrors', Craft::t('At least one locale must be selected for the section.'));
     }
     $firstSectionLocale = null;
     foreach ($sectionLocales as $localeId => $sectionLocale) {
         // Is this the first one?
         if ($firstSectionLocale === null) {
             $firstSectionLocale = $sectionLocale;
         }
         if ($section->type == SectionType::Single) {
             $errorKey = 'urlFormat-' . $localeId;
             if (empty($sectionLocale->urlFormat)) {
                 $section->addError($errorKey, Craft::t('URI cannot be blank.'));
             } else {
                 if ($section) {
                     // Make sure no other elements are using this URI already
                     $query = craft()->db->createCommand()->from('elements_i18n elements_i18n')->where(array('and', 'elements_i18n.locale = :locale', 'elements_i18n.uri = :uri'), array(':locale' => $localeId, ':uri' => $sectionLocale->urlFormat));
                     if ($section->id) {
                         $query->join('entries entries', 'entries.id = elements_i18n.elementId')->andWhere('entries.sectionId != :sectionId', array(':sectionId' => $section->id));
                     }
                     $count = $query->count('elements_i18n.id');
                     if ($count) {
                         $section->addError($errorKey, Craft::t('This URI is already in use.'));
                     }
                 }
             }
             $sectionLocale->nestedUrlFormat = null;
         } else {
             if ($section->hasUrls) {
                 $urlFormatAttributes = array('urlFormat');
                 $sectionLocale->urlFormatIsRequired = true;
                 if ($section->type == SectionType::Structure && $section->maxLevels != 1) {
                     $urlFormatAttributes[] = 'nestedUrlFormat';
                     $sectionLocale->nestedUrlFormatIsRequired = true;
                 } else {
                     $sectionLocale->nestedUrlFormat = null;
                 }
                 foreach ($urlFormatAttributes as $attribute) {
                     if (!$sectionLocale->validate(array($attribute))) {
                         $section->addError($attribute . '-' . $localeId, $sectionLocale->getError($attribute));
                     }
                 }
             } else {
                 $sectionLocale->urlFormat = null;
                 $sectionLocale->nestedUrlFormat = null;
             }
         }
     }
     if (!$section->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             // Fire an 'onBeforeSaveSection' event
             $event = new Event($this, array('section' => $section, 'isNewSection' => $isNewSection));
             $this->onBeforeSaveSection($event);
             // Is the event giving us the go-ahead?
             if ($event->performAction) {
                 // Do we need to create a structure?
                 if ($section->type == SectionType::Structure) {
                     if (!$isNewSection && $oldSection->type == SectionType::Structure) {
                         $structure = craft()->structures->getStructureById($oldSection->structureId);
                         $isNewStructure = false;
                     }
                     if (empty($structure)) {
                         $structure = new StructureModel();
                         $isNewStructure = true;
                     }
                     $structure->maxLevels = $section->maxLevels;
                     craft()->structures->saveStructure($structure);
                     $sectionRecord->structureId = $structure->id;
                     $section->structureId = $structure->id;
                 } else {
                     if (!$isNewSection && $oldSection->structureId) {
                         // Delete the old one
                         craft()->structures->deleteStructureById($oldSection->structureId);
                         $sectionRecord->structureId = null;
                     }
                 }
                 $sectionRecord->save(false);
                 // Now that we have a section ID, save it on the model
                 if ($isNewSection) {
                     $section->id = $sectionRecord->id;
                 }
                 // Might as well update our cache of the section while we have it. (It's possible that the URL format
                 //includes {section.handle} or something...)
                 $this->_sectionsById[$section->id] = $section;
                 // Update the sections_i18n table
                 $newLocaleData = array();
                 if (!$isNewSection) {
                     // Get the old section locales
                     $oldSectionLocaleRecords = SectionLocaleRecord::model()->findAllByAttributes(array('sectionId' => $section->id));
                     $oldSectionLocales = SectionLocaleModel::populateModels($oldSectionLocaleRecords, 'locale');
                 }
                 foreach ($sectionLocales as $localeId => $locale) {
                     // Was this already selected?
                     if (!$isNewSection && isset($oldSectionLocales[$localeId])) {
                         $oldLocale = $oldSectionLocales[$localeId];
                         // Has anything changed?
                         if ($locale->enabledByDefault != $oldLocale->enabledByDefault || $locale->urlFormat != $oldLocale->urlFormat || $locale->nestedUrlFormat != $oldLocale->nestedUrlFormat) {
                             craft()->db->createCommand()->update('sections_i18n', array('enabledByDefault' => (int) $locale->enabledByDefault, 'urlFormat' => $locale->urlFormat, 'nestedUrlFormat' => $locale->nestedUrlFormat), array('id' => $oldLocale->id));
                         }
                     } else {
                         $newLocaleData[] = array($section->id, $localeId, (int) $locale->enabledByDefault, $locale->urlFormat, $locale->nestedUrlFormat);
                     }
                 }
                 // Insert the new locales
                 craft()->db->createCommand()->insertAll('sections_i18n', array('sectionId', 'locale', 'enabledByDefault', 'urlFormat', 'nestedUrlFormat'), $newLocaleData);
                 if (!$isNewSection) {
                     // Drop any locales that are no longer being used, as well as the associated entry/element locale
                     // rows
                     $droppedLocaleIds = array_diff(array_keys($oldSectionLocales), array_keys($sectionLocales));
                     if ($droppedLocaleIds) {
                         craft()->db->createCommand()->delete('sections_i18n', array('and', 'sectionId = :sectionId', array('in', 'locale', $droppedLocaleIds)), array(':sectionId' => $section->id));
                     }
                 }
                 // Make sure there's at least one entry type for this section
                 $entryTypeId = null;
                 if (!$isNewSection) {
                     // Let's grab all of the entry type IDs to save ourselves a query down the road if this is a Single
                     $entryTypeIds = craft()->db->createCommand()->select('id')->from('entrytypes')->where('sectionId = :sectionId', array(':sectionId' => $section->id))->queryColumn();
                     if ($entryTypeIds) {
                         $entryTypeId = array_shift($entryTypeIds);
                     }
                 }
                 if (!$entryTypeId) {
                     $entryType = new EntryTypeModel();
                     $entryType->sectionId = $section->id;
                     $entryType->name = $section->name;
                     $entryType->handle = $section->handle;
                     if ($section->type == SectionType::Single) {
                         $entryType->hasTitleField = false;
                         $entryType->titleLabel = null;
                         $entryType->titleFormat = '{section.name|raw}';
                     } else {
                         $entryType->hasTitleField = true;
                         $entryType->titleLabel = Craft::t('Title');
                         $entryType->titleFormat = null;
                     }
                     $this->saveEntryType($entryType);
                     $entryTypeId = $entryType->id;
                 }
                 // Now, regardless of whether the section type changed or not, let the section type make sure
                 // everything is cool
                 switch ($section->type) {
                     case SectionType::Single:
                         // Make sure that there is one and only one Entry Type and Entry for this section.
                         $singleEntryId = null;
                         if (!$isNewSection) {
                             // Make sure there's only one entry in this section
                             $entryIds = craft()->db->createCommand()->select('id')->from('entries')->where('sectionId = :sectionId', array(':sectionId' => $section->id))->queryColumn();
                             if ($entryIds) {
                                 $singleEntryId = array_shift($entryIds);
                                 // If there are any more, get rid of them
                                 if ($entryIds) {
                                     craft()->elements->deleteElementById($entryIds);
                                 }
                                 // Make sure it's enabled and all that.
                                 craft()->db->createCommand()->update('elements', array('enabled' => 1, 'archived' => 0), array('id' => $singleEntryId));
                                 craft()->db->createCommand()->update('entries', array('typeId' => $entryTypeId, 'authorId' => null, 'postDate' => DateTimeHelper::currentTimeForDb(), 'expiryDate' => null), array('id' => $singleEntryId));
                             }
                             // Make sure there's only one entry type for this section
                             if ($entryTypeIds) {
                                 $this->deleteEntryTypeById($entryTypeIds);
                             }
                         }
                         if (!$singleEntryId) {
                             // Create it, baby
                             $singleEntry = new EntryModel();
                             $singleEntry->locale = $firstSectionLocale->locale;
                             $singleEntry->sectionId = $section->id;
                             $singleEntry->typeId = $entryTypeId;
                             $singleEntry->getContent()->title = $section->name;
                             craft()->entries->saveEntry($singleEntry);
                         }
                         break;
                     case SectionType::Structure:
                         if (!$isNewSection && $isNewStructure) {
                             // Add all of the entries to the structure
                             $criteria = craft()->elements->getCriteria(ElementType::Entry);
                             $criteria->locale = ArrayHelper::getFirstKey($oldSectionLocales);
                             $criteria->sectionId = $section->id;
                             $criteria->status = null;
                             $criteria->localeEnabled = null;
                             $criteria->order = 'elements.id';
                             $criteria->limit = 25;
                             do {
                                 $batchEntries = $criteria->find();
                                 foreach ($batchEntries as $entry) {
                                     craft()->structures->appendToRoot($section->structureId, $entry, 'insert');
                                 }
                                 $criteria->offset += 25;
                             } while ($batchEntries);
                         }
                         break;
                 }
                 // Finally, deal with the existing entries...
                 if (!$isNewSection) {
                     $criteria = craft()->elements->getCriteria(ElementType::Entry);
                     // Get the most-primary locale that this section was already enabled in
                     $locales = array_values(array_intersect(craft()->i18n->getSiteLocaleIds(), array_keys($oldSectionLocales)));
                     if ($locales) {
                         $criteria->locale = $locales[0];
                         $criteria->sectionId = $section->id;
                         $criteria->status = null;
                         $criteria->localeEnabled = null;
                         $criteria->limit = null;
                         craft()->tasks->createTask('ResaveElements', Craft::t('Resaving {section} entries', array('section' => $section->name)), array('elementType' => ElementType::Entry, 'criteria' => $criteria->getAttributes()));
                     }
                 }
                 $success = true;
             } else {
                 $success = false;
             }
             // Commit the transaction regardless of whether we saved the section, in case something changed
             // in onBeforeSaveSection
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
     } else {
         $success = false;
     }
     if ($success) {
         // Fire an 'onSaveSection' event
         $this->onSaveSection(new Event($this, array('section' => $section, 'isNewSection' => $isNewSection)));
     }
     return $success;
 }
 /**
  * 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 &amp; 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);
     }
 }
 /**
  * Saves a section.
  *
  * @param SectionModel $section
  * @throws \Exception
  * @return bool
  */
 public function saveSection(SectionModel $section)
 {
     $sectionRecord = $this->_getSectionRecordById($section->id);
     $isNewSection = $sectionRecord->isNewRecord();
     if (!$isNewSection) {
         $oldSection = SectionModel::populateModel($sectionRecord);
     }
     $sectionRecord->name = $section->name;
     $sectionRecord->handle = $section->handle;
     $sectionRecord->titleLabel = $section->titleLabel;
     $sectionRecord->hasUrls = $section->hasUrls;
     if ($section->hasUrls) {
         $sectionRecord->template = $section->template;
     } else {
         $sectionRecord->template = $section->template = null;
     }
     $sectionRecord->validate();
     $section->addErrors($sectionRecord->getErrors());
     // Make sure that all of the URL formats are set properly
     foreach ($section->getLocales() as $localeId => $sectionLocale) {
         if ($section->hasUrls) {
             $errorKey = 'urlFormat-' . $localeId;
             if (empty($sectionLocale->urlFormat)) {
                 $section->addError($errorKey, Craft::t('{attribute} cannot be blank.', array('attribute' => 'URL Format')));
             } else {
                 if (strpos($sectionLocale->urlFormat, '{slug}') === false) {
                     $section->addError($errorKey, Craft::t('URL Format must contain “{slug}”'));
                 }
             }
         } else {
             $sectionLocale->urlFormat = null;
         }
     }
     if (!$section->hasErrors()) {
         $transaction = craft()->db->beginTransaction();
         try {
             if (!$isNewSection && $oldSection->fieldLayoutId) {
                 // Drop the old field layout
                 craft()->fields->deleteLayoutById($oldSection->fieldLayoutId);
             }
             // Save the new one
             $fieldLayout = $section->getFieldLayout();
             craft()->fields->saveLayout($fieldLayout);
             // Update the section record/model with the new layout ID
             $section->fieldLayoutId = $fieldLayout->id;
             $sectionRecord->fieldLayoutId = $fieldLayout->id;
             $sectionRecord->save(false);
             // Now that we have a section ID, save it on the model
             if (!$section->id) {
                 $section->id = $sectionRecord->id;
             }
             // Might as well update our cache of the section while we have it.
             // (It's possilbe that the URL format includes {section.handle} or something...)
             $this->_sectionsById[$section->id] = $section;
             // Update the sections_i18n table
             $newLocaleData = array();
             if (!$isNewSection) {
                 // Get the old section locales
                 $oldSectionLocaleRecords = SectionLocaleRecord::model()->findAllByAttributes(array('sectionId' => $section->id));
                 $oldSectionLocales = SectionLocaleModel::populateModels($oldSectionLocaleRecords, 'locale');
             }
             foreach ($section->getLocales() as $localeId => $locale) {
                 $updateEntries = false;
                 // Was this already selected?
                 if (!$isNewSection && isset($oldSectionLocales[$localeId])) {
                     $oldLocale = $oldSectionLocales[$localeId];
                     // Has the URL format changed?
                     if ($locale->urlFormat != $oldLocale->urlFormat) {
                         craft()->db->createCommand()->update('sections_i18n', array('urlFormat' => $locale->urlFormat), array('id' => $oldLocale->id));
                         $updateEntries = true;
                     }
                 } else {
                     $newLocaleData[] = array($section->id, $localeId, $locale->urlFormat);
                     if (!$isNewSection) {
                         $updateEntries = true;
                     }
                 }
                 if ($updateEntries && $section->hasUrls) {
                     // This may take a while...
                     set_time_limit(120);
                     // Fetch all the entries in this section
                     $entries = craft()->elements->getCriteria(ElementType::Entry, array('sectionId' => $section->id, 'locale' => $localeId, 'limit' => null))->find();
                     foreach ($entries as $entry) {
                         $uri = craft()->templates->renderObjectTemplate($locale->urlFormat, $entry);
                         if ($uri != $entry->uri) {
                             craft()->db->createCommand()->update('elements_i18n', array('uri' => $uri), array('elementId' => $entry->id, 'locale' => $localeId));
                         }
                     }
                 }
             }
             // Insert the new locales
             craft()->db->createCommand()->insertAll('sections_i18n', array('sectionId', 'locale', 'urlFormat'), $newLocaleData);
             if (!$isNewSection) {
                 // Drop the old ones
                 $disabledLocaleIds = array_diff(array_keys($oldSectionLocales), array_keys($section->getLocales()));
                 foreach ($disabledLocaleIds as $localeId) {
                     craft()->db->createCommand()->delete('sections_i18n', array('id' => $oldSectionLocales[$localeId]->id));
                 }
                 // Drop the old entry URIs if the section no longer has URLs
                 if (!$section->hasUrls && $oldSection->hasUrls) {
                     // Clear out all the URIs
                     $entryIds = craft()->db->createCommand()->select('id')->from('entries')->where(array('sectionId' => $section->id))->queryColumn();
                     craft()->db->createCommand()->update('elements_i18n', array('uri' => null), array('in', 'elementId', $entryIds));
                 }
             }
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }
 /**
  * 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);
     }
 }