~agasti-rhok-02/sahana-agasti/mayon-rhok-02

« back to all changes in this revision

Viewing changes to plugins/sfPhpExcelPlugin/lib/PHPExcel/PHPExcel/Style/Alignment.php

  • Committer: Chad Heuschober
  • Date: 2010-12-04 04:01:41 UTC
  • mfrom: (1.2.1 mayon)
  • Revision ID: chad.heuschober@mail.cuny.edu-20101204040141-orjw950gt411jzmt
Merged changes from r2 of project trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/**
3
3
 * PHPExcel
4
4
 *
5
 
 * Copyright (c) 2006 - 2009 PHPExcel
 
5
 * Copyright (c) 2006 - 2010 PHPExcel
6
6
 *
7
7
 * This library is free software; you can redistribute it and/or
8
8
 * modify it under the terms of the GNU Lesser General Public
9
9
 * License as published by the Free Software Foundation; either
10
10
 * version 2.1 of the License, or (at your option) any later version.
11
 
 * 
 
11
 *
12
12
 * This library is distributed in the hope that it will be useful,
13
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
15
 * Lesser General Public License for more details.
16
 
 * 
 
16
 *
17
17
 * You should have received a copy of the GNU Lesser General Public
18
18
 * License along with this library; if not, write to the Free Software
19
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
20
 *
21
21
 * @category   PHPExcel
22
22
 * @package    PHPExcel_Style
23
 
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 
23
 * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
24
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 
 * @version    ##VERSION##, ##DATE##
 
25
 * @version    1.7.4, 2010-08-26
26
26
 */
27
27
 
28
28
 
29
 
/** PHPExcel_IComparable */
30
 
require_once 'PHPExcel/IComparable.php';
31
 
 
32
 
 
33
29
/**
34
30
 * PHPExcel_Style_Alignment
35
31
 *
36
32
 * @category   PHPExcel
37
33
 * @package    PHPExcel_Style
38
 
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 
34
 * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
39
35
 */
40
36
class PHPExcel_Style_Alignment implements PHPExcel_IComparable
41
 
{       
 
37
{
42
38
        /* Horizontal alignment styles */
43
39
        const HORIZONTAL_GENERAL                                = 'general';
44
40
        const HORIZONTAL_LEFT                                   = 'left';
45
41
        const HORIZONTAL_RIGHT                                  = 'right';
46
42
        const HORIZONTAL_CENTER                                 = 'center';
 
43
        const HORIZONTAL_CENTER_CONTINUOUS              = 'centerContinuous';
47
44
        const HORIZONTAL_JUSTIFY                                = 'justify';
48
 
        
 
45
 
49
46
        /* Vertical alignment styles */
50
47
        const VERTICAL_BOTTOM                                   = 'bottom';
51
48
        const VERTICAL_TOP                                              = 'top';
52
49
        const VERTICAL_CENTER                                   = 'center';
53
50
        const VERTICAL_JUSTIFY                                  = 'justify';
54
 
        
 
51
 
55
52
        /**
56
53
         * Horizontal
57
54
         *
58
55
         * @var string
59
56
         */
60
57
        private $_horizontal;
61
 
        
 
58
 
62
59
        /**
63
60
         * Vertical
64
61
         *
65
62
         * @var string
66
63
         */
67
64
        private $_vertical;
68
 
        
 
65
 
69
66
        /**
70
67
         * Text rotation
71
68
         *
72
69
         * @var int
73
70
         */
74
71
        private $_textRotation;
75
 
        
 
72
 
76
73
        /**
77
74
         * Wrap text
78
75
         *
79
76
         * @var boolean
80
77
         */
81
78
        private $_wrapText;
82
 
        
 
79
 
83
80
        /**
84
81
         * Shrink to fit
85
82
         *
86
83
         * @var boolean
87
84
         */
88
85
        private $_shrinkToFit;
89
 
        
 
86
 
90
87
        /**
91
88
         * Indent - only possible with horizontal alignment left and right
92
89
         *
93
90
         * @var int
94
91
         */
95
92
        private $_indent;
96
 
        
97
 
        /**
98
 
         * Parent Style
99
 
         *
100
 
         * @var PHPExcel_Style
101
 
         */
102
 
         
103
 
        private $_parent;
104
 
        
 
93
 
105
94
        /**
106
95
         * Parent Borders
107
96
         *
108
97
         * @var _parentPropertyName string
109
98
         */
110
99
        private $_parentPropertyName;
111
 
                
 
100
 
 
101
        /**
 
102
         * Supervisor?
 
103
         *
 
104
         * @var boolean
 
105
         */
 
106
        private $_isSupervisor;
 
107
 
 
108
        /**
 
109
         * Parent. Only used for supervisor
 
110
         *
 
111
         * @var PHPExcel_Style
 
112
         */
 
113
        private $_parent;
 
114
 
112
115
    /**
113
116
     * Create a new PHPExcel_Style_Alignment
114
117
     */
115
 
    public function __construct()
 
118
    public function __construct($isSupervisor = false)
116
119
    {
 
120
        // Supervisor?
 
121
                $this->_isSupervisor = $isSupervisor;
 
122
 
117
123
        // Initialise values
118
124
        $this->_horizontal                      = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
119
125
        $this->_vertical                        = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
124
130
    }
125
131
 
126
132
        /**
127
 
         * Property Prepare bind
 
133
         * Bind parent. Only used for supervisor
128
134
         *
129
 
         * Configures this object for late binding as a property of a parent object
130
 
         *       
131
 
         * @param $parent
132
 
         * @param $parentPropertyName
 
135
         * @param PHPExcel $parent
 
136
         * @return PHPExcel_Style_Alignment
133
137
         */
134
 
        public function propertyPrepareBind($parent, $parentPropertyName)
 
138
        public function bindParent($parent)
135
139
        {
136
 
                // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
137
 
                // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
138
 
                $this->_parent                          = $parent;
139
 
                $this->_parentPropertyName      = $parentPropertyName;
140
 
        }
141
 
        
142
 
    /**
143
 
     * Property Get Bound
144
 
     *
145
 
     * Returns the PHPExcel_Style_Alignment that is actual bound to PHPExcel_Style
146
 
         *
147
 
         * @return PHPExcel_Style_Alignment
148
 
     */
149
 
        private function propertyGetBound() {
150
 
                if(!isset($this->_parent))
151
 
                        return $this;                                                                                                                           // I am bound
152
 
 
153
 
                if($this->_parent->propertyIsBound($this->_parentPropertyName))
154
 
                        return $this->_parent->getAlignment();                                                                          // Another one is bound
155
 
 
156
 
                return $this;                                                                                                                                   // No one is bound yet
157
 
        }
158
 
        
159
 
    /**
160
 
     * Property Begin Bind
161
 
     *
162
 
     * If no PHPExcel_Style_Alignment has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
163
 
         *
164
 
         * @return PHPExcel_Style_Alignment
165
 
     */
166
 
        private function propertyBeginBind() {
167
 
                if(!isset($this->_parent))
168
 
                        return $this;                                                                                                                           // I am already bound
169
 
 
170
 
                if($this->_parent->propertyIsBound($this->_parentPropertyName))
171
 
                        return $this->_parent->getAlignment();                                                                          // Another one is already bound
172
 
                        
173
 
                $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName);                // Bind myself
174
 
                $this->_parent = null;
175
 
                
 
140
                $this->_parent = $parent;
176
141
                return $this;
177
142
        }
178
 
        
 
143
 
 
144
        /**
 
145
         * Is this a supervisor or a real style component?
 
146
         *
 
147
         * @return boolean
 
148
         */
 
149
        public function getIsSupervisor()
 
150
        {
 
151
                return $this->_isSupervisor;
 
152
        }
 
153
 
 
154
        /**
 
155
         * Get the shared style component for the currently active cell in currently active sheet.
 
156
         * Only used for style supervisor
 
157
         *
 
158
         * @return PHPExcel_Style_Alignment
 
159
         */
 
160
        public function getSharedComponent()
 
161
        {
 
162
                return $this->_parent->getSharedComponent()->getAlignment();
 
163
        }
 
164
 
 
165
        /**
 
166
         * Get the currently active sheet. Only used for supervisor
 
167
         *
 
168
         * @return PHPExcel_Worksheet
 
169
         */
 
170
        public function getActiveSheet()
 
171
        {
 
172
                return $this->_parent->getActiveSheet();
 
173
        }
 
174
 
 
175
        /**
 
176
         * Get the currently active cell coordinate in currently active sheet.
 
177
         * Only used for supervisor
 
178
         *
 
179
         * @return string E.g. 'A1'
 
180
         */
 
181
        public function getSelectedCells()
 
182
        {
 
183
                return $this->getActiveSheet()->getSelectedCells();
 
184
        }
 
185
 
 
186
        /**
 
187
         * Get the currently active cell coordinate in currently active sheet.
 
188
         * Only used for supervisor
 
189
         *
 
190
         * @return string E.g. 'A1'
 
191
         */
 
192
        public function getActiveCell()
 
193
        {
 
194
                return $this->getActiveSheet()->getActiveCell();
 
195
        }
 
196
 
 
197
        /**
 
198
         * Build style array from subcomponents
 
199
         *
 
200
         * @param array $array
 
201
         * @return array
 
202
         */
 
203
        public function getStyleArray($array)
 
204
        {
 
205
                return array('alignment' => $array);
 
206
        }
 
207
 
179
208
    /**
180
209
     * Apply styles from array
181
 
     * 
 
210
     *
182
211
     * <code>
183
212
     * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
184
213
     *          array(
189
218
     *          )
190
219
     * );
191
220
     * </code>
192
 
     * 
 
221
     *
193
222
     * @param   array   $pStyles        Array containing style information
194
223
     * @throws  Exception
 
224
     * @return PHPExcel_Style_Alignment
195
225
     */
196
 
    public function applyFromArray($pStyles = null) {
197
 
        if (is_array($pStyles)) {
198
 
                if (array_key_exists('horizontal', $pStyles)) {
199
 
                        $this->setHorizontal($pStyles['horizontal']);
200
 
                }
201
 
                if (array_key_exists('vertical', $pStyles)) {
202
 
                        $this->setVertical($pStyles['vertical']);
203
 
                }
204
 
                if (array_key_exists('rotation', $pStyles)) {
205
 
                        $this->setTextRotation($pStyles['rotation']);
206
 
                }
207
 
                if (array_key_exists('wrap', $pStyles)) {
208
 
                        $this->setWrapText($pStyles['wrap']);
209
 
                }
210
 
                if (array_key_exists('shrinkToFit', $pStyles)) {
211
 
                        $this->setShrinkToFit($pStyles['shrinkToFit']);
212
 
                }
213
 
                if (array_key_exists('indent', $pStyles)) {
214
 
                        $this->setIndent($pStyles['indent']);
215
 
                }
216
 
        } else {
217
 
                throw new Exception("Invalid style array passed.");
218
 
        }
219
 
    }
220
 
    
 
226
        public function applyFromArray($pStyles = null) {
 
227
                if (is_array($pStyles)) {
 
228
                        if ($this->_isSupervisor) {
 
229
                                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
 
230
                        } else {
 
231
                                if (array_key_exists('horizontal', $pStyles)) {
 
232
                                        $this->setHorizontal($pStyles['horizontal']);
 
233
                                }
 
234
                                if (array_key_exists('vertical', $pStyles)) {
 
235
                                        $this->setVertical($pStyles['vertical']);
 
236
                                }
 
237
                                if (array_key_exists('rotation', $pStyles)) {
 
238
                                        $this->setTextRotation($pStyles['rotation']);
 
239
                                }
 
240
                                if (array_key_exists('wrap', $pStyles)) {
 
241
                                        $this->setWrapText($pStyles['wrap']);
 
242
                                }
 
243
                                if (array_key_exists('shrinkToFit', $pStyles)) {
 
244
                                        $this->setShrinkToFit($pStyles['shrinkToFit']);
 
245
                                }
 
246
                                if (array_key_exists('indent', $pStyles)) {
 
247
                                        $this->setIndent($pStyles['indent']);
 
248
                                }
 
249
                        }
 
250
                } else {
 
251
                        throw new Exception("Invalid style array passed.");
 
252
                }
 
253
                return $this;
 
254
        }
 
255
 
221
256
    /**
222
257
     * Get Horizontal
223
258
     *
224
259
     * @return string
225
260
     */
226
261
    public function getHorizontal() {
227
 
        return $this->propertyGetBound()->_horizontal;
 
262
                if ($this->_isSupervisor) {
 
263
                        return $this->getSharedComponent()->getHorizontal();
 
264
                }
 
265
        return $this->_horizontal;
228
266
    }
229
 
    
 
267
 
230
268
    /**
231
269
     * Set Horizontal
232
270
     *
233
271
     * @param string $pValue
 
272
     * @return PHPExcel_Style_Alignment
234
273
     */
235
274
    public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL) {
236
275
        if ($pValue == '') {
237
276
                $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
238
277
        }
239
 
        $this->propertyBeginBind()->_horizontal = $pValue;
 
278
 
 
279
                if ($this->_isSupervisor) {
 
280
                        $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
 
281
                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 
282
                }
 
283
                else {
 
284
                        $this->_horizontal = $pValue;
 
285
                }
 
286
                return $this;
240
287
    }
241
 
    
 
288
 
242
289
    /**
243
290
     * Get Vertical
244
291
     *
245
292
     * @return string
246
293
     */
247
294
    public function getVertical() {
248
 
        return $this->propertyGetBound()->_vertical;
 
295
                if ($this->_isSupervisor) {
 
296
                        return $this->getSharedComponent()->getVertical();
 
297
                }
 
298
        return $this->_vertical;
249
299
    }
250
 
    
 
300
 
251
301
    /**
252
302
     * Set Vertical
253
303
     *
254
304
     * @param string $pValue
 
305
     * @return PHPExcel_Style_Alignment
255
306
     */
256
307
    public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM) {
257
308
        if ($pValue == '') {
258
309
                $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
259
310
        }
260
 
        $this->propertyBeginBind()->_vertical = $pValue;
 
311
 
 
312
                if ($this->_isSupervisor) {
 
313
                        $styleArray = $this->getStyleArray(array('vertical' => $pValue));
 
314
                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 
315
                } else {
 
316
                        $this->_vertical = $pValue;
 
317
                }
 
318
                return $this;
261
319
    }
262
 
    
 
320
 
263
321
    /**
264
322
     * Get TextRotation
265
323
     *
266
324
     * @return int
267
325
     */
268
326
    public function getTextRotation() {
269
 
        return $this->propertyGetBound()->_textRotation;
 
327
                if ($this->_isSupervisor) {
 
328
                        return $this->getSharedComponent()->getTextRotation();
 
329
                }
 
330
        return $this->_textRotation;
270
331
    }
271
 
    
 
332
 
272
333
    /**
273
334
     * Set TextRotation
274
335
     *
275
336
     * @param int $pValue
276
337
     * @throws Exception
 
338
     * @return PHPExcel_Style_Alignment
277
339
     */
278
340
    public function setTextRotation($pValue = 0) {
279
 
        // Excel2007 value 255 => PHPExcel value -165
 
341
                // Excel2007 value 255 => PHPExcel value -165
280
342
        if ($pValue == 255) {
281
343
                $pValue = -165;
282
344
        }
283
345
 
284
 
        // Set rotation
 
346
                // Set rotation
285
347
        if ( ($pValue >= -90 && $pValue <= 90) || $pValue == -165 ) {
286
 
                $this->propertyBeginBind()->_textRotation = $pValue;
 
348
                        if ($this->_isSupervisor) {
 
349
                                $styleArray = $this->getStyleArray(array('rotation' => $pValue));
 
350
                                $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 
351
                        } else {
 
352
                                $this->_textRotation = $pValue;
 
353
                        }
287
354
        } else {
288
355
                throw new Exception("Text rotation should be a value between -90 and 90.");
289
356
        }
 
357
 
 
358
        return $this;
290
359
    }
291
 
    
 
360
 
292
361
    /**
293
362
     * Get Wrap Text
294
363
     *
295
364
     * @return boolean
296
365
     */
297
366
    public function getWrapText() {
298
 
        return $this->propertyGetBound()->_wrapText;
 
367
                if ($this->_isSupervisor) {
 
368
                        return $this->getSharedComponent()->getWrapText();
 
369
                }
 
370
        return $this->_wrapText;
299
371
    }
300
 
    
 
372
 
301
373
    /**
302
374
     * Set Wrap Text
303
375
     *
304
376
     * @param boolean $pValue
 
377
     * @return PHPExcel_Style_Alignment
305
378
     */
306
379
    public function setWrapText($pValue = false) {
307
380
        if ($pValue == '') {
308
381
                $pValue = false;
309
382
        }
310
 
        $this->propertyBeginBind()->_wrapText = $pValue;
 
383
                if ($this->_isSupervisor) {
 
384
                        $styleArray = $this->getStyleArray(array('wrap' => $pValue));
 
385
                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 
386
                } else {
 
387
                        $this->_wrapText = $pValue;
 
388
                }
 
389
                return $this;
311
390
    }
312
 
        
 
391
 
313
392
    /**
314
393
     * Get Shrink to fit
315
394
     *
316
395
     * @return boolean
317
396
     */
318
397
    public function getShrinkToFit() {
319
 
        return $this->propertyGetBound()->_shrinkToFit;
 
398
                if ($this->_isSupervisor) {
 
399
                        return $this->getSharedComponent()->getShrinkToFit();
 
400
                }
 
401
        return $this->_shrinkToFit;
320
402
    }
321
 
    
 
403
 
322
404
    /**
323
405
     * Set Shrink to fit
324
406
     *
325
407
     * @param boolean $pValue
 
408
     * @return PHPExcel_Style_Alignment
326
409
     */
327
410
    public function setShrinkToFit($pValue = false) {
328
411
        if ($pValue == '') {
329
412
                $pValue = false;
330
413
        }
331
 
        $this->propertyBeginBind()->_shrinkToFit = $pValue;
 
414
                if ($this->_isSupervisor) {
 
415
                        $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
 
416
                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 
417
                } else {
 
418
                        $this->_shrinkToFit = $pValue;
 
419
                }
 
420
                return $this;
332
421
    }
333
422
 
334
423
    /**
337
426
     * @return int
338
427
     */
339
428
    public function getIndent() {
340
 
        return $this->propertyGetBound()->_indent;
 
429
                if ($this->_isSupervisor) {
 
430
                        return $this->getSharedComponent()->getIndent();
 
431
                }
 
432
        return $this->_indent;
341
433
    }
342
 
    
 
434
 
343
435
    /**
344
436
     * Set indent
345
437
     *
346
438
     * @param int $pValue
 
439
     * @return PHPExcel_Style_Alignment
347
440
     */
348
441
    public function setIndent($pValue = 0) {
349
442
                if ($pValue > 0) {
351
444
                                $pValue = 0; // indent not supported
352
445
                        }
353
446
                }
354
 
                
355
 
                $this->propertyBeginBind()->_indent = $pValue;
 
447
                if ($this->_isSupervisor) {
 
448
                        $styleArray = $this->getStyleArray(array('indent' => $pValue));
 
449
                        $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 
450
                } else {
 
451
                        $this->_indent = $pValue;
 
452
                }
 
453
                return $this;
356
454
    }
357
 
        
 
455
 
358
456
        /**
359
457
         * Get hash code
360
458
         *
361
459
         * @return string       Hash code
362
 
         */     
 
460
         */
363
461
        public function getHashCode() {
364
 
                $property = $this->propertyGetBound();
 
462
                if ($this->_isSupervisor) {
 
463
                        return $this->getSharedComponent()->getHashCode();
 
464
                }
365
465
        return md5(
366
 
                  $property->_horizontal
367
 
                . $property->_vertical
368
 
                . $property->_textRotation
369
 
                . ($property->_wrapText ? 't' : 'f')
370
 
                . ($property->_shrinkToFit ? 't' : 'f')
371
 
                        . $property->_indent
 
466
                  $this->_horizontal
 
467
                . $this->_vertical
 
468
                . $this->_textRotation
 
469
                . ($this->_wrapText ? 't' : 'f')
 
470
                . ($this->_shrinkToFit ? 't' : 'f')
 
471
                        . $this->_indent
372
472
                . __CLASS__
373
473
        );
374
474
    }
375
 
    
376
 
    /**
377
 
     * Hash index
378
 
     *
379
 
     * @var string
380
 
     */
381
 
    private $_hashIndex;
382
 
    
383
 
        /**
384
 
         * Get hash index
385
 
         * 
386
 
         * Note that this index may vary during script execution! Only reliable moment is
387
 
         * while doing a write of a workbook and when changes are not allowed.
388
 
         *
389
 
         * @return string       Hash index
390
 
         */
391
 
        public function getHashIndex() {
392
 
                return $this->_hashIndex;
393
 
        }
394
 
        
395
 
        /**
396
 
         * Set hash index
397
 
         * 
398
 
         * Note that this index may vary during script execution! Only reliable moment is
399
 
         * while doing a write of a workbook and when changes are not allowed.
400
 
         *
401
 
         * @param string        $value  Hash index
402
 
         */
403
 
        public function setHashIndex($value) {
404
 
                $this->_hashIndex = $value;
405
 
        }
406
 
    
 
475
 
407
476
        /**
408
477
         * Implement PHP __clone to create a deep clone, not just a shallow copy.
409
478
         */
410
479
        public function __clone() {
411
480
                $vars = get_object_vars($this);
412
481
                foreach ($vars as $key => $value) {
413
 
                        if (is_object($value)) {
 
482
                        if ((is_object($value)) && ($key != '_parent')) {
414
483
                                $this->$key = clone $value;
415
484
                        } else {
416
485
                                $this->$key = $value;