PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 537 lines | 281 code | 38 blank | 218 comment | 45 complexity | 571f815f0d8307b4b44a7786667c4796 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_Alignment
  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_Alignment implements PHPExcel_IComparable
  35. {
  36. /* Horizontal alignment styles */
  37. const HORIZONTAL_GENERAL = 'general';
  38. const HORIZONTAL_LEFT = 'left';
  39. const HORIZONTAL_RIGHT = 'right';
  40. const HORIZONTAL_CENTER = 'center';
  41. const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  42. const HORIZONTAL_JUSTIFY = 'justify';
  43. /* Vertical alignment styles */
  44. const VERTICAL_BOTTOM = 'bottom';
  45. const VERTICAL_TOP = 'top';
  46. const VERTICAL_CENTER = 'center';
  47. const VERTICAL_JUSTIFY = 'justify';
  48. /**
  49. * Horizontal
  50. *
  51. * @var string
  52. */
  53. private $_horizontal = PHPExcel_Style_Alignment :: HORIZONTAL_GENERAL;
  54. /**
  55. * Vertical
  56. *
  57. * @var string
  58. */
  59. private $_vertical = PHPExcel_Style_Alignment :: VERTICAL_BOTTOM;
  60. /**
  61. * Text rotation
  62. *
  63. * @var int
  64. */
  65. private $_textRotation = 0;
  66. /**
  67. * Wrap text
  68. *
  69. * @var boolean
  70. */
  71. private $_wrapText = false;
  72. /**
  73. * Shrink to fit
  74. *
  75. * @var boolean
  76. */
  77. private $_shrinkToFit = false;
  78. /**
  79. * Indent - only possible with horizontal alignment left and right
  80. *
  81. * @var int
  82. */
  83. private $_indent = 0;
  84. /**
  85. * Parent Borders
  86. *
  87. * @var _parentPropertyName string
  88. */
  89. private $_parentPropertyName;
  90. /**
  91. * Supervisor?
  92. *
  93. * @var boolean
  94. */
  95. private $_isSupervisor;
  96. /**
  97. * Parent. Only used for supervisor
  98. *
  99. * @var PHPExcel_Style
  100. */
  101. private $_parent;
  102. /**
  103. * Create a new PHPExcel_Style_Alignment
  104. */
  105. public function __construct($isSupervisor = false)
  106. {
  107. // Supervisor?
  108. $this->_isSupervisor = $isSupervisor;
  109. }
  110. /**
  111. * Bind parent. Only used for supervisor
  112. *
  113. * @param PHPExcel $parent
  114. * @return PHPExcel_Style_Alignment
  115. */
  116. public function bindParent($parent)
  117. {
  118. $this->_parent = $parent;
  119. return $this;
  120. }
  121. /**
  122. * Is this a supervisor or a real style component?
  123. *
  124. * @return boolean
  125. */
  126. public function getIsSupervisor()
  127. {
  128. return $this->_isSupervisor;
  129. }
  130. /**
  131. * Get the shared style component for the currently active cell in currently active sheet.
  132. * Only used for style supervisor
  133. *
  134. * @return PHPExcel_Style_Alignment
  135. */
  136. public function getSharedComponent()
  137. {
  138. return $this->_parent->getSharedComponent()->getAlignment();
  139. }
  140. /**
  141. * Get the currently active sheet. Only used for supervisor
  142. *
  143. * @return PHPExcel_Worksheet
  144. */
  145. public function getActiveSheet()
  146. {
  147. return $this->_parent->getActiveSheet();
  148. }
  149. /**
  150. * Get the currently active cell coordinate in currently active sheet.
  151. * Only used for supervisor
  152. *
  153. * @return string E.g. 'A1'
  154. */
  155. public function getSelectedCells()
  156. {
  157. return $this->getActiveSheet()->getSelectedCells();
  158. }
  159. /**
  160. * Get the currently active cell coordinate in currently active sheet.
  161. * Only used for supervisor
  162. *
  163. * @return string E.g. 'A1'
  164. */
  165. public function getActiveCell()
  166. {
  167. return $this->getActiveSheet()->getActiveCell();
  168. }
  169. /**
  170. * Build style array from subcomponents
  171. *
  172. * @param array $array
  173. * @return array
  174. */
  175. public function getStyleArray($array)
  176. {
  177. return array('alignment' => $array);
  178. }
  179. /**
  180. * Apply styles from array
  181. *
  182. * <code>
  183. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  184. * array(
  185. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  186. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  187. * 'rotation' => 0,
  188. * 'wrap' => true
  189. * )
  190. * );
  191. * </code>
  192. *
  193. * @param array $pStyles Array containing style information
  194. * @throws Exception
  195. * @return PHPExcel_Style_Alignment
  196. */
  197. public function applyFromArray($pStyles = null)
  198. {
  199. if (is_array($pStyles))
  200. {
  201. if ($this->_isSupervisor)
  202. {
  203. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  204. }
  205. else
  206. {
  207. if (array_key_exists('horizontal', $pStyles))
  208. {
  209. $this->setHorizontal($pStyles['horizontal']);
  210. }
  211. if (array_key_exists('vertical', $pStyles))
  212. {
  213. $this->setVertical($pStyles['vertical']);
  214. }
  215. if (array_key_exists('rotation', $pStyles))
  216. {
  217. $this->setTextRotation($pStyles['rotation']);
  218. }
  219. if (array_key_exists('wrap', $pStyles))
  220. {
  221. $this->setWrapText($pStyles['wrap']);
  222. }
  223. if (array_key_exists('shrinkToFit', $pStyles))
  224. {
  225. $this->setShrinkToFit($pStyles['shrinkToFit']);
  226. }
  227. if (array_key_exists('indent', $pStyles))
  228. {
  229. $this->setIndent($pStyles['indent']);
  230. }
  231. }
  232. }
  233. else
  234. {
  235. throw new Exception("Invalid style array passed.");
  236. }
  237. return $this;
  238. }
  239. /**
  240. * Get Horizontal
  241. *
  242. * @return string
  243. */
  244. public function getHorizontal()
  245. {
  246. if ($this->_isSupervisor)
  247. {
  248. return $this->getSharedComponent()->getHorizontal();
  249. }
  250. return $this->_horizontal;
  251. }
  252. /**
  253. * Set Horizontal
  254. *
  255. * @param string $pValue
  256. * @return PHPExcel_Style_Alignment
  257. */
  258. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL)
  259. {
  260. if ($pValue == '')
  261. {
  262. $pValue = PHPExcel_Style_Alignment :: HORIZONTAL_GENERAL;
  263. }
  264. if ($this->_isSupervisor)
  265. {
  266. $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
  267. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  268. }
  269. else
  270. {
  271. $this->_horizontal = $pValue;
  272. }
  273. return $this;
  274. }
  275. /**
  276. * Get Vertical
  277. *
  278. * @return string
  279. */
  280. public function getVertical()
  281. {
  282. if ($this->_isSupervisor)
  283. {
  284. return $this->getSharedComponent()->getVertical();
  285. }
  286. return $this->_vertical;
  287. }
  288. /**
  289. * Set Vertical
  290. *
  291. * @param string $pValue
  292. * @return PHPExcel_Style_Alignment
  293. */
  294. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM)
  295. {
  296. if ($pValue == '')
  297. {
  298. $pValue = PHPExcel_Style_Alignment :: VERTICAL_BOTTOM;
  299. }
  300. if ($this->_isSupervisor)
  301. {
  302. $styleArray = $this->getStyleArray(array('vertical' => $pValue));
  303. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  304. }
  305. else
  306. {
  307. $this->_vertical = $pValue;
  308. }
  309. return $this;
  310. }
  311. /**
  312. * Get TextRotation
  313. *
  314. * @return int
  315. */
  316. public function getTextRotation()
  317. {
  318. if ($this->_isSupervisor)
  319. {
  320. return $this->getSharedComponent()->getTextRotation();
  321. }
  322. return $this->_textRotation;
  323. }
  324. /**
  325. * Set TextRotation
  326. *
  327. * @param int $pValue
  328. * @throws Exception
  329. * @return PHPExcel_Style_Alignment
  330. */
  331. public function setTextRotation($pValue = 0)
  332. {
  333. // Excel2007 value 255 => PHPExcel value -165
  334. if ($pValue == 255)
  335. {
  336. $pValue = - 165;
  337. }
  338. // Set rotation
  339. if (($pValue >= - 90 && $pValue <= 90) || $pValue == - 165)
  340. {
  341. if ($this->_isSupervisor)
  342. {
  343. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  344. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  345. }
  346. else
  347. {
  348. $this->_textRotation = $pValue;
  349. }
  350. }
  351. else
  352. {
  353. throw new Exception("Text rotation should be a value between -90 and 90.");
  354. }
  355. return $this;
  356. }
  357. /**
  358. * Get Wrap Text
  359. *
  360. * @return boolean
  361. */
  362. public function getWrapText()
  363. {
  364. if ($this->_isSupervisor)
  365. {
  366. return $this->getSharedComponent()->getWrapText();
  367. }
  368. return $this->_wrapText;
  369. }
  370. /**
  371. * Set Wrap Text
  372. *
  373. * @param boolean $pValue
  374. * @return PHPExcel_Style_Alignment
  375. */
  376. public function setWrapText($pValue = false)
  377. {
  378. if ($pValue == '')
  379. {
  380. $pValue = false;
  381. }
  382. if ($this->_isSupervisor)
  383. {
  384. $styleArray = $this->getStyleArray(array('wrap' => $pValue));
  385. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  386. }
  387. else
  388. {
  389. $this->_wrapText = $pValue;
  390. }
  391. return $this;
  392. }
  393. /**
  394. * Get Shrink to fit
  395. *
  396. * @return boolean
  397. */
  398. public function getShrinkToFit()
  399. {
  400. if ($this->_isSupervisor)
  401. {
  402. return $this->getSharedComponent()->getShrinkToFit();
  403. }
  404. return $this->_shrinkToFit;
  405. }
  406. /**
  407. * Set Shrink to fit
  408. *
  409. * @param boolean $pValue
  410. * @return PHPExcel_Style_Alignment
  411. */
  412. public function setShrinkToFit($pValue = false)
  413. {
  414. if ($pValue == '')
  415. {
  416. $pValue = false;
  417. }
  418. if ($this->_isSupervisor)
  419. {
  420. $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
  421. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  422. }
  423. else
  424. {
  425. $this->_shrinkToFit = $pValue;
  426. }
  427. return $this;
  428. }
  429. /**
  430. * Get indent
  431. *
  432. * @return int
  433. */
  434. public function getIndent()
  435. {
  436. if ($this->_isSupervisor)
  437. {
  438. return $this->getSharedComponent()->getIndent();
  439. }
  440. return $this->_indent;
  441. }
  442. /**
  443. * Set indent
  444. *
  445. * @param int $pValue
  446. * @return PHPExcel_Style_Alignment
  447. */
  448. public function setIndent($pValue = 0)
  449. {
  450. if ($pValue > 0)
  451. {
  452. if ($this->getHorizontal() != self :: HORIZONTAL_GENERAL && $this->getHorizontal() != self :: HORIZONTAL_LEFT && $this->getHorizontal() != self :: HORIZONTAL_RIGHT)
  453. {
  454. $pValue = 0; // indent not supported
  455. }
  456. }
  457. if ($this->_isSupervisor)
  458. {
  459. $styleArray = $this->getStyleArray(array('indent' => $pValue));
  460. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  461. }
  462. else
  463. {
  464. $this->_indent = $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->_horizontal . $this->_vertical . $this->_textRotation . ($this->_wrapText ? 't' : 'f') . ($this->_shrinkToFit ? 't' : 'f') . $this->_indent . __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. }