1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
<?php
/**
* Pagesets are a set of pages.
* You can associate an album, category, person or place with a pageset.
* This means that the first page in the set is shown when calling this album, etc.
* Through a pagination link, one can go to the other pages.
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Jeroen Roos
* @package Zoph
*/
use db\select;
use db\param;
use db\insert;
use db\update;
use db\clause;
use db\delete;
use template\block;
use template\template;
/**
* The pageset class groups a set of pages in a certain order
* @author Jeroen Roos
* @package Zoph
*/
class pageset extends zophTable {
/** @var string The name of the database table */
protected static $tableName="pageset";
/** @var array List of primary keys */
protected static $primaryKeys=array("pageset_id");
/** @var array Fields that may not be empty */
protected static $notNull=array("title");
/** @var bool keep keys with insert. In most cases the keys are set
* by the db with auto_increment */
protected static $keepKeys = false;
/** @var string URL for this class */
protected static $url="pageset.php?pageset_id=";
/**
* Create pageset
* @param int If existing pageset is to be pulled from the db, the id has to be given
* @return pageset
*/
public function __construct($id = 0) {
parent::__construct($id);
$this->set("date", "now()");
}
/**
* Update existing pageset in db
*/
public function update() {
$this->set("timestamp", "now()");
parent::update();
$this->lookup();
}
/**
* Delete pageset from db
* Also delete page-pageset relations
*/
public function delete() {
if (!$this->get("pageset_id")) {
return;
}
parent::delete(array("pages_pageset"));
}
/**
* Get an array of information to be displayed for this pageset
*/
public function getDisplayArray() {
return array(
translate("title") => $this->get("title"),
translate("date") => $this->get("date"),
translate("updated") => $this->get("timestamp"),
translate("created by", false) => $this->getUser()->getLink(),
translate("show original page") => translate($this->get("show_orig"), 0),
translate("position of original") => translate($this->get("orig_pos"), 0)
);
}
/**
* Get a dropdown to select what to do with the original (zoph default) page to
* be displayed
*/
public function getOriginalDropdown() {
return template::createDropdown("show_orig", $this->get("show_orig"),
array(
"never" => translate("Never", 0),
"first" => translate("On first page", 0),
"last" => translate("On last page", 0),
"all" => translate("On all pages", 0)
)
);
}
/**
* Get the pages in this pageset
* @param int Specific page to get instead of all
*/
public function getPages($pagenum=null) {
$qry=new select(array("pps" => "pages_pageset"));
$qry->addFields(array("page_id"));
$qry->where(new clause("pageset_id=:pagesetid"));
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
$qry->addOrder("page_order");
if ($pagenum) {
$qry->addLimit(1, (int) $pagenum);
}
return page::getRecordsFromQuery($qry);
}
/**
* Get the number of pages in this pageset
*/
public function getPageCount() {
$qry=new select(array("pps" => "pages_pageset"));
$qry->addFunction(array("count" => "COUNT(page_id)"));
$qry->where(new clause("pageset_id=:pagesetid"));
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
return $qry->getCount();
}
public function getName() {
return $this->get("title");
}
/**
* Add a page to this set
* @param page Page to add
* @todo If the page already exists in this pageset, it fails silently
* because, at this moment a page cannot be more than once in a pageset
* Someday, this should either give a nice error or this limitation
* should be removed.
*/
public function addPage(page $page) {
if (!$page->getOrder($this)) {
$qry=new insert(array("pages_pageset"));
$qry->addParam(new param(":pageset_id", $this->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":page_id", $page->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":page_order", $this->getMaxOrder() + 1, PDO::PARAM_INT));
$qry->execute();
}
}
/**
* Remove a page from this pageset
* @param page Page to remove
*/
public function removePage(page $page) {
$qry=new delete(array("pages_pageset"));
$where=new clause("pageset_id=:pagesetid");
$where->addAnd(new clause("page_id=:pageid"));
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":pageid", $page->getId(), PDO::PARAM_INT));
$qry->where($where);
$qry->execute();
}
/**
* Move a page up in the order list
* @param page Page to move up
*/
public function moveUp(page $page) {
$order=$page->getOrder($this);
if ($order>=2) {
$currentOrder=new param(":curorder", $order, PDO::PARAM_INT);
$newOrder=new param(":neworder", $this->getPrevOrder($order), PDO::PARAM_INT);
$pageId=new param(":pageid", $page->getId(), PDO::PARAM_INT);
$this->move($currentOrder, $newOrder, $pageId);
}
}
/**
* Move a page down in the order list
* @param page Page to move down
*/
public function moveDown(page $page) {
$order=$page->getOrder($this);
$max=$this->getMaxOrder();
if ($order!=0 && $order<$max) {
$currentOrder=new param(":curorder", $order, PDO::PARAM_INT);
$newOrder=new param(":neworder", $this->getNextOrder($order), PDO::PARAM_INT);
$pageId=new param(":pageid", $page->getId(), PDO::PARAM_INT);
$this->move($currentOrder, $newOrder, $pageId);
}
}
/**
* Move a page up or down in a pageset
* First, it changes the page that has the new order for the page we want to move
* to the old order for that page.
* For example, if we have a pageset with 2 pages, page 1 and 2, in that order:
* pageId = 1, order = 1
* pageId = 2, order = 2
* [step 1]
* We are going to move page 2 up, then after the first action, it will look like this:
* pageId = 1, order = 1
* pageId = 2, order = 1
* [step 2]
* Then finally, we update the order for the page we are actually moving:
* pageId = 1, order = 2
* pageId = 2, order = 1
* @param param currentOrder: a database parameter for the current order
* @param param newOder: a database parameter for the new order
* @param param pageId: a database parameter for the pageId.
*/
private function move(param $currentOrder, param $newOrder, param $pageId) {
$pagesetId=new param(":pagesetid", $this->getId(), PDO::PARAM_INT);
// [step 1]
$qry=new update(array("pages_pageset"));
$qry->addSet("page_order", "curorder");
$where=new clause("page_order=:neworder");
$where->addAnd(new clause("pageset_id=:pagesetid"));
$qry->where($where);
$qry->addParams(array($currentOrder, $newOrder, $pagesetId));
$qry->execute();
// [step 2]
$qry=new update(array("pages_pageset"));
$qry->addSet("page_order", "neworder");
$where=new clause("page_id=:pageid");
$where->addAnd(new clause("pageset_id=:pagesetid"));
$qry->where($where);
$qry->addParams(array($newOrder, $pageId, $pagesetId));
$qry->execute();
}
/**
* Get the highest used page_order value for this pageset
* @return int maximum page_order
*/
private function getMaxOrder() {
$qry=new select(array("pps" => "pages_pageset"));
$qry->addFunction(array("max_order" => "MAX(page_order)"));
$qry->where(new clause("pageset_id=:pagesetid"));
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
$stmt=$qry->execute();
return intval($stmt->fetchColumn());
}
/**
* Get Next order
* If pages have been deleted, the page_order field may no longer
* be nicely numbered 1, 2, 3, etc. but there may be holes in the list
* so this function and getPrevOrder() determine the next or previous
* value of page_order.
* @param int Get the next order after...
*/
private function getNextOrder($order) {
$qry=new select(array("pps" => "pages_pageset"));
$qry->addFunction(array("next_order" => "MIN(page_order)"));
$where=new clause("pageset_id=:pagesetid");
$where->addAnd(new clause("page_order>:order"));
$qry->where($where);
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":order", $order, PDO::PARAM_INT));
$stmt=$qry->execute();
return intval($stmt->fetchColumn());
}
/**
* Get previous order
* If pages have been deleted, the page_order field may no longer
* be nicely numbered 1, 2, 3, etc. but there may be holes in the list
* so this function and getiNextOrder() determine the next or previous
* value of page_order.
* @param int Get the previous order before...
*/
private function getPrevOrder($order) {
$qry=new select(array("pps" => "pages_pageset"));
$qry->addFunction(array("prev_order" => "MAX(page_order)"));
$where=new clause("pageset_id=:pagesetid");
$where->addAnd(new clause("page_order<:order"));
$qry->where($where);
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":order", $order, PDO::PARAM_INT));
$stmt=$qry->execute();
return intval($stmt->fetchColumn());
}
/**
* Get the user who created this pageset
* @return user the user
*/
public function getUser() {
$user = new user($this->get("user"));
$user->lookup();
return $user;
}
/**
* Get table of pagesets
* @param array pagesets to put in the table (default: all)
* @return block template block with all pagesets
*/
public static function getTable(array $pagesets=null) {
if (!$pagesets) {
$pagesets=pageset::getAll();
}
$lpagesets=array();
foreach ($pagesets as $pageset) {
$pageset->lookup();
$lpagesets[]=$pageset;
}
return new block("pagesets", array(
"pagesets" => $lpagesets
));
}
}
|