praiadeseselle/wire/modules/Process/ProcessPageTrash.module

162 lines
4.9 KiB
Text
Raw Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* ProcessWire Page Trash Process
*
* Provides empty trash capability.
*
* For more details about how Process modules work, please see:
* /wire/core/Process.php
*
2023-03-10 19:41:40 +01:00
* ProcessWire 3.x, Copyright 2022 by Ryan Cramer
2022-03-08 15:55:41 +01:00
* https://processwire.com
*
*/
class ProcessPageTrash extends Process {
const debug = false;
const defaultTimeLimit = 30;
const defaultPageLimit = 0;
public static function getModuleInfo() {
return array(
'title' => __('Page Trash', __FILE__), // getModuleInfo title
'summary' => __('Handles emptying of Page trash', __FILE__), // getModuleInfo summary
'version' => 103,
'permanent' => true,
2023-03-10 19:41:40 +01:00
);
2022-03-08 15:55:41 +01:00
}
/**
* Check if an empty request has been received and delete if so, otherwise render a confirmation form
*
*/
public function ___execute() {
2023-03-10 19:41:40 +01:00
if(!$this->wire()->user->isSuperuser()) throw new WirePermissionException();
$input = $this->wire()->input;
2022-03-08 15:55:41 +01:00
$timeLimit = abs((int) $input->post('time_limit'));
if($timeLimit > 0) $this->session->setFor($this, 'timeLimit', $timeLimit);
if(!$input->post('submit_empty') || !$input->post('confirm_empty')) {
// render a form showing what pages are in the trash and confirming they want to empty it
if($input->post('submit_empty')) $this->warning($this->_('You must check the box to confirm'));
return $this->render();
}
$this->session->CSRF->validate();
$options = array(
'verbose' => true,
'timeLimit' => $timeLimit > 0 ? $timeLimit : self::defaultTimeLimit,
);
2023-03-10 19:41:40 +01:00
$result = $this->wire()->pages->emptyTrash($options);
2022-03-08 15:55:41 +01:00
if(self::debug) $this->warning($result);
$error = false;
$message = sprintf($this->_n('Deleted %d page', 'Deleted %d pages', $result['numDeleted']), $result['numDeleted']);
if($result['numDeleted'] && $result['pagesPerSecond'] && $result['numDeleted'] > $result['pagesPerSecond']) {
$message .= ' ' . sprintf($this->_('(%d pages per second)'), $result['pagesPerSecond']);
}
if($result['numRemain'] > 0) {
$message .= ' - ' . sprintf($this->_('Not all pages could be deleted (%d remain)'), $result['numRemain']);
$error = true;
}
if($result['timeExpired']) {
$message .= ' - ' . sprintf($this->_('Time limit reached (%d seconds)'), "$options[timeLimit]");
$error = true;
}
2023-03-10 19:41:40 +01:00
$session = $this->wire()->session;
2022-03-08 15:55:41 +01:00
if($error) {
2023-03-10 19:41:40 +01:00
$session->warning($message);
$session->redirect('./');
2022-03-08 15:55:41 +01:00
} else {
// redirect to admin root after emptying trash
2023-03-10 19:41:40 +01:00
$session->message($message);
$session->redirect($this->wire()->config->urls('admin'));
2022-03-08 15:55:41 +01:00
}
return ''; // unreachable due to redirects above
}
/**
* Render a form showing what pages are in the trash and confirming they want to empty it
*
*/
protected function render() {
2023-03-10 19:41:40 +01:00
$modules = $this->wire()->modules;
2022-03-08 15:55:41 +01:00
$trashTotal = $this->pages->trasher()->getTrashTotal();
/** @var InputfieldForm $form */
2023-03-10 19:41:40 +01:00
$form = $modules->get("InputfieldForm");
2022-03-08 15:55:41 +01:00
$form->attr('action', './');
$form->attr('method', 'post');
if(!$trashTotal) return "<h2>" . $this->_("The trash is empty") . "</h2>";
/** @var InputfieldCheckbox $field */
2023-03-10 19:41:40 +01:00
$field = $modules->get("InputfieldCheckbox");
2022-03-08 15:55:41 +01:00
$field->attr('name', 'confirm_empty');
$field->attr('value', 1);
$field->label2 = $this->_('Empty the trash (confirm)');
$field->label = sprintf(
$this->_n('Permanently delete %d page in the trash?', 'Permanently delete %d pages in the trash?', $trashTotal),
$trashTotal
);
if($trashTotal > 100) {
$field->notes = $this->_("If there are too many items in the trash, you may have to empty it multiple times.");
}
$form->add($field);
/** @var InputfieldMarkup $field */
2023-03-10 19:41:40 +01:00
$field = $modules->get("InputfieldMarkup");
2022-03-08 15:55:41 +01:00
$field->label = $this->_('Pages in the trash');
$field->icon = 'list';
$field->collapsed = Inputfield::collapsedYes;
/** @var ProcessPageList $pageList */
$pageList = $this->modules->get('ProcessPageList');
$pageList->set('id', $this->config->trashPageID);
$pageList->set('showRootPage', false);
$field->value = $pageList->execute();
$form->add($field);
/** @var InputfieldInteger $f */
2023-03-10 19:41:40 +01:00
$f = $modules->get('InputfieldInteger');
2022-03-08 15:55:41 +01:00
$f->attr('name', 'time_limit');
$f->label = $this->_('Time limit (in seconds)');
$timeLimit = (int) $this->session->getFor($this, 'timeLimit');
$f->attr('value', $timeLimit > 0 ? $timeLimit : self::defaultTimeLimit);
$f->icon = 'clock-o';
$f->collapsed = Inputfield::collapsedYes;
$form->add($f);
/** @var InputfieldSubmit $field */
2023-03-10 19:41:40 +01:00
$field = $modules->get("InputfieldSubmit");
2022-03-08 15:55:41 +01:00
$field->attr('name', 'submit_empty');
$field->showInHeader(true);
$field->icon = 'trash';
$form->add($field);
2023-03-10 19:41:40 +01:00
/** @var InputfieldButton $field */
$field = $modules->get("InputfieldButton");
2022-03-08 15:55:41 +01:00
$field->attr('name', 'submit_cancel');
$field->setSecondary(true);
$field->value = $this->_('Cancel');
2023-03-10 19:41:40 +01:00
$field->href = $this->wire()->config->urls->admin;
2022-03-08 15:55:41 +01:00
$field->icon = 'times';
$form->add($field);
return $form->render();
}
}