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