1: <?php
2: /**
3: * Ingo_Storage_Filters is the object used to hold user-defined filtering rule
4: * information.
5: *
6: * See the enclosed file LICENSE for license information (ASL). If you
7: * did not receive this file, see http://www.horde.org/licenses/apache.
8: *
9: * @author Michael Slusarz <slusarz@horde.org>
10: * @package Ingo
11: */
12: class Ingo_Storage_Filters
13: {
14: /**
15: * The filter list.
16: *
17: * @var array
18: */
19: protected $_filters = array();
20:
21: /**
22: * The object type.
23: *
24: * @var integer
25: */
26: protected $_obtype = Ingo_Storage::ACTION_FILTERS;
27:
28: /**
29: * Returns the object rule type.
30: *
31: * @return integer The object rule type.
32: */
33: public function obType()
34: {
35: return $this->_obtype;
36: }
37:
38: /**
39: * Propagates the filter list with data.
40: *
41: * @param array $data A list of rule hashes.
42: */
43: public function setFilterlist($data)
44: {
45: $this->_filters = $data;
46: }
47:
48: /**
49: * Returns the filter list.
50: *
51: * @return array The list of rule hashes.
52: */
53: public function getFilterList()
54: {
55: return $this->_filters;
56: }
57:
58: /**
59: * Return the filter entry for a given ID.
60: *
61: * @return mixed The rule hash entry, or false if not defined.
62: */
63: public function getFilter($id)
64: {
65: return isset($this->_filters[$id])
66: ? $this->_filters[$id]
67: : false;
68: }
69:
70: /**
71: * Returns a single rule hash.
72: *
73: * @param integer $id A rule number.
74: *
75: * @return array The requested rule hash.
76: */
77: public function getRule($id)
78: {
79: return $this->_filters[$id];
80: }
81:
82: /**
83: * Returns a rule hash with default value used when creating new rules.
84: *
85: * @return array A rule hash.
86: */
87: public function getDefaultRule()
88: {
89: return array(
90: 'name' => _("New Rule"),
91: 'combine' => Ingo_Storage::COMBINE_ALL,
92: 'conditions' => array(),
93: 'action' => Ingo_Storage::ACTION_KEEP,
94: 'action-value' => '',
95: 'stop' => true,
96: 'flags' => 0,
97: 'disable' => false
98: );
99: }
100:
101: /**
102: * Searches for the first rule of a certain action type and returns its
103: * number.
104: *
105: * @param integer $action The field type of the searched rule
106: * (ACTION_* constants).
107: *
108: * @return integer The number of the first matching rule or null.
109: */
110: public function findRuleId($action)
111: {
112: foreach ($this->_filters as $id => $rule) {
113: if ($rule['action'] == $action) {
114: return $id;
115: }
116: }
117: }
118:
119: /**
120: * Searches for and returns the first rule of a certain action type.
121: *
122: * @param integer $action The field type of the searched rule
123: * (ACTION_* constants).
124: *
125: * @return array The first matching rule hash or null.
126: */
127: public function findRule($action)
128: {
129: $id = $this->findRuleId($action);
130: if ($id !== null) {
131: return $this->getRule($id);
132: }
133: }
134:
135: /**
136: * Adds a rule hash to the filters list.
137: *
138: * @param array $rule A rule hash.
139: * @param boolean $default If true merge the rule hash with default rule
140: * values.
141: */
142: public function addRule($rule, $default = true)
143: {
144: if ($default) {
145: $this->_filters[] = array_merge($this->getDefaultRule(), $rule);
146: } else {
147: $this->_filters[] = $rule;
148: }
149: }
150:
151: /**
152: * Updates an existing rule with a rule hash.
153: *
154: * @param array $rule A rule hash
155: * @param integer $id A rule number
156: */
157: public function updateRule($rule, $id)
158: {
159: $this->_filters[$id] = $rule;
160: }
161:
162: /**
163: * Deletes a rule from the filters list.
164: *
165: * @param integer $id Number of the rule to delete.
166: *
167: * @return boolean True if the rule has been found and deleted.
168: */
169: public function deleteRule($id)
170: {
171: if (isset($this->_filters[$id])) {
172: unset($this->_filters[$id]);
173: $this->_filters = array_values($this->_filters);
174: return true;
175: }
176:
177: return false;
178: }
179:
180: /**
181: * Creates a copy of an existing rule.
182: *
183: * The created copy is added to the filters list right after the original
184: * rule.
185: *
186: * @param integer $id Number of the rule to copy.
187: *
188: * @return boolean True if the rule has been found and copied.
189: */
190: public function copyRule($id)
191: {
192: if (isset($this->_filters[$id])) {
193: $newrule = $this->_filters[$id];
194: $newrule['name'] = sprintf(_("Copy of %s"), $this->_filters[$id]['name']);
195: $this->_filters = array_merge(array_slice($this->_filters, 0, $id + 1), array($newrule), array_slice($this->_filters, $id + 1));
196: return true;
197: }
198:
199: return false;
200: }
201:
202: /**
203: * Moves a rule up in the filters list.
204: *
205: * @param integer $id Number of the rule to move.
206: * @param integer $steps Number of positions to move the rule up.
207: */
208: public function ruleUp($id, $steps = 1)
209: {
210: for ($i = 0; $i < $steps && $id > 0;) {
211: $temp = $this->_filters[$id - 1];
212: $this->_filters[$id - 1] = $this->_filters[$id];
213: $this->_filters[$id] = $temp;
214: /* Continue to move up until we swap with a viewable category. */
215: if (in_array($temp['action'], $GLOBALS['session']->get('ingo', 'script_categories'))) {
216: $i++;
217: }
218: $id--;
219: }
220: }
221:
222: /**
223: * Moves a rule down in the filters list.
224: *
225: * @param integer $id Number of the rule to move.
226: * @param integer $steps Number of positions to move the rule down.
227: */
228: public function ruleDown($id, $steps = 1)
229: {
230: $rulecount = count($this->_filters) - 1;
231: for ($i = 0; $i < $steps && $id < $rulecount;) {
232: $temp = $this->_filters[$id + 1];
233: $this->_filters[$id + 1] = $this->_filters[$id];
234: $this->_filters[$id] = $temp;
235: /* Continue to move down until we swap with a viewable
236: category. */
237: if (in_array($temp['action'], $GLOBALS['session']->get('ingo', 'script_categories'))) {
238: $i++;
239: }
240: $id++;
241: }
242: }
243:
244: /**
245: * Disables a rule.
246: *
247: * @param integer $id Number of the rule to disable.
248: */
249: public function ruleDisable($id)
250: {
251: $this->_filters[$id]['disable'] = true;
252: }
253:
254: /**
255: * Enables a rule.
256: *
257: * @param integer $id Number of the rule to enable.
258: */
259: public function ruleEnable($id)
260: {
261: $this->_filters[$id]['disable'] = false;
262: }
263:
264: }
265: