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

/htdocs/includes/phpexcel/PHPExcel/Style/Borders.php

http://github.com/Dolibarr/dolibarr
PHP | 505 lines | 200 code | 42 blank | 263 comment | 27 complexity | c8f230ce376c47554eaab858a260579b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, 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. // Initialize pseudo-borders
  142. $this->_allBorders = new PHPExcel_Style_Border(true);
  143. $this->_outline = new PHPExcel_Style_Border(true);
  144. $this->_inside = new PHPExcel_Style_Border(true);
  145. $this->_vertical = new PHPExcel_Style_Border(true);
  146. $this->_horizontal = new PHPExcel_Style_Border(true);
  147. // bind parent if we are a supervisor
  148. $this->_left->bindParent($this, '_left');
  149. $this->_right->bindParent($this, '_right');
  150. $this->_top->bindParent($this, '_top');
  151. $this->_bottom->bindParent($this, '_bottom');
  152. $this->_diagonal->bindParent($this, '_diagonal');
  153. $this->_allBorders->bindParent($this, '_allBorders');
  154. $this->_outline->bindParent($this, '_outline');
  155. $this->_inside->bindParent($this, '_inside');
  156. $this->_vertical->bindParent($this, '_vertical');
  157. $this->_horizontal->bindParent($this, '_horizontal');
  158. }
  159. }
  160. /**
  161. * Bind parent. Only used for supervisor
  162. *
  163. * @param PHPExcel_Style $parent
  164. * @return PHPExcel_Style_Borders
  165. */
  166. public function bindParent($parent)
  167. {
  168. $this->_parent = $parent;
  169. return $this;
  170. }
  171. /**
  172. * Is this a supervisor or a real style component?
  173. *
  174. * @return boolean
  175. */
  176. public function getIsSupervisor()
  177. {
  178. return $this->_isSupervisor;
  179. }
  180. /**
  181. * Get the shared style component for the currently active cell in currently active sheet.
  182. * Only used for style supervisor
  183. *
  184. * @return PHPExcel_Style_Borders
  185. */
  186. public function getSharedComponent()
  187. {
  188. return $this->_parent->getSharedComponent()->getBorders();
  189. }
  190. /**
  191. * Get the currently active sheet. Only used for supervisor
  192. *
  193. * @return PHPExcel_Worksheet
  194. */
  195. public function getActiveSheet()
  196. {
  197. return $this->_parent->getActiveSheet();
  198. }
  199. /**
  200. * Get the currently active cell coordinate in currently active sheet.
  201. * Only used for supervisor
  202. *
  203. * @return string E.g. 'A1'
  204. */
  205. public function getSelectedCells()
  206. {
  207. return $this->getActiveSheet()->getSelectedCells();
  208. }
  209. /**
  210. * Get the currently active cell coordinate in currently active sheet.
  211. * Only used for supervisor
  212. *
  213. * @return string E.g. 'A1'
  214. */
  215. public function getActiveCell()
  216. {
  217. return $this->getActiveSheet()->getActiveCell();
  218. }
  219. /**
  220. * Build style array from subcomponents
  221. *
  222. * @param array $array
  223. * @return array
  224. */
  225. public function getStyleArray($array)
  226. {
  227. return array('borders' => $array);
  228. }
  229. /**
  230. * Apply styles from array
  231. *
  232. * <code>
  233. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  234. * array(
  235. * 'bottom' => array(
  236. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  237. * 'color' => array(
  238. * 'rgb' => '808080'
  239. * )
  240. * ),
  241. * 'top' => array(
  242. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  243. * 'color' => array(
  244. * 'rgb' => '808080'
  245. * )
  246. * )
  247. * )
  248. * );
  249. * </code>
  250. * <code>
  251. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  252. * array(
  253. * 'allborders' => array(
  254. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  255. * 'color' => array(
  256. * 'rgb' => '808080'
  257. * )
  258. * )
  259. * )
  260. * );
  261. * </code>
  262. *
  263. * @param array $pStyles Array containing style information
  264. * @throws Exception
  265. * @return PHPExcel_Style_Borders
  266. */
  267. public function applyFromArray($pStyles = null) {
  268. if (is_array($pStyles)) {
  269. if ($this->_isSupervisor) {
  270. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  271. } else {
  272. if (array_key_exists('left', $pStyles)) {
  273. $this->getLeft()->applyFromArray($pStyles['left']);
  274. }
  275. if (array_key_exists('right', $pStyles)) {
  276. $this->getRight()->applyFromArray($pStyles['right']);
  277. }
  278. if (array_key_exists('top', $pStyles)) {
  279. $this->getTop()->applyFromArray($pStyles['top']);
  280. }
  281. if (array_key_exists('bottom', $pStyles)) {
  282. $this->getBottom()->applyFromArray($pStyles['bottom']);
  283. }
  284. if (array_key_exists('diagonal', $pStyles)) {
  285. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  286. }
  287. if (array_key_exists('diagonaldirection', $pStyles)) {
  288. $this->setDiagonalDirection($pStyles['diagonaldirection']);
  289. }
  290. if (array_key_exists('allborders', $pStyles)) {
  291. $this->getLeft()->applyFromArray($pStyles['allborders']);
  292. $this->getRight()->applyFromArray($pStyles['allborders']);
  293. $this->getTop()->applyFromArray($pStyles['allborders']);
  294. $this->getBottom()->applyFromArray($pStyles['allborders']);
  295. }
  296. }
  297. } else {
  298. throw new Exception("Invalid style array passed.");
  299. }
  300. return $this;
  301. }
  302. /**
  303. * Get Left
  304. *
  305. * @return PHPExcel_Style_Border
  306. */
  307. public function getLeft() {
  308. return $this->_left;
  309. }
  310. /**
  311. * Get Right
  312. *
  313. * @return PHPExcel_Style_Border
  314. */
  315. public function getRight() {
  316. return $this->_right;
  317. }
  318. /**
  319. * Get Top
  320. *
  321. * @return PHPExcel_Style_Border
  322. */
  323. public function getTop() {
  324. return $this->_top;
  325. }
  326. /**
  327. * Get Bottom
  328. *
  329. * @return PHPExcel_Style_Border
  330. */
  331. public function getBottom() {
  332. return $this->_bottom;
  333. }
  334. /**
  335. * Get Diagonal
  336. *
  337. * @return PHPExcel_Style_Border
  338. */
  339. public function getDiagonal() {
  340. return $this->_diagonal;
  341. }
  342. /**
  343. * Get AllBorders (pseudo-border). Only applies to supervisor.
  344. *
  345. * @return PHPExcel_Style_Border
  346. * @throws Exception
  347. */
  348. public function getAllBorders() {
  349. if (!$this->_isSupervisor) {
  350. throw new Exception('Can only get pseudo-border for supervisor.');
  351. }
  352. return $this->_allBorders;
  353. }
  354. /**
  355. * Get Outline (pseudo-border). Only applies to supervisor.
  356. *
  357. * @return boolean
  358. * @throws Exception
  359. */
  360. public function getOutline() {
  361. if (!$this->_isSupervisor) {
  362. throw new Exception('Can only get pseudo-border for supervisor.');
  363. }
  364. return $this->_outline;
  365. }
  366. /**
  367. * Get Inside (pseudo-border). Only applies to supervisor.
  368. *
  369. * @return boolean
  370. * @throws Exception
  371. */
  372. public function getInside() {
  373. if (!$this->_isSupervisor) {
  374. throw new Exception('Can only get pseudo-border for supervisor.');
  375. }
  376. return $this->_inside;
  377. }
  378. /**
  379. * Get Vertical (pseudo-border). Only applies to supervisor.
  380. *
  381. * @return PHPExcel_Style_Border
  382. * @throws Exception
  383. */
  384. public function getVertical() {
  385. if (!$this->_isSupervisor) {
  386. throw new Exception('Can only get pseudo-border for supervisor.');
  387. }
  388. return $this->_vertical;
  389. }
  390. /**
  391. * Get Horizontal (pseudo-border). Only applies to supervisor.
  392. *
  393. * @return PHPExcel_Style_Border
  394. * @throws Exception
  395. */
  396. public function getHorizontal() {
  397. if (!$this->_isSupervisor) {
  398. throw new Exception('Can only get pseudo-border for supervisor.');
  399. }
  400. return $this->_horizontal;
  401. }
  402. /**
  403. * Get DiagonalDirection
  404. *
  405. * @return int
  406. */
  407. public function getDiagonalDirection() {
  408. if ($this->_isSupervisor) {
  409. return $this->getSharedComponent()->getDiagonalDirection();
  410. }
  411. return $this->_diagonalDirection;
  412. }
  413. /**
  414. * Set DiagonalDirection
  415. *
  416. * @param int $pValue
  417. * @return PHPExcel_Style_Borders
  418. */
  419. public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE) {
  420. if ($pValue == '') {
  421. $pValue = PHPExcel_Style_Borders::DIAGONAL_NONE;
  422. }
  423. if ($this->_isSupervisor) {
  424. $styleArray = $this->getStyleArray(array('diagonaldirection' => $pValue));
  425. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  426. } else {
  427. $this->_diagonalDirection = $pValue;
  428. }
  429. return $this;
  430. }
  431. /**
  432. * Get hash code
  433. *
  434. * @return string Hash code
  435. */
  436. public function getHashCode() {
  437. if ($this->_isSupervisor) {
  438. return $this->getSharedComponent()->getHashcode();
  439. }
  440. return md5(
  441. $this->getLeft()->getHashCode()
  442. . $this->getRight()->getHashCode()
  443. . $this->getTop()->getHashCode()
  444. . $this->getBottom()->getHashCode()
  445. . $this->getDiagonal()->getHashCode()
  446. . $this->getDiagonalDirection()
  447. . __CLASS__
  448. );
  449. }
  450. /**
  451. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  452. */
  453. public function __clone() {
  454. $vars = get_object_vars($this);
  455. foreach ($vars as $key => $value) {
  456. if ((is_object($value)) && ($key != '_parent')) {
  457. $this->$key = clone $value;
  458. } else {
  459. $this->$key = $value;
  460. }
  461. }
  462. }
  463. }