Helper.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Date **/
  23. require_once 'Zend/Date.php';
  24. /**
  25. * @category Demos
  26. * @package Demos_Zend_Service
  27. * @subpackage LiveDocx
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Demos_Zend_Service_LiveDocx_Helper
  32. {
  33. /**
  34. * Name of configuration file stored in /demos/Zend/Service/LiveDocx/
  35. */
  36. const CONFIGURATION_FILE = 'configuration.php';
  37. /**
  38. * Line length in characters (used to wrap long lines)
  39. */
  40. const LINE_LENGTH = 80;
  41. /**
  42. * Default locale
  43. */
  44. const LOCALE = 'en_US';
  45. /**
  46. * Return true, if configuration file exists and constants
  47. * DEMOS_ZEND_SERVICE_LIVEDOCX_USERNAME and
  48. * DEMOS_ZEND_SERVICE_LIVEDOCX_PASSWORD have been set.
  49. *
  50. * @return boolean
  51. */
  52. public static function credentialsAvailable()
  53. {
  54. $ret = false;
  55. $filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . self::CONFIGURATION_FILE;
  56. if (is_file($filename) && is_readable($filename)) {
  57. include_once $filename;
  58. if (defined('DEMOS_ZEND_SERVICE_LIVEDOCX_USERNAME') &&
  59. defined('DEMOS_ZEND_SERVICE_LIVEDOCX_PASSWORD') &&
  60. false !== DEMOS_ZEND_SERVICE_LIVEDOCX_USERNAME &&
  61. false !== DEMOS_ZEND_SERVICE_LIVEDOCX_PASSWORD ) {
  62. $ret = true;
  63. }
  64. }
  65. return $ret;
  66. }
  67. /**
  68. * Return instructions on how to register to use LiveDocx service and enter
  69. * username and password into configuration file.
  70. *
  71. * @return string
  72. */
  73. public static function credentialsHowTo()
  74. {
  75. $ret = PHP_EOL;
  76. $ret .= sprintf('ERROR: LIVEDOCX USERNAME AND PASSWORD HAVE NOT BEEN SET.%s', PHP_EOL);
  77. $ret .= PHP_EOL;
  78. $ret .= sprintf('1. Using a web browser, register to use the LiveDocx service at:%s', PHP_EOL);
  79. $ret .= sprintf(' https://www.livedocx.com/user/account_registration.aspx%s', PHP_EOL);
  80. $ret .= sprintf(' (takes less than 1 minute).%s', PHP_EOL);
  81. $ret .= PHP_EOL;
  82. $ret .= sprintf('2. Change directory into:%s', PHP_EOL);
  83. $ret .= sprintf(' %s%s', dirname(__FILE__), PHP_EOL);
  84. $ret .= PHP_EOL;
  85. $ret .= sprintf('3. Copy %s.dist to %s.%s', self::CONFIGURATION_FILE, self::CONFIGURATION_FILE, PHP_EOL);
  86. $ret .= PHP_EOL;
  87. $ret .= sprintf('4. Open %s in a text editor and enter the username and password%s', self::CONFIGURATION_FILE, PHP_EOL);
  88. $ret .= sprintf(' you obtained in step 1 (lines 43 and 44).%s', PHP_EOL);
  89. $ret .= PHP_EOL;
  90. $ret .= sprintf('5. Save and close configuration.php.%s', PHP_EOL);
  91. $ret .= PHP_EOL;
  92. $ret .= sprintf('Congratulations!%s', PHP_EOL);
  93. $ret .= PHP_EOL;
  94. $ret .= sprintf('You have now set up the Zend_Service_LiveDocx demo applications.%s', PHP_EOL);
  95. $ret .= PHP_EOL;
  96. return $ret;
  97. }
  98. /**
  99. * Decorator to format return value of list methods
  100. *
  101. * @param array $result
  102. * @return string
  103. */
  104. public static function listDecorator($result)
  105. {
  106. $ret = '';
  107. $date = new Zend_Date();
  108. if (count($result) > 0) {
  109. foreach ($result as $record) {
  110. $date->set($record['createTime']);
  111. $createTimeFormatted = $date->get(Zend_Date::RFC_1123);
  112. $date->set($record['modifyTime']);
  113. $modifyTimeFormatted = $date->get(Zend_Date::RFC_1123);
  114. $ret .= sprintf(' Filename : %s%s', $record['filename'], PHP_EOL);
  115. $ret .= sprintf(' File Size : %d b%s', $record['fileSize'], PHP_EOL);
  116. $ret .= sprintf(' Creation Time : %d (%s)%s', $record['createTime'], $createTimeFormatted, PHP_EOL);
  117. $ret .= sprintf('Last Modified Time : %d (%s)%s', $record['modifyTime'], $modifyTimeFormatted, PHP_EOL);
  118. $ret .= PHP_EOL;
  119. }
  120. }
  121. unset($date);
  122. return $ret;
  123. }
  124. /**
  125. * Decorator to format array
  126. *
  127. * @param array $result
  128. * @return string
  129. */
  130. public static function arrayDecorator($result)
  131. {
  132. $ret = '';
  133. $count = count($result);
  134. if ($count > 0) {
  135. for ($i = 0; $i < $count; $i ++) {
  136. $ret .= $result[$i];
  137. if ($count === ($i + 1)) {
  138. $ret .= '.';
  139. } elseif ($count === ($i + 2)) {
  140. $ret .= ' & ';
  141. } else {
  142. $ret .= ', ';
  143. }
  144. }
  145. } else {
  146. $ret .= 'none';
  147. }
  148. return $ret;
  149. }
  150. /**
  151. * Wrap the length of long lines
  152. *
  153. * @param string $str
  154. * @return string
  155. */
  156. public static function wrapLine($str)
  157. {
  158. return wordwrap($str, self::LINE_LENGTH);
  159. }
  160. }