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
|
<?php
/*
* $Horde: horde/lib/Category.php,v 1.8.2.15 2003/04/28 19:59:07 jan Exp $
*
* Copyright 1999-2003 Stephane Huther <shuther@bigfoot.com>
* Copyright 2001-2003 Chuck Hagenbuch <chuck@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*/
require_once HORDE_BASE . '/lib/Serialize.php';
/**
* Required values for $params:
* groupid: define each group of categories we want to build
*/
// Available import/export formats.
/** @constant CATEGORY_FORMAT_TREE List every category in an array,
similar to PEAR/html/menu.php */
define('CATEGORY_FORMAT_TREE', 1);
/** @constant CATEGORY_FORMAT_FETCH List every category in an array
child-parent. Comes from driver pear/sql */
define('CATEGORY_FORMAT_FETCH', 2);
/** @constant CATEGORY_FORMAT_FLAT Get a full list - an array of keys */
define('CATEGORY_FORMAT_FLAT', 3);
/** @constant CATEGORY_FORMAT_3D Use a specific format, comes from the
project olbookmarks - sourceforge / libdrawtree.php:
$data[0][0]['name'] = 'Root'; $data[0][0]['p'] = '0.0';
$data[1][1]['name'] = 'dir1'; $data[1][1]['p'] = '0.0';
$data[2][2]['name'] = 'subdir1'; $data[2][2]['p'] = '1.1';
$data[3][3]['name'] = 'data1'; $data[3][3]['p'] = '2.2';
$data[3][4]['name'] = 'data2'; $data[3][4]['p'] = '2.2';
$data[3][5]['name'] = 'data3'; $data[3][5]['p'] = '2.2';
$data[2][6]['name'] = 'subdir2'; $data[2][6]['p'] = '1.1';
$data[1][7]['name'] = 'dir2'; $data[1][7]['p'] = '0.0';
$data[2][8]['name'] = 'subdir3'; $data[2][8]['p'] = '1.7';
$data[2][9]['name'] = 'subdir4'; $data[2][9]['p'] = '1.7';
*/
define('CATEGORY_FORMAT_3D', 4);
// Format used to serialize
define('CATEGORY_SERIALIZE_FORMAT', SERIALIZEUNIT_BASIC);
/**
* The Category:: class provides a common abstracted interface into
* the various backends for the Horde Categories system.
*
* A category is just a title that is saved in the page for the null
* driver or can be saved in a database to be accessed from
* everywhere. Every category must have a different name (for a same
* groupid). A category may have different parent categories.
*
* @note -1 is used as the root, but it is a STRING, it is important
* because database methods in PHP work only with strings, so we avoid
* confusion.
*
* @author Stephane Huther <shuther@bigfoot.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @version $Revision: 1.8.2.15 $
* @since Horde 2.1
* @package horde.category
*/
class Category {
/**
* Array of all categories: indexed by name = parent right now,
* the format is array(name => array(parents)), but it could
* change to an XML/DOM organization.
* @var array $categories
*/
var $_categories = array();
/**
* Hash containing connection parameters.
* @var array $_params
*/
var $_params = array();
/**
* Constructor
* @param array $params A hash containing any additional
* configuration or connection parameters a subclass
* might need.
* here, we need 'groupid' = a constant that defines
* in each group we will work
*/
function Category($params)
{
$this->_params = $params;
}
/**
* Attempts to return a concrete Category instance based on
* $driver.
*
* @param mixed $driver The type of concrete Category subclass to return.
* This is based on the storage driver ($driver). The
* code is dynamically included. If $driver is an array,
* then we will look in $driver[0]/lib/Category/ for
* the subclass implementation named $driver[1].php.
* @param array $params A hash containing any additional
* configuration or connection parameters a subclass
* might need.
* here, we need 'groupid' = a string that defines
* top-level categories of categories.
*
* @return object Category The newly created concrete Category instance,
* or false on an error.
*/
function &factory($driver, $params)
{
$driver = strtolower($driver);
if (empty($driver) || (strcmp($driver, 'none') == 0)) {
return new Category($params);
}
if (!empty($app)) {
include_once $GLOBALS['registry']->getParam('fileroot', $app) . '/lib/Category/' . $driver . '.php';
} elseif (@file_exists(dirname(__FILE__) . '/Category/' . $driver . '.php')) {
include_once dirname(__FILE__) . '/Category/' . $driver . '.php';
} else {
@include_once 'Horde/Category/' . $driver . '.php';
}
$class = 'Category_' . $driver;
if (class_exists($class)) {
return new $class($params);
} else {
return PEAR::raiseError('Class definition of ' . $class . ' not found.');
}
}
/**
* Attempts to return a reference to a concrete Category instance based on
* $driver. It will only create a new instance if no Category instance
* with the same parameters currently exists.
*
* This should be used if multiple category sources (and, thus,
* multiple Category instances) are required.
*
* This method must be invoked as: $var = &Category::singleton()
*
* @param mixed $driver The type of concrete Category subclass to return.
* This is based on the storage driver ($driver). The
* code is dynamically included. If $driver is an array,
* then we will look in $driver[0]/lib/Category/ for
* the subclass implementation named $driver[1].php.
* @param array $params (optional) A hash containing any additional
* configuration or connection parameters a subclass
* might need.
*
* @return object Category The concrete Category reference, or false on an
* error.
*/
function &singleton($driver, $params = array())
{
static $instances;
if (!isset($instances)) $instances = array();
if (is_array($driver)) {
$drivertag = implode(':', $driver);
} else {
$drivertag = $driver;
}
$signature = md5(strtolower($drivertag) . '][' . implode('][', $params));
if (!isset($instances[$signature])) {
$instances[$signature] = &Category::factory($driver, $params);
}
return $instances[$signature];
}
/**
* Add a category
*
* Note: there is no check against circular reference.
*
* @param mixed $category The name of the category.
* If it is a string, just the name, if it is a
* sub-class of CategoryObject, we get the real
* information from this object (getData)
* @param optional string $parent the name of the parent category
*
* @access protected
*/
function addCategory($category, $parent = '-1', $extended = null)
{
if (is_subclass_of($category, 'CategoryObject')) {
$data = $category->getData();
$category = $category->getName();
}
if ($this->exists($category, $parent)) {
return new PEAR_Error('Already exists');
} elseif ($parent != '-1' && !isset($this->_categories[$parent])) {
return new PEAR_Error('Add failed');
} elseif (!isset($this->_categories[$category])) {
$this->_categories[$category] = array();
}
$this->_categories[$category][$parent] = true;
return true;
}
/**
* Remove a category
*
* @param string $category The category to remove.
*
* @param optional string $parent The name of the parent category to remove $category from.
* If default, we removed it from every category.
* 0 means every branch
* -1 means the root
* Other means just that sub-category
* @param optional boolean force [default = false] Force to remove
* every child NOT YET IMPLEMENTED
*
* @note, the extended status is not removed!
*/
function removeCategory($category, $parent = '0', $force = false)
{
if ($force) {
return PEAR::raiseError('Not supported');
}
if (is_subclass_of($category, 'CategoryObject')) {
$category = $category->getName();
}
if ($this->exists($category, $parent) != true) {
return PEAR::raiseError('Does not exist');
}
switch ($parent) {
case '0':
unset($this->_categories[$category]);
break;
case '-1':
if (!isset($this->_categories[$category][$parent])) {
return new PEAR_Error('Does not exist');
}
unset($this->_categories[$category][$parent]);
break;
default:
if (!isset($this->_categories[$category][$parent])) {
return new PEAR_Error('Does not exist');
}
unset($this->_categories[$category][$parent]);
}
if (isset($this->_categories[$category]) &&
count($this->_categories[$category]) == 0) {
unset($this->_categories[$category]);
}
return true;
}
/**
* Move a category from one parent to a new one.
*
* @param string $category The name of the category.
* @param string $old_parent The name of the old parent.
* @param string $new_parent The name of the new parent.
*
* @note There is no check against circular references.
*/
function moveCategory($category, $old_parent, $new_parent)
{
if (is_subclass_of($category, 'CategoryObject')) {
$category = $category->getName();
}
if ($this->exists($category, $old_parent) != true) {
return new PEAR_Error('Does not exist');
}
if ($this->exists($new_parent) != true) {
return new PEAR_Error('Does not exist');
}
unset($this->_categories[$category][$old_parent]);
$this->_categories[$category][$new_parent] = true;
return true;
}
/**
* Change a category's name.
*
* @param string $old_category The old category.
* @param string $new_category The new category.
*/
function renameCategory($old_category, $new_category)
{
if (is_subclass_of($old_category, 'CategoryObject')) {
$old_name = $old_category->getName();
} else {
$old_name = $old_category;
}
if (is_subclass_of($new_category, 'CategoryObject')) {
$new_name = $new_category->getName();
$new_data = $new_category->getData();
} else {
$new_name = $new_category;
$new_data = null;
}
if (!$this->exists($old_name)) {
return new PEAR_Error('Does not exist');
}
if ($this->exists($new_name)) {
return new PEAR_Error('Duplicate name');
}
$this->_categories[$new_name] = $this->_categories[$old_name];
unset($this->_categories[$old_name]);
return true;
}
/**
* Return a CategoryObject (or subclass) object of the data in the category.
*
* @param string $category The name of the category to fetch.
* @param optional string $class The subclass of CategoryObject to use. Defaults
* to CategoryObject.
*/
function &getCategory($category, $class = 'CategoryObject')
{
$this->_load($category);
if (!class_exists($class)) {
return PEAR::raiseError($class . ' not found');
}
if (empty($this->_categories[$category])) {
return PEAR::raiseError($category . ' not found');
}
$categoryOb = new $class($category);
$categoryOb->data = $this->getCategoryData($category);
return $categoryOb;
}
function getCategoryData($category)
{
return null;
}
/**
* Update the data in a category. Does not change the category's
* parent or name, just serialized data.
*
* @param string $category The category object.
*/
function updateCategoryData($category)
{
if (is_subclass_of($category, 'CategoryObject')) {
$name = $category->getName();
} else {
// Nothing to do for non-objects.
return true;
}
if (!$this->exists($name)) {
return new PEAR_Error('Does not exist');
}
return true;
}
/**
* Export a list of categories
*
* @param integer $format Format of the export
* @param string optional $parent The name of the parent from
* where we export.
*
* @return mixed - usually an array
*
*/
function export($format, $parent = '-1')
{
$this->_load($parent);
$out = array();
switch ($format) {
case CATEGORY_FORMAT_TREE:
$this->extractAllLevelTree($out, $parent);
break;
case CATEGORY_FORMAT_FLAT:
$this->extractAllLevelList($out2, $parent);
if (empty($out2)) {
$out[$parent] = true;
} else {
foreach ($out2 as $key => $val) {
$out[$key] = true;
foreach ($val as $kkey => $vval) {
$out[$kkey] = true;
}
}
}
break;
case CATEGORY_FORMAT_3D:
$out2 = $this->export(CATEGORY_FORMAT_TREE, $parent);
$id = 0;
$this->map3d($out, $out2, 0, $id, 0);
break;
default:
return PEAR::raiseError('Not supported');
}
return $out;
}
/**
* Used by the export function to handle CATEGORY_FORMAT_3D.
*
* @param array $out Array that will contain the result
* @param array $arr Array from export(CATEGORY_FORMAT_TREE)
* @param integer $depth Depth of the child
* @param integer $id kind of auto increment value
* @param integer $pId $id of the parent, the depth will be $depth - 1
*
* @access private
* @see export()
*/
function map3d(&$out, $arr, $depth, &$id, $pId)
{
foreach ($arr as $key => $val) {
if (0 == $depth) {
$pDepth = 0;
} else {
$pDepth = $depth - 1;
}
if ('-1' == $key) {
$key = 'root';
}
$out[$depth][$id]['p'] = $pDepth . '.' . $pId;
$out[$depth][$id]['name'] = $key;
if (!isset($out['x']) || $depth > $out['x']) {
$out['x'] = $depth;
}
if (!isset($out['y']) || $id > $out['y']) {
$out['y'] = $id;
}
$id = $id + 1;
if (is_array($val)) {
$this->map3d($out, $val, $depth + 1, $id, $id - 1);
}
}
}
/**
* Import a list of categories. Used by drivers to populate the
* internal $categories array.
* @access private
*
* @param integer $format Format of the import (CATEGORY_FORMAT_*).
* @param array $data The data to import.
*/
function import($format, $data)
{
switch ($format) {
case CATEGORY_FORMAT_FETCH:
$cats = array();
$cdata = array();
$cids = array();
foreach ($data as $cat) {
$cids[$cat[0]] = $cat[1];
$cats[$cat[1]] = $cat[2];
}
foreach ($cats as $cat => $parents) {
if (!empty($parents)) {
$parents = explode(':', substr($parents, 1));
$par = $parents[count($parents) - 1];
$cdata[$cat] = array($cids[$par] => true);
} else {
$cdata[$cat] = array('-1' => true);
}
}
$this->_categories = array_merge_recursive($this->_categories, $cdata);
break;
default:
return PEAR::raiseError('Not supported');
}
return true;
}
/**
* Give the number of children a category has. We are talking
* about immediate children, not grandchildren, etc.
*
* @param string optional $parent The name of the parent from
* where we begin.
*
* @return integer
* @todo could be easily optimized ;-)
*/
function getNumberOfChildren($parent = '-1')
{
if (is_subclass_of($parent, 'CategoryObject')) {
$parent = $parent->getName();
}
$out = $this->extractOneLevel($parent);
return isset($out[$parent]) ? count($out[$parent]) : 0;
}
/**
* Extract one level of categories, based on a parent, get the childs
* format parent - name
* We can see this function as a way to get a collection of node's children
*
* @param string optional $parent The name of the parent from
* where we begin.
*
* @return array
*/
function extractOneLevel($parent = '-1')
{
$out = array();
foreach ($this->_categories as $category => $qparent) {
foreach ($qparent as $vparent => $notuse) {
if ($vparent == $parent) {
if (!isset($out[$parent])) $out[$parent] = array();
$out[$parent][$category] = true;
}
}
}
return $out;
}
/**
* Extract all level of categories, based on a parent
* Tree format
*
* @param array $out Contain the result
* @param string optional $parent The name of the parent from
* where we begin.
* @param integer optional $maxlevel The number of level of depth to check it
*
* Note, if nothing is returned that means there is no child, but
* don't forget to add the parent if you make some operations!
*/
function extractAllLevelTree(&$out, $parent='-1', $level=-1)
{
if ($level == 0) {
return false;
}
$k = $this->extractOneLevel($parent);
if (!isset($k[$parent])) {
return false;
}
$k = $k[$parent];
foreach ($k as $category => $v) {
if (!isset($out[$parent]) || !is_array($out[$parent])) {
$out[$parent] = array();
}
$out[$parent][$category] = true;
$this->extractAllLevelTree($out[$parent], $category, $level - 1);
}
}
/**
* Extract all level of categories, based on a parent
* List format: array(parent => array(child => true))
*
* @param string optional $parent The name of the parent from
* where we begin.
* @param integer optional $maxlevel The number of levels of depth to check it
* @param array $out Contain the result
*
* Note, if nothing is returned that means there is no child, but
* don't forget to add the parent if you make some operations!
*/
function extractAllLevelList(&$out, $parent = '-1', $level = -1)
{
if ($level == 0) {
return false;
}
$k = $this->extractOneLevel($parent);
if (!isset($k[$parent])) {
return false;
}
$k = $k[$parent];
foreach ($k as $category => $v) {
if (!isset($out[$parent])) {
$out[$parent] = array();
}
if (!isset($out[$parent][$category])) {
$out[$parent][$category] = true;
$this->extractAllLevelList($out, $category, $level - 1);
}
}
}
/**
* Get a list of parents, based on a child - just one level
*
* @param string $child The name of the child.
* @param optional string $parent The name of the parent from where
* we want to check.
*
* @return array
*/
function getImmediateParents($child, $parentfrom = '0')
{
if (is_subclass_of($child, 'CategoryObject')) {
$child = $child->getName();
}
if ($this->exists($child, $parentfrom) != true) {
return new PEAR_Error('Does not exist');
}
return $this->_categories[$child];
}
/**
* Get a list of parents, based on a child - every levels
*
* @param string $child The name of the child
* @param optional string $parent The name of the parent from where
* we want to check.
* @return array [child] [parent] with a tree format
*/
function getParents($child, $parentfrom = '0')
{
$ret = $this->getImmediateParents($child, $parentfrom);
if (!is_array($ret)) {
return new PEAR_Error('Parents not found');
}
foreach ($ret as $parent => $trueval) {
if ($parent != '-1') {
$ret[$parent]=$this->getParents($parent);
}
}
return $ret;
}
/**
* Check if a category exists or not. The category -1 always
* exists.
*
* @param string $category The name of the category
*
* @return boolean True if the category exists, false otherwise.
*/
function exists($category)
{
$this->_load($category);
if (is_subclass_of($category, 'CategoryObject')) {
$category = $category->getName();
}
if ($category == '-1') {
return true;
}
if (!array_key_exists($category, $this->_categories)) {
return false;
}
return true;
}
/**
* Load (a subset of) the category tree into the $_categories
* array.
*
* @param string $root (optional) Which portion of the category
* tree to load. Defaults to all of it.
*
* @return mixed True on success or a PEAR_Error on failure.
*
* @note No check against circular references.
* @access private
*/
function _load($root = null)
{
}
}
/**
* Class that can be extended to save arbitrary information as part of
* a category. The Groups system makes use of this; the
* CategoryObject_Group class is an example of an extension of this
* class with specialized methods.
*
* @author Stephane Huther <shuther@bigfoot.com>
* @author Chuck Hagenbuch <chuck@horde.org>
* @version $Revision: 1.8.2.15 $
* @since Horde 2.1
* @package horde.category
*/
class CategoryObject {
/**
* Key-value hash that will be serialized.
* @see getData()
* @var array $data
*/
var $data = array();
/**
* The unique name of this category. These names have the same
* requirements as other category names - they must be unique,
* etc.
* @var string $name.
*/
var $name;
/**
* CategoryObject constructor. Just sets the $name parameter.
*
* @param string $name The category name.
*/
function CategoryObject($name)
{
$this->name = $name;
}
/**
* Get the name of this category.
*
* @return string The category name.
*/
function getName()
{
return $this->name;
}
/**
* Get a pointer/accessor to the array that we will save
* needed because PHP is not an object language
* @return array reference to the internal array to serialize
*/
function &getData()
{
return $this->data;
}
/**
* Merge the data of an array with the one already in the class
* @param array $arr
*/
function mergeData(&$arr)
{
$this->data = array_merge_recursive($this->getData(), $arr);
}
}
?>
|