1<?php 2 /* libraries/alphabetize.php 3 * 4 * Copyright (C) by Hugo Leisink <hugo@leisink.net> 5 * This file is part of the Banshee PHP framework 6 * http://www.banshee-php.org/ 7 */ 8 9 class alphabetize { 10 private $output = null; 11 private $name = null; 12 private $char = "0"; 13 14 /* Constructor 15 * 16 * INPUT: object output, string name 17 * OUTPUT: - 18 * ERROR: - 19 */ 20 public function __construct($output, $name) { 21 $this->output = $output; 22 $this->name = $name; 23 24 /* Initialize session storage 25 */ 26 if (is_array($_SESSION["alphabetize"]) == false) { 27 $_SESSION["alphabetize"] = array(); 28 } 29 if (isset($_SESSION["alphabetize"][$name]) == false) { 30 $_SESSION["alphabetize"][$name] = $this->char; 31 } 32 33 /* Set starting character 34 */ 35 $this->char = &$_SESSION["alphabetize"][$name]; 36 if (isset($_GET["char"])) { 37 $this->char = $this->make_valid_char($_GET["char"]); 38 } 39 40 $this->output->add_css("banshee/alphabetize.css"); 41 } 42 43 /* Magic method get 44 * 45 * INPUT: string key 46 * OUTPUT: mixed value 47 * ERROR: null 48 */ 49 public function __get($key) { 50 switch ($key) { 51 case "char": return $this->char; 52 } 53 54 return null; 55 } 56 57 /* Makes the start charater a valid one 58 * 59 * INPUT: string starting character 60 * OUTPUT: string valid starting character 61 * ERROR: - 62 */ 63 private function make_valid_char($char) { 64 if ($char == "") { 65 return "0"; 66 } 67 68 $char = strtolower(substr($char, 0, 1)); 69 70 if ((ord($char) < ord("a")) || (ord($char) > ord("z"))) { 71 return "0"; 72 } 73 74 return $char; 75 } 76 77 /* Set active page to "0" 78 * 79 * INPUT: - 80 * OUTPUT: - 81 * ERROR: - 82 */ 83 public function reset() { 84 $this->char = "0"; 85 } 86 87 /* Generate XML for the browse links 88 * 89 * INPUT: - 90 * OUTPUT: boolean xml generated 91 * ERROR: - 92 */ 93 public function show_browse_links() { 94 $this->output->open_tag("alphabetize", array("char" => $this->char)); 95 96 $this->output->add_tag("char", "#", array("link" => "0")); 97 for ($c = ord("a"); $c <= ord("z"); $c++) { 98 $this->output->add_tag("char", chr($c), array("link" => chr($c))); 99 } 100 101 $this->output->close_tag(); 102 } 103 104 /* Returns content of table for current start character 105 * 106 * INPUT: object database, string table name, string column name[, string column name for ordering] 107 * OUTPUT: array table content 108 * ERROR: false 109 */ 110 public function get_items($db, $table, $column, $order = null) { 111 $query = "select * from %S"; 112 $args = array($table); 113 114 if ($this->char == "0") { 115 $query .= " where ord(lower(substr(%S, 1, 1)))<ord(%s) or ord(lower(substr(%S, 1, 1)))>ord(%s)"; 116 array_push($args, $column, "a", $column, "z"); 117 } else { 118 $query .= " where %S like %s"; 119 array_push($args, $column, $this->char."%"); 120 } 121 122 if ($order != null) { 123 $query .= " order by %S"; 124 array_push($args, $order); 125 } 126 127 return $db->execute($query, $args); 128 } 129 } 130?> 131