PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/common/libraries/plugin/phpexcel/PHPExcel/Style/Borders.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 540 lines | 236 code | 41 blank | 263 comment | 23 complexity | 44ca036cf51c854f97a88128bc4be427 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Style_Borders
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Borders implements PHPExcel_IComparable
  35. {
  36. /* Diagonal directions */
  37. const DIAGONAL_NONE = 0;
  38. const DIAGONAL_UP = 1;
  39. const DIAGONAL_DOWN = 2;
  40. const DIAGONAL_BOTH = 3;
  41. /**
  42. * Left
  43. *
  44. * @var PHPExcel_Style_Border
  45. */
  46. private $_left;
  47. /**
  48. * Right
  49. *
  50. * @var PHPExcel_Style_Border
  51. */
  52. private $_right;
  53. /**
  54. * Top
  55. *
  56. * @var PHPExcel_Style_Border
  57. */
  58. private $_top;
  59. /**
  60. * Bottom
  61. *
  62. * @var PHPExcel_Style_Border
  63. */
  64. private $_bottom;
  65. /**
  66. * Diagonal
  67. *
  68. * @var PHPExcel_Style_Border
  69. */
  70. private $_diagonal;
  71. /**
  72. * DiagonalDirection
  73. *
  74. * @var int
  75. */
  76. private $_diagonalDirection;
  77. /**
  78. * All borders psedo-border. Only applies to supervisor.
  79. *
  80. * @var PHPExcel_Style_Border
  81. */
  82. private $_allBorders;
  83. /**
  84. * Outline psedo-border. Only applies to supervisor.
  85. *
  86. * @var PHPExcel_Style_Border
  87. */
  88. private $_outline;
  89. /**
  90. * Inside psedo-border. Only applies to supervisor.
  91. *
  92. * @var PHPExcel_Style_Border
  93. */
  94. private $_inside;
  95. /**
  96. * Vertical pseudo-border. Only applies to supervisor.
  97. *
  98. * @var PHPExcel_Style_Border
  99. */
  100. private $_vertical;
  101. /**
  102. * Horizontal pseudo-border. Only applies to supervisor.
  103. *
  104. * @var PHPExcel_Style_Border
  105. */
  106. private $_horizontal;
  107. /**
  108. * Parent Borders
  109. *
  110. * @var _parentPropertyName string
  111. */
  112. private $_parentPropertyName;
  113. /**
  114. * Supervisor?
  115. *
  116. * @var boolean
  117. */
  118. private $_isSupervisor;
  119. /**
  120. * Parent. Only used for supervisor
  121. *
  122. * @var PHPExcel_Style
  123. */
  124. private $_parent;
  125. /**
  126. * Create a new PHPExcel_Style_Borders
  127. */
  128. public function __construct($isSupervisor = false)
  129. {
  130. // Supervisor?
  131. $this->_isSupervisor = $isSupervisor;
  132. // Initialise values
  133. $this->_left = new PHPExcel_Style_Border($isSupervisor);
  134. $this->_right = new PHPExcel_Style_Border($isSupervisor);
  135. $this->_top = new PHPExcel_Style_Border($isSupervisor);
  136. $this->_bottom = new PHPExcel_Style_Border($isSupervisor);
  137. $this->_diagonal = new PHPExcel_Style_Border($isSupervisor);
  138. $this->_diagonalDirection = PHPExcel_Style_Borders :: DIAGONAL_NONE;
  139. // Specially for supervisor
  140. if ($isSupervisor)
  141. {
  142. // Initialize pseudo-borders
  143. $this->_allBorders = new PHPExcel_Style_Border(true);
  144. $this->_outline = new PHPExcel_Style_Border(true);
  145. $this->_inside = new PHPExcel_Style_Border(true);
  146. $this->_vertical = new PHPExcel_Style_Border(true);
  147. $this->_horizontal = new PHPExcel_Style_Border(true);
  148. // bind parent if we are a supervisor
  149. $this->_left->bindParent($this, '_left');
  150. $this->_right->bindParent($this, '_right');
  151. $this->_top->bindParent($this, '_top');
  152. $this->_bottom->bindParent($this, '_bottom');
  153. $this->_diagonal->bindParent($this, '_diagonal');
  154. $this->_allBorders->bindParent($this, '_allBorders');
  155. $this->_outline->bindParent($this, '_outline');
  156. $this->_inside->bindParent($this, '_inside');
  157. $this->_vertical->bindParent($this, '_vertical');
  158. $this->_horizontal->bindParent($this, '_horizontal');
  159. }
  160. }
  161. /**
  162. * Bind parent. Only used for supervisor
  163. *
  164. * @param PHPExcel_Style $parent
  165. * @return PHPExcel_Style_Borders
  166. */
  167. public function bindParent($parent)
  168. {
  169. $this->_parent = $parent;
  170. return $this;
  171. }
  172. /**
  173. * Is this a supervisor or a real style component?
  174. *
  175. * @return boolean
  176. */
  177. public function getIsSupervisor()
  178. {
  179. return $this->_isSupervisor;
  180. }
  181. /**
  182. * Get the shared style component for the currently active cell in currently active sheet.
  183. * Only used for style supervisor
  184. *
  185. * @return PHPExcel_Style_Borders
  186. */
  187. public function getSharedComponent()
  188. {
  189. return $this->_parent->getSharedComponent()->getBorders();
  190. }
  191. /**
  192. * Get the currently active sheet. Only used for supervisor
  193. *
  194. * @return PHPExcel_Worksheet
  195. */
  196. public function getActiveSheet()
  197. {
  198. return $this->_parent->getActiveSheet();
  199. }
  200. /**
  201. * Get the currently active cell coordinate in currently active sheet.
  202. * Only used for supervisor
  203. *
  204. * @return string E.g. 'A1'
  205. */
  206. public function getSelectedCells()
  207. {
  208. return $this->getActiveSheet()->getSelectedCells();
  209. }
  210. /**
  211. * Get the currently active cell coordinate in currently active sheet.
  212. * Only used for supervisor
  213. *
  214. * @return string E.g. 'A1'
  215. */
  216. public function getActiveCell()
  217. {
  218. return $this->getActiveSheet()->getActiveCell();
  219. }
  220. /**
  221. * Build style array from subcomponents
  222. *
  223. * @param array $array
  224. * @return array
  225. */
  226. public function getStyleArray($array)
  227. {
  228. return array('borders' => $array);
  229. }
  230. /**
  231. * Apply styles from array
  232. *
  233. * <code>
  234. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  235. * array(
  236. * 'bottom' => array(
  237. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  238. * 'color' => array(
  239. * 'rgb' => '808080'
  240. * )
  241. * ),
  242. * 'top' => array(
  243. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  244. * 'color' => array(
  245. * 'rgb' => '808080'
  246. * )
  247. * )
  248. * )
  249. * );
  250. * </code>
  251. * <code>
  252. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  253. * array(
  254. * 'allborders' => array(
  255. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  256. * 'color' => array(
  257. * 'rgb' => '808080'
  258. * )
  259. * )
  260. * )
  261. * );
  262. * </code>
  263. *
  264. * @param array $pStyles Array containing style information
  265. * @throws Exception
  266. * @return PHPExcel_Style_Borders
  267. */
  268. public function applyFromArray($pStyles = null)
  269. {
  270. if (is_array($pStyles))
  271. {
  272. if ($this->_isSupervisor)
  273. {
  274. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  275. }
  276. else
  277. {
  278. if (array_key_exists('left', $pStyles))
  279. {
  280. $this->getLeft()->applyFromArray($pStyles['left']);
  281. }
  282. if (array_key_exists('right', $pStyles))
  283. {
  284. $this->getRight()->applyFromArray($pStyles['right']);
  285. }
  286. if (array_key_exists('top', $pStyles))
  287. {
  288. $this->getTop()->applyFromArray($pStyles['top']);
  289. }
  290. if (array_key_exists('bottom', $pStyles))
  291. {
  292. $this->getBottom()->applyFromArray($pStyles['bottom']);
  293. }
  294. if (array_key_exists('diagonal', $pStyles))
  295. {
  296. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  297. }
  298. if (array_key_exists('diagonaldirection', $pStyles))
  299. {
  300. $this->setDiagonalDirection($pStyles['diagonaldirection']);
  301. }
  302. if (array_key_exists('allborders', $pStyles))
  303. {
  304. $this->getLeft()->applyFromArray($pStyles['allborders']);
  305. $this->getRight()->applyFromArray($pStyles['allborders']);
  306. $this->getTop()->applyFromArray($pStyles['allborders']);
  307. $this->getBottom()->applyFromArray($pStyles['allborders']);
  308. }
  309. }
  310. }
  311. else
  312. {
  313. throw new Exception("Invalid style array passed.");
  314. }
  315. return $this;
  316. }
  317. /**
  318. * Get Left
  319. *
  320. * @return PHPExcel_Style_Border
  321. */
  322. public function getLeft()
  323. {
  324. return $this->_left;
  325. }
  326. /**
  327. * Get Right
  328. *
  329. * @return PHPExcel_Style_Border
  330. */
  331. public function getRight()
  332. {
  333. return $this->_right;
  334. }
  335. /**
  336. * Get Top
  337. *
  338. * @return PHPExcel_Style_Border
  339. */
  340. public function getTop()
  341. {
  342. return $this->_top;
  343. }
  344. /**
  345. * Get Bottom
  346. *
  347. * @return PHPExcel_Style_Border
  348. */
  349. public function getBottom()
  350. {
  351. return $this->_bottom;
  352. }
  353. /**
  354. * Get Diagonal
  355. *
  356. * @return PHPExcel_Style_Border
  357. */
  358. public function getDiagonal()
  359. {
  360. return $this->_diagonal;
  361. }
  362. /**
  363. * Get AllBorders (pseudo-border). Only applies to supervisor.
  364. *
  365. * @return PHPExcel_Style_Border
  366. * @throws Exception
  367. */
  368. public function getAllBorders()
  369. {
  370. if (! $this->_isSupervisor)
  371. {
  372. throw new Exception('Can only get pseudo-border for supervisor.');
  373. }
  374. return $this->_allBorders;
  375. }
  376. /**
  377. * Get Outline (pseudo-border). Only applies to supervisor.
  378. *
  379. * @return boolean
  380. * @throws Exception
  381. */
  382. public function getOutline()
  383. {
  384. if (! $this->_isSupervisor)
  385. {
  386. throw new Exception('Can only get pseudo-border for supervisor.');
  387. }
  388. return $this->_outline;
  389. }
  390. /**
  391. * Get Inside (pseudo-border). Only applies to supervisor.
  392. *
  393. * @return boolean
  394. * @throws Exception
  395. */
  396. public function getInside()
  397. {
  398. if (! $this->_isSupervisor)
  399. {
  400. throw new Exception('Can only get pseudo-border for supervisor.');
  401. }
  402. return $this->_inside;
  403. }
  404. /**
  405. * Get Vertical (pseudo-border). Only applies to supervisor.
  406. *
  407. * @return PHPExcel_Style_Border
  408. * @throws Exception
  409. */
  410. public function getVertical()
  411. {
  412. if (! $this->_isSupervisor)
  413. {
  414. throw new Exception('Can only get pseudo-border for supervisor.');
  415. }
  416. return $this->_vertical;
  417. }
  418. /**
  419. * Get Horizontal (pseudo-border). Only applies to supervisor.
  420. *
  421. * @return PHPExcel_Style_Border
  422. * @throws Exception
  423. */
  424. public function getHorizontal()
  425. {
  426. if (! $this->_isSupervisor)
  427. {
  428. throw new Exception('Can only get pseudo-border for supervisor.');
  429. }
  430. return $this->_horizontal;
  431. }
  432. /**
  433. * Get DiagonalDirection
  434. *
  435. * @return int
  436. */
  437. public function getDiagonalDirection()
  438. {
  439. if ($this->_isSupervisor)
  440. {
  441. return $this->getSharedComponent()->getDiagonalDirection();
  442. }
  443. return $this->_diagonalDirection;
  444. }
  445. /**
  446. * Set DiagonalDirection
  447. *
  448. * @param int $pValue
  449. * @return PHPExcel_Style_Borders
  450. */
  451. public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE)
  452. {
  453. if ($pValue == '')
  454. {
  455. $pValue = PHPExcel_Style_Borders :: DIAGONAL_NONE;
  456. }
  457. if ($this->_isSupervisor)
  458. {
  459. $styleArray = $this->getStyleArray(array('diagonaldirection' => $pValue));
  460. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  461. }
  462. else
  463. {
  464. $this->_diagonalDirection = $pValue;
  465. }
  466. return $this;
  467. }
  468. /**
  469. * Get hash code
  470. *
  471. * @return string Hash code
  472. */
  473. public function getHashCode()
  474. {
  475. if ($this->_isSupervisor)
  476. {
  477. return $this->getSharedComponent()->getHashcode();
  478. }
  479. return md5($this->getLeft()->getHashCode() . $this->getRight()->getHashCode() . $this->getTop()->getHashCode() . $this->getBottom()->getHashCode() . $this->getDiagonal()->getHashCode() . $this->getDiagonalDirection() . __CLASS__);
  480. }
  481. /**
  482. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  483. */
  484. public function __clone()
  485. {
  486. $vars = get_object_vars($this);
  487. foreach ($vars as $key => $value)
  488. {
  489. if ((is_object($value)) && ($key != '_parent'))
  490. {
  491. $this->$key = clone $value;
  492. }
  493. else
  494. {
  495. $this->$key = $value;
  496. }
  497. }
  498. }
  499. }