4 * PHPExcel_Worksheet_AutoFilter_Column
6 * Copyright (c) 2006 - 2015 PHPExcel
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * @package PHPExcel_Worksheet
24 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26 * @version ##VERSION##, ##DATE##
28 class PHPExcel_Worksheet_AutoFilter_Column
30 const AUTOFILTER_FILTERTYPE_FILTER = 'filters';
31 const AUTOFILTER_FILTERTYPE_CUSTOMFILTER = 'customFilters';
32 // Supports no more than 2 rules, with an And/Or join criteria
33 // if more than 1 rule is defined
34 const AUTOFILTER_FILTERTYPE_DYNAMICFILTER = 'dynamicFilter';
35 // Even though the filter rule is constant, the filtered data can vary
36 // e.g. filtered by date = TODAY
37 const AUTOFILTER_FILTERTYPE_TOPTENFILTER = 'top10';
40 * Types of autofilter rules
44 private static $filterTypes = array(
45 // Currently we're not handling
49 self::AUTOFILTER_FILTERTYPE_FILTER,
50 self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER,
51 self::AUTOFILTER_FILTERTYPE_DYNAMICFILTER,
52 self::AUTOFILTER_FILTERTYPE_TOPTENFILTER,
55 /* Multiple Rule Connections */
56 const AUTOFILTER_COLUMN_JOIN_AND = 'and';
57 const AUTOFILTER_COLUMN_JOIN_OR = 'or';
60 * Join options for autofilter rules
64 private static $ruleJoins = array(
65 self::AUTOFILTER_COLUMN_JOIN_AND,
66 self::AUTOFILTER_COLUMN_JOIN_OR,
72 * @var PHPExcel_Worksheet_AutoFilter
78 * Autofilter Column Index
82 private $columnIndex = '';
86 * Autofilter Column Filter Type
90 private $filterType = self::AUTOFILTER_FILTERTYPE_FILTER;
94 * Autofilter Multiple Rules And/Or
98 private $join = self::AUTOFILTER_COLUMN_JOIN_OR;
102 * Autofilter Column Rules
104 * @var array of PHPExcel_Worksheet_AutoFilter_Column_Rule
106 private $ruleset = array();
110 * Autofilter Column Dynamic Attributes
112 * @var array of mixed
114 private $attributes = array();
118 * Create a new PHPExcel_Worksheet_AutoFilter_Column
120 * @param string $pColumn Column (e.g. A)
121 * @param PHPExcel_Worksheet_AutoFilter $pParent Autofilter for this column
123 public function __construct($pColumn, PHPExcel_Worksheet_AutoFilter $pParent = null)
125 $this->columnIndex = $pColumn;
126 $this->parent = $pParent;
130 * Get AutoFilter Column Index
134 public function getColumnIndex()
136 return $this->columnIndex;
140 * Set AutoFilter Column Index
142 * @param string $pColumn Column (e.g. A)
143 * @throws PHPExcel_Exception
144 * @return PHPExcel_Worksheet_AutoFilter_Column
146 public function setColumnIndex($pColumn)
148 // Uppercase coordinate
149 $pColumn = strtoupper($pColumn);
150 if ($this->parent !== null) {
151 $this->parent->testColumnInRange($pColumn);
154 $this->columnIndex = $pColumn;
160 * Get this Column's AutoFilter Parent
162 * @return PHPExcel_Worksheet_AutoFilter
164 public function getParent()
166 return $this->parent;
170 * Set this Column's AutoFilter Parent
172 * @param PHPExcel_Worksheet_AutoFilter
173 * @return PHPExcel_Worksheet_AutoFilter_Column
175 public function setParent(PHPExcel_Worksheet_AutoFilter $pParent = null)
177 $this->parent = $pParent;
183 * Get AutoFilter Type
187 public function getFilterType()
189 return $this->filterType;
193 * Set AutoFilter Type
195 * @param string $pFilterType
196 * @throws PHPExcel_Exception
197 * @return PHPExcel_Worksheet_AutoFilter_Column
199 public function setFilterType($pFilterType = self::AUTOFILTER_FILTERTYPE_FILTER)
201 if (!in_array($pFilterType, self::$filterTypes)) {
202 throw new PHPExcel_Exception('Invalid filter type for column AutoFilter.');
205 $this->filterType = $pFilterType;
211 * Get AutoFilter Multiple Rules And/Or Join
215 public function getJoin()
221 * Set AutoFilter Multiple Rules And/Or
223 * @param string $pJoin And/Or
224 * @throws PHPExcel_Exception
225 * @return PHPExcel_Worksheet_AutoFilter_Column
227 public function setJoin($pJoin = self::AUTOFILTER_COLUMN_JOIN_OR)
230 $pJoin = strtolower($pJoin);
231 if (!in_array($pJoin, self::$ruleJoins)) {
232 throw new PHPExcel_Exception('Invalid rule connection for column AutoFilter.');
235 $this->join = $pJoin;
241 * Set AutoFilter Attributes
243 * @param string[] $pAttributes
244 * @throws PHPExcel_Exception
245 * @return PHPExcel_Worksheet_AutoFilter_Column
247 public function setAttributes($pAttributes = array())
249 $this->attributes = $pAttributes;
255 * Set An AutoFilter Attribute
257 * @param string $pName Attribute Name
258 * @param string $pValue Attribute Value
259 * @throws PHPExcel_Exception
260 * @return PHPExcel_Worksheet_AutoFilter_Column
262 public function setAttribute($pName, $pValue)
264 $this->attributes[$pName] = $pValue;
270 * Get AutoFilter Column Attributes
274 public function getAttributes()
276 return $this->attributes;
280 * Get specific AutoFilter Column Attribute
282 * @param string $pName Attribute Name
285 public function getAttribute($pName)
287 if (isset($this->attributes[$pName])) {
288 return $this->attributes[$pName];
294 * Get all AutoFilter Column Rules
296 * @throws PHPExcel_Exception
297 * @return array of PHPExcel_Worksheet_AutoFilter_Column_Rule
299 public function getRules()
301 return $this->ruleset;
305 * Get a specified AutoFilter Column Rule
307 * @param integer $pIndex Rule index in the ruleset array
308 * @return PHPExcel_Worksheet_AutoFilter_Column_Rule
310 public function getRule($pIndex)
312 if (!isset($this->ruleset[$pIndex])) {
313 $this->ruleset[$pIndex] = new PHPExcel_Worksheet_AutoFilter_Column_Rule($this);
315 return $this->ruleset[$pIndex];
319 * Create a new AutoFilter Column Rule in the ruleset
321 * @return PHPExcel_Worksheet_AutoFilter_Column_Rule
323 public function createRule()
325 $this->ruleset[] = new PHPExcel_Worksheet_AutoFilter_Column_Rule($this);
327 return end($this->ruleset);
331 * Add a new AutoFilter Column Rule to the ruleset
333 * @param PHPExcel_Worksheet_AutoFilter_Column_Rule $pRule
334 * @param boolean $returnRule Flag indicating whether the rule object or the column object should be returned
335 * @return PHPExcel_Worksheet_AutoFilter_Column|PHPExcel_Worksheet_AutoFilter_Column_Rule
337 public function addRule(PHPExcel_Worksheet_AutoFilter_Column_Rule $pRule, $returnRule = true)
339 $pRule->setParent($this);
340 $this->ruleset[] = $pRule;
342 return ($returnRule) ? $pRule : $this;
346 * Delete a specified AutoFilter Column Rule
347 * If the number of rules is reduced to 1, then we reset And/Or logic to Or
349 * @param integer $pIndex Rule index in the ruleset array
350 * @return PHPExcel_Worksheet_AutoFilter_Column
352 public function deleteRule($pIndex)
354 if (isset($this->ruleset[$pIndex])) {
355 unset($this->ruleset[$pIndex]);
356 // If we've just deleted down to a single rule, then reset And/Or joining to Or
357 if (count($this->ruleset) <= 1) {
358 $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
366 * Delete all AutoFilter Column Rules
368 * @return PHPExcel_Worksheet_AutoFilter_Column
370 public function clearRules()
372 $this->ruleset = array();
373 $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
379 * Implement PHP __clone to create a deep clone, not just a shallow copy.
381 public function __clone()
383 $vars = get_object_vars($this);
384 foreach ($vars as $key => $value) {
385 if (is_object($value)) {
386 if ($key == 'parent') {
387 // Detach from autofilter parent
390 $this->$key = clone $value;
392 } elseif ((is_array($value)) && ($key == 'ruleset')) {
393 // The columns array of PHPExcel_Worksheet_AutoFilter objects
394 $this->$key = array();
395 foreach ($value as $k => $v) {
396 $this->$key[$k] = clone $v;
397 // attach the new cloned Rule to this new cloned Autofilter Cloned object
398 $this->$key[$k]->setParent($this);
401 $this->$key = $value;