source: pro-violet-viettel/www/deploy/20150304/application/libraries/PHPExcel/Style/Alignment.php @ 780

Last change on this file since 780 was 780, checked in by dungnv, 10 years ago
File size: 9.9 KB
Line 
1<?php
2/**
3 * PHPExcel
4 *
5 * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license     http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt   LGPL
25 * @version     1.8.0, 2014-03-02
26 */
27
28
29/**
30 * PHPExcel_Style_Alignment
31 *
32 * @category   PHPExcel
33 * @package     PHPExcel_Style
34 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
35 */
36class PHPExcel_Style_Alignment extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
37{
38        /* Horizontal alignment styles */
39        const HORIZONTAL_GENERAL                                = 'general';
40        const HORIZONTAL_LEFT                                   = 'left';
41        const HORIZONTAL_RIGHT                                  = 'right';
42        const HORIZONTAL_CENTER                                 = 'center';
43        const HORIZONTAL_CENTER_CONTINUOUS              = 'centerContinuous';
44        const HORIZONTAL_JUSTIFY                                = 'justify';
45        const HORIZONTAL_FILL                               = 'fill';
46        const HORIZONTAL_DISTRIBUTED                = 'distributed';        // Excel2007 only
47
48        /* Vertical alignment styles */
49        const VERTICAL_BOTTOM                                   = 'bottom';
50        const VERTICAL_TOP                                              = 'top';
51        const VERTICAL_CENTER                                   = 'center';
52        const VERTICAL_JUSTIFY                                  = 'justify';
53        const VERTICAL_DISTRIBUTED                      = 'distributed';        // Excel2007 only
54
55        /**
56         * Horizontal
57         *
58         * @var string
59         */
60        protected $_horizontal  = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
61
62        /**
63         * Vertical
64         *
65         * @var string
66         */
67        protected $_vertical            = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
68
69        /**
70         * Text rotation
71         *
72         * @var int
73         */
74        protected $_textRotation        = 0;
75
76        /**
77         * Wrap text
78         *
79         * @var boolean
80         */
81        protected $_wrapText            = FALSE;
82
83        /**
84         * Shrink to fit
85         *
86         * @var boolean
87         */
88        protected $_shrinkToFit = FALSE;
89
90        /**
91         * Indent - only possible with horizontal alignment left and right
92         *
93         * @var int
94         */
95        protected $_indent              = 0;
96
97        /**
98         * Create a new PHPExcel_Style_Alignment
99         *
100         * @param       boolean $isSupervisor   Flag indicating if this is a supervisor or not
101         *                                                                      Leave this value at default unless you understand exactly what
102         *                                                                              its ramifications are
103         * @param       boolean $isConditional  Flag indicating if this is a conditional style or not
104         *                                                                      Leave this value at default unless you understand exactly what
105         *                                                                              its ramifications are
106         */
107        public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
108        {
109                // Supervisor?
110                parent::__construct($isSupervisor);
111
112                if ($isConditional) {
113                        $this->_horizontal              = NULL;
114                        $this->_vertical                = NULL;
115                        $this->_textRotation    = NULL;
116                }
117        }
118
119        /**
120         * Get the shared style component for the currently active cell in currently active sheet.
121         * Only used for style supervisor
122         *
123         * @return PHPExcel_Style_Alignment
124         */
125        public function getSharedComponent()
126        {
127                return $this->_parent->getSharedComponent()->getAlignment();
128        }
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('alignment' => $array);
139        }
140
141        /**
142         * Apply styles from array
143         *
144         * <code>
145         * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
146         *              array(
147         *                      'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
148         *                      'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
149         *                      'rotation'   => 0,
150         *                      'wrap'                  => TRUE
151         *              )
152         * );
153         * </code>
154         *
155         * @param       array   $pStyles        Array containing style information
156         * @throws      PHPExcel_Exception
157         * @return PHPExcel_Style_Alignment
158         */
159        public function applyFromArray($pStyles = NULL) {
160                if (is_array($pStyles)) {
161                        if ($this->_isSupervisor) {
162                                $this->getActiveSheet()->getStyle($this->getSelectedCells())
163                                    ->applyFromArray($this->getStyleArray($pStyles));
164                        } else {
165                                if (isset($pStyles['horizontal'])) {
166                                        $this->setHorizontal($pStyles['horizontal']);
167                                }
168                                if (isset($pStyles['vertical'])) {
169                                        $this->setVertical($pStyles['vertical']);
170                                }
171                                if (isset($pStyles['rotation'])) {
172                                        $this->setTextRotation($pStyles['rotation']);
173                                }
174                                if (isset($pStyles['wrap'])) {
175                                        $this->setWrapText($pStyles['wrap']);
176                                }
177                                if (isset($pStyles['shrinkToFit'])) {
178                                        $this->setShrinkToFit($pStyles['shrinkToFit']);
179                                }
180                                if (isset($pStyles['indent'])) {
181                                        $this->setIndent($pStyles['indent']);
182                                }
183                        }
184                } else {
185                        throw new PHPExcel_Exception("Invalid style array passed.");
186                }
187                return $this;
188        }
189
190        /**
191         * Get Horizontal
192         *
193         * @return string
194         */
195        public function getHorizontal() {
196                if ($this->_isSupervisor) {
197                        return $this->getSharedComponent()->getHorizontal();
198                }
199                return $this->_horizontal;
200        }
201
202        /**
203         * Set Horizontal
204         *
205         * @param string $pValue
206         * @return PHPExcel_Style_Alignment
207         */
208        public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
209                if ($pValue == '') {
210                        $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
211                }
212
213                if ($this->_isSupervisor) {
214                        $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
215                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
216                }
217                else {
218                        $this->_horizontal = $pValue;
219                }
220                return $this;
221        }
222
223        /**
224         * Get Vertical
225         *
226         * @return string
227         */
228        public function getVertical() {
229                if ($this->_isSupervisor) {
230                        return $this->getSharedComponent()->getVertical();
231                }
232                return $this->_vertical;
233        }
234
235        /**
236         * Set Vertical
237         *
238         * @param string $pValue
239         * @return PHPExcel_Style_Alignment
240         */
241        public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
242                if ($pValue == '') {
243                        $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
244                }
245
246                if ($this->_isSupervisor) {
247                        $styleArray = $this->getStyleArray(array('vertical' => $pValue));
248                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
249                } else {
250                        $this->_vertical = $pValue;
251                }
252                return $this;
253        }
254
255        /**
256         * Get TextRotation
257         *
258         * @return int
259         */
260        public function getTextRotation() {
261                if ($this->_isSupervisor) {
262                        return $this->getSharedComponent()->getTextRotation();
263                }
264                return $this->_textRotation;
265        }
266
267        /**
268         * Set TextRotation
269         *
270         * @param int $pValue
271         * @throws PHPExcel_Exception
272         * @return PHPExcel_Style_Alignment
273         */
274        public function setTextRotation($pValue = 0) {
275                // Excel2007 value 255 => PHPExcel value -165
276                if ($pValue == 255) {
277                        $pValue = -165;
278                }
279
280                // Set rotation
281                if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
282                        if ($this->_isSupervisor) {
283                                $styleArray = $this->getStyleArray(array('rotation' => $pValue));
284                                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
285                        } else {
286                                $this->_textRotation = $pValue;
287                        }
288                } else {
289                        throw new PHPExcel_Exception("Text rotation should be a value between -90 and 90.");
290                }
291
292                return $this;
293        }
294
295        /**
296         * Get Wrap Text
297         *
298         * @return boolean
299         */
300        public function getWrapText() {
301                if ($this->_isSupervisor) {
302                        return $this->getSharedComponent()->getWrapText();
303                }
304                return $this->_wrapText;
305        }
306
307        /**
308         * Set Wrap Text
309         *
310         * @param boolean $pValue
311         * @return PHPExcel_Style_Alignment
312         */
313        public function setWrapText($pValue = FALSE) {
314                if ($pValue == '') {
315                        $pValue = FALSE;
316                }
317                if ($this->_isSupervisor) {
318                        $styleArray = $this->getStyleArray(array('wrap' => $pValue));
319                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
320                } else {
321                        $this->_wrapText = $pValue;
322                }
323                return $this;
324        }
325
326        /**
327         * Get Shrink to fit
328         *
329         * @return boolean
330         */
331        public function getShrinkToFit() {
332                if ($this->_isSupervisor) {
333                        return $this->getSharedComponent()->getShrinkToFit();
334                }
335                return $this->_shrinkToFit;
336        }
337
338        /**
339         * Set Shrink to fit
340         *
341         * @param boolean $pValue
342         * @return PHPExcel_Style_Alignment
343         */
344        public function setShrinkToFit($pValue = FALSE) {
345                if ($pValue == '') {
346                        $pValue = FALSE;
347                }
348                if ($this->_isSupervisor) {
349                        $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
350                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
351                } else {
352                        $this->_shrinkToFit = $pValue;
353                }
354                return $this;
355        }
356
357        /**
358         * Get indent
359         *
360         * @return int
361         */
362        public function getIndent() {
363                if ($this->_isSupervisor) {
364                        return $this->getSharedComponent()->getIndent();
365                }
366                return $this->_indent;
367        }
368
369        /**
370         * Set indent
371         *
372         * @param int $pValue
373         * @return PHPExcel_Style_Alignment
374         */
375        public function setIndent($pValue = 0) {
376                if ($pValue > 0) {
377                        if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
378                                $this->getHorizontal() != self::HORIZONTAL_LEFT &&
379                                $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
380                                $pValue = 0; // indent not supported
381                        }
382                }
383                if ($this->_isSupervisor) {
384                        $styleArray = $this->getStyleArray(array('indent' => $pValue));
385                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
386                } else {
387                        $this->_indent = $pValue;
388                }
389                return $this;
390        }
391
392        /**
393         * Get hash code
394         *
395         * @return string       Hash code
396         */
397        public function getHashCode() {
398                if ($this->_isSupervisor) {
399                        return $this->getSharedComponent()->getHashCode();
400                }
401                return md5(
402                          $this->_horizontal
403                        . $this->_vertical
404                        . $this->_textRotation
405                        . ($this->_wrapText ? 't' : 'f')
406                        . ($this->_shrinkToFit ? 't' : 'f')
407                        . $this->_indent
408                        . __CLASS__
409                );
410        }
411
412}
Note: See TracBrowser for help on using the repository browser.