/**
  * Saves a field layout for a given pimped block type
  */
 public function actionSaveFieldLayout()
 {
     $this->requirePostRequest();
     $this->requireAjaxRequest();
     $pimpedBlockTypeId = craft()->request->getPost('pimpedBlockTypeId');
     $blockTypeFieldLayouts = craft()->request->getPost('blockTypeFieldLayouts');
     if ($pimpedBlockTypeId) {
         $pimpedBlockTypeRecord = PimpMyMatrix_BlockTypeRecord::model()->findById($pimpedBlockTypeId);
         if (!$pimpedBlockTypeRecord) {
             throw new Exception(Craft::t('No PimpMyMatrix block type exists with the ID “{id}”', array('id' => $pimpedBlockTypeId)));
         }
         $pimpedBlockType = PimpMyMatrix_BlockTypeModel::populateModel($pimpedBlockTypeRecord);
     } else {
         return false;
     }
     // Set the field layout on the model
     $postedFieldLayout = craft()->request->getPost('blockTypeFieldLayouts', array());
     $assembledLayout = craft()->fields->assembleLayout($postedFieldLayout, array());
     $pimpedBlockType->setFieldLayout($assembledLayout);
     // Save it
     if (craft()->pimpMyMatrix_blockTypes->saveFieldLayout($pimpedBlockType)) {
         $this->returnJson(array('success' => true));
     } else {
         $this->returnJson(array('success' => false));
     }
 }
 /**
  * Returns an array of fieldLayoutIds indexed by matrixBlockTypeIds
  * for the given context and fieldId combination
  *
  * @param  string            $context required
  * @param  int               $fieldId required
  * @return false|array
  */
 public function getFieldLayoutIds($context, $fieldId = false)
 {
     if (!$fieldId) {
         return false;
     }
     $blockTypeRecords = PimpMyMatrix_BlockTypeRecord::model()->findAllByAttributes(array('context' => $context, 'fieldId' => $fieldId));
     $return = array();
     foreach ($blockTypeRecords as $blockTypeRecord) {
         $return[$blockTypeRecord->matrixBlockTypeId] = $blockTypeRecord->fieldLayoutId;
     }
     return $return;
 }