| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Administrator 4 * @subpackage com_finder 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE 8 */ 9 10 defined('_JEXEC') or die; 11 12 /** 13 * Stemmer base class for the Finder indexer package. 14 * 15 * @package Joomla.Administrator 16 * @subpackage com_finder 17 * @since 2.5 18 */ 19 class FinderIndexerTaxonomy 20 { 21 /** 22 * An internal cache of taxonomy branch data. 23 * 24 * @var array 25 * @since 2.5 26 */ 27 public static $branches = array(); 28 29 /** 30 * An internal cache of taxonomy node data. 31 * 32 * @var array 33 * @since 2.5 34 */ 35 public static $nodes = array(); 36 37 /** 38 * Method to add a branch to the taxonomy tree. 39 * 40 * @param string $title The title of the branch. 41 * @param integer $state The published state of the branch. [optional] 42 * @param integer $access The access state of the branch. [optional] 43 * 44 * @return integer The id of the branch. 45 * 46 * @since 2.5 47 * @throws Exception on database error. 48 */ 49 public static function addBranch($title, $state = 1, $access = 1) 50 { 51 // Check to see if the branch is in the cache. 52 if (isset(self::$branches[$title])) 53 { 54 return self::$branches[$title]->id; 55 } 56 57 // Check to see if the branch is in the table. 58 $db = JFactory::getDBO(); 59 $query = $db->getQuery(true); 60 $query->select('*'); 61 $query->from($db->quoteName('#__finder_taxonomy')); 62 $query->where($db->quoteName('parent_id') . ' = 1'); 63 $query->where($db->quoteName('title') . ' = ' . $db->quote($title)); 64 $db->setQuery($query); 65 66 // Get the result. 67 $result = $db->loadObject(); 68 69 // Check for a database error. 70 if ($db->getErrorNum()) 71 { 72 // Throw database error exception. 73 throw new Exception($db->getErrorMsg(), 500); 74 } 75 76 // Check if the database matches the input data. 77 if (!empty($result) && $result->state == $state && $result->access == $access) 78 { 79 // The data matches, add the item to the cache. 80 self::$branches[$title] = $result; 81 82 return self::$branches[$title]->id; 83 } 84 85 // The database did not match the input. This could be because the 86 // state has changed or because the branch does not exist. Let's figure 87 // out which case is true and deal with it. 88 $branch = new JObject; 89 if (empty($data)) 90 { 91 // Prepare the branch object. 92 $branch->parent_id = 1; 93 $branch->title = $title; 94 $branch->state = (int) $state; 95 $branch->access = (int) $access; 96 } 97 else 98 { 99 // Prepare the branch object. 100 $branch->id = (int) $result->id; 101 $branch->parent_id = (int) $result->parent_id; 102 $branch->title = $result->title; 103 $branch->state = (int) $result->title; 104 $branch->access = (int) $result->access; 105 $branch->ordering = (int) $result->ordering; 106 } 107 108 // Store the branch. 109 self::storeNode($branch); 110 111 // Add the branch to the cache. 112 self::$branches[$title] = $branch; 113 114 return self::$branches[$title]->id; 115 } 116 117 /** 118 * Method to add a node to the taxonomy tree. 119 * 120 * @param string $branch The title of the branch to store the node in. 121 * @param string $title The title of the node. 122 * @param integer $state The published state of the node. [optional] 123 * @param integer $access The access state of the node. [optional] 124 * 125 * @return integer The id of the node. 126 * 127 * @since 2.5 128 * @throws Exception on database error. 129 */ 130 public static function addNode($branch, $title, $state = 1, $access = 1) 131 { 132 // Check to see if the node is in the cache. 133 if (isset(self::$nodes[$branch][$title])) 134 { 135 return self::$nodes[$branch][$title]->id; 136 } 137 138 // Get the branch id, inserted it if it does not exist. 139 $branchId = self::addBranch($branch); 140 141 // Check to see if the node is in the table. 142 $db = JFactory::getDBO(); 143 $query = $db->getQuery(true); 144 $query->select('*'); 145 $query->from($db->quoteName('#__finder_taxonomy')); 146 $query->where($db->quoteName('parent_id') . ' = ' . $db->quote($branchId)); 147 $query->where($db->quoteName('title') . ' = ' . $db->quote($title)); 148 $db->setQuery($query); 149 150 // Get the result. 151 $result = $db->loadObject(); 152 153 // Check for a database error. 154 if ($db->getErrorNum()) 155 { 156 // Throw database error exception. 157 throw new Exception($db->getErrorMsg(), 500); 158 } 159 160 // Check if the database matches the input data. 161 if (!empty($result) && $result->state == $state && $result->access == $access) 162 { 163 // The data matches, add the item to the cache. 164 self::$nodes[$branch][$title] = $result; 165 166 return self::$nodes[$branch][$title]->id; 167 } 168 169 // The database did not match the input. This could be because the 170 // state has changed or because the node does not exist. Let's figure 171 // out which case is true and deal with it. 172 $node = new JObject; 173 if (empty($data)) 174 { 175 // Prepare the node object. 176 $node->parent_id = (int) $branchId; 177 $node->title = $title; 178 $node->state = (int) $state; 179 $node->access = (int) $access; 180 } 181 else 182 { 183 // Prepare the node object. 184 $node->id = (int) $result->id; 185 $node->parent_id = (int) $result->parent_id; 186 $node->title = $result->title; 187 $node->state = (int) $result->title; 188 $node->access = (int) $result->access; 189 $node->ordering = (int) $result->ordering; 190 } 191 192 // Store the node. 193 self::storeNode($node); 194 195 // Add the node to the cache. 196 self::$nodes[$branch][$title] = $node; 197 198 return self::$nodes[$branch][$title]->id; 199 } 200 201 /** 202 * Method to add a map entry between a link and a taxonomy node. 203 * 204 * @param integer $linkId The link to map to. 205 * @param integer $nodeId The node to map to. 206 * 207 * @return boolean True on success. 208 * 209 * @since 2.5 210 * @throws Exception on database error. 211 */ 212 public static function addMap($linkId, $nodeId) 213 { 214 // Insert the map. 215 $db = JFactory::getDBO(); 216 217 $query = $db->getQuery(true); 218 $query->select($db->quoteName('link_id')); 219 $query->from($db->quoteName('#__finder_taxonomy_map')); 220 $query->where($db->quoteName('link_id') . ' = ' . (int)$linkId); 221 $query->where($db->quoteName('node_id') . ' = ' . (int)$nodeId); 222 $db->setQuery($query); 223 $db->query(); 224 $id = (int) $db->loadResult(); 225 226 $map = new JObject(); 227 $map->link_id = (int) $linkId; 228 $map->node_id = (int) $nodeId; 229 230 if ($id) { 231 $db->updateObject('#__finder_taxonomy_map', $map); 232 } 233 else { 234 $db->insertObject('#__finder_taxonomy_map', $map); 235 } 236 237 // Check for a database error. 238 if ($db->getErrorNum()) 239 { 240 // Throw database error exception. 241 throw new Exception($db->getErrorMsg(), 500); 242 } 243 244 return true; 245 } 246 247 /** 248 * Method to get the title of all taxonomy branches. 249 * 250 * @return array An array of branch titles. 251 * 252 * @since 2.5 253 * @throws Exception on database error. 254 */ 255 public static function getBranchTitles() 256 { 257 $db = JFactory::getDBO(); 258 259 // Set user variables 260 $user = JFactory::getUser(); 261 $groups = implode(',', $user->getAuthorisedViewLevels()); 262 263 // Create a query to get the taxonomy branch titles. 264 $query = $db->getQuery(true); 265 $query->select($db->quoteName('title')); 266 $query->from($db->quoteName('#__finder_taxonomy')); 267 $query->where($db->quoteName('parent_id') . ' = 1'); 268 $query->where($db->quoteName('state') . ' = 1'); 269 $query->where($db->quoteName('access') . ' IN (' . $groups . ')'); 270 271 // Get the branch titles. 272 $db->setQuery($query); 273 $results = $db->loadColumn(); 274 275 // Check for a database error. 276 if ($db->getErrorNum()) 277 { 278 // Throw database error exception. 279 throw new Exception($db->getErrorMsg(), 500); 280 } 281 282 return $results; 283 } 284 285 /** 286 * Method to find a taxonomy node in a branch. 287 * 288 * @param string $branch The branch to search. 289 * @param string $title The title of the node. 290 * 291 * @return mixed Integer id on success, null on no match. 292 * 293 * @since 2.5 294 * @throws Exception on database error. 295 */ 296 public static function getNodeByTitle($branch, $title) 297 { 298 $db = JFactory::getDBO(); 299 300 // Set user variables 301 $user = JFactory::getUser(); 302 $groups = implode(',', $user->getAuthorisedViewLevels()); 303 304 // Create a query to get the node. 305 $query = $db->getQuery(true); 306 $query->select('t1.*'); 307 $query->from($db->quoteName('#__finder_taxonomy') . ' AS t1'); 308 $query->join('INNER', $db->quoteName('#__finder_taxonomy') . ' AS t2 ON t2.id = t1.parent_id'); 309 $query->where('t1.' . $db->quoteName('access') . ' IN (' . $groups . ')'); 310 $query->where('t1.' . $db->quoteName('state') . ' = 1'); 311 $query->where('t1.' . $db->quoteName('title') . ' LIKE "' . $db->escape($title) . '%"'); 312 $query->where('t2.' . $db->quoteName('access') . ' IN (' . $groups . ')'); 313 $query->where('t2.' . $db->quoteName('state') . ' = 1'); 314 $query->where('t2.' . $db->quoteName('title') . ' = ' . $db->quote($branch)); 315 316 // Get the node. 317 $db->setQuery($query, 0, 1); 318 $result = $db->loadObject(); 319 320 // Check for a database error. 321 if ($db->getErrorNum()) 322 { 323 // Throw database error exception. 324 throw new Exception($db->getErrorMsg(), 500); 325 } 326 327 return $result; 328 } 329 330 /** 331 * Method to remove map entries for a link. 332 * 333 * @param integer $linkId The link to remove. 334 * 335 * @return boolean True on success. 336 * 337 * @since 2.5 338 * @throws Exception on database error. 339 */ 340 public static function removeMaps($linkId) 341 { 342 // Delete the maps. 343 $db = JFactory::getDBO(); 344 $query = $db->getQuery(true); 345 $query->delete(); 346 $query->from($db->quoteName('#__finder_taxonomy_map')); 347 $query->where($db->quoteName('link_id') . ' = ' . (int) $linkId); 348 $db->setQuery($query); 349 $db->query(); 350 351 // Check for a database error. 352 if ($db->getErrorNum()) 353 { 354 // Throw database error exception. 355 throw new Exception($db->getErrorMsg(), 500); 356 } 357 358 return true; 359 } 360 361 /** 362 * Method to remove orphaned taxonomy nodes and branches. 363 * 364 * @return integer The number of deleted rows. 365 * 366 * @since 2.5 367 * @throws Exception on database error. 368 */ 369 public static function removeOrphanNodes() 370 { 371 // Delete all orphaned nodes. 372 $db = JFactory::getDBO(); 373 /*$query = $db->getQuery(true); 374 $query->delete(); 375 $query->from($db->quoteName('#__finder_taxonomy') . ' AS t'); 376 $query->join('LEFT', $db->quoteName('#__finder_taxonomy_map') . ' AS m ON m.node_id = t.id'); 377 $query->where('t.' . $db->quoteName('parent_id') . ' > 1'); 378 $query->where('m.' . $db->quoteName('link_id') . ' IS NULL');*/ 379 //@TODO: Query does not work with JDatabaseQuery, does not support DELETE t.*, must be DELETE FROM ... 380 $query = 'DELETE t.*' . 381 ' FROM ' . $db->quoteName('#__finder_taxonomy') . ' AS t' . 382 ' LEFT JOIN ' . $db->quoteName('#__finder_taxonomy_map') . ' AS m ON m.node_id = t.id' . 383 ' WHERE t.' . $db->quoteName('parent_id') . ' > 1' . 384 ' AND m.' . $db->quoteName('link_id') . ' IS NULL'; 385 $db->setQuery($query); 386 $db->query(); 387 388 // Check for a database error. 389 if ($db->getErrorNum()) 390 { 391 // Throw database error exception. 392 throw new Exception($db->getErrorMsg(), 500); 393 } 394 395 return $db->getAffectedRows(); 396 } 397 398 /** 399 * Method to store a node to the database. This method will accept either a branch or a node. 400 * 401 * @param object $item The item to store. 402 * 403 * @return boolean True on success. 404 * 405 * @since 2.5 406 * @throws Exception on database error. 407 */ 408 protected static function storeNode($item) 409 { 410 $db = JFactory::getDBO(); 411 412 // Check if we are updating or inserting the item. 413 if (empty($item->id)) 414 { 415 // Insert the item. 416 $db->insertObject('#__finder_taxonomy', $item, 'id'); 417 } 418 else 419 { 420 // Update the item. 421 $db->updateObject('#__finder_taxonomy', $item, 'id'); 422 } 423 424 // Check for a database error. 425 if ($db->getErrorNum()) 426 { 427 // Throw database error exception. 428 throw new Exception($db->getErrorMsg(), 500); 429 } 430 431 return true; 432 } 433 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |