/** * @param GlobalSetModel $globalSet * @return array */ private function getGlobalDefinition(GlobalSetModel $globalSet) { return array('name' => $globalSet->name, 'fieldLayout' => craft()->artVandelay_fields->getFieldLayoutDefinition($globalSet->getFieldLayout())); }
/** * 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; } }