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

/lib/php/HTML/Progress.php

https://bitbucket.org/adarshj/convenient_website
PHP | 2269 lines | 1077 code | 183 blank | 1009 comment | 168 complexity | 16a413e49d70bb7e5cdb6e3b0bc22118 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * The HTML_Progress class allow you to add a loading bar
  4. * to any of your xhtml document.
  5. * You should have a browser that accept DHTML feature.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category HTML
  16. * @package HTML_Progress
  17. * @author Laurent Laville <pear@laurent-laville.org>
  18. * @copyright 1997-2005 The PHP Group
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id: Progress.php,v 1.15 2005/07/25 13:11:26 farell Exp $
  21. * @link http://pear.php.net/package/HTML_Progress
  22. * @tutorial HTML_Progress.pkg
  23. */
  24. require_once 'HTML/Progress/DM.php';
  25. require_once 'HTML/Progress/UI.php';
  26. /**#@+
  27. * Progress Bar shape types
  28. *
  29. * @var integer
  30. * @since 0.6
  31. */
  32. define ('HTML_PROGRESS_BAR_HORIZONTAL', 1);
  33. define ('HTML_PROGRESS_BAR_VERTICAL', 2);
  34. /**#@-*/
  35. /**#@+
  36. * Progress Bar shape types
  37. *
  38. * @var integer
  39. * @since 1.2.0RC1
  40. */
  41. define ('HTML_PROGRESS_POLYGONAL', 3);
  42. define ('HTML_PROGRESS_CIRCLE', 4);
  43. /**#@-*/
  44. /**
  45. * Basic error code that indicate a wrong input
  46. *
  47. * @var integer
  48. * @since 1.0
  49. */
  50. define ('HTML_PROGRESS_ERROR_INVALID_INPUT', -100);
  51. /**
  52. * Basic error code that indicate a wrong callback definition.
  53. * Allows only function or class-method structure.
  54. *
  55. * @var integer
  56. * @since 1.1
  57. */
  58. define ('HTML_PROGRESS_ERROR_INVALID_CALLBACK',-101);
  59. /**
  60. * Basic error code that indicate a deprecated method
  61. * that may be removed at any time from a future version
  62. *
  63. * @var integer
  64. * @since 1.2.0RC1
  65. */
  66. define ('HTML_PROGRESS_DEPRECATED', -102);
  67. /**#@+
  68. * One of five possible return values from the error Callback
  69. *
  70. * @see HTML_Progress::_handleError
  71. * @var integer
  72. * @since 1.2.0
  73. */
  74. /**
  75. * If this is returned, then the error will be both pushed onto the stack
  76. * and logged.
  77. */
  78. define('HTML_PROGRESS_ERRORSTACK_PUSHANDLOG', 1);
  79. /**
  80. * If this is returned, then the error will only be pushed onto the stack,
  81. * and not logged.
  82. */
  83. define('HTML_PROGRESS_ERRORSTACK_PUSH', 2);
  84. /**
  85. * If this is returned, then the error will only be logged, but not pushed
  86. * onto the error stack.
  87. */
  88. define('HTML_PROGRESS_ERRORSTACK_LOG', 3);
  89. /**
  90. * If this is returned, then the error is completely ignored.
  91. */
  92. define('HTML_PROGRESS_ERRORSTACK_IGNORE', 4);
  93. /**
  94. * If this is returned, then the error will only be logged, but not pushed
  95. * onto the error stack because will halt script execution.
  96. */
  97. define('HTML_PROGRESS_ERRORSTACK_LOGANDDIE', 5);
  98. /**#@-*/
  99. /**#@+
  100. * Log types for PHP's native error_log() function
  101. *
  102. * @see HTML_Progress::_errorHandler
  103. * @var integer
  104. * @since 1.2.0
  105. */
  106. /**
  107. * Use PHP's system logger
  108. */
  109. define('HTML_PROGRESS_LOG_TYPE_SYSTEM', 0);
  110. /**
  111. * Use PHP's mail() function
  112. */
  113. define('HTML_PROGRESS_LOG_TYPE_MAIL', 1);
  114. /**
  115. * Append to a file
  116. */
  117. define('HTML_PROGRESS_LOG_TYPE_FILE', 3);
  118. /**#@-*/
  119. /**
  120. * Global error message callback.
  121. * This will be used to generate the error message
  122. * from the error code.
  123. *
  124. * @global false|string|array $GLOBALS['_HTML_PROGRESS_CALLBACK_MESSAGE']
  125. * @since 1.2.0
  126. * @access private
  127. * @see HTML_Progress::_initErrorHandler
  128. */
  129. $GLOBALS['_HTML_PROGRESS_CALLBACK_MESSAGE'] = false;
  130. /**
  131. * Global error context callback.
  132. * This will be used to generate the error context for an error.
  133. *
  134. * @global false|string|array $GLOBALS['_HTML_PROGRESS_CALLBACK_CONTEXT']
  135. * @since 1.2.0
  136. * @access private
  137. * @see HTML_Progress::_initErrorHandler
  138. */
  139. $GLOBALS['_HTML_PROGRESS_CALLBACK_CONTEXT'] = false;
  140. /**
  141. * Global error push callback.
  142. * This will be called every time an error is pushed onto the stack.
  143. * The return value will be used to determine whether to allow
  144. * an error to be pushed or logged.
  145. *
  146. * @global false|string|array $GLOBALS['_HTML_PROGRESS_CALLBACK_PUSH']
  147. * @since 1.2.0
  148. * @access private
  149. * @see HTML_Progress::_initErrorHandler
  150. */
  151. $GLOBALS['_HTML_PROGRESS_CALLBACK_PUSH'] = false;
  152. /**
  153. * Global error handler callback.
  154. * This will handle any errors raised by this package.
  155. *
  156. * @global false|string|array $GLOBALS['_HTML_PROGRESS_CALLBACK_ERRORHANDLER']
  157. * @since 1.2.0
  158. * @access private
  159. * @see HTML_Progress::_initErrorHandler
  160. */
  161. $GLOBALS['_HTML_PROGRESS_CALLBACK_ERRORHANDLER'] = false;
  162. /**
  163. * Global associative array of key-value pairs
  164. * that are used to specify any handler-specific settings.
  165. *
  166. * @global array $GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS']
  167. * @since 1.2.0
  168. * @access private
  169. * @see HTML_Progress::_initErrorHandler
  170. */
  171. $GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS'] = array();
  172. /**
  173. * Global error stack for this package.
  174. *
  175. * @global array $GLOBALS['_HTML_PROGRESS_ERRORSTACK']
  176. * @since 1.2.0
  177. * @access private
  178. * @see HTML_Progress::raiseError
  179. */
  180. $GLOBALS['_HTML_PROGRESS_ERRORSTACK'] = array();
  181. /**
  182. * The HTML_Progress class allow you to add a loading bar
  183. * to any of your xhtml document.
  184. * You should have a browser that accept DHTML feature.
  185. *
  186. * PHP versions 4 and 5
  187. *
  188. * LICENSE: This source file is subject to version 3.0 of the PHP license
  189. * that is available through the world-wide-web at the following URI:
  190. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  191. * the PHP License and are unable to obtain it through the web, please
  192. * send a note to license@php.net so we can mail you a copy immediately.
  193. *
  194. * @category HTML
  195. * @package HTML_Progress
  196. * @author Laurent Laville <pear@laurent-laville.org>
  197. * @copyright 1997-2005 The PHP Group
  198. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  199. * @version Release: 1.2.2
  200. * @link http://pear.php.net/package/HTML_Progress
  201. */
  202. class HTML_Progress
  203. {
  204. /**
  205. * Whether the progress bar is in determinate or indeterminate mode.
  206. * The default is false.
  207. * An indeterminate progress bar continuously displays animation indicating
  208. * that an operation of unknown length is occuring.
  209. *
  210. * @var boolean
  211. * @since 1.0
  212. * @access private
  213. * @see setIndeterminate(), isIndeterminate()
  214. */
  215. var $_indeterminate;
  216. /**
  217. * Whether to display a border around the progress bar.
  218. * The default is false.
  219. *
  220. * @var boolean
  221. * @since 1.0
  222. * @access private
  223. * @see setBorderPainted(), isBorderPainted()
  224. */
  225. var $_paintBorder;
  226. /**
  227. * Whether to textually display a string on the progress bar.
  228. * The default is false.
  229. * Setting this to true causes a textual display of the progress to be rendered
  230. * on the progress bar. If the $_progressString is null, the percentage of completion
  231. * is displayed on the progress bar. Otherwise, the $_progressString is rendered
  232. * on the progress bar.
  233. *
  234. * @var boolean
  235. * @since 1.0
  236. * @access private
  237. * @see setStringPainted(), isStringPainted()
  238. */
  239. var $_paintString;
  240. /**
  241. * An optional string that can be displayed on the progress bar.
  242. * The default is null.
  243. * Setting this to a non-null value does not imply that the string
  244. * will be displayed.
  245. *
  246. * @var string
  247. * @since 1.0
  248. * @access private
  249. * @see getString(), setString()
  250. */
  251. var $_progressString;
  252. /**
  253. * The data model (HTML_Progress_DM instance or extends)
  254. * handles any mathematical issues arising from assigning faulty values.
  255. *
  256. * @var object
  257. * @since 1.0
  258. * @access private
  259. * @see getDM(), setDM()
  260. */
  261. var $_DM;
  262. /**
  263. * The user interface (HTML_Progress_UI instance or extends)
  264. * handles look-and-feel of the progress bar.
  265. *
  266. * @var object
  267. * @since 1.0
  268. * @access private
  269. * @see getUI(), setUI()
  270. */
  271. var $_UI;
  272. /**
  273. * The label that uniquely identifies this progress object.
  274. *
  275. * @var string
  276. * @since 1.0
  277. * @access private
  278. * @see getIdent(), setIdent()
  279. */
  280. var $_ident;
  281. /**
  282. * Holds all HTML_Progress_Observer objects that wish to be notified of new messages.
  283. *
  284. * @var array
  285. * @since 1.0
  286. * @access private
  287. * @see getListeners(), addListener(), removeListener()
  288. */
  289. var $_listeners;
  290. /**
  291. * Delay in milisecond before each progress cells display.
  292. * 1000 ms === sleep(1)
  293. * <strong>usleep()</strong> function does not run on Windows platform.
  294. *
  295. * @var integer
  296. * @since 1.1
  297. * @access private
  298. * @see setAnimSpeed()
  299. */
  300. var $_anim_speed;
  301. /**
  302. * Callback, either function name or array(&$object, 'method')
  303. *
  304. * @var mixed
  305. * @since 1.2.0RC3
  306. * @access private
  307. * @see setProgressHandler()
  308. */
  309. var $_callback = null;
  310. /**
  311. * Constructor Summary
  312. *
  313. * o Creates a natural horizontal progress bar that displays ten cells/units
  314. * with no border and no progress string.
  315. * The initial and minimum values are 0, and the maximum is 100.
  316. * <code>
  317. * $bar = new HTML_Progress();
  318. * </code>
  319. *
  320. * o Creates a natural progress bar with the specified orientation, which can be
  321. * either HTML_PROGRESS_BAR_HORIZONTAL or HTML_PROGRESS_BAR_VERTICAL
  322. * By default, no border and no progress string are painted.
  323. * The initial and minimum values are 0, and the maximum is 100.
  324. * <code>
  325. * $bar = new HTML_Progress($orient);
  326. * </code>
  327. *
  328. * o Creates a natural horizontal progress bar with the specified minimum and
  329. * maximum. Sets the initial value of the progress bar to the specified
  330. * minimum, and the maximum that the progress bar can reach.
  331. * By default, no border and no progress string are painted.
  332. * <code>
  333. * $bar = new HTML_Progress($min, $max);
  334. * </code>
  335. *
  336. * o Creates a natural horizontal progress bar with the specified orientation,
  337. * minimum and maximum. Sets the initial value of the progress bar to the
  338. * specified minimum, and the maximum that the progress bar can reach.
  339. * By default, no border and no progress string are painted.
  340. * <code>
  341. * $bar = new HTML_Progress($orient, $min, $max);
  342. * </code>
  343. *
  344. * o Creates a natural horizontal progress that uses the specified model
  345. * to hold the progress bar's data.
  346. * By default, no border and no progress string are painted.
  347. * <code>
  348. * $bar = new HTML_Progress($model);
  349. * </code>
  350. *
  351. *
  352. * @param object $model (optional) Model that hold the progress bar's data
  353. * @param int $orient (optional) Orientation of progress bar
  354. * @param int $min (optional) Minimum value of progress bar
  355. * @param int $max (optional) Maximum value of progress bar
  356. * @param array $errorPrefs (optional) Always last argument of class constructor.
  357. * hash of params to configure PEAR_ErrorStack and loggers
  358. *
  359. * @since 1.0
  360. * @access public
  361. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  362. * @see setIndeterminate(),
  363. * setBorderPainted(), setStringPainted(), setString(),
  364. * setDM(), setUI(), setIdent()
  365. */
  366. function HTML_Progress()
  367. {
  368. $args = func_get_args();
  369. $num_args = func_num_args();
  370. if ($num_args > 0) {
  371. $errorPrefs = func_get_arg($num_args - 1);
  372. if (!is_array($errorPrefs)) {
  373. $errorPrefs = array();
  374. } else {
  375. $num_args--;
  376. }
  377. HTML_Progress::_initErrorHandler($errorPrefs);
  378. } else {
  379. HTML_Progress::_initErrorhandler();
  380. }
  381. $this->_listeners = array(); // none listeners by default
  382. $this->_DM = new HTML_Progress_DM(); // new instance of a progress DataModel
  383. $this->_UI = new HTML_Progress_UI(); // new instance of a progress UserInterface
  384. switch ($num_args) {
  385. case 1:
  386. if (is_object($args[0]) && (is_a($args[0], 'html_progress_dm'))) {
  387. /* object html_progress_dm extends */
  388. $this->_DM = &$args[0];
  389. } elseif (is_int($args[0])) {
  390. /* int orient */
  391. $this->_UI->setOrientation($args[0]);
  392. } else {
  393. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  394. array('var' => '$model | $orient',
  395. 'was' => (gettype($args[0]) == 'object') ?
  396. get_class($args[0]).' object' : gettype($args[0]),
  397. 'expected' => 'html_progress_dm object | integer',
  398. 'paramnum' => 1));
  399. }
  400. break;
  401. case 2:
  402. /* int min, int max */
  403. if (!is_int($args[0])) {
  404. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  405. array('var' => '$min',
  406. 'was' => $args[0],
  407. 'expected' => 'integer',
  408. 'paramnum' => 1));
  409. } elseif (!is_int($args[1])) {
  410. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  411. array('var' => '$max',
  412. 'was' => $args[1],
  413. 'expected' => 'integer',
  414. 'paramnum' => 2));
  415. } else {
  416. $this->_DM->setMinimum($args[0]);
  417. $this->_DM->setMaximum($args[1]);
  418. }
  419. break;
  420. case 3:
  421. /* int orient, int min, int max */
  422. if (!is_int($args[0])) {
  423. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  424. array('var' => '$orient',
  425. 'was' => $args[0],
  426. 'expected' => 'integer',
  427. 'paramnum' => 1));
  428. } elseif (!is_int($args[1])) {
  429. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  430. array('var' => '$min',
  431. 'was' => $args[1],
  432. 'expected' => 'integer',
  433. 'paramnum' => 2));
  434. } elseif (!is_int($args[2])) {
  435. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  436. array('var' => '$max',
  437. 'was' => $args[2],
  438. 'expected' => 'integer',
  439. 'paramnum' => 3));
  440. } else {
  441. $this->_UI->setOrientation($args[0]);
  442. $this->_DM->setMinimum($args[1]);
  443. $this->_DM->setMaximum($args[2]);
  444. }
  445. break;
  446. default:
  447. }
  448. $this->setString(null);
  449. $this->setStringPainted(false);
  450. $this->setBorderPainted(false);
  451. $this->setIndeterminate(false);
  452. $this->setIdent();
  453. $this->setAnimSpeed(0);
  454. // to fix a potential php config problem with PHP 4.2.0 : turn 'implicit_flush' ON
  455. ob_implicit_flush(1);
  456. }
  457. /**
  458. * Returns the current API version
  459. *
  460. * @return float
  461. * @since 0.1
  462. * @access public
  463. */
  464. function apiVersion()
  465. {
  466. return 1.2;
  467. }
  468. /**
  469. * Returns mode of the progress bar (determinate or not).
  470. *
  471. * @return boolean
  472. * @since 1.0
  473. * @access public
  474. * @see setIndeterminate()
  475. * @tutorial progress.isindeterminate.pkg
  476. */
  477. function isIndeterminate()
  478. {
  479. return $this->_indeterminate;
  480. }
  481. /**
  482. * Sets the $_indeterminate property of the progress bar, which determines
  483. * whether the progress bar is in determinate or indeterminate mode.
  484. * An indeterminate progress bar continuously displays animation indicating
  485. * that an operation of unknown length is occuring.
  486. * By default, this property is false.
  487. *
  488. * @param boolean $continuous whether countinuously displays animation
  489. *
  490. * @return void
  491. * @since 1.0
  492. * @access public
  493. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  494. * @see isIndeterminate()
  495. * @tutorial progress.setindeterminate.pkg
  496. */
  497. function setIndeterminate($continuous)
  498. {
  499. if (!is_bool($continuous)) {
  500. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  501. array('var' => '$continuous',
  502. 'was' => gettype($continuous),
  503. 'expected' => 'boolean',
  504. 'paramnum' => 1));
  505. }
  506. $this->_indeterminate = $continuous;
  507. }
  508. /**
  509. * Determines whether the progress bar border is painted or not.
  510. * The default is false.
  511. *
  512. * @return boolean
  513. * @since 1.0
  514. * @access public
  515. * @see setBorderPainted()
  516. * @tutorial progress.isborderpainted.pkg
  517. */
  518. function isBorderPainted()
  519. {
  520. return $this->_paintBorder;
  521. }
  522. /**
  523. * Sets the value of $_paintBorder property, which determines whether the
  524. * progress bar should paint its border. The default is false.
  525. *
  526. * @param boolean $paint whether the progress bar should paint its border
  527. *
  528. * @return void
  529. * @since 1.0
  530. * @access public
  531. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  532. * @see isBorderPainted()
  533. * @tutorial progress.setborderpainted.pkg
  534. */
  535. function setBorderPainted($paint)
  536. {
  537. if (!is_bool($paint)) {
  538. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  539. array('var' => '$paint',
  540. 'was' => gettype($paint),
  541. 'expected' => 'boolean',
  542. 'paramnum' => 1));
  543. }
  544. $this->_paintBorder = $paint;
  545. }
  546. /**
  547. * Determines whether the progress bar string is painted or not.
  548. * The default is false.
  549. * The progress bar displays the value returned by getPercentComplete() method
  550. * formatted as a percent such as 33%.
  551. *
  552. * @return boolean
  553. * @since 1.0
  554. * @access public
  555. * @see setStringPainted(), setString()
  556. * @tutorial progress.isstringpainted.pkg
  557. */
  558. function isStringPainted()
  559. {
  560. return $this->_paintString;
  561. }
  562. /**
  563. * Sets the value of $_paintString property, which determines whether the
  564. * progress bar should render a progress string. The default is false.
  565. *
  566. * @param boolean $paint whether the progress bar should render a string
  567. *
  568. * @return void
  569. * @since 1.0
  570. * @access public
  571. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  572. * @see isStringPainted(), setString()
  573. * @tutorial progress.setstringpainted.pkg
  574. */
  575. function setStringPainted($paint)
  576. {
  577. if (!is_bool($paint)) {
  578. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  579. array('var' => '$paint',
  580. 'was' => gettype($paint),
  581. 'expected' => 'boolean',
  582. 'paramnum' => 1));
  583. }
  584. $this->_paintString = $paint;
  585. }
  586. /**
  587. * Returns the current value of the progress string.
  588. * By default, the progress bar displays the value returned by
  589. * getPercentComplete() method formatted as a percent such as 33%.
  590. *
  591. * @return string
  592. * @since 1.0
  593. * @access public
  594. * @see setString(), isStringPainted()
  595. * @tutorial progress.getstring.pkg
  596. */
  597. function getString()
  598. {
  599. if ($this->isStringPainted() && !is_null($this->_progressString)) {
  600. return $this->_progressString;
  601. } else {
  602. return sprintf("%s", $this->getPercentComplete(false)).' %';
  603. }
  604. }
  605. /**
  606. * Sets the current value of the progress string. By default, this string
  607. * is null. If you have provided a custom progress string and want to revert
  608. * to the built-in-behavior, set the string back to null.
  609. * The progress string is painted only if the isStringPainted() method
  610. * returns true.
  611. *
  612. * @param string $str progress string
  613. *
  614. * @return void
  615. * @since 1.0
  616. * @access public
  617. * @see getString(), isStringPainted(), setStringPainted()
  618. * @tutorial progress.setstring.pkg
  619. */
  620. function setString($str)
  621. {
  622. $this->_progressString = $str;
  623. }
  624. /**
  625. * Returns the data model used by this progress bar.
  626. *
  627. * @return object
  628. * @since 1.0
  629. * @access public
  630. * @see setDM()
  631. */
  632. function &getDM()
  633. {
  634. return $this->_DM;
  635. }
  636. /**
  637. * Sets the data model used by this progress bar.
  638. *
  639. * @param string $model class name of a html_progress_dm extends object
  640. *
  641. * @return void
  642. * @since 1.0
  643. * @access public
  644. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  645. * @see getDM()
  646. */
  647. function setDM($model)
  648. {
  649. if (!class_exists($model)) {
  650. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  651. array('var' => '$model',
  652. 'was' => 'class does not exists',
  653. 'expected' => $model.' class defined',
  654. 'paramnum' => 1));
  655. }
  656. $_dm = new $model();
  657. if (!is_a($_dm, 'html_progress_dm')) {
  658. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  659. array('var' => '$model',
  660. 'was' => $model,
  661. 'expected' => 'HTML_Progress_DM extends',
  662. 'paramnum' => 1));
  663. }
  664. $this->_DM =& $_dm;
  665. }
  666. /**
  667. * Returns the progress bar's minimum value stored in the progress bar's data model.
  668. * The default value is 0.
  669. *
  670. * @return integer
  671. * @since 1.0
  672. * @access public
  673. * @see setMinimum(),
  674. * HTML_Progress_DM::getMinimum()
  675. * @tutorial dm.getminimum.pkg
  676. */
  677. function getMinimum()
  678. {
  679. return $this->_DM->getMinimum();
  680. }
  681. /**
  682. * Sets the progress bar's minimum value stored in the progress bar's data model.
  683. * If the minimum value is different from previous minimum, all change listeners
  684. * are notified.
  685. *
  686. * @param integer $min progress bar's minimal value
  687. *
  688. * @return void
  689. * @since 1.0
  690. * @access public
  691. * @see getMinimum(),
  692. * HTML_Progress_DM::setMinimum()
  693. * @tutorial dm.setminimum.pkg
  694. */
  695. function setMinimum($min)
  696. {
  697. $oldVal = $this->getMinimum();
  698. $this->_DM->setMinimum($min);
  699. if ($oldVal != $min) {
  700. $this->_announce(array('log' => 'setMinimum', 'value' => $min));
  701. }
  702. }
  703. /**
  704. * Returns the progress bar's maximum value stored in the progress bar's data model.
  705. * The default value is 100.
  706. *
  707. * @return integer
  708. * @since 1.0
  709. * @access public
  710. * @see setMaximum(),
  711. * HTML_Progress_DM::getMaximum()
  712. * @tutorial dm.getmaximum.pkg
  713. */
  714. function getMaximum()
  715. {
  716. return $this->_DM->getMaximum();
  717. }
  718. /**
  719. * Sets the progress bar's maximum value stored in the progress bar's data model.
  720. * If the maximum value is different from previous maximum, all change listeners
  721. * are notified.
  722. *
  723. * @param integer $max progress bar's maximal value
  724. *
  725. * @return void
  726. * @since 1.0
  727. * @access public
  728. * @see getMaximum(),
  729. * HTML_Progress_DM::setMaximum()
  730. * @tutorial dm.setmaximum.pkg
  731. */
  732. function setMaximum($max)
  733. {
  734. $oldVal = $this->getMaximum();
  735. $this->_DM->setMaximum($max);
  736. if ($oldVal != $max) {
  737. $this->_announce(array('log' => 'setMaximum', 'value' => $max));
  738. }
  739. }
  740. /**
  741. * Returns the progress bar's increment value stored in the progress bar's data model.
  742. * The default value is +1.
  743. *
  744. * @return integer
  745. * @since 1.0
  746. * @access public
  747. * @see setIncrement(),
  748. * HTML_Progress_DM::getIncrement()
  749. * @tutorial dm.getincrement.pkg
  750. */
  751. function getIncrement()
  752. {
  753. return $this->_DM->getIncrement();
  754. }
  755. /**
  756. * Sets the progress bar's increment value stored in the progress bar's data model.
  757. *
  758. * @param integer $inc progress bar's increment value
  759. *
  760. * @return void
  761. * @since 1.0
  762. * @access public
  763. * @see getIncrement(),
  764. * HTML_Progress_DM::setIncrement()
  765. * @tutorial dm.setincrement.pkg
  766. */
  767. function setIncrement($inc)
  768. {
  769. $this->_DM->setIncrement($inc);
  770. }
  771. /**
  772. * Returns the progress bar's current value, which is stored in the
  773. * progress bar's data model. The value is always between the minimum
  774. * and maximum values, inclusive.
  775. * By default, the value is initialized to be equal to the minimum value.
  776. *
  777. * @return integer
  778. * @since 1.0
  779. * @access public
  780. * @see setValue(), incValue(),
  781. * HTML_Progress_DM::getValue()
  782. * @tutorial dm.getvalue.pkg
  783. */
  784. function getValue()
  785. {
  786. return $this->_DM->getValue();
  787. }
  788. /**
  789. * Sets the progress bar's current value stored in the progress bar's data model.
  790. * If the new value is different from previous value, all change listeners
  791. * are notified.
  792. *
  793. * @param integer $val progress bar's current value
  794. *
  795. * @return void
  796. * @since 1.0
  797. * @access public
  798. * @see getValue(), incValue(),
  799. * HTML_Progress_DM::setValue()
  800. * @tutorial dm.setvalue.pkg
  801. */
  802. function setValue($val)
  803. {
  804. $oldVal = $this->getValue();
  805. $this->_DM->setValue($val);
  806. if ($oldVal != $val) {
  807. $this->_announce(array('log' => 'setValue', 'value' => $val));
  808. }
  809. }
  810. /**
  811. * Updates the progress bar's current value by adding increment value.
  812. * All change listeners are notified.
  813. *
  814. * @return void
  815. * @since 1.0
  816. * @access public
  817. * @see getValue(), setValue(),
  818. * HTML_Progress_DM::incValue()
  819. * @tutorial dm.incvalue.pkg
  820. */
  821. function incValue()
  822. {
  823. $this->_DM->incValue();
  824. $this->_announce(array('log' => 'incValue', 'value' => $this->_DM->getValue() ));
  825. }
  826. /**
  827. * Returns the percent complete for the progress bar. Note that this number is
  828. * between 0.00 and 1.00 or 0 and 100.
  829. *
  830. * @param boolean $float (optional) float or integer format
  831. *
  832. * @return mixed
  833. * @since 1.0
  834. * @access public
  835. * @see getValue(), getMaximum(),
  836. * HTML_Progress_DM::getPercentComplete()
  837. * @tutorial dm.getpercentcomplete.pkg
  838. */
  839. function getPercentComplete($float = true)
  840. {
  841. return $this->_DM->getPercentComplete($float);
  842. }
  843. /**
  844. * Returns the look-and-feel object that renders the progress bar.
  845. *
  846. * @return object
  847. * @since 1.0
  848. * @access public
  849. * @see setUI()
  850. */
  851. function &getUI()
  852. {
  853. return $this->_UI;
  854. }
  855. /**
  856. * Sets the look-and-feel object that renders the progress bar.
  857. *
  858. * @param string $ui class name of a html_progress_ui extends object
  859. *
  860. * @return void
  861. * @since 1.0
  862. * @access public
  863. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  864. * @see getUI()
  865. */
  866. function setUI($ui)
  867. {
  868. if (!class_exists($ui)) {
  869. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  870. array('var' => '$ui',
  871. 'was' => 'class does not exists',
  872. 'expected' => $ui.' class defined',
  873. 'paramnum' => 1));
  874. }
  875. $_ui = new $ui();
  876. if (!is_a($_ui, 'html_progress_ui')) {
  877. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  878. array('var' => '$ui',
  879. 'was' => $ui,
  880. 'expected' => 'HTML_Progress_UI extends',
  881. 'paramnum' => 1));
  882. }
  883. $this->_UI =& $_ui;
  884. }
  885. /**
  886. * Sets the look-and-feel model that renders the progress bar.
  887. *
  888. * @param string $file file name of model properties
  889. * @param string $type type of external ressource (phpArray, iniFile, XML ...)
  890. *
  891. * @return void
  892. * @since 1.0
  893. * @access public
  894. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  895. * @see setUI()
  896. * @tutorial progress.setmodel.pkg
  897. */
  898. function setModel($file, $type)
  899. {
  900. if (!file_exists($file)) {
  901. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  902. array('var' => '$file',
  903. 'was' => $file,
  904. 'expected' => 'file exists',
  905. 'paramnum' => 1));
  906. }
  907. include_once 'Config.php';
  908. $conf = new Config();
  909. if (!$conf->isConfigTypeRegistered($type)) {
  910. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  911. array('var' => '$type',
  912. 'was' => $type,
  913. 'expected' => implode (" | ", array_keys($GLOBALS['CONFIG_TYPES'])),
  914. 'paramnum' => 2));
  915. }
  916. $data = $conf->parseConfig($file, $type);
  917. $structure = $data->toArray(false);
  918. $progress =& $structure['root'];
  919. $ui = new HTML_Progress_UI();
  920. if (isset($progress['core']['speed'])) {
  921. $this->setAnimSpeed(intval($progress['core']['speed']));
  922. }
  923. if (isset($progress['core']['indeterminate'])) {
  924. $mode = (strtolower(trim($progress['core']['indeterminate'])) == 'true');
  925. $this->setIndeterminate($mode);
  926. }
  927. if (isset($progress['core']['increment'])) {
  928. $this->setIncrement(intval($progress['core']['increment']));
  929. }
  930. if (isset($progress['core']['javascript']) && file_exists($progress['core']['javascript'])) {
  931. $ui->setScript($progress['core']['javascript']);
  932. }
  933. if (isset($progress['orientation']['shape'])) {
  934. $ui->setOrientation(intval($progress['orientation']['shape']));
  935. }
  936. if (isset($progress['orientation']['fillway'])) {
  937. $ui->setFillWay($progress['orientation']['fillway']);
  938. }
  939. if (isset($progress['cell']['count'])) {
  940. $ui->setCellCount(intval($progress['cell']['count']));
  941. }
  942. if (isset($progress['cell']['font-family'])) {
  943. if (is_array($progress['cell']['font-family'])) {
  944. $progress['cell']['font-family'] = implode(",", $progress['cell']['font-family']);
  945. }
  946. }
  947. if (isset($progress['cell'])) {
  948. $ui->setCellAttributes($progress['cell']);
  949. }
  950. if (isset($progress['border'])) {
  951. $this->setBorderPainted(true);
  952. $ui->setBorderAttributes($progress['border']);
  953. }
  954. if (isset($progress['string']['font-family'])) {
  955. if (is_array($progress['string']['font-family'])) {
  956. $progress['string']['font-family'] = implode(",", $progress['string']['font-family']);
  957. }
  958. }
  959. if (isset($progress['string'])) {
  960. $this->setStringPainted(true);
  961. $ui->setStringAttributes($progress['string']);
  962. }
  963. if (isset($progress['progress'])) {
  964. $ui->setProgressAttributes($progress['progress']);
  965. }
  966. $this->_UI = $ui;
  967. }
  968. /**
  969. * Returns delay execution of the progress bar
  970. *
  971. * @return integer
  972. * @since 1.2.0RC1
  973. * @access public
  974. * @see setAnimSpeed()
  975. * @tutorial progress.getanimspeed.pkg
  976. */
  977. function getAnimSpeed()
  978. {
  979. return $this->_anim_speed;
  980. }
  981. /**
  982. * Set the delays progress bar execution for the given number of miliseconds.
  983. *
  984. * @param integer $delay Delay in milisecond.
  985. *
  986. * @return void
  987. * @since 1.1
  988. * @access public
  989. * @throws HTML_PROGRESS_ERROR_INVALID_INPUT
  990. * @see getAnimSpeed()
  991. * @tutorial progress.setanimspeed.pkg
  992. */
  993. function setAnimSpeed($delay)
  994. {
  995. if (!is_int($delay)) {
  996. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception',
  997. array('var' => '$delay',
  998. 'was' => gettype($delay),
  999. 'expected' => 'integer',
  1000. 'paramnum' => 1));
  1001. } elseif ($delay < 0) {
  1002. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  1003. array('var' => '$delay',
  1004. 'was' => $delay,
  1005. 'expected' => 'greater than zero',
  1006. 'paramnum' => 1));
  1007. } elseif ($delay > 1000) {
  1008. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_INPUT, 'error',
  1009. array('var' => '$delay',
  1010. 'was' => $delay,
  1011. 'expected' => 'less or equal 1000',
  1012. 'paramnum' => 1));
  1013. }
  1014. $this->_anim_speed = $delay;
  1015. }
  1016. /**
  1017. * Get the cascading style sheet to put inline on HTML document
  1018. *
  1019. * @return string
  1020. * @since 1.0
  1021. * @access public
  1022. * @see HTML_Progress_UI::getStyle()
  1023. * @tutorial ui.getstyle.pkg
  1024. */
  1025. function getStyle()
  1026. {
  1027. $ui = $this->getUI();
  1028. $lnEnd = $ui->_getLineEnd();
  1029. $style = $lnEnd . $ui->getStyle();
  1030. $style = str_replace('{%pIdent%}', '.'.$this->getIdent(), $style);
  1031. return $style;
  1032. }
  1033. /**
  1034. * Get the javascript code to manage progress bar.
  1035. *
  1036. * @return string JavaScript URL or inline code to manage progress bar
  1037. * @since 1.0
  1038. * @access public
  1039. * @see HTML_Progress_UI::getScript()
  1040. * @tutorial ui.getscript.pkg
  1041. */
  1042. function getScript()
  1043. {
  1044. $ui = $this->getUI();
  1045. $js =& $ui->getScript();
  1046. return $js;
  1047. }
  1048. /**
  1049. * Returns the progress bar structure in an array.
  1050. *
  1051. * @return array of progress bar properties
  1052. * @since 1.0
  1053. * @access public
  1054. */
  1055. function toArray()
  1056. {
  1057. $ui =& $this->getUI();
  1058. $dm =& $this->getDM();
  1059. $_structure = array();
  1060. $_structure['id'] = $this->getIdent();
  1061. $_structure['indeterminate'] = $this->isIndeterminate();
  1062. $_structure['borderpainted'] = $this->isBorderPainted();
  1063. $_structure['stringpainted'] = $this->isStringPainted();
  1064. $_structure['string'] = $this->_progressString;
  1065. $_structure['animspeed'] = $this->getAnimSpeed();
  1066. $_structure['ui']['classID'] = get_class($ui);
  1067. $_structure['ui']['orientation'] = $ui->getOrientation();
  1068. $_structure['ui']['fillway'] = $ui->getFillWay();
  1069. $_structure['ui']['cell'] = $ui->getCellAttributes();
  1070. $_structure['ui']['cell']['count'] = $ui->getCellCount();
  1071. $_structure['ui']['border'] = $ui->getBorderAttributes();
  1072. $_structure['ui']['string'] = $ui->getStringAttributes();
  1073. $_structure['ui']['progress'] = $ui->getProgressAttributes();
  1074. $_structure['ui']['script'] = $ui->getScript();
  1075. $_structure['dm']['classID'] = get_class($dm);
  1076. $_structure['dm']['minimum'] = $dm->getMinimum();
  1077. $_structure['dm']['maximum'] = $dm->getMaximum();
  1078. $_structure['dm']['increment'] = $dm->getIncrement();
  1079. $_structure['dm']['value'] = $dm->getValue();
  1080. $_structure['dm']['percent'] = $dm->getPercentComplete(false);
  1081. return $_structure;
  1082. }
  1083. /**
  1084. * Returns the progress structure as HTML.
  1085. *
  1086. * @return string HTML Progress bar
  1087. * @since 0.2
  1088. * @access public
  1089. */
  1090. function toHtml()
  1091. {
  1092. $strHtml = '';
  1093. $ui =& $this->_UI;
  1094. $tabs = $ui->_getTabs();
  1095. $tab = $ui->_getTab();
  1096. $lnEnd = $ui->_getLineEnd();
  1097. $comment = $ui->getComment();
  1098. $orient = $ui->getOrientation();
  1099. $progressAttr = $ui->getProgressAttributes();
  1100. $borderAttr = $ui->getBorderAttributes();
  1101. $stringAttr = $ui->getStringAttributes();
  1102. $valign = strtolower($stringAttr['valign']);
  1103. /**
  1104. * Adds a progress bar legend in html code is possible.
  1105. * See HTML_Common::setComment() method.
  1106. */
  1107. if (strlen($comment) > 0) {
  1108. $strHtml .= $tabs . "<!-- $comment -->" . $lnEnd;
  1109. }
  1110. $strHtml .= $tabs . "<div id=\"".$this->getIdent()."_progress\" class=\"".$this->getIdent()."\">" . $lnEnd;
  1111. $strHtml .= $tabs . "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" . $lnEnd;
  1112. $progressId = $this->getIdent().'_';
  1113. /**
  1114. * Creates all cells of progress bar in order
  1115. * depending of the FillWay and Orientation.
  1116. */
  1117. if ($orient == HTML_PROGRESS_BAR_HORIZONTAL) {
  1118. $progressHtml = $this->_getProgressHbar_toHtml();
  1119. }
  1120. if ($orient == HTML_PROGRESS_BAR_VERTICAL) {
  1121. $progressHtml = $this->_getProgressVbar_toHtml();
  1122. }
  1123. if ($orient == HTML_PROGRESS_POLYGONAL) {
  1124. $progressHtml = $this->_getProgressPolygonal_toHtml();
  1125. }
  1126. if ($orient == HTML_PROGRESS_CIRCLE) {
  1127. $cellAttr = $ui->getCellAttributes();
  1128. if (!isset($cellAttr[0]['background-image']) || !file_exists($cellAttr[0]['background-image'])) {
  1129. // creates default circle segments pictures :
  1130. // 'c0.png'->0% 'c1.png'->10%, 'c2.png'->20%, ... 'c10.png'->100%
  1131. $ui->drawCircleSegments();
  1132. }
  1133. $progressHtml = $this->_getProgressCircle_toHtml();
  1134. }
  1135. /**
  1136. * Progress Bar (2) alignment rules:
  1137. * - percent / messsage area (1)
  1138. *
  1139. * +---------------------------------------+
  1140. * | +t1---+ |
  1141. * | | (1) | |
  1142. * | +-----+ |
  1143. * | +t2---+ +-------------------+ +t3---+ |
  1144. * | | (1) | | | | | (2) | | | | | | (1) | |
  1145. * | +-----+ +-------------------+ +-----+ |
  1146. * | +t4---+ |
  1147. * | | (1) | |
  1148. * | +-----+ |
  1149. * +---------------------------------------+
  1150. */
  1151. if (($valign == 'left') || ($valign == 'right')) {
  1152. $tRows = 1;
  1153. $tCols = 2;
  1154. $ps = ($valign == 'left') ? 0 : 1;
  1155. } else {
  1156. $tRows = 2;
  1157. $tCols = 1;
  1158. $ps = ($valign == 'top') ? 0 : 1;
  1159. }
  1160. for ($r = 0 ; $r < $tRows ; $r++) {
  1161. $strHtml .= $tabs . "<tr>" . $lnEnd;
  1162. for ($c = 0 ; $c < $tCols ; $c++) {
  1163. if (($c == $ps) || ($r == $ps)) {
  1164. $id = $stringAttr['id'];
  1165. $strHtml .= $tabs . $tab . "<td class=\"$id\" id=\"$progressId$id\">" . $lnEnd;
  1166. $strHtml .= $tabs . $tab . $tab . $this->getString() . $lnEnd;
  1167. $ps = -1;
  1168. } else {
  1169. $class = $progressAttr['class'];
  1170. $strHtml .= $tabs . $tab ."<td class=\"$class\">" . $lnEnd;
  1171. $strHtml .= $tabs . $tab . $tab . "<div class=\"".$borderAttr['class']."\">" . $lnEnd;
  1172. $strHtml .= $progressHtml;
  1173. $strHtml .= $tabs . $tab . $tab . "</div>" . $lnEnd;
  1174. }
  1175. $strHtml .= $tabs . $tab ."</td>" . $lnEnd;
  1176. }
  1177. $strHtml .= $tabs . "</tr>" . $lnEnd;
  1178. }
  1179. $strHtml .= $tabs . "</table>" . $lnEnd;
  1180. $strHtml .= $tabs . "</div>" . $lnEnd;
  1181. return $strHtml;
  1182. }
  1183. /**
  1184. * Renders the new value of progress bar.
  1185. *
  1186. * @return void
  1187. * @since 0.2
  1188. * @access public
  1189. */
  1190. function display()
  1191. {
  1192. static $lnEnd;
  1193. static $cellAmount;
  1194. static $determinate;
  1195. if(!isset($lnEnd)) {
  1196. $ui =& $this->_UI;
  1197. $lnEnd = $ui->_getLineEnd();
  1198. $cellAmount = ($this->getMaximum() - $this->getMinimum()) / $ui->getCellCount();
  1199. }
  1200. if (function_exists('ob_get_clean')) {
  1201. $bar = ob_get_clean(); // use for PHP 4.3+
  1202. } else {
  1203. $bar = ob_get_contents(); // use for PHP 4.2+
  1204. ob_end_clean();
  1205. }
  1206. $bar .= $lnEnd;
  1207. $progressId = $this->getIdent().'_';
  1208. if ($this->isIndeterminate()) {
  1209. if (isset($determinate)) {
  1210. $determinate++;
  1211. $progress = $determinate;
  1212. } else {
  1213. $progress = $determinate = 1;
  1214. }
  1215. } else {
  1216. $progress = ($this->getValue() - $this->getMinimum()) / $cellAmount;
  1217. $determinate = 0;
  1218. }
  1219. $bar .= '<script type="text/javascript">self.setprogress("'.$progressId.'",'.((int) $progress).',"'.$this->getString().'",'.$determinate.'); </script>';
  1220. echo $bar;
  1221. ob_start();
  1222. }
  1223. /**
  1224. * Hides the progress bar.
  1225. *
  1226. * @return void
  1227. * @since 1.2.0RC3
  1228. * @access public
  1229. */
  1230. function hide()
  1231. {
  1232. $ui = $this->getUI();
  1233. $lnEnd = $ui->_getLineEnd();
  1234. $progressId = $this->getIdent().'_';
  1235. if (function_exists('ob_get_clean')) {
  1236. $bar = ob_get_clean(); // use for PHP 4.3+
  1237. } else {
  1238. $bar = ob_get_contents(); // use for PHP 4.2+
  1239. ob_end_clean();
  1240. }
  1241. $bar .= $lnEnd;
  1242. $bar .= '<script type="text/javascript">self.hideProgress("'.$progressId.'"); </script>';
  1243. echo $bar;
  1244. }
  1245. /**
  1246. * Default user callback when none are defined.
  1247. * Delay execution of progress meter for the given number of milliseconds.
  1248. *
  1249. * NOTE: The function {@link http://www.php.net/manual/en/function.usleep.php}
  1250. * did not work on Windows systems until PHP 5.0.0
  1251. *
  1252. * @return void
  1253. * @since 1.2.0RC3
  1254. * @access public
  1255. * @see getAnimSpeed(), setAnimSpeed(), process()
  1256. */
  1257. function sleep()
  1258. {
  1259. // convert delay from milliseconds to microseconds
  1260. $usecs = $this->getAnimSpeed()*1000;
  1261. if ((substr(PHP_OS, 0, 3) == 'WIN') && (substr(PHP_VERSION,0,1) < '5') ){
  1262. for ($i=0; $i<$usecs; $i++) { }
  1263. } else {
  1264. usleep($usecs);
  1265. }
  1266. }
  1267. /**
  1268. * Sets the user callback function that execute all actions pending progress
  1269. *
  1270. * @param mixed $handler Name of function or a class-method.
  1271. *
  1272. * @return void
  1273. * @since 1.2.0RC3
  1274. * @access public
  1275. * @throws HTML_PROGRESS_ERROR_INVALID_CALLBACK
  1276. * @see process()
  1277. */
  1278. function setProgressHandler($handler)
  1279. {
  1280. if (!is_callable($handler)) {
  1281. return HTML_Progress::raiseError(HTML_PROGRESS_ERROR_INVALID_CALLBACK, 'warning',
  1282. array('var' => '$handler',
  1283. 'element' => 'valid Class-Method/Function',
  1284. 'was' => 'element',
  1285. 'paramnum' => 1));
  1286. }
  1287. $this->_callback = $handler;
  1288. }
  1289. /**
  1290. * Performs the progress actions
  1291. *
  1292. * @return void
  1293. * @since 1.2.0RC3
  1294. * @access public
  1295. * @see sleep()
  1296. */
  1297. function process()
  1298. {
  1299. if (!$this->_callbackExists($this->_callback)) {
  1300. // when there is no valid user callback then default is to sleep a bit ...
  1301. $this->sleep();
  1302. } else {
  1303. call_user_func_array($this->_callback, array($this->getValue(), &$this));
  1304. }
  1305. }
  1306. /**
  1307. * Runs the progress bar (both modes: indeterminate and determinate),
  1308. * and execute all actions defined in user callback identified by
  1309. * method setProgressHandler.
  1310. *
  1311. * @return void
  1312. * @since 1.2.0RC3
  1313. * @access public
  1314. * @see process(), setProgressHandler()
  1315. */
  1316. function run()
  1317. {
  1318. do {
  1319. $this->display();
  1320. $this->process();
  1321. if ($this->getPercentComplete() == 1) {
  1322. if ($this->isIndeterminate()) {
  1323. $this->setValue(0);
  1324. } else {
  1325. return;
  1326. }
  1327. }
  1328. $this->incValue();
  1329. } while(1);
  1330. }
  1331. /**
  1332. * Returns the current identification string.
  1333. *
  1334. * @return string current Progress instance's identification string
  1335. * @since 1.0
  1336. * @access public
  1337. * @see setIdent()
  1338. */
  1339. function getIdent()
  1340. {
  1341. return $this->_ident;
  1342. }
  1343. /**
  1344. * Sets this Progress instance's identification string.
  1345. *
  1346. * @param mixed $ident (optional) the new identification string.
  1347. *
  1348. * @since 1.0
  1349. * @access public
  1350. * @see getIdent()
  1351. */
  1352. function setIdent($ident = null)
  1353. {
  1354. if (is_null($ident)) {
  1355. $this->_ident = 'p_' . substr(md5(microtime()), 0, 6);
  1356. } else {
  1357. $this->_ident = $ident;
  1358. }
  1359. }
  1360. /**
  1361. * Returns an array of all the listeners added to this progress bar.
  1362. *
  1363. * @return array
  1364. * @since 1.0
  1365. * @access public
  1366. * @see addListener(), removeListener()
  1367. * @tutorial progress.getlisteners.pkg
  1368. */
  1369. function getListeners()
  1370. {
  1371. return $this->_listeners;
  1372. }
  1373. /**
  1374. * Adds a HTML_Progress_Observer instance to the list of observers
  1375. * that are listening for messages emitted by this HTML_Progress instance.
  1376. *
  1377. * @param object $observer The HTML_Progress_Observer instance
  1378. * to attach as a listener.
  1379. *
  1380. * @return boolean True if the observer is successfully attached.
  1381. * @since 1.0
  1382. * @access public
  1383. * @see getListeners(), removeListener()
  1384. * @tutorial progress.addlistener.pkg
  1385. */
  1386. function addListener($observer)
  1387. {
  1388. if (!is_a($observer, 'HTML_Progress_Observer') &&
  1389. !is_a($observer, 'HTML_Progress_Monitor')) {
  1390. return false;
  1391. }
  1392. $this->_listeners[$observer->_id] = &$observer;
  1393. return true;
  1394. }
  1395. /**
  1396. * Removes a HTML_Progress_Observer instance from the list of observers.
  1397. *
  1398. * @param object $observer The HTML_Progress_Observer instance
  1399. * to detach from the list of listeners.
  1400. *
  1401. * @return boolean True if the observer is successfully detached.
  1402. * @since 1.0
  1403. * @access public
  1404. * @see getListeners(), addListener()
  1405. * @tutorial progress.removelistener.pkg
  1406. */
  1407. function removeListener($observer)
  1408. {
  1409. if ((!is_a($observer, 'HTML_Progress_Observer') &&
  1410. !is_a($observer, 'HTML_Progress_Monitor')
  1411. ) ||
  1412. (!isset($this->_listeners[$observer->_id])) ) {
  1413. return false;
  1414. }
  1415. unset($this->_listeners[$observer->_id]);
  1416. return true;
  1417. }
  1418. /**
  1419. * Notifies all listeners that have registered interest in $event message.
  1420. *
  1421. * @param mixed $event A hash describing the progress event.
  1422. *
  1423. * @since 1.0
  1424. * @access private
  1425. * @see setMinimum(), setMaximum(), setValue(), incValue()
  1426. */
  1427. function _announce($event)
  1428. {
  1429. foreach ($this->_listeners as $id => $listener) {
  1430. $this->_listeners[$id]->notify($event);
  1431. }
  1432. }
  1433. /**
  1434. * Returns a horizontal progress bar structure as HTML.
  1435. *
  1436. * @return string Horizontal HTML Progress bar
  1437. * @since 1.0
  1438. * @access private
  1439. */
  1440. function _getProgressHbar_toHtml()
  1441. {
  1442. $ui =& $this->_UI;
  1443. $tabs = $ui->_getTabs();
  1444. $tab = $ui->_getTab();
  1445. $lnEnd = $ui->_getLineEnd();
  1446. $way_natural = ($ui->getFillWay() == 'natural');
  1447. $cellAttr = $ui->getCellAttributes();
  1448. $cellCount = $ui->getCellCount();
  1449. $progressId = $this->getIdent().'_';
  1450. $progressHtml = "";
  1451. if ($way_natural) {
  1452. // inactive cells first
  1453. $pos = $cellAttr['spacing'];
  1454. for ($i=0; $i<$cellCount; $i++) {
  1455. $progressHtml .= $tabs . $tab . $tab;
  1456. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1457. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1458. $progressHtml .= " style=\"position:absolute;top:".$cellAttr['spacing']."px;left:".$pos."px;\"";
  1459. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1460. $pos += ($cellAttr['width'] + $cellAttr['spacing']);
  1461. }
  1462. // then active cells
  1463. $pos = $cellAttr['spacing'];
  1464. for ($i=0; $i<$cellCount; $i++) {
  1465. $progressHtml .= $tabs . $tab . $tab;
  1466. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1467. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1468. $progressHtml .= " style=\"position:absolute;top:".$cellAttr['spacing']."px;left:".$pos."px;";
  1469. if (isset($cellAttr[$i])) {
  1470. $progressHtml .= "color:".$cellAttr[$i]['color'].";\"";
  1471. } else {
  1472. $progressHtml .= "\"";
  1473. }
  1474. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1475. $pos += ($cellAttr['width'] + $cellAttr['spacing']);
  1476. }
  1477. } else {
  1478. // inactive cells first
  1479. $pos = $cellAttr['spacing'];
  1480. for ($i=$cellCount-1; $i>=0; $i--) {
  1481. $progressHtml .= $tabs . $tab . $tab;
  1482. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1483. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1484. $progressHtml .= " style=\"position:absolute;top:".$cellAttr['spacing']."px;left:".$pos."px;\"";
  1485. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1486. $pos += ($cellAttr['width'] + $cellAttr['spacing']);
  1487. }
  1488. // then active cells
  1489. $pos = $cellAttr['spacing'];
  1490. for ($i=$cellCount-1; $i>=0; $i--) {
  1491. $progressHtml .= $tabs . $tab . $tab;
  1492. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1493. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1494. $progressHtml .= " style=\"position:absolute;top:".$cellAttr['spacing']."px;left:".$pos."px;";
  1495. if (isset($cellAttr[$i])) {
  1496. $progressHtml .= "color:".$cellAttr[$i]['color'].";\"";
  1497. } else {
  1498. $progressHtml .= "\"";
  1499. }
  1500. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1501. $pos += ($cellAttr['width'] + $cellAttr['spacing']);
  1502. }
  1503. }
  1504. return $progressHtml;
  1505. }
  1506. /**
  1507. * Returns a vertical progress bar structure as HTML.
  1508. *
  1509. * @return string Vertical HTML Progress bar
  1510. * @since 1.0
  1511. * @access private
  1512. */
  1513. function _getProgressVbar_toHtml()
  1514. {
  1515. $ui =& $this->_UI;
  1516. $tabs = $ui->_getTabs();
  1517. $tab = $ui->_getTab();
  1518. $lnEnd = $ui->_getLineEnd();
  1519. $way_natural = ($ui->getFillWay() == 'natural');
  1520. $cellAttr = $ui->getCellAttributes();
  1521. $cellCount = $ui->getCellCount();
  1522. $progressId = $this->getIdent().'_';
  1523. $progressHtml = "";
  1524. if ($way_natural) {
  1525. // inactive cells first
  1526. $pos = $cellAttr['spacing'];
  1527. for ($i=$cellCount-1; $i>=0; $i--) {
  1528. $progressHtml .= $tabs . $tab . $tab;
  1529. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1530. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1531. $progressHtml .= " style=\"position:absolute;left:".$cellAttr['spacing']."px;top:".$pos."px;\"";
  1532. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1533. $pos += ($cellAttr['height'] + $cellAttr['spacing']);
  1534. }
  1535. // then active cells
  1536. $pos = $cellAttr['spacing'];
  1537. for ($i=$cellCount-1; $i>=0; $i--) {
  1538. $progressHtml .= $tabs . $tab . $tab;
  1539. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1540. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1541. $progressHtml .= " style=\"position:absolute;left:".$cellAttr['spacing']."px;top:".$pos."px;";
  1542. if (isset($cellAttr[$i])) {
  1543. $progressHtml .= "color:".$cellAttr[$i]['color'].";\"";
  1544. } else {
  1545. $progressHtml .= "\"";
  1546. }
  1547. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1548. $pos += ($cellAttr['height'] + $cellAttr['spacing']);
  1549. }
  1550. } else {
  1551. // inactive cells first
  1552. $pos = $cellAttr['spacing'];
  1553. for ($i=0; $i<$cellCount; $i++) {
  1554. $progressHtml .= $tabs . $tab . $tab;
  1555. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1556. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1557. $progressHtml .= " style=\"position:absolute;left:".$cellAttr['spacing']."px;top:".$pos."px;\"";
  1558. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1559. $pos += ($cellAttr['height'] + $cellAttr['spacing']);
  1560. }
  1561. // then active cells
  1562. $pos = $cellAttr['spacing'];
  1563. for ($i=0; $i<$cellCount; $i++) {
  1564. $progressHtml .= $tabs . $tab . $tab;
  1565. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1566. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1567. $progressHtml .= " style=\"position:absolute;left:".$cellAttr['spacing']."px;top:".$pos."px;";
  1568. if (isset($cellAttr[$i])) {
  1569. $progressHtml .= "color:".$cellAttr[$i]['color'].";\"";
  1570. } else {
  1571. $progressHtml .= "\"";
  1572. }
  1573. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1574. $pos += ($cellAttr['height'] + $cellAttr['spacing']);
  1575. }
  1576. }
  1577. return $progressHtml;
  1578. }
  1579. /**
  1580. * Returns a polygonal progress structure as HTML.
  1581. *
  1582. * @return string Polygonal HTML Progress
  1583. * @since 1.2.0RC1
  1584. * @access private
  1585. */
  1586. function _getProgressPolygonal_toHtml()
  1587. {
  1588. $ui =& $this->_UI;
  1589. $tabs = $ui->_getTabs();
  1590. $tab = $ui->_getTab();
  1591. $lnEnd = $ui->_getLineEnd();
  1592. $way_natural = ($ui->getFillWay() == 'natural');
  1593. $cellAttr = $ui->getCellAttributes();
  1594. $cellCount = $ui->getCellCount();
  1595. $coord = $ui->_coordinates;
  1596. $progressId = $this->getIdent().'_';
  1597. $progressHtml = "";
  1598. if ($way_natural) {
  1599. // inactive cells first
  1600. for ($i=0; $i<$cellCount; $i++) {
  1601. $top = $coord[$i][0] * $cellAttr['width'];
  1602. $left = $coord[$i][1] * $cellAttr['height'];
  1603. $progressHtml .= $tabs . $tab . $tab;
  1604. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1605. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1606. $progressHtml .= " style=\"position:absolute;top:".$top."px;left:".$left."px;\"";
  1607. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1608. }
  1609. // then active cells
  1610. for ($i=0; $i<$cellCount; $i++) {
  1611. $top = $coord[$i][0] * $cellAttr['width'];
  1612. $left = $coord[$i][1] * $cellAttr['height'];
  1613. $progressHtml .= $tabs . $tab . $tab;
  1614. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1615. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1616. $progressHtml .= " style=\"position:absolute;top:".$top."px;left:".$left."px;\"";
  1617. if (isset($cellAttr[$i])) {
  1618. $progressHtml .= "color:".$cellAttr[$i]['color'].";\"";
  1619. } else {
  1620. $progressHtml .= "\"";
  1621. }
  1622. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1623. }
  1624. } else {
  1625. $c = count($coord) - 1;
  1626. // inactive cells first
  1627. for ($i=0; $i<$cellCount; $i++) {
  1628. $top = $coord[$c-$i][0] * $cellAttr['width'];
  1629. $left = $coord[$c-$i][1] * $cellAttr['height'];
  1630. $progressHtml .= $tabs . $tab . $tab;
  1631. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1632. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1633. $progressHtml .= " style=\"position:absolute;top:".$top."px;left:".$left."px;\"";
  1634. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1635. }
  1636. // then active cells
  1637. for ($i=0; $i<$cellCount; $i++) {
  1638. $top = $coord[$c-$i][0] * $cellAttr['width'];
  1639. $left = $coord[$c-$i][1] * $cellAttr['height'];
  1640. $progressHtml .= $tabs . $tab . $tab;
  1641. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1642. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1643. $progressHtml .= " style=\"position:absolute;top:".$top."px;left:".$left."px;\"";
  1644. if (isset($cellAttr[$i])) {
  1645. $progressHtml .= "color:".$cellAttr[$i]['color'].";\"";
  1646. } else {
  1647. $progressHtml .= "\"";
  1648. }
  1649. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1650. }
  1651. }
  1652. return $progressHtml;
  1653. }
  1654. /**
  1655. * Returns a circle progress structure as HTML.
  1656. *
  1657. * @return string Circle HTML Progress
  1658. * @since 1.2.0RC1
  1659. * @access private
  1660. */
  1661. function _getProgressCircle_toHtml()
  1662. {
  1663. $ui =& $this->_UI;
  1664. $tabs = $ui->_getTabs();
  1665. $tab = $ui->_getTab();
  1666. $lnEnd = $ui->_getLineEnd();
  1667. $way_natural = ($ui->getFillWay() == 'natural');
  1668. $cellAttr = $ui->getCellAttributes();
  1669. $cellCount = $ui->getCellCount();
  1670. $progressId = $this->getIdent().'_';
  1671. $progressHtml = "";
  1672. if ($way_natural) {
  1673. // inactive cells first
  1674. for ($i=0; $i<$cellCount; $i++) {
  1675. $progressHtml .= $tabs . $tab . $tab;
  1676. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1677. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1678. $progressHtml .= " style=\"position:absolute;top:0;left:0;\"";
  1679. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1680. }
  1681. // then active cells
  1682. for ($i=0; $i<$cellCount; $i++) {
  1683. $progressHtml .= $tabs . $tab . $tab;
  1684. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1685. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1686. $progressHtml .= " style=\"position:absolute;top:0;left:0;\"";
  1687. $progressHtml .= "><img src=\"".$cellAttr[$i+1]['background-image']."\" border=\"0\" /></div>" . $lnEnd;
  1688. }
  1689. } else {
  1690. // inactive cells first
  1691. for ($i=0; $i<$cellCount; $i++) {
  1692. $progressHtml .= $tabs . $tab . $tab;
  1693. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."I\"";
  1694. $progressHtml .= " class=\"".$cellAttr['class']."I\"";
  1695. $progressHtml .= " style=\"position:absolute;top:0;left:0;\"";
  1696. $progressHtml .= ">&nbsp;</div>" . $lnEnd;
  1697. }
  1698. // then active cells
  1699. for ($i=0; $i<$cellCount; $i++) {
  1700. $progressHtml .= $tabs . $tab . $tab;
  1701. $progressHtml .= "<div id=\"". $progressId . sprintf($cellAttr['id'],$i) ."A\"";
  1702. $progressHtml .= " class=\"".$cellAttr['class']."A\"";
  1703. $progressHtml .= " style=\"position:absolute;top:0;left:0;\"";
  1704. $progressHtml .= "><img src=\"".$cellAttr[$i+1]['background-image']."\" border=\"0\" /></div>" . $lnEnd;
  1705. }
  1706. }
  1707. return $progressHtml;
  1708. }
  1709. /**
  1710. * Checks for callback function existance
  1711. *
  1712. * @param mixed $callback a callback, like one used by call_user_func()
  1713. *
  1714. * @return boolean
  1715. * @since 1.2.0RC3
  1716. * @access private
  1717. */
  1718. function _callbackExists($callback)
  1719. {
  1720. if (is_string($callback)) {
  1721. return function_exists($callback);
  1722. } elseif (is_array($callback) && is_object($callback[0])) {
  1723. return method_exists($callback[0], $callback[1]);
  1724. } else {
  1725. return false;
  1726. }
  1727. }
  1728. /**
  1729. * Initialize Error Handler
  1730. *
  1731. * Parameter '$prefs' contains a hash of options to define the error handler.
  1732. * You may find :
  1733. * 'message_callback' A callback to generate message body.
  1734. * Default is: HTML_Progress::_msgCallback()
  1735. * 'context_callback' A callback to generate context of error.
  1736. * Default is: HTML_Progress::_getBacktrace()
  1737. * 'push_callback' A callback to determine whether to allow an error
  1738. * to be pushed or logged.
  1739. * Default is: HTML_Progress::_handleError()
  1740. * 'error_handler' A callback to manage all error raised.
  1741. * Default is: HTML_Progress::_errorHandler()
  1742. * 'handler' Hash of params to configure all handlers (display, file, mail ...)
  1743. * There are only a display handler by default with options below:
  1744. * <code>
  1745. * array('display' => array('conf' => $options));
  1746. * // where $options are:
  1747. * $options = array(
  1748. * 'lineFormat' => '<b>%1$s</b>: %2$s %3$s',
  1749. * 'contextFormat' => ' in <b>%3$s</b> (file <b>%1$s</b> at line <b>%2$s</b>)'
  1750. * );
  1751. * </code>
  1752. *
  1753. * @param array $prefs hash of params to configure error handler
  1754. *
  1755. * @return void
  1756. * @since 1.2.0
  1757. * @access private
  1758. * @static
  1759. */
  1760. function _initErrorHandler($prefs = array())
  1761. {
  1762. // error message mapping callback
  1763. if (isset($prefs['message_callback']) && is_callable($prefs['message_callback'])) {
  1764. $GLOBALS['_HTML_PROGRESS_CALLBACK_MESSAGE'] = $prefs['message_callback'];
  1765. } else {
  1766. $GLOBALS['_HTML_PROGRESS_CALLBACK_MESSAGE'] = array('HTML_Progress', '_msgCallback');
  1767. }
  1768. // error context mapping callback
  1769. if (isset($prefs['context_callback']) && is_callable($prefs['context_callback'])) {
  1770. $GLOBALS['_HTML_PROGRESS_CALLBACK_CONTEXT'] = $prefs['context_callback'];
  1771. } else {
  1772. $GLOBALS['_HTML_PROGRESS_CALLBACK_CONTEXT'] = array('HTML_Progress', '_getBacktrace');
  1773. }
  1774. // determine whether to allow an error to be pushed or logged
  1775. if (isset($prefs['push_callback']) && is_callable($prefs['push_callback'])) {
  1776. $GLOBALS['_HTML_PROGRESS_CALLBACK_PUSH'] = $prefs['push_callback'];
  1777. } else {
  1778. $GLOBALS['_HTML_PROGRESS_CALLBACK_PUSH'] = array('HTML_Progress', '_handleError');
  1779. }
  1780. // default error handler will use PEAR_Error
  1781. if (isset($prefs['error_handler']) && is_callable($prefs['error_handler'])) {
  1782. $GLOBALS['_HTML_PROGRESS_CALLBACK_ERRORHANDLER'] = $prefs['error_handler'];
  1783. } else {
  1784. $GLOBALS['_HTML_PROGRESS_CALLBACK_ERRORHANDLER'] = array('HTML_Progress', '_errorHandler');
  1785. }
  1786. // only a display handler is set by default with specific settings
  1787. $conf = array('lineFormat' => '<b>%1$s</b>: %2$s %3$s',
  1788. 'contextFormat' => ' in <b>%3$s</b> (file <b>%1$s</b> at line <b>%2$s</b>)'
  1789. );
  1790. $optionsHandler['display'] = array('conf' => $conf);
  1791. if (isset($prefs['handler'])) {
  1792. $optionsHandler = array_merge($optionsHandler, $prefs['handler']);
  1793. }
  1794. $GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS'] = $optionsHandler;
  1795. }
  1796. /**
  1797. * Default callback to generate error messages for any instance
  1798. *
  1799. * @param array $err current error structure with context info
  1800. *
  1801. * @return string
  1802. * @since 1.2.0RC1
  1803. * @access private
  1804. * @static
  1805. */
  1806. function _msgCallback($err)
  1807. {
  1808. $messages = HTML_Progress::_getErrorMessage();
  1809. $mainmsg = $messages[$err['code']];
  1810. if (count($err['params'])) {
  1811. foreach ($err['params'] as $name => $val) {
  1812. if (is_array($val)) {
  1813. $val = implode(', ', $val);
  1814. }
  1815. $mainmsg = str_replace('%' . $name . '%', $val, $mainmsg);
  1816. }
  1817. }
  1818. return $mainmsg;
  1819. }
  1820. /**
  1821. * Standard file/line number/function/class context callback
  1822. *
  1823. * @return false|array
  1824. * @since 1.2.0
  1825. * @access private
  1826. * @static
  1827. */
  1828. function _getBacktrace()
  1829. {
  1830. if (function_exists('debug_backtrace')) {
  1831. $backtrace = debug_backtrace(); // PHP 4.3+
  1832. $backtrace = $backtrace[count($backtrace)-1];
  1833. } else {
  1834. $backtrace = false; // PHP 4.1.x, 4.2.x (no context info available)
  1835. }
  1836. return $backtrace;
  1837. }
  1838. /**
  1839. * Standard callback, this will be called every time an error
  1840. * is pushed onto the stack. The return value will be used to determine
  1841. * whether to allow an error to be pushed or logged.
  1842. * Dies if the error is an exception (and would have died anyway)
  1843. *
  1844. * @param array $err current error structure with context info
  1845. *
  1846. * @return null|HTML_PROGRESS_ERRORSTACK_* constant
  1847. * @since 1.2.0RC2
  1848. * @access private
  1849. * @static
  1850. * @see HTML_PROGRESS_ERRORSTACK_PUSHANDLOG, HTML_PROGRESS_ERRORSTACK_PUSH,
  1851. * HTML_PROGRESS_ERRORSTACK_LOG, HTML_PROGRESS_ERRORSTACK_IGNORE,
  1852. * HTML_PROGRESS_ERRORSTACK_LOGANDDIE
  1853. *
  1854. */
  1855. function _handleError($err)
  1856. {
  1857. if ($err['level'] == 'exception') {
  1858. return HTML_PROGRESS_ERRORSTACK_LOGANDDIE;
  1859. }
  1860. }
  1861. /**
  1862. * Standard error handler that will use PEAR_Error object
  1863. *
  1864. * To improve performances, the PEAR.php file is included dynamically.
  1865. * The file is so included only when an error is triggered. So, in most
  1866. * cases, the file isn't included and perfs are much better.
  1867. *
  1868. * @param array $err current error structure with context info
  1869. *
  1870. * @return PEAR_Error
  1871. * @since 1.2.0
  1872. * @access private
  1873. * @static
  1874. */
  1875. function _errorHandler($err)
  1876. {
  1877. include_once 'PEAR.php';
  1878. $e = PEAR::raiseError($err['message'], $err['code'], PEAR_ERROR_RETURN, E_USER_ERROR,
  1879. $err['context']);
  1880. if (isset($err['context'])) {
  1881. $file = $err['context']['file'];
  1882. $line = $err['context']['line'];
  1883. $func = $err['context']['class'];
  1884. $func .= $err['context']['type'];
  1885. $func .= $err['context']['function'];
  1886. }
  1887. $display_errors = ini_get('display_errors');
  1888. $log_errors = ini_get('log_errors');
  1889. $display = $GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS']['display'];
  1890. if ($display_errors) {
  1891. $lineFormat = $display['conf']['lineFormat'];
  1892. $contextFormat = $display['conf']['contextFormat'];
  1893. $context = sprintf($contextFormat, $file, $line, $func);
  1894. printf($lineFormat."<br />\n", ucfirst($err['level']), $err['message'], $context);
  1895. }
  1896. if ($log_errors) {
  1897. if (isset($GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS']['error_log'])) {
  1898. $error_log = $GLOBALS['_HTML_PROGRESS_ERRORHANDLER_OPTIONS']['error_log'];
  1899. $message_type = $error_log['name'];
  1900. $destination = '';
  1901. $extra_headers = '';
  1902. $send = true;
  1903. switch ($message_type) {
  1904. case HTML_PROGRESS_LOG_TYPE_SYSTEM:
  1905. break;
  1906. case HTML_PROGRESS_LOG_TYPE_MAIL:
  1907. $destination = $error_log['conf']['destination'];
  1908. $extra_headers = $error_log['conf']['extra_headers'];
  1909. break;
  1910. case HTML_PROGRESS_LOG_TYPE_FILE:
  1911. $destination = $error_log['conf']['destination'];
  1912. break;
  1913. default:
  1914. $send = false;
  1915. }
  1916. if ($send) {
  1917. /**
  1918. * String containing the format of a log line.
  1919. * Position arguments are:
  1920. * $1 -> timestamp
  1921. * $2 -> ident
  1922. * $3 -> level
  1923. * $4 -> message
  1924. * $5 -> context
  1925. */
  1926. if (isset($error_log['conf']['lineFormat'])) {
  1927. $lineFormat = $error_log['conf']['lineFormat'];
  1928. } else {
  1929. $lineFormat = '%1$s %2$s [%3$s] %4$s %5$s';
  1930. }
  1931. /**
  1932. * String containing the timestamp format
  1933. */
  1934. if (isset($error_log['conf']['timeFormat'])) {
  1935. $timeFormat = $error_log['conf']['timeFormat'];
  1936. } else {
  1937. $timeFormat = '%b %d %H:%M:%S';
  1938. }
  1939. /**
  1940. * String containing the error execution context format
  1941. */
  1942. if (isset($error_log['conf']['contextFormat'])) {
  1943. $contextFormat = $error_log['conf']['contextFormat'];
  1944. } else {
  1945. $contextFormat = strip_tags($display['conf']['contextFormat']);
  1946. }
  1947. /**
  1948. * String containing the end-on-line character sequence
  1949. */
  1950. if (isset($error_log['conf']['eol'])) {
  1951. $eol = $error_log['conf']['eol'];
  1952. } else {
  1953. $eol = "\n";
  1954. }
  1955. $context = sprintf($contextFormat, $file, $line, $func);
  1956. $message = sprintf($lineFormat,
  1957. strftime($timeFormat, $err['time']),
  1958. $error_log['ident'],
  1959. $err['level'],
  1960. $err['message'],
  1961. $context);
  1962. error_log($message.$eol, $message_type, $destination, $extra_headers);
  1963. }
  1964. }
  1965. }
  1966. return $e;
  1967. }
  1968. /**
  1969. * Error Message Template array
  1970. *
  1971. * @return string
  1972. * @since 1.0
  1973. * @access private
  1974. */
  1975. function _getErrorMessage()
  1976. {
  1977. $messages = array(
  1978. HTML_PROGRESS_ERROR_INVALID_INPUT =>
  1979. 'invalid input, parameter #%paramnum% '
  1980. . '"%var%" was expecting '
  1981. . '"%expected%", instead got "%was%"',
  1982. HTML_PROGRESS_ERROR_INVALID_CALLBACK =>
  1983. 'invalid callback, parameter #%paramnum% '
  1984. . '"%var%" expecting %element%,'
  1985. . ' instead got "%was%" does not exists',
  1986. HTML_PROGRESS_DEPRECATED =>
  1987. 'method is deprecated '
  1988. . 'use %newmethod% instead of %oldmethod%'
  1989. );
  1990. return $messages;
  1991. }
  1992. /**
  1993. * Add an error to the stack
  1994. *
  1995. * @param integer $code Error code.
  1996. * @param string $level The error level of the message.
  1997. * @param array $params Associative array of error parameters
  1998. *
  1999. * @return NULL|PEAR_Error PEAR_Error instance,
  2000. * with context info if PHP 4.3.0+
  2001. * @since 1.2.0RC1
  2002. * @access public
  2003. * @static
  2004. * @see hasErrors(), getError()
  2005. */
  2006. function raiseError($code, $level, $params)
  2007. {
  2008. // obey at protocol
  2009. if (error_reporting() == 0) {
  2010. return;
  2011. }
  2012. // grab error context
  2013. $context = call_user_func($GLOBALS['_HTML_PROGRESS_CALLBACK_CONTEXT']);
  2014. // save error
  2015. $time = explode(' ', microtime());
  2016. $time = $time[1] + $time[0];
  2017. $err = array(
  2018. 'code' => $code,
  2019. 'params' => $params,
  2020. 'package' => 'HTML_Progress',
  2021. 'level' => $level,
  2022. 'time' => $time,
  2023. 'context' => $context
  2024. );
  2025. // set up the error message, if necessary
  2026. $err['message'] = call_user_func($GLOBALS['_HTML_PROGRESS_CALLBACK_MESSAGE'], $err);
  2027. $push = $log = true;
  2028. $die = false;
  2029. $action = call_user_func($GLOBALS['_HTML_PROGRESS_CALLBACK_PUSH'], $err);
  2030. switch($action){
  2031. case HTML_PROGRESS_ERRORSTACK_IGNORE:
  2032. $push = $log = false;
  2033. break;
  2034. case HTML_PROGRESS_ERRORSTACK_PUSH:
  2035. $log = false;
  2036. break;
  2037. case HTML_PROGRESS_ERRORSTACK_LOG:
  2038. $push = false;
  2039. break;
  2040. case HTML_PROGRESS_ERRORSTACK_LOGANDDIE:
  2041. $push = false;
  2042. $die = true;
  2043. break;
  2044. // anything else returned has the same effect as pushandlog
  2045. }
  2046. $e = false;
  2047. if ($push) {
  2048. array_unshift($GLOBALS['_HTML_PROGRESS_ERRORSTACK'], $err);
  2049. }
  2050. if ($log) {
  2051. // default callback returns a PEAR_Error object
  2052. $e = call_user_func($GLOBALS['_HTML_PROGRESS_CALLBACK_ERRORHANDLER'], $err);
  2053. }
  2054. if ($die) {
  2055. die();
  2056. }
  2057. return $e;
  2058. }
  2059. /**
  2060. * Determine whether there are errors into the HTML_Progress stack
  2061. *
  2062. * @return integer
  2063. * @since 1.2.0RC3
  2064. * @access public
  2065. * @static
  2066. * @see getError(), raiseError()
  2067. */
  2068. function hasErrors()
  2069. {
  2070. return count($GLOBALS['_HTML_PROGRESS_ERRORSTACK']);
  2071. }
  2072. /**
  2073. * Pop an error off of the HTML_Progress stack
  2074. *
  2075. * @return false|array
  2076. * @since 1.2.0RC3
  2077. * @access public
  2078. * @static
  2079. * @see hasErrors(), raiseError()
  2080. */
  2081. function getError()
  2082. {
  2083. return @array_shift($GLOBALS['_HTML_PROGRESS_ERRORSTACK']);
  2084. }
  2085. }
  2086. ?>