PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/Protection.php

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