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