MailMerge.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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_Service
  17. * @subpackage LiveDocx
  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_Date **/
  23. require_once 'Zend/Date.php';
  24. /** Zend_Service_LiveDocx **/
  25. require_once 'Zend/Service/LiveDocx.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service
  29. * @subpackage LiveDocx
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @since LiveDocx 1.0
  33. */
  34. class Zend_Service_LiveDocx_MailMerge extends Zend_Service_LiveDocx
  35. {
  36. /**
  37. * URI of LiveDocx.MailMerge WSDL
  38. * @since LiveDocx 1.0
  39. */
  40. const WSDL = 'https://api.livedocx.com/1.2/mailmerge.asmx?WSDL';
  41. /**
  42. * Document access permission: After the document has been opened no further
  43. * document access is restricted
  44. * @since LiveDocx 1.2 Premium
  45. */
  46. const DOCUMENT_ACCESS_PERMISSION_ALLOW_ALL = 'AllowAll';
  47. /**
  48. * Document access permission: Allow comments to be added and interactive
  49. * form fields (including signature fields) to be filled in
  50. * @since LiveDocx 1.2 Premium
  51. */
  52. const DOCUMENT_ACCESS_PERMISSION_ALLOW_AUTHORING = 'AllowAuthoring';
  53. /**
  54. * Document access permission: Allow existing interactive form fields
  55. * (including signature fields) to be filled in
  56. * @since LiveDocx 1.2 Premium
  57. */
  58. const DOCUMENT_ACCESS_PERMISSION_ALLOW_AUTHORING_FIELDS = 'AllowAuthoringFields';
  59. /**
  60. * Document access permission: Allow content access for the visually
  61. * impaired only
  62. * @since LiveDocx 1.2 Premium
  63. */
  64. const DOCUMENT_ACCESS_PERMISSION_ALLOW_CONTENT_ACCESSIBILITY = 'AllowContentAccessibility';
  65. /**
  66. * Document access permission: Allow the document to be to assembled
  67. * (insert, rotate or delete pages and create bookmarks or thumbnails)
  68. * @since LiveDocx 1.2 Premium
  69. */
  70. const DOCUMENT_ACCESS_PERMISSION_ALLOW_DOCUMENT_ASSEMBLY = 'AllowDocumentAssembly';
  71. /**
  72. * Document access permission: Allow text and/or graphics to be extracted
  73. * @since LiveDocx 1.2 Premium
  74. */
  75. const DOCUMENT_ACCESS_PERMISSION_ALLOW_EXTRACT_CONTENTS = 'AllowExtractContents';
  76. /**
  77. * Document access permission: Allow the document contents to be modified
  78. * @since LiveDocx 1.2 Premium
  79. */
  80. const DOCUMENT_ACCESS_PERMISSION_ALLOW_GENERAL_EDITING = 'AllowGeneralEditing';
  81. /**
  82. * Document access permission: Allow the document to be printed
  83. * @since LiveDocx 1.2 Premium
  84. */
  85. const DOCUMENT_ACCESS_PERMISSION_ALLOW_PRINTING_HIGH_LEVEL = 'AllowHighLevelPrinting';
  86. /**
  87. * Document access permission: Allow the document to be printed (low-level)
  88. * @since LiveDocx 1.2 Premium
  89. */
  90. const DOCUMENT_ACCESS_PERMISSION_ALLOW_PRINTING_LOW_LEVEL = 'AllowLowLevelPrinting';
  91. /**
  92. * Field values
  93. *
  94. * @var array
  95. * @since LiveDocx 1.0
  96. */
  97. protected $_fieldValues;
  98. /**
  99. * Block field values
  100. *
  101. * @var array
  102. * @since LiveDocx 1.0
  103. */
  104. protected $_blockFieldValues;
  105. /**
  106. * Document properties of PDF file (only)
  107. *
  108. * @var array
  109. * @since LiveDocx 1.0
  110. */
  111. protected $_documentProperties;
  112. /**
  113. * Constructor (LiveDocx.MailMerge SOAP Service)
  114. *
  115. * @return void
  116. * @return throws Zend_Service_LiveDocx_Exception
  117. * @since LiveDocx 1.0
  118. */
  119. public function __construct($options = null)
  120. {
  121. $this->_wsdl = self::WSDL;
  122. $this->_fieldValues = array();
  123. $this->_blockFieldValues = array();
  124. $this->_setDefaultDocumentProperties();
  125. parent::__construct($options);
  126. }
  127. /**
  128. * Set the filename of a LOCAL template
  129. * (i.e. a template stored locally on YOUR server)
  130. *
  131. * @param string $filename
  132. * @return Zend_Service_LiveDocx_MailMerge
  133. * @throws Zend_Service_LiveDocx_Exception
  134. * @since LiveDocx 1.0
  135. */
  136. public function setLocalTemplate($filename)
  137. {
  138. $this->logIn();
  139. try {
  140. $this->getSoapClient()->SetLocalTemplate(array(
  141. 'template' => base64_encode(file_get_contents($filename)),
  142. 'format' => self::getFormat($filename),
  143. ));
  144. } catch (Exception $e) {
  145. require_once 'Zend/Service/LiveDocx/Exception.php';
  146. throw new Zend_Service_LiveDocx_Exception(
  147. 'Cannot set local template', 0, $e
  148. );
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Set the filename of a REMOTE template
  154. * (i.e. a template stored remotely on the LIVEDOCX server)
  155. *
  156. * @param string $filename
  157. * @return Zend_Service_LiveDocx_MailMerge
  158. * @throws Zend_Service_LiveDocx_Exception
  159. * @since LiveDocx 1.0
  160. */
  161. public function setRemoteTemplate($filename)
  162. {
  163. $this->logIn();
  164. try {
  165. $this->getSoapClient()->SetRemoteTemplate(array(
  166. 'filename' => $filename,
  167. ));
  168. } catch (Exception $e) {
  169. require_once 'Zend/Service/LiveDocx/Exception.php';
  170. throw new Zend_Service_LiveDocx_Exception(
  171. 'Cannot set remote template', 0, $e
  172. );
  173. }
  174. return $this;
  175. }
  176. /**
  177. * Set an associative or multi-associative array of keys and values pairs
  178. *
  179. * @param array $values
  180. * @return Zend_Service_LiveDocx_MailMerge
  181. * @throws Zend_Service_LiveDocx_Exception
  182. * @since LiveDocx 1.0
  183. */
  184. public function setFieldValues($values)
  185. {
  186. $this->logIn();
  187. foreach ($values as $value) {
  188. if (is_array($value)) {
  189. $method = 'multiAssocArrayToArrayOfArrayOfString';
  190. } else {
  191. $method = 'assocArrayToArrayOfArrayOfString';
  192. }
  193. break;
  194. }
  195. try {
  196. $this->getSoapClient()->SetFieldValues(array(
  197. 'fieldValues' => self::$method($values),
  198. ));
  199. } catch (Exception $e) {
  200. require_once 'Zend/Service/LiveDocx/Exception.php';
  201. throw new Zend_Service_LiveDocx_Exception(
  202. 'Cannot set field values', 0, $e
  203. );
  204. }
  205. return $this;
  206. }
  207. /**
  208. * Set an array of key and value or array of values
  209. *
  210. * @param string $field
  211. * @param array|string $value
  212. *
  213. * @throws Zend_Service_LiveDocx_Exception
  214. * @return Zend_Service_LiveDocx_MailMerge
  215. * @since LiveDocx 1.0
  216. */
  217. public function setFieldValue($field, $value)
  218. {
  219. $this->_fieldValues[$field] = $value;
  220. return $this;
  221. }
  222. /**
  223. * Set block field values
  224. *
  225. * @param string $blockName
  226. * @param array $blockFieldValues
  227. *
  228. * @return Zend_Service_LiveDocx_MailMerge
  229. * @throws Zend_Service_LiveDocx_Exception
  230. * @since LiveDocx 1.0
  231. */
  232. public function setBlockFieldValues($blockName, $blockFieldValues)
  233. {
  234. $this->logIn();
  235. try {
  236. $this->getSoapClient()->SetBlockFieldValues(array(
  237. 'blockName' => $blockName,
  238. 'blockFieldValues' => self::multiAssocArrayToArrayOfArrayOfString($blockFieldValues)
  239. ));
  240. } catch (Exception $e) {
  241. require_once 'Zend/Service/LiveDocx/Exception.php';
  242. throw new Zend_Service_LiveDocx_Exception(
  243. 'Cannot set block field values', 0, $e
  244. );
  245. }
  246. return $this;
  247. }
  248. /**
  249. * Assign values to template fields
  250. *
  251. * @param array|string $field
  252. * @param array|string $value
  253. * @return Zend_Service_LiveDocx_MailMerge
  254. * @throws Zend_Service_LiveDocx_Exception
  255. * @since LiveDocx 1.0
  256. */
  257. public function assign($field, $value = null)
  258. {
  259. try {
  260. if (is_array($field) && (null === $value)) {
  261. foreach ($field as $fieldName => $fieldValue) {
  262. $this->setFieldValue($fieldName, $fieldValue);
  263. }
  264. } elseif (is_array($value)) {
  265. $this->setBlockFieldValues($field, $value);
  266. } else {
  267. $this->setFieldValue($field, $value);
  268. }
  269. } catch (Exception $e) {
  270. require_once 'Zend/Service/LiveDocx/Exception.php';
  271. throw new Zend_Service_LiveDocx_Exception(
  272. 'Cannot assign data to template', 0, $e
  273. );
  274. }
  275. return $this;
  276. }
  277. /**
  278. * Set a password to open to document
  279. *
  280. * This method can only be used for PDF documents
  281. *
  282. * @param string $password
  283. * @return Zend_Service_LiveDocx_MailMerge
  284. * @throws Zend_Service_LiveDocx_Exception
  285. * @since LiveDocx 1.2 Premium
  286. */
  287. public function setDocumentPassword($password)
  288. {
  289. $this->logIn();
  290. try {
  291. $this->getSoapClient()->SetDocumentPassword(array(
  292. 'password' => $password
  293. ));
  294. } catch (Exception $e) {
  295. require_once 'Zend/Service/LiveDocx/Exception.php';
  296. throw new Zend_Service_LiveDocx_Exception(
  297. 'Cannot set document password. This method can be used on PDF files only.', 0, $e
  298. );
  299. }
  300. $this->_documentProperties['Encrypted'] = true;
  301. return $this;
  302. }
  303. /**
  304. * Set a master password for document and determine which security features
  305. * are accessible without using the master password.
  306. *
  307. * As default, nothing is allowed. To allow a security setting,
  308. * explicatively set it using one of he DOCUMENT_ACCESS_PERMISSION_* class
  309. * constants.
  310. *
  311. * {code}
  312. * $phpLiveDocx->setDocumentAccessPermissions(
  313. * array (
  314. * Zend_Service_LiveDocx_MailMerge::DOCUMENT_ACCESS_PERMISSION_ALLOW_PRINTING_HIGH_LEVEL,
  315. * Zend_Service_LiveDocx_MailMerge::DOCUMENT_ACCESS_PERMISSION_ALLOW_EXTRACT_CONTENTS
  316. * ),
  317. * 'myDocumentAccessPassword'
  318. * );
  319. * {code}
  320. *
  321. * This method can only be used for PDF documents
  322. *
  323. * @param array $permissions
  324. * @param string $password
  325. * @return Zend_Service_LiveDocx_MailMerge
  326. * @throws Zend_Service_LiveDocx_Exception
  327. * @since LiveDocx 1.2 Premium
  328. */
  329. public function setDocumentAccessPermissions($permissions, $password)
  330. {
  331. $validPermissions = array(
  332. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_ALL ,
  333. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_AUTHORING ,
  334. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_AUTHORING_FIELDS ,
  335. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_CONTENT_ACCESSIBILITY ,
  336. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_DOCUMENT_ASSEMBLY ,
  337. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_EXTRACT_CONTENTS ,
  338. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_GENERAL_EDITING ,
  339. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_PRINTING_HIGH_LEVEL ,
  340. self::DOCUMENT_ACCESS_PERMISSION_ALLOW_PRINTING_LOW_LEVEL
  341. );
  342. foreach ($permissions as $permission) {
  343. if (! in_array($permission, $validPermissions)) {
  344. require_once 'Zend/Service/LiveDocx/Exception.php';
  345. throw new Zend_Service_LiveDocx_Exception(
  346. 'Invalid document access permission. '
  347. . 'Must be one of Zend_Service_LiveDocx_MailMerge::DOCUMENT_ACCESS_PERMISSION_* class constants'
  348. );
  349. }
  350. }
  351. $this->logIn();
  352. try {
  353. $this->getSoapClient()->SetDocumentAccessPermissions(array(
  354. 'permissions' => $permissions,
  355. 'password' => $password
  356. ));
  357. } catch (Exception $e) {
  358. require_once 'Zend/Service/LiveDocx/Exception.php';
  359. throw new Zend_Service_LiveDocx_Exception(
  360. 'Cannot set document access permissions', 0, $e
  361. );
  362. }
  363. $this->_documentProperties['Encrypted'] = true;
  364. return $this;
  365. }
  366. /**
  367. * Merge assigned data with template to generate document
  368. *
  369. * @throws Zend_Service_LiveDocx_Excpetion
  370. * @return void
  371. * @since LiveDocx 1.0
  372. */
  373. public function createDocument()
  374. {
  375. $this->logIn();
  376. if (count($this->_fieldValues) > 0) {
  377. $this->setFieldValues($this->_fieldValues);
  378. }
  379. $this->_fieldValues = array();
  380. $this->_blockFieldValues = array();
  381. try {
  382. $this->getSoapClient()->CreateDocument();
  383. } catch (Exception $e) {
  384. require_once 'Zend/Service/LiveDocx/Exception.php';
  385. throw new Zend_Service_LiveDocx_Exception(
  386. 'Cannot create document', 0, $e
  387. );
  388. }
  389. }
  390. /**
  391. * Retrieve document in specified format
  392. *
  393. * @param string $format
  394. *
  395. * @throws Zend_Service_LiveDocx_Exception
  396. * @return binary
  397. * @since LiveDocx 1.0
  398. */
  399. public function retrieveDocument($format)
  400. {
  401. $this->logIn();
  402. $ret = null;
  403. $format = strtolower($format);
  404. try {
  405. $result = $this->getSoapClient()->RetrieveDocument(array(
  406. 'format' => $format,
  407. ));
  408. } catch (Exception $e) {
  409. require_once 'Zend/Service/LiveDocx/Exception.php';
  410. throw new Zend_Service_LiveDocx_Exception(
  411. 'Cannot retrieve document - call setLocalTemplate() or setRemoteTemplate() first', 0, $e
  412. );
  413. }
  414. $ret = base64_decode($result->RetrieveDocumentResult);
  415. // @todo: Move this logic to backend server in future version
  416. if ('pdf' === $format &&
  417. false === $this->_documentProperties['Encrypted']) {
  418. require_once 'Zend/Pdf.php';
  419. $pdf = Zend_Pdf::parse($ret);
  420. $pdf->properties = $this->_getDocumentProperties();
  421. $ret = $pdf->render();
  422. }
  423. return $ret;
  424. }
  425. /**
  426. * Return WMF (aka Windows metafile) data for specified page range of created document
  427. * Return array contains WMF data (binary) - array key is page number
  428. *
  429. * @param integer $fromPage
  430. * @param integer $toPage
  431. * @return array
  432. * @since LiveDocx 1.2
  433. */
  434. public function getMetafiles($fromPage, $toPage)
  435. {
  436. $this->logIn();
  437. $ret = array();
  438. $result = $this->getSoapClient()->GetMetafiles(array(
  439. 'fromPage' => (integer) $fromPage,
  440. 'toPage' => (integer) $toPage,
  441. ));
  442. if (isset($result->GetMetafilesResult->string)) {
  443. $pageCounter = (integer) $fromPage;
  444. if (is_array($result->GetMetafilesResult->string)) {
  445. foreach ($result->GetMetafilesResult->string as $string) {
  446. $ret[$pageCounter] = base64_decode($string);
  447. $pageCounter++;
  448. }
  449. } else {
  450. $ret[$pageCounter] = base64_decode($result->GetMetafilesResult->string);
  451. }
  452. }
  453. return $ret;
  454. }
  455. /**
  456. * Return WMF (aka Windows metafile) data for pages of created document
  457. * Return array contains WMF data (binary) - array key is page number
  458. *
  459. * @return array
  460. * @since LiveDocx 1.2
  461. */
  462. public function getAllMetafiles()
  463. {
  464. $this->logIn();
  465. $ret = array();
  466. $result = $this->getSoapClient()->GetAllMetafiles();
  467. if (isset($result->GetAllMetafilesResult->string)) {
  468. $pageCounter = 1;
  469. if (is_array($result->GetAllMetafilesResult->string)) {
  470. foreach ($result->GetAllMetafilesResult->string as $string) {
  471. $ret[$pageCounter] = base64_decode($string);
  472. $pageCounter++;
  473. }
  474. } else {
  475. $ret[$pageCounter] = base64_decode($result->GetAllMetafilesResult->string);
  476. }
  477. }
  478. return $ret;
  479. }
  480. /**
  481. * Return graphical bitmap data for specified page range of created document
  482. * Return array contains bitmap data (binary) - array key is page number
  483. *
  484. * @param integer $fromPage
  485. * @param integer $toPage
  486. * @param integer $zoomFactor
  487. * @param string $format
  488. * @return array
  489. * @since LiveDocx 1.2
  490. */
  491. public function getBitmaps($fromPage, $toPage, $zoomFactor, $format)
  492. {
  493. $this->logIn();
  494. $ret = array();
  495. $result = $this->getSoapClient()->GetBitmaps(array(
  496. 'fromPage' => (integer) $fromPage,
  497. 'toPage' => (integer) $toPage,
  498. 'zoomFactor' => (integer) $zoomFactor,
  499. 'format' => (string) $format,
  500. ));
  501. if (isset($result->GetBitmapsResult->string)) {
  502. $pageCounter = (integer) $fromPage;
  503. if (is_array($result->GetBitmapsResult->string)) {
  504. foreach ($result->GetBitmapsResult->string as $string) {
  505. $ret[$pageCounter] = base64_decode($string);
  506. $pageCounter++;
  507. }
  508. } else {
  509. $ret[$pageCounter] = base64_decode($result->GetBitmapsResult->string);
  510. }
  511. }
  512. return $ret;
  513. }
  514. /**
  515. * Return graphical bitmap data for all pages of created document
  516. * Return array contains bitmap data (binary) - array key is page number
  517. *
  518. * @param integer $zoomFactor
  519. * @param string $format
  520. * @return array
  521. * @since LiveDocx 1.2
  522. */
  523. public function getAllBitmaps($zoomFactor, $format)
  524. {
  525. $this->logIn();
  526. $ret = array();
  527. $result = $this->getSoapClient()->GetAllBitmaps(array(
  528. 'zoomFactor' => (integer) $zoomFactor,
  529. 'format' => (string) $format,
  530. ));
  531. if (isset($result->GetAllBitmapsResult->string)) {
  532. $pageCounter = 1;
  533. if (is_array($result->GetAllBitmapsResult->string)) {
  534. foreach ($result->GetAllBitmapsResult->string as $string) {
  535. $ret[$pageCounter] = base64_decode($string);
  536. $pageCounter++;
  537. }
  538. } else {
  539. $ret[$pageCounter] = base64_decode($result->GetAllBitmapsResult->string);
  540. }
  541. }
  542. return $ret;
  543. }
  544. /**
  545. * Return all the fields in the template
  546. *
  547. * @return array
  548. * @since LiveDocx 1.0
  549. */
  550. public function getFieldNames()
  551. {
  552. $this->logIn();
  553. $ret = array();
  554. $result = $this->getSoapClient()->GetFieldNames();
  555. if (isset($result->GetFieldNamesResult->string)) {
  556. if (is_array($result->GetFieldNamesResult->string)) {
  557. $ret = $result->GetFieldNamesResult->string;
  558. } else {
  559. $ret[] = $result->GetFieldNamesResult->string;
  560. }
  561. }
  562. return $ret;
  563. }
  564. /**
  565. * Return all the block fields in the template
  566. *
  567. * @param string $blockName
  568. * @return array
  569. * @since LiveDocx 1.0
  570. */
  571. public function getBlockFieldNames($blockName)
  572. {
  573. $this->logIn();
  574. $ret = array();
  575. $result = $this->getSoapClient()->GetBlockFieldNames(array(
  576. 'blockName' => $blockName
  577. ));
  578. if (isset($result->GetBlockFieldNamesResult->string)) {
  579. if (is_array($result->GetBlockFieldNamesResult->string)) {
  580. $ret = $result->GetBlockFieldNamesResult->string;
  581. } else {
  582. $ret[] = $result->GetBlockFieldNamesResult->string;
  583. }
  584. }
  585. return $ret;
  586. }
  587. /**
  588. * Return all the block fields in the template
  589. *
  590. * @return array
  591. * @since LiveDocx 1.0
  592. */
  593. public function getBlockNames()
  594. {
  595. $this->logIn();
  596. $ret = array();
  597. $result = $this->getSoapClient()->GetBlockNames();
  598. if (isset($result->GetBlockNamesResult->string)) {
  599. if (is_array($result->GetBlockNamesResult->string)) {
  600. $ret = $result->GetBlockNamesResult->string;
  601. } else {
  602. $ret[] = $result->GetBlockNamesResult->string;
  603. }
  604. }
  605. return $ret;
  606. }
  607. /**
  608. * Set the default document properties
  609. *
  610. * @return null
  611. * @since LiveDocx 1.0
  612. */
  613. protected function _setDefaultDocumentProperties()
  614. {
  615. $date = new Zend_Date();
  616. $this->_documentProperties = array();
  617. $projectName = sprintf('phpLiveDocx %s', self::getVersion());
  618. $projectUrl = 'http://www.phpLiveDocx.org';
  619. $creationDate = sprintf('D:%s', $date->toString('YYYYMMddHHmmss'));
  620. // Zend_Pdf expects keys with uppercase first letter
  621. $this->_documentProperties['Creator'] = $projectName;
  622. $this->_documentProperties['Producer'] = $projectUrl;
  623. $this->_documentProperties['CreationDate'] = $creationDate;
  624. $this->_documentProperties['ModDate'] = $creationDate;
  625. // Set to true, if contents are password protected or encrypted
  626. $this->_documentProperties['Encrypted'] = false;
  627. }
  628. /**
  629. * Set the document properties
  630. *
  631. * $properties is an assoc array with the following format:
  632. *
  633. * {code}
  634. * $properties = array (
  635. * 'title' => '', // (string)
  636. * 'author' => '', // (string)
  637. * 'subject' => '', // (string)
  638. * 'keywords' => '', // (string)
  639. * );
  640. * {code}
  641. *
  642. * This method can only be used for PDF documents
  643. *
  644. * @param array $properties
  645. * @return Zend_Service_LiveDocx_MailMerge
  646. * @since LiveDocx 1.0
  647. */
  648. public function setDocumentProperties($properties)
  649. {
  650. // For consistency, keys in $properties are lowercase.
  651. // Zend_Pdf expects keys with uppercase first letter
  652. $keys = array('Title', 'Author', 'Subject', 'Keywords');
  653. foreach ($keys as $key) {
  654. $lowerCaseKey = strtolower($key);
  655. if (isset($properties[$lowerCaseKey])) {
  656. $this->_documentProperties[$key] = $properties[$lowerCaseKey];
  657. }
  658. }
  659. return $this;
  660. }
  661. /**
  662. * Return currently set document properties
  663. *
  664. * @return array
  665. * @since LiveDocx 1.0
  666. */
  667. protected function _getDocumentProperties()
  668. {
  669. return $this->_documentProperties;
  670. }
  671. /**
  672. * Upload a template file to LiveDocx service
  673. *
  674. * @param string $filename
  675. * @return void
  676. * @throws Zend_Service_LiveDocx_Exception
  677. * @since LiveDocx 1.0
  678. */
  679. public function uploadTemplate($filename)
  680. {
  681. $this->logIn();
  682. try {
  683. $this->getSoapClient()->UploadTemplate(array(
  684. 'template' => base64_encode(file_get_contents($filename)),
  685. 'filename' => basename($filename),
  686. ));
  687. } catch (Exception $e) {
  688. require_once 'Zend/Service/LiveDocx/Exception.php';
  689. throw new Zend_Service_LiveDocx_Exception(
  690. 'Cannot upload template', 0, $e
  691. );
  692. }
  693. }
  694. /**
  695. * Download template file from LiveDocx service
  696. *
  697. * @param string $filename
  698. * @return binary
  699. * @throws Zend_Service_LiveDocx_Exception
  700. * @since LiveDocx 1.0
  701. */
  702. public function downloadTemplate($filename)
  703. {
  704. $this->logIn();
  705. try {
  706. $result = $this->getSoapClient()->DownloadTemplate(array(
  707. 'filename' => basename($filename),
  708. ));
  709. } catch (Exception $e) {
  710. require_once 'Zend/Service/LiveDocx/Exception.php';
  711. throw new Zend_Service_LiveDocx_Exception(
  712. 'Cannot download template', 0, $e
  713. );
  714. }
  715. return base64_decode($result->DownloadTemplateResult);
  716. }
  717. /**
  718. * Delete a template file from LiveDocx service
  719. *
  720. * @param string $filename
  721. * @return void
  722. * @throws Zend_Service_LiveDocx_Exception
  723. * @since LiveDocx 1.0
  724. */
  725. public function deleteTemplate($filename)
  726. {
  727. $this->logIn();
  728. $this->getSoapClient()->DeleteTemplate(array(
  729. 'filename' => basename($filename),
  730. ));
  731. }
  732. /**
  733. * List all templates stored on LiveDocx service
  734. *
  735. * @return array
  736. * @since LiveDocx 1.0
  737. */
  738. public function listTemplates()
  739. {
  740. $this->logIn();
  741. $ret = array();
  742. $result = $this->getSoapClient()->ListTemplates();
  743. if (isset($result->ListTemplatesResult)) {
  744. $ret = $this->_backendListArrayToMultiAssocArray($result->ListTemplatesResult);
  745. }
  746. return $ret;
  747. }
  748. /**
  749. * Check whether a template file is available on LiveDocx service
  750. *
  751. * @param string $filename
  752. * @return boolean
  753. * @since LiveDocx 1.0
  754. */
  755. public function templateExists($filename)
  756. {
  757. $this->logIn();
  758. $result = $this->getSoapClient()->TemplateExists(array(
  759. 'filename' => basename($filename),
  760. ));
  761. return (boolean) $result->TemplateExistsResult;
  762. }
  763. /**
  764. * Share a document - i.e. the document is available to all over the Internet
  765. *
  766. * @return string
  767. * @since LiveDocx 1.0
  768. */
  769. public function shareDocument()
  770. {
  771. $this->logIn();
  772. $ret = null;
  773. $result = $this->getSoapClient()->ShareDocument();
  774. if (isset($result->ShareDocumentResult)) {
  775. $ret = (string) $result->ShareDocumentResult;
  776. }
  777. return $ret;
  778. }
  779. /**
  780. * List all shared documents stored on LiveDocx service
  781. *
  782. * @return array
  783. * @since LiveDocx 1.0
  784. */
  785. public function listSharedDocuments()
  786. {
  787. $this->logIn();
  788. $ret = array();
  789. $result = $this->getSoapClient()->ListSharedDocuments();
  790. if (isset($result->ListSharedDocumentsResult)) {
  791. $ret = $this->_backendListArrayToMultiAssocArray(
  792. $result->ListSharedDocumentsResult
  793. );
  794. }
  795. return $ret;
  796. }
  797. /**
  798. * Delete a shared document from LiveDocx service
  799. *
  800. * @param string $filename
  801. * @return void
  802. * @since LiveDocx 1.0
  803. */
  804. public function deleteSharedDocument($filename)
  805. {
  806. $this->logIn();
  807. $this->getSoapClient()->DeleteSharedDocument(array(
  808. 'filename' => basename($filename),
  809. ));
  810. }
  811. /*
  812. * Download a shared document from LiveDocx service
  813. *
  814. * @param string $filename
  815. * @return binary
  816. * @throws Zend_Service_LiveDocx_Exception
  817. * @since LiveDocx 1.0
  818. */
  819. public function downloadSharedDocument($filename)
  820. {
  821. $this->logIn();
  822. try {
  823. $result = $this->getSoapClient()->DownloadSharedDocument(array(
  824. 'filename' => basename($filename),
  825. ));
  826. } catch (Exception $e) {
  827. require_once 'Zend/Service/LiveDocx/Exception.php';
  828. throw new Zend_Service_LiveDocx_Exception(
  829. 'Cannot download shared document', 0, $e
  830. );
  831. }
  832. return base64_decode($result->DownloadSharedDocumentResult);
  833. }
  834. /**
  835. * Check whether a shared document is available on LiveDocx service
  836. *
  837. * @param string $filename
  838. * @return boolean
  839. * @since LiveDocx 1.0
  840. */
  841. public function sharedDocumentExists($filename)
  842. {
  843. $this->logIn();
  844. $ret = false;
  845. $sharedDocuments = $this->listSharedDocuments();
  846. foreach ($sharedDocuments as $shareDocument) {
  847. if (isset($shareDocument['filename'])
  848. && (basename($filename) === $shareDocument['filename'])
  849. ) {
  850. $ret = true;
  851. break;
  852. }
  853. }
  854. return $ret;
  855. }
  856. /**
  857. * Return supported template formats (lowercase)
  858. *
  859. * @return array
  860. * @since LiveDocx 1.0
  861. */
  862. public function getTemplateFormats()
  863. {
  864. $this->logIn();
  865. $ret = array();
  866. $result = $this->getSoapClient()->GetTemplateFormats();
  867. if (isset($result->GetTemplateFormatsResult->string)) {
  868. $ret = $result->GetTemplateFormatsResult->string;
  869. $ret = array_map('strtolower', $ret);
  870. }
  871. return $ret;
  872. }
  873. /**
  874. * Return supported document formats (lowercase)
  875. *
  876. * @return array
  877. * @since LiveDocx 1.1
  878. */
  879. public function getDocumentFormats()
  880. {
  881. $this->logIn();
  882. $ret = array();
  883. $result = $this->getSoapClient()->GetDocumentFormats();
  884. if (isset($result->GetDocumentFormatsResult->string)) {
  885. $ret = $result->GetDocumentFormatsResult->string;
  886. $ret = array_map('strtolower', $ret);
  887. }
  888. return $ret;
  889. }
  890. /*
  891. * Return supported image formats (lowercase)
  892. *
  893. * @return array
  894. * @since LiveDocx 1.2
  895. */
  896. public function getImageFormats()
  897. {
  898. $this->logIn();
  899. $ret = array();
  900. $result = $this->getSoapClient()->GetImageFormats();
  901. if (isset($result->GetImageFormatsResult->string)) {
  902. $ret = $result->GetImageFormatsResult->string;
  903. $ret = array_map('strtolower', $ret);
  904. }
  905. return $ret;
  906. }
  907. /**
  908. * Return the names of all fonts that are installed on backend server
  909. *
  910. * @return array
  911. * @since LiveDocx 1.2
  912. */
  913. public function getFontNames()
  914. {
  915. $this->logIn();
  916. $ret = array();
  917. $result = $this->getSoapClient()->GetFontNames();
  918. if (isset($result->GetFontNamesResult->string)) {
  919. $ret = $result->GetFontNamesResult->string;
  920. }
  921. return $ret;
  922. }
  923. /**
  924. * Convert LiveDocx service return value from list methods to consistent PHP array
  925. *
  926. * @param array $list
  927. * @return array
  928. * @since LiveDocx 1.0
  929. */
  930. protected function _backendListArrayToMultiAssocArray($list)
  931. {
  932. $this->logIn();
  933. $ret = array();
  934. if (isset($list->ArrayOfString)) {
  935. foreach ($list->ArrayOfString as $a) {
  936. if (is_array($a)) { // 1 template only
  937. $o = new stdClass();
  938. $o->string = $a;
  939. } else { // 2 or more templates
  940. $o = $a;
  941. }
  942. unset($a);
  943. if (isset($o->string)) {
  944. $date1 = new Zend_Date($o->string[3], Zend_Date::RFC_1123);
  945. $date2 = new Zend_Date($o->string[1], Zend_Date::RFC_1123);
  946. $ret[] = array (
  947. 'filename' => $o->string[0],
  948. 'fileSize' => (integer) $o->string[2],
  949. 'createTime' => (integer) $date1->get(Zend_Date::TIMESTAMP),
  950. 'modifyTime' => (integer) $date2->get(Zend_Date::TIMESTAMP),
  951. );
  952. }
  953. }
  954. }
  955. return $ret;
  956. }
  957. /**
  958. * Convert assoc array to required SOAP type
  959. *
  960. * @param array $assoc
  961. *
  962. * @return array
  963. * @since LiveDocx 1.0
  964. */
  965. public static function assocArrayToArrayOfArrayOfString($assoc)
  966. {
  967. $arrayKeys = array_keys($assoc);
  968. $arrayValues = array_values($assoc);
  969. return array($arrayKeys, $arrayValues);
  970. }
  971. /**
  972. * Convert multi assoc array to required SOAP type
  973. *
  974. * @param array $multi
  975. * @return array
  976. * @since LiveDocx 1.0
  977. */
  978. public static function multiAssocArrayToArrayOfArrayOfString($multi)
  979. {
  980. $arrayKeys = array_keys($multi[0]);
  981. $arrayValues = array();
  982. foreach ($multi as $v) {
  983. $arrayValues[] = array_values($v);
  984. }
  985. $arrayKeys = array($arrayKeys);
  986. return array_merge($arrayKeys, $arrayValues);
  987. }
  988. }