PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpexcel/PHPExcel/Worksheet/AutoFilter/Column.php

http://github.com/moodle/moodle
PHP | 405 lines | 158 code | 49 blank | 198 comment | 14 complexity | 31191381a7b51873c16398178077de0a MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel_Worksheet_AutoFilter_Column
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Worksheet_AutoFilter_Column
  28. {
  29. const AUTOFILTER_FILTERTYPE_FILTER = 'filters';
  30. const AUTOFILTER_FILTERTYPE_CUSTOMFILTER = 'customFilters';
  31. // Supports no more than 2 rules, with an And/Or join criteria
  32. // if more than 1 rule is defined
  33. const AUTOFILTER_FILTERTYPE_DYNAMICFILTER = 'dynamicFilter';
  34. // Even though the filter rule is constant, the filtered data can vary
  35. // e.g. filtered by date = TODAY
  36. const AUTOFILTER_FILTERTYPE_TOPTENFILTER = 'top10';
  37. /**
  38. * Types of autofilter rules
  39. *
  40. * @var string[]
  41. */
  42. private static $filterTypes = array(
  43. // Currently we're not handling
  44. // colorFilter
  45. // extLst
  46. // iconFilter
  47. self::AUTOFILTER_FILTERTYPE_FILTER,
  48. self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER,
  49. self::AUTOFILTER_FILTERTYPE_DYNAMICFILTER,
  50. self::AUTOFILTER_FILTERTYPE_TOPTENFILTER,
  51. );
  52. /* Multiple Rule Connections */
  53. const AUTOFILTER_COLUMN_JOIN_AND = 'and';
  54. const AUTOFILTER_COLUMN_JOIN_OR = 'or';
  55. /**
  56. * Join options for autofilter rules
  57. *
  58. * @var string[]
  59. */
  60. private static $ruleJoins = array(
  61. self::AUTOFILTER_COLUMN_JOIN_AND,
  62. self::AUTOFILTER_COLUMN_JOIN_OR,
  63. );
  64. /**
  65. * Autofilter
  66. *
  67. * @var PHPExcel_Worksheet_AutoFilter
  68. */
  69. private $parent;
  70. /**
  71. * Autofilter Column Index
  72. *
  73. * @var string
  74. */
  75. private $columnIndex = '';
  76. /**
  77. * Autofilter Column Filter Type
  78. *
  79. * @var string
  80. */
  81. private $filterType = self::AUTOFILTER_FILTERTYPE_FILTER;
  82. /**
  83. * Autofilter Multiple Rules And/Or
  84. *
  85. * @var string
  86. */
  87. private $join = self::AUTOFILTER_COLUMN_JOIN_OR;
  88. /**
  89. * Autofilter Column Rules
  90. *
  91. * @var array of PHPExcel_Worksheet_AutoFilter_Column_Rule
  92. */
  93. private $ruleset = array();
  94. /**
  95. * Autofilter Column Dynamic Attributes
  96. *
  97. * @var array of mixed
  98. */
  99. private $attributes = array();
  100. /**
  101. * Create a new PHPExcel_Worksheet_AutoFilter_Column
  102. *
  103. * @param string $pColumn Column (e.g. A)
  104. * @param PHPExcel_Worksheet_AutoFilter $pParent Autofilter for this column
  105. */
  106. public function __construct($pColumn, PHPExcel_Worksheet_AutoFilter $pParent = null)
  107. {
  108. $this->columnIndex = $pColumn;
  109. $this->parent = $pParent;
  110. }
  111. /**
  112. * Get AutoFilter Column Index
  113. *
  114. * @return string
  115. */
  116. public function getColumnIndex()
  117. {
  118. return $this->columnIndex;
  119. }
  120. /**
  121. * Set AutoFilter Column Index
  122. *
  123. * @param string $pColumn Column (e.g. A)
  124. * @throws PHPExcel_Exception
  125. * @return PHPExcel_Worksheet_AutoFilter_Column
  126. */
  127. public function setColumnIndex($pColumn)
  128. {
  129. // Uppercase coordinate
  130. $pColumn = strtoupper($pColumn);
  131. if ($this->parent !== null) {
  132. $this->parent->testColumnInRange($pColumn);
  133. }
  134. $this->columnIndex = $pColumn;
  135. return $this;
  136. }
  137. /**
  138. * Get this Column's AutoFilter Parent
  139. *
  140. * @return PHPExcel_Worksheet_AutoFilter
  141. */
  142. public function getParent()
  143. {
  144. return $this->parent;
  145. }
  146. /**
  147. * Set this Column's AutoFilter Parent
  148. *
  149. * @param PHPExcel_Worksheet_AutoFilter
  150. * @return PHPExcel_Worksheet_AutoFilter_Column
  151. */
  152. public function setParent(PHPExcel_Worksheet_AutoFilter $pParent = null)
  153. {
  154. $this->parent = $pParent;
  155. return $this;
  156. }
  157. /**
  158. * Get AutoFilter Type
  159. *
  160. * @return string
  161. */
  162. public function getFilterType()
  163. {
  164. return $this->filterType;
  165. }
  166. /**
  167. * Set AutoFilter Type
  168. *
  169. * @param string $pFilterType
  170. * @throws PHPExcel_Exception
  171. * @return PHPExcel_Worksheet_AutoFilter_Column
  172. */
  173. public function setFilterType($pFilterType = self::AUTOFILTER_FILTERTYPE_FILTER)
  174. {
  175. if (!in_array($pFilterType, self::$filterTypes)) {
  176. throw new PHPExcel_Exception('Invalid filter type for column AutoFilter.');
  177. }
  178. $this->filterType = $pFilterType;
  179. return $this;
  180. }
  181. /**
  182. * Get AutoFilter Multiple Rules And/Or Join
  183. *
  184. * @return string
  185. */
  186. public function getJoin()
  187. {
  188. return $this->join;
  189. }
  190. /**
  191. * Set AutoFilter Multiple Rules And/Or
  192. *
  193. * @param string $pJoin And/Or
  194. * @throws PHPExcel_Exception
  195. * @return PHPExcel_Worksheet_AutoFilter_Column
  196. */
  197. public function setJoin($pJoin = self::AUTOFILTER_COLUMN_JOIN_OR)
  198. {
  199. // Lowercase And/Or
  200. $pJoin = strtolower($pJoin);
  201. if (!in_array($pJoin, self::$ruleJoins)) {
  202. throw new PHPExcel_Exception('Invalid rule connection for column AutoFilter.');
  203. }
  204. $this->join = $pJoin;
  205. return $this;
  206. }
  207. /**
  208. * Set AutoFilter Attributes
  209. *
  210. * @param string[] $pAttributes
  211. * @throws PHPExcel_Exception
  212. * @return PHPExcel_Worksheet_AutoFilter_Column
  213. */
  214. public function setAttributes($pAttributes = array())
  215. {
  216. $this->attributes = $pAttributes;
  217. return $this;
  218. }
  219. /**
  220. * Set An AutoFilter Attribute
  221. *
  222. * @param string $pName Attribute Name
  223. * @param string $pValue Attribute Value
  224. * @throws PHPExcel_Exception
  225. * @return PHPExcel_Worksheet_AutoFilter_Column
  226. */
  227. public function setAttribute($pName, $pValue)
  228. {
  229. $this->attributes[$pName] = $pValue;
  230. return $this;
  231. }
  232. /**
  233. * Get AutoFilter Column Attributes
  234. *
  235. * @return string
  236. */
  237. public function getAttributes()
  238. {
  239. return $this->attributes;
  240. }
  241. /**
  242. * Get specific AutoFilter Column Attribute
  243. *
  244. * @param string $pName Attribute Name
  245. * @return string
  246. */
  247. public function getAttribute($pName)
  248. {
  249. if (isset($this->attributes[$pName])) {
  250. return $this->attributes[$pName];
  251. }
  252. return null;
  253. }
  254. /**
  255. * Get all AutoFilter Column Rules
  256. *
  257. * @throws PHPExcel_Exception
  258. * @return array of PHPExcel_Worksheet_AutoFilter_Column_Rule
  259. */
  260. public function getRules()
  261. {
  262. return $this->ruleset;
  263. }
  264. /**
  265. * Get a specified AutoFilter Column Rule
  266. *
  267. * @param integer $pIndex Rule index in the ruleset array
  268. * @return PHPExcel_Worksheet_AutoFilter_Column_Rule
  269. */
  270. public function getRule($pIndex)
  271. {
  272. if (!isset($this->ruleset[$pIndex])) {
  273. $this->ruleset[$pIndex] = new PHPExcel_Worksheet_AutoFilter_Column_Rule($this);
  274. }
  275. return $this->ruleset[$pIndex];
  276. }
  277. /**
  278. * Create a new AutoFilter Column Rule in the ruleset
  279. *
  280. * @return PHPExcel_Worksheet_AutoFilter_Column_Rule
  281. */
  282. public function createRule()
  283. {
  284. $this->ruleset[] = new PHPExcel_Worksheet_AutoFilter_Column_Rule($this);
  285. return end($this->ruleset);
  286. }
  287. /**
  288. * Add a new AutoFilter Column Rule to the ruleset
  289. *
  290. * @param PHPExcel_Worksheet_AutoFilter_Column_Rule $pRule
  291. * @param boolean $returnRule Flag indicating whether the rule object or the column object should be returned
  292. * @return PHPExcel_Worksheet_AutoFilter_Column|PHPExcel_Worksheet_AutoFilter_Column_Rule
  293. */
  294. public function addRule(PHPExcel_Worksheet_AutoFilter_Column_Rule $pRule, $returnRule = true)
  295. {
  296. $pRule->setParent($this);
  297. $this->ruleset[] = $pRule;
  298. return ($returnRule) ? $pRule : $this;
  299. }
  300. /**
  301. * Delete a specified AutoFilter Column Rule
  302. * If the number of rules is reduced to 1, then we reset And/Or logic to Or
  303. *
  304. * @param integer $pIndex Rule index in the ruleset array
  305. * @return PHPExcel_Worksheet_AutoFilter_Column
  306. */
  307. public function deleteRule($pIndex)
  308. {
  309. if (isset($this->ruleset[$pIndex])) {
  310. unset($this->ruleset[$pIndex]);
  311. // If we've just deleted down to a single rule, then reset And/Or joining to Or
  312. if (count($this->ruleset) <= 1) {
  313. $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
  314. }
  315. }
  316. return $this;
  317. }
  318. /**
  319. * Delete all AutoFilter Column Rules
  320. *
  321. * @return PHPExcel_Worksheet_AutoFilter_Column
  322. */
  323. public function clearRules()
  324. {
  325. $this->ruleset = array();
  326. $this->setJoin(self::AUTOFILTER_COLUMN_JOIN_OR);
  327. return $this;
  328. }
  329. /**
  330. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  331. */
  332. public function __clone()
  333. {
  334. $vars = get_object_vars($this);
  335. foreach ($vars as $key => $value) {
  336. if (is_object($value)) {
  337. if ($key == 'parent') {
  338. // Detach from autofilter parent
  339. $this->$key = null;
  340. } else {
  341. $this->$key = clone $value;
  342. }
  343. } elseif ((is_array($value)) && ($key == 'ruleset')) {
  344. // The columns array of PHPExcel_Worksheet_AutoFilter objects
  345. $this->$key = array();
  346. foreach ($value as $k => $v) {
  347. $this->{$key[$k]} = clone $v;
  348. // attach the new cloned Rule to this new cloned Autofilter Cloned object
  349. $this->{$key[$k]}->setParent($this);
  350. }
  351. } else {
  352. $this->$key = $value;
  353. }
  354. }
  355. }
  356. }