/**
  * 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;
 }
 /**
  * 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;
     }
 }