-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Josh Crawford <[email protected]>
- Loading branch information
1 parent
d788627
commit 96992d6
Showing
6 changed files
with
239 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
namespace Craft; | ||
|
||
class FieldManager_ExportService extends BaseApplicationComponent | ||
{ | ||
public function export(array $fieldIds) | ||
{ | ||
$fieldDefs = array(); | ||
|
||
foreach ($fieldIds as $fieldId) { | ||
$field = craft()->fields->getFieldById($fieldId); | ||
|
||
if ($field) { | ||
$fieldDefs[$field->handle] = array( | ||
'name' => $field->name, | ||
'context' => $field->context, | ||
'instructions' => $field->instructions, | ||
'translatable' => $field->translatable, | ||
'type' => $field->type, | ||
'settings' => $field->settings | ||
); | ||
|
||
if ($field->type == 'Matrix') { | ||
$fieldDefs[$field->handle]['blockTypes'] = $this->handleMatrixExport($field); | ||
} | ||
|
||
if ($field->type == 'SuperTable') { | ||
$fieldDefs[$field->handle]['blockTypes'] = $this->handleSuperTableExport($field); | ||
} | ||
} | ||
} | ||
|
||
return $fieldDefs; | ||
} | ||
|
||
public function handleMatrixExport($field) { | ||
$blockTypeDefs = array(); | ||
$blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id); | ||
|
||
foreach ($blockTypes as $blockType) { | ||
$blockTypeFieldDefs = array(); | ||
|
||
foreach ($blockType->getFields() as $blockTypeField) { | ||
$blockTypeFieldDefs[$blockTypeField->handle] = array( | ||
'name' => $blockTypeField->name, | ||
'required' => $blockTypeField->required, | ||
'translatable' => $blockTypeField->translatable, | ||
'type' => $blockTypeField->type, | ||
'settings' => $blockTypeField->settings | ||
); | ||
|
||
if ($blockTypeField->type == 'SuperTable') { | ||
$blockTypeFieldDefs[$blockTypeField->handle]['blockTypes'] = $this->handleSuperTableExport($blockTypeField); | ||
} | ||
} | ||
|
||
$blockTypeDefs[$blockType->handle] = array( | ||
'name' => $blockType->name, | ||
'fields' => $blockTypeFieldDefs | ||
); | ||
} | ||
|
||
return $blockTypeDefs; | ||
} | ||
|
||
public function handleSuperTableExport($field) { | ||
$blockTypeDefs = array(); | ||
$blockTypes = craft()->superTable->getBlockTypesByFieldId($field->id); | ||
|
||
foreach ($blockTypes as $blockType) { | ||
$blockTypeFieldDefs = array(); | ||
|
||
foreach ($blockType->getFields() as $blockTypeField) { | ||
$blockTypeFieldDefs[$blockTypeField->handle] = array( | ||
'name' => $blockTypeField->name, | ||
'required' => $blockTypeField->required, | ||
'translatable' => $blockTypeField->translatable, | ||
'type' => $blockTypeField->type, | ||
'settings' => $blockTypeField->settings | ||
); | ||
|
||
if ($blockTypeField->type == 'Matrix') { | ||
$blockTypeFieldDefs[$blockTypeField->handle]['blockTypes'] = $this->handleMatrixExport($blockTypeField); | ||
} | ||
} | ||
|
||
$blockTypeDefs = array( | ||
'fields' => $blockTypeFieldDefs | ||
); | ||
} | ||
|
||
return $blockTypeDefs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
namespace Craft; | ||
|
||
class FieldManager_ImportService extends BaseApplicationComponent | ||
{ | ||
public function import($fieldDefs) { | ||
|
||
$fields = craft()->fields->getAllFields('handle'); | ||
$fieldTypes = craft()->fields->getAllFieldTypes(); | ||
|
||
foreach ($fieldDefs as $fieldHandle => $fieldDef) { | ||
if (array_key_exists($fieldDef['type'], $fieldTypes)) { | ||
$field = new FieldModel(); | ||
|
||
$field->setAttributes(array( | ||
'handle' => $fieldHandle, | ||
'groupId' => $fieldDef['groupId'], | ||
'name' => $fieldDef['name'], | ||
'context' => $fieldDef['context'], | ||
'instructions' => $fieldDef['instructions'], | ||
'translatable' => $fieldDef['translatable'], | ||
'type' => $fieldDef['type'], | ||
'settings' => $fieldDef['settings'] | ||
)); | ||
|
||
if (!craft()->fields->saveField($field)) { | ||
return $field->getAllErrors(); | ||
} | ||
|
||
if ($field->type == 'Matrix') { | ||
$this->handleMatrixImport($fieldDef, $field); | ||
} | ||
|
||
if ($field->type == 'SuperTable') { | ||
$this->handleSuperTableImport($fieldDef, $field); | ||
} | ||
|
||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function handleMatrixImport($fieldDef, $field) { | ||
$blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id, 'handle'); | ||
|
||
if (!array_key_exists('blockTypes', $fieldDef)) { | ||
return $result->error('`fields[handle].blockTypes` must exist'); | ||
} | ||
|
||
foreach ($fieldDef['blockTypes'] as $blockTypeHandle => $blockTypeDef) { | ||
$blockType = array_key_exists($blockTypeHandle, $blockTypes) ? $blockTypes[$blockTypeHandle] : new MatrixBlockTypeModel(); | ||
$blockType->fieldId = $field->id; | ||
$blockType->name = $blockTypeDef['name']; | ||
$blockType->handle = $blockTypeHandle; | ||
|
||
if (!array_key_exists('fields', $blockTypeDef)) { | ||
return $result->error('`fields[handle].blockTypes[handle].fields` must exist'); | ||
} | ||
|
||
$blockTypeFields = array(); | ||
|
||
foreach ($blockType->getFields() as $blockTypeField) { | ||
$blockTypeFields[$blockTypeField->handle] = $blockTypeField; | ||
} | ||
|
||
$newBlockTypeFields = array(); | ||
$extraFields = array(); | ||
foreach ($blockTypeDef['fields'] as $blockTypeFieldHandle => $blockTypeFieldDef) { | ||
$blockTypeField = array_key_exists($blockTypeFieldHandle, $blockTypeFields) ? $blockTypeFields[$blockTypeFieldHandle] : new FieldModel(); | ||
$blockTypeField->name = $blockTypeFieldDef['name']; | ||
$blockTypeField->handle = $blockTypeFieldHandle; | ||
$blockTypeField->required = $blockTypeFieldDef['required']; | ||
$blockTypeField->translatable = $blockTypeFieldDef['translatable']; | ||
$blockTypeField->type = $blockTypeFieldDef['type']; | ||
$blockTypeField->settings = $blockTypeFieldDef['settings']; | ||
|
||
$newBlockTypeFields[] = $blockTypeField; | ||
|
||
if ($blockTypeField->type == 'SuperTable') { | ||
$extraFields[] = array($blockTypeFieldDef, $blockTypeField); | ||
} | ||
} | ||
|
||
$blockType->setFields($newBlockTypeFields); | ||
|
||
if (!craft()->matrix->saveBlockType($blockType)) { | ||
return $blockType->getAllErrors(); | ||
} | ||
|
||
foreach ($extraFields as $options) { | ||
$this->handleSuperTableImport($options[0], $options[1]); | ||
} | ||
} | ||
} | ||
|
||
public function handleSuperTableImport($fieldDef, $field) { | ||
$blockTypes = craft()->superTable->getBlockTypesByFieldId($field->id, 'id'); | ||
|
||
foreach ($fieldDef['blockTypes'] as $blockTypeFields) { | ||
$blockType = new SuperTable_BlockTypeModel(); | ||
$blockType->fieldId = $field->id; | ||
|
||
$newBlockTypeFields = array(); | ||
$extraFields = array(); | ||
foreach ($blockTypeFields as $blockTypeFieldHandle => $blockTypeFieldDef) { | ||
$blockTypeField = new FieldModel(); | ||
$blockTypeField->name = $blockTypeFieldDef['name']; | ||
$blockTypeField->handle = $blockTypeFieldHandle; | ||
$blockTypeField->required = $blockTypeFieldDef['required']; | ||
$blockTypeField->translatable = $blockTypeFieldDef['translatable']; | ||
$blockTypeField->type = $blockTypeFieldDef['type']; | ||
$blockTypeField->settings = $blockTypeFieldDef['settings']; | ||
|
||
$newBlockTypeFields[] = $blockTypeField; | ||
|
||
if ($blockTypeField->type == 'Matrix') { | ||
$extraFields[] = array($blockTypeFieldDef, $blockTypeField); | ||
} | ||
} | ||
|
||
$blockType->setFields($newBlockTypeFields); | ||
|
||
if (!craft()->superTable->saveBlockType($blockType)) { | ||
return $blockType->getAllErrors(); | ||
} | ||
|
||
foreach ($extraFields as $options) { | ||
$this->handleMatrixImport($options[0], $options[1]); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.