<NME> TheArchitectController.php
<BEF> <?php

namespace Craft;

/**
 * TheArchitectController.
 */
class TheArchitectController extends BaseController
{
    // Public Methods
    // =========================================================================

    /**
     * {@inheritdoc} BaseController::init()
     *
     * @throws HttpException
     */
    public function init()
    {
        // All section actions require an admin
        craft()->userSession->requireAdmin();
    }

    /**
     * actionBlueprint [list the exportable items].
     */
    public function actionBlueprint()
    {
        $variables = array(
            'assetSources' => craft()->assetSources->getAllSources(),
            'assetTransforms' => craft()->assetTransforms->getAllTransforms(),
        );

        craft()->templates->includeJsResource('thearchitect/js/diff.min.js');
        craft()->templates->includeJsResource('thearchitect/js/thearchitect.js');
        craft()->templates->includeCssResource('thearchitect/css/thearchitect.css');

        $this->renderTemplate('thearchitect/blueprint', $variables);
    }

    /**
     * actionConstructBlueprint.
     */
    public function actionConstructBlueprint()
    {
        // Prevent GET Requests
        $this->requirePostRequest();

        $post = craft()->request->getPost();

        $output = craft()->theArchitect->exportConstruct($post);

        $groups = [];
        $fields = [];
        foreach ($post['fieldSelection'] as $id) {
            $field = craft()->fields->getFieldById($id);

            if (!in_array($field->group->name, $groups)) {
                array_push($groups, $field->group->name);
            }

            $tmpField = [
                "group" => $field->group->name,
                "name" => $field->name,
                "handle" => $field->handle,
                "instructions" => $field->instructions,
                "required" => $field->required,
                "type" => $field->type,
                "typesettings" => $field->settings
            ];

            if ($field->type == 'Neo') {
                $blockTypes = craft()->neo->getBlockTypesByFieldId($id);
                $blockCount = 0;
                foreach ($blockTypes as $blockType) {
                    $tmpField["typesettings"]["blockTypes"]["new" . $blockCount] = [
                        "sortOrder" => $blockType->sortOrder,
                        "name" => $blockType->name,
                        "handle" => $blockType->handle,
                        "maxBlocks" => $blockType->maxBlocks,
                        "childBlocks" => $blockType->childBlocks,
                        "topLevel" => $blockType->topLevel,
                        "fieldLayout" => []
                    ];
                    foreach ($blockType->getFieldLayout()->getTabs() as $tab) {
                        $tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fieldLayout"][$tab->name] = [];
                        foreach ($tab->getFields() as $tabField) {
                            array_push($tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fieldLayout"][$tab->name], craft()->fields->getFieldById($tabField->fieldId)->handle);
                        }
                    }
                    $blockCount++;
                }
            }

            if ($field->type == 'Matrix') {
                $blockTypes = craft()->matrix->getBlockTypesByFieldId($id);
                $blockCount = 1;
                foreach ($blockTypes as $blockType) {
                    $tmpField["typesettings"]["blockTypes"]["new" . $blockCount] = [
                        "name" => $blockType->name,
                        "handle" => $blockType->handle,
                        "fields" => []
                    ];
                    $fieldCount = 1;
                    foreach ($blockType->fields as $blockField) {
                        $tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fields"]["new" . $fieldCount] = [
                            "name" => $blockField->name,
                            "handle" => $blockField->handle,
                            "instructions" => $blockField->instructions,
                            "required" => $blockField->required,
                            "type" => $blockField->type,
                            "typesettings" => $blockField->settings
                        ];
                        $fieldCount++;
                    }
                    $blockCount++;
                }
            }

            array_push($fields, $tmpField);
        }

        $output = [
            'groups' => $groups,
            'fields' => $fields
        ];

        $variables = array(
            'json' => json_encode($output, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT)
        );

        $this->renderTemplate('thearchitect/output', $variables);
    }



        // Set last import to match this export time.
        craft()->plugins->savePluginSettings(craft()->plugins->getPlugin('theArchitect'), array('lastImport' => (new DateTime())->getTimestamp()));

        $this->redirect('thearchitect/migrations');
    }

    public function actionMigrationImport()
    {
        $force = (!is_null(craft()->request->getPost('force')));

        $jsonPath = craft()->config->get('modelsPath', 'theArchitect');
        $masterJson = $jsonPath.'_master_.json';

        if (!file_exists($masterJson)) {
            craft()->userSession->setError(Craft::t('There is no migration file to import.'));
        } else {
            $result = craft()->theArchitect->importMigrationConstruct($force);

            if ($result) {
                touch($masterJson);
                craft()->userSession->setNotice(Craft::t('Migration imported successfully.'));
            } else {
                craft()->userSession->setError(Craft::t('There is some field type changes. To prevent content loss please review the field types before forcing.'));
            }
        }

        $this->redirect('thearchitect/migrations');
    }

    public function actionGenerateKey()
    {
        // Generate a new API key for import / export url calls
        craft()->plugins->savePluginSettings(craft()->plugins->getPlugin('theArchitect'), array('apiKey' => craft()->theArchitect->generateUUID4()));

        $this->redirect('thearchitect/migrations');
    }

    /**
     * actionConstructList [list the files inside the `config.jsonPath` folder].
     */
    public function actionConstructList()
    {
        $files = array();
        $jsonPath = craft()->path->getConfigPath().'thearchitect/';

        if (file_exists($jsonPath) && is_dir($jsonPath) && $handle = opendir($jsonPath)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != '.' && $entry != '..' && $entry != '_master_.json' && strtolower(pathinfo($entry, PATHINFO_EXTENSION)) == 'json') {
                    $files[] = $entry;
                }
            }
            closedir($handle);
        }

        natsort($files);

        $groups = craft()->fields->getAllGroups();
        $fields = craft()->fields->getAllFields();
        $sections = craft()->sections->getAllSections();

        foreach ($fields as $field) {
            if ($field->type == 'Matrix') {
                $blockTypes = craft()->matrix->getBlockTypesByFieldId($field->id);
                foreach ($blockTypes as $blockType) {
                    $blockType->getFields();
                }
            }
        }

        $variables = array(
            'files' => $files,
        );

        $this->renderTemplate('thearchitect/files', $variables);
    }

    /**
     * actionConstructLoad [load the selected file for review before processing].
     */
    public function actionConstructLoad()
    {
        // Prevent GET Requests
        $this->requirePostRequest();

        $fileName = craft()->request->getRequiredPost('fileName');
        $jsonPath = craft()->path->getConfigPath().'thearchitect/';

        $filePath = $jsonPath.$fileName;

        if (file_exists($filePath)) {
            $json = file_get_contents($filePath);

            $variables = array(
                'json' => $json,
                'filename' => $fileName,
            );

            $this->renderTemplate('thearchitect/index', $variables);
        }
    }

    /**
     * actionConstruct.
     */
    public function actionConstruct()
    {
        // Prevent GET Requests
        $this->requirePostRequest();

        $json = craft()->request->getRequiredPost('json');

        if ($json) {
            $notice = craft()->theArchitect->parseJson($json);

            $variables = array(
                'json' => $json,
                'result' => $notice,
            );

            $this->renderTemplate('thearchitect/index', $variables);
        }
    }
}

<MSG> Fix error if nothing is selected in a given section.

If output is empty redirect to the export selection page.

<DFF> @@ -52,84 +52,104 @@ class TheArchitectController extends BaseController {
 
         $groups = [];
         $fields = [];
-        foreach ($post['fieldSelection'] as $id) {
-            $field = craft()->fields->getFieldById($id);
-
-            if (!in_array($field->group->name, $groups)) {
-                array_push($groups, $field->group->name);
-            }
-
-            $tmpField = [
-                "group" => $field->group->name,
-                "name" => $field->name,
-                "handle" => $field->handle,
-                "instructions" => $field->instructions,
-                "required" => $field->required,
-                "type" => $field->type,
-                "typesettings" => $field->settings
-            ];
-
-            if ($field->type == 'Neo') {
-                $blockTypes = craft()->neo->getBlockTypesByFieldId($id);
-                $blockCount = 0;
-                foreach ($blockTypes as $blockType) {
-                    $tmpField["typesettings"]["blockTypes"]["new" . $blockCount] = [
-                        "sortOrder" => $blockType->sortOrder,
-                        "name" => $blockType->name,
-                        "handle" => $blockType->handle,
-                        "maxBlocks" => $blockType->maxBlocks,
-                        "childBlocks" => $blockType->childBlocks,
-                        "topLevel" => $blockType->topLevel,
-                        "fieldLayout" => []
-                    ];
-                    foreach ($blockType->getFieldLayout()->getTabs() as $tab) {
-                        $tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fieldLayout"][$tab->name] = [];
-                        foreach ($tab->getFields() as $tabField) {
-                            array_push($tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fieldLayout"][$tab->name], craft()->fields->getFieldById($tabField->fieldId)->handle);
+        $sections = [];
+        $entryTypes = [];
+        $transforms = [];
+        $globals = [];
+        if (isset($post['fieldSelection'])) {
+            foreach ($post['fieldSelection'] as $id) {
+                $field = craft()->fields->getFieldById($id);
+
+                if (!in_array($field->group->name, $groups)) {
+                    array_push($groups, $field->group->name);
+                }
+
+                $tmpField = [
+                    "group" => $field->group->name,
+                    "name" => $field->name,
+                    "handle" => $field->handle,
+                    "instructions" => $field->instructions,
+                    "required" => $field->required,
+                    "type" => $field->type,
+                    "typesettings" => $field->settings
+                ];
+
+                if ($field->type == 'Neo') {
+                    $blockTypes = craft()->neo->getBlockTypesByFieldId($id);
+                    $blockCount = 0;
+                    foreach ($blockTypes as $blockType) {
+                        $tmpField["typesettings"]["blockTypes"]["new" . $blockCount] = [
+                            "sortOrder" => $blockType->sortOrder,
+                            "name" => $blockType->name,
+                            "handle" => $blockType->handle,
+                            "maxBlocks" => $blockType->maxBlocks,
+                            "childBlocks" => $blockType->childBlocks,
+                            "topLevel" => $blockType->topLevel,
+                            "fieldLayout" => []
+                        ];
+                        foreach ($blockType->getFieldLayout()->getTabs() as $tab) {
+                            $tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fieldLayout"][$tab->name] = [];
+                            foreach ($tab->getFields() as $tabField) {
+                                array_push($tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fieldLayout"][$tab->name], craft()->fields->getFieldById($tabField->fieldId)->handle);
+                            }
                         }
+                        $blockCount++;
                     }
-                    $blockCount++;
                 }
-            }
 
-            if ($field->type == 'Matrix') {
-                $blockTypes = craft()->matrix->getBlockTypesByFieldId($id);
-                $blockCount = 1;
-                foreach ($blockTypes as $blockType) {
-                    $tmpField["typesettings"]["blockTypes"]["new" . $blockCount] = [
-                        "name" => $blockType->name,
-                        "handle" => $blockType->handle,
-                        "fields" => []
-                    ];
-                    $fieldCount = 1;
-                    foreach ($blockType->fields as $blockField) {
-                        $tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fields"]["new" . $fieldCount] = [
-                            "name" => $blockField->name,
-                            "handle" => $blockField->handle,
-                            "instructions" => $blockField->instructions,
-                            "required" => $blockField->required,
-                            "type" => $blockField->type,
-                            "typesettings" => $blockField->settings
+                if ($field->type == 'Matrix') {
+                    $blockTypes = craft()->matrix->getBlockTypesByFieldId($id);
+                    $blockCount = 1;
+                    foreach ($blockTypes as $blockType) {
+                        $tmpField["typesettings"]["blockTypes"]["new" . $blockCount] = [
+                            "name" => $blockType->name,
+                            "handle" => $blockType->handle,
+                            "fields" => []
                         ];
-                        $fieldCount++;
+                        $fieldCount = 1;
+                        foreach ($blockType->fields as $blockField) {
+                            $tmpField["typesettings"]["blockTypes"]["new" . $blockCount]["fields"]["new" . $fieldCount] = [
+                                "name" => $blockField->name,
+                                "handle" => $blockField->handle,
+                                "instructions" => $blockField->instructions,
+                                "required" => $blockField->required,
+                                "type" => $blockField->type,
+                                "typesettings" => $blockField->settings
+                            ];
+                            $fieldCount++;
+                        }
+                        $blockCount++;
                     }
-                    $blockCount++;
                 }
-            }
 
-            array_push($fields, $tmpField);
+                array_push($fields, $tmpField);
+            }
         }
 
         $output = [
             'groups' => $groups,
-            'fields' => $fields
+            'sections' => $sections,
+            'fields' => $fields,
+            'entryTypes' => $entryTypes,
+            'tranforms' => $tranforms,
+            'globals' => $globals
         ];
 
-        $variables = array(
-            'json' => json_encode($output, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT)
-        );
+        foreach ($output as $key => $value) {
+            if ($value == []) {
+                unset($output[$key]);
+            }
+        }
 
-        $this->renderTemplate('thearchitect/output', $variables);
+        if ($output == []) {
+            $this->redirect('thearchitect/blueprint');
+        } else {
+            $variables = array(
+                'json' => json_encode($output, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT)
+            );
+
+            $this->renderTemplate('thearchitect/output', $variables);
+        }
     }
 
 

Fix error if nothing is selected in a given section.