/usr/share/horde/whups/lib/Query.php is in php-horde-whups 3.0.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | <?php
/**
* Copyright 2001-2002 Robert E. Coyle <robertecoyle@hotmail.com>
* Copyright 2001-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsdl.php.
*
* @author Robert E. Coyle <robertecoyle@hotmail.com>
* @author Jan Schneider <jan@horde.org>
* @package Whups
*/
/**
* array(
* 'type' => Whups_Query::TYPE_...
* 'children' => array(...) unless type == Whups_Query::TYPE_CRITERION
* 'criterion' => Whups_Query::CRITERION_... if type == Whups_Query::TYPE_CRITERION
* 'operator' => Whups_Query::OPERATOR_... if type == Whups_Query::TYPE_CRITERION
* 'value' => other argument to operator of criterion
*/
/**
* @package Whups
*/
class Whups_Query
{
/** Mode types. */
const TYPE_AND = 1;
const TYPE_OR = 2;
const TYPE_NOT = 3;
const TYPE_CRITERION = 4;
/** Criterion types. */
const CRITERION_ID = 1;
const CRITERION_QUEUE = 2;
const CRITERION_TYPE = 3;
const CRITERION_STATE = 4;
const CRITERION_PRIORITY = 5;
const CRITERION_OWNERS = 7;
const CRITERION_REQUESTER = 8;
const CRITERION_GROUPS = 9;
const CRITERION_ADDED_COMMENT = 11;
const CRITERION_COMMENT = 12;
const CRITERION_SUMMARY = 13;
const CRITERION_ATTRIBUTE = 14;
const CRITERION_VERSION = 15;
const CRITERION_TIMESTAMP = 16;
const CRITERION_UPDATED = 17;
const CRITERION_RESOLVED = 18;
const CRITERION_ASSIGNED = 19;
const CRITERION_DUE = 20;
/** Operators for integer fields. */
const OPERATOR_GREATER = 1;
const OPERATOR_LESS = 2;
const OPERATOR_EQUAL = 3;
/** Operators for text fields. */
const OPERATOR_CI_SUBSTRING = 4;
const OPERATOR_CS_SUBSTRING = 5;
const OPERATOR_WORD = 6;
const OPERATOR_PATTERN = 7;
/**
* @var Whups_Query_Manager
*/
protected $_qManager;
/**
* Query id.
*
* @var integer
*/
public $id;
/**
* The full name of the query.
*
* @var string
*/
public $name;
/**
* The query slug (short name).
*
* @var string
*/
public $slug;
/**
* @var array
*/
public $query = array(
'type' => Whups_Query::TYPE_AND,
'children' => array());
/**
* @var array
*/
public $parameters = array();
/**
* Constructor
*
* @param Whups_Query_Manager $qManager
* @param array $qDetails
*/
function __construct(Whups_Query_Manager &$qManager, array $qDetails = array())
{
$this->_qManager = &$qManager;
if (isset($qDetails['query_id'])) {
$this->id = $qDetails['query_id'];
}
if (isset($qDetails['query_name'])) {
$this->name = $qDetails['query_name'];
}
if (isset($qDetails['query_slug'])) {
$this->slug = $qDetails['query_slug'];
}
if (isset($qDetails['query_object'])) {
$this->query = @unserialize($qDetails['query_object']);
}
if (isset($qDetails['query_parameters'])) {
$this->parameters = @unserialize($qDetails['query_parameters']);
}
}
/**
* @static
*/
public static function pathToString(&$path)
{
return implode(',', $path);
}
/**
* @static
*/
public static function stringToPath($pathstring)
{
if (!strlen($pathstring)) {
return array();
}
return explode(',', $pathstring);
}
/**
* Returns human readable descriptions of all operator types.
*
* @static
*
* @return array Hash with operator types and descriptions.
*/
public static function textOperators()
{
return array(
Whups_Query::OPERATOR_EQUAL => _("Exact Match"),
Whups_Query::OPERATOR_CI_SUBSTRING => _("Case Insensitive Substring"),
Whups_Query::OPERATOR_CS_SUBSTRING => _("Case Sensitive Substring"),
Whups_Query::OPERATOR_WORD => _("Match Word"),
Whups_Query::OPERATOR_PATTERN => _("Match Pattern"));
}
/**
* Checks to see if a user has a given permission.
*
* @param string $userid The userid of the user.
* @param integer $permission A Horde_Perms::* constant to test for.
* @param string $creator The creator of the event.
*
* @return boolean Whether or not $userid has $permission.
*/
public function hasPermission($userid, $permission, $creator = null)
{
return $this->_qManager->hasPermission($this->id, $userid, $permission, $creator);
}
/**
* Saves any changes to this object to the backend
* permanently. New objects are added instead.
*
*/
public function save()
{
$this->_qManager->save($this);
}
/**
* Delete this object from the backend permanently.
*
*/
public function delete()
{
$this->_qManager->delete($this);
}
/**
* Returns <link> data for this query's feed.
*
* @return array Link data.
*/
public function feedLink()
{
return array(
'href' => Whups::urlFor('query_rss', empty($this->slug) ? array('id' => $this->id) : array('slug' => $this->slug), true, -1),
'title' => $this->name
);
}
/**
* Tab operations for this query.
*
* @params Horde_Variables
*/
public function getTabs(Horde_Variables $vars)
{
// Create a few variables that are reused.
$queryurl = Horde::url('query/index.php');
$edit = $this->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT);
$delete = $this->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::DELETE);
$tabs = new Horde_Core_Ui_Tabs('action', $vars);
$tabs->addTab(_("Ne_w Query"), $queryurl, 'new');
if (!$this->id || $edit) {
$tabs->addTab(_("_Edit Query"), $queryurl, 'edit');
}
if ($this->id && $edit && empty($GLOBALS['conf']['share']['no_sharing'])) {
$GLOBALS['page_output']->addScriptFile('popup.js', 'horde');
$permsurl = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/services/shares/edit.php')->add(array(
'app' => 'whups',
'cid' => $this->id));
$tabs->addTab(
_("Edit _Permissions"),
$permsurl,
array(
'tabname' => 'perms',
'onclick' => 'popup(\'' . $permsurl . '\'); return false;',
'target' => '_blank'));
}
$tabs->addTab(_("E_xecute Query"), Horde::url('query/run.php'), 'run');
$tabs->addTab(_("_Load Query"), $queryurl, 'load');
if ((!$this->id && $GLOBALS['registry']->getAuth()) ||
($this->id && $edit)) {
$tabs->addTab(_("Sa_ve Query"), $queryurl, 'save');
}
if ($this->id && $delete) {
$tabs->addTab(_("_Delete Query"), $queryurl, 'delete');
}
return $tabs;
}
/**
* Path to form
*
* @param Horde_Variables $vars
*
* @return string
* @throws Whups_Exception
*/
public function pathToForm(&$vars)
{
$path = Whups_Query::stringToPath($vars->get('path'));
$parent = null;
$qobj = $this->query;
for ($i = 0, $c = count($path); $i < $c; $i++) {
$parent = $qobj;
$qobj = $qobj['children'][$path[$i]];
}
if ($qobj['type'] != Whups_Query::TYPE_CRITERION) {
// Search for any criteria that have been combined automatically
// with an AND or OR.
switch ($qobj['type']) {
case Whups_Query::TYPE_OR:
// Search for multiple ids.
$criteria = array();
foreach ($qobj['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
$child['criterion'] != Whups_Query::CRITERION_ID) {
$criteria = false;
break;
}
$criteria[] = $child['value'];
}
if ($criteria) {
$vars->set('id', implode(',', $criteria));
return 'props';
}
// Search for user criteria.
$criteria = array();
$operator = $value = null;
foreach ($qobj['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
($child['criterion'] != Whups_Query::CRITERION_OWNERS &&
$child['criterion'] != Whups_Query::CRITERION_REQUESTER &&
$child['criterion'] != Whups_Query::CRITERION_ADDED_COMMENT) ||
(isset($operator) && $operator != $child['operator']) ||
(isset($value) && $value != $child['value'])) {
$criteria = false;
break;
}
$criteria[] = $child['criterion'];
$operator = $child['operator'];
$value = $child['value'];
}
if ($criteria) {
$vars->set('user', $value);
$vars->set('operator', $operator);
foreach ($criteria as $criterion) {
switch ($criterion) {
case Whups_Query::CRITERION_OWNERS:
$vars->set('owners', true);
break;
case Whups_Query::CRITERION_REQUESTER:
$vars->set('requester', true);
break;
case Whups_Query::CRITERION_ADDED_COMMENT:
$vars->set('comments', true);
break;
}
}
return 'user';
}
// Search for text criteria.
$criteria = array();
$operator = $value = null;
foreach ($qobj['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
($child['criterion'] != Whups_Query::CRITERION_COMMENT &&
$child['criterion'] != Whups_Query::CRITERION_SUMMARY) ||
(isset($operator) && $operator != $child['operator']) ||
(isset($value) && $value != $child['value'])) {
$criteria = false;
break;
}
$criteria[] = $child['criterion'];
$operator = $child['operator'];
$value = $child['value'];
}
if ($criteria) {
$vars->set('text', $value);
$vars->set('operator', $operator);
foreach ($criteria as $criterion) {
if ($criterion == Whups_Query::CRITERION_COMMENT) {
$vars->set('comments', true);
} elseif ($criterion == Whups_Query::CRITERION_SUMMARY) {
$vars->set('summary', true);
}
}
return 'text';
}
// Search for attributes.
$attribs = array_keys($GLOBALS['whups_driver']->getAttributesForType());
$criteria = array();
$operator = $value = null;
foreach ($qobj['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
$child['criterion'] != Whups_Query::CRITERION_ATTRIBUTE ||
(isset($operator) && $operator != $child['operator']) ||
(isset($value) && $value != $child['value']) ||
!in_array($child['cvalue'], $attribs)) {
$criteria = false;
break;
}
$criteria[] = $child['cvalue'];
$operator = $child['operator'];
$value = $child['value'];
}
if ($criteria) {
$vars->set('text', $value);
$vars->set('operator', $operator);
foreach ($criteria as $criterion) {
$vars->set('a' . $criterion, true);
}
return 'attribs';
}
break;
case Whups_Query::TYPE_AND:
// Search for date criteria.
$criteria = false;
foreach ($qobj['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
($child['criterion'] != Whups_Query::CRITERION_TIMESTAMP &&
$child['criterion'] != Whups_Query::CRITERION_UPDATED &&
$child['criterion'] != Whups_Query::CRITERION_RESOLVED &&
$child['criterion'] != Whups_Query::CRITERION_ASSIGNED &&
$child['criterion'] != Whups_Query::CRITERION_DUE)) {
$criteria = false;
break;
}
$criteria = true;
}
if ($criteria) {
foreach ($qobj['children'] as $child) {
switch ($child['criterion'] . $child['operator']) {
case Whups_Query::CRITERION_TIMESTAMP . Whups_Query::OPERATOR_GREATER:
$vars->set('ticket_timestamp[from]', $child['value']);
break;
case Whups_Query::CRITERION_TIMESTAMP . Whups_Query::OPERATOR_LESS:
$vars->set('ticket_timestamp[to]', $child['value']);
break;
case Whups_Query::CRITERION_UPDATED . Whups_Query::OPERATOR_GREATER:
$vars->set('date_updated[from]', $child['value']);
break;
case Whups_Query::CRITERION_UPDATED . Whups_Query::OPERATOR_LESS:
$vars->set('date_updated[to]', $child['value']);
break;
case Whups_Query::CRITERION_RESOLVED . Whups_Query::OPERATOR_GREATER:
$vars->set('date_resolved[from]', $child['value']);
break;
case Whups_Query::CRITERION_RESOLVED . Whups_Query::OPERATOR_LESS:
$vars->set('date_resolved[to]', $child['value']);
break;
case Whups_Query::CRITERION_ASSIGNED . Whups_Query::OPERATOR_GREATER:
$vars->set('date_assigned[from]', $child['value']);
break;
case Whups_Query::CRITERION_ASSIGNED . Whups_Query::OPERATOR_LESS:
$vars->set('date_assigned[to]', $child['value']);
break;
case Whups_Query::CRITERION_DUE . Whups_Query::OPERATOR_GREATER:
$vars->set('ticket_due[from]', $child['value']);
break;
case Whups_Query::CRITERION_DUE . Whups_Query::OPERATOR_LESS:
$vars->set('ticket_due[to]', $child['value']);
break;
}
}
return 'date';
}
// Search for version criterion.
if (count($qobj['children']) == 2 &&
$qobj['children'][0]['type'] == Whups_Query::TYPE_CRITERION &&
$qobj['children'][0]['criterion'] == Whups_Query::CRITERION_QUEUE &&
$qobj['children'][1]['type'] == Whups_Query::TYPE_CRITERION &&
$qobj['children'][1]['criterion'] == Whups_Query::CRITERION_VERSION) {
$vars->set('queue', $qobj['children'][0]['value']);
$vars->set('version', $qobj['children'][1]['value']);
return 'props';
}
break;
}
throw new Whups_Exception(_("This query element cannot be edited."));
}
switch ($qobj['criterion']) {
case Whups_Query::CRITERION_ID:
$multiple = false;
if ($parent && $parent['type'] == Whups_Query::TYPE_OR) {
$multiple = array();
foreach ($parent['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
$child['criterion'] != Whups_Query::CRITERION_ID) {
$multiple = false;
break;
}
$multiple[] = $child['value'];
}
}
if ($multiple) {
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
$vars->set('id', implode(',', $multiple));
} else {
$vars->set('id', $qobj['value']);
}
return 'props';
case Whups_Query::CRITERION_QUEUE:
if ($parent && $parent['type'] == Whups_Query::TYPE_AND &&
count($parent['children']) == 2 &&
$parent['children'][1]['type'] == Whups_Query::TYPE_CRITERION &&
$parent['children'][1]['criterion'] == Whups_Query::CRITERION_VERSION) {
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
$vars->set('version', $parent['children'][1]['value']);
}
$vars->set('queue', $qobj['value']);
return 'props';
case Whups_Query::CRITERION_VERSION:
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
$vars->set('queue', $parent['children'][0]['value']);
$vars->set('version', $qobj['value']);
return 'props';
case Whups_Query::CRITERION_TYPE:
$vars->set('ttype', $qobj['value']);
return 'props';
case Whups_Query::CRITERION_STATE:
$vars->set('state', $qobj['value']);
return 'props';
case Whups_Query::CRITERION_PRIORITY:
$vars->set('priority', $qobj['value']);
return 'props';
case Whups_Query::CRITERION_TIMESTAMP:
case Whups_Query::CRITERION_UPDATED:
case Whups_Query::CRITERION_RESOLVED:
case Whups_Query::CRITERION_ASSIGNED:
case Whups_Query::CRITERION_DUE:
$criteria = false;
if ($parent && $parent['type'] == Whups_Query::TYPE_AND) {
foreach ($parent['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
($child['criterion'] != Whups_Query::CRITERION_TIMESTAMP &&
$child['criterion'] != Whups_Query::CRITERION_UPDATED &&
$child['criterion'] != Whups_Query::CRITERION_RESOLVED &&
$child['criterion'] != Whups_Query::CRITERION_ASSIGNED &&
$child['criterion'] != Whups_Query::CRITERION_DUE)) {
$criteria = false;
break;
}
$criteria = true;
}
}
if ($criteria) {
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
foreach ($parent['children'] as $child) {
switch ($child['criterion'] . $child['operator']) {
case Whups_Query::CRITERION_TIMESTAMP . Whups_Query::OPERATOR_GREATER:
$vars->set('ticket_timestamp[from]', $child['value']);
break;
case Whups_Query::CRITERION_TIMESTAMP . Whups_Query::OPERATOR_LESS:
$vars->set('ticket_timestamp[to]', $child['value']);
break;
case Whups_Query::CRITERION_UPDATED . Whups_Query::OPERATOR_GREATER:
$vars->set('date_updated[from]', $child['value']);
break;
case Whups_Query::CRITERION_UPDATED . Whups_Query::OPERATOR_LESS:
$vars->set('date_updated[to]', $child['value']);
break;
case Whups_Query::CRITERION_RESOLVED . Whups_Query::OPERATOR_GREATER:
$vars->set('date_resolved[from]', $child['value']);
break;
case Whups_Query::CRITERION_RESOLVED . Whups_Query::OPERATOR_LESS:
$vars->set('date_resolved[to]', $child['value']);
break;
case Whups_Query::CRITERION_ASSIGNED . Whups_Query::OPERATOR_GREATER:
$vars->set('date_assigned[from]', $child['value']);
break;
case Whups_Query::CRITERION_ASSIGNED . Whups_Query::OPERATOR_LESS:
$vars->set('date_assigned[to]', $child['value']);
break;
case Whups_Query::CRITERION_DUE . Whups_Query::OPERATOR_GREATER:
$vars->set('ticket_due[from]', $child['value']);
break;
case Whups_Query::CRITERION_DUE . Whups_Query::OPERATOR_LESS:
$vars->set('ticket_due[to]', $child['value']);
break;
}
}
}
return 'date';
case Whups_Query::CRITERION_OWNERS:
case Whups_Query::CRITERION_REQUESTER:
case Whups_Query::CRITERION_ADDED_COMMENT:
$criteria = false;
if ($parent && $parent['type'] == Whups_Query::TYPE_OR) {
$criteria = array();
foreach ($parent['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
!in_array($child['criterion'], array(Whups_Query::CRITERION_OWNERS, Whups_Query::CRITERION_REQUESTER, Whups_Query::CRITERION_ADDED_COMMENT))) {
$criteria = false;
break;
}
$criteria[] = $child['criterion'];
}
if ($criteria) {
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
}
}
if (!$criteria) {
$criteria = array($qobj['criterion']);
}
$vars->set('user', $qobj['value']);
$vars->set('operator', $qobj['operator']);
foreach ($criteria as $criterion) {
switch ($criterion) {
case Whups_Query::CRITERION_OWNERS:
$vars->set('owners', true);
break;
case Whups_Query::CRITERION_REQUESTER:
$vars->set('requester', true);
break;
case Whups_Query::CRITERION_ADDED_COMMENT:
$vars->set('comments', true);
break;
}
}
return 'user';
case Whups_Query::CRITERION_GROUPS:
$vars->set('groups', $qobj['value']);
return 'group';
case Whups_Query::CRITERION_COMMENT:
case Whups_Query::CRITERION_SUMMARY:
$criteria = false;
if ($parent && $parent['type'] == Whups_Query::TYPE_OR) {
$criteria = array();
$operator = $value = null;
foreach ($parent['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
($child['criterion'] != Whups_Query::CRITERION_COMMENT &&
$child['criterion'] != Whups_Query::CRITERION_SUMMARY) ||
(isset($operator) && $operator != $child['operator']) ||
(isset($value) && $value != $child['value'])) {
$criteria = false;
break;
}
$criteria[] = $child['criterion'];
$operator = $child['operator'];
$value = $child['value'];
}
if ($criteria) {
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
}
} else {
$operator = $qobj['operator'];
$value = $qobj['value'];
}
if (!$criteria) {
$criteria = array($qobj['criterion']);
}
$vars->set('text', $value);
$vars->set('operator', $operator);
foreach ($criteria as $criterion) {
if ($criterion == Whups_Query::CRITERION_COMMENT) {
$vars->set('comments', true);
} elseif ($criterion == Whups_Query::CRITERION_SUMMARY) {
$vars->set('summary', true);
}
}
return 'text';
case Whups_Query::CRITERION_ATTRIBUTE:
$attribs = array_keys($GLOBALS['whups_driver']->getAttributesForType());
$criteria = false;
if ($parent && $parent['type'] == Whups_Query::TYPE_OR) {
$criteria = array();
$operator = $value = null;
foreach ($parent['children'] as $child) {
if ($child['type'] != Whups_Query::TYPE_CRITERION ||
$child['criterion'] != Whups_Query::CRITERION_ATTRIBUTE ||
(isset($operator) && $operator != $child['operator']) ||
(isset($value) && $value != $child['value']) ||
!in_array($child['cvalue'], $attribs)) {
$criteria = false;
break;
}
$criteria[] = $child['cvalue'];
$operator = $child['operator'];
$value = $child['value'];
}
if ($criteria) {
array_pop($path);
$vars->set('path', Whups_Query::pathToString($path));
}
} else {
$operator = $qobj['operator'];
$value = $qobj['value'];
}
if (!$criteria) {
$criteria = array($qobj['cvalue']);
}
$vars->set('text', $value);
$vars->set('operator', $operator);
foreach ($criteria as $criterion) {
$vars->set('a' . $criterion, true);
}
return 'attribs';
}
throw new Whups_Exception(_("This query element cannot be edited."));
}
public function deleteNode($pathstring)
{
$path = Whups_Query::stringToPath($pathstring);
$qobj = &$this->query;
if (!strlen($pathstring)) {
// Deleting the root node isn't supported.
$GLOBALS['notification']->push(_("Choose New Query instead of deleting the root node."), 'horde.warning');
return false;
} else {
$count = count($path) - 1;
for ($i = 0; $i < $count; $i++) {
$qobj = &$qobj['children'][$path[$i]];
}
if (!empty($qobj['children'][$path[$count]]['value']) &&
$this->_getParameterName($qobj['children'][$path[$count]]['value']) !== null) {
unset($this->parameters[array_search($pn, $this->parameters)]);
}
array_splice($qobj['children'], $path[$count], 1);
}
}
public function hoist($pathstring)
{
$path = Whups_Query::stringToPath($pathstring);
$qobj = &$this->query;
if (!strlen($pathstring)) {
// Can't hoist the root node.
} else {
$count = count($path) - 1;
for ($i = 0; $i < $count; $i++) {
$qobj = &$qobj['children'][$path[$i]];
}
$cobj = &$qobj['children'][$path[$count]];
// TODO: make sure we're hoisting a branch.
array_splice($qobj['children'], $path[$count], 0, $cobj['children']);
array_splice($cobj['children'], 0, count($cobj['children']));
}
}
public function insertBranch($pathstring, $type)
{
$path = Whups_Query::stringToPath($pathstring);
$qobj = &$this->query;
$newbranch = array(
'type' => $type,
'children' => array());
$count = count($path);
for ($i = 0; $i < $count; $i++) {
$qobj = &$qobj['children'][$path[$i]];
}
if (!isset($qobj['children']) ||
!is_array($qobj['children'])) {
$qobj['children'] = array();
}
$qobj['children'][] = $newbranch;
$path[] = count($qobj['children']) - 1;
return Whups_Query::pathToString($path);
}
public function insertCriterion($pathstring, $criterion, $cvalue, $operator, $value)
{
$path = Whups_Query::stringToPath($pathstring);
$qobj = &$this->query;
$value = trim($value);
if ($value[0] == '"') {
// FIXME: The last character should be '"' as well.
$value = substr($value, 1, -1);
} else {
$pn = $this->_getParameterName($value);
if ($pn !== null) {
$this->parameters[] = $pn;
}
}
$newbranch = array(
'type' => Whups_Query::TYPE_CRITERION,
'criterion' => $criterion,
'cvalue' => $cvalue,
'operator' => $operator,
'value' => $value);
$count = count($path);
for ($i = 0; $i < $count; $i++) {
$qobj = &$qobj['children'][$path[$i]];
}
$qobj['children'][] = $newbranch;
}
/**
* Top down traversal.
*/
public function walk(&$obj, $method)
{
$path = array();
$more = array();
$this->_walk($this->query, $more, $path, $obj, $method);
}
/**
* @access private
*/
protected function _walk(&$node, &$more, &$path, &$obj, $method)
{
if ($node['type'] == Whups_Query::TYPE_CRITERION) {
$obj->$method($more, $path, Whups_Query::TYPE_CRITERION, $node['criterion'],
$node['cvalue'], $node['operator'], $node['value']);
} else {
$obj->$method($more, $path, $node['type'], null, null, null, null);
}
if (isset($node['children'])) {
$count = count($node['children']);
for ($i = 0; $i < $count; $i++) {
$path[] = $i;
$more[] = ($i < $count - 1);
$this->_walk($node['children'][$i], $more, $path, $obj, $method);
array_pop($more);
array_pop($path);
}
}
}
/**
* Bottom up traversal.
*/
public function reduce($method, &$vars)
{
return $this->_reduce($this->query, $method, $vars);
}
/**
* @access private
*/
protected function _reduce(&$node, $method, &$vars)
{
$args = array();
if (isset($node['children'])) {
$count = count($node['children']);
for ($i = 0; $i < $count; $i++) {
$result = $this->_reduce($node['children'][$i], $method, $vars);
$args[] = $result;
}
}
if ($node['type'] == Whups_Query::TYPE_CRITERION) {
$value = $node['value'];
$pn = $this->_getParameterName($value);
if ($pn !== null) {
$value = $vars->get($pn);
}
return call_user_func(
$method, $args, Whups_Query::TYPE_CRITERION, $node['criterion'],
$node['cvalue'], $node['operator'], $value);
}
return call_user_func($method, $args, $node['type'], null, null, null, null);
}
/**
* @access private
*/
protected function _getParameterName($value)
{
if (strcmp(substr($value, 0, 2), '${'))
return null;
$pn = substr($value, 2, -1);
if (!is_string($pn))
$pn = null;
return $pn;
}
}
|