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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
|
<?php
/**
* A generic table class. Is is meant to be subclassed by particular
* table classes. A table is represented by a name, an array of
* primary keys, and an array mapping field names to values.
*
* 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 Jason Geiger
* @author Jeroen Roos
* @package Zoph
*/
use db\clause;
use db\db;
use db\delete;
use db\exception as databaseException;
use db\insert;
use db\param;
use db\query;
use db\select;
use db\update;
use conf\conf;
use template\block;
use template\template;
/**
* A generic table class. Is is meant to be subclassed by particular
* table classes. A table is represented by a name, an array of
* primary keys, and an array mapping field names to values.
*
* @author Jason Geiger
* @author Jeroen Roos
* @package Zoph
*/
abstract class zophTable {
/** @var string The name of the database table */
protected static $tableName;
/** @var array List of primary keys */
protected static $primaryKeys=array();
/** @var array Fields that are integers */
protected static $isInteger=array();
/** @var array Fields that are floats */
protected static $isFloat=array();
/** @var array Fields that may not be empty */
protected static $notNull=array();
/** @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;
/** @var array Contains the values of attributes that will be stored in the db */
public $fields=array();
/** @var array Contains the selectArray cache */
protected static $sacache;
/**
* Create new object
* @param int object id
*/
public function __construct($id=0) {
if ($id && !is_numeric($id)) { die("id for " . get_called_class() . " must be numeric"); }
$this->set(static::$primaryKeys[0],$id);
}
/**
* Returns the value of a field
* @param string name of field to get
* @return string value of the field
*/
public function get($name) {
log::msg("<b>GET</b> " . $name, log::DEBUG, log::VARS);
log::msg("<pre>" . var_export($this->fields, true) . "</pre>", log::MOREDEBUG, log::VARS);
if (isset($this->fields[$name])) {
return $this->fields[$name];
} else {
return "";
}
}
/**
* Get ID
* @return int id
* @throws zophException
*/
public function getId() {
if (sizeof(static::$primaryKeys)==1) {
return (int) $this->get(static::$primaryKeys[0]);
} else {
throw new zophException("This class (" . get_class($this) . ") " .
"requires a specific getId() implementation, please report a bug");
}
}
/**
* Sets the value of a field.
* @param string Name of the field to set
* @param string Value to set it to
*/
public function set($name, $value) {
$this->fields[$name] = $value;
}
/**
* Sets fields from the given array. Can be used to set vars
* directly from a GET or POST.
* @param array Variables to be set (like $_GET)
* @param string Prefix to cut off from beginning of key name
* @param string Suffic to cut off from end of key name
* @param bool Whether or not to process empty fields
*/
public function setFields(array $vars, $prefix = null, $suffix = null, $null=true) {
foreach ($vars as $key => $val) {
log::msg("<b>" . $key . "</b> = " . implode(",", (array) $val), log::DEBUG, log::VARS);
// ignore empty keys or values unless the field must be set.
if ($null) {
if ((!in_array($key, static::$notNull)) && (empty($key))) { continue; }
} else {
if ((!in_array($key, static::$notNull)) && (empty($key) || $val == "")) {
continue;
}
}
if ($prefix) {
if (strpos($key, $prefix) === 0) {
$key = substr($key, strlen($prefix));
} else {
continue;
}
} else if ($key[0] == '_') {
// a leading uderscore signals a non-database field
continue;
}
if ($suffix) {
$pos = strpos($key, $suffix);
if (($pos > 0) && (preg_match("/".$suffix."$/", $key))) {
$key = substr($key, 0, $pos);
} else {
continue;
}
}
// something in ALL CAPS is probably PHP or HTML related
if (strtoupper($key) == $key) { continue; }
try {
if (empty($val)) {
$this->fields[$key] = $val;
} else {
$this->fields[$key] = stripslashes($val);
}
} catch (TypeError $e) {
log::msg("<b>" . $key . "</b>: unexpected value", log::FATAL, log::VARS);
}
}
}
/**
* Checks to see if the given field is listed as a primary key.
* @param string Name of the field
* @return bool Whether or not field is listed
*/
public function isKey($name) {
return in_array($name, static::$primaryKeys);
}
/**
* Looks up a record.
* @return bool success or fail
* @todo Should return something more sensible
*/
public function lookup() {
$qry=new select(array(static::$tableName));
list($qry, $where) = $this->addWhereForKeys($qry);
if (!($where instanceof clause)) {
log::msg("No constraint found", log::NOTIFY, log::GENERAL);
return;
}
$qry->where($where);
return $this->lookupFromSQL($qry);
}
/**
* Looks up a record using supplied SQL query
* @param select SQL query to use
* @return bool success or fail
*/
public function lookupFromSQL(select $qry) {
try {
$result = db::query($qry);
} catch (PDOException $e) {
log::msg("Lookup failed", log::FATAL, log::DB);
}
$results=$result->fetchAll(PDO::FETCH_ASSOC);
$rows=count($results);
if ($rows == 1) {
$row=array_pop($results);
$this->fields = array();
$this->fields = array_merge($this->fields, $row);
return true;
}
return false;
}
/**
* Inserts a record.
* The default behavior is to ignore the
* primary key field(s) with the assumption that these will
* be generated by the db (auto_increment). Passing a non null
* parameter causes these fields to be manually inserted.
*/
public function insert() {
$qry=new insert(array(static::$tableName));
reset($this->fields);
foreach ($this->fields as $name => $value) {
if (!static::$keepKeys && $this->isKey($name)) {
continue;
}
if ($value === "now()") {
/* Lastnotify is normally set to "now()" and should not be escaped */
$qry->addSet($name, "now()");
} else {
$qry=$this->processValues($name, $value, $qry);
}
}
$id=$qry->execute();
if (count(static::$primaryKeys) == 1 && !static::$keepKeys) {
$this->fields[static::$primaryKeys[0]] = $id;
}
return $id;
}
/**
* Retrieving a the selectarray can take a long time in some cases
* pages that use it multiple times can cache it, so it only needs
* to be retrieved once per page request.
* @param array selectArray;
*/
public static function setSAcache(array $sa=null) {
if (!$sa) {
$sa=static::getSelectArray();
}
static::$sacache=$sa;
}
/**
* Deletes a record. If extra tables are specified, entries from
* those tables this match the keys are removed as well.
* @var $extra_tables array Tables to delete referencing objects from
*/
public function delete() {
// simulate overloading
if (func_num_args()>=1) {
$extra_tables = func_get_arg(0);
} else {
$extra_tables = null;
}
$qry=new delete(array(static::$tableName));
list($qry, $where) = $this->addWhereForKeys($qry);
if (!($where instanceof clause)) {
log::msg("No constraint found", log::NOTIFY, log::GENERAL);
return;
}
$qry->where($where);
try {
$qry->execute();
} catch (PDOException $e) {
log::msg("Delete failed", log::FATAL, log::DB);
}
if ($extra_tables) {
foreach ($extra_tables as $table) {
$qry=new delete(array($table));
list($qry, $where) = $this->addWhereForKeys($qry);
$qry->where($where);
try {
$qry->execute();
} catch (PDOException $e) {
log::msg("Delete from " . $table . " failed", log::FATAL, log::DB);
}
}
}
}
/**
* Updates a record.
*/
public function update() {
$qry=new update(array(static::$tableName));
list($qry, $where) = $this->addWhereForKeys($qry);
reset($this->fields);
foreach ($this->fields as $name => $value) {
if ($this->isKey($name)) {
continue;
}
if ($value === "now()") {
/* Lastnotify is normally set to "now()" and should not be escaped */
$qry->addSetFunction($name . "=now()");
} else {
$qry=$this->processValues($name, $value, $qry);
$qry->addSet($name, $name);
}
}
if (sizeof($qry->getParams()) === 0 || sizeof($qry->getSet()) === 0) {
return;
}
$qry->where($where);
try {
$qry->execute();
} catch (PDOException $e) {
log::msg("Update failed: " . $e->getMessage(), log::FATAL, log::DB);
}
}
protected function processValues($name, $value, $qry) {
if ((is_null($value) || $value==="") && in_array($name, static::$notNull)) {
throw new notNullValueIsNullDataException(e($name) . "may not be empty");
} else {
if (in_array($name, static::$isFloat) && empty($value)) {
$value = null;
}
if (in_array($name, static::$isInteger)) {
if (is_null($value) || $value==="") {
$qry->addParam(new param(":" . $name, null, PDO::PARAM_NULL));
} else {
$qry->addParam(new param(":" . $name, (int) $value, PDO::PARAM_INT));
}
} else {
if (is_null($value)) {
$qry->addParam(new param(":" . $name, null, PDO::PARAM_NULL));
} else {
$qry->addParam(new param(":" . $name, $value, PDO::PARAM_STR));
}
}
}
return $qry;
}
/**
* Creates an alphabetized array of field names and values.
* @return array Array for displaying object
*/
public function getDisplayArray() {
if (!$this->fields) { return; }
$keys = array_keys($this->fields);
sort($keys);
reset($keys);
$da=array();
foreach ($keys as $k) {
if ($this->isKey($k)) {
continue;
}
$title = ucfirst(str_replace("_", " ", $k));
$da[$title] = $this->fields[$k];
}
return $da;
}
/**
* Get an URL for the current object
* @return string URL
*/
public function getURL() {
return static::$url . $this->getId();
}
/**
* Turn the array from @see getDetails() into XML
* @param array Don't fetch details, but use the given array
*/
public function getDetailsXML(array $details = array()) {
if (empty($details)) {
$details=$this->getDetails();
}
if (isset($details["title"])) {
$display["title"]=$details["title"];
}
if (array_key_exists("count", $details) && $details["count"] > 0) {
// Remove timezone identifiers from time format
// Because in the current way Zoph works, they do not make sense
// It's not completely correct this way, because the data comes
// from the database where it is not yet timezone-corrected.
$timezone=array("e", "I", "O", "P", "T", "Z");
$timeformat=str_replace($timezone, "", conf::get("date.timeformat"));
$timeformat=trim(preg_replace("/\s\s+/", "", $timeformat));
$format=conf::get("date.format") . " " . $timeformat;
$oldest=new Time($details["oldest"]);
$disp_oldest=$oldest->format($format);
$newest=new Time($details["newest"]);
$disp_newest=$newest->format($format);
$first=new Time($details["first"]);
$disp_first=$first->format($format);
$last=new Time($details["last"]);
$disp_last=$last->format($format);
$display["count"]=$details["count"] . " " . translate("photos");
$display["taken"]=sprintf(translate("taken between %s and %s",false),
$disp_oldest, $disp_newest);
$display["modified"]=sprintf(translate("last changed from %s to %s",false),
$disp_first, $disp_last);
if (isset($details["lowest"]) &&
isset($details["highest"]) &&
isset($details["average"])) {
$display["rated"]=sprintf(
translate("rated between %s and %s and an average of %s",false),
$details["lowest"], $details["highest"], $details["average"]);
} else {
$display["rated"]=translate("no rating", false);
}
} else {
$display["count"]=translate("no photos", false);
}
if (isset($details["children"])) {
$count=$details["children"];
if ($count==0) {
$display["children"]="";
$no="no ";
} else {
$display["children"]=$count . " ";
$no="";
}
if ($this instanceof album) {
$text=translate($no . "sub-albums", false);
} else if ($this instanceof category) {
$text=translate($no . "sub-categories", false);
} else if ($this instanceof place) {
$text=translate($no . "sub-places", false);
} else {
$text=translate($no . "children", false);
}
$display["children"].=$text;
}
$xml = new DOMDocument('1.0','UTF-8');
$rootnode=$xml->createElement("details");
$request=$xml->createElement("request");
$class=$xml->createElement("class");
$class->appendChild($xml->createTextNode(get_class($this)));
$id=$xml->createElement("id");
$id->appendChild($xml->createTextNode($this->getId()));
$request->appendChild($class);
$request->appendChild($id);
$rootnode->appendChild($request);
$response=$xml->createElement("response");
foreach ($display as $subj => $data) {
$detail=$xml->createElement("detail");
$subject=$xml->createElement("subject");
$subject->appendChild($xml->createTextNode($subj));
$xmldata=$xml->createElement("data");
$xmldata->appendChild($xml->createTextNode($data));
$detail->appendChild($subject);
$detail->appendChild($xmldata);
$response->appendChild($detail);
}
$rootnode->appendChild($response);
$xml->appendChild($rootnode);
return $xml->saveXML();
}
/**
* Return object from Id
* @param int id
* @return mixed object
*/
public static function getFromId($id) {
if (!is_null($id) && $id!=0) {
$class=get_called_class();
$obj=new $class($id);
$obj->lookup();
return $obj;
}
}
/**
* Gets the total count of records in the table for the given class.
* @return int count
*/
public static function getCount() {
$qry=new select(array(static::$tableName));
$qry->addFunction(array("count" => "COUNT(*)"));
return $qry->getCount();
}
/**
* Generates an array for Top N albums/cat/..
* Executes a query and returns an array in which each record's
* link is mapped to its count (dirived by a group by clause).
* @param string query SQL query to use
* @return array Table of Top N most popular $class
*/
protected static function getTopNfromSQL($query) {
$pop_array=array();
$records = static::getRecordsFromQuery($query);
foreach ($records as $rec) {
$pop_array[] = array(
"id" => $rec->getId(),
"url" => $rec->getURL(),
"count" => $rec->get("count"),
"title" => $rec->getName()
);
}
return $pop_array;
}
/**
* Gets an array of the records for a table by doing a * "select *"
* and storing the results in classes of the given type.
* @param string Sort order
* @param array Constraints, conditions that the records must comply to
* @param array Conjunctions, and/or
* @param array Operators =, !=, >, <, >= or <=
* @return array records
* @todo This should be an internal (protected) function
*/
public static function getRecords($order = null, $constraints = null,
$conj = "AND", $ops = null) {
$qry = new select(static::$tableName);
if (is_array($constraints)) {
$qry->addWhereFromConstraints($constraints, $conj, $ops);
}
if ($order) {
$qry->addOrder($order);
}
return static::getRecordsFromQuery($qry);
}
/**
* Return all
* @return array Array of objects
*/
public static function getAll() {
return static::getRecords();
}
/**
* Extract a specific class from vars
* @param array vars (like $_GET or $_POST)
* @param string suffix to add to var key (e.g. _id)
* @return array vars for specific class.
*/
public static function getFromVars(array $vars, $suffix="") {
$class=get_called_class();
if ($class == "set\model") {
$class = "set";
}
$return=array();
$key="_" . $class . $suffix;
if (isset($vars[$key])) {
if (is_array($vars[$key])) {
foreach ($vars[$key] as $id=>$var) {
if (!empty($var)) {
$return[$id]=$var;
}
}
} else {
$return=(array) $vars[$key];
}
}
return $return;
}
/**
* Stores the results the the given query in an array of objects of
* this given type.
* @param select SQL query
*/
public static function getRecordsFromQuery(select $qry) {
$class=get_called_class();
try {
$result = db::query($qry);
} catch (PDOException $e) {
throw new databaseException("Unable to get records: " . $e->getMessage());
}
$objs=array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$obj = new $class;
$obj->setFields($row);
$objs[] = $obj;
}
return $objs;
}
/**
* Creates a constraint clause based on the given keys
*/
private function addWhereForKeys(query $query, clause $where = null) {
foreach (static::$primaryKeys as $key) {
$value = $this->fields[$key];
if (!$value) {
continue;
}
$clause = new clause($key . "=:" . $key);
$query->addParam(new param(":" . $key, $value, PDO::PARAM_INT));
if ($where instanceof clause) {
$where->addAnd($clause);
} else {
$where = $clause;
}
}
return array($query, $where);
}
/**
* Get coverphoto.
* @return photo coverphoto
*/
public function getCoverphoto() {
if ($this->get("coverphoto")) {
$coverphoto=new photo($this->get("coverphoto"));
if ($coverphoto->lookup()) {
return $coverphoto;
}
}
return false;
}
/**
* Lookup an autocover and create template to display
* @param how to select the autocover (olders, newest, first, last, random, highest [default])
* @return block thumb img
*/
public function displayAutoCover($autocover=null) {
$cover=$this->getAutoCover($autocover);
if ($cover instanceof photo) {
return $cover->getImageTag(THUMB_PREFIX);
}
}
/**
* Lookup cover and create template to display
* @return block thumb img
*/
public function displayCoverPhoto() {
$cover=$this->getCoverphoto();
if ($cover instanceof photo) {
return $cover->getImageTag(THUMB_PREFIX);
}
}
/**
* Get XML from a database table
* This is a wrapper around several objects which will call a method from
* those objects
* @param string Search string
*/
public static function getXML($search) {
$search=$search ? strtolower($search) : null;
$xml = new DOMDocument('1.0','UTF-8');
$rootnode=$xml->createElement(static::XMLROOT);
$newchild=$xml->createElement(static::XMLNODE);
$key=$xml->createElement("key");
$title=$xml->createElement("title");
$key->appendChild($xml->createTextNode("null"));
$title->appendChild($xml->createTextNode(" "));
$newchild->appendChild($key);
$newchild->appendChild($title);
$rootnode->appendChild($newchild);
return static::getXMLdata($search, $xml, $rootnode);
}
/**
* Create a dropdown menu for this object
* @param string name for this dropdown
* @param int|string id of value
*/
public static function createDropdown($name, $value=null) {
if (static::getAutocompPref()) {
return static::createAutoCompDropdown($name, $value);
} else {
if (isset(static::$sacache)) {
$sa=static::$sacache;
} else {
$sa=static::getSelectArray();
}
return template::createDropdown($name, $value, $sa);
}
}
public static function createAutoCompDropdown($name, $value=null) {
$id=preg_replace("/^_+/", "", $name);
$text="";
if ($value) {
$obj=static::getFromId($value);
$obj->lookup();
$text=$obj->getName();
}
$tpl=new block("autocomplete", array(
"id" => $id,
"name" => $name,
"value" => $value,
"text" => $text
));
return $tpl;
}
/**
* Get an array of id => name to build a non-hierarchical array
* this function does NOT check user permissions
* @return array
*/
public static function getSelectArray() {
$records=static::getRecords();
$selectArray=array(null => "");
foreach ($records as $record) {
$selectArray[(string) $record->getId()] = $record->getName();
}
return $selectArray;
}
}
?>
|