Exemplo n.º 1
0
 /**
  * Sets the layout's tabs.
  *
  * @param array $tabs
  *
  * @return null
  */
 public function setTabs($tabs)
 {
     $this->_tabs = array();
     foreach ($tabs as $tab) {
         if (is_array($tab)) {
             $tab = new FieldLayoutTabModel($tab);
         }
         $tab->setLayout($this);
         $this->_tabs[] = $tab;
     }
 }
Exemplo n.º 2
0
 /**
  * Saves a block type.
  *
  * @param MatrixBlockTypeModel $blockType The block type to be saved.
  * @param bool                 $validate  Whether the block type should be validated before being saved.
  *                                        Defaults to `true`.
  *
  * @throws \Exception
  * @return bool Whether the block type was saved successfully.
  */
 public function saveBlockType(MatrixBlockTypeModel $blockType, $validate = true)
 {
     if (!$validate || $this->validateBlockType($blockType)) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             $contentService = craft()->content;
             $fieldsService = craft()->fields;
             $originalFieldContext = $contentService->fieldContext;
             $originalFieldColumnPrefix = $contentService->fieldColumnPrefix;
             $originalOldFieldColumnPrefix = $fieldsService->oldFieldColumnPrefix;
             // Get the block type record
             $blockTypeRecord = $this->_getBlockTypeRecord($blockType);
             $isNewBlockType = $blockType->isNew();
             if (!$isNewBlockType) {
                 // Get the old block type fields
                 $oldBlockTypeRecord = MatrixBlockTypeRecord::model()->findById($blockType->id);
                 $oldBlockType = MatrixBlockTypeModel::populateModel($oldBlockTypeRecord);
                 $contentService->fieldContext = 'matrixBlockType:' . $blockType->id;
                 $contentService->fieldColumnPrefix = 'field_' . $oldBlockType->handle . '_';
                 $fieldsService->oldFieldColumnPrefix = 'field_' . $oldBlockType->handle . '_';
                 $oldFieldsById = array();
                 foreach ($oldBlockType->getFields() as $field) {
                     $oldFieldsById[$field->id] = $field;
                 }
                 // Figure out which ones are still around
                 foreach ($blockType->getFields() as $field) {
                     if (!$field->isNew()) {
                         unset($oldFieldsById[$field->id]);
                     }
                 }
                 // Drop the old fields that aren't around anymore
                 foreach ($oldFieldsById as $field) {
                     $fieldsService->deleteField($field);
                 }
                 // Refresh the schema cache
                 craft()->db->getSchema()->refresh();
             }
             // Set the basic info on the new block type record
             $blockTypeRecord->fieldId = $blockType->fieldId;
             $blockTypeRecord->name = $blockType->name;
             $blockTypeRecord->handle = $blockType->handle;
             $blockTypeRecord->sortOrder = $blockType->sortOrder;
             // Save it, minus the field layout for now
             $blockTypeRecord->save(false);
             if ($isNewBlockType) {
                 // Set the new ID on the model
                 $blockType->id = $blockTypeRecord->id;
             }
             // Save the fields and field layout
             // -------------------------------------------------------------
             $fieldLayoutFields = array();
             $sortOrder = 0;
             // Resetting the fieldContext here might be redundant if this isn't a new blocktype but whatever
             $contentService->fieldContext = 'matrixBlockType:' . $blockType->id;
             $contentService->fieldColumnPrefix = 'field_' . $blockType->handle . '_';
             foreach ($blockType->getFields() as $field) {
                 // Hack to allow blank field names
                 if (!$field->name) {
                     $field->name = '__blank__';
                 }
                 if (!$fieldsService->saveField($field, false)) {
                     throw new Exception(Craft::t('An error occurred while saving this Matrix block type.'));
                 }
                 $fieldLayoutField = new FieldLayoutFieldModel();
                 $fieldLayoutField->fieldId = $field->id;
                 $fieldLayoutField->required = $field->required;
                 $fieldLayoutField->sortOrder = ++$sortOrder;
                 $fieldLayoutFields[] = $fieldLayoutField;
             }
             $contentService->fieldContext = $originalFieldContext;
             $contentService->fieldColumnPrefix = $originalFieldColumnPrefix;
             $fieldsService->oldFieldColumnPrefix = $originalOldFieldColumnPrefix;
             $fieldLayoutTab = new FieldLayoutTabModel();
             $fieldLayoutTab->name = 'Content';
             $fieldLayoutTab->sortOrder = 1;
             $fieldLayoutTab->setFields($fieldLayoutFields);
             $fieldLayout = new FieldLayoutModel();
             $fieldLayout->type = ElementType::MatrixBlock;
             $fieldLayout->setTabs(array($fieldLayoutTab));
             $fieldLayout->setFields($fieldLayoutFields);
             $fieldsService->saveLayout($fieldLayout);
             // Update the block type model & record with our new field layout ID
             $blockType->setFieldLayout($fieldLayout);
             $blockType->fieldLayoutId = $fieldLayout->id;
             $blockTypeRecord->fieldLayoutId = $fieldLayout->id;
             // Update the block type with the field layout ID
             $blockTypeRecord->save(false);
             if (!$isNewBlockType) {
                 // Delete the old field layout
                 $fieldsService->deleteLayoutById($oldBlockType->fieldLayoutId);
             }
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }
 /**
  * Assembles a field layout.
  *
  * @param array      $postedFieldLayout
  * @param array|null $requiredFields
  *
  * @return FieldLayoutModel
  */
 public function assembleLayout($postedFieldLayout, $requiredFields)
 {
     $tabs = array();
     $fields = array();
     $tabSortOrder = 0;
     foreach ($postedFieldLayout as $tabName => $fieldIds) {
         $tabFields = array();
         $tabSortOrder++;
         foreach ($fieldIds as $fieldSortOrder => $fieldId) {
             $field = new FieldLayoutFieldModel();
             $field->fieldId = $fieldId;
             $field->required = in_array($fieldId, $requiredFields);
             $field->sortOrder = $fieldSortOrder + 1;
             $fields[] = $field;
             $tabFields[] = $field;
         }
         $tab = new FieldLayoutTabModel();
         $tab->name = urldecode($tabName);
         $tab->sortOrder = $tabSortOrder;
         $tab->setFields($tabFields);
         $tabs[] = $tab;
     }
     $layout = new FieldLayoutModel();
     $layout->setTabs($tabs);
     $layout->setFields($fields);
     return $layout;
 }
Exemplo n.º 4
0
 /**
  * Sets the layout's tabs.
  *
  * @param array $tabs
  */
 public function setTabs($tabs)
 {
     $this->_tabs = FieldLayoutTabModel::populateModels($tabs);
 }
 /**
  * Attempt to import a field layout.
  *
  * @param array $fieldLayoutDef
  *
  * @return FieldLayoutModel
  */
 private function _importFieldLayout(array $fieldLayoutDef)
 {
     $layoutTabs = array();
     $layoutFields = array();
     if (array_key_exists('tabs', $fieldLayoutDef)) {
         $tabSortOrder = 0;
         foreach ($fieldLayoutDef['tabs'] as $tabName => $tabDef) {
             $layoutTabFields = array();
             foreach ($tabDef as $fieldHandle => $required) {
                 $fieldSortOrder = 0;
                 $field = craft()->fields->getFieldByHandle($fieldHandle);
                 if ($field) {
                     $layoutField = new FieldLayoutFieldModel();
                     $layoutField->setAttributes(array('fieldId' => $field->id, 'required' => $required, 'sortOrder' => ++$fieldSortOrder));
                     $layoutTabFields[] = $layoutField;
                     $layoutFields[] = $layoutField;
                 }
             }
             $layoutTab = new FieldLayoutTabModel();
             $layoutTab->setAttributes(array('name' => $tabName, 'sortOrder' => ++$tabSortOrder));
             $layoutTab->setFields($layoutTabFields);
             $layoutTabs[] = $layoutTab;
         }
     } else {
         if (array_key_exists('fields', $fieldLayoutDef)) {
             $fieldSortOrder = 0;
             foreach ($fieldLayoutDef['fields'] as $fieldHandle => $required) {
                 $field = craft()->fields->getFieldByHandle($fieldHandle);
                 if ($field) {
                     $layoutField = new FieldLayoutFieldModel();
                     $layoutField->setAttributes(array('fieldId' => $field->id, 'required' => $required, 'sortOrder' => ++$fieldSortOrder));
                     $layoutFields[] = $layoutField;
                 }
             }
         }
     }
     $fieldLayout = new FieldLayoutModel();
     $fieldLayout->type = ElementType::Entry;
     $fieldLayout->setTabs($layoutTabs);
     $fieldLayout->setFields($layoutFields);
     return $fieldLayout;
 }
 /**
  * Add fields to Set's layout.
  *
  * @param array $fields Array filled with FieldLayoutFieldModel.
  */
 private function _addFieldsToLayout($fields)
 {
     $oldLayout = false;
     $newLayout = $this->_globalSet->getFieldLayout();
     $layoutTabs = array();
     $layoutFields = array();
     // Delete old layout if available
     if ($this->_globalSet->fieldLayoutId) {
         $oldLayout = $this->_globalSet->getFieldLayout();
         // Overwrite variables
         $layoutTabs = $oldLayout->getTabs();
         $layoutFields = $oldLayout->getFields();
     }
     // Create layout
     if (!count($layoutTabs)) {
         // Nothing is set, so we have to create it
         $tab = new FieldLayoutTabModel();
         $tab->name = Craft::t('Content');
         $tab->sortOrder = 1;
         $tab->setFields($fields);
         $layoutTabs[] = $tab;
         // Add fields to layout
         foreach ($fields as $field) {
             $layoutFields[] = $field;
         }
     } else {
         // Update existing information
         foreach ($layoutTabs as $tab) {
             // Add fields to first tab
             if ($tab === reset($layoutTabs)) {
                 $tabFields = $tab->getFields();
                 $sortOrder = count($tabFields) + 1;
                 foreach ($fields as $field) {
                     // Update position for added field
                     $field->sortOrder = $sortOrder;
                     // Add field to tab and layout
                     $tabFields[] = $field;
                     $layoutFields[] = $field;
                     // Update order
                     $sortOrder++;
                 }
                 $tab->setFields($tabFields);
             }
         }
     }
     $newLayout->setTabs($layoutTabs);
     $newLayout->setFields($layoutFields);
     // Delete old layout
     if ($oldLayout !== false) {
         craft()->fields->deleteLayoutById($oldLayout->id);
     }
     // Set new layout
     $this->_globalSet->setFieldLayout($newLayout);
 }
 /**
  * Save a field.
  */
 public function actionSaveField()
 {
     $this->requirePostRequest();
     // Make sure our field has a section
     // @TODO - handle this much more gracefully
     $tabId = craft()->request->getPost('tabId');
     // Get the Form these fields are related to
     $formId = craft()->request->getRequiredPost('formId');
     $form = sproutForms()->forms->getFormById($formId);
     $field = new FieldModel();
     $field->id = craft()->request->getPost('fieldId');
     $field->name = craft()->request->getRequiredPost('name');
     $field->handle = craft()->request->getRequiredPost('handle');
     $field->instructions = craft()->request->getPost('instructions');
     $field->required = craft()->request->getPost('required');
     $field->translatable = (bool) craft()->request->getPost('translatable');
     $field->type = craft()->request->getRequiredPost('type');
     $typeSettings = craft()->request->getPost('types');
     if (isset($typeSettings[$field->type])) {
         $field->settings = $typeSettings[$field->type];
     }
     // Set our field context
     craft()->content->fieldContext = $form->getFieldContext();
     craft()->content->contentTable = $form->getContentTable();
     // Does our field validate?
     if (!craft()->fields->validateField($field)) {
         SproutFormsPlugin::log("Field does not validate.");
         // Send the field back to the template
         craft()->urlManager->setRouteVariables(array('field' => $field));
         // Route our request back to the field template
         $route = craft()->urlManager->parseUrl(craft()->request);
         craft()->runController($route);
         craft()->end();
     }
     // Save a new field
     if (!$field->id) {
         SproutFormsPlugin::log('New Field');
         $isNewField = true;
     } else {
         SproutFormsPlugin::log('Existing Field');
         $isNewField = false;
     }
     // Save our field
     craft()->fields->saveField($field);
     // Now let's add this field to our field layout
     // ------------------------------------------------------------
     // Set the field layout
     $oldFieldLayout = $form->getFieldLayout();
     $oldFields = $oldFieldLayout->getFields();
     $oldTabs = $oldFieldLayout->getTabs();
     $tabFields = array();
     $postedFieldLayout = array();
     $requiredFields = array();
     // If no tabs exist, let's create a
     // default one for all of our fields
     if (!$oldTabs) {
         // Create a tab
         $fieldLayoutTab = new FieldLayoutTabModel();
         $fieldLayoutTab->name = Craft::t('Form');
         $fieldLayoutTab->sortOrder = 1;
         if ($oldFields) {
             $fieldSortOrder = 0;
             // Add any existing fields to a default tab
             foreach ($oldFields as $oldFieldLayoutField) {
                 $fieldSortOrder++;
                 $newField = new FieldLayoutFieldModel();
                 $newField->fieldId = $oldFieldLayoutField->fieldId;
                 $newField->required = $oldFieldLayoutField->required;
                 $newField->sortOrder = $fieldSortOrder;
                 $tabFields[] = $newField;
                 $postedFieldLayout[$fieldLayoutTab->name][] = $oldFieldLayoutField->fieldId;
                 if ($oldFieldLayoutField->required) {
                     $requiredFields[] = $oldFieldLayoutField->fieldId;
                 }
             }
         }
         // Add our new field
         $postedFieldLayout[$fieldLayoutTab->name][] = $field->id;
         $fieldLayoutTab->setFields($tabFields);
     } else {
         foreach ($oldTabs as $oldTab) {
             $oldTabFields = $oldTab->getFields();
             foreach ($oldTabFields as $oldFieldLayoutField) {
                 $postedFieldLayout[$oldTab->name][] = $oldFieldLayoutField->fieldId;
                 if ($oldFieldLayoutField->required) {
                     $requiredFields[] = $oldFieldLayoutField->fieldId;
                 }
             }
             // Add our new field to the tab it belongs to
             if ($isNewField && $tabId == $oldTab->id) {
                 $postedFieldLayout[$oldTab->name][] = $field->id;
             }
         }
     }
     // Set the field layout
     $fieldLayout = craft()->fields->assembleLayout($postedFieldLayout, $requiredFields);
     $fieldLayout->type = 'SproutForms_Form';
     $form->setFieldLayout($fieldLayout);
     // Hand the field off to be saved in the
     // field layout of our Form Element
     if (sproutForms()->forms->saveForm($form)) {
         SproutFormsPlugin::log('Field Saved');
         craft()->userSession->setNotice(Craft::t('Field saved.'));
         $this->redirectToPostedUrl($field);
     } else {
         SproutFormsPlugin::log("Couldn't save field.");
         craft()->userSession->setError(Craft::t('Couldn’t save field.'));
         // Send the field back to the template
         craft()->urlManager->setRouteVariables(array('field' => $field));
     }
 }