| 1 |
Index: phptagengine.class.inc.php |
|---|
| 2 |
=================================================================== |
|---|
| 3 |
--- phptagengine.class.inc.php (revision 21) |
|---|
| 4 |
+++ phptagengine.class.inc.php (working copy) |
|---|
| 5 |
@@ -1,1220 +1,1286 @@ |
|---|
| 6 |
-<?php |
|---|
| 7 |
- |
|---|
| 8 |
-// Copyright (c) 2006-2007 Alex King. All rights reserved. |
|---|
| 9 |
-// http://alexking.org/projects/php-tag-engine |
|---|
| 10 |
-// |
|---|
| 11 |
-// Released under the LGPL license |
|---|
| 12 |
-// http://www.opensource.org/licenses/lgpl-license.php |
|---|
| 13 |
-// |
|---|
| 14 |
-// ********************************************************************** |
|---|
| 15 |
-// This program is distributed in the hope that it will be useful, but |
|---|
| 16 |
-// WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 |
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 18 |
-// ********************************************************************** |
|---|
| 19 |
- |
|---|
| 20 |
-/** |
|---|
| 21 |
- * This file contains the PHP Tag Engine class definition. |
|---|
| 22 |
- * |
|---|
| 23 |
- * @package phptagengine |
|---|
| 24 |
- */ |
|---|
| 25 |
- |
|---|
| 26 |
-if (__FILE__ == basename($_SERVER['SCRIPT_NAME'])) { die(); } |
|---|
| 27 |
- |
|---|
| 28 |
-/** |
|---|
| 29 |
- * The phptagengine class, all the good stuff happens here. |
|---|
| 30 |
- * |
|---|
| 31 |
- * @package phptagengine |
|---|
| 32 |
- */ |
|---|
| 33 |
-class phptagengine { |
|---|
| 34 |
- /** |
|---|
| 35 |
- * set to global $ADOdb instance |
|---|
| 36 |
- * @var mixed |
|---|
| 37 |
- */ |
|---|
| 38 |
- var $db; |
|---|
| 39 |
- /** |
|---|
| 40 |
- * Column name escape string for the database being used. Checks for: mysql, postgres7, mssql |
|---|
| 41 |
- * |
|---|
| 42 |
- * @var string |
|---|
| 43 |
- */ |
|---|
| 44 |
- var $db_col_escape_char; |
|---|
| 45 |
- /** |
|---|
| 46 |
- * Name of the tags table in the database |
|---|
| 47 |
- * |
|---|
| 48 |
- * @var string |
|---|
| 49 |
- */ |
|---|
| 50 |
- var $table_tags; |
|---|
| 51 |
- /** |
|---|
| 52 |
- * Name of the tag names table in the database |
|---|
| 53 |
- * |
|---|
| 54 |
- * @var string |
|---|
| 55 |
- */ |
|---|
| 56 |
- var $table_tag_names; |
|---|
| 57 |
- /** |
|---|
| 58 |
- * Name of the users table in the database |
|---|
| 59 |
- * |
|---|
| 60 |
- * @var string |
|---|
| 61 |
- */ |
|---|
| 62 |
- var $table_users; |
|---|
| 63 |
- /** |
|---|
| 64 |
- * The display name column (normally name or username) in the users table in the database |
|---|
| 65 |
- * |
|---|
| 66 |
- * @var string |
|---|
| 67 |
- */ |
|---|
| 68 |
- var $table_users_name; |
|---|
| 69 |
- /** |
|---|
| 70 |
- * URL to the PHP Tag Engine folder, include the trailing slash |
|---|
| 71 |
- * |
|---|
| 72 |
- * @var string |
|---|
| 73 |
- */ |
|---|
| 74 |
- var $base_url; |
|---|
| 75 |
- /** |
|---|
| 76 |
- * URL of the file handling AJAX requests |
|---|
| 77 |
- * |
|---|
| 78 |
- * @var string |
|---|
| 79 |
- */ |
|---|
| 80 |
- var $ajax_handler; |
|---|
| 81 |
- /** |
|---|
| 82 |
- * Name of language file to include - languages/(language).inc.php |
|---|
| 83 |
- * |
|---|
| 84 |
- * @var string |
|---|
| 85 |
- */ |
|---|
| 86 |
- var $language; |
|---|
| 87 |
- /** |
|---|
| 88 |
- * Character set |
|---|
| 89 |
- * |
|---|
| 90 |
- * @var string |
|---|
| 91 |
- */ |
|---|
| 92 |
- var $charset; |
|---|
| 93 |
- /** |
|---|
| 94 |
- * Array of strings for localization |
|---|
| 95 |
- * |
|---|
| 96 |
- * @var array |
|---|
| 97 |
- */ |
|---|
| 98 |
- var $strings; |
|---|
| 99 |
- /** |
|---|
| 100 |
- * Value to be used if no type is passed in, useful if you only have one |
|---|
| 101 |
- * type of object you are tagging and just need a default value |
|---|
| 102 |
- * |
|---|
| 103 |
- * @var string |
|---|
| 104 |
- */ |
|---|
| 105 |
- var $default_type; |
|---|
| 106 |
- /** |
|---|
| 107 |
- * Value to be used if no user is passed in, useful if you are not tracking |
|---|
| 108 |
- * tags by user and just need a default value |
|---|
| 109 |
- * |
|---|
| 110 |
- * @var string |
|---|
| 111 |
- */ |
|---|
| 112 |
- var $default_user; |
|---|
| 113 |
- /** |
|---|
| 114 |
- * URL for browsing items by tag, includes token replacement for tag, type |
|---|
| 115 |
- * |
|---|
| 116 |
- * example: http://example.com/index.php?view=tag&tag=<tag>&type=<type> |
|---|
| 117 |
- * |
|---|
| 118 |
- * @var string |
|---|
| 119 |
- */ |
|---|
| 120 |
- var $tag_browse_url; |
|---|
| 121 |
- /** |
|---|
| 122 |
- * Show an X next to each tag to allow removal of that tag |
|---|
| 123 |
- * |
|---|
| 124 |
- * @var boolean |
|---|
| 125 |
- */ |
|---|
| 126 |
- var $show_remove_links; |
|---|
| 127 |
- /** |
|---|
| 128 |
- * Show text or image? |
|---|
| 129 |
- * |
|---|
| 130 |
- * example: 'text' - uses @link phptagengine::$strings |
|---|
| 131 |
- * example: 'image' |
|---|
| 132 |
- * |
|---|
| 133 |
- * @var string |
|---|
| 134 |
- */ |
|---|
| 135 |
- var $edit_button_display; |
|---|
| 136 |
- /** |
|---|
| 137 |
- * URL of the image for the edit button |
|---|
| 138 |
- * |
|---|
| 139 |
- * @var string |
|---|
| 140 |
- */ |
|---|
| 141 |
- var $edit_button_image_url; |
|---|
| 142 |
- /** |
|---|
| 143 |
- * Show text or image? |
|---|
| 144 |
- * |
|---|
| 145 |
- * example: 'text' - uses @link phptagengine::$strings |
|---|
| 146 |
- * example: 'image' |
|---|
| 147 |
- * |
|---|
| 148 |
- * @var string |
|---|
| 149 |
- */ |
|---|
| 150 |
- var $delete_button_display; |
|---|
| 151 |
- /** |
|---|
| 152 |
- * URL of the image for the delete button |
|---|
| 153 |
- * |
|---|
| 154 |
- * @var string |
|---|
| 155 |
- */ |
|---|
| 156 |
- var $delete_button_image_url; |
|---|
| 157 |
- /** |
|---|
| 158 |
- * Stores arrays of tags for items, use as a cache to reduce queries |
|---|
| 159 |
- * |
|---|
| 160 |
- * @var array |
|---|
| 161 |
- */ |
|---|
| 162 |
- var $item_tags_cache; |
|---|
| 163 |
- /** |
|---|
| 164 |
- * Enable Yahoo! Auto-Complete |
|---|
| 165 |
- * |
|---|
| 166 |
- * @var boolean |
|---|
| 167 |
- */ |
|---|
| 168 |
- var $yac; |
|---|
| 169 |
- /** |
|---|
| 170 |
- * List of Yahoo! UI Pattern files to be included, in case Yahoo! UI Patterns are already in use elsewhere and we don't want to duplicate their inclusion. The array should contain filenames in the 'yui' directory. |
|---|
| 171 |
- * |
|---|
| 172 |
- * Note: order matters in this list. |
|---|
| 173 |
- * |
|---|
| 174 |
- * @var array |
|---|
| 175 |
- */ |
|---|
| 176 |
- var $yac_files; |
|---|
| 177 |
- /** |
|---|
| 178 |
- * PHP Tag Engine Version |
|---|
| 179 |
- * |
|---|
| 180 |
- * @var string |
|---|
| 181 |
- */ |
|---|
| 182 |
- var $version; |
|---|
| 183 |
- |
|---|
| 184 |
- /** |
|---|
| 185 |
- * Initializes the class |
|---|
| 186 |
- * |
|---|
| 187 |
- * @return phptagengine |
|---|
| 188 |
- */ |
|---|
| 189 |
- function phptagengine() { |
|---|
| 190 |
- $this->base_url = 'http://example.com/'; |
|---|
| 191 |
- $this->ajax_handler = 'http://example.com/ajax.php'; |
|---|
| 192 |
- $this->language = 'english'; |
|---|
| 193 |
- $this->charset = 'UTF-8'; |
|---|
| 194 |
- $this->db_type = 'mysql'; |
|---|
| 195 |
- $this->strings = array(); |
|---|
| 196 |
- $this->tag_browse_url = 'http://example.com/index.php?view=tags&tag=<tag>&type=<type>'; |
|---|
| 197 |
- $this->default_type = ''; |
|---|
| 198 |
- $this->default_user = 1; |
|---|
| 199 |
- $this->edit_button_display = 'text'; |
|---|
| 200 |
- $this->delete_button_display = 'text'; |
|---|
| 201 |
- $this->yac = true; |
|---|
| 202 |
- $this->yac_files = array( |
|---|
| 203 |
- 'yahoo.js' |
|---|
| 204 |
- ,'dom.js' |
|---|
| 205 |
- ,'event.js' |
|---|
| 206 |
- ,'autocomplete.js' |
|---|
| 207 |
- ); |
|---|
| 208 |
- $this->version = '1.0'; |
|---|
| 209 |
- } |
|---|
| 210 |
- |
|---|
| 211 |
- /** |
|---|
| 212 |
- * Sets the character used for escaping column names for the database type in use |
|---|
| 213 |
- */ |
|---|
| 214 |
- function set_db_col_escape_char() { |
|---|
| 215 |
- switch ($this->db->databaseType) { |
|---|
| 216 |
- case 'mysql': |
|---|
| 217 |
- $this->db_col_escape_char = '`'; |
|---|
| 218 |
- break; |
|---|
| 219 |
- case 'postgres7': |
|---|
| 220 |
- case 'mssql': |
|---|
| 221 |
- $this->db_col_escape_char = '"'; |
|---|
| 222 |
- break; |
|---|
| 223 |
- } |
|---|
| 224 |
- } |
|---|
| 225 |
- |
|---|
| 226 |
- /** |
|---|
| 227 |
- * Sets the type or user property to the default value if the value is |
|---|
| 228 |
- * null, could be extended in the future |
|---|
| 229 |
- * @param string $prop expected 'type' or 'user' |
|---|
| 230 |
- * @param mixed $value if this is null, we set the default |
|---|
| 231 |
- */ |
|---|
| 232 |
- function default_value($prop, $value) { |
|---|
| 233 |
- if ($value == null && in_array($prop, array('type', 'user'))) { |
|---|
| 234 |
- eval('$value = $this->default_'.$prop.';'); |
|---|
| 235 |
- } |
|---|
| 236 |
- return $value; |
|---|
| 237 |
- } |
|---|
| 238 |
- |
|---|
| 239 |
- /** |
|---|
| 240 |
- * Set a tag to lowercase and remove spaces, could be extended in the future |
|---|
| 241 |
- * @param mixed $value the value to be normalized |
|---|
| 242 |
- * @param string $type the type of value, used for the switch statement |
|---|
| 243 |
- * @return mixed |
|---|
| 244 |
- */ |
|---|
| 245 |
- function normalize($value, $type = 'tag') { |
|---|
| 246 |
- switch ($type) { |
|---|
| 247 |
- case 'tag': |
|---|
| 248 |
- $value = preg_replace('|[^a-z0-9_.\-@#$%*!&]|i', '', strtolower($value)); |
|---|
| 249 |
- break; |
|---|
| 250 |
- } |
|---|
| 251 |
- return $value; |
|---|
| 252 |
- } |
|---|
| 253 |
- |
|---|
| 254 |
- /** |
|---|
| 255 |
- * Does a tag already exist |
|---|
| 256 |
- * |
|---|
| 257 |
- * @uses phptagengine::get_tag_id() |
|---|
| 258 |
- * |
|---|
| 259 |
- * @param string $tag |
|---|
| 260 |
- * @return boolean |
|---|
| 261 |
- */ |
|---|
| 262 |
- function tag_exists($tag) { |
|---|
| 263 |
- if ($this->get_tag_id($tag) != false) { |
|---|
| 264 |
- return true; |
|---|
| 265 |
- } |
|---|
| 266 |
- else { |
|---|
| 267 |
- return false; |
|---|
| 268 |
- } |
|---|
| 269 |
- } |
|---|
| 270 |
- |
|---|
| 271 |
- /** |
|---|
| 272 |
- * Creates a new tag (noramlized) in the database, returns the ID of the |
|---|
| 273 |
- * created tag or false |
|---|
| 274 |
- * |
|---|
| 275 |
- * @uses phptagengine::normalize() to normalize the $tag |
|---|
| 276 |
- * @uses phptagengine::tag_exists() to see if the tag needs creating |
|---|
| 277 |
- * |
|---|
| 278 |
- * @param string $tag |
|---|
| 279 |
- * @return mixed |
|---|
| 280 |
- */ |
|---|
| 281 |
- function create_tag($tag = null) { |
|---|
| 282 |
- if ($tag == null) { |
|---|
| 283 |
- return false; |
|---|
| 284 |
- } |
|---|
| 285 |
- $tag = $this->normalize($tag); |
|---|
| 286 |
- if (strlen($tag) < 1) { |
|---|
| 287 |
- return false; |
|---|
| 288 |
- } |
|---|
| 289 |
- $test = $this->get_tag_id($tag); |
|---|
| 290 |
- if ($test != false) { |
|---|
| 291 |
- return $test; |
|---|
| 292 |
- } |
|---|
| 293 |
- $result = $this->db->Execute(" |
|---|
| 294 |
- INSERT |
|---|
| 295 |
- INTO $this->table_tag_names |
|---|
| 296 |
- ( ".$this->db_col_escape_char."name".$this->db_col_escape_char." |
|---|
| 297 |
- ) |
|---|
| 298 |
- VALUES |
|---|
| 299 |
- ( ".$this->db->qstr($tag)." |
|---|
| 300 |
- ) |
|---|
| 301 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 302 |
- |
|---|
| 303 |
- if ($result) { |
|---|
| 304 |
- $id = false; |
|---|
| 305 |
- if (strstr($this->db->databaseType, 'postgres')) { |
|---|
| 306 |
- $id = $this->db->GetOne(" |
|---|
| 307 |
- SELECT CURRVAL('".$this->table_tag_names."_id_seq') as id |
|---|
| 308 |
- ") or die($this->db->ErrorMsg()); |
|---|
| 309 |
- } |
|---|
| 310 |
- else { |
|---|
| 311 |
- $id = $this->db->Insert_ID(); |
|---|
| 312 |
- } |
|---|
| 313 |
- return $id; |
|---|
| 314 |
- } |
|---|
| 315 |
- |
|---|
| 316 |
- return false; |
|---|
| 317 |
- } |
|---|
| 318 |
- |
|---|
| 319 |
- /** |
|---|
| 320 |
- * Adds a tag to an item, creates the tag if needed |
|---|
| 321 |
- * |
|---|
| 322 |
- * @uses phptagengine::default_value() to set a default value for $type and $user if needed |
|---|
| 323 |
- * @uses phptagengine::create_tag() to create a new tag or get the id of the existing tag |
|---|
| 324 |
- * |
|---|
| 325 |
- * @param string $user user creating the tag |
|---|
| 326 |
- * @param string $item item being tagged |
|---|
| 327 |
- * @param string $tag tag being used |
|---|
| 328 |
- * @param string $type type of item being tagged |
|---|
| 329 |
- * @return boolean |
|---|
| 330 |
- */ |
|---|
| 331 |
- function add_tag($user = null, $item = null, $tag = null, $type = null) { |
|---|
| 332 |
- if ($item == null || $tag == null) { |
|---|
| 333 |
- return false; |
|---|
| 334 |
- } |
|---|
| 335 |
- $type = $this->default_value('type', $type); |
|---|
| 336 |
- $user = $this->default_value('user', $user); |
|---|
| 337 |
- $tag_id = $this->create_tag($tag); |
|---|
| 338 |
- if ($tag_id == false) { |
|---|
| 339 |
- return false; |
|---|
| 340 |
- } |
|---|
| 341 |
- if ($this->item_tag_exists($user, $item, $tag, $type)) { |
|---|
| 342 |
- return true; |
|---|
| 343 |
- } |
|---|
| 344 |
- else { |
|---|
| 345 |
- $this->set_db_col_escape_char(); |
|---|
| 346 |
- $result = $this->db->Execute(" |
|---|
| 347 |
- INSERT |
|---|
| 348 |
- INTO $this->table_tags |
|---|
| 349 |
- ( ".$this->db_col_escape_char."user".$this->db_col_escape_char." |
|---|
| 350 |
- , ".$this->db_col_escape_char."item".$this->db_col_escape_char." |
|---|
| 351 |
- , ".$this->db_col_escape_char."type".$this->db_col_escape_char." |
|---|
| 352 |
- , ".$this->db_col_escape_char."tag".$this->db_col_escape_char." |
|---|
| 353 |
- , ".$this->db_col_escape_char."date".$this->db_col_escape_char." |
|---|
| 354 |
- ) |
|---|
| 355 |
- VALUES |
|---|
| 356 |
- ( ".$this->db->qstr($user)." |
|---|
| 357 |
- , ".$this->db->qstr($item)." |
|---|
| 358 |
- , ".$this->db->qstr($type)." |
|---|
| 359 |
- , ".$this->db->qstr($tag_id)." |
|---|
| 360 |
- , ".$this->db->DBDate(date("Y-m-d H:i:s"))." |
|---|
| 361 |
- ) |
|---|
| 362 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 363 |
- if ($result) { |
|---|
| 364 |
- return true; |
|---|
| 365 |
- } |
|---|
| 366 |
- } |
|---|
| 367 |
- return false; |
|---|
| 368 |
- } |
|---|
| 369 |
- |
|---|
| 370 |
- /** |
|---|
| 371 |
- * Save item tags |
|---|
| 372 |
- * |
|---|
| 373 |
- * @param string $item |
|---|
| 374 |
- * @param string $tags |
|---|
| 375 |
- * @param string $type |
|---|
| 376 |
- * @param string $user |
|---|
| 377 |
- * |
|---|
| 378 |
- * @return array key success is 'y/n', key tags is an arra of the item's tags |
|---|
| 379 |
- */ |
|---|
| 380 |
- function save_tags($item, $tags, $type = null, $user = null) { |
|---|
| 381 |
- $type = $this->default_value('type', $type); |
|---|
| 382 |
- $user = $this->default_value('user', $user); |
|---|
| 383 |
- $old_tags = $this->get_tags($item, $type, $user); |
|---|
| 384 |
- $new_tags = array(); |
|---|
| 385 |
- $success = 'y'; |
|---|
| 386 |
- if ($tags != null && $tags != '') { |
|---|
| 387 |
- $tags = explode(' ', $tags); |
|---|
| 388 |
- $tags = array_unique($tags); |
|---|
| 389 |
- natcasesort($tags); |
|---|
| 390 |
- reset($tags); |
|---|
| 391 |
- if (count($tags) > 0) { |
|---|
| 392 |
- foreach ($tags as $tag) { |
|---|
| 393 |
- if ($tag != '') { |
|---|
| 394 |
- if ($this->add_tag($user, $item, $tag, $type)) { |
|---|
| 395 |
- $new_tags[] = $this->normalize($tag); |
|---|
| 396 |
- } |
|---|
| 397 |
- else { |
|---|
| 398 |
- $success = 'n'; |
|---|
| 399 |
- } |
|---|
| 400 |
- } |
|---|
| 401 |
- } |
|---|
| 402 |
- if (count($new_tags) > 0) { |
|---|
| 403 |
- $tags = implode(' ', $new_tags); |
|---|
| 404 |
- } |
|---|
| 405 |
- } |
|---|
| 406 |
- } |
|---|
| 407 |
- if (count($old_tags) > 0) { |
|---|
| 408 |
- foreach ($old_tags as $id => $tag) { |
|---|
| 409 |
- if (!in_array($tag, $new_tags)) { |
|---|
| 410 |
- if (!$this->remove_tag_by_id(str_replace('id_', '', $id))) { |
|---|
| 411 |
- $success = 'n'; |
|---|
| 412 |
- } |
|---|
| 413 |
- } |
|---|
| 414 |
- } |
|---|
| 415 |
- } |
|---|
| 416 |
- $result = array( |
|---|
| 417 |
- 'success' => $success |
|---|
| 418 |
- , 'tags' => $new_tags |
|---|
| 419 |
- ); |
|---|
| 420 |
- return $result; |
|---|
| 421 |
- } |
|---|
| 422 |
- |
|---|
| 423 |
- |
|---|
| 424 |
- /** |
|---|
| 425 |
- * Removes a tag |
|---|
| 426 |
- * |
|---|
| 427 |
- * @uses phptagengine::get_item_tag_id() |
|---|
| 428 |
- * @uses phptagengine::remove_tag_by_id() |
|---|
| 429 |
- * |
|---|
| 430 |
- * @param string $item |
|---|
| 431 |
- * @param string $tag |
|---|
| 432 |
- * @param string $user |
|---|
| 433 |
- * @param string $type |
|---|
| 434 |
- * @return boolean |
|---|
| 435 |
- */ |
|---|
| 436 |
- function remove_tag($item = null, $tag = null, $user = null, $type = null) { |
|---|
| 437 |
- $id = $this->get_item_tag_id($user, $item, $tag, $type); |
|---|
| 438 |
- return $this->remove_tag_by_id($id); |
|---|
| 439 |
- } |
|---|
| 440 |
- |
|---|
| 441 |
- /** |
|---|
| 442 |
- * Removes a tag by id |
|---|
| 443 |
- * |
|---|
| 444 |
- * @param integer $id |
|---|
| 445 |
- * @return boolean |
|---|
| 446 |
- */ |
|---|
| 447 |
- function remove_tag_by_id($id) { |
|---|
| 448 |
- if ($id == null) { |
|---|
| 449 |
- return false; |
|---|
| 450 |
- } |
|---|
| 451 |
- $result = $this->db->Execute(" |
|---|
| 452 |
- DELETE |
|---|
| 453 |
- FROM $this->table_tags |
|---|
| 454 |
- WHERE id = ".$this->db->qstr($id)." |
|---|
| 455 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 456 |
- if ($result) { |
|---|
| 457 |
- return true; |
|---|
| 458 |
- } |
|---|
| 459 |
- return false; |
|---|
| 460 |
- } |
|---|
| 461 |
- |
|---|
| 462 |
- /** |
|---|
| 463 |
- * Gets the tags for an item |
|---|
| 464 |
- * |
|---|
| 465 |
- * @param string $item |
|---|
| 466 |
- * @param string $type |
|---|
| 467 |
- * @param string $user |
|---|
| 468 |
- * @param boolean $use_cache |
|---|
| 469 |
- * @return array |
|---|
| 470 |
- */ |
|---|
| 471 |
- function get_tags($item = null, $type = null, $user = null, $use_cache = false) { |
|---|
| 472 |
- if ($item == null) { |
|---|
| 473 |
- return false; |
|---|
| 474 |
- } |
|---|
| 475 |
- $tags = array(); |
|---|
| 476 |
- if ($use_cache) { |
|---|
| 477 |
- if (isset($this->item_tags_cache['item_'.$item])) { |
|---|
| 478 |
- $tags = $this->item_tags_cache['item_'.$item]; |
|---|
| 479 |
- } |
|---|
| 480 |
- return $tags; |
|---|
| 481 |
- } |
|---|
| 482 |
- $where = ''; |
|---|
| 483 |
- if ($user != null) { |
|---|
| 484 |
- $where .= ' AND t.user = '.$this->db->qstr($user); |
|---|
| 485 |
- } |
|---|
| 486 |
- if ($item != null) { |
|---|
| 487 |
- $where .= ' AND t.item = '.$this->db->qstr($item); |
|---|
| 488 |
- } |
|---|
| 489 |
- if ($type != null) { |
|---|
| 490 |
- $where .= ' AND t.type = '.$this->db->qstr($type); |
|---|
| 491 |
- } |
|---|
| 492 |
- if ($where == '') { |
|---|
| 493 |
- return false; |
|---|
| 494 |
- } |
|---|
| 495 |
- $result = $this->db->Execute(" |
|---|
| 496 |
- SELECT t.id AS ID |
|---|
| 497 |
- , tn.name AS NAME |
|---|
| 498 |
- FROM $this->table_tags t |
|---|
| 499 |
- JOIN $this->table_tag_names tn |
|---|
| 500 |
- ON t.tag = tn.id |
|---|
| 501 |
- WHERE 1 = 1 |
|---|
| 502 |
- $where |
|---|
| 503 |
- ORDER BY tn.name |
|---|
| 504 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 505 |
- if ($result && $result->RowCount() > 0) { |
|---|
| 506 |
- while ($data = $result->FetchNextObject()) { |
|---|
| 507 |
- $tags['id_'.$data->ID] = $data->NAME; |
|---|
| 508 |
- } |
|---|
| 509 |
- } |
|---|
| 510 |
- return $tags; |
|---|
| 511 |
- } |
|---|
| 512 |
- |
|---|
| 513 |
- /** |
|---|
| 514 |
- * Completely removes a tag from the system (from @link phptagengine::$table_tags and @link phptagengine::$table_tag_names) |
|---|
| 515 |
- * |
|---|
| 516 |
- * @param string $tag |
|---|
| 517 |
- * @return boolean |
|---|
| 518 |
- */ |
|---|
| 519 |
- function delete_tag($tag = null) { |
|---|
| 520 |
- if (is_null($tag) || strstr($tag, ' ')) { |
|---|
| 521 |
- return false; |
|---|
| 522 |
- } |
|---|
| 523 |
- $tag_id = $this->get_tag_id($tag); |
|---|
| 524 |
- if ($tag_id == false) { |
|---|
| 525 |
- return false; |
|---|
| 526 |
- } |
|---|
| 527 |
- $result = $this->db->Execute(" |
|---|
| 528 |
- DELETE |
|---|
| 529 |
- FROM $this->table_tags |
|---|
| 530 |
- WHERE tag = '$tag_id' |
|---|
| 531 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 532 |
- if (!$result) { |
|---|
| 533 |
- return false; |
|---|
| 534 |
- } |
|---|
| 535 |
- $result = $this->db->Execute(" |
|---|
| 536 |
- DELETE |
|---|
| 537 |
- FROM $this->table_tag_names |
|---|
| 538 |
- WHERE id = '$tag_id' |
|---|
| 539 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 540 |
- if (!$result) { |
|---|
| 541 |
- return false; |
|---|
| 542 |
- } |
|---|
| 543 |
- return true; |
|---|
| 544 |
- } |
|---|
| 545 |
- |
|---|
| 546 |
- /** |
|---|
| 547 |
- * Changes the name of a tag, or consolidates two existing tags |
|---|
| 548 |
- * |
|---|
| 549 |
- * @param string $old_tag |
|---|
| 550 |
- * @param string $new_tag |
|---|
| 551 |
- * @return boolean |
|---|
| 552 |
- */ |
|---|
| 553 |
- function edit_tag($old_tag = null, $new_tag = null) { |
|---|
| 554 |
- if (is_null($old_tag) || is_null($new_tag) || strstr($new_tag, ' ')) { |
|---|
| 555 |
- return false; |
|---|
| 556 |
- } |
|---|
| 557 |
- if ($old_tag == $new_tag) { |
|---|
| 558 |
- return true; |
|---|
| 559 |
- } |
|---|
| 560 |
- if ($this->tag_exists($new_tag)) { |
|---|
| 561 |
-// move all tags to existing tag |
|---|
| 562 |
- $old_tag_id = $this->get_tag_id($old_tag); |
|---|
| 563 |
- $new_tag_id = $this->get_tag_id($new_tag); |
|---|
| 564 |
- $result = $this->db->Execute(" |
|---|
| 565 |
- UPDATE $this->table_tags |
|---|
| 566 |
- SET tag = '$new_tag_id' |
|---|
| 567 |
- WHERE tag = '$old_tag_id' |
|---|
| 568 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 569 |
- if (!$result) { |
|---|
| 570 |
- return false; |
|---|
| 571 |
- } |
|---|
| 572 |
-// check for dupes |
|---|
| 573 |
- $result = $this->db->Execute(" |
|---|
| 574 |
- SELECT * |
|---|
| 575 |
- FROM $this->table_tags |
|---|
| 576 |
- WHERE tag = '$new_tag_id' |
|---|
| 577 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 578 |
- if (!$result) { |
|---|
| 579 |
- return false; |
|---|
| 580 |
- } |
|---|
| 581 |
- else if ($result->RowCount() > 0) { |
|---|
| 582 |
- $tags = array(); |
|---|
| 583 |
- $ids_to_delete = array(); |
|---|
| 584 |
- while ($data = $result->FetchNextObject()) { |
|---|
| 585 |
- $key = $data->TAG.$data->ITEM.$data->TYPE.$data->USER; |
|---|
| 586 |
- if (!in_array($key, $tags)) { |
|---|
| 587 |
- $tags[] = $key; |
|---|
| 588 |
- } |
|---|
| 589 |
- else { |
|---|
| 590 |
- $ids_to_delete[] = $data->ID; |
|---|
| 591 |
- } |
|---|
| 592 |
- } |
|---|
| 593 |
-// remove dupes |
|---|
| 594 |
- if (count($ids_to_delete) > 0) { |
|---|
| 595 |
- $result = $this->db->Execute(" |
|---|
| 596 |
- DELETE |
|---|
| 597 |
- FROM $this->table_tags |
|---|
| 598 |
- WHERE id IN (".implode(',', $ids_to_delete).") |
|---|
| 599 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 600 |
- if (!$result) { |
|---|
| 601 |
- return false; |
|---|
| 602 |
- } |
|---|
| 603 |
- } |
|---|
| 604 |
- } |
|---|
| 605 |
- } |
|---|
| 606 |
- else { |
|---|
| 607 |
- $result = $this->db->Execute(" |
|---|
| 608 |
- UPDATE $this->table_tag_names |
|---|
| 609 |
- SET name = ".$this->db->qstr($new_tag)." |
|---|
| 610 |
- WHERE name = ".$this->db->qstr($old_tag)." |
|---|
| 611 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 612 |
- if (!$result) { |
|---|
| 613 |
- return false; |
|---|
| 614 |
- } |
|---|
| 615 |
- } |
|---|
| 616 |
- return true; |
|---|
| 617 |
- } |
|---|
| 618 |
- |
|---|
| 619 |
- /** |
|---|
| 620 |
- * Populate cache with items and tags to reduce queries |
|---|
| 621 |
- * |
|---|
| 622 |
- * @param array $items |
|---|
| 623 |
- * @param mixed $user |
|---|
| 624 |
- * @param mixed $type |
|---|
| 625 |
- */ |
|---|
| 626 |
- function populate_cache($items = array(), $user = null, $type = null) { |
|---|
| 627 |
- if (count($items) == 0) { |
|---|
| 628 |
- return; |
|---|
| 629 |
- } |
|---|
| 630 |
- $where = ''; |
|---|
| 631 |
- if ($user != null) { |
|---|
| 632 |
- $where .= ' AND t.user = '.$this->db->qstr($user); |
|---|
| 633 |
- } |
|---|
| 634 |
- if ($type != null) { |
|---|
| 635 |
- $where .= ' AND t.type = '.$this->db->qstr($type); |
|---|
| 636 |
- } |
|---|
| 637 |
- $where .= ' AND ( '; |
|---|
| 638 |
- $i = 0; |
|---|
| 639 |
- foreach ($items as $item) { |
|---|
| 640 |
- if ($i > 0) { |
|---|
| 641 |
- $where .= ' OR '; |
|---|
| 642 |
- } |
|---|
| 643 |
- $where .= ' t.item = '.$this->db->qstr($item); |
|---|
| 644 |
- $i++; |
|---|
| 645 |
- } |
|---|
| 646 |
- $where .= ' )'; |
|---|
| 647 |
- $result = $this->db->Execute(" |
|---|
| 648 |
- SELECT t.item AS ITEM |
|---|
| 649 |
- , t.id AS ID |
|---|
| 650 |
- , tn.name AS NAME |
|---|
| 651 |
- FROM $this->table_tags t |
|---|
| 652 |
- JOIN $this->table_tag_names tn |
|---|
| 653 |
- ON t.tag = tn.id |
|---|
| 654 |
- WHERE 1 = 1 |
|---|
| 655 |
- $where |
|---|
| 656 |
- ORDER BY t.item, tn.name |
|---|
| 657 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 658 |
- if ($result) { |
|---|
| 659 |
- while ($data = $result->FetchNextObject()) { |
|---|
| 660 |
- if (!isset($this->item_tags_cache['item_'.$data->ITEM])) { |
|---|
| 661 |
- $this->item_tags_cache['item_'.$data->ITEM] = array(); |
|---|
| 662 |
- } |
|---|
| 663 |
- $this->item_tags_cache['item_'.$data->ITEM]['id_'.$data->ID] = $data->NAME; |
|---|
| 664 |
- } |
|---|
| 665 |
- } |
|---|
| 666 |
- } |
|---|
| 667 |
- |
|---|
| 668 |
- /** |
|---|
| 669 |
- * Gets the ID of a tag, returns false if the tag does not exist |
|---|
| 670 |
- * |
|---|
| 671 |
- * @param string $name |
|---|
| 672 |
- * @return mixed |
|---|
| 673 |
- */ |
|---|
| 674 |
- function get_tag_id($name = null) { |
|---|
| 675 |
- if ($name == null) { |
|---|
| 676 |
- return false; |
|---|
| 677 |
- } |
|---|
| 678 |
- $name = $this->normalize($name); |
|---|
| 679 |
- $result = $this->db->Execute(" |
|---|
| 680 |
- SELECT id AS ID |
|---|
| 681 |
- FROM $this->table_tag_names |
|---|
| 682 |
- WHERE name = ".$this->db->qstr($name)." |
|---|
| 683 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 684 |
- if ($result && $result->RowCount() == 1) { |
|---|
| 685 |
- while ($data = $result->FetchNextObject()) { |
|---|
| 686 |
- return $data->ID; |
|---|
| 687 |
- } |
|---|
| 688 |
- } |
|---|
| 689 |
- return false; |
|---|
| 690 |
- } |
|---|
| 691 |
- |
|---|
| 692 |
- /** |
|---|
| 693 |
- * Checks if an item already has a certain tag |
|---|
| 694 |
- * |
|---|
| 695 |
- * @uses phptagengine::get_item_tag_id() |
|---|
| 696 |
- * |
|---|
| 697 |
- * @param mixed $user |
|---|
| 698 |
- * @param mixed $item |
|---|
| 699 |
- * @param mixed $tag |
|---|
| 700 |
- * @param mixed $type |
|---|
| 701 |
- * @return boolean |
|---|
| 702 |
- */ |
|---|
| 703 |
- function item_tag_exists($user = null, $item = null, $tag = null, $type = null) { |
|---|
| 704 |
- if (!$this->get_item_tag_id($user, $item, $tag, $type)) { |
|---|
| 705 |
- return false; |
|---|
| 706 |
- } |
|---|
| 707 |
- else { |
|---|
| 708 |
- return true; |
|---|
| 709 |
- } |
|---|
| 710 |
- } |
|---|
| 711 |
- |
|---|
| 712 |
- /** |
|---|
| 713 |
- * Gets the id of an item tag if it exists |
|---|
| 714 |
- * |
|---|
| 715 |
- * @param mixed $user |
|---|
| 716 |
- * @param mixed $item |
|---|
| 717 |
- * @param mixed $tag |
|---|
| 718 |
- * @param mixed $type |
|---|
| 719 |
- * @return mixed id of item or false |
|---|
| 720 |
- */ |
|---|
| 721 |
- function get_item_tag_id($user = null, $item = null, $tag = null, $type = null) { |
|---|
| 722 |
- if ($item == null || $tag == null) { |
|---|
| 723 |
- return false; |
|---|
| 724 |
- } |
|---|
| 725 |
- $tag = $this->normalize($tag); |
|---|
| 726 |
- $type = $this->default_value('type', $type); |
|---|
| 727 |
- $user = $this->default_value('user', $user); |
|---|
| 728 |
- $tags = $this->get_tags($item, $type, $user); |
|---|
| 729 |
- if (count($tags) == 0) { |
|---|
| 730 |
- return false; |
|---|
| 731 |
- } |
|---|
| 732 |
- else { |
|---|
| 733 |
- foreach ($tags as $id => $existing_tag) { |
|---|
| 734 |
- if ($tag == $existing_tag) { |
|---|
| 735 |
- return str_replace('id_', '', $id); |
|---|
| 736 |
- } |
|---|
| 737 |
- } |
|---|
| 738 |
- } |
|---|
| 739 |
- } |
|---|
| 740 |
- |
|---|
| 741 |
- /** |
|---|
| 742 |
- * Sends an XML response - used for AJAX |
|---|
| 743 |
- * |
|---|
| 744 |
- * @param string $string |
|---|
| 745 |
- */ |
|---|
| 746 |
- function xml_out($string) { |
|---|
| 747 |
- header("Content-type: text/xml"); |
|---|
| 748 |
- die('<?xml version="1.0"?>'."\n".'<response>'.$string.'</response>'); |
|---|
| 749 |
- } |
|---|
| 750 |
- |
|---|
| 751 |
- /** |
|---|
| 752 |
- * Prints the CSS and JS links needed in the HTML output |
|---|
| 753 |
- * |
|---|
| 754 |
- */ |
|---|
| 755 |
- function html_head() { |
|---|
| 756 |
- print(' |
|---|
| 757 |
-<!-- PHP Tag Engine CSS/JS - begin --> |
|---|
| 758 |
- |
|---|
| 759 |
-<style type="text/css"> @import url('.$this->base_url.'phptagengine.css?version='.$this->version.'); </style> |
|---|
| 760 |
-<script type="text/javascript" src="'.$this->base_url.'phptagengine.js.php?version='.$this->version.'"></script> |
|---|
| 761 |
- '."\n"); |
|---|
| 762 |
- if ($this->yac) { |
|---|
| 763 |
- $tags = $this->get_all_tags(); |
|---|
| 764 |
- if (count($tags) > 0) { |
|---|
| 765 |
- $tags = implode("','", $tags); |
|---|
| 766 |
- } |
|---|
| 767 |
- else { |
|---|
| 768 |
- $tags = ''; |
|---|
| 769 |
- } |
|---|
| 770 |
- print(" |
|---|
| 771 |
-<script type=\"text/javascript\"> |
|---|
| 772 |
-var tags = ['".$tags."']; |
|---|
| 773 |
-var yac_tags = new YAHOO.widget.DS_JSArray(tags); |
|---|
| 774 |
-</script> |
|---|
| 775 |
- \n"); |
|---|
| 776 |
- } |
|---|
| 777 |
- print('<!-- PHP Tag Engine CSS/JS - end -->'."\n"); |
|---|
| 778 |
- } |
|---|
| 779 |
- |
|---|
| 780 |
- /** |
|---|
| 781 |
- * Wrapper for htmlspecialchars, in case we need any special processing |
|---|
| 782 |
- * |
|---|
| 783 |
- * @param string $string |
|---|
| 784 |
- * @return string |
|---|
| 785 |
- */ |
|---|
| 786 |
- function html($string) { |
|---|
| 787 |
- return htmlspecialchars($string); |
|---|
| 788 |
- } |
|---|
| 789 |
- |
|---|
| 790 |
- /** |
|---|
| 791 |
- * Get a URL (to be used in a link) for browsing by tag |
|---|
| 792 |
- * |
|---|
| 793 |
- * @uses phptagengine::$tag_browse_url with string replacement |
|---|
| 794 |
- * @uses phptagengine::token_replace() |
|---|
| 795 |
- * |
|---|
| 796 |
- * @param string $tag the tag to browse |
|---|
| 797 |
- * @param mixed $type the type of item, if needed |
|---|
| 798 |
- * @return string |
|---|
| 799 |
- */ |
|---|
| 800 |
- function tag_browse_url($tag = '', $type = '') { |
|---|
| 801 |
- $type = $this->default_value('type', $type); |
|---|
| 802 |
- return $this->token_replace($this->tag_browse_url, urlencode($tag), urlencode($type)); |
|---|
| 803 |
- } |
|---|
| 804 |
- |
|---|
| 805 |
- /** |
|---|
| 806 |
- * Returns a list of all tags |
|---|
| 807 |
- * |
|---|
| 808 |
- * @param mixed $type |
|---|
| 809 |
- * @return array |
|---|
| 810 |
- */ |
|---|
| 811 |
- function get_all_tags($type = null) { |
|---|
| 812 |
- $type = $this->default_value('type', $type); |
|---|
| 813 |
- $tags = array(); |
|---|
| 814 |
- $result = $this->db->Execute(" |
|---|
| 815 |
- SELECT tn.id AS ID |
|---|
| 816 |
- , tn.name AS NAME |
|---|
| 817 |
- FROM $this->table_tag_names tn |
|---|
| 818 |
- JOIN $this->table_tags t |
|---|
| 819 |
- ON tn.id = t.tag |
|---|
| 820 |
- ORDER BY tn.name |
|---|
| 821 |
- ") or die($this->db->ErrorMsg().' in '.__FILE__); |
|---|
| 822 |
- if ($result) { |
|---|
| 823 |
- while ($data = $result->FetchNextObject()) { |
|---|
| 824 |
- if (!isset($tags['id_'.$data->ID])) { |
|---|
| 825 |
- $tags['id_'.$data->ID] = $data->NAME; |
|---|
| 826 |
- } |
|---|
| 827 |
- } |
|---|
| 828 |
- } |
|---|
| 829 |
- return $tags; |
|---|
| 830 |
- } |
|---|
| 831 |
- |
|---|
| 832 |
- /** |
|---|
| 833 |
- * Prints an HTML list of all tags |
|---|
| 834 |
- * |
|---|
| 835 |
- * @uses phptagengine::get_all_tags() |
|---|
| 836 |
- * |
|---|
| 837 |
- * @param string $url |
|---|
| 838 |
- * @param mixed $type |
|---|
| 839 |
- * @param mixed $onclick |
|---|
| 840 |
- */ |
|---|
| 841 |
- function html_tags_list($url = null, $type = null, $onclick = null, $edit_button = false, $delete_button = false) { |
|---|
| 842 |
- if (is_null($url)) { |
|---|
| 843 |
- $url = $this->tag_browse_url; |
|---|
| 844 |
- } |
|---|
| 845 |
- $tags = $this->get_all_tags($type); |
|---|
| 846 |
- print(' |
|---|
| 847 |
- <ul class="pte_tags_list_all"> |
|---|
| 848 |
- '); |
|---|
| 849 |
- if (count($tags) == 0) { |
|---|
| 850 |
- print(' |
|---|
| 851 |
- <li>'.$this->strings['data_none'].'</li> |
|---|
| 852 |
- '); |
|---|
| 853 |
- } |
|---|
| 854 |
- else { |
|---|
| 855 |
- foreach ($tags as $id => $tag) { |
|---|
| 856 |
- if (is_null($onclick)) { |
|---|
| 857 |
- $onclick_str = ''; |
|---|
| 858 |
- } |
|---|
| 859 |
- else { |
|---|
| 860 |
- $onclick_str = ' onclick="'.$this->token_replace($onclick, $tag, $type).'"'; |
|---|
| 861 |
- } |
|---|
| 862 |
- |
|---|
| 863 |
- /** |
|---|
| 864 |
- * @todo in some future release, add support for editing and deleting tags |
|---|
| 865 |
- * / |
|---|
| 866 |
-/* |
|---|
| 867 |
- if (!$edit_button) { |
|---|
| 868 |
- $edit_button_str = ''; |
|---|
| 869 |
- } |
|---|
| 870 |
- else { |
|---|
| 871 |
- $edit_button_str = '<input type="image" src="'.$this->edit_button_image_url.'" onclick="" alt="'.$this->strings['action_edit'].'" class="pte_image_button" />'; |
|---|
| 872 |
- } |
|---|
| 873 |
- if (!$delete_button) { |
|---|
| 874 |
- $delete_button_str = ''; |
|---|
| 875 |
- } |
|---|
| 876 |
- else { |
|---|
| 877 |
- $delete_button_str = '<input type="image" src="'.$this->delete_button_image_url.'" onclick="" alt="'.$this->strings['action_delete'].'" class="pte_image_button" />'; |
|---|
| 878 |
- } |
|---|
| 879 |
- print(' |
|---|
| 880 |
- <li id="pte_tag_'.$tag.'">'.$delete_button_str.$edit_button_str.'<a href="'.$this->token_replace($url, $tag, $type).'"'.$onclick_str.'>'.$this->html($tag).'</a></li> |
|---|
| 881 |
- '); |
|---|
| 882 |
-*/ |
|---|
| 883 |
- print(' |
|---|
| 884 |
- <li id="pte_tag_'.$tag.'"><a href="'.$this->token_replace($url, $tag, $type).'"'.$onclick_str.'>'.$this->html($tag).'</a></li> |
|---|
| 885 |
- '); |
|---|
| 886 |
- } |
|---|
| 887 |
- } |
|---|
| 888 |
- print(' |
|---|
| 889 |
- </ul> |
|---|
| 890 |
- '); |
|---|
| 891 |
- } |
|---|
| 892 |
- |
|---|
| 893 |
- /** |
|---|
| 894 |
- * Returns a string, replacing tokens for tag, type |
|---|
| 895 |
- * |
|---|
| 896 |
- * @param string $url |
|---|
| 897 |
- * @param string $tag |
|---|
| 898 |
- * @param string $type |
|---|
| 899 |
- * @return string |
|---|
| 900 |
- */ |
|---|
| 901 |
- function token_replace($string = null, $tag = null, $type = null) { |
|---|
| 902 |
- if (is_null($string) || is_null($tag)) { |
|---|
| 903 |
- return ''; |
|---|
| 904 |
- } |
|---|
| 905 |
- $type = $this->default_value('type', $type); |
|---|
| 906 |
- return str_replace('<tag>', $tag, str_replace('<type>', $type, $string)); |
|---|
| 907 |
- } |
|---|
| 908 |
- |
|---|
| 909 |
- /** |
|---|
| 910 |
- * Outputs the HTML display of tags for an item |
|---|
| 911 |
- * |
|---|
| 912 |
- * @param string $item |
|---|
| 913 |
- * @param mixed $type |
|---|
| 914 |
- */ |
|---|
| 915 |
- function html_item_tags($item, $type = null, $user = null, $use_cache = false) { |
|---|
| 916 |
- $user = $this->default_value('user', $user); |
|---|
| 917 |
- $type = $this->default_value('type', $type); |
|---|
| 918 |
- $tags = $this->get_tags($item, $type, $user, $use_cache); |
|---|
| 919 |
- if (count($tags) > 0) { |
|---|
| 920 |
- $tags_class = ' pte_has_tags'; |
|---|
| 921 |
- } |
|---|
| 922 |
- else { |
|---|
| 923 |
- $tags_class = ''; |
|---|
| 924 |
- } |
|---|
| 925 |
- print(' |
|---|
| 926 |
- < |
|---|