1: <?php
2: /**
3: * PHPExcel
4: *
5: * Copyright (c) 2006 - 2014 PHPExcel
6: *
7: * This library is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU Lesser General Public
9: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20: *
21: * @category PHPExcel
22: * @package PHPExcel_Worksheet
23: * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24: * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25: * @version 1.8.0, 2014-03-02
26: */
27:
28:
29: /**
30: * PHPExcel_Worksheet_AutoFilter_Column
31: *
32: * @category PHPExcel
33: * @package PHPExcel_Worksheet
34: * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
35: */
36: class PHPExcel_Worksheet_AutoFilter_Column
37: {
38: const AUTOFILTER_FILTERTYPE_FILTER = 'filters';
39: const AUTOFILTER_FILTERTYPE_CUSTOMFILTER = 'customFilters';
40: // Supports no more than 2 rules, with an And/Or join criteria
41: // if more than 1 rule is defined
42: const AUTOFILTER_FILTERTYPE_DYNAMICFILTER = 'dynamicFilter';
43: // Even though the filter rule is constant, the filtered data can vary
44: // e.g. filtered by date = TODAY
45: const AUTOFILTER_FILTERTYPE_TOPTENFILTER = 'top10';
46:
47: /**
48: * Types of autofilter rules
49: *
50: * @var string[]
51: */
52: private static $_filterTypes = array(
53: // Currently we're not handling
54: // colorFilter
55: // extLst
56: // iconFilter
57: self::AUTOFILTER_FILTERTYPE_FILTER,
58: self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER,
59: self::AUTOFILTER_FILTERTYPE_DYNAMICFILTER,
60: self::AUTOFILTER_FILTERTYPE_TOPTENFILTER,
61: );
62:
63: /* Multiple Rule Connections */
64: const AUTOFILTER_COLUMN_JOIN_AND = 'and';
65: const AUTOFILTER_COLUMN_JOIN_OR = 'or';
66:
67: /**
68: * Join options for autofilter rules
69: *
70: * @var string[]
71: */
72: private static $_ruleJoins = array(
73: self::AUTOFILTER_COLUMN_JOIN_AND,
74: self::AUTOFILTER_COLUMN_JOIN_OR,
75: );
76:
77: /**
78: * Autofilter
79: *
80: * @var PHPExcel_Worksheet_AutoFilter
81: */
82: private $_parent = NULL;
83:
84:
85: /**
86: * Autofilter Column Index
87: *
88: * @var string
89: */
90: private $_columnIndex = '';
91:
92:
93: /**
94: * Autofilter Column Filter Type
95: *
96: * @var string
97: */
98: private $_filterType = self::AUTOFILTER_FILTERTYPE_FILTER;
99:
100:
101: /**
102: * Autofilter Multiple Rules And/Or
103: *
104: * @var string
105: */
106: private $_join = self::AUTOFILTER_COLUMN_JOIN_OR;
107:
108:
109: /**
110: * Autofilter Column Rules
111: *
112: * @var array of PHPExcel_Worksheet_AutoFilter_Column_Rule
113: */
114: private $_ruleset = array();
115:
116:
117: /**
118: * Autofilter Column Dynamic Attributes
119: *
120: * @var array of mixed
121: */
122: private $_attributes = array();
123:
124:
125: /**
126: * Create a new PHPExcel_Worksheet_AutoFilter_Column
127: *
128: * @param string $pColumn Column (e.g. A)
129: * @param PHPExcel_Worksheet_AutoFilter $pParent Autofilter for this column
130: */
131: public function __construct($pColumn, PHPExcel_Worksheet_AutoFilter $pParent = NULL)
132: {
133: $this->_columnIndex = $pColumn;
134: $this->_parent = $pParent;
135: }
136:
137: /**
138: * Get AutoFilter Column Index
139: *
140: * @return string
141: */
142: public function getColumnIndex() {
143: return $this->_columnIndex;
144: }
145:
146: /**
147: * Set AutoFilter Column Index
148: *
149: * @param string $pColumn Column (e.g. A)
150: * @throws PHPExcel_Exception
151: * @return PHPExcel_Worksheet_AutoFilter_Column
152: */
153: public function setColumnIndex($pColumn) {
154: // Uppercase coordinate
155: $pColumn = strtoupper($pColumn);
156: if ($this->_parent !== NULL) {
157: $this->_parent->testColumnInRange($pColumn);
158: }
159:
160: $this->_columnIndex = $pColumn;
161:
162: return $this;
163: }
164:
165: /**
166: * Get this Column's AutoFilter Parent
167: *
168: * @return PHPExcel_Worksheet_AutoFilter
169: */
170: public function getParent() {
171: return $this->_parent;
172: }
173:
174: /**
175: * Set this Column's AutoFilter Parent
176: *
177: * @param PHPExcel_Worksheet_AutoFilter
178: * @return PHPExcel_Worksheet_AutoFilter_Column
179: */
180: public function setParent(PHPExcel_Worksheet_AutoFilter $pParent = NULL) {
181: $this->_parent = $pParent;
182:
183: return $this;
184: }
185:
186: /**
187: * Get AutoFilter Type
188: *
189: * @return string
190: */
191: public function getFilterType() {
192: return $this->_filterType;
193: }
194:
195: /**
196: * Set AutoFilter Type
197: *
198: * @param string $pFilterType
199: * @throws PHPExcel_Exception
200: * @return PHPExcel_Worksheet_AutoFilter_Column
201: */
202: public function setFilterType($pFilterType = self::AUTOFILTER_FILTERTYPE_FILTER) {
203: if (!in_array($pFilterType,self::$_filterTypes)) {
204: throw new PHPExcel_Exception('Invalid filter type for column AutoFilter.');
205: }
206:
207: $this->_filterType = $pFilterType;
208:
209: return $this;
210: }
211:
212: /**
213: * Get AutoFilter Multiple Rules And/Or Join
214: *
215: * @return string
216: */
217: public function getJoin() {
218: return $this->_join;
219: }
220:
221: /**
222: * Set AutoFilter Multiple Rules And/Or
223: *
224: * @param string $pJoin And/Or
225: * @throws PHPExcel_Exception
226: * @return PHPExcel_Worksheet_AutoFilter_Column
227: */
228: public function setJoin($pJoin = self::AUTOFILTER_COLUMN_JOIN_OR) {
229: // Lowercase And/Or
230: $pJoin = strtolower($pJoin);
231: if (!in_array($pJoin,self::$_ruleJoins)) {
232: throw new PHPExcel_Exception('Invalid rule connection for column AutoFilter.');
233: }
234:
235: $this->_join = $pJoin;
236:
237: return $this;
238: }
239:
240: /**
241: * Set AutoFilter Attributes
242: *
243: * @param string[] $pAttributes
244: * @throws PHPExcel_Exception
245: * @return PHPExcel_Worksheet_AutoFilter_Column
246: */
247: public function setAttributes($pAttributes = array()) {
248: $this->_attributes = $pAttributes;
249:
250: return $this;
251: }
252:
253: /**
254: * Set An AutoFilter Attribute
255: *
256: * @param string $pName Attribute Name
257: * @param string $pValue Attribute Value
258: * @throws PHPExcel_Exception
259: * @return PHPExcel_Worksheet_AutoFilter_Column
260: */
261: public function setAttribute($pName, $pValue) {
262: $this->_attributes[$pName] = $pValue;
263:
264: return $this;
265: }
266:
267: /**
268: * Get AutoFilter Column Attributes
269: *
270: * @return string
271: */
272: public function getAttributes() {
273: return $this->_attributes;
274: }
275:
276: /**
277: * Get specific AutoFilter Column Attribute
278: *
279: * @param string $pName Attribute Name
280: * @return string
281: */
282: public function getAttribute($pName) {
283: if (isset($this->_attributes[$pName]))
284: return $this->_attributes[$pName];
285: return NULL;
286: }
287:
288: /**
289: * Get all AutoFilter Column Rules
290: *
291: * @throws PHPExcel_Exception
292: * @return array of PHPExcel_Worksheet_AutoFilter_Column_Rule
293: */
294: public function getRules() {
295: return $this->_ruleset;
296: }
297:
298: /**
299: * Get a specified AutoFilter Column Rule
300: *
301: * @param integer $pIndex Rule index in the ruleset array
302: * @return PHPExcel_Worksheet_AutoFilter_Column_Rule
303: */
304: public function getRule($pIndex) {
305: if (!isset($this->_ruleset[$pIndex])) {
306: $this->_ruleset[$pIndex] = new PHPExcel_Worksheet_AutoFilter_Column_Rule($this);
307: }
308: return $this->_ruleset[$pIndex];
309: }
310:
311: /**
312: * Create a new AutoFilter Column Rule in the ruleset
313: *
314: * @return PHPExcel_Worksheet_AutoFilter_Column_Rule
315: */
316: public function createRule() {
317: $this->_ruleset[] = new PHPExcel_Worksheet_AutoFilter_Column_Rule($this);
318:
319: return end($this->_ruleset);
320: }
321:
322: /**
323: * Add a new AutoFilter Column Rule to the ruleset
324: *
325: * @param PHPExcel_Worksheet_AutoFilter_Column_Rule $pRule
326: * @param boolean $returnRule Flag indicating whether the rule object or the column object should be returned
327: * @return PHPExcel_Worksheet_AutoFilter_Column|PHPExcel_Worksheet_AutoFilter_Column_Rule
328: */
329: public function addRule(PHPExcel_Worksheet_AutoFilter_Column_Rule $pRule, $returnRule=TRUE) {
330: $pRule->setParent($this);
331: $this->_ruleset[] = $pRule;
332:
333: return ($returnRule) ? $pRule : $this;
334: }
335:
336: /**
337: * Delete a specified AutoFilter Column Rule
338: * If the number of rules is reduced to 1, then we reset And/Or logic to Or
339: *
340: * @param integer $pIndex Rule index in the ruleset array
341: * @return PHPExcel_Worksheet_AutoFilter_Column
342: */
343: public function deleteRule($pIndex) {
344: if (isset($this->_ruleset[$pIndex])) {
345: unset($this->_ruleset[$pIndex]);
346: // If we've just deleted down to a single rule, then reset And/Or joining to Or
347: if (count($this->_ruleset) <= 1) {
348: $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
349: }
350: }
351:
352: return $this;
353: }
354:
355: /**
356: * Delete all AutoFilter Column Rules
357: *
358: * @return PHPExcel_Worksheet_AutoFilter_Column
359: */
360: public function clearRules() {
361: $this->_ruleset = array();
362: $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
363:
364: return $this;
365: }
366:
367: /**
368: * Implement PHP __clone to create a deep clone, not just a shallow copy.
369: */
370: public function __clone() {
371: $vars = get_object_vars($this);
372: foreach ($vars as $key => $value) {
373: if (is_object($value)) {
374: if ($key == '_parent') {
375: // Detach from autofilter parent
376: $this->$key = NULL;
377: } else {
378: $this->$key = clone $value;
379: }
380: } elseif ((is_array($value)) && ($key == '_ruleset')) {
381: // The columns array of PHPExcel_Worksheet_AutoFilter objects
382: $this->$key = array();
383: foreach ($value as $k => $v) {
384: $this->$key[$k] = clone $v;
385: // attach the new cloned Rule to this new cloned Autofilter Cloned object
386: $this->$key[$k]->setParent($this);
387: }
388: } else {
389: $this->$key = $value;
390: }
391: }
392: }
393:
394: }
395: