AbstractElement.php 11 KB

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