|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00024 require_once 'Zend/Date.php'; 00025 00027 require_once 'Zend/Service/LiveDocx.php'; 00028 00037 class Zend_Service_LiveDocx_MailMerge extends Zend_Service_LiveDocx 00038 { 00043 const WSDL = 'https://api.livedocx.com/1.2/mailmerge.asmx?WSDL'; 00044 00051 protected $_fieldValues; 00052 00059 protected $_blockFieldValues; 00060 00068 public function __construct($options = null) 00069 { 00070 $this->_wsdl = self::WSDL; 00071 $this->_fieldValues = array(); 00072 $this->_blockFieldValues = array(); 00073 00074 parent::__construct($options); 00075 } 00076 00086 public function setLocalTemplate($filename) 00087 { 00088 $this->logIn(); 00089 00090 try { 00091 $this->getSoapClient()->SetLocalTemplate(array( 00092 'template' => base64_encode(file_get_contents($filename)), 00093 'format' => self::getFormat($filename), 00094 )); 00095 } catch (Exception $e) { 00096 require_once 'Zend/Service/LiveDocx/Exception.php'; 00097 throw new Zend_Service_LiveDocx_Exception( 00098 'Cannot set local template', 0, $e 00099 ); 00100 } 00101 00102 return $this; 00103 } 00104 00114 public function setRemoteTemplate($filename) 00115 { 00116 $this->logIn(); 00117 00118 try { 00119 $this->getSoapClient()->SetRemoteTemplate(array( 00120 'filename' => $filename, 00121 )); 00122 } catch (Exception $e) { 00123 require_once 'Zend/Service/LiveDocx/Exception.php'; 00124 throw new Zend_Service_LiveDocx_Exception( 00125 'Cannot set remote template', 0, $e 00126 ); 00127 } 00128 00129 return $this; 00130 } 00131 00140 public function setFieldValues($values) 00141 { 00142 $this->logIn(); 00143 00144 foreach ($values as $value) { 00145 if (is_array($value)) { 00146 $method = 'multiAssocArrayToArrayOfArrayOfString'; 00147 } else { 00148 $method = 'assocArrayToArrayOfArrayOfString'; 00149 } 00150 break; 00151 } 00152 00153 try { 00154 $this->getSoapClient()->SetFieldValues(array( 00155 'fieldValues' => self::$method($values), 00156 )); 00157 } catch (Exception $e) { 00158 require_once 'Zend/Service/LiveDocx/Exception.php'; 00159 throw new Zend_Service_LiveDocx_Exception( 00160 'Cannot set field values', 0, $e 00161 ); 00162 } 00163 00164 return $this; 00165 } 00166 00177 public function setFieldValue($field, $value) 00178 { 00179 $this->_fieldValues[$field] = $value; 00180 00181 return $this; 00182 } 00183 00194 public function setBlockFieldValues($blockName, $blockFieldValues) 00195 { 00196 $this->logIn(); 00197 00198 try { 00199 $this->getSoapClient()->SetBlockFieldValues(array( 00200 'blockName' => $blockName, 00201 'blockFieldValues' => self::multiAssocArrayToArrayOfArrayOfString($blockFieldValues) 00202 )); 00203 } catch (Exception $e) { 00204 require_once 'Zend/Service/LiveDocx/Exception.php'; 00205 throw new Zend_Service_LiveDocx_Exception( 00206 'Cannot set block field values', 0, $e 00207 ); 00208 } 00209 00210 return $this; 00211 } 00212 00222 public function assign($field, $value = null) 00223 { 00224 try { 00225 if (is_array($field) && (null === $value)) { 00226 foreach ($field as $fieldName => $fieldValue) { 00227 $this->setFieldValue($fieldName, $fieldValue); 00228 } 00229 } elseif (is_array($value)) { 00230 $this->setBlockFieldValues($field, $value); 00231 } else { 00232 $this->setFieldValue($field, $value); 00233 } 00234 } catch (Exception $e) { 00235 require_once 'Zend/Service/LiveDocx/Exception.php'; 00236 throw new Zend_Service_LiveDocx_Exception( 00237 'Cannot assign data to template', 0, $e 00238 ); 00239 } 00240 00241 return $this; 00242 } 00243 00254 public function setDocumentPassword($password) 00255 { 00256 $this->logIn(); 00257 00258 try { 00259 $this->getSoapClient()->SetDocumentPassword(array( 00260 'password' => $password 00261 )); 00262 } catch (Exception $e) { 00263 require_once 'Zend/Service/LiveDocx/Exception.php'; 00264 throw new Zend_Service_LiveDocx_Exception( 00265 'Cannot set document password. This method can be used on PDF files only.', 0, $e 00266 ); 00267 } 00268 00269 return $this; 00270 } 00271 00298 public function setDocumentAccessPermissions($permissions, $password) 00299 { 00300 $this->logIn(); 00301 00302 try { 00303 $this->getSoapClient()->SetDocumentAccessPermissions(array( 00304 'permissions' => $permissions, 00305 'password' => $password 00306 )); 00307 } catch (Exception $e) { 00308 require_once 'Zend/Service/LiveDocx/Exception.php'; 00309 throw new Zend_Service_LiveDocx_Exception( 00310 'Cannot set document access permissions', 0, $e 00311 ); 00312 } 00313 00314 return $this; 00315 } 00316 00324 public function createDocument() 00325 { 00326 $this->logIn(); 00327 00328 if (count($this->_fieldValues) > 0) { 00329 $this->setFieldValues($this->_fieldValues); 00330 } 00331 00332 $this->_fieldValues = array(); 00333 $this->_blockFieldValues = array(); 00334 00335 try { 00336 $this->getSoapClient()->CreateDocument(); 00337 } catch (Exception $e) { 00338 require_once 'Zend/Service/LiveDocx/Exception.php'; 00339 throw new Zend_Service_LiveDocx_Exception( 00340 'Cannot create document', 0, $e 00341 ); 00342 } 00343 } 00344 00354 public function retrieveDocument($format) 00355 { 00356 $this->logIn(); 00357 00358 $format = strtolower($format); 00359 00360 try { 00361 $result = $this->getSoapClient()->RetrieveDocument(array( 00362 'format' => $format, 00363 )); 00364 } catch (Exception $e) { 00365 require_once 'Zend/Service/LiveDocx/Exception.php'; 00366 throw new Zend_Service_LiveDocx_Exception( 00367 'Cannot retrieve document - call setLocalTemplate() or setRemoteTemplate() first', 0, $e 00368 ); 00369 } 00370 00371 return base64_decode($result->RetrieveDocumentResult); 00372 } 00373 00383 public function getMetafiles($fromPage, $toPage) 00384 { 00385 $this->logIn(); 00386 00387 $ret = array(); 00388 $result = $this->getSoapClient()->GetMetafiles(array( 00389 'fromPage' => (integer) $fromPage, 00390 'toPage' => (integer) $toPage, 00391 )); 00392 00393 if (isset($result->GetMetafilesResult->string)) { 00394 $pageCounter = (integer) $fromPage; 00395 if (is_array($result->GetMetafilesResult->string)) { 00396 foreach ($result->GetMetafilesResult->string as $string) { 00397 $ret[$pageCounter] = base64_decode($string); 00398 $pageCounter++; 00399 } 00400 } else { 00401 $ret[$pageCounter] = base64_decode($result->GetMetafilesResult->string); 00402 } 00403 } 00404 00405 return $ret; 00406 } 00407 00415 public function getAllMetafiles() 00416 { 00417 $this->logIn(); 00418 00419 $ret = array(); 00420 $result = $this->getSoapClient()->GetAllMetafiles(); 00421 00422 if (isset($result->GetAllMetafilesResult->string)) { 00423 $pageCounter = 1; 00424 if (is_array($result->GetAllMetafilesResult->string)) { 00425 foreach ($result->GetAllMetafilesResult->string as $string) { 00426 $ret[$pageCounter] = base64_decode($string); 00427 $pageCounter++; 00428 } 00429 } else { 00430 $ret[$pageCounter] = base64_decode($result->GetAllMetafilesResult->string); 00431 } 00432 } 00433 00434 return $ret; 00435 } 00436 00448 public function getBitmaps($fromPage, $toPage, $zoomFactor, $format) 00449 { 00450 $this->logIn(); 00451 00452 $ret = array(); 00453 00454 $result = $this->getSoapClient()->GetBitmaps(array( 00455 'fromPage' => (integer) $fromPage, 00456 'toPage' => (integer) $toPage, 00457 'zoomFactor' => (integer) $zoomFactor, 00458 'format' => (string) $format, 00459 )); 00460 00461 if (isset($result->GetBitmapsResult->string)) { 00462 $pageCounter = (integer) $fromPage; 00463 if (is_array($result->GetBitmapsResult->string)) { 00464 foreach ($result->GetBitmapsResult->string as $string) { 00465 $ret[$pageCounter] = base64_decode($string); 00466 $pageCounter++; 00467 } 00468 } else { 00469 $ret[$pageCounter] = base64_decode($result->GetBitmapsResult->string); 00470 } 00471 } 00472 00473 return $ret; 00474 } 00475 00485 public function getAllBitmaps($zoomFactor, $format) 00486 { 00487 $this->logIn(); 00488 00489 $ret = array(); 00490 $result = $this->getSoapClient()->GetAllBitmaps(array( 00491 'zoomFactor' => (integer) $zoomFactor, 00492 'format' => (string) $format, 00493 )); 00494 00495 if (isset($result->GetAllBitmapsResult->string)) { 00496 $pageCounter = 1; 00497 if (is_array($result->GetAllBitmapsResult->string)) { 00498 foreach ($result->GetAllBitmapsResult->string as $string) { 00499 $ret[$pageCounter] = base64_decode($string); 00500 $pageCounter++; 00501 } 00502 } else { 00503 $ret[$pageCounter] = base64_decode($result->GetAllBitmapsResult->string); 00504 } 00505 } 00506 00507 return $ret; 00508 } 00509 00516 public function getFieldNames() 00517 { 00518 $this->logIn(); 00519 00520 $ret = array(); 00521 $result = $this->getSoapClient()->GetFieldNames(); 00522 00523 if (isset($result->GetFieldNamesResult->string)) { 00524 if (is_array($result->GetFieldNamesResult->string)) { 00525 $ret = $result->GetFieldNamesResult->string; 00526 } else { 00527 $ret[] = $result->GetFieldNamesResult->string; 00528 } 00529 } 00530 00531 return $ret; 00532 } 00533 00541 public function getBlockFieldNames($blockName) 00542 { 00543 $this->logIn(); 00544 00545 $ret = array(); 00546 $result = $this->getSoapClient()->GetBlockFieldNames(array( 00547 'blockName' => $blockName 00548 )); 00549 00550 if (isset($result->GetBlockFieldNamesResult->string)) { 00551 if (is_array($result->GetBlockFieldNamesResult->string)) { 00552 $ret = $result->GetBlockFieldNamesResult->string; 00553 } else { 00554 $ret[] = $result->GetBlockFieldNamesResult->string; 00555 } 00556 } 00557 00558 return $ret; 00559 } 00560 00567 public function getBlockNames() 00568 { 00569 $this->logIn(); 00570 00571 $ret = array(); 00572 $result = $this->getSoapClient()->GetBlockNames(); 00573 00574 if (isset($result->GetBlockNamesResult->string)) { 00575 if (is_array($result->GetBlockNamesResult->string)) { 00576 $ret = $result->GetBlockNamesResult->string; 00577 } else { 00578 $ret[] = $result->GetBlockNamesResult->string; 00579 } 00580 } 00581 00582 return $ret; 00583 } 00584 00593 public function uploadTemplate($filename) 00594 { 00595 $this->logIn(); 00596 00597 try { 00598 $this->getSoapClient()->UploadTemplate(array( 00599 'template' => base64_encode(file_get_contents($filename)), 00600 'filename' => basename($filename), 00601 )); 00602 } catch (Exception $e) { 00603 require_once 'Zend/Service/LiveDocx/Exception.php'; 00604 throw new Zend_Service_LiveDocx_Exception( 00605 'Cannot upload template', 0, $e 00606 ); 00607 } 00608 } 00609 00618 public function downloadTemplate($filename) 00619 { 00620 $this->logIn(); 00621 00622 try { 00623 $result = $this->getSoapClient()->DownloadTemplate(array( 00624 'filename' => basename($filename), 00625 )); 00626 } catch (Exception $e) { 00627 require_once 'Zend/Service/LiveDocx/Exception.php'; 00628 throw new Zend_Service_LiveDocx_Exception( 00629 'Cannot download template', 0, $e 00630 ); 00631 } 00632 00633 return base64_decode($result->DownloadTemplateResult); 00634 } 00635 00644 public function deleteTemplate($filename) 00645 { 00646 $this->logIn(); 00647 00648 $this->getSoapClient()->DeleteTemplate(array( 00649 'filename' => basename($filename), 00650 )); 00651 } 00652 00659 public function listTemplates() 00660 { 00661 $this->logIn(); 00662 00663 $ret = array(); 00664 $result = $this->getSoapClient()->ListTemplates(); 00665 00666 if (isset($result->ListTemplatesResult)) { 00667 $ret = $this->_backendListArrayToMultiAssocArray($result->ListTemplatesResult); 00668 } 00669 00670 return $ret; 00671 } 00672 00680 public function templateExists($filename) 00681 { 00682 $this->logIn(); 00683 00684 $result = $this->getSoapClient()->TemplateExists(array( 00685 'filename' => basename($filename), 00686 )); 00687 00688 return (boolean) $result->TemplateExistsResult; 00689 } 00690 00697 public function shareDocument() 00698 { 00699 $this->logIn(); 00700 00701 $ret = null; 00702 $result = $this->getSoapClient()->ShareDocument(); 00703 00704 if (isset($result->ShareDocumentResult)) { 00705 $ret = (string) $result->ShareDocumentResult; 00706 } 00707 00708 return $ret; 00709 } 00710 00717 public function listSharedDocuments() 00718 { 00719 $this->logIn(); 00720 00721 $ret = array(); 00722 $result = $this->getSoapClient()->ListSharedDocuments(); 00723 00724 if (isset($result->ListSharedDocumentsResult)) { 00725 $ret = $this->_backendListArrayToMultiAssocArray( 00726 $result->ListSharedDocumentsResult 00727 ); 00728 } 00729 00730 return $ret; 00731 } 00732 00740 public function deleteSharedDocument($filename) 00741 { 00742 $this->logIn(); 00743 00744 $this->getSoapClient()->DeleteSharedDocument(array( 00745 'filename' => basename($filename), 00746 )); 00747 } 00748 00749 /* 00750 * Download a shared document from LiveDocx service 00751 * 00752 * @param string $filename 00753 * @return binary 00754 * @throws Zend_Service_LiveDocx_Exception 00755 * @since LiveDocx 1.0 00756 */ 00757 public function downloadSharedDocument($filename) 00758 { 00759 $this->logIn(); 00760 00761 try { 00762 $result = $this->getSoapClient()->DownloadSharedDocument(array( 00763 'filename' => basename($filename), 00764 )); 00765 } catch (Exception $e) { 00766 require_once 'Zend/Service/LiveDocx/Exception.php'; 00767 throw new Zend_Service_LiveDocx_Exception( 00768 'Cannot download shared document', 0, $e 00769 ); 00770 } 00771 00772 return base64_decode($result->DownloadSharedDocumentResult); 00773 } 00774 00782 public function sharedDocumentExists($filename) 00783 { 00784 $this->logIn(); 00785 00786 $ret = false; 00787 $sharedDocuments = $this->listSharedDocuments(); 00788 foreach ($sharedDocuments as $shareDocument) { 00789 if (isset($shareDocument['filename']) 00790 && (basename($filename) === $shareDocument['filename']) 00791 ) { 00792 $ret = true; 00793 break; 00794 } 00795 } 00796 00797 return $ret; 00798 } 00799 00806 public function getTemplateFormats() 00807 { 00808 $this->logIn(); 00809 00810 $ret = array(); 00811 $result = $this->getSoapClient()->GetTemplateFormats(); 00812 00813 if (isset($result->GetTemplateFormatsResult->string)) { 00814 $ret = $result->GetTemplateFormatsResult->string; 00815 $ret = array_map('strtolower', $ret); 00816 } 00817 00818 return $ret; 00819 } 00820 00827 public function getDocumentFormats() 00828 { 00829 $this->logIn(); 00830 00831 $ret = array(); 00832 $result = $this->getSoapClient()->GetDocumentFormats(); 00833 00834 if (isset($result->GetDocumentFormatsResult->string)) { 00835 $ret = $result->GetDocumentFormatsResult->string; 00836 $ret = array_map('strtolower', $ret); 00837 } 00838 00839 return $ret; 00840 } 00841 00842 /* 00843 * Return supported image formats (lowercase) 00844 * 00845 * @return array 00846 * @since LiveDocx 1.2 00847 */ 00848 public function getImageFormats() 00849 { 00850 $this->logIn(); 00851 00852 $ret = array(); 00853 $result = $this->getSoapClient()->GetImageFormats(); 00854 00855 if (isset($result->GetImageFormatsResult->string)) { 00856 $ret = $result->GetImageFormatsResult->string; 00857 $ret = array_map('strtolower', $ret); 00858 } 00859 00860 return $ret; 00861 } 00862 00869 public function getFontNames() 00870 { 00871 $this->logIn(); 00872 00873 $ret = array(); 00874 $result = $this->getSoapClient()->GetFontNames(); 00875 00876 if (isset($result->GetFontNamesResult->string)) { 00877 $ret = $result->GetFontNamesResult->string; 00878 } 00879 00880 return $ret; 00881 } 00882 00889 public function getDocumentAccessOptions() 00890 { 00891 $this->logIn(); 00892 00893 $ret = array(); 00894 $result = $this->getSoapClient()->GetDocumentAccessOptions(); 00895 00896 if (isset($result->GetDocumentAccessOptionsResult->string)) { 00897 $ret = $result->GetDocumentAccessOptionsResult->string; 00898 } 00899 00900 return $ret; 00901 } 00902 00910 protected function _backendListArrayToMultiAssocArray($list) 00911 { 00912 $this->logIn(); 00913 00914 $ret = array(); 00915 if (isset($list->ArrayOfString)) { 00916 foreach ($list->ArrayOfString as $a) { 00917 if (is_array($a)) { // 1 template only 00918 $o = new stdClass(); 00919 $o->string = $a; 00920 } else { // 2 or more templates 00921 $o = $a; 00922 } 00923 unset($a); 00924 00925 if (isset($o->string)) { 00926 $date1 = new Zend_Date($o->string[3], Zend_Date::RFC_1123); 00927 $date2 = new Zend_Date($o->string[1], Zend_Date::RFC_1123); 00928 00929 $ret[] = array ( 00930 'filename' => $o->string[0], 00931 'fileSize' => (integer) $o->string[2], 00932 'createTime' => (integer) $date1->get(Zend_Date::TIMESTAMP), 00933 'modifyTime' => (integer) $date2->get(Zend_Date::TIMESTAMP), 00934 ); 00935 } 00936 } 00937 } 00938 00939 return $ret; 00940 } 00941 00950 public static function assocArrayToArrayOfArrayOfString($assoc) 00951 { 00952 $arrayKeys = array_keys($assoc); 00953 $arrayValues = array_values($assoc); 00954 00955 return array($arrayKeys, $arrayValues); 00956 } 00957 00965 public static function multiAssocArrayToArrayOfArrayOfString($multi) 00966 { 00967 $arrayKeys = array_keys($multi[0]); 00968 $arrayValues = array(); 00969 00970 foreach ($multi as $v) { 00971 $arrayValues[] = array_values($v); 00972 } 00973 00974 $arrayKeys = array($arrayKeys); 00975 00976 return array_merge($arrayKeys, $arrayValues); 00977 } 00978 }