1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-24 15:23:11 +02:00

Code improvements and some refactoring of ProcessTemplate module

This commit is contained in:
Ryan Cramer
2022-01-14 13:45:14 -05:00
parent 842ea3df79
commit 883b0ab438
2 changed files with 1000 additions and 635 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,16 @@
<?php namespace ProcessWire; <?php namespace ProcessWire;
/**
* @method string buildExport()
* @method InputfieldForm buildInputDataForm()
* @method string buildImport()
* @method void processImport()
*
*/
class ProcessTemplateExportImport extends Wire { class ProcessTemplateExportImport extends Wire {
/** @var Templates $items */
protected $items; protected $items;
public function __construct() { public function __construct() {
@@ -129,12 +138,16 @@ class ProcessTemplateExportImport extends Wire {
*/ */
protected function ___buildInputDataForm() { protected function ___buildInputDataForm() {
$form = $this->modules->get('InputfieldForm'); $modules = $this->wire()->modules;
/** @var InputfieldForm $form */
$form = $modules->get('InputfieldForm');
$form->action = './'; $form->action = './';
$form->method = 'post'; $form->method = 'post';
$form->attr('id', 'import_form'); $form->attr('id', 'import_form');
$f = $this->modules->get('InputfieldTextarea'); /** @var InputfieldTextarea $f */
$f = $modules->get('InputfieldTextarea');
$f->attr('name', 'import_data'); $f->attr('name', 'import_data');
$f->label = $this->_x('Import', 'input'); $f->label = $this->_x('Import', 'input');
$f->icon = 'paste'; $f->icon = 'paste';
@@ -143,7 +156,8 @@ class ProcessTemplateExportImport extends Wire {
$f->notes = $this->_('Copy the export data from another installation and then paste into the box above with CTRL-V or CMD-V.'); $f->notes = $this->_('Copy the export data from another installation and then paste into the box above with CTRL-V or CMD-V.');
$form->add($f); $form->add($f);
$f = $this->wire('modules')->get('InputfieldSubmit') ; /** @var InputfieldSubmit $f */
$f = $modules->get('InputfieldSubmit') ;
$f->attr('name', 'submit_import'); $f->attr('name', 'submit_import');
$f->attr('value', $this->_('Preview')); $f->attr('value', $this->_('Preview'));
$form->add($f); $form->add($f);
@@ -160,7 +174,10 @@ class ProcessTemplateExportImport extends Wire {
*/ */
public function ___buildImport() { public function ___buildImport() {
if($this->input->post('submit_commit')) return $this->processImport(); if($this->input->post('submit_commit')) {
$this->processImport();
return '';
}
$verify = (int) $this->input->get('verify'); $verify = (int) $this->input->get('verify');
@@ -174,6 +191,7 @@ class ProcessTemplateExportImport extends Wire {
$data = is_array($json) ? $json : wireDecodeJSON($json); $data = is_array($json) ? $json : wireDecodeJSON($json);
if(!$data) throw new WireException("Invalid import data"); if(!$data) throw new WireException("Invalid import data");
/** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm'); $form = $this->modules->get('InputfieldForm');
$form->action = './'; $form->action = './';
$form->method = 'post'; $form->method = 'post';
@@ -209,6 +227,7 @@ class ProcessTemplateExportImport extends Wire {
$name = $this->wire('sanitizer')->name($name); $name = $this->wire('sanitizer')->name($name);
$template = $this->wire('templates')->get($name); $template = $this->wire('templates')->get($name);
$numChangesTemplate = 0; $numChangesTemplate = 0;
/** @var InputfieldFieldset $fieldset */
$fieldset = $this->modules->get('InputfieldFieldset'); $fieldset = $this->modules->get('InputfieldFieldset');
$fieldset->label = $name; $fieldset->label = $name;
$form->add($fieldset); $form->add($fieldset);
@@ -223,6 +242,7 @@ class ProcessTemplateExportImport extends Wire {
$fieldset->icon = 'moon-o'; $fieldset->icon = 'moon-o';
} }
/** @var InputfieldMarkup $markup */
$markup = $this->modules->get('InputfieldMarkup'); $markup = $this->modules->get('InputfieldMarkup');
$markup->addClass('InputfieldCheckboxes'); $markup->addClass('InputfieldCheckboxes');
$markup->value = ""; $markup->value = "";
@@ -233,6 +253,7 @@ class ProcessTemplateExportImport extends Wire {
$changes = $template->setImportData($templateData); $changes = $template->setImportData($templateData);
$template->setImportData($savedTemplateData); // restore $template->setImportData($savedTemplateData); // restore
} catch(\Exception $e) { } catch(\Exception $e) {
$changes = array();
$this->error($e->getMessage()); $this->error($e->getMessage());
} }
@@ -335,6 +356,7 @@ class ProcessTemplateExportImport extends Wire {
$form->description = $this->_('No changes were found'); $form->description = $this->_('No changes were found');
} }
/** @var InputfieldButton $f */
$f = $this->modules->get('InputfieldButton'); $f = $this->modules->get('InputfieldButton');
$f->href = './'; $f->href = './';
$f->value = $this->_x('Ok', 'button'); $f->value = $this->_x('Ok', 'button');
@@ -419,12 +441,19 @@ class ProcessTemplateExportImport extends Wire {
$this->session->redirect("./?verify=1"); $this->session->redirect("./?verify=1");
} }
/**
* @param Template $item
* @param array $changes
*
*/
public function saveItem($item, array $changes) { public function saveItem($item, array $changes) {
if($changes) {} // ignore
/** @var Fieldgroup $fieldgroup */
$fieldgroup = $item->fieldgroup; $fieldgroup = $item->fieldgroup;
$fieldgroup->save(); $fieldgroup->save();
$fieldgroup->saveContext(); $fieldgroup->saveContext();
$item->save(); $item->save();
if(!$item->fieldgroup_id) { if(!$item->fieldgroup) {
$item->setFieldgroup($fieldgroup); $item->setFieldgroup($fieldgroup);
$item->save(); $item->save();
} }