From cdee60fbbc1f4e5c2c97a6652399dd04b3188d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eug=C3=A8ne=20Fritz?= Date: Sat, 6 Jul 2024 21:36:08 +0200 Subject: [PATCH 1/5] added new console commands to import or export formie form to a target folder --- docs/get-started/configuration.md | 3 + src/console/controllers/FormsController.php | 220 +++++++++++++++++++- src/controllers/ImportExportController.php | 44 +--- src/helpers/ImportExportHelper.php | 55 ++++- src/jobs/ImportForm.php | 48 +++++ src/models/Settings.php | 12 ++ 6 files changed, 338 insertions(+), 44 deletions(-) create mode 100644 src/jobs/ImportForm.php diff --git a/docs/get-started/configuration.md b/docs/get-started/configuration.md index 08fa49c7a..0ab14b6cd 100644 --- a/docs/get-started/configuration.md +++ b/docs/get-started/configuration.md @@ -114,6 +114,9 @@ return [ ### Theme - `themeConfig` - Sets the configuration for theming your form and fields. See below for an example. +### Export +- `defaultExportFolder` - Sets the default folder for exported forms via console comandes. + ## Control Panel You can also manage configuration settings through the Control Panel by visiting Settings → Formie. diff --git a/src/console/controllers/FormsController.php b/src/console/controllers/FormsController.php index ed84032ca..e8ddc4f53 100644 --- a/src/console/controllers/FormsController.php +++ b/src/console/controllers/FormsController.php @@ -1,12 +1,19 @@ getExportPath(); + try { + $files = FileHelper::findFiles($path, ['only' => ['*.json']]); + } catch (\Throwable $th) { + $this->stderr("Export directory is empty or don't exist" . PHP_EOL, Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; + } + + if (!empty($files)) { + $listEntries[] = [ + 'title' => 'Json to import:', + 'entriesList' => array_map(function ($file) { + return [ + 'name' => $file, + 'title' => '' + ]; + }, $files) + ]; + } + + $allForms = Formie::$plugin->getForms()->getAllForms(); + if (!empty($allForms)) { + $listEntries[] = [ + 'title' => 'formie:', + 'entriesList' => array_map(function ($form) { + return [ + 'name' => $form->handle, + 'title' => $form->title + ]; + }, $allForms) + ]; + } + + foreach ($listEntries as $entries) { + $this->stderr($entries['title'] . PHP_EOL, Console::FG_YELLOW); + + $handleMaxLen = max(array_map('strlen', array_column($entries['entriesList'], 'name'))); + + foreach ($entries['entriesList'] as $entry) { + $this->stderr("- " . $entry['name'], Console::FG_GREEN); + $this->stderr(Console::moveCursorTo($handleMaxLen + 5)); + $this->stderr($entry['title'] . PHP_EOL); + } + } + + + return ExitCode::OK; + } + + /** + * Export Formie forms as json, comma separated list of ids and/or handles + */ + public function actionExport($idsOrHandles = null): int + { + $formIds = null; + + foreach (explode(',', $idsOrHandles) as $idOrHandle) { + if (is_numeric($idOrHandle)) { + $formIds[] = $idOrHandle; + } else { + $formIds[] = Form::find()->handle($idOrHandle)->one()->id ?? null; + } + } + + if (!$formIds) { + $this->stderr('Unable to find any matching forms.' . PHP_EOL, Console::FG_RED); + + return ExitCode::UNSPECIFIED_ERROR; + } + + $query = Form::find()->id($formIds); + $count = (int)$query->count(); + + if ($count === 0) { + $this->stdout('No forms exist for that criteria.' . PHP_EOL, Console::FG_YELLOW); + } + + $elementsText = $count === 1 ? 'form' : 'forms'; + $this->stdout("Exporting {$count} {$elementsText} ..." . PHP_EOL, Console::FG_YELLOW); + + foreach (Db::each($query) as $element) { + try { + $formExport = ImportExportHelper::generateFormExport($element); + $json = Json::encode($formExport, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); + $exportPath = $this->generateExportPathByHandle($element->handle); + FileHelper::writeToFile($exportPath, $json); + $this->stdout("Exporting form #{$element->id} to {$exportPath}" . PHP_EOL, Console::FG_GREEN); + } catch (Throwable $e) { + + $this->stderr("Unable to export form #{$element->id}" . PHP_EOL); + return ExitCode::UNSPECIFIED_ERROR; + } + } + + return ExitCode::OK; + } + + /** + * Import a Formie form json from a path. + */ + public function actionImport($fileLocation = null): int + { + if ($fileLocation === null) { + $this->stderr('You must provide a path to a Json file' . PHP_EOL, Console::FG_RED); + } + + if (!is_file($fileLocation)) { + $this->stderr("No file exists for given path" . PHP_EOL, Console::FG_RED); + } + + if (strtolower(pathinfo($fileLocation, PATHINFO_EXTENSION)) !== 'json') { + $this->stderr("file is not of type Json" . PHP_EOL, Console::FG_RED); + } + + $json = Json::decode(file_get_contents($fileLocation)); + + // default, update existing form + $formAction = $this->create ? 'create' : 'update'; + + $form = ImportExportHelper::importFormFromJson($json, $formAction); + + // check for errors + if ($form->getConsolidatedErrors()) { + $this->stderr("Unable to import form." . PHP_EOL, Console::FG_RED); + $errors = Json::encode($form->getConsolidatedErrors()); + $this->stderr("Errors: {$errors}" . PHP_EOL, Console::FG_RED); + + return ExitCode::UNSPECIFIED_ERROR; + } + + $this->stdout("Form {$form->handle} has be {$formAction}." . PHP_EOL, Console::FG_GREEN); + + return ExitCode::OK; + } + + /** + * Import all Formie json from a folder + */ + public function actionImportAll($folderPath = null): int + { + $path = $folderPath ?? $this->getExportPath(); + try { + $files = FileHelper::findFiles($path, ['only' => ['*.json']]); + } catch (\Throwable $th) { + $this->stderr("Export directory is empty or don't exist" . PHP_EOL, Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; + } + + if (empty($files)) { + $this->stderr("No json fund in folder $path" . PHP_EOL, Console::FG_YELLOW); + return ExitCode::UNSPECIFIED_ERROR; + } + + // use jobs to prevent db overload or php timeout + foreach ($files as $file) { + + Queue::push(new ImportForm( + [ + 'fileLocation' => $file, + 'formAction' => $this->create ? 'create' : 'update' + ] + )); + + $basename = basename($file); + $this->stderr("File '{$basename}' has been added to the import queue." . PHP_EOL, Console::FG_GREEN); + } + + + return ExitCode::OK; + } + + // Protected Methods + // ========================================================================= + + private function generateExportPathByHandle($handle): string + { + return $this->getExportPath() . DIRECTORY_SEPARATOR . "formie-{$handle}.json"; + } + + private function getExportPath(): string + { + $settings = Formie::$plugin->getSettings(); + return $settings->getAbsoluteDefaultExportFolder(); + } } diff --git a/src/controllers/ImportExportController.php b/src/controllers/ImportExportController.php index abb0584ab..d2995d945 100644 --- a/src/controllers/ImportExportController.php +++ b/src/controllers/ImportExportController.php @@ -149,46 +149,12 @@ public function actionImportComplete(): ?Response } $json = Json::decode(file_get_contents($fileLocation)); + + + $form = ImportExportHelper::importFormFromJson($json, $formAction); - // Find an existing form with the same handle - $existingForm = null; - $formHandle = $json['handle'] ?? null; - - if ($formHandle) { - $existingForm = Formie::$plugin->getForms()->getFormByHandle($formHandle); - } - - // When creating a new form, change the handle - if ($formAction === 'create') { - $formHandles = (new Query()) - ->select(['handle']) - ->from(Table::FORMIE_FORMS) - ->column(); - - $json['handle'] = HandleHelper::getUniqueHandle($formHandles, $json['handle']); - } - - if ($formAction === 'update') { - // Update the form (force) - $form = ImportExportHelper::createFormFromImport($json, $existingForm); - } else { - // Create the form element, ready to go - $form = ImportExportHelper::createFormFromImport($json); - } - - // Because we also export the UID for forms, we need to check if we're importing a new form, but we've - // found a form with the same UID. If this happens, then the original form will be overwritten - if ($formAction === 'create') { - // Is there already a form that exists with this UID? Then we need to assign a new one. - // See discussion https://github.com/verbb/formie/discussions/1696 and actual issue https://github.com/verbb/formie/issues/1725 - $existingForm = Formie::$plugin->getForms()->getFormByHandle($form->handle); - - if ($existingForm) { - $form->uid = StringHelper::UUID(); - } - } - - if (!Craft::$app->getElements()->saveElement($form)) { + // check for errors + if( $form->getConsolidatedErrors() ){ $this->setFailFlash(Craft::t('formie', 'Unable to import form.')); Craft::$app->getUrlManager()->setRouteParams([ diff --git a/src/helpers/ImportExportHelper.php b/src/helpers/ImportExportHelper.php index b5414211b..617c4394e 100644 --- a/src/helpers/ImportExportHelper.php +++ b/src/helpers/ImportExportHelper.php @@ -22,6 +22,9 @@ use Craft; use craft\elements\Entry; use craft\helpers\Json; +use craft\db\Query; + +use yii\base\Exception; class ImportExportHelper { @@ -218,7 +221,8 @@ public static function createFormFromImport(array $data, ?Form $form = null): Fo foreach ($page['rows'] as $rowKey => &$row) { if (isset($row['fields'])) { foreach ($row['fields'] as $fieldKey => &$field) { - $existingField = $existingFields[$field['handle']] ?? null; + // dd($field); + $existingField = $existingFields[$field['settings']['handle']] ?? null; if ($existingField) { $field['id'] = $existingField->id; @@ -229,7 +233,7 @@ public static function createFormFromImport(array $data, ?Form $form = null): Fo foreach ($field['rows'] as $nestedRowKey => &$nestedRow) { if (isset($nestedRow['fields'])) { foreach ($nestedRow['fields'] as $nestedFieldKey => &$nestedField) { - $existingNestedField = $existingFields[$field['handle'] . '_fields'][$nestedField['handle']] ?? null; + $existingNestedField = $existingFields[$field['settings']['handle'] . '_fields'][$nestedField['handle']] ?? null; if ($existingNestedField) { $nestedField['id'] = $existingNestedField->id; @@ -304,6 +308,53 @@ public static function createFormFromImport(array $data, ?Form $form = null): Fo return $form; } + public static function importFormFromJson($json, $formAction = "update"): Form + { + + // Find an existing form with the same handle + $existingForm = null; + $formHandle = $json['handle'] ?? null; + + if ($formHandle) { + $existingForm = Formie::$plugin->getForms()->getFormByHandle($formHandle); + } + + // When creating a new form, change the handle + if ($formAction === 'create') { + $formHandles = (new Query()) + ->select(['handle']) + ->from(Table::FORMIE_FORMS) + ->column(); + + $json['handle'] = HandleHelper::getUniqueHandle($formHandles, $json['handle']); + } + + if ($formAction === 'update') { + // Update the form (force) + $form = self::createFormFromImport($json, $existingForm); + } else { + // Create the form element, ready to go + $form = self::createFormFromImport($json); + } + + // Because we also export the UID for forms, we need to check if we're importing a new form, but we've + // found a form with the same UID. If this happens, then the original form will be overwritten + if ($formAction === 'create') { + // Is there already a form that exists with this UID? Then we need to assign a new one. + // See discussion https://github.com/verbb/formie/discussions/1696 and actual issue https://github.com/verbb/formie/issues/1725 + $existingForm = Formie::$plugin->getForms()->getFormByHandle($form->handle); + + if ($existingForm) { + $form->uid = StringHelper::UUID(); + } + } + + Craft::$app->getElements()->saveElement($form); + + return $form; + + } + // Private Methods // ========================================================================= diff --git a/src/jobs/ImportForm.php b/src/jobs/ImportForm.php new file mode 100644 index 000000000..d998ff857 --- /dev/null +++ b/src/jobs/ImportForm.php @@ -0,0 +1,48 @@ +setProgress($queue, 0.33); + + if (!$this->fileLocation) { + throw new Exception("No file provided :"); + } + + $json = Json::decode(file_get_contents($this->fileLocation)); + $form = ImportExportHelper::importFormFromJson($json, $this->formAction); + + $this->setProgress($queue, 0.66); + + // check for errors + if ($form->getConsolidatedErrors()) { + $errors = Json::encode($form->getConsolidatedErrors()); + throw new Exception("Unable to import form {$this->fileLocation}" . PHP_EOL . "Errors: {$errors}"); + } + + $this->setProgress($queue, 1); + } + + protected function defaultDescription(): string + { + $fileName = basename($this->fileLocation); + return "Import of Json '$fileName'"; + } +} diff --git a/src/models/Settings.php b/src/models/Settings.php index 10232f0bf..e234f1995 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -9,6 +9,7 @@ use craft\base\Model; use craft\helpers\App; use craft\helpers\DateTimeHelper; +use craft\helpers\FileHelper; use yii\validators\EmailValidator; @@ -88,6 +89,8 @@ class Settings extends Model // Captcha settings are stored in Project Config, but otherwise private public array $captchas = []; + // Export + public string $defaultExportFolder = '@storage/formie-export'; // Public Methods // ========================================================================= @@ -192,4 +195,13 @@ protected function defineRules(): array return $rules; } + + public function getAbsoluteDefaultExportFolder(): ?string + { + $path = Craft::getAlias( $this->defaultExportFolder ); + $exportFolder = FileHelper::normalizePath($path); + FileHelper::createDirectory($exportFolder); + + return $exportFolder; + } } From b9a2a20e4d6718567297055abc23398d9e1b592e Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Mon, 8 Jul 2024 10:26:50 +1000 Subject: [PATCH 2/5] typo --- docs/get-started/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/get-started/configuration.md b/docs/get-started/configuration.md index 0ab14b6cd..626867537 100644 --- a/docs/get-started/configuration.md +++ b/docs/get-started/configuration.md @@ -115,7 +115,7 @@ return [ - `themeConfig` - Sets the configuration for theming your form and fields. See below for an example. ### Export -- `defaultExportFolder` - Sets the default folder for exported forms via console comandes. +- `defaultExportFolder` - Sets the default folder for exported forms via console commands. ## Control Panel From fda299601a2a0a188e9df768401999db0e1208e1 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Mon, 8 Jul 2024 10:28:41 +1000 Subject: [PATCH 3/5] Remove debug --- src/helpers/ImportExportHelper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/helpers/ImportExportHelper.php b/src/helpers/ImportExportHelper.php index 617c4394e..ff892a7b6 100644 --- a/src/helpers/ImportExportHelper.php +++ b/src/helpers/ImportExportHelper.php @@ -221,7 +221,6 @@ public static function createFormFromImport(array $data, ?Form $form = null): Fo foreach ($page['rows'] as $rowKey => &$row) { if (isset($row['fields'])) { foreach ($row['fields'] as $fieldKey => &$field) { - // dd($field); $existingField = $existingFields[$field['settings']['handle']] ?? null; if ($existingField) { From 304d153a8311d36f50f0fc3912326a90e6f49e17 Mon Sep 17 00:00:00 2001 From: Josh Crawford Date: Mon, 8 Jul 2024 10:31:24 +1000 Subject: [PATCH 4/5] typos --- src/console/controllers/FormsController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/console/controllers/FormsController.php b/src/console/controllers/FormsController.php index e8ddc4f53..576f32ae0 100644 --- a/src/console/controllers/FormsController.php +++ b/src/console/controllers/FormsController.php @@ -28,17 +28,17 @@ class FormsController extends Controller // ========================================================================= /** - * @var string form ID as comma separated list + * @var string form ID as a comma-separated list */ public ?string $formId = null; /** - * @var string form handle as comma separated list + * @var string form handle as a comma-separated list */ public ?string $formHandle = null; /** - * @var bool Create a new form, prevent updateing existing form + * @var bool Create a new form, prevent updating an existing form */ public bool $create = false; From 6e0a613ee4090dd98452a3ad82523d8af98c8537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eug=C3=A8ne=20Fritz?= Date: Mon, 8 Jul 2024 21:10:51 +0200 Subject: [PATCH 5/5] Use stdout for successful messages and stderr for errors Add early returns after error Fix typos in comments and method descriptions Small Refactoring --- src/console/controllers/FormsController.php | 66 +++++++++++---------- src/helpers/ImportExportHelper.php | 2 +- src/jobs/ImportForm.php | 9 +-- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/console/controllers/FormsController.php b/src/console/controllers/FormsController.php index 576f32ae0..b0b1d0e33 100644 --- a/src/console/controllers/FormsController.php +++ b/src/console/controllers/FormsController.php @@ -104,14 +104,14 @@ public function actionDelete(): int } $elementsText = $count === 1 ? 'form' : 'forms'; - $this->stdout("Deleting {$count} {$elementsText} for form #{$formId} ..." . PHP_EOL, Console::FG_YELLOW); + $this->stdout("Deleting $count $elementsText for form $formId ..." . PHP_EOL, Console::FG_YELLOW); $elementsService = Craft::$app->getElements(); foreach (Db::each($query) as $element) { $elementsService->deleteElement($element); - $this->stdout("Deleted form #{$element->id} ..." . PHP_EOL, Console::FG_GREEN); + $this->stdout("Deleted form $element->id ..." . PHP_EOL, Console::FG_GREEN); } } @@ -119,7 +119,7 @@ public function actionDelete(): int } /** - * list all possible Formie forms to be exported or imported + * List all possible Formie forms to be exported or imported. */ public function actionList($folderPath = null): int { @@ -127,13 +127,13 @@ public function actionList($folderPath = null): int try { $files = FileHelper::findFiles($path, ['only' => ['*.json']]); } catch (\Throwable $th) { - $this->stderr("Export directory is empty or don't exist" . PHP_EOL, Console::FG_RED); + $this->stderr("The export directory is empty or does not exist." . PHP_EOL, Console::FG_RED); return ExitCode::UNSPECIFIED_ERROR; } if (!empty($files)) { $listEntries[] = [ - 'title' => 'Json to import:', + 'title' => 'JSON to import:', 'entriesList' => array_map(function ($file) { return [ 'name' => $file, @@ -146,10 +146,10 @@ public function actionList($folderPath = null): int $allForms = Formie::$plugin->getForms()->getAllForms(); if (!empty($allForms)) { $listEntries[] = [ - 'title' => 'formie:', + 'title' => 'Formie forms:', 'entriesList' => array_map(function ($form) { return [ - 'name' => $form->handle, + 'name' => "$form->id: $form->handle", 'title' => $form->title ]; }, $allForms) @@ -157,14 +157,14 @@ public function actionList($folderPath = null): int } foreach ($listEntries as $entries) { - $this->stderr($entries['title'] . PHP_EOL, Console::FG_YELLOW); + $this->stdout($entries['title'] . PHP_EOL, Console::FG_YELLOW); $handleMaxLen = max(array_map('strlen', array_column($entries['entriesList'], 'name'))); foreach ($entries['entriesList'] as $entry) { - $this->stderr("- " . $entry['name'], Console::FG_GREEN); - $this->stderr(Console::moveCursorTo($handleMaxLen + 5)); - $this->stderr($entry['title'] . PHP_EOL); + $this->stdout("- " . $entry['name'], Console::FG_GREEN); + $this->stdout(Console::moveCursorTo($handleMaxLen + 5)); + $this->stdout($entry['title'] . PHP_EOL); } } @@ -173,7 +173,7 @@ public function actionList($folderPath = null): int } /** - * Export Formie forms as json, comma separated list of ids and/or handles + * Export Formie forms as JSON. Accepts comma-separated lists of form IDs and/or handles. */ public function actionExport($idsOrHandles = null): int { @@ -189,7 +189,6 @@ public function actionExport($idsOrHandles = null): int if (!$formIds) { $this->stderr('Unable to find any matching forms.' . PHP_EOL, Console::FG_RED); - return ExitCode::UNSPECIFIED_ERROR; } @@ -201,7 +200,7 @@ public function actionExport($idsOrHandles = null): int } $elementsText = $count === 1 ? 'form' : 'forms'; - $this->stdout("Exporting {$count} {$elementsText} ..." . PHP_EOL, Console::FG_YELLOW); + $this->stdout("Exporting $count $elementsText ..." . PHP_EOL, Console::FG_YELLOW); foreach (Db::each($query) as $element) { try { @@ -209,10 +208,10 @@ public function actionExport($idsOrHandles = null): int $json = Json::encode($formExport, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); $exportPath = $this->generateExportPathByHandle($element->handle); FileHelper::writeToFile($exportPath, $json); - $this->stdout("Exporting form #{$element->id} to {$exportPath}" . PHP_EOL, Console::FG_GREEN); + $this->stdout("Exporting form $element->id to $exportPath." . PHP_EOL, Console::FG_GREEN); } catch (Throwable $e) { - $this->stderr("Unable to export form #{$element->id}" . PHP_EOL); + $this->stderr("Unable to export form $element->id." . PHP_EOL, Console::FG_RED); return ExitCode::UNSPECIFIED_ERROR; } } @@ -221,23 +220,31 @@ public function actionExport($idsOrHandles = null): int } /** - * Import a Formie form json from a path. + * Import a Formie form JSON from a path. */ public function actionImport($fileLocation = null): int { if ($fileLocation === null) { - $this->stderr('You must provide a path to a Json file' . PHP_EOL, Console::FG_RED); + $this->stderr('You must provide a path to a JSON file.' . PHP_EOL, Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; } if (!is_file($fileLocation)) { - $this->stderr("No file exists for given path" . PHP_EOL, Console::FG_RED); + $this->stderr("No file exists at the given path." . PHP_EOL, Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; } if (strtolower(pathinfo($fileLocation, PATHINFO_EXTENSION)) !== 'json') { - $this->stderr("file is not of type Json" . PHP_EOL, Console::FG_RED); + $this->stderr("The file is not of type JSON." . PHP_EOL, Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; } - $json = Json::decode(file_get_contents($fileLocation)); + try { + $json = Json::decode(file_get_contents($fileLocation)); + } catch (\Exception $e) { + $this->stderr("Failed to decode JSON from the file." . PHP_EOL, Console::FG_RED); + return ExitCode::UNSPECIFIED_ERROR; + } // default, update existing form $formAction = $this->create ? 'create' : 'update'; @@ -246,20 +253,19 @@ public function actionImport($fileLocation = null): int // check for errors if ($form->getConsolidatedErrors()) { - $this->stderr("Unable to import form." . PHP_EOL, Console::FG_RED); + $this->stderr("Unable to import the form." . PHP_EOL, Console::FG_RED); $errors = Json::encode($form->getConsolidatedErrors()); - $this->stderr("Errors: {$errors}" . PHP_EOL, Console::FG_RED); - + $this->stderr("Errors: $errors" . PHP_EOL, Console::FG_RED); return ExitCode::UNSPECIFIED_ERROR; } - $this->stdout("Form {$form->handle} has be {$formAction}." . PHP_EOL, Console::FG_GREEN); + $this->stdout("Form $form->handle has be {$formAction}d." . PHP_EOL, Console::FG_GREEN); return ExitCode::OK; } /** - * Import all Formie json from a folder + * Import all Formie JSON from a folder. */ public function actionImportAll($folderPath = null): int { @@ -267,12 +273,12 @@ public function actionImportAll($folderPath = null): int try { $files = FileHelper::findFiles($path, ['only' => ['*.json']]); } catch (\Throwable $th) { - $this->stderr("Export directory is empty or don't exist" . PHP_EOL, Console::FG_RED); + $this->stderr("The export directory is empty or does not exist." . PHP_EOL, Console::FG_RED); return ExitCode::UNSPECIFIED_ERROR; } if (empty($files)) { - $this->stderr("No json fund in folder $path" . PHP_EOL, Console::FG_YELLOW); + $this->stderr("No JSON files found in folder $path." . PHP_EOL, Console::FG_RED); return ExitCode::UNSPECIFIED_ERROR; } @@ -287,7 +293,7 @@ public function actionImportAll($folderPath = null): int )); $basename = basename($file); - $this->stderr("File '{$basename}' has been added to the import queue." . PHP_EOL, Console::FG_GREEN); + $this->stdout("File '$basename' has been added to the import queue." . PHP_EOL, Console::FG_GREEN); } @@ -299,7 +305,7 @@ public function actionImportAll($folderPath = null): int private function generateExportPathByHandle($handle): string { - return $this->getExportPath() . DIRECTORY_SEPARATOR . "formie-{$handle}.json"; + return $this->getExportPath() . DIRECTORY_SEPARATOR . "formie-$handle.json"; } private function getExportPath(): string diff --git a/src/helpers/ImportExportHelper.php b/src/helpers/ImportExportHelper.php index ff892a7b6..18684f41d 100644 --- a/src/helpers/ImportExportHelper.php +++ b/src/helpers/ImportExportHelper.php @@ -232,7 +232,7 @@ public static function createFormFromImport(array $data, ?Form $form = null): Fo foreach ($field['rows'] as $nestedRowKey => &$nestedRow) { if (isset($nestedRow['fields'])) { foreach ($nestedRow['fields'] as $nestedFieldKey => &$nestedField) { - $existingNestedField = $existingFields[$field['settings']['handle'] . '_fields'][$nestedField['handle']] ?? null; + $existingNestedField = $existingFields[$field['settings']['handle'] . '_fields'][$nestedField['settings']['handle']] ?? null; if ($existingNestedField) { $nestedField['id'] = $existingNestedField->id; diff --git a/src/jobs/ImportForm.php b/src/jobs/ImportForm.php index d998ff857..0ddc41e41 100644 --- a/src/jobs/ImportForm.php +++ b/src/jobs/ImportForm.php @@ -2,10 +2,7 @@ namespace verbb\formie\jobs; - -use Craft; use craft\queue\BaseJob; -use yii\console\ExitCode; use craft\helpers\Json; use verbb\formie\helpers\ImportExportHelper; @@ -23,7 +20,7 @@ public function execute($queue): void $this->setProgress($queue, 0.33); if (!$this->fileLocation) { - throw new Exception("No file provided :"); + throw new Exception("No file provided."); } $json = Json::decode(file_get_contents($this->fileLocation)); @@ -34,7 +31,7 @@ public function execute($queue): void // check for errors if ($form->getConsolidatedErrors()) { $errors = Json::encode($form->getConsolidatedErrors()); - throw new Exception("Unable to import form {$this->fileLocation}" . PHP_EOL . "Errors: {$errors}"); + throw new Exception("Unable to import form {$this->fileLocation}" . PHP_EOL . "Errors: {$errors}."); } $this->setProgress($queue, 1); @@ -43,6 +40,6 @@ public function execute($queue): void protected function defaultDescription(): string { $fileName = basename($this->fileLocation); - return "Import of Json '$fileName'"; + return "Import of JSON '$fileName'."; } }