/**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     Craft::log('Adding a fieldLayoutId column for Asset sources.', LogLevel::Info, true);
     if (!craft()->db->columnExists('assetsources', 'fieldLayoutId')) {
         $this->addColumnAfter('assetsources', 'fieldLayoutId', array('column' => ColumnType::Int), 'id');
         $this->addForeignKey('assetsources', 'fieldLayoutId', 'fieldlayouts', 'id', 'SET NULL');
         $layoutId = craft()->db->createCommand()->select('id')->from('fieldlayouts')->where('type = :elementType', array(':elementType' => ElementType::Asset))->queryScalar();
         if (!$layoutId) {
             Craft::log('No layouts found for Assets.', LogLevel::Info, true);
             return true;
         } else {
             $sourceIds = craft()->assetSources->getAllSourceIds();
             if (empty($sourceIds)) {
                 Craft::log("No sources exist for Assets.", LogLevel::Info, true);
                 return true;
             }
             Craft::log('Copying the existing Assets layout to all sources.', LogLevel::Info, true);
             // Add the existing layout to the first source in list
             $sourceId = array_pop($sourceIds);
             craft()->db->createCommand()->update('assetsources', array('fieldLayoutId' => $layoutId), array('id' => $sourceId));
             $layout = FieldLayoutModel::populateModel(FieldLayoutRecord::model()->findByPk($layoutId));
             $fields = $layout->getFields();
             foreach ($sourceIds as $sourceId) {
                 $layout = new FieldLayoutRecord();
                 $layout->type = ElementType::Asset;
                 $layout->save();
                 foreach ($fields as $field) {
                     $fieldRecord = new FieldLayoutFieldRecord();
                     $fieldRecord->layoutId = $layout->id;
                     $fieldRecord->fieldId = $field->fieldId;
                     $fieldRecord->required = $field->required;
                     $fieldRecord->sortOrder = $field->sortOrder;
                     $fieldRecord->save();
                 }
                 craft()->db->createCommand()->update('assetsources', array('fieldLayoutId' => $layout->id), array('id' => $sourceId));
             }
             Craft::log('Copied the existing Assets layout to ' . (count($sourceIds) + 1) . ' sources.', LogLevel::Info, true);
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Saves a field layout.
  *
  * @param FieldLayoutModel $layout
  * @param bool $saveTabs Whether to save tab records.
  * @return bool
  */
 public function saveLayout(FieldLayoutModel $layout, $saveTabs = true)
 {
     // First save the layout
     $layoutRecord = new FieldLayoutRecord();
     $layoutRecord->type = $layout->type;
     $layoutRecord->save(false);
     $layout->id = $layoutRecord->id;
     if ($saveTabs) {
         foreach ($layout->getTabs() as $tab) {
             $tabRecord = new FieldLayoutTabRecord();
             $tabRecord->layoutId = $layout->id;
             $tabRecord->name = $tab->name;
             $tabRecord->sortOrder = $tab->sortOrder;
             $tabRecord->save(false);
             $tab->id = $tabRecord->id;
             foreach ($tab->getFields() as $field) {
                 $fieldRecord = new FieldLayoutFieldRecord();
                 $fieldRecord->layoutId = $layout->id;
                 $fieldRecord->tabId = $tab->id;
                 $fieldRecord->fieldId = $field->fieldId;
                 $fieldRecord->required = $field->required;
                 $fieldRecord->sortOrder = $field->sortOrder;
                 $fieldRecord->save(false);
                 $field->id = $fieldRecord->id;
             }
         }
     } else {
         foreach ($layout->getFields() as $field) {
             $fieldRecord = new FieldLayoutFieldRecord();
             $fieldRecord->layoutId = $layout->id;
             $fieldRecord->fieldId = $field->fieldId;
             $fieldRecord->required = $field->required;
             $fieldRecord->sortOrder = $field->sortOrder;
             $fieldRecord->save(false);
             $field->id = $fieldRecord->id;
         }
     }
     return true;
 }
 /**
  * Saves a field layout.
  *
  * @param FieldLayoutModel $layout
  *
  * @return bool
  */
 public function saveLayout(FieldLayoutModel $layout)
 {
     // First save the layout
     $layoutRecord = new FieldLayoutRecord();
     $layoutRecord->type = $layout->type;
     $layoutRecord->save(false);
     $layout->id = $layoutRecord->id;
     foreach ($layout->getTabs() as $tab) {
         $tabRecord = new FieldLayoutTabRecord();
         $tabRecord->layoutId = $layout->id;
         $tabRecord->name = $tab->name;
         $tabRecord->sortOrder = $tab->sortOrder;
         $tabRecord->save(false);
         $tab->id = $tabRecord->id;
         foreach ($tab->getFields() as $field) {
             $fieldRecord = new FieldLayoutFieldRecord();
             $fieldRecord->layoutId = $layout->id;
             $fieldRecord->tabId = $tab->id;
             $fieldRecord->fieldId = $field->fieldId;
             $fieldRecord->required = $field->required;
             $fieldRecord->sortOrder = $field->sortOrder;
             $fieldRecord->save(false);
             $field->id = $fieldRecord->id;
         }
     }
     // Fire an 'onSaveFieldLayout' event
     $this->onSaveFieldLayout(new Event($this, array('layout' => $layout)));
     return true;
 }
 /**
  * Gets a Field Layout Field's record.
  *
  * @access private
  * @param int $fieldId
  * @return FieldLayoutFieldRecord
  */
 private function _getFieldLayoutFieldRecordByFieldId($fieldId = null)
 {
     if ($fieldId) {
         $record = FieldLayoutFieldRecord::model()->find('fieldId=:fieldId', array(':fieldId' => $fieldId));
         if (!$record) {
             throw new Exception(Craft::t('No field exists with the ID “{id}”', array('id' => $fieldId)));
         }
     } else {
         $record = new FieldLayoutFieldRecord();
     }
     return $record;
 }
 /**
  * Edit a field.
  *
  * @param array $variables
  * @throws HttpException
  * @throws Exception
  */
 public function actionEditFieldTemplate(array $variables = array())
 {
     $formId = craft()->request->getSegment(3);
     $form = sproutForms()->forms->getFormById($formId);
     if (isset($variables['fieldId'])) {
         if (!isset($variables['field'])) {
             $field = craft()->fields->getFieldById($variables['fieldId']);
             $variables['field'] = $field;
             $fieldLayoutField = FieldLayoutFieldRecord::model()->find(array('condition' => 'fieldId = :fieldId AND layoutId = :layoutId', 'params' => array(':fieldId' => $field->id, ':layoutId' => $form->fieldLayoutId)));
             $variables['required'] = $fieldLayoutField->required;
             $variables['tabId'] = $fieldLayoutField->tabId;
             if (!isset($variables['field'])) {
                 throw new HttpException(404);
             }
         }
         $variables['title'] = isset($field->name) ? $field->name : "";
     } else {
         if (!isset($variables['field'])) {
             $variables['field'] = new FieldModel();
         }
         $variables['tabId'] = null;
         $variables['title'] = Craft::t('Create a new field');
     }
     $variables['sections'] = $form->getFieldLayout()->getTabs();
     $this->renderTemplate('sproutforms/forms/_editField', $variables);
 }
 /**
  * Saves a field layout and attaches it to the given pimped block type
  *
  * @param  PimpMyMatrix_BlockTypeModel $pimpedBlockType [description]
  * @return bool
  */
 public function saveFieldLayout(PimpMyMatrix_BlockTypeModel $pimpedBlockType)
 {
     // First, get the layout and save the old field layout id for later
     $layout = $pimpedBlockType->getFieldLayout();
     $oldFieldLayoutId = $pimpedBlockType->fieldLayoutId;
     // Second save the layout - replicated from FieldsService::saveLayout()
     // to allow us to retain the $layout->id for our own use
     if ($layout->getTabs()) {
         $layoutRecord = new FieldLayoutRecord();
         $layoutRecord->type = 'PimpMyMatrix_BlockType';
         $layoutRecord->save(false);
         $layout->id = $layoutRecord->id;
         foreach ($layout->getTabs() as $tab) {
             $tabRecord = new FieldLayoutTabRecord();
             $tabRecord->layoutId = $layout->id;
             $tabRecord->name = $tab->name;
             $tabRecord->sortOrder = $tab->sortOrder;
             $tabRecord->save(false);
             $tab->id = $tabRecord->id;
             foreach ($tab->getFields() as $field) {
                 $fieldRecord = new FieldLayoutFieldRecord();
                 $fieldRecord->layoutId = $layout->id;
                 $fieldRecord->tabId = $tab->id;
                 $fieldRecord->fieldId = $field->fieldId;
                 $fieldRecord->required = $field->required;
                 $fieldRecord->sortOrder = $field->sortOrder;
                 $fieldRecord->save(false);
                 $field->id = $fieldRecord->id;
             }
         }
         // Now we have saved the layout, update the id on the given
         // pimped blocktype model
         $pimpedBlockType->fieldLayoutId = $layout->id;
     } else {
         $pimpedBlockType->fieldLayoutId = null;
     }
     // Save our pimped block type again
     if ($this->saveBlockType($pimpedBlockType)) {
         // Delete the old field layout
         craft()->fields->deleteLayoutById($oldFieldLayoutId);
         return true;
     } else {
         return false;
     }
 }
 /**
  * @param SproutForms_FormModel $form
  *
  * @throws \Exception
  * @return bool
  */
 public function saveForm(SproutForms_FormModel $form)
 {
     $formRecord = new SproutForms_FormRecord();
     $isNewForm = true;
     if ($form->id && !$form->saveAsNew) {
         $formRecord = SproutForms_FormRecord::model()->findById($form->id);
         if (!$formRecord) {
             throw new Exception(Craft::t('No form exists with the ID “{id}”', array('id' => $form->id)));
         }
         $oldForm = SproutForms_FormModel::populateModel($formRecord);
         $isNewForm = false;
         $hasLayout = count($form->getFieldLayout()->getFields()) > 0;
         // Add the oldHandle to our model so we can determine if we
         // need to rename the content table
         $form->oldHandle = $formRecord->getOldHandle();
     }
     // Create our new Form Record
     $formRecord->name = $form->name;
     $formRecord->handle = $form->handle;
     $formRecord->titleFormat = $form->titleFormat ? $form->titleFormat : "{dateCreated|date('D, d M Y H:i:s')}";
     $formRecord->displaySectionTitles = $form->displaySectionTitles;
     $formRecord->groupId = $form->groupId;
     $formRecord->redirectUri = $form->redirectUri;
     $formRecord->submitAction = $form->submitAction;
     $formRecord->submitButtonText = $form->submitButtonText;
     $formRecord->notificationEnabled = $form->notificationEnabled;
     $formRecord->notificationRecipients = $form->notificationRecipients;
     $formRecord->notificationSubject = $form->notificationSubject;
     $formRecord->notificationSenderName = $form->notificationSenderName;
     $formRecord->notificationSenderEmail = $form->notificationSenderEmail;
     $formRecord->notificationReplyToEmail = $form->notificationReplyToEmail;
     $formRecord->validate();
     $form->addErrors($formRecord->getErrors());
     if ($form->saveAsNew) {
         $form->name = $formRecord->name;
         $form->handle = $formRecord->handle;
     }
     if (!$form->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             // Set the field context
             craft()->content->fieldContext = $form->getFieldContext();
             craft()->content->contentTable = $form->getContentTable();
             if ($isNewForm) {
                 $fieldLayout = $form->getFieldLayout();
                 // Save the field layout
                 craft()->fields->saveLayout($fieldLayout);
                 // Assign our new layout id info to our form model and records
                 $form->fieldLayoutId = $fieldLayout->id;
                 $form->setFieldLayout($fieldLayout);
                 $formRecord->fieldLayoutId = $fieldLayout->id;
             } else {
                 // If we have a layout use it, otherwise
                 // since this is an existing form, grab the oldForm layout
                 if ($hasLayout) {
                     // Delete our previous record
                     craft()->fields->deleteLayoutById($oldForm->fieldLayoutId);
                     $fieldLayout = $form->getFieldLayout();
                     // Save the field layout
                     craft()->fields->saveLayout($fieldLayout);
                     // Assign our new layout id info to our
                     // form model and records
                     $form->fieldLayoutId = $fieldLayout->id;
                     $form->setFieldLayout($fieldLayout);
                     $formRecord->fieldLayoutId = $fieldLayout->id;
                 } else {
                     // We don't have a field layout right now
                     $form->fieldLayoutId = NULL;
                 }
             }
             // Create the content table first since the form will need it
             $oldContentTable = $this->getContentTableName($form, true);
             $newContentTable = $this->getContentTableName($form);
             // Do we need to create/rename the content table?
             if (!craft()->db->tableExists($newContentTable)) {
                 if ($oldContentTable && craft()->db->tableExists($oldContentTable)) {
                     MigrationHelper::renameTable($oldContentTable, $newContentTable);
                 } else {
                     $this->_createContentTable($newContentTable);
                 }
             }
             if (craft()->elements->saveElement($form)) {
                 // Create the new fields
                 if ($form->saveAsNew) {
                     // Duplicate the fields in the newContent Table also set the fields in the craft fields table
                     $newFields = array();
                     foreach ($form->getFields() as $key => $value) {
                         $field = new FieldModel();
                         $field->name = $value->name;
                         $field->handle = $value->handle;
                         $field->instructions = $value->instructions;
                         $field->required = $value->required;
                         $field->translatable = (bool) $value->translatable;
                         $field->type = $value->type;
                         if (isset($value->settings)) {
                             $field->settings = $value->settings;
                         }
                         craft()->content->fieldContext = $form->getFieldContext();
                         craft()->content->contentTable = $form->getContentTable();
                         craft()->fields->saveField($field);
                         array_push($newFields, $field);
                         SproutFormsPlugin::log('Saved field as new ' . $field->id);
                     }
                     // Update fieldId on layoutfields table
                     $fieldLayout = $form->getFieldLayout();
                     $fieldLayoutIds = FieldLayoutFieldRecord::model()->findAll("layoutId = {$fieldLayout->id}");
                     foreach ($fieldLayoutIds as $key => $layout) {
                         SproutFormsPlugin::log('Updated field layout  ' . $layout->id);
                         $model = FieldLayoutFieldRecord::model()->findByPk($layout->id);
                         $model->fieldId = $newFields[$key]->id;
                         $model->save();
                     }
                 }
                 // Now that we have an element ID, save it on the other stuff
                 if ($isNewForm) {
                     $formRecord->id = $form->id;
                 }
                 // Save our Form Settings
                 $formRecord->save(false);
                 if ($transaction !== null) {
                     $transaction->commit();
                 }
                 return true;
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
     }
 }