1: <?php
2: 3: 4:
5: class Whups_Form_Query_AttributeCriterion extends Horde_Form
6: {
7: 8: 9:
10: public $attribs = array();
11:
12: public function __construct(&$vars)
13: {
14: global $whups_driver;
15:
16: parent::__construct(
17: $vars,
18: $vars->get('edit')
19: ? _("Edit Attribute Criterion")
20: : _("Add Attribute Criterion"),
21: 'Whups_Form_Query_AttributeCriterion');
22:
23: $this->addHidden('', 'edit', 'boolean', false);
24:
25: try {
26: $this->attribs = $whups_driver->getAttributesForType();
27: if ($this->attribs) {
28: $this->addVariable(_("Match"), 'text', 'text', true);
29: $this->addVariable(
30: _("Match Operator"), 'operator', 'enum', true, false, null,
31: array(Whups_Query::textOperators()));
32:
33: foreach ($this->attribs as $id => $attribute) {
34: $this->addVariable(
35: sprintf(_("Search %s Attribute"), $attribute['human_name']),
36: "a$id", 'boolean', false);
37: }
38: } else {
39: $this->addVariable(
40: _("Search Attribute"), 'attribute', 'invalid', true, false,
41: null, array(_("There are no attributes defined.")));
42: }
43: } catch (Whups_Exception $e) {
44: $this->addVariable(
45: _("Search Attribute"), 'attribute', 'invalid', true, false,
46: null, array($e->getMessage()));
47: }
48: }
49:
50: public function execute(&$vars)
51: {
52: $path = $vars->get('path');
53: $text = $vars->get('text');
54: $operator = $vars->get('operator');
55:
56: $count = 0;
57:
58: $keys = array_keys($this->attribs);
59: foreach ($keys as $id) {
60: $count += $vars->exists("a$id") ? 1 : 0;
61: }
62:
63: if ($count > 1) {
64: $path = $GLOBALS['whups_query']->insertBranch($path, Whups_Query::TYPE_OR);
65: }
66:
67: foreach ($keys as $id) {
68: if ($vars->get("a$id")) {
69: $GLOBALS['whups_query']->insertCriterion(
70: $path, Whups_Query::CRITERION_ATTRIBUTE, $id, $operator, $text);
71: }
72: }
73:
74: $this->unsetVars($vars);
75: }
76: }
77: