PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Protection.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 290 lines | 116 code | 23 blank | 151 comment | 18 complexity | e1d012d35c7eca12f4bca0b9d038f396 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  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_Style
  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 1.4.5, 2007-08-23
  26. */
  27. /**
  28. * PHPExcel_Style_Protection
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Protection implements PHPExcel_IComparable
  35. {
  36. /** Protection styles */
  37. const PROTECTION_INHERIT = 'inherit';
  38. const PROTECTION_PROTECTED = 'protected';
  39. const PROTECTION_UNPROTECTED = 'unprotected';
  40. /**
  41. * Locked
  42. *
  43. * @var string
  44. */
  45. private $_locked;
  46. /**
  47. * Hidden
  48. *
  49. * @var string
  50. */
  51. private $_hidden;
  52. /**
  53. * Parent Borders
  54. *
  55. * @var _parentPropertyName string
  56. */
  57. private $_parentPropertyName;
  58. /**
  59. * Supervisor?
  60. *
  61. * @var boolean
  62. */
  63. private $_isSupervisor;
  64. /**
  65. * Parent. Only used for supervisor
  66. *
  67. * @var PHPExcel_Style
  68. */
  69. private $_parent;
  70. /**
  71. * Create a new PHPExcel_Style_Protection
  72. *
  73. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  74. * Leave this value at default unless you understand exactly what
  75. * its ramifications are
  76. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  77. * Leave this value at default unless you understand exactly what
  78. * its ramifications are
  79. */
  80. public function __construct($isSupervisor = false, $isConditional = false)
  81. {
  82. // Supervisor?
  83. $this->_isSupervisor = $isSupervisor;
  84. // Initialise values
  85. if (!$isConditional) {
  86. $this->_locked = self::PROTECTION_INHERIT;
  87. $this->_hidden = self::PROTECTION_INHERIT;
  88. }
  89. }
  90. /**
  91. * Bind parent. Only used for supervisor
  92. *
  93. * @param PHPExcel_Style $parent
  94. * @return PHPExcel_Style_Protection
  95. */
  96. public function bindParent($parent)
  97. {
  98. $this->_parent = $parent;
  99. return $this;
  100. }
  101. /**
  102. * Is this a supervisor or a real style component?
  103. *
  104. * @return boolean
  105. */
  106. public function getIsSupervisor()
  107. {
  108. return $this->_isSupervisor;
  109. }
  110. /**
  111. * Get the shared style component for the currently active cell in currently active sheet.
  112. * Only used for style supervisor
  113. *
  114. * @return PHPExcel_Style_Protection
  115. */
  116. public function getSharedComponent()
  117. {
  118. return $this->_parent->getSharedComponent()->getProtection();
  119. }
  120. /**
  121. * Get the currently active sheet. Only used for supervisor
  122. *
  123. * @return PHPExcel_Worksheet
  124. */
  125. public function getActiveSheet()
  126. {
  127. return $this->_parent->getActiveSheet();
  128. }
  129. /**
  130. * Get the currently active cell coordinate in currently active sheet.
  131. * Only used for supervisor
  132. *
  133. * @return string E.g. 'A1'
  134. */
  135. public function getSelectedCells()
  136. {
  137. return $this->getActiveSheet()->getSelectedCells();
  138. }
  139. /**
  140. * Get the currently active cell coordinate in currently active sheet.
  141. * Only used for supervisor
  142. *
  143. * @return string E.g. 'A1'
  144. */
  145. public function getActiveCell()
  146. {
  147. return $this->getActiveSheet()->getActiveCell();
  148. }
  149. /**
  150. * Build style array from subcomponents
  151. *
  152. * @param array $array
  153. * @return array
  154. */
  155. public function getStyleArray($array)
  156. {
  157. return array('protection' => $array);
  158. }
  159. /**
  160. * Apply styles from array
  161. *
  162. * <code>
  163. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray( array('locked' => true, 'hidden' => false) );
  164. * </code>
  165. *
  166. * @param array $pStyles Array containing style information
  167. * @throws Exception
  168. * @return PHPExcel_Style_Protection
  169. */
  170. public function applyFromArray($pStyles = null) {
  171. if (is_array($pStyles)) {
  172. if ($this->_isSupervisor) {
  173. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  174. } else {
  175. if (array_key_exists('locked', $pStyles)) {
  176. $this->setLocked($pStyles['locked']);
  177. }
  178. if (array_key_exists('hidden', $pStyles)) {
  179. $this->setHidden($pStyles['hidden']);
  180. }
  181. }
  182. } else {
  183. throw new Exception("Invalid style array passed.");
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Get locked
  189. *
  190. * @return string
  191. */
  192. public function getLocked() {
  193. if ($this->_isSupervisor) {
  194. return $this->getSharedComponent()->getLocked();
  195. }
  196. return $this->_locked;
  197. }
  198. /**
  199. * Set locked
  200. *
  201. * @param string $pValue
  202. * @return PHPExcel_Style_Protection
  203. */
  204. public function setLocked($pValue = self::PROTECTION_INHERIT) {
  205. if ($this->_isSupervisor) {
  206. $styleArray = $this->getStyleArray(array('locked' => $pValue));
  207. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  208. } else {
  209. $this->_locked = $pValue;
  210. }
  211. return $this;
  212. }
  213. /**
  214. * Get hidden
  215. *
  216. * @return string
  217. */
  218. public function getHidden() {
  219. if ($this->_isSupervisor) {
  220. return $this->getSharedComponent()->getHidden();
  221. }
  222. return $this->_hidden;
  223. }
  224. /**
  225. * Set hidden
  226. *
  227. * @param string $pValue
  228. * @return PHPExcel_Style_Protection
  229. */
  230. public function setHidden($pValue = self::PROTECTION_INHERIT) {
  231. if ($this->_isSupervisor) {
  232. $styleArray = $this->getStyleArray(array('hidden' => $pValue));
  233. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  234. } else {
  235. $this->_hidden = $pValue;
  236. }
  237. return $this;
  238. }
  239. /**
  240. * Get hash code
  241. *
  242. * @return string Hash code
  243. */
  244. public function getHashCode() {
  245. if ($this->_isSupervisor) {
  246. return $this->getSharedComponent()->getHashCode();
  247. }
  248. return md5(
  249. $this->_locked
  250. . $this->_hidden
  251. . __CLASS__
  252. );
  253. }
  254. /**
  255. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  256. */
  257. public function __clone() {
  258. $vars = get_object_vars($this);
  259. foreach ($vars as $key => $value) {
  260. if ((is_object($value)) && ($key != '_parent')) {
  261. $this->$key = clone $value;
  262. } else {
  263. $this->$key = $value;
  264. }
  265. }
  266. }
  267. }