Skip to content

Instantly share code, notes, and snippets.

@CanRau
Forked from apeisa/ProcessRoskis.module
Created May 30, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CanRau/478b5006d418a2acc55b to your computer and use it in GitHub Desktop.
Save CanRau/478b5006d418a2acc55b to your computer and use it in GitHub Desktop.
<?php
/**
* Roskis
*
* Adds custom trash view that can be also accessed by defined roles
* Pages in trash can be searched, sorted and restored.
*
*
* Copyright 2013 by Antti Peisa
*
*
* ProcessWire 2.x
* Copyright (C) 2012 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class ProcessRoskis extends Process {
/**
* Return information about this module (required)
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Roskis',
'summary' => 'Custom trash view for clients also.',
'version' => 1,
'author' => 'Antti Peisa',
'href' => 'http://modules.processwire.com/',
'permission' => 'page-edit',
'installs' => 'RoskisWatchdog'
);
}
const pageName = 'roskis';
public function ___execute() {
$out = '';
$trash = $this->pages->get($this->config->trashPageID);
$trashedPages = $trash->children("include=all");
if ($trashedPages->count() == 0) return $this->_("Trash empty");
foreach ($trashedPages as $page) {
$sql = "SELECT parent_id FROM {$this->className} WHERE page_id = {$page->id};";
$result = $this->db->query($sql)->fetch_row();
$parent = $this->pages->get($result[0]);
$out .= "$page->title from {$parent->title} - <a href='./restore/?page_id={$page->id}&parent_id={$parent->id}'>restore</a><br>";
}
return $out;
}
public function ___executeRestore() {
if ($this->input->post->restore_page) return $this->processInput ();
$page = $this->pages->get($this->input->get->page_id);
$out = '';
if (!$page->id) {
$this->error($this->_("Page not found"));
$this->session->redirect("../");
}
if (!$page->isTrash()) {
$this->error($this->_("Page is not in trash"));
$this->session->redirect("../");
}
// set a new headline, replacing the one used by our page (optional)
Wire::setFuel('processHeadline', $this->_("Restore page"));
$out .= "<h2>$page->title</h2>";
$form = $this->modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$field = $this->modules->get("InputfieldPageListSelect");
$field->parent_id = 0;
$field->value = (int) $this->input->get->parent_id;
$field->required = true;
$field->label = $this->_("Parent page for the restored page");
$field->name = "parent_id";
$form->add($field);
$field = $this->modules->get("InputfieldHidden");
$field->name = "page_id";
$field->value = $page->id;
$form->add($field);
$submit = $this->modules->get("InputfieldSubmit");
$submit->name = "restore_page";
$form->add($submit);
$out .= $form->render();
$out .= "<p><a href='../'>" . $this->_("Go Back") . "</a></p>";
// add a breadcrumb that returns to our main page
$this->breadcrumbs->add(new Breadcrumb('../', 'Hello'));
return $out;
}
public function processInput() {
if (!$this->input->post->parent_id || !$this->input->post->page_id) {
$this->error($this->_("Error while trying to restore the page"));
return "<a href='../'>". $this->_("Go back") ."</a>";
}
$page = $this->pages->get($this->input->post->page_id);
$page->parent = $this->pages->get($this->input->post->parent_id);
$this->pages->restore($page);
$this->message($this->_("Page restored"));
$this->session->redirect("../");
}
/**
* Called only when your module is installed
*
* This version creates a new page with this Process module assigned.
*
*/
public function ___install() {
$page = new Page();
$page->template = 'admin';
$page->name = self::pageName;
$page->parent = $this->pages->get($this->config->adminRootPageID)->child('name=setup');
$page->process = $this;
$page->title = "Special kind of trash";
$page->save();
$this->message("Created Page: {$page->path}");
$sql = <<< _END
CREATE TABLE {$this->className} (
id int unsigned NOT NULL auto_increment,
page_id int unsigned DEFAULT 0,
parent_id int unsigned DEFAULT 0,
PRIMARY KEY(id),
UNIQUE KEY(page_id)
) ENGINE = MYISAM;
_END;
$this->db->query($sql);
$this->message("Created database table: {$this->className}");
}
/**
* Called only when your module is uninstalled
*
* This should return the site to the same state it was in before the module was installed.
*
*/
public function ___uninstall() {
// find the page we installed, locating it by the process field (which has the module ID)
// it would probably be sufficient just to locate by name, but this is just to be extra sure.
$moduleID = $this->modules->getModuleID($this);
$page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName);
if($page->id) {
// if we found the page, let the user know and delete it
$this->message("Deleting Page: {$page->path}");
$page->delete();
}
$this->db->query("DROP TABLE {$this->className}");
}
}
<?php
/**
* Roskis Watchdog
*
* Copyright 2013 by Antti Peisa
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class RoskisWatchdog extends WireData implements Module {
/**
* getModuleInfo is a module required by all modules to tell ProcessWire about them
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => 'Roskis Watchdog',
'version' => 1,
'summary' => 'Autoload module, that saves the parent_before_trash value before page is trashed',
'href' => 'http://modules.processwire.com',
'singular' => true,
'autoload' => true,
'requires' => 'ProcessRoskis'
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// add a hook after the $pages->save, to issue a notice every time a page is saved
$this->pages->addHookBefore('trash', $this, 'pageTrashed');
}
/**
* Example1 hooks into the pages->save method and displays a notice every time a page is saved
*
*/
public function pageTrashed($event) {
$page = $event->arguments[0];
if ($page->parent->id != $this->config->trashPageID) {
$sql = "INSERT INTO ProcessRoskis SET page_id = {$page->id}, parent_id = {$page->parent->id} ON DUPLICATE KEY UPDATE parent_id = {$page->parent->id};";
$this->db->query($sql);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment