/wire/modules/Process/ProcessPageSort.module
http://github.com/ryancramerdesign/ProcessWire · Unknown · 140 lines · 114 code · 26 blank · 0 comment · 0 complexity · a331309656085004c3554e211a670f7a MD5 · raw file
- <?php
- /**
- * ProcessWire Page Sort Process
- *
- * Saves moved or sorted pages for the PageList process.
- * Intended to be executed via an ajax call.
- *
- * For more details about how Process modules work, please see:
- * /wire/core/Process.php
- *
- * ProcessWire 2.x
- * Copyright (C) 2012 by Ryan Cramer
- * Licensed under GNU/GPL v2, see LICENSE.TXT
- *
- * http://www.processwire.com
- * http://www.ryancramer.com
- *
- */
- class ProcessPageSort extends Process {
- protected $ids = array();
- protected $parent_id = 0;
- protected $move_id = 0;
- protected $user;
- protected $isMoved = false;
- public static function getModuleInfo() {
- return array(
- 'title' => __('Page Sort and Move', __FILE__), // getModuleInfo title
- 'summary' => __('Handles page sorting and moving for PageList', __FILE__), // getModuleInfo summary
- 'version' => 100,
- 'permanent' => true,
- 'permission' => 'page-edit',
- );
- }
- /**
- * Install a new permission in addition to the regular ProcessPageSort permission
- *
- * The "ProcessPageSortMove" permission refers to changing the page's parent,
- * whereas the "ProcessPageSort" permission refers to changing the sort within the same parent.
- *
- */
- public function ___install() {
- parent::___install();
- }
- /**
- * Save a move/sort request
- *
- */
- public function ___execute() {
- if($this->config->demo) throw new WireException($this->_("Your change was not saved because this site is in demo mode"));
- if(!isset($_POST['sort'])) throw new WireException($this->_("This Process is only accessible via POST"));
- $this->session->CSRF->validate(); // throws exception if invalid
- $this->user = $this->fuel('user');
- $this->ids = array();
- $ids = explode(',', $_POST['sort']);
- foreach($ids as $sort => $id) $this->ids[(int) $sort] = (int) $id;
- if(!count($this->ids)) return;
- unset($ids);
- $this->parent_id = (int) $_POST['parent_id'];
- $this->move_id = (int) $_POST['id'];
-
- $parentPage = $this->fuel('pages')->get($this->parent_id);
- $movePage = $this->fuel('pages')->get($this->move_id);
- if($movePage->id < 2 || !$parentPage->id) return;
- $this->movePage($movePage, $parentPage);
- $this->sortPages($movePage, $parentPage);
- }
- /**
- * Saves a page that has had it's parent_id changed
- *
- */
- protected function movePage(Page $page, Page $parent) {
- if($page->parent_id == $parent->id) return;
- if(!$page->moveable($parent))
- throw new WirePermissionException($this->_("You do not have permission to move pages using this parent") . " - {$parent->path}");
- $page->setOutputFormatting(false);
- $page->resetTrackChanges(true);
- $page->parent = $parent;
- $page->save();
- $this->message("Moved page $page to parent $parent");
- $this->isMoved = true;
- }
- /**
- * Updates the sortfield for all pages having the same parent
- *
- */
- protected function sortPages(Page $page, Page $parent) {
- if(!$page->sortable()) {
- if(!$this->isMoved) throw new WirePermissionException($this->_("You do not have permission to sort pages using this parent") . " - {$parent->path}");
- return;
- }
- if($parent->sortfield && $parent->sortfield != 'sort') {
- $msg = sprintf($this->_("Your sort was not saved because these pages are automatically sorted by %s."), $parent->sortfield);
- if(!$this->isMoved) throw new WireException($msg);
- else $this->message($msg);
- return;
- }
- $changes = 0;
- $sortStart = 0;
- // locate the 'sort' value of the current first sorted item, to use as our starting point (in case sorting in a pagination other than 1)
- $sql = "SELECT sort FROM pages " .
- "WHERE parent_id={$parent->id} " .
- "AND id IN(" . implode(',', $this->ids) . ") " .
- ($this->isMoved ? "AND id!={$this->move_id} " : '') .
- "ORDER BY sort " .
- "LIMIT 1";
-
- $result = $this->db->query($sql);
- if($result->num_rows) list($sortStart) = $result->fetch_row();
- foreach($this->ids as $sort => $id) {
- $sort += $sortStart;
- $this->db->query("UPDATE pages SET sort=$sort WHERE id=$id AND parent_id=$parent->id AND sort!=$sort");
- if($this->db->affected_rows) $changes++;
- }
-
- if($changes) $this->message("Updated sort for $changes pages");
-
- }
-
- }