TOC.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  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 PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_TOC
  29. *
  30. * @category PHPWord
  31. * @package PHPWord_TOC
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_TOC {
  35. /**
  36. * Title Elements
  37. *
  38. * @var array
  39. */
  40. private static $_titles = array();
  41. /**
  42. * TOC Style
  43. *
  44. * @var array
  45. */
  46. private static $_styleTOC;
  47. /**
  48. * Font Style
  49. *
  50. * @var array
  51. */
  52. private static $_styleFont;
  53. /**
  54. * Title Anchor
  55. *
  56. * @var array
  57. */
  58. private static $_anchor = 252634154;
  59. /**
  60. * Title Bookmark
  61. *
  62. * @var array
  63. */
  64. private static $_bookmarkId = 0;
  65. /**
  66. * Create a new Table-of-Contents Element
  67. *
  68. * @param array $styleFont
  69. * @param array $styleTOC
  70. */
  71. public function __construct($styleFont = null, $styleTOC = null) {
  72. self::$_styleTOC = new PHPWord_Style_TOC();
  73. if(!is_null($styleTOC) && is_array($styleTOC)) {
  74. foreach($styleTOC as $key => $value) {
  75. if(substr($key, 0, 1) != '_') {
  76. $key = '_'.$key;
  77. }
  78. self::$_styleTOC->setStyleValue($key, $value);
  79. }
  80. }
  81. if(!is_null($styleFont)) {
  82. if(is_array($styleFont)) {
  83. self::$_styleFont = new PHPWord_Style_Font();
  84. foreach($styleFont as $key => $value) {
  85. if(substr($key, 0, 1) != '_') {
  86. $key = '_'.$key;
  87. }
  88. self::$_styleFont->setStyleValue($key, $value);
  89. }
  90. } else {
  91. self::$_styleFont = $styleFont;
  92. }
  93. }
  94. }
  95. /**
  96. * Add a Title
  97. *
  98. * @return array
  99. */
  100. public static function addTitle($text, $depth = 0) {
  101. $anchor = '_Toc'.++self::$_anchor;
  102. $bookmarkId = self::$_bookmarkId++;
  103. $title = array();
  104. $title['text'] = $text;
  105. $title['depth'] = $depth;
  106. $title['anchor'] = $anchor;
  107. $title['bookmarkId'] = $bookmarkId;
  108. self::$_titles[] = $title;
  109. return array($anchor, $bookmarkId);
  110. }
  111. /**
  112. * Get all titles
  113. *
  114. * @return array
  115. */
  116. public static function getTitles() {
  117. return self::$_titles;
  118. }
  119. /**
  120. * Get TOC Style
  121. *
  122. * @return PHPWord_Style_TOC
  123. */
  124. public static function getStyleTOC() {
  125. return self::$_styleTOC;
  126. }
  127. /**
  128. * Get Font Style
  129. *
  130. * @return PHPWord_Style_Font
  131. */
  132. public static function getStyleFont() {
  133. return self::$_styleFont;
  134. }
  135. }
  136. ?>