Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Worksheet;
   4  
   5  /**
   6   * <code>
   7   * Header/Footer Formatting Syntax taken from Office Open XML Part 4 - Markup Language Reference, page 1970:.
   8   *
   9   * There are a number of formatting codes that can be written inline with the actual header / footer text, which
  10   * affect the formatting in the header or footer.
  11   *
  12   * Example: This example shows the text "Center Bold Header" on the first line (center section), and the date on
  13   * the second line (center section).
  14   *         &CCenter &"-,Bold"Bold&"-,Regular"Header_x000A_&D
  15   *
  16   * General Rules:
  17   * There is no required order in which these codes must appear.
  18   *
  19   * The first occurrence of the following codes turns the formatting ON, the second occurrence turns it OFF again:
  20   * - strikethrough
  21   * - superscript
  22   * - subscript
  23   * Superscript and subscript cannot both be ON at same time. Whichever comes first wins and the other is ignored,
  24   * while the first is ON.
  25   * &L - code for "left section" (there are three header / footer locations, "left", "center", and "right"). When
  26   * two or more occurrences of this section marker exist, the contents from all markers are concatenated, in the
  27   * order of appearance, and placed into the left section.
  28   * &P - code for "current page #"
  29   * &N - code for "total pages"
  30   * &font size - code for "text font size", where font size is a font size in points.
  31   * &K - code for "text font color"
  32   * RGB Color is specified as RRGGBB
  33   * Theme Color is specifed as TTSNN where TT is the theme color Id, S is either "+" or "-" of the tint/shade
  34   * value, NN is the tint/shade value.
  35   * &S - code for "text strikethrough" on / off
  36   * &X - code for "text super script" on / off
  37   * &Y - code for "text subscript" on / off
  38   * &C - code for "center section". When two or more occurrences of this section marker exist, the contents
  39   * from all markers are concatenated, in the order of appearance, and placed into the center section.
  40   *
  41   * &D - code for "date"
  42   * &T - code for "time"
  43   * &G - code for "picture as background"
  44   * &U - code for "text single underline"
  45   * &E - code for "double underline"
  46   * &R - code for "right section". When two or more occurrences of this section marker exist, the contents
  47   * from all markers are concatenated, in the order of appearance, and placed into the right section.
  48   * &Z - code for "this workbook's file path"
  49   * &F - code for "this workbook's file name"
  50   * &A - code for "sheet tab name"
  51   * &+ - code for add to page #.
  52   * &- - code for subtract from page #.
  53   * &"font name,font type" - code for "text font name" and "text font type", where font name and font type
  54   * are strings specifying the name and type of the font, separated by a comma. When a hyphen appears in font
  55   * name, it means "none specified". Both of font name and font type can be localized values.
  56   * &"-,Bold" - code for "bold font style"
  57   * &B - also means "bold font style".
  58   * &"-,Regular" - code for "regular font style"
  59   * &"-,Italic" - code for "italic font style"
  60   * &I - also means "italic font style"
  61   * &"-,Bold Italic" code for "bold italic font style"
  62   * &O - code for "outline style"
  63   * &H - code for "shadow style"
  64   * </code>
  65   */
  66  class HeaderFooter
  67  {
  68      // Header/footer image location
  69      const IMAGE_HEADER_LEFT = 'LH';
  70      const IMAGE_HEADER_CENTER = 'CH';
  71      const IMAGE_HEADER_RIGHT = 'RH';
  72      const IMAGE_FOOTER_LEFT = 'LF';
  73      const IMAGE_FOOTER_CENTER = 'CF';
  74      const IMAGE_FOOTER_RIGHT = 'RF';
  75  
  76      /**
  77       * OddHeader.
  78       *
  79       * @var string
  80       */
  81      private $oddHeader = '';
  82  
  83      /**
  84       * OddFooter.
  85       *
  86       * @var string
  87       */
  88      private $oddFooter = '';
  89  
  90      /**
  91       * EvenHeader.
  92       *
  93       * @var string
  94       */
  95      private $evenHeader = '';
  96  
  97      /**
  98       * EvenFooter.
  99       *
 100       * @var string
 101       */
 102      private $evenFooter = '';
 103  
 104      /**
 105       * FirstHeader.
 106       *
 107       * @var string
 108       */
 109      private $firstHeader = '';
 110  
 111      /**
 112       * FirstFooter.
 113       *
 114       * @var string
 115       */
 116      private $firstFooter = '';
 117  
 118      /**
 119       * Different header for Odd/Even, defaults to false.
 120       *
 121       * @var bool
 122       */
 123      private $differentOddEven = false;
 124  
 125      /**
 126       * Different header for first page, defaults to false.
 127       *
 128       * @var bool
 129       */
 130      private $differentFirst = false;
 131  
 132      /**
 133       * Scale with document, defaults to true.
 134       *
 135       * @var bool
 136       */
 137      private $scaleWithDocument = true;
 138  
 139      /**
 140       * Align with margins, defaults to true.
 141       *
 142       * @var bool
 143       */
 144      private $alignWithMargins = true;
 145  
 146      /**
 147       * Header/footer images.
 148       *
 149       * @var HeaderFooterDrawing[]
 150       */
 151      private $headerFooterImages = [];
 152  
 153      /**
 154       * Create a new HeaderFooter.
 155       */
 156      public function __construct()
 157      {
 158      }
 159  
 160      /**
 161       * Get OddHeader.
 162       *
 163       * @return string
 164       */
 165      public function getOddHeader()
 166      {
 167          return $this->oddHeader;
 168      }
 169  
 170      /**
 171       * Set OddHeader.
 172       *
 173       * @param string $oddHeader
 174       *
 175       * @return $this
 176       */
 177      public function setOddHeader($oddHeader)
 178      {
 179          $this->oddHeader = $oddHeader;
 180  
 181          return $this;
 182      }
 183  
 184      /**
 185       * Get OddFooter.
 186       *
 187       * @return string
 188       */
 189      public function getOddFooter()
 190      {
 191          return $this->oddFooter;
 192      }
 193  
 194      /**
 195       * Set OddFooter.
 196       *
 197       * @param string $oddFooter
 198       *
 199       * @return $this
 200       */
 201      public function setOddFooter($oddFooter)
 202      {
 203          $this->oddFooter = $oddFooter;
 204  
 205          return $this;
 206      }
 207  
 208      /**
 209       * Get EvenHeader.
 210       *
 211       * @return string
 212       */
 213      public function getEvenHeader()
 214      {
 215          return $this->evenHeader;
 216      }
 217  
 218      /**
 219       * Set EvenHeader.
 220       *
 221       * @param string $eventHeader
 222       *
 223       * @return $this
 224       */
 225      public function setEvenHeader($eventHeader)
 226      {
 227          $this->evenHeader = $eventHeader;
 228  
 229          return $this;
 230      }
 231  
 232      /**
 233       * Get EvenFooter.
 234       *
 235       * @return string
 236       */
 237      public function getEvenFooter()
 238      {
 239          return $this->evenFooter;
 240      }
 241  
 242      /**
 243       * Set EvenFooter.
 244       *
 245       * @param string $evenFooter
 246       *
 247       * @return $this
 248       */
 249      public function setEvenFooter($evenFooter)
 250      {
 251          $this->evenFooter = $evenFooter;
 252  
 253          return $this;
 254      }
 255  
 256      /**
 257       * Get FirstHeader.
 258       *
 259       * @return string
 260       */
 261      public function getFirstHeader()
 262      {
 263          return $this->firstHeader;
 264      }
 265  
 266      /**
 267       * Set FirstHeader.
 268       *
 269       * @param string $firstHeader
 270       *
 271       * @return $this
 272       */
 273      public function setFirstHeader($firstHeader)
 274      {
 275          $this->firstHeader = $firstHeader;
 276  
 277          return $this;
 278      }
 279  
 280      /**
 281       * Get FirstFooter.
 282       *
 283       * @return string
 284       */
 285      public function getFirstFooter()
 286      {
 287          return $this->firstFooter;
 288      }
 289  
 290      /**
 291       * Set FirstFooter.
 292       *
 293       * @param string $firstFooter
 294       *
 295       * @return $this
 296       */
 297      public function setFirstFooter($firstFooter)
 298      {
 299          $this->firstFooter = $firstFooter;
 300  
 301          return $this;
 302      }
 303  
 304      /**
 305       * Get DifferentOddEven.
 306       *
 307       * @return bool
 308       */
 309      public function getDifferentOddEven()
 310      {
 311          return $this->differentOddEven;
 312      }
 313  
 314      /**
 315       * Set DifferentOddEven.
 316       *
 317       * @param bool $differentOddEvent
 318       *
 319       * @return $this
 320       */
 321      public function setDifferentOddEven($differentOddEvent)
 322      {
 323          $this->differentOddEven = $differentOddEvent;
 324  
 325          return $this;
 326      }
 327  
 328      /**
 329       * Get DifferentFirst.
 330       *
 331       * @return bool
 332       */
 333      public function getDifferentFirst()
 334      {
 335          return $this->differentFirst;
 336      }
 337  
 338      /**
 339       * Set DifferentFirst.
 340       *
 341       * @param bool $differentFirst
 342       *
 343       * @return $this
 344       */
 345      public function setDifferentFirst($differentFirst)
 346      {
 347          $this->differentFirst = $differentFirst;
 348  
 349          return $this;
 350      }
 351  
 352      /**
 353       * Get ScaleWithDocument.
 354       *
 355       * @return bool
 356       */
 357      public function getScaleWithDocument()
 358      {
 359          return $this->scaleWithDocument;
 360      }
 361  
 362      /**
 363       * Set ScaleWithDocument.
 364       *
 365       * @param bool $scaleWithDocument
 366       *
 367       * @return $this
 368       */
 369      public function setScaleWithDocument($scaleWithDocument)
 370      {
 371          $this->scaleWithDocument = $scaleWithDocument;
 372  
 373          return $this;
 374      }
 375  
 376      /**
 377       * Get AlignWithMargins.
 378       *
 379       * @return bool
 380       */
 381      public function getAlignWithMargins()
 382      {
 383          return $this->alignWithMargins;
 384      }
 385  
 386      /**
 387       * Set AlignWithMargins.
 388       *
 389       * @param bool $alignWithMargins
 390       *
 391       * @return $this
 392       */
 393      public function setAlignWithMargins($alignWithMargins)
 394      {
 395          $this->alignWithMargins = $alignWithMargins;
 396  
 397          return $this;
 398      }
 399  
 400      /**
 401       * Add header/footer image.
 402       *
 403       * @param string $location
 404       *
 405       * @return $this
 406       */
 407      public function addImage(HeaderFooterDrawing $image, $location = self::IMAGE_HEADER_LEFT)
 408      {
 409          $this->headerFooterImages[$location] = $image;
 410  
 411          return $this;
 412      }
 413  
 414      /**
 415       * Remove header/footer image.
 416       *
 417       * @param string $location
 418       *
 419       * @return $this
 420       */
 421      public function removeImage($location = self::IMAGE_HEADER_LEFT)
 422      {
 423          if (isset($this->headerFooterImages[$location])) {
 424              unset($this->headerFooterImages[$location]);
 425          }
 426  
 427          return $this;
 428      }
 429  
 430      /**
 431       * Set header/footer images.
 432       *
 433       * @param HeaderFooterDrawing[] $images
 434       *
 435       * @return $this
 436       */
 437      public function setImages(array $images)
 438      {
 439          $this->headerFooterImages = $images;
 440  
 441          return $this;
 442      }
 443  
 444      /**
 445       * Get header/footer images.
 446       *
 447       * @return HeaderFooterDrawing[]
 448       */
 449      public function getImages()
 450      {
 451          // Sort array
 452          $images = [];
 453          if (isset($this->headerFooterImages[self::IMAGE_HEADER_LEFT])) {
 454              $images[self::IMAGE_HEADER_LEFT] = $this->headerFooterImages[self::IMAGE_HEADER_LEFT];
 455          }
 456          if (isset($this->headerFooterImages[self::IMAGE_HEADER_CENTER])) {
 457              $images[self::IMAGE_HEADER_CENTER] = $this->headerFooterImages[self::IMAGE_HEADER_CENTER];
 458          }
 459          if (isset($this->headerFooterImages[self::IMAGE_HEADER_RIGHT])) {
 460              $images[self::IMAGE_HEADER_RIGHT] = $this->headerFooterImages[self::IMAGE_HEADER_RIGHT];
 461          }
 462          if (isset($this->headerFooterImages[self::IMAGE_FOOTER_LEFT])) {
 463              $images[self::IMAGE_FOOTER_LEFT] = $this->headerFooterImages[self::IMAGE_FOOTER_LEFT];
 464          }
 465          if (isset($this->headerFooterImages[self::IMAGE_FOOTER_CENTER])) {
 466              $images[self::IMAGE_FOOTER_CENTER] = $this->headerFooterImages[self::IMAGE_FOOTER_CENTER];
 467          }
 468          if (isset($this->headerFooterImages[self::IMAGE_FOOTER_RIGHT])) {
 469              $images[self::IMAGE_FOOTER_RIGHT] = $this->headerFooterImages[self::IMAGE_FOOTER_RIGHT];
 470          }
 471          $this->headerFooterImages = $images;
 472  
 473          return $this->headerFooterImages;
 474      }
 475  
 476      /**
 477       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 478       */
 479      public function __clone()
 480      {
 481          $vars = get_object_vars($this);
 482          foreach ($vars as $key => $value) {
 483              if (is_object($value)) {
 484                  $this->$key = clone $value;
 485              } else {
 486                  $this->$key = $value;
 487              }
 488          }
 489      }
 490  }