public function processMatrix($originField, $field)
 {
     $settings = new MatrixSettingsModel($field);
     // Get the original Matrix Blocks
     $blockTypes = craft()->matrix->getBlockTypesByFieldId($originField->id);
     $newBlockTypes = array();
     foreach ($blockTypes as $originBlockType) {
         $newBlockType = new MatrixBlockTypeModel();
         // New Matrix Block
         $newBlockType->fieldId = $field->id;
         $newBlockType->name = $originBlockType->name;
         $newBlockType->handle = $originBlockType->handle;
         $newBlockFields = array();
         foreach ($originBlockType->fields as $originField) {
             $newBlockField = new FieldModel();
             // New Matrix Block field
             $newBlockField->name = $originField->name;
             $newBlockField->handle = $originField->handle;
             $newBlockField->required = $originField->required;
             $newBlockField->translatable = $originField->translatable;
             $newBlockField->type = $originField->type;
             $newBlockField->settings = $originField->settings;
             $newBlockFields[] = $newBlockField;
         }
         $newBlockType->setFields($newBlockFields);
         craft()->matrix->saveBlockType($newBlockType);
         $newBlockTypes[] = $newBlockType;
     }
     $settings->setBlockTypes($newBlockTypes);
     craft()->matrix->saveSettings($settings);
 }
 /**
  * @inheritDoc ISavableComponentType::prepSettings()
  *
  * @param array $settings
  *
  * @return array
  */
 public function prepSettings($settings)
 {
     if ($settings instanceof MatrixSettingsModel) {
         return $settings;
     }
     $matrixSettings = new MatrixSettingsModel($this->model);
     $blockTypes = array();
     if (!empty($settings['blockTypes'])) {
         foreach ($settings['blockTypes'] as $blockTypeId => $blockTypeSettings) {
             $blockType = new MatrixBlockTypeModel();
             $blockType->id = $blockTypeId;
             $blockType->fieldId = $this->model->id;
             $blockType->name = $blockTypeSettings['name'];
             $blockType->handle = $blockTypeSettings['handle'];
             $fields = array();
             if (!empty($blockTypeSettings['fields'])) {
                 foreach ($blockTypeSettings['fields'] as $fieldId => $fieldSettings) {
                     $field = new FieldModel();
                     $field->id = $fieldId;
                     $field->name = $fieldSettings['name'];
                     $field->handle = $fieldSettings['handle'];
                     $field->instructions = $fieldSettings['instructions'];
                     $field->required = !empty($fieldSettings['required']);
                     $field->translatable = !empty($fieldSettings['translatable']);
                     $field->type = $fieldSettings['type'];
                     if (isset($fieldSettings['typesettings'])) {
                         $field->settings = $fieldSettings['typesettings'];
                     }
                     $fields[] = $field;
                 }
             }
             $blockType->setFields($fields);
             $blockTypes[] = $blockType;
         }
     }
     $matrixSettings->setBlockTypes($blockTypes);
     if (!empty($settings['maxBlocks'])) {
         $matrixSettings->maxBlocks = $settings['maxBlocks'];
     }
     return $matrixSettings;
 }
Esempio n. 3
0
 /**
  * Returns a block type record by its ID or creates a new one.
  *
  * @param MatrixBlockTypeModel $blockType
  *
  * @throws Exception
  * @return MatrixBlockTypeRecord
  */
 private function _getBlockTypeRecord(MatrixBlockTypeModel $blockType)
 {
     if (!$blockType->isNew()) {
         $blockTypeId = $blockType->id;
         if (!isset($this->_blockTypeRecordsById) || !array_key_exists($blockTypeId, $this->_blockTypeRecordsById)) {
             $this->_blockTypeRecordsById[$blockTypeId] = MatrixBlockTypeRecord::model()->findById($blockTypeId);
             if (!$this->_blockTypeRecordsById[$blockTypeId]) {
                 throw new Exception(Craft::t('No block type exists with the ID “{id}”.', array('id' => $blockTypeId)));
             }
         }
         return $this->_blockTypeRecordsById[$blockTypeId];
     } else {
         return new MatrixBlockTypeRecord();
     }
 }
 /**
  * @param FieldModel $field
  * @param MatrixBlockTypeModel $blockType
  * @param array $blockTypeDef
  * @param string $blockTypeHandle
  */
 private function populateBlockType(FieldModel $field, MatrixBlockTypeModel $blockType, array $blockTypeDef, $blockTypeHandle)
 {
     $blockType->fieldId = $field->id;
     $blockType->name = $blockTypeDef['name'];
     $blockType->handle = $blockTypeHandle;
     $blockTypeFields = array();
     foreach ($blockType->getFields() as $blockTypeField) {
         $blockTypeFields[$blockTypeField->handle] = $blockTypeField;
     }
     $newBlockTypeFields = array();
     foreach ($blockTypeDef['fields'] as $blockTypeFieldHandle => $blockTypeFieldDef) {
         $blockTypeField = array_key_exists($blockTypeFieldHandle, $blockTypeFields) ? $blockTypeFields[$blockTypeFieldHandle] : new FieldModel();
         $this->populateField($blockTypeFieldDef, $blockTypeField, $blockTypeFieldHandle);
         $newBlockTypeFields[] = $blockTypeField;
     }
     $blockType->setFields($newBlockTypeFields);
 }