示例#1
0
 /**
  * Saves an entry type.
  *
  * @param EntryTypeModel $entryType
  *
  * @throws Exception
  * @throws \CDbException
  * @throws \Exception
  * @return bool
  */
 public function saveEntryType(EntryTypeModel $entryType)
 {
     if ($entryType->id) {
         $entryTypeRecord = EntryTypeRecord::model()->findById($entryType->id);
         if (!$entryTypeRecord) {
             throw new Exception(Craft::t('No entry type exists with the ID “{id}”.', array('id' => $entryType->id)));
         }
         $isNewEntryType = false;
         $oldEntryType = EntryTypeModel::populateModel($entryTypeRecord);
     } else {
         $entryTypeRecord = new EntryTypeRecord();
         $isNewEntryType = true;
     }
     $entryTypeRecord->sectionId = $entryType->sectionId;
     $entryTypeRecord->name = $entryType->name;
     $entryTypeRecord->handle = $entryType->handle;
     $entryTypeRecord->hasTitleField = $entryType->hasTitleField;
     $entryTypeRecord->titleLabel = $entryType->hasTitleField ? $entryType->titleLabel : null;
     $entryTypeRecord->titleFormat = !$entryType->hasTitleField ? $entryType->titleFormat : null;
     $entryTypeRecord->validate();
     $entryType->addErrors($entryTypeRecord->getErrors());
     if (!$entryType->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             // Fire an 'onBeforeSaveEntryType' event
             $event = new Event($this, array('entryType' => $entryType, 'isNewEntryType' => $isNewEntryType));
             $this->onBeforeSaveEntryType($event);
             // Is the event giving us the go-ahead?
             if ($event->performAction) {
                 if (!$isNewEntryType && $oldEntryType->fieldLayoutId) {
                     // Drop the old field layout
                     craft()->fields->deleteLayoutById($oldEntryType->fieldLayoutId);
                 }
                 // Save the new one
                 $fieldLayout = $entryType->getFieldLayout();
                 craft()->fields->saveLayout($fieldLayout);
                 // Update the entry type record/model with the new layout ID
                 $entryType->fieldLayoutId = $fieldLayout->id;
                 $entryTypeRecord->fieldLayoutId = $fieldLayout->id;
                 $entryTypeRecord->save(false);
                 // Now that we have an entry type ID, save it on the model
                 if (!$entryType->id) {
                     $entryType->id = $entryTypeRecord->id;
                 }
                 // Might as well update our cache of the entry type while we have it.
                 $this->_entryTypesById[$entryType->id] = $entryType;
                 $success = true;
             } else {
                 $success = false;
             }
             // Commit the transaction regardless of whether we saved the user, in case something changed
             // in onBeforeSaveEntryType
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
     } else {
         $success = false;
     }
     if ($success) {
         // Fire an 'onSaveEntryType' event
         $this->onSaveEntryType(new Event($this, array('entryType' => $entryType, 'isNewEntryType' => $isNewEntryType)));
     }
     return $success;
 }
示例#2
0
 /**
  * Saves an entry type.
  *
  * @param EntryTypeModel $entryType
  *
  * @throws \Exception
  * @return bool
  */
 public function saveEntryType(EntryTypeModel $entryType)
 {
     if ($entryType->id) {
         $entryTypeRecord = EntryTypeRecord::model()->findById($entryType->id);
         if (!$entryTypeRecord) {
             throw new Exception(Craft::t('No entry type exists with the ID “{id}”', array('id' => $entryType->id)));
         }
         $isNewEntryType = false;
         $oldEntryType = EntryTypeModel::populateModel($entryTypeRecord);
     } else {
         $entryTypeRecord = new EntryTypeRecord();
         $isNewEntryType = true;
     }
     $entryTypeRecord->sectionId = $entryType->sectionId;
     $entryTypeRecord->name = $entryType->name;
     $entryTypeRecord->handle = $entryType->handle;
     $entryTypeRecord->hasTitleField = $entryType->hasTitleField;
     $entryTypeRecord->titleLabel = $entryType->hasTitleField ? $entryType->titleLabel : null;
     $entryTypeRecord->titleFormat = !$entryType->hasTitleField ? $entryType->titleFormat : null;
     $entryTypeRecord->validate();
     $entryType->addErrors($entryTypeRecord->getErrors());
     if (!$entryType->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             if (!$isNewEntryType && $oldEntryType->fieldLayoutId) {
                 // Drop the old field layout
                 craft()->fields->deleteLayoutById($oldEntryType->fieldLayoutId);
             }
             // Save the new one
             $fieldLayout = $entryType->getFieldLayout();
             craft()->fields->saveLayout($fieldLayout);
             // Update the entry type record/model with the new layout ID
             $entryType->fieldLayoutId = $fieldLayout->id;
             $entryTypeRecord->fieldLayoutId = $fieldLayout->id;
             $entryTypeRecord->save(false);
             // Now that we have an entry type ID, save it on the model
             if (!$entryType->id) {
                 $entryType->id = $entryTypeRecord->id;
             }
             // Might as well update our cache of the entry type while we have it.
             $this->_entryTypesById[$entryType->id] = $entryType;
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }