Exemplo n.º 1
0
 /**
  * Deletes a block type.
  *
  * @param MatrixBlockTypeModel $blockType The block type.
  *
  * @throws \Exception
  * @return bool Whether the block type was deleted successfully.
  */
 public function deleteBlockType(MatrixBlockTypeModel $blockType)
 {
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         // First delete the blocks of this type
         $blockIds = craft()->db->createCommand()->select('id')->from('matrixblocks')->where(array('typeId' => $blockType->id))->queryColumn();
         $this->deleteBlockById($blockIds);
         // Set the new contentTable
         $originalContentTable = craft()->content->contentTable;
         $matrixField = craft()->fields->getFieldById($blockType->fieldId);
         $newContentTable = $this->getContentTableName($matrixField);
         craft()->content->contentTable = $newContentTable;
         // Set the new fieldColumnPrefix
         $originalFieldColumnPrefix = craft()->content->fieldColumnPrefix;
         craft()->content->fieldColumnPrefix = 'field_' . $blockType->handle . '_';
         // Now delete the block type fields
         foreach ($blockType->getFields() as $field) {
             craft()->fields->deleteField($field);
         }
         // Restore the contentTable and the fieldColumnPrefix to original values.
         craft()->content->fieldColumnPrefix = $originalFieldColumnPrefix;
         craft()->content->contentTable = $newContentTable;
         // Delete the field layout
         craft()->fields->deleteLayoutById($blockType->fieldLayoutId);
         // Finally delete the actual block type
         $affectedRows = craft()->db->createCommand()->delete('matrixblocktypes', array('id' => $blockType->id));
         if ($transaction !== null) {
             $transaction->commit();
         }
         return (bool) $affectedRows;
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
 }
 /**
  * @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);
 }