PageRenderTime 21ms CodeModel.GetById 8ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/PHPExcel/Style/Protection.php

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