mirror of
https://github.com/processwire/processwire.git
synced 2025-08-16 19:54:24 +02:00
Minor code optimizations to the PagesExportImport modules
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* ProcessWire Page Export and Import
|
||||
*
|
||||
* ProcessWire 3.x, Copyright 2017 by Ryan Cramer
|
||||
* ProcessWire 3.x, Copyright 2024 by Ryan Cramer
|
||||
* https://processwire.com
|
||||
*
|
||||
* Note: this module supports page-edit-export and page-edit-import permissions, but currently the module is
|
||||
@@ -49,8 +49,10 @@ class ProcessPagesExportImport extends Process {
|
||||
*
|
||||
*/
|
||||
public function ___execute() {
|
||||
$user = $this->wire()->user;
|
||||
$input = $this->wire()->input;
|
||||
|
||||
if(!$this->wire('user')->isSuperuser()) {
|
||||
if(!$user->isSuperuser()) {
|
||||
throw new WirePermissionException($this->_('Export/import is currently only available to superuser'));
|
||||
}
|
||||
|
||||
@@ -58,9 +60,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$this->wire($this->exportImport);
|
||||
$this->exportImport->cleanupFiles(600);
|
||||
|
||||
$input = $this->wire('input');
|
||||
$user = $this->wire('user');
|
||||
$breadcrumbLabel = $this->wire('page')->title;
|
||||
$breadcrumbLabel = $this->wire()->page->title;
|
||||
|
||||
try {
|
||||
if($input->post('submit_export')) {
|
||||
@@ -77,19 +77,13 @@ class ProcessPagesExportImport extends Process {
|
||||
return $form->render();
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
$this->warning(
|
||||
'Please note this is a development version of pages export/import ' .
|
||||
'and not yet recommended for production use.'
|
||||
);
|
||||
*/
|
||||
$form = $this->buildForm();
|
||||
return $form->render();
|
||||
}
|
||||
} catch(\Exception $e) {
|
||||
if(self::debug) throw $e;
|
||||
$this->error($e->getMessage());
|
||||
$this->wire('session')->redirect($this->wire('page')->url);
|
||||
$this->wire()->session->location($this->wire()->page->url);
|
||||
}
|
||||
|
||||
return '';
|
||||
@@ -104,11 +98,9 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function buildForm($tab = '') {
|
||||
|
||||
/** @var Modules $modules */
|
||||
$modules = $this->wire('modules');
|
||||
$modules = $this->wire()->modules;
|
||||
$modules->get('JqueryWireTabs');
|
||||
/** @var User $user */
|
||||
$user = $this->wire('user');
|
||||
$user = $this->wire()->user;
|
||||
|
||||
/** @var InputfieldForm $form */
|
||||
$form = $modules->get('InputfieldForm');
|
||||
@@ -135,8 +127,7 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function buildImportTab() {
|
||||
|
||||
/** @var Modules $modules */
|
||||
$modules = $this->wire('modules');
|
||||
$modules = $this->wire()->modules;
|
||||
|
||||
/** @var InputfieldWrapper $tab */
|
||||
$tab = $this->wire(new InputfieldWrapper());
|
||||
@@ -185,8 +176,9 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function processImport() {
|
||||
|
||||
/** @var WireInput $input */
|
||||
$input = $this->wire('input');
|
||||
$input = $this->wire()->input;
|
||||
$files = $this->wire()->files;
|
||||
$session = $this->wire()->session;
|
||||
|
||||
/** @var InputfieldWrapper|InputfieldForm $importTab */
|
||||
$importTab = $this->buildForm('import');
|
||||
@@ -194,8 +186,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$submitCommit = $input->post('submit_commit_import') ? true : false;
|
||||
$submitTest = $input->post('submit_test_import') ? true : false;
|
||||
$submitZIP = !empty($_FILES['import_zip']) && !empty($_FILES['import_zip']['name'][0]);
|
||||
$fileField = null;
|
||||
$filesPath = $this->wire('session')->getFor($this, 'filesPath');
|
||||
$filesPath = $session->getFor($this, 'filesPath');
|
||||
$jsonFile = '';
|
||||
$a = null;
|
||||
|
||||
@@ -206,11 +197,11 @@ class ProcessPagesExportImport extends Process {
|
||||
$zipFile = $this->exportImport->getExportPath() . $fileField->value->first()->name;
|
||||
if(!$zipFile || !is_file($zipFile)) throw new WireException('No ZIP file found: ' . $zipFile);
|
||||
$unzipPath = $this->exportImport->getExportPath('import-zip');
|
||||
$zipFileItems = $this->wire('files')->unzip($zipFile, $unzipPath);
|
||||
$this->wire('files')->unlink($zipFile);
|
||||
$zipFileItems = $files->unzip($zipFile, $unzipPath);
|
||||
$files->unlink($zipFile);
|
||||
if(empty($zipFileItems)) throw new WireException("No files found in ZIP");
|
||||
$jsonFile = $unzipPath . "pages.json";
|
||||
$this->wire('session')->setFor($this, 'filesPath', $unzipPath);
|
||||
$session->setFor($this, 'filesPath', $unzipPath);
|
||||
|
||||
} else if(!empty($_POST['import_json'])) {
|
||||
// JSON import
|
||||
@@ -218,7 +209,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$json = $importTab->getChildByName('import_json')->val();
|
||||
if(empty($json)) throw new WireException($this->_('No import data found'));
|
||||
$a = json_decode($json, true);
|
||||
$this->wire('session')->setFor($this, 'filesPath', '');
|
||||
$session->setFor($this, 'filesPath', '');
|
||||
|
||||
} else if($filesPath) {
|
||||
// ZIP import commit or test
|
||||
@@ -252,6 +243,7 @@ class ProcessPagesExportImport extends Process {
|
||||
if($submitCommit) {
|
||||
$form->description = sprintf($this->_n('Imported %d page', 'Imported %d pages', $qty), $qty);
|
||||
foreach($form->children() as $f) {
|
||||
/** @var Inputfield $f */
|
||||
if($f->name != 'import_items') $form->remove($f);
|
||||
}
|
||||
} else {
|
||||
@@ -293,8 +285,8 @@ class ProcessPagesExportImport extends Process {
|
||||
// );
|
||||
set_time_limit(3600);
|
||||
|
||||
/** @var WireInput $input */
|
||||
$input = $this->wire('input');
|
||||
$input = $this->wire()->input;
|
||||
$config = $this->wire()->config;
|
||||
|
||||
$form->processInput($input->post);
|
||||
|
||||
@@ -317,12 +309,12 @@ class ProcessPagesExportImport extends Process {
|
||||
'changeName' => in_array('name', $fieldNames),
|
||||
'changeStatus' => in_array('status', $fieldNames),
|
||||
'changeSort' => in_array('sort', $fieldNames),
|
||||
'filesPath' => $this->wire('session')->getFor($this, 'filesPath'),
|
||||
'originalHost' => isset($a['host']) ? $a['host'] : $this->wire('config')->httpHost,
|
||||
'originalRootUrl' => isset($a['url']) ? $a['url'] : $this->wire('config')->urls->root,
|
||||
'filesPath' => $this->wire()->session->getFor($this, 'filesPath'),
|
||||
'originalHost' => isset($a['host']) ? $a['host'] : $config->httpHost,
|
||||
'originalRootUrl' => isset($a['url']) ? $a['url'] : $config->urls->root,
|
||||
);
|
||||
|
||||
foreach($a['pages'] as $key => $item) {
|
||||
foreach($a['pages'] as /* $key => */ $item) {
|
||||
$id = $item['settings']['id'];
|
||||
if($submitCommit && !$input->post("confirm$id")) continue;
|
||||
$page = $this->processImportItemToPage($item, $options);
|
||||
@@ -346,23 +338,12 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function identifyMissingResources(array &$a) {
|
||||
|
||||
/** @var Sanitizer $sanitizer */
|
||||
$sanitizer = $this->wire('sanitizer');
|
||||
|
||||
/** @var Modules $modules*/
|
||||
$modules = $this->wire('modules');
|
||||
|
||||
/** @var Templates $templates */
|
||||
$templates = $this->wire('templates');
|
||||
|
||||
/** @var Fields $fields */
|
||||
$fields = $this->wire('fields');
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = $this->wire('pages');
|
||||
|
||||
/** @var WireInput $input */
|
||||
$input = $this->wire('input');
|
||||
$sanitizer = $this->wire()->sanitizer;
|
||||
$modules = $this->wire()->modules;
|
||||
$templates = $this->wire()->templates;
|
||||
$fields = $this->wire()->fields;
|
||||
$pages = $this->wire()->pages;
|
||||
$input = $this->wire()->input;
|
||||
|
||||
$numFatalItems = 0;
|
||||
$missingItems = array();
|
||||
@@ -386,7 +367,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$templateName
|
||||
) . ' ' . $this->_('Select a template to substitute when creating these pages.');
|
||||
|
||||
foreach($this->wire('templates') as $t) {
|
||||
foreach($templates as $t) {
|
||||
$f->addOption($t->name);
|
||||
}
|
||||
|
||||
@@ -463,7 +444,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$optionsRecommended = array();
|
||||
$optionsOther = array();
|
||||
|
||||
foreach($this->wire('fields') as $_field) {
|
||||
foreach($fields as $_field) {
|
||||
/** @var Field $_field */
|
||||
if($_field->type->className() == $fieldtypeClass && !$_field->hasFlag(Field::flagSystem)) {
|
||||
$optionsRecommended[$_field->name] = $_field->name;
|
||||
@@ -569,8 +550,7 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function adjustImportData(&$a) {
|
||||
|
||||
/** @var WireInput $input */
|
||||
$input = $this->wire('input');
|
||||
$input = $this->wire()->input;
|
||||
|
||||
$importParentID = (int) $input->post('import_parent');
|
||||
$importParentType = $input->post('import_parent_type') === 'direct' ? 'direct' : 'below';
|
||||
@@ -654,8 +634,10 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function buildImportForm(InputfieldForm $tab, array &$a) {
|
||||
|
||||
$modules = $this->wire('modules');
|
||||
|
||||
$modules = $this->wire()->modules;
|
||||
$input = $this->wire()->input;
|
||||
|
||||
/** @var InputfieldForm $form */
|
||||
$form = $modules->get('InputfieldForm');
|
||||
$form->attr('id', 'import-form');
|
||||
$form->description = $this->_('Import summary');
|
||||
@@ -672,6 +654,7 @@ class ProcessPagesExportImport extends Process {
|
||||
/** @var InputfieldFieldset $importTab */
|
||||
$importTab = $tab->getChildByName('tab_import');
|
||||
foreach($importTab->children() as $f) {
|
||||
/** @var Inputfield $f */
|
||||
if($f->attr('name') == 'import_zip') {
|
||||
continue;
|
||||
} else if($f instanceof InputfieldSubmit) {
|
||||
@@ -715,7 +698,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->icon = 'female';
|
||||
$f->showIf = 'import_mode!=update';
|
||||
$f->startLabel = $this->_('Choose parent');
|
||||
$checkedDirect = $this->wire('input')->post('import_parent_type') == 'direct' ? "checked='checked'" : '';
|
||||
$checkedDirect = $input->post('import_parent_type') == 'direct' ? "checked='checked'" : '';
|
||||
$checkedBelow = $checkedDirect ? '' : "checked='checked'";
|
||||
$f->appendMarkup =
|
||||
"<p class='InputfieldRadios'>" .
|
||||
@@ -758,13 +741,14 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->addOption('status', "status|" . $this->_('Page status') . "|System");
|
||||
$f->addOption('sort', "sort|" . $this->_('Page sort index') . "|System");
|
||||
$f->attr('value', $value);
|
||||
if(!$this->wire('input')->post('submit_import')) $f->collapsed = Inputfield::collapsedYes;
|
||||
if(!$input->post('submit_import')) $f->collapsed = Inputfield::collapsedYes;
|
||||
$form->add($f);
|
||||
|
||||
$submitTest = $this->wire('input')->post('submit_test_import') ? true : false;
|
||||
$submitCommit = $this->wire('input')->post('submit_commit_import') ? true : false;
|
||||
$submitTest = $input->post('submit_test_import') ? true : false;
|
||||
$submitCommit = $input->post('submit_commit_import') ? true : false;
|
||||
|
||||
if($submitCommit || $submitTest) {
|
||||
/** @var InputfieldFieldset $fieldset */
|
||||
$fieldset = $modules->get('InputfieldFieldset');
|
||||
$fieldset->attr('name', 'import_items');
|
||||
$fieldset->label = $this->_('Import pages');
|
||||
@@ -781,6 +765,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$form->add($f);
|
||||
|
||||
if(($submitTest || $submitCommit) && empty($a['_noCommit'])) {
|
||||
/** @var InputfieldSubmit $f */
|
||||
$f = $modules->get('InputfieldSubmit');
|
||||
$f->attr('name', 'submit_commit_import');
|
||||
$f->val($this->_('Commit Import'));
|
||||
@@ -796,25 +781,27 @@ class ProcessPagesExportImport extends Process {
|
||||
* Build a summary InputfieldMarkup for a single item/Page
|
||||
*
|
||||
* @param array $item Import item array
|
||||
* @param Page|NullPage $page Resulting Page object
|
||||
* @param Page $page Resulting Page object
|
||||
* @param array $options Import options that were passed to PagesExportImport import() method
|
||||
* @return InputfieldMarkup
|
||||
*
|
||||
*/
|
||||
protected function buildImportItemSummary(array $item, Page $page, array $options) {
|
||||
|
||||
$sanitizer = $this->wire('sanitizer');
|
||||
$sanitizer = $this->wire()->sanitizer;
|
||||
$languages = $this->wire()->languages;
|
||||
|
||||
$changes = $page->get('_importChanges');
|
||||
$importType = $page->get('_importType');
|
||||
$languages = $this->wire('languages');
|
||||
$numChanges = wireCount($changes);
|
||||
$originalID = (int) $item['settings']['id'];
|
||||
$out = '';
|
||||
|
||||
static $n = 0;
|
||||
$n++;
|
||||
|
||||
$f = $this->wire('modules')->get('InputfieldMarkup');
|
||||
|
||||
/** @var InputfieldMarkup $f */
|
||||
$f = $this->wire()->modules->get('InputfieldMarkup');
|
||||
$f->addClass('import-form-item');
|
||||
$f->label = "$n. ";
|
||||
|
||||
@@ -869,7 +856,7 @@ class ProcessPagesExportImport extends Process {
|
||||
foreach($notices as $notice) {
|
||||
$noticeText = $sanitizer->entities($notice->text);
|
||||
if($noticeType != 'messages') {
|
||||
$icon = $noticeType == 'errors' ? 'warning' : 'warning';
|
||||
$icon = 'warning';
|
||||
} else {
|
||||
$icon = 'check';
|
||||
}
|
||||
@@ -885,8 +872,8 @@ class ProcessPagesExportImport extends Process {
|
||||
} else if($numChanges) {
|
||||
// Page (success)
|
||||
$attr = "type='radio' name='confirm$originalID' class='import-confirm'";
|
||||
$val = $this->wire('input')->post("confirm$originalID");
|
||||
if(($val == $originalID || $val === null) && $numChanges) {
|
||||
$val = $this->wire()->input->post("confirm$originalID");
|
||||
if($val == $originalID || $val === null) {
|
||||
$checkedYes = "checked='checked'";
|
||||
$checkedNo = "";
|
||||
} else {
|
||||
@@ -916,14 +903,13 @@ class ProcessPagesExportImport extends Process {
|
||||
*/
|
||||
protected function buildExportTab() {
|
||||
|
||||
$modules = $this->wire('modules');
|
||||
|
||||
/** @var InputfieldWrapper $tab */
|
||||
$tab = $this->wire(new InputfieldWrapper());
|
||||
$tab->attr('id+name', 'tab_export');
|
||||
$tab->attr('title', $this->_('Export'));
|
||||
$tab->addClass('WireTab');
|
||||
|
||||
$f = $modules->get('InputfieldRadios');
|
||||
$f = $tab->InputfieldRadios;
|
||||
$f->attr('name', 'export_type');
|
||||
$f->label = $this->_('What pages do you want to export?');
|
||||
$f->icon = 'sitemap';
|
||||
@@ -932,7 +918,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->addOption('selector', $this->_('Pages matching search'));
|
||||
$tab->add($f);
|
||||
|
||||
$f = $modules->get('InputfieldPageListSelectMultiple');
|
||||
$f = $tab->InputfieldPageListSelectMultiple;
|
||||
$f->attr('name', 'pages_specific');
|
||||
$f->label = $this->_('Select pages');
|
||||
$f->description = $this->_('Select one or more pages to include in the export.');
|
||||
@@ -940,7 +926,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->showIf = 'export_type=specific';
|
||||
$tab->add($f);
|
||||
|
||||
$f = $modules->get('InputfieldPageListSelect');
|
||||
$f = $tab->InputfieldPageListSelect;
|
||||
$f->attr('name', 'pages_parent');
|
||||
$f->label = $this->_('Select parent page');
|
||||
$f->description = $this->_('Select the parent of the pages you want to export. The children of this page will be exported.');
|
||||
@@ -948,7 +934,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->showIf = 'export_type=parent';
|
||||
$tab->add($f);
|
||||
|
||||
$f = $modules->get('InputfieldCheckboxes');
|
||||
$f = $tab->InputfieldCheckboxes;
|
||||
$f->attr('name', 'options_parent');
|
||||
$f->label = $this->_('Additional options');
|
||||
$f->icon = 'sliders';
|
||||
@@ -960,7 +946,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->addOption('unpublished', $this->_('Include hidden and unpublished pages'));
|
||||
$tab->add($f);
|
||||
|
||||
$f = $modules->get('InputfieldSelector');
|
||||
$f = $tab->InputfieldSelector;
|
||||
$f->attr('name', 'pages_selector');
|
||||
$f->label = $this->_('Build a search to match pages for export');
|
||||
$f->description = $this->_('Add one or more fields to search and match pages for export.');
|
||||
@@ -968,7 +954,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$f->showIf = 'export_type=selector';
|
||||
$tab->add($f);
|
||||
|
||||
$f = $modules->get('InputfieldCheckboxes');
|
||||
$f = $tab->InputfieldCheckboxes;
|
||||
$f->attr('name', 'export_fields');
|
||||
$f->label = $this->_('Export fields');
|
||||
$f->description =
|
||||
@@ -1003,8 +989,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$tab->add($f);
|
||||
*/
|
||||
|
||||
/** @var InputfieldSubmit $f */
|
||||
$f = $modules->get('InputfieldSubmit');
|
||||
$f = $tab->InputfieldSubmit;
|
||||
$f->attr('name', 'submit_export');
|
||||
$f->value = $this->_('Export Now');
|
||||
$f->showIf = $showIf;
|
||||
@@ -1035,10 +1020,10 @@ class ProcessPagesExportImport extends Process {
|
||||
|
||||
set_time_limit(3600);
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = $this->wire('pages');
|
||||
/** @var WireInput $input */
|
||||
$input = $this->wire('input');
|
||||
$pages = $this->wire()->pages;
|
||||
$input = $this->wire()->input;
|
||||
$modules = $this->wire()->modules;
|
||||
$files = $this->wire()->files;
|
||||
|
||||
$form = $this->buildForm();
|
||||
$form->processInput($input->post);
|
||||
@@ -1100,8 +1085,9 @@ class ProcessPagesExportImport extends Process {
|
||||
if($exportTo == 'json') {
|
||||
// json
|
||||
$json = $exporter->exportJSON($exportPages, $exportOptions);
|
||||
$form = $this->wire('modules')->get('InputfieldForm');
|
||||
$f = $this->wire('modules')->get('InputfieldTextarea');
|
||||
/** @var InputfieldForm $form */
|
||||
$form = $modules->get('InputfieldForm');
|
||||
$f = $form->InputfieldTextarea;
|
||||
$f->attr('id+name', 'export_json');
|
||||
$f->label = $this->_('Pages export data for copy/paste');
|
||||
$f->description = sprintf(
|
||||
@@ -1119,11 +1105,11 @@ class ProcessPagesExportImport extends Process {
|
||||
// zip file download
|
||||
$zipFile = $exporter->exportZIP($exportPages, $exportOptions);
|
||||
if($zipFile) {
|
||||
$this->wire('files')->send($zipFile, array(
|
||||
$files->send($zipFile, array(
|
||||
'forceDownload' => true,
|
||||
'exit' => false
|
||||
));
|
||||
$this->wire('files')->unlink($zipFile);
|
||||
$files->unlink($zipFile);
|
||||
exit;
|
||||
} else {
|
||||
throw new WireException('Export failed during ZIP file generation');
|
||||
@@ -1143,7 +1129,7 @@ class ProcessPagesExportImport extends Process {
|
||||
$exporter = new PagesExportImport();
|
||||
$this->wire($exporter);
|
||||
$fields = array();
|
||||
foreach($this->wire('fields') as $field) {
|
||||
foreach($this->wire()->fields as $field) {
|
||||
if(!$field->type) continue;
|
||||
$info = $exporter->getFieldInfo($field);
|
||||
if($info['exportable']) $fields[$field->name] = $field;
|
||||
@@ -1169,4 +1155,3 @@ class ProcessPagesExportImport extends Process {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user