Section.php 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
<?php
/*
    PHPRtfLite
    Copyright 2007-2008 Denis Slaveckij <sinedas@gmail.com>
    Copyright 2010-2012 Steffen Zeidler <sigma_z@sigma-scripts.de>

    This file is part of PHPRtfLite.

    PHPRtfLite is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PHPRtfLite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with PHPRtfLite.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * Class for creating sections within the rtf document.
 * @version     1.2
 * @author      Denis Slaveckij <sinedas@gmail.com>
 * @author      Steffen Zeidler <sigma_z@sigma-scripts.de>
 * @copyright   2007-2008 Denis Slaveckij, 2010-2012 Steffen Zeidler
 * @package     PHPRtfLite
 * @subpackage  PHPRtfLite_Container
 */
class PHPRtfLite_Container_Section extends PHPRtfLite_Container
{

    /**
     * border for section
     * @var PHPRtfLite_Border
     */
    protected $_border;

    /**
     * number of columns within the section
     * @var integer
     */
    protected $_numberOfColumns = 1;

    /**
     * array of column widths. only used when using more than one column within the section
     * @var array
     */
    protected $_columnWidths = array();

    /**
     * flag for not breaking within the section, if true do not break
     * @var boolean
     */
    protected $_doNotBreak = false;

    /**
     * flag, if true using lines between the section columns
     * @var boolean
     */
    protected $_lineBetweenColumns = false;

    /**
     * defines space between the section columns
     * @var float
     */
    protected $_spaceBetweenColumns;

    /**
     * flag, if true use landscape layout for this section, isLandscape of rtf must be set to portrait
     * @var boolean
     */
    private $_isLandscape = false;

    /**
     * paper width
     * @var float
     */
    protected $_paperWidth;

    /**
     * paper height
     * @var float
     */
    protected $_paperHeight;

    /**
     * left margin
     * @var float
     */
    protected $_marginLeft;

    /**
     * right margin
     * @var float
     */
    protected $_marginRight;

    /**
     * top margin
     * @var float
     */
    protected $_marginTop;

    /**
     * bottom margin
     * @var float
     */
    protected $_marginBottom;

    /**
     * gutter
     * @var float
     */
    protected $_gutter;

    /**
     * flag, if true margins will be the opposite for odd and even pages
     * @var boolean
     */
    protected $_useMirrorMargins;

    /**
     * rtf headers
     * @var PHPRtfLite_Container_Header[]
     */
    protected $_headers = array();

    /**
     * rtf footers
     * @var PHPRtfLite_Container_Footer[]
     */
    protected $_footers = array();

    /**
     * @var PHPRtfLite_Font
     */
    protected $_font;

    /**
     * @var bool
     */
    protected $_borderSurroundsHeader = false;

    /**
     * @var bool
     */
    protected $_borderSurroundsFooter = false;


    /**
     * sets landscape orientation for the section
     */
    public function setLandscape()
    {
        $this->_isLandscape = true;
    }


    /**
     * returns true, if landscape layout should be used
     *
     * @return boolean
     */
    public function isLandscape()
    {
        return $this->_isLandscape;
    }


    /**
     * sets the paper width of pages in section.
     *
     * @param float $width paper width
     */
    public function setPaperWidth($width)
    {
        $this->_paperWidth = $width;
    }


    /**
     * gets the paper width of pages in section.
     *
     * @return float paper width
     */
    public function getPaperWidth()
    {
        return $this->_paperWidth;
    }


    /**
     * sets the paper height of pages in section.
     *
     * @param float $height paper height
     */
    public function setPaperHeight($height)
    {
        $this->_paperHeight = $height;
    }


    /**
     * gets the paper height of pages in section.
     *
     * @return float paper height
     */
    public function getPaperHeight()
    {
        return $this->_paperHeight;
    }


    /**
     * sets the margins of pages in section.
     *
     * @param float $marginLeft Margin left
     * @param float $marginTop Margin top
     * @param float $marginRight Margin right
     * @param float $marginBottom Margin bottom
     */
    public function setMargins($marginLeft, $marginTop, $marginRight, $marginBottom)
    {
        $this->_marginLeft      = $marginLeft;
        $this->_marginTop       = $marginTop;
        $this->_marginRight     = $marginRight;
        $this->_marginBottom    = $marginBottom;
    }


    /**
     * sets the left margin of document pages.
     *
     * @param float $margin
     */
    public function setMarginLeft($margin)
    {
        $this->_marginLeft = $margin;
    }


    /**
     * gets the left margin of document pages.
     *
     * @return float $margin
     */
    public function getMarginLeft()
    {
        return $this->_marginLeft;
    }


    /**
     * sets the right margin of document pages.
     *
     * @param float $margin
     */
    public function setMarginRight($margin)
    {
        $this->_marginRight = $margin;
    }


    /**
     * gets the right margin of document pages.
     *
     * @return float $margin
     */
    public function getMarginRight()
    {
        return $this->_marginRight;
    }


    /**
     * sets the top margin of document pages.
     *
     * @param float $margin
     */
    public function setMarginTop($margin)
    {
        $this->_marginTop = $margin;
    }


    /**
     * gets the top margin of document pages.
     *
     * @return float $margin
     */
    public function getMarginTop()
    {
        return $this->_marginTop;
    }


    /**
     * sets the bottom margin of document pages.
     *
     * @param float $margin
     */
    public function setMarginBottom($margin)
    {
        $this->_marginBottom = $margin;
    }


    /**
     * gets the bottom margin of document pages.
     *
     * @return float $margin
     */
    public function getMarginBottom()
    {
        return $this->_marginBottom;
    }


    /**
     * sets the gutter width. <br>
     * NOTICE: Does note work with OpenOffice.
     *
     * @param float $gutter Gutter width
     */
    public function setGutter($gutter)
    {
        $this->_gutter = $gutter;
    }


    /**
     * gets the gutter width.
     *
     * @return float $gutter gutter width
     */
    public function getGutter()
    {
        return $this->_gutter;
    }


    /**
     * sets the margin definitions on left and right pages.<br>
     * Notice: Does not work with OpenOffice.
     */
    public function setMirrorMargins()
    {
        $this->_useMirrorMargins = true;
    }


    /**
     * returns true, if use mirror margins should be used
     *
     * @return boolean
     */
    public function isMirrorMargins()
    {
        return $this->_useMirrorMargins;
    }


    /**
     * gets width of page layout
     *
     * @return float
     * @throws PHPRtfLite_Exception thrown if paper layout width is lower or equal 0
     */
    public function getLayoutWidth()
    {
        $paperWidth     = $this->_paperWidth !== null   ? $this->_paperWidth    : $this->_rtf->getPaperWidth();
        $marginLeft     = $this->_marginLeft !== null   ? $this->_marginLeft    : $this->_rtf->getMarginLeft();
        $marginRight    = $this->_marginRight !== null  ? $this->_marginRight   : $this->_rtf->getMarginRight();
        $layoutWidth    = $paperWidth - $marginLeft - $marginRight;

        if ($layoutWidth <= 0) {
            throw new PHPRtfLite_Exception('The paper layout width is lower or equal zero!');
        }

        return $layoutWidth;
    }


    /**
     * sets border to rtf document
     *
     * @param PHPRtfLite_Border $border
     */
    public function setBorder(PHPRtfLite_Border $border)
    {
        $this->_border = $border;
    }


    /**
     * gets border of document
     *
     * @return PHPRtfLite_Border
     */
    public function getBorder()
    {
        return $this->_border;
    }


    /**
     * sets borders to rtf document.
     *
     * @param PHPRtfLite_Border_Format  $borderFormat
     * @param boolean                   $left
     * @param boolean                   $top
     * @param boolean                   $right
     * @param boolean                   $bottom
     */
    public function setBorders(PHPRtfLite_Border_Format $borderFormat,
                               $left = true, $top = true,
                               $right = true, $bottom = true)
    {
        if ($this->_border === null) {
            $this->_border = new PHPRtfLite_Border($this->_rtf);
        }
        $this->_border->setBorders($borderFormat, $left, $top, $right, $bottom);
    }


    /**
     * sets number of columns in section.
     *
     * @param integer $columnsCount number of columns
     */
    public function setNumberOfColumns($columnsCount)
    {
        $this->_numberOfColumns = $columnsCount;
        $this->_columnWidths    = array();
    }


    /**
     * gets number of columns in section.
     *
     * @return  integer $columnsCount number of columns
     */
    public function getNumberOfColumns()
    {
        return $this->_numberOfColumns;
    }


    /**
     * sets space (width) between columns
     *
     * @param float $spaceBetweenColumns Space between columns
     */
    public function setSpaceBetweenColumns($spaceBetweenColumns)
    {
        $this->_spaceBetweenColumns = $spaceBetweenColumns;
    }


    /**
     * gets space (width) between columns
     *
     * @return float
     */
    public function getSpaceBetweenColumns()
    {
        return $this->_spaceBetweenColumns;
    }


    /**
     * sets section columns with different widths.<br>
     * NOTE: If you use this function, you shouldn't use {@see setNumberOfColumns}.
     *
     * @param   array   $columnWidths array with columns widths
     * @throws  PHPRtfLite_Exception, if column widths are exceeding the defined layout width
     */
    public function setColumnWidths($columnWidths)
    {
        if (is_array($columnWidths)) {
            $this->_numberOfColumns = count($columnWidths);
            $layoutWidth = $this->getLayoutWidth();
            $usedWidth = array_sum($columnWidths);

            if ($usedWidth <= $layoutWidth) {
                $this->_columnWidths = $columnWidths;
            }
            else {
                throw new PHPRtfLite_Exception('The section column widths are exceeding the defined layout width!');
            }
        }
    }


    /**
     * gets widths for columns
     *
     * @return array
     */
    public function getColumnWidths()
    {
        return $this->_columnWidths;
    }


    /**
     * sets that it should not do a break within the section
     * NOTE: If foot notes are used in different sections, MS Word will always break sections.
     *
     * @param boolean $doNotBreak
     */
    public function setNoBreak($doNotBreak = true)
    {
        $this->_doNotBreak = $doNotBreak;
    }


    /**
     * sets line between columns.
     *
     * @param boolean $flag
     */
    public function setLineBetweenColumns($flag = true)
    {
        $this->_lineBetweenColumns = $flag;
    }


    /**
     * returns true, if line between columns is sets
     *
     * @return boolean
     */
    public function hasLineBetweenColumns()
    {
        return $this->_lineBetweenColumns;
    }


    /**
     * creates header for sections.
     *
     * @param string $type Represented by class constants PHPRtfLite_Container_Header::TYPE_*
     * Possible values: <br>
     *   PHPRtfLite_Container_Header::TYPE_ALL      => 'all' - all pages (different odd and even headers/footers must be not set) <br>
     *   PHPRtfLite_Container_Header::TYPE_LEFT     => 'left' - left pages (different odd and even headers/footers must be set) <br>
     *   PHPRtfLite_Container_Header::TYPE_RIGHT    => 'right' - right pages (different odd and even headers/footers must be set) <br>
     *   PHPRtfLite_Container_Header::TYPE_FIRST    => 'first' - first page
     *
     * @return PHPRtfLite_Container_Header
     */
    public function addHeader($type = PHPRtfLite_Container_Header::TYPE_ALL)
    {
        $header = new PHPRtfLite_Container_Header($this->_rtf, $type);
        $this->_headers[$type] = $header;

        return $header;
    }


    /**
     * gets defined headers for document pages.
     *
     * @return array contains PHPRtfLite_Container_Header objects
     */
    public function getHeaders()
    {
        return $this->_headers;
    }


    /**
     * creates footer for the document.
     *
     * @param string $type Represented by class constants PHPRtfLite_Container_Footer::TYPE_*
     *   PHPRtfLite_Container_Footer::TYPE_ALL      => 'all' - all pages (different odd and even headers/footers must be not set) <br>
     *   PHPRtfLite_Container_Footer::TYPE_LEFT     => 'left' - left pages (different odd and even headers/footers must be set) <br>
     *   PHPRtfLite_Container_Footer::TYPE_RIGHT    => 'right' - right pages (different odd and even headers/footers must be set)     <br>
     *   PHPRtfLite_Container_Footer::TYPE_FIRST    => 'first' - first page
     *
     * @return PHPRtfLite_Container_Footer
     */
    public function addFooter($type = PHPRtfLite_Container_Footer::TYPE_ALL)
    {
        $footer = new PHPRtfLite_Container_Footer($this->_rtf, $type);
        $this->_footers[$type] = $footer;

        return $footer;
    }


    /**
     * gets defined footers for document pages.
     *
     * @return array contains PHPRtfLite_Container_FOOTER objects
     */
    public function getFooters()
    {
        return $this->_footers;
    }


    /**
     * insert a page break
     */
    public function insertPageBreak()
    {
        $this->writeRtfCode('\page');
    }


    /**
     * @param PHPRtfLite_Font $font
     */
    public function setFont(PHPRtfLite_Font $font)
    {
        $this->_font = $font;
        $this->_rtf->registerFont($font);
    }


    /**
     * @return PHPRtfLite_Font
     */
    public function getFont()
    {
        return $this->_font;
    }


    /**
     * sets border surrounds header
     *
     * @param bool $borderSurroundsHeader
     * @return PHPRtfLite
     */
    public function setBorderSurroundsHeader($borderSurroundsHeader = true)
    {
        $this->_borderSurroundsHeader = $borderSurroundsHeader;
        return $this;
    }


    /**
     * checks, if border surrounds header
     *
     * @return bool
     */
    public function borderSurroundsHeader()
    {
        return $this->_borderSurroundsHeader;
    }


    /**
     * @param bool $borderSurroundsFooter
     * @return PHPRtfLite
     */
    public function setBorderSurroundsFooter($borderSurroundsFooter = true)
    {
        $this->_borderSurroundsFooter = $borderSurroundsFooter;
        return $this;
    }


    /**
     * checks, if border surrounds footer
     *
     * @return bool
     */
    public function borderSurroundsFooter()
    {
        return $this->_borderSurroundsFooter;
    }


    /**
     * renders rtf code of section
     */
    public function render()
    {
        $writer = $this->_rtf->getWriter();

        //headers
        $headers = $this->_headers ? $this->_headers : $this->_rtf->getHeaders();
        if (!empty($headers)) {
            foreach ($headers as $header) {
                $header->render();
            }
        }

        //footers
        $footers = $this->_footers ? $this->_footers : $this->_rtf->getFooters();
        if (!empty($footers)) {
            foreach ($footers as $footer) {
                $footer->render();
            }
        }

        //borders
        if ($this->_border) {
            if ($this->_borderSurroundsHeader) {
                $writer->write('\pgbrdrhead');
            }
            if ($this->_borderSurroundsFooter) {
                $writer->write('\pgbrdrfoot');
            }
            $writer->write($this->_border->getContent('\pg'));
        }
        else if ($border = $this->_rtf->getBorder()) {
            if ($this->_rtf->borderSurroundsHeader()) {
                $writer->write('\pgbrdrhead');
            }
            if ($this->_rtf->borderSurroundsFooter()) {
                $writer->write('\pgbrdrfoot');
            }
            $writer->write($border->getContent('\pg'));
        }

        //do not break within the section
        if ($this->_doNotBreak) {
            $writer->write('\sbknone ');
        }

        //set column index, when using more than one column for this section
        if ($this->_numberOfColumns > 1) {
            $writer->write('\cols' . $this->_numberOfColumns . ' ');
        }

        if (empty($this->_columnWidths)) {
            if ($this->_spaceBetweenColumns) {
                  $writer->write('\colsx' . PHPRtfLite_Unit::getUnitInTwips($this->_spaceBetweenColumns) . ' ');
            }
        }
        else {
            $width = 0;
            foreach ($this->_columnWidths as $value) {
                $width += PHPRtfLite_Unit::getUnitInTwips($value);
            }

            $printableWidth = $this->_rtf->getPaperWidth() - $this->_rtf->getMarginLeft() - $this->_rtf->getMarginRight();
            $space = round((PHPRtfLite_Unit::getUnitInTwips($printableWidth) - $width) / (count($this->_columnWidths) - 1));

            $i = 1;
            foreach ($this->_columnWidths as $key => $value) {
                $writer->write('\colno' . $i . '\colw' . PHPRtfLite_Unit::getUnitInTwips($value));
                if (!empty($this->_columnWidths[$key])) {
                    $writer->write('\colsr' . $space);
                }
                $i++;
            }
            $writer->write(' ');
        }

        if ($this->_lineBetweenColumns) {
            $writer->write('\linebetcol ');
        }

        /*---Page part---*/
        if ($this->_isLandscape) {
            $writer->write('\lndscpsxn ');
        }

        if ($this->_paperWidth) {
            $writer->write('\pgwsxn' . PHPRtfLite_Unit::getUnitInTwips($this->_paperWidth) . ' ');
        }

        if ($this->_paperHeight) {
            $writer->write('\pghsxn' . PHPRtfLite_Unit::getUnitInTwips($this->_paperHeight) . ' ');
        }

        if ($this->_marginLeft) {
            $writer->write('\marglsxn' . PHPRtfLite_Unit::getUnitInTwips($this->_marginLeft) . ' ');
        }

        if ($this->_marginRight) {
            $writer->write('\margrsxn' . PHPRtfLite_Unit::getUnitInTwips($this->_marginRight) . ' ');
        }

        if ($this->_marginTop) {
            $writer->write('\margtsxn' . PHPRtfLite_Unit::getUnitInTwips($this->_marginTop) . ' ');
        }

        if ($this->_marginBottom) {
            $writer->write('\margbsxn' . PHPRtfLite_Unit::getUnitInTwips($this->_marginBottom) . ' ');
        }

        if ($this->_gutter) {
            $writer->write('\guttersxn' . PHPRtfLite_Unit::getUnitInTwips($this->_gutter) . ' ');
        }

        if ($this->_useMirrorMargins) {
            $writer->write('\margmirsxn ');
        }

        if ($this->_font) {
            $writer->write($this->_font->getContent());
        }

        $writer->write("\r\n");
        parent::render();
        $writer->write("\r\n");
    }

}