Action.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @subpackage Actions
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Pdf_ElementFactory */
  23. require_once 'Zend/Pdf/ElementFactory.php';
  24. /** Zend_Pdf_Target */
  25. require_once 'Zend/Pdf/Target.php';
  26. /**
  27. * Abstract PDF action representation class
  28. *
  29. * @package Zend_Pdf
  30. * @subpackage Actions
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Pdf_Action extends Zend_Pdf_Target implements RecursiveIterator, Countable
  35. {
  36. /**
  37. * Action dictionary
  38. *
  39. * @var Zend_Pdf_Element_Dictionary|Zend_Pdf_Element_Object|Zend_Pdf_Element_Reference
  40. */
  41. protected $_actionDictionary;
  42. /**
  43. * An original list of chained actions
  44. *
  45. * @var array Array of Zend_Pdf_Action objects
  46. */
  47. protected $_originalNextList;
  48. /**
  49. * A list of next actions in actions tree (used for actions chaining)
  50. *
  51. * @var array Array of Zend_Pdf_Action objects
  52. */
  53. public $next = array();
  54. /**
  55. * Object constructor
  56. *
  57. * @param Zend_Pdf_Element_Dictionary $dictionary
  58. * @param SplObjectStorage $processedActions list of already processed action dictionaries, used to avoid cyclic references
  59. * @throws Zend_Pdf_Exception
  60. */
  61. public function __construct(Zend_Pdf_Element $dictionary, SplObjectStorage $processedActions)
  62. {
  63. if ($dictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  64. require_once 'Zend/Pdf/Exception.php';
  65. throw new Zend_Pdf_Exception('$dictionary mast be a direct or an indirect dictionary object.');
  66. }
  67. $this->_actionDictionary = $dictionary;
  68. if ($dictionary->Next !== null) {
  69. if ($dictionary->Next instanceof Zend_Pdf_Element_Dictionary) {
  70. // Check if dictionary object is not already processed
  71. if (!$processedActions->contains($dictionary->Next)) {
  72. $processedActions->attach($dictionary->Next);
  73. $this->next[] = Zend_Pdf_Action::load($dictionary->Next, $processedActions);
  74. }
  75. } else if ($dictionary->Next instanceof Zend_Pdf_Element_Array) {
  76. foreach ($dictionary->Next->items as $chainedActionDictionary) {
  77. // Check if dictionary object is not already processed
  78. if (!$processedActions->contains($chainedActionDictionary)) {
  79. $processedActions->attach($chainedActionDictionary);
  80. $this->next[] = Zend_Pdf_Action::load($chainedActionDictionary, $processedActions);
  81. }
  82. }
  83. } else {
  84. require_once 'Zend/Pdf/Exception.php';
  85. throw new Zend_Pdf_Exception('PDF Action dictionary Next entry must be a dictionary or an array.');
  86. }
  87. }
  88. $this->_originalNextList = $this->next;
  89. }
  90. /**
  91. * Load PDF action object using specified dictionary
  92. *
  93. * @internal
  94. * @param Zend_Pdf_Element $dictionary (It's actually Dictionary or Dictionary Object or Reference to a Dictionary Object)
  95. * @param SplObjectStorage $processedActions list of already processed action dictionaries, used to avoid cyclic references
  96. * @return Zend_Pdf_Action
  97. * @throws Zend_Pdf_Exception
  98. */
  99. public static function load(Zend_Pdf_Element $dictionary, SplObjectStorage $processedActions = null)
  100. {
  101. if ($processedActions === null) {
  102. $processedActions = new SplObjectStorage();
  103. }
  104. if ($dictionary->getType() != Zend_Pdf_Element::TYPE_DICTIONARY) {
  105. require_once 'Zend/Pdf/Exception.php';
  106. throw new Zend_Pdf_Exception('$dictionary mast be a direct or an indirect dictionary object.');
  107. }
  108. if (isset($dictionary->Type) && $dictionary->Type->value != 'Action') {
  109. require_once 'Zend/Pdf/Exception.php';
  110. throw new Zend_Pdf_Exception('Action dictionary Type entry must be set to \'Action\'.');
  111. }
  112. if ($dictionary->S === null) {
  113. require_once 'Zend/Pdf/Exception.php';
  114. throw new Zend_Pdf_Exception('Action dictionary must contain S entry');
  115. }
  116. switch ($dictionary->S->value) {
  117. case 'GoTo':
  118. require_once 'Zend/Pdf/Action/GoTo.php';
  119. return new Zend_Pdf_Action_GoTo($dictionary, $processedActions);
  120. brake;
  121. case 'GoToR':
  122. require_once 'Zend/Pdf/Action/GoToR.php';
  123. return new Zend_Pdf_Action_GoToR($dictionary, $processedActions);
  124. brake;
  125. case 'GoToE':
  126. require_once 'Zend/Pdf/Action/GoToE.php';
  127. return new Zend_Pdf_Action_GoToE($dictionary, $processedActions);
  128. brake;
  129. case 'Launch':
  130. require_once 'Zend/Pdf/Action/Launch.php';
  131. return new Zend_Pdf_Action_Launch($dictionary, $processedActions);
  132. brake;
  133. case 'Thread':
  134. require_once 'Zend/Pdf/Action/Thread.php';
  135. return new Zend_Pdf_Action_Thread($dictionary, $processedActions);
  136. brake;
  137. case 'URI':
  138. require_once 'Zend/Pdf/Action/URI.php';
  139. return new Zend_Pdf_Action_URI($dictionary, $processedActions);
  140. brake;
  141. case 'Sound':
  142. require_once 'Zend/Pdf/Action/Sound.php';
  143. return new Zend_Pdf_Action_Sound($dictionary, $processedActions);
  144. brake;
  145. case 'Movie':
  146. require_once 'Zend/Pdf/Action/Movie.php';
  147. return new Zend_Pdf_Action_Movie($dictionary, $processedActions);
  148. brake;
  149. case 'Hide':
  150. require_once 'Zend/Pdf/Action/Hide.php';
  151. return new Zend_Pdf_Action_Hide($dictionary, $processedActions);
  152. brake;
  153. case 'Named':
  154. require_once 'Zend/Pdf/Action/Named.php';
  155. return new Zend_Pdf_Action_Named($dictionary, $processedActions);
  156. brake;
  157. case 'SubmitForm':
  158. require_once 'Zend/Pdf/Action/SubmitForm.php';
  159. return new Zend_Pdf_Action_SubmitForm($dictionary, $processedActions);
  160. brake;
  161. case 'ResetForm':
  162. require_once 'Zend/Pdf/Action/ResetForm.php';
  163. return new Zend_Pdf_Action_ResetForm($dictionary, $processedActions);
  164. brake;
  165. case 'ImportData':
  166. require_once 'Zend/Pdf/Action/ImportData.php';
  167. return new Zend_Pdf_Action_ImportData($dictionary, $processedActions);
  168. brake;
  169. case 'JavaScript':
  170. require_once 'Zend/Pdf/Action/JavaScript.php';
  171. return new Zend_Pdf_Action_JavaScript($dictionary, $processedActions);
  172. brake;
  173. case 'SetOCGState':
  174. require_once 'Zend/Pdf/Action/SetOCGState.php';
  175. return new Zend_Pdf_Action_SetOCGState($dictionary, $processedActions);
  176. brake;
  177. case 'Rendition':
  178. require_once 'Zend/Pdf/Action/Rendition.php';
  179. return new Zend_Pdf_Action_Rendition($dictionary, $processedActions);
  180. brake;
  181. case 'Trans':
  182. require_once 'Zend/Pdf/Action/Trans.php';
  183. return new Zend_Pdf_Action_Trans($dictionary, $processedActions);
  184. brake;
  185. case 'GoTo3DView':
  186. require_once 'Zend/Pdf/Action/GoTo3DView.php';
  187. return new Zend_Pdf_Action_GoTo3DView($dictionary, $processedActions);
  188. brake;
  189. default:
  190. require_once 'Zend/Pdf/Action/Unknown.php';
  191. return new Zend_Pdf_Action_Unknown($dictionary, $processedActions);
  192. brake;
  193. }
  194. }
  195. /**
  196. * Get resource
  197. *
  198. * @internal
  199. * @return Zend_Pdf_Element
  200. */
  201. public function getResource()
  202. {
  203. return $this->_actionDictionary;
  204. }
  205. /**
  206. * Dump Action and its child actions into PDF structures
  207. *
  208. * Returns dictionary indirect object or reference
  209. *
  210. * @internal
  211. * @param Zend_Pdf_ElementFactory $factory Object factory for newly created indirect objects
  212. * @param SplObjectStorage $processedActions list of already processed actions (used to prevent infinity loop caused by cyclic references)
  213. * @return Zend_Pdf_Element_Object|Zend_Pdf_Element_Reference Dictionary indirect object
  214. */
  215. public function dumpAction(Zend_Pdf_ElementFactory_Interface $factory, SplObjectStorage $processedActions = null)
  216. {
  217. if ($processedActions === null) {
  218. $processedActions = new SplObjectStorage();
  219. }
  220. if ($processedActions->contains($this)) {
  221. require_once 'Zend/Pdf/Exception.php';
  222. throw new Zend_Pdf_Exception('Action chain cyclyc reference is detected.');
  223. }
  224. $processedActions->attach($this);
  225. $childListUpdated = false;
  226. if (count($this->_originalNextList) != count($this->next)) {
  227. // If original and current children arrays have different size then children list was updated
  228. $childListUpdated = true;
  229. } else if ( !(array_keys($this->_originalNextList) === array_keys($this->next)) ) {
  230. // If original and current children arrays have different keys (with a glance to an order) then children list was updated
  231. $childListUpdated = true;
  232. } else {
  233. foreach ($this->next as $key => $childAction) {
  234. if ($this->_originalNextList[$key] !== $childAction) {
  235. $childListUpdated = true;
  236. break;
  237. }
  238. }
  239. }
  240. if ($childListUpdated) {
  241. $this->_actionDictionary->touch();
  242. switch (count($this->next)) {
  243. case 0:
  244. $this->_actionDictionary->Next = null;
  245. break;
  246. case 1:
  247. $child = reset($this->next);
  248. $this->_actionDictionary->Next = $child->dumpAction($factory, $processedActions);
  249. break;
  250. default:
  251. $pdfChildArray = new Zend_Pdf_Element_Array();
  252. foreach ($this->next as $child) {
  253. $pdfChildArray->items[] = $child->dumpAction($factory, $processedActions);
  254. }
  255. $this->_actionDictionary->Next = $pdfChildArray;
  256. break;
  257. }
  258. } else {
  259. foreach ($this->next as $child) {
  260. $child->dumpAction($factory, $processedActions);
  261. }
  262. }
  263. if ($this->_actionDictionary instanceof Zend_Pdf_Element_Dictionary) {
  264. // It's a newly created action. Register it within object factory and return indirect object
  265. return $factory->newObject($this->_actionDictionary);
  266. } else {
  267. // It's a loaded object
  268. return $this->_actionDictionary;
  269. }
  270. }
  271. ////////////////////////////////////////////////////////////////////////
  272. // RecursiveIterator interface methods
  273. //////////////
  274. /**
  275. * Returns current child action.
  276. *
  277. * @return Zend_Pdf_Action
  278. */
  279. public function current()
  280. {
  281. return current($this->next);
  282. }
  283. /**
  284. * Returns current iterator key
  285. *
  286. * @return integer
  287. */
  288. public function key()
  289. {
  290. return key($this->next);
  291. }
  292. /**
  293. * Go to next child
  294. */
  295. public function next()
  296. {
  297. return next($this->next);
  298. }
  299. /**
  300. * Rewind children
  301. */
  302. public function rewind()
  303. {
  304. return reset($this->next);
  305. }
  306. /**
  307. * Check if current position is valid
  308. *
  309. * @return boolean
  310. */
  311. public function valid()
  312. {
  313. return current($this->next) !== false;
  314. }
  315. /**
  316. * Returns the child action.
  317. *
  318. * @return Zend_Pdf_Outline|null
  319. */
  320. public function getChildren()
  321. {
  322. return current($this->next);
  323. }
  324. /**
  325. * Implements RecursiveIterator interface.
  326. *
  327. * @return bool whether container has any pages
  328. */
  329. public function hasChildren()
  330. {
  331. return count($this->next) > 0;
  332. }
  333. ////////////////////////////////////////////////////////////////////////
  334. // Countable interface methods
  335. //////////////
  336. /**
  337. * count()
  338. *
  339. * @return int
  340. */
  341. public function count()
  342. {
  343. return count($this->childOutlines);
  344. }
  345. }