File indexing completed on 2024-05-26 06:03:27

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage LiveDocx
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Date **/
0024 // require_once 'Zend/Date.php';
0025 
0026 /** Zend_Service_LiveDocx **/
0027 // require_once 'Zend/Service/LiveDocx.php';
0028 
0029 /**
0030  * @category   Zend
0031  * @package    Zend_Service
0032  * @subpackage LiveDocx
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  * @since      LiveDocx 1.0
0036  */
0037 class Zend_Service_LiveDocx_MailMerge extends Zend_Service_LiveDocx
0038 {
0039     /**
0040      * URI of LiveDocx.MailMerge WSDL
0041      * @since LiveDocx 1.0
0042      */
0043     //const WSDL = 'https://api.livedocx.com/1.2/mailmerge.asmx?WSDL';
0044     const WSDL = 'https://api.livedocx.com/2.0/mailmerge.asmx?WSDL';
0045 
0046     /**
0047      * Field values
0048      *
0049      * @var   array
0050      * @since LiveDocx 1.0
0051      */
0052     protected $_fieldValues;
0053 
0054     /**
0055      * Block field values
0056      *
0057      * @var   array
0058      * @since LiveDocx 1.0
0059      */
0060     protected $_blockFieldValues;
0061 
0062     /**
0063      * Constructor (LiveDocx.MailMerge SOAP Service)
0064      *
0065      * @return void
0066      * @return throws Zend_Service_LiveDocx_Exception
0067      * @since  LiveDocx 1.0
0068      */
0069     public function __construct($options = null)
0070     {
0071         $this->_wsdl             = self::WSDL;
0072         $this->_fieldValues      = array();
0073         $this->_blockFieldValues = array();
0074 
0075         parent::__construct($options);
0076     }
0077 
0078     /**
0079      * Set the filename of a LOCAL template
0080      * (i.e. a template stored locally on YOUR server)
0081      *
0082      * @param  string $filename
0083      * @return Zend_Service_LiveDocx_MailMerge
0084      * @throws Zend_Service_LiveDocx_Exception
0085      * @since  LiveDocx 1.0
0086      */
0087     public function setLocalTemplate($filename)
0088     {
0089         if (!is_readable($filename)) {
0090             throw new Zend_Service_LiveDocx_Exception(
0091                 'Cannot read local template from disk.'
0092             );
0093         }
0094 
0095         $this->logIn();
0096 
0097         try {
0098             $this->getSoapClient()->SetLocalTemplate(array(
0099                 'template' => base64_encode(file_get_contents($filename)),
0100                 'format'   => self::getFormat($filename),
0101             ));
0102         } catch (Exception $e) {
0103             // require_once 'Zend/Service/LiveDocx/Exception.php';
0104             throw new Zend_Service_LiveDocx_Exception(
0105                 'Cannot set local template', 0, $e
0106             );
0107         }
0108 
0109         return $this;
0110     }
0111 
0112     /**
0113      * Set the filename of a REMOTE template
0114      * (i.e. a template stored remotely on the LIVEDOCX server)
0115      *
0116      * @param  string $filename
0117      * @return Zend_Service_LiveDocx_MailMerge
0118      * @throws Zend_Service_LiveDocx_Exception
0119      * @since  LiveDocx 1.0
0120      */
0121     public function setRemoteTemplate($filename)
0122     {
0123         $this->logIn();
0124 
0125         try {
0126             $this->getSoapClient()->SetRemoteTemplate(array(
0127                 'filename' => $filename,
0128             ));
0129         } catch (Exception $e) {
0130             // require_once 'Zend/Service/LiveDocx/Exception.php';
0131             throw new Zend_Service_LiveDocx_Exception(
0132                 'Cannot set remote template', 0, $e
0133             );
0134         }
0135 
0136         return $this;
0137     }
0138 
0139     /**
0140      * Set an associative or multi-associative array of keys and values pairs
0141      *
0142      * @param  array $values
0143      * @return Zend_Service_LiveDocx_MailMerge
0144      * @throws Zend_Service_LiveDocx_Exception
0145      * @since  LiveDocx 1.0
0146      */
0147     public function setFieldValues($values)
0148     {
0149         $this->logIn();
0150 
0151         foreach ($values as $value) {
0152             if (is_array($value)) {
0153                 $method = 'multiAssocArrayToArrayOfArrayOfString';
0154             } else {
0155                 $method = 'assocArrayToArrayOfArrayOfString';
0156             }
0157             break;
0158         }
0159 
0160         try {
0161             $this->getSoapClient()->SetFieldValues(array(
0162                 'fieldValues' => self::$method($values),
0163             ));
0164         } catch (Exception $e) {
0165             // require_once 'Zend/Service/LiveDocx/Exception.php';
0166             throw new Zend_Service_LiveDocx_Exception(
0167                 'Cannot set field values', 0, $e
0168             );
0169         }
0170 
0171         return $this;
0172     }
0173 
0174     /**
0175      * Set an array of key and value or array of values
0176      *
0177      * @param string $field
0178      * @param array|string $value
0179      *
0180      * @throws Zend_Service_LiveDocx_Exception
0181      * @return Zend_Service_LiveDocx_MailMerge
0182      * @since  LiveDocx 1.0
0183      */
0184     public function setFieldValue($field, $value)
0185     {
0186         $this->_fieldValues[$field] = $value;
0187 
0188         return $this;
0189     }
0190 
0191     /**
0192      * Set block field values
0193      *
0194      * @param string $blockName
0195      * @param array $blockFieldValues
0196      *
0197      * @return Zend_Service_LiveDocx_MailMerge
0198      * @throws Zend_Service_LiveDocx_Exception
0199      * @since  LiveDocx 1.0
0200      */
0201     public function setBlockFieldValues($blockName, $blockFieldValues)
0202     {
0203         $this->logIn();
0204 
0205         try {
0206             $this->getSoapClient()->SetBlockFieldValues(array(
0207                 'blockName'        => $blockName,
0208                 'blockFieldValues' => self::multiAssocArrayToArrayOfArrayOfString($blockFieldValues)
0209             ));
0210         } catch (Exception $e) {
0211             // require_once 'Zend/Service/LiveDocx/Exception.php';
0212             throw new Zend_Service_LiveDocx_Exception(
0213                 'Cannot set block field values', 0, $e
0214             );
0215         }
0216 
0217         return $this;
0218     }
0219 
0220     /**
0221      * Assign values to template fields
0222      *
0223      * @param array|string $field
0224      * @param array|string $value
0225      * @return Zend_Service_LiveDocx_MailMerge
0226      * @throws Zend_Service_LiveDocx_Exception
0227      * @since  LiveDocx 1.0
0228      */
0229     public function assign($field, $value = null)
0230     {
0231         try {
0232             if (is_array($field) && (null === $value)) {
0233                 foreach ($field as $fieldName => $fieldValue) {
0234                     $this->setFieldValue($fieldName, $fieldValue);
0235                 }
0236             } elseif (is_array($value)) {
0237                 $this->setBlockFieldValues($field, $value);
0238             } else {
0239                 $this->setFieldValue($field, $value);
0240             }
0241         } catch (Exception $e) {
0242             // require_once 'Zend/Service/LiveDocx/Exception.php';
0243             throw new Zend_Service_LiveDocx_Exception(
0244                 'Cannot assign data to template', 0, $e
0245             );
0246         }
0247 
0248         return $this;
0249     }
0250 
0251     /**
0252      * Set a password to open to document
0253      *
0254      * This method can only be used for PDF documents
0255      *
0256      * @param  string  $password
0257      * @return Zend_Service_LiveDocx_MailMerge
0258      * @throws Zend_Service_LiveDocx_Exception
0259      * @since  LiveDocx 1.2 Premium
0260      */
0261     public function setDocumentPassword($password)
0262     {
0263         $this->logIn();
0264 
0265         try {
0266             $this->getSoapClient()->SetDocumentPassword(array(
0267                 'password' => $password
0268             ));
0269         } catch (Exception $e) {
0270             // require_once 'Zend/Service/LiveDocx/Exception.php';
0271             throw new Zend_Service_LiveDocx_Exception(
0272                 'Cannot set document password. This method can be used on PDF files only.', 0, $e
0273             );
0274         }
0275 
0276         return $this;
0277     }
0278 
0279     /**
0280      * Set a master password for document and determine which security features
0281      * are accessible without using the master password.
0282      *
0283      * As default, nothing is allowed. To allow a security setting,
0284      * explicatively set it using one of he DOCUMENT_ACCESS_PERMISSION_* class
0285      * constants.
0286      *
0287      * {code}
0288      * $phpLiveDocx->setDocumentAccessPermissions(
0289      *     array (
0290      *         Zend_Service_LiveDocx_MailMerge::DOCUMENT_ACCESS_PERMISSION_ALLOW_PRINTING_HIGH_LEVEL,
0291      *         Zend_Service_LiveDocx_MailMerge::DOCUMENT_ACCESS_PERMISSION_ALLOW_EXTRACT_CONTENTS
0292      *     ),
0293      *     'myDocumentAccessPassword'
0294      * );
0295      * {code}
0296      *
0297      * This method can only be used for PDF documents
0298      *
0299      * @param  array  $permissions
0300      * @param  string $password
0301      * @return Zend_Service_LiveDocx_MailMerge
0302      * @throws Zend_Service_LiveDocx_Exception
0303      * @since  LiveDocx 1.2 Premium
0304      */
0305     public function setDocumentAccessPermissions($permissions, $password)
0306     {
0307         $this->logIn();
0308 
0309         try {
0310             $this->getSoapClient()->SetDocumentAccessPermissions(array(
0311                 'permissions' => $permissions,
0312                 'password'    => $password
0313             ));
0314         } catch (Exception $e) {
0315             // require_once 'Zend/Service/LiveDocx/Exception.php';
0316             throw new Zend_Service_LiveDocx_Exception(
0317                 'Cannot set document access permissions', 0, $e
0318             );
0319         }
0320 
0321         return $this;
0322     }
0323 
0324     /**
0325      * Merge assigned data with template to generate document
0326      *
0327      * @throws Zend_Service_LiveDocx_Excpetion
0328      * @return void
0329      * @since  LiveDocx 1.0
0330      */
0331     public function createDocument()
0332     {
0333         $this->logIn();
0334 
0335         if (count($this->_fieldValues) > 0) {
0336             $this->setFieldValues($this->_fieldValues);
0337         }
0338 
0339         $this->_fieldValues      = array();
0340         $this->_blockFieldValues = array();
0341 
0342         try {
0343             $this->getSoapClient()->CreateDocument();
0344         } catch (Exception $e) {
0345             // require_once 'Zend/Service/LiveDocx/Exception.php';
0346             throw new Zend_Service_LiveDocx_Exception(
0347                 'Cannot create document', 0, $e
0348             );
0349         }
0350     }
0351 
0352     /**
0353      * Retrieve document in specified format
0354      *
0355      * @param string $format
0356      *
0357      * @throws Zend_Service_LiveDocx_Exception
0358      * @return binary
0359      * @since  LiveDocx 1.0
0360      */
0361     public function retrieveDocument($format)
0362     {
0363         $this->logIn();
0364 
0365         $format = strtolower($format);
0366 
0367         try {
0368             $result = $this->getSoapClient()->RetrieveDocument(array(
0369                 'format' => $format,
0370             ));
0371         } catch (Exception $e) {
0372             // require_once 'Zend/Service/LiveDocx/Exception.php';
0373             throw new Zend_Service_LiveDocx_Exception(
0374                 'Cannot retrieve document - call setLocalTemplate() or setRemoteTemplate() first', 0, $e
0375             );
0376         }
0377 
0378         return base64_decode($result->RetrieveDocumentResult);
0379     }
0380 
0381     /**
0382      * Return WMF (aka Windows metafile) data for specified page range of created document
0383      * Return array contains WMF data (binary) - array key is page number
0384      *
0385      * @param  integer $fromPage
0386      * @param  integer $toPage
0387      * @return array
0388      * @since  LiveDocx 1.2
0389      */
0390     public function getMetafiles($fromPage, $toPage)
0391     {
0392         $this->logIn();
0393 
0394         $ret    = array();
0395         $result = $this->getSoapClient()->GetMetafiles(array(
0396             'fromPage' => (integer) $fromPage,
0397             'toPage'   => (integer) $toPage,
0398         ));
0399 
0400         if (isset($result->GetMetafilesResult->string)) {
0401             $pageCounter = (integer) $fromPage;
0402             if (is_array($result->GetMetafilesResult->string)) {
0403                 foreach ($result->GetMetafilesResult->string as $string) {
0404                     $ret[$pageCounter] = base64_decode($string);
0405                     $pageCounter++;
0406                 }
0407             } else {
0408                $ret[$pageCounter] = base64_decode($result->GetMetafilesResult->string);
0409             }
0410         }
0411 
0412         return $ret;
0413     }
0414 
0415     /**
0416      * Return WMF (aka Windows metafile) data for pages of created document
0417      * Return array contains WMF data (binary) - array key is page number
0418      *
0419      * @return array
0420      * @since  LiveDocx 1.2
0421      */
0422     public function getAllMetafiles()
0423     {
0424         $this->logIn();
0425 
0426         $ret    = array();
0427         $result = $this->getSoapClient()->GetAllMetafiles();
0428 
0429         if (isset($result->GetAllMetafilesResult->string)) {
0430             $pageCounter = 1;
0431             if (is_array($result->GetAllMetafilesResult->string)) {
0432                 foreach ($result->GetAllMetafilesResult->string as $string) {
0433                     $ret[$pageCounter] = base64_decode($string);
0434                     $pageCounter++;
0435                 }
0436             } else {
0437                $ret[$pageCounter] = base64_decode($result->GetAllMetafilesResult->string);
0438             }
0439         }
0440 
0441         return $ret;
0442     }
0443 
0444     /**
0445      * Return graphical bitmap data for specified page range of created document
0446      * Return array contains bitmap data (binary) - array key is page number
0447      *
0448      * @param  integer $fromPage
0449      * @param  integer $toPage
0450      * @param  integer $zoomFactor
0451      * @param  string  $format
0452      * @return array
0453      * @since  LiveDocx 1.2
0454      */
0455     public function getBitmaps($fromPage, $toPage, $zoomFactor, $format)
0456     {
0457         $this->logIn();
0458 
0459         $ret = array();
0460 
0461         $result = $this->getSoapClient()->GetBitmaps(array(
0462             'fromPage'   => (integer) $fromPage,
0463             'toPage'     => (integer) $toPage,
0464             'zoomFactor' => (integer) $zoomFactor,
0465             'format'     => (string)  $format,
0466         ));
0467 
0468         if (isset($result->GetBitmapsResult->string)) {
0469             $pageCounter = (integer) $fromPage;
0470             if (is_array($result->GetBitmapsResult->string)) {
0471                 foreach ($result->GetBitmapsResult->string as $string) {
0472                     $ret[$pageCounter] = base64_decode($string);
0473                     $pageCounter++;
0474                 }
0475             } else {
0476                $ret[$pageCounter] = base64_decode($result->GetBitmapsResult->string);
0477             }
0478         }
0479 
0480         return $ret;
0481     }
0482 
0483     /**
0484      * Return graphical bitmap data for all pages of created document
0485      * Return array contains bitmap data (binary) - array key is page number
0486      *
0487      * @param  integer $zoomFactor
0488      * @param  string  $format
0489      * @return array
0490      * @since  LiveDocx 1.2
0491      */
0492     public function getAllBitmaps($zoomFactor, $format)
0493     {
0494         $this->logIn();
0495 
0496         $ret    = array();
0497         $result = $this->getSoapClient()->GetAllBitmaps(array(
0498             'zoomFactor' => (integer) $zoomFactor,
0499             'format'     => (string)  $format,
0500         ));
0501 
0502         if (isset($result->GetAllBitmapsResult->string)) {
0503             $pageCounter = 1;
0504             if (is_array($result->GetAllBitmapsResult->string)) {
0505                 foreach ($result->GetAllBitmapsResult->string as $string) {
0506                     $ret[$pageCounter] = base64_decode($string);
0507                     $pageCounter++;
0508                 }
0509             } else {
0510                $ret[$pageCounter] = base64_decode($result->GetAllBitmapsResult->string);
0511             }
0512         }
0513 
0514         return $ret;
0515     }
0516 
0517     /**
0518      * Return all the fields in the template
0519      *
0520      * @return array
0521      * @since  LiveDocx 1.0
0522      */
0523     public function getFieldNames()
0524     {
0525         $this->logIn();
0526 
0527         $ret    = array();
0528         $result = $this->getSoapClient()->GetFieldNames();
0529 
0530         if (isset($result->GetFieldNamesResult->string)) {
0531             if (is_array($result->GetFieldNamesResult->string)) {
0532                 $ret = $result->GetFieldNamesResult->string;
0533             } else {
0534                 $ret[] = $result->GetFieldNamesResult->string;
0535             }
0536         }
0537 
0538         return $ret;
0539     }
0540 
0541     /**
0542      * Return all the block fields in the template
0543      *
0544      * @param  string $blockName
0545      * @return array
0546      * @since  LiveDocx 1.0
0547      */
0548     public function getBlockFieldNames($blockName)
0549     {
0550         $this->logIn();
0551 
0552         $ret    = array();
0553         $result = $this->getSoapClient()->GetBlockFieldNames(array(
0554             'blockName' => $blockName
0555         ));
0556 
0557         if (isset($result->GetBlockFieldNamesResult->string)) {
0558             if (is_array($result->GetBlockFieldNamesResult->string)) {
0559                 $ret = $result->GetBlockFieldNamesResult->string;
0560             } else {
0561                 $ret[] = $result->GetBlockFieldNamesResult->string;
0562             }
0563         }
0564 
0565         return $ret;
0566     }
0567 
0568     /**
0569      * Return all the block fields in the template
0570      *
0571      * @return array
0572      * @since  LiveDocx 1.0
0573      */
0574     public function getBlockNames()
0575     {
0576         $this->logIn();
0577 
0578         $ret    = array();
0579         $result = $this->getSoapClient()->GetBlockNames();
0580 
0581         if (isset($result->GetBlockNamesResult->string)) {
0582             if (is_array($result->GetBlockNamesResult->string)) {
0583                 $ret = $result->GetBlockNamesResult->string;
0584             } else {
0585                 $ret[] = $result->GetBlockNamesResult->string;
0586             }
0587         }
0588 
0589         return $ret;
0590     }
0591 
0592     /**
0593      * Upload a template file to LiveDocx service
0594      *
0595      * @param  string $filename
0596      * @return void
0597      * @throws Zend_Service_LiveDocx_Exception
0598      * @since  LiveDocx 1.0
0599      */
0600     public function uploadTemplate($filename)
0601     {
0602         $this->logIn();
0603 
0604         try {
0605             $this->getSoapClient()->UploadTemplate(array(
0606                 'template' => base64_encode(file_get_contents($filename)),
0607                 'filename' => basename($filename),
0608             ));
0609         } catch (Exception $e) {
0610             // require_once 'Zend/Service/LiveDocx/Exception.php';
0611             throw new Zend_Service_LiveDocx_Exception(
0612                 'Cannot upload template', 0, $e
0613             );
0614         }
0615     }
0616 
0617     /**
0618      * Download template file from LiveDocx service
0619      *
0620      * @param  string $filename
0621      * @return binary
0622      * @throws Zend_Service_LiveDocx_Exception
0623      * @since  LiveDocx 1.0
0624      */
0625     public function downloadTemplate($filename)
0626     {
0627         $this->logIn();
0628 
0629         try {
0630             $result = $this->getSoapClient()->DownloadTemplate(array(
0631                 'filename' => basename($filename),
0632             ));
0633         } catch (Exception $e) {
0634             // require_once 'Zend/Service/LiveDocx/Exception.php';
0635             throw new Zend_Service_LiveDocx_Exception(
0636                 'Cannot download template', 0, $e
0637             );
0638         }
0639 
0640         return base64_decode($result->DownloadTemplateResult);
0641     }
0642 
0643     /**
0644      * Delete a template file from LiveDocx service
0645      *
0646      * @param  string $filename
0647      * @return void
0648      * @throws Zend_Service_LiveDocx_Exception
0649      * @since  LiveDocx 1.0
0650      */
0651     public function deleteTemplate($filename)
0652     {
0653         $this->logIn();
0654 
0655         $this->getSoapClient()->DeleteTemplate(array(
0656             'filename' => basename($filename),
0657         ));
0658     }
0659 
0660     /**
0661      * List all templates stored on LiveDocx service
0662      *
0663      * @return array
0664      * @since  LiveDocx 1.0
0665      */
0666     public function listTemplates()
0667     {
0668         $this->logIn();
0669 
0670         $ret    = array();
0671         $result = $this->getSoapClient()->ListTemplates();
0672 
0673         if (isset($result->ListTemplatesResult)) {
0674             $ret = $this->_backendListArrayToMultiAssocArray($result->ListTemplatesResult);
0675         }
0676 
0677         return $ret;
0678     }
0679 
0680     /**
0681      * Check whether a template file is available on LiveDocx service
0682      *
0683      * @param  string $filename
0684      * @return boolean
0685      * @since  LiveDocx 1.0
0686      */
0687     public function templateExists($filename)
0688     {
0689         $this->logIn();
0690 
0691         $result = $this->getSoapClient()->TemplateExists(array(
0692             'filename' => basename($filename),
0693         ));
0694 
0695         return (boolean) $result->TemplateExistsResult;
0696     }
0697 
0698     /**
0699      * Share a document - i.e. the document is available to all over the Internet
0700      *
0701      * @return string
0702      * @since  LiveDocx 1.0
0703      */
0704     public function shareDocument()
0705     {
0706         $this->logIn();
0707 
0708         $ret    = null;
0709         $result = $this->getSoapClient()->ShareDocument();
0710 
0711         if (isset($result->ShareDocumentResult)) {
0712             $ret = (string) $result->ShareDocumentResult;
0713         }
0714 
0715         return $ret;
0716     }
0717 
0718     /**
0719      * List all shared documents stored on LiveDocx service
0720      *
0721      * @return array
0722      * @since  LiveDocx 1.0
0723      */
0724     public function listSharedDocuments()
0725     {
0726         $this->logIn();
0727 
0728         $ret    = array();
0729         $result = $this->getSoapClient()->ListSharedDocuments();
0730 
0731         if (isset($result->ListSharedDocumentsResult)) {
0732             $ret = $this->_backendListArrayToMultiAssocArray(
0733                 $result->ListSharedDocumentsResult
0734             );
0735         }
0736 
0737         return $ret;
0738     }
0739 
0740     /**
0741      * Delete a shared document from LiveDocx service
0742      *
0743      * @param  string $filename
0744      * @return void
0745      * @since  LiveDocx 1.0
0746      */
0747     public function deleteSharedDocument($filename)
0748     {
0749         $this->logIn();
0750 
0751         $this->getSoapClient()->DeleteSharedDocument(array(
0752             'filename' => basename($filename),
0753         ));
0754     }
0755 
0756     /*
0757      * Download a shared document from LiveDocx service
0758      *
0759      * @param  string $filename
0760      * @return binary
0761      * @throws Zend_Service_LiveDocx_Exception
0762      * @since  LiveDocx 1.0
0763      */
0764     public function downloadSharedDocument($filename)
0765     {
0766         $this->logIn();
0767 
0768         try {
0769             $result = $this->getSoapClient()->DownloadSharedDocument(array(
0770                 'filename' => basename($filename),
0771             ));
0772         } catch (Exception $e) {
0773             // require_once 'Zend/Service/LiveDocx/Exception.php';
0774             throw new Zend_Service_LiveDocx_Exception(
0775                 'Cannot download shared document', 0, $e
0776             );
0777         }
0778 
0779         return base64_decode($result->DownloadSharedDocumentResult);
0780     }
0781 
0782     /**
0783      * Check whether a shared document is available on LiveDocx service
0784      *
0785      * @param  string $filename
0786      * @return boolean
0787      * @since  LiveDocx 1.0
0788      */
0789     public function sharedDocumentExists($filename)
0790     {
0791         $this->logIn();
0792 
0793         $ret             = false;
0794         $sharedDocuments = $this->listSharedDocuments();
0795         foreach ($sharedDocuments as $shareDocument) {
0796             if (isset($shareDocument['filename'])
0797                 && (basename($filename) === $shareDocument['filename'])
0798             ) {
0799                 $ret = true;
0800                 break;
0801             }
0802         }
0803 
0804         return $ret;
0805     }
0806 
0807     /**
0808      * Return supported template formats (lowercase)
0809      *
0810      * @return array
0811      * @since  LiveDocx 1.0
0812      */
0813     public function getTemplateFormats()
0814     {
0815         $this->logIn();
0816 
0817         $ret    = array();
0818         $result = $this->getSoapClient()->GetTemplateFormats();
0819 
0820         if (isset($result->GetTemplateFormatsResult->string)) {
0821             $ret = $result->GetTemplateFormatsResult->string;
0822             $ret = array_map('strtolower', $ret);
0823         }
0824 
0825         return $ret;
0826     }
0827 
0828     /**
0829      * Return supported document formats (lowercase)
0830      *
0831      * @return array
0832      * @since  LiveDocx 1.1
0833      */
0834     public function getDocumentFormats()
0835     {
0836         $this->logIn();
0837 
0838         $ret    = array();
0839         $result = $this->getSoapClient()->GetDocumentFormats();
0840 
0841         if (isset($result->GetDocumentFormatsResult->string)) {
0842             $ret = $result->GetDocumentFormatsResult->string;
0843             $ret = array_map('strtolower', $ret);
0844         }
0845 
0846         return $ret;
0847     }
0848 
0849     /**
0850      * Return the names of all fonts that are installed on backend server
0851      *
0852      * @return array
0853      * @since  LiveDocx 1.2
0854      */
0855     public function getFontNames()
0856     {
0857         $this->logIn();
0858 
0859         $ret    = array();
0860         $result = $this->getSoapClient()->GetFontNames();
0861 
0862         if (isset($result->GetFontNamesResult->string)) {
0863             $ret = $result->GetFontNamesResult->string;
0864         }
0865 
0866         return $ret;
0867     }
0868 
0869     /**
0870      * Return supported document access options
0871      *
0872      * @return array
0873      * @since  LiveDocx 1.2 Premium
0874      */
0875     public function getDocumentAccessOptions()
0876     {
0877         $this->logIn();
0878 
0879         $ret    = array();
0880         $result = $this->getSoapClient()->GetDocumentAccessOptions();
0881 
0882         if (isset($result->GetDocumentAccessOptionsResult->string)) {
0883             $ret = $result->GetDocumentAccessOptionsResult->string;
0884         }
0885 
0886         return $ret;
0887     }
0888 
0889     /**
0890      * Return supported image formats from which can be imported (lowercase)
0891      *
0892      * @return array
0893      * @since  LiveDocx 2.0
0894      */
0895     public function getImageImportFormats()
0896     {
0897         $this->logIn();
0898 
0899         $ret    = array();
0900         $result = $this->getSoapClient()->GetImageImportFormats();
0901 
0902         if (isset($result->GetImageImportFormatsResult->string)) {
0903             $ret = $result->GetImageImportFormatsResult->string;
0904             $ret = array_map('strtolower', $ret);
0905         }
0906 
0907         return $ret;
0908     }
0909 
0910     /**
0911      * Return supported image formats to which can be exported (lowercase)
0912      *
0913      * @return array
0914      * @since  LiveDocx 2.0
0915      */
0916     public function getImageExportFormats()
0917     {
0918         $this->logIn();
0919 
0920         $ret    = array();
0921         $result = $this->getSoapClient()->GetImageExportFormats();
0922 
0923         if (isset($result->GetImageExportFormatsResult->string)) {
0924             $ret = $result->GetImageExportFormatsResult->string;
0925             $ret = array_map('strtolower', $ret);
0926         }
0927 
0928         return $ret;
0929     }
0930 
0931     /*
0932      * Return supported image formats (lowercase)
0933      *
0934      * @return array
0935      * @since  LiveDocx 1.2
0936      * @deprecated since LiveDocx 2.0
0937      */
0938     public function getImageFormats()
0939     {
0940         $replacement = 'getImageExportFormats';
0941 
0942         /*
0943         $errorMessage = sprintf(
0944                         "%s::%s is deprecated as of LiveDocx 2.0. "
0945                       . "It has been replaced by %s::%s() (drop in replacement)",
0946                         __CLASS__, __FUNCTION__, __CLASS__, $replacement);
0947 
0948         trigger_error($errorMessage, E_USER_NOTICE);
0949         */
0950 
0951         return $this->$replacement();
0952     }
0953 
0954     /**
0955      * Upload an image file to LiveDocx service
0956      *
0957      * @param  string $filename
0958      * @return void
0959      * @throws Zend_Service_LiveDocx_Exception
0960      * @since  LiveDocx 2.0
0961      */
0962     public function uploadImage($filename)
0963     {
0964         $this->logIn();
0965 
0966         try {
0967             $this->getSoapClient()->UploadImage(array(
0968                 'image'    => base64_encode(file_get_contents($filename)),
0969                 'filename' => basename($filename),
0970             ));
0971         } catch (Exception $e) {
0972             // require_once 'Zend/Service/LiveDocx/Exception.php';
0973             throw new Zend_Service_LiveDocx_Exception(
0974                 'Cannot upload image', 0, $e
0975             );
0976         }
0977     }
0978 
0979     /**
0980      * Download an image file from LiveDocx service
0981      *
0982      * @param  string $filename
0983      * @return void
0984      * @throws Zend_Service_LiveDocx_Exception
0985      * @since  LiveDocx 2.0
0986      */
0987     public function downloadImage($filename)
0988     {
0989         $this->logIn();
0990 
0991         try {
0992             $result = $this->getSoapClient()->DownloadImage(array(
0993                 'filename' => basename($filename),
0994             ));
0995         } catch (Exception $e) {
0996             // require_once 'Zend/Service/LiveDocx/Exception.php';
0997             throw new Zend_Service_LiveDocx_Exception(
0998                 'Cannot download image', 0, $e
0999             );
1000         }
1001 
1002         return base64_decode($result->DownloadImageResult);
1003     }
1004 
1005     /**
1006      * List all images stored on LiveDocx service
1007      *
1008      * @return array
1009      * @since  LiveDocx 2.0
1010      */
1011     public function listImages()
1012     {
1013         $this->logIn();
1014 
1015         $ret    = array();
1016         $result = $this->getSoapClient()->ListImages();
1017 
1018         if (isset($result->ListImagesResult)) {
1019             $ret = $this->_backendListArrayToMultiAssocArray($result->ListImagesResult);
1020         }
1021 
1022         return $ret;
1023     }
1024 
1025     /**
1026      * Delete an image file from LiveDocx service
1027      *
1028      * @param  string $filename
1029      * @return void
1030      * @throws Zend_Service_LiveDocx_Exception
1031      * @since  LiveDocx 2.0
1032      */
1033     public function deleteImage($filename)
1034     {
1035         $this->logIn();
1036 
1037         $this->getSoapClient()->DeleteImage(array(
1038             'filename' => basename($filename),
1039         ));
1040     }
1041 
1042     /**
1043      * Check whether an image file is available on LiveDocx service
1044      *
1045      * @param  string $filename
1046      * @return boolean
1047      * @since  LiveDocx 2.0
1048      */
1049     public function imageExists($filename)
1050     {
1051         $this->logIn();
1052 
1053         $result = $this->getSoapClient()->ImageExists(array(
1054             'filename' => basename($filename),
1055         ));
1056 
1057         return (boolean) $result->ImageExistsResult;
1058     }
1059 
1060     /**
1061      * Convert LiveDocx service return value from list methods to consistent PHP array
1062      *
1063      * @param  array $list
1064      * @return array
1065      * @since  LiveDocx 1.0
1066      */
1067     protected function _backendListArrayToMultiAssocArray($list)
1068     {
1069         $this->logIn();
1070 
1071         $ret = array();
1072         if (isset($list->ArrayOfString)) {
1073            foreach ($list->ArrayOfString as $a) {
1074                if (is_array($a)) {      // 1 template only
1075                    $o = new stdClass();
1076                    $o->string = $a;
1077                } else {                 // 2 or more templates
1078                    $o = $a;
1079                }
1080                unset($a);
1081 
1082                if (isset($o->string)) {
1083                    $date1 = new Zend_Date($o->string[3], Zend_Date::RFC_1123);
1084                    $date2 = new Zend_Date($o->string[1], Zend_Date::RFC_1123);
1085 
1086                    $ret[] = array (
1087                         'filename'   => $o->string[0],
1088                         'fileSize'   => (integer) $o->string[2],
1089                         'createTime' => (integer) $date1->get(Zend_Date::TIMESTAMP),
1090                         'modifyTime' => (integer) $date2->get(Zend_Date::TIMESTAMP),
1091                    );
1092                }
1093            }
1094         }
1095 
1096         return $ret;
1097     }
1098 
1099     /**
1100      * Convert assoc array to required SOAP type
1101      *
1102      * @param array $assoc
1103      *
1104      * @return array
1105      * @since  LiveDocx 1.0
1106      */
1107     public static function assocArrayToArrayOfArrayOfString($assoc)
1108     {
1109         $arrayKeys   = array_keys($assoc);
1110         $arrayValues = array_values($assoc);
1111 
1112         return array($arrayKeys, $arrayValues);
1113     }
1114 
1115     /**
1116      * Convert multi assoc array to required SOAP type
1117      *
1118      * @param  array $multi
1119      * @return array
1120      * @since  LiveDocx 1.0
1121      */
1122     public static function multiAssocArrayToArrayOfArrayOfString($multi)
1123     {
1124         $arrayKeys   = array_keys($multi[0]);
1125         $arrayValues = array();
1126 
1127         foreach ($multi as $v) {
1128             $arrayValues[] = array_values($v);
1129         }
1130 
1131         $arrayKeys = array($arrayKeys);
1132 
1133         return array_merge($arrayKeys, $arrayValues);
1134     }
1135 
1136     // -------------------------------------------------------------------------
1137 
1138 }