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
|
<?php
/**
* Page class
* A page is plaintext or zophCode that can be used to personalize parts
* of the Zoph interface
*
* 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\clause;
use template\block;
/**
* Page class
* A page is plaintext or zophCode that can be used to personalize parts
* of the Zoph interface
*
* @author Jeroen Roos
* @package Zoph
*/
class page extends zophTable {
/** @var string The name of the database table */
protected static $tableName="pages";
/** @var array List of primary keys */
protected static $primaryKeys=array("page_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="page?page_id=";
/**
* Return the page in a string
* @return string page
*/
public function __toString() {
return (string) $this->display();
}
/**
* Insert a new page into the db
*/
public function insert() {
$this->set("date", "now()");
parent::insert();
$this->lookup();
}
/**
* Update an existing page in the db
*/
public function update() {
$this->set("timestamp", "now()");
parent::update();
$this->lookup();
}
/**
* Delete a page from the db
*/
public function delete() {
if (!$this->getId()) {
return;
}
parent::delete(array("pages_pageset"));
}
public function getName() {
return $this->get("title");
}
/**
* Return an array of fields to display
* @return array array of fields
*/
public function getDisplayArray() {
$tpl = new block("pagePreview", array(
"text" => new zophCode\parser($this->get("text"))
));
return array(
translate("title") => e($this->get("title")),
translate("date") => e($this->get("date")),
translate("updated") => e($this->get("timestamp")),
translate("text") => $tpl
);
}
/**
* Parse Zophcode
* @return string parsed code
*/
public function display() {
return new zophCode\parser($this->get("text"));
}
/**
* Get the position of a page in a pageset
* @param pageset The pageset to look in
*/
public function getOrder(pageset $pageset) {
$qry=new select(array("pgps" => "pages_pageset"));
$qry->addFields(array("page_order"));
$where=new clause("pageset_id=:psid");
$where->addAnd(new clause("page_id=:pageid"));
$qry->addParam(new param(":psid", $pageset->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":pageid", $this->getId(), PDO::PARAM_INT));
$qry->where($where);
$qry->addLimit(1);
$stmt=$qry->execute();
if ($stmt->rowCount()) {
return intval($stmt->fetchColumn());
} else {
return false;
}
}
/**
* Get the pagesets this page is in
*/
public function getPagesets() {
$qry=new select(array("pgps" => "pages_pageset"));
$qry->addFields(array("pageset_id"));
$where=new clause("page_id=:pageid");
$qry->addParam(new param(":pageid", $this->getId(), PDO::PARAM_INT));
$qry->where($where);
return pageset::getRecordsFromQuery($qry);
}
/**
* Get a table of pages
* @param array array of pages to show
* @param pageset Pageset to display
* @return block template to display
*/
public static function getTable(array $pages = null, pageset $pageset=null) {
if (is_null($pages)) {
$pages=page::getAll();
}
$lpages=array();
foreach ($pages as $page) {
$page->lookup();
$lpages[]=$page;
}
return new block("pages", array(
"pages" => $lpages,
"pageset" => $pageset
));
}
}
|