PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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