PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/LiveDocx/MailMerge.php

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