Skip to content

Commit

Permalink
version 1.3.4
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Crawford <[email protected]>
  • Loading branch information
engram-design committed Jul 8, 2015
1 parent d788627 commit 96992d6
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 185 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ One thing to note for field group cloning, is that fields are required to have u

You may also set this yourself if you choose to, using the `Prefix` field when cloning a field group. Please note that it needs to be a valid handle (no spaces, no hyphens, underscores only).

### Supported FieldTypes
## Supported FieldTypes

**Craft**

Expand Down Expand Up @@ -69,7 +69,12 @@ You may also set this yourself if you choose to, using the `Prefix` field when c
Found a bug? Have a suggestion? [Submit an issue](https://github.com/engram-design/FieldManager/issues)


### Changelog
## Changelog

#### 1.3.4

- Refactor import/export services.
- Fully support import/export Matrix-in-SuperTable and SuperTable-in-Matrix.

#### 1.3.3

Expand Down
2 changes: 1 addition & 1 deletion fieldmanager/FieldManagerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getName()

public function getVersion()
{
return '1.3.3';
return '1.3.4';
}

public function getDeveloper()
Expand Down
4 changes: 2 additions & 2 deletions fieldmanager/controllers/FieldManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function actionExport()
$this->requirePostRequest();

$fields = craft()->request->getParam('selectedFields');
$fieldsObj = craft()->fieldManager_port->export($fields);
$fieldsObj = craft()->fieldManager_export->export($fields);

// Support PHP <5.4, JSON_PRETTY_PRINT = 128, JSON_NUMERIC_CHECK = 32
$json = json_encode($fieldsObj, 128 | 32);
Expand Down Expand Up @@ -171,7 +171,7 @@ public function actionImport()
}

if (count($fieldsToImport) > 0) {
$fieldImportResult = craft()->fieldManager_port->import($fieldsToImport);
$fieldImportResult = craft()->fieldManager_import->import($fieldsToImport);

if ($fieldImportResult === true) {
craft()->userSession->setNotice('Imported successfully.');
Expand Down
94 changes: 94 additions & 0 deletions fieldmanager/services/FieldManager_ExportService.php
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;
}
}
135 changes: 135 additions & 0 deletions fieldmanager/services/FieldManager_ImportService.php
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]);
}
}
}


}
Loading

0 comments on commit 96992d6

Please sign in to comment.