AbstractElement.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Element;
  18. use PhpOffice\PhpWord\Media;
  19. use PhpOffice\PhpWord\PhpWord;
  20. /**
  21. * Element abstract class
  22. *
  23. * @since 0.10.0
  24. */
  25. abstract class AbstractElement
  26. {
  27. /**
  28. * PhpWord object
  29. *
  30. * @var \PhpOffice\PhpWord\PhpWord
  31. */
  32. protected $phpWord;
  33. /**
  34. * Section Id
  35. *
  36. * @var int
  37. */
  38. protected $sectionId;
  39. /**
  40. * Document part type: Section|Header|Footer|Footnote|Endnote
  41. *
  42. * Used by textrun and cell container to determine where the element is
  43. * located because it will affect the availability of other element,
  44. * e.g. footnote will not be available when $docPart is header or footer.
  45. *
  46. * @var string
  47. */
  48. protected $docPart = 'Section';
  49. /**
  50. * Document part Id
  51. *
  52. * For header and footer, this will be = ($sectionId - 1) * 3 + $index
  53. * because the max number of header/footer in every page is 3, i.e.
  54. * AUTO, FIRST, and EVEN (AUTO = ODD)
  55. *
  56. * @var int
  57. */
  58. protected $docPartId = 1;
  59. /**
  60. * Index of element in the elements collection (start with 1)
  61. *
  62. * @var int
  63. */
  64. protected $elementIndex = 1;
  65. /**
  66. * Unique Id for element
  67. *
  68. * @var string
  69. */
  70. protected $elementId;
  71. /**
  72. * Relation Id
  73. *
  74. * @var int
  75. */
  76. protected $relationId;
  77. /**
  78. * Depth of table container nested level; Primarily used for RTF writer/reader
  79. *
  80. * 0 = Not in a table; 1 = in a table; 2 = in a table inside another table, etc.
  81. *
  82. * @var int
  83. */
  84. private $nestedLevel = 0;
  85. /**
  86. * A reference to the parent
  87. *
  88. * @var AbstractElement|null
  89. */
  90. private $parent;
  91. /**
  92. * changed element info
  93. *
  94. * @var TrackChange
  95. */
  96. private $trackChange;
  97. /**
  98. * Parent container type
  99. *
  100. * @var string
  101. */
  102. private $parentContainer;
  103. /**
  104. * Has media relation flag; true for Link, Image, and Object
  105. *
  106. * @var bool
  107. */
  108. protected $mediaRelation = false;
  109. /**
  110. * Is part of collection; true for Title, Footnote, Endnote, Chart, and Comment
  111. *
  112. * @var bool
  113. */
  114. protected $collectionRelation = false;
  115. /**
  116. * The start position for the linked comment
  117. *
  118. * @var Comment
  119. */
  120. protected $commentRangeStart;
  121. /**
  122. * The end position for the linked comment
  123. *
  124. * @var Comment
  125. */
  126. protected $commentRangeEnd;
  127. /**
  128. * Get PhpWord
  129. *
  130. * @return \PhpOffice\PhpWord\PhpWord
  131. */
  132. public function getPhpWord()
  133. {
  134. return $this->phpWord;
  135. }
  136. /**
  137. * Set PhpWord as reference.
  138. *
  139. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  140. */
  141. public function setPhpWord(PhpWord $phpWord = null)
  142. {
  143. $this->phpWord = $phpWord;
  144. }
  145. /**
  146. * Get section number
  147. *
  148. * @return int
  149. */
  150. public function getSectionId()
  151. {
  152. return $this->sectionId;
  153. }
  154. /**
  155. * Set doc part.
  156. *
  157. * @param string $docPart
  158. * @param int $docPartId
  159. */
  160. public function setDocPart($docPart, $docPartId = 1)
  161. {
  162. $this->docPart = $docPart;
  163. $this->docPartId = $docPartId;
  164. }
  165. /**
  166. * Get doc part
  167. *
  168. * @return string
  169. */
  170. public function getDocPart()
  171. {
  172. return $this->docPart;
  173. }
  174. /**
  175. * Get doc part Id
  176. *
  177. * @return int
  178. */
  179. public function getDocPartId()
  180. {
  181. return $this->docPartId;
  182. }
  183. /**
  184. * Return media element (image, object, link) container name
  185. *
  186. * @return string section|headerx|footerx|footnote|endnote
  187. */
  188. private function getMediaPart()
  189. {
  190. $mediaPart = $this->docPart;
  191. if ($mediaPart == 'Header' || $mediaPart == 'Footer') {
  192. $mediaPart .= $this->docPartId;
  193. }
  194. return strtolower($mediaPart);
  195. }
  196. /**
  197. * Get element index
  198. *
  199. * @return int
  200. */
  201. public function getElementIndex()
  202. {
  203. return $this->elementIndex;
  204. }
  205. /**
  206. * Set element index.
  207. *
  208. * @param int $value
  209. */
  210. public function setElementIndex($value)
  211. {
  212. $this->elementIndex = $value;
  213. }
  214. /**
  215. * Get element unique ID
  216. *
  217. * @return string
  218. */
  219. public function getElementId()
  220. {
  221. return $this->elementId;
  222. }
  223. /**
  224. * Set element unique ID from 6 first digit of md5.
  225. */
  226. public function setElementId()
  227. {
  228. $this->elementId = substr(md5(rand()), 0, 6);
  229. }
  230. /**
  231. * Get relation Id
  232. *
  233. * @return int
  234. */
  235. public function getRelationId()
  236. {
  237. return $this->relationId;
  238. }
  239. /**
  240. * Set relation Id.
  241. *
  242. * @param int $value
  243. */
  244. public function setRelationId($value)
  245. {
  246. $this->relationId = $value;
  247. }
  248. /**
  249. * Get nested level
  250. *
  251. * @return int
  252. */
  253. public function getNestedLevel()
  254. {
  255. return $this->nestedLevel;
  256. }
  257. /**
  258. * Get comment start
  259. *
  260. * @return Comment
  261. */
  262. public function getCommentRangeStart()
  263. {
  264. return $this->commentRangeStart;
  265. }
  266. /**
  267. * Set comment start
  268. *
  269. * @param Comment $value
  270. */
  271. public function setCommentRangeStart(Comment $value)
  272. {
  273. if ($this instanceof Comment) {
  274. throw new \InvalidArgumentException('Cannot set a Comment on a Comment');
  275. }
  276. $this->commentRangeStart = $value;
  277. $this->commentRangeStart->setStartElement($this);
  278. }
  279. /**
  280. * Get comment end
  281. *
  282. * @return Comment
  283. */
  284. public function getCommentRangeEnd()
  285. {
  286. return $this->commentRangeEnd;
  287. }
  288. /**
  289. * Set comment end
  290. *
  291. * @param Comment $value
  292. */
  293. public function setCommentRangeEnd(Comment $value)
  294. {
  295. if ($this instanceof Comment) {
  296. throw new \InvalidArgumentException('Cannot set a Comment on a Comment');
  297. }
  298. $this->commentRangeEnd = $value;
  299. $this->commentRangeEnd->setEndElement($this);
  300. }
  301. /**
  302. * Get parent element
  303. *
  304. * @return AbstractElement|null
  305. */
  306. public function getParent()
  307. {
  308. return $this->parent;
  309. }
  310. /**
  311. * Set parent container
  312. *
  313. * Passed parameter should be a container, except for Table (contain Row) and Row (contain Cell)
  314. *
  315. * @param \PhpOffice\PhpWord\Element\AbstractElement $container
  316. */
  317. public function setParentContainer(self $container)
  318. {
  319. $this->parentContainer = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
  320. $this->parent = $container;
  321. // Set nested level
  322. $this->nestedLevel = $container->getNestedLevel();
  323. if ($this->parentContainer == 'Cell') {
  324. $this->nestedLevel++;
  325. }
  326. // Set phpword
  327. $this->setPhpWord($container->getPhpWord());
  328. // Set doc part
  329. if (!$this instanceof Footnote) {
  330. $this->setDocPart($container->getDocPart(), $container->getDocPartId());
  331. }
  332. $this->setMediaRelation();
  333. $this->setCollectionRelation();
  334. }
  335. /**
  336. * Set relation Id for media elements (link, image, object; legacy of OOXML)
  337. *
  338. * - Image element needs to be passed to Media object
  339. * - Icon needs to be set for Object element
  340. */
  341. private function setMediaRelation()
  342. {
  343. if (!$this instanceof Link && !$this instanceof Image && !$this instanceof OLEObject) {
  344. return;
  345. }
  346. $elementName = substr(get_class($this), strrpos(get_class($this), '\\') + 1);
  347. if ($elementName == 'OLEObject') {
  348. $elementName = 'Object';
  349. }
  350. $mediaPart = $this->getMediaPart();
  351. $source = $this->getSource();
  352. $image = null;
  353. if ($this instanceof Image) {
  354. $image = $this;
  355. }
  356. $rId = Media::addElement($mediaPart, strtolower($elementName), $source, $image);
  357. $this->setRelationId($rId);
  358. if ($this instanceof OLEObject) {
  359. $icon = $this->getIcon();
  360. $rId = Media::addElement($mediaPart, 'image', $icon, new Image($icon));
  361. $this->setImageRelationId($rId);
  362. }
  363. }
  364. /**
  365. * Set relation Id for elements that will be registered in the Collection subnamespaces.
  366. */
  367. private function setCollectionRelation()
  368. {
  369. if ($this->collectionRelation === true && $this->phpWord instanceof PhpWord) {
  370. $elementName = substr(get_class($this), strrpos(get_class($this), '\\') + 1);
  371. $addMethod = "add{$elementName}";
  372. $rId = $this->phpWord->$addMethod($this);
  373. $this->setRelationId($rId);
  374. }
  375. }
  376. /**
  377. * Check if element is located in Section doc part (as opposed to Header/Footer)
  378. *
  379. * @return bool
  380. */
  381. public function isInSection()
  382. {
  383. return $this->docPart == 'Section';
  384. }
  385. /**
  386. * Set new style value
  387. *
  388. * @param mixed $styleObject Style object
  389. * @param mixed $styleValue Style value
  390. * @param bool $returnObject Always return object
  391. * @return mixed
  392. */
  393. protected function setNewStyle($styleObject, $styleValue = null, $returnObject = false)
  394. {
  395. if (!is_null($styleValue) && is_array($styleValue)) {
  396. $styleObject->setStyleByArray($styleValue);
  397. $style = $styleObject;
  398. } else {
  399. $style = $returnObject ? $styleObject : $styleValue;
  400. }
  401. return $style;
  402. }
  403. /**
  404. * Sets the trackChange information
  405. *
  406. * @param TrackChange $trackChange
  407. */
  408. public function setTrackChange(TrackChange $trackChange)
  409. {
  410. $this->trackChange = $trackChange;
  411. }
  412. /**
  413. * Gets the trackChange information
  414. *
  415. * @return TrackChange
  416. */
  417. public function getTrackChange()
  418. {
  419. return $this->trackChange;
  420. }
  421. /**
  422. * Set changed
  423. *
  424. * @param string $type INSERTED|DELETED
  425. * @param string $author
  426. * @param null|int|\DateTime $date allways in UTC
  427. */
  428. public function setChangeInfo($type, $author, $date = null)
  429. {
  430. $this->trackChange = new TrackChange($type, $author, $date);
  431. }
  432. /**
  433. * Set enum value
  434. *
  435. * @param string|null $value
  436. * @param string[] $enum
  437. * @param string|null $default
  438. *
  439. * @throws \InvalidArgumentException
  440. * @return string|null
  441. *
  442. * @todo Merge with the same method in AbstractStyle
  443. */
  444. protected function setEnumVal($value = null, $enum = array(), $default = null)
  445. {
  446. if ($value !== null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {
  447. throw new \InvalidArgumentException("Invalid style value: {$value}");
  448. } elseif ($value === null || trim($value) == '') {
  449. $value = $default;
  450. }
  451. return $value;
  452. }
  453. }