/** * The progress bar's UI extended model class constructor * * @param string $file file name of model properties * @param string $type type of external ressource (phpArray, iniFile, XML ...) * * @since 1.0 * @access public * @throws HTML_PROGRESS_ERROR_INVALID_INPUT */ function HTML_Progress_Model($file, $type) { $this->_package = 'HTML_Progress_Model'; Error_Raise::initialize($this->_package, array('HTML_Progress', '_getErrorMessage')); if (!file_exists($file)) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'error', array('var' => '$file', 'was' => $file, 'expected' => 'file exists', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } $conf = new Config(); if (!$conf->isConfigTypeRegistered($type)) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'error', array('var' => '$type', 'was' => $type, 'expected' => implode(" | ", array_keys($GLOBALS['CONFIG_TYPES'])), 'paramnum' => 2), PEAR_ERROR_TRIGGER); } $data = $conf->parseConfig($file, $type); $structure = $data->toArray(false); $this->_progress =& $structure['root']; if (is_array($this->_progress['cell']['font-family'])) { $this->_progress['cell']['font-family'] = implode(",", $this->_progress['cell']['font-family']); } if (is_array($this->_progress['string']['font-family'])) { $this->_progress['string']['font-family'] = implode(",", $this->_progress['string']['font-family']); } $this->_orientation = $this->_progress['orientation']['shape']; $this->_fillWay = $this->_progress['orientation']['fillway']; if (isset($this->_progress['script']['file'])) { $this->_script = $this->_progress['script']['file']; } else { $this->_script = null; } if (isset($this->_progress['cell']['count'])) { $this->_cellCount = $this->_progress['cell']['count']; } else { $this->_cellCount = 10; } $this->_updateProgressSize(); }
/** * Sets the progress bar's current value. * If the new value is different from previous value, all change listeners * are notified. * * @param integer $val progress bar's current value * * @return void * @since 1.0 * @access public * @throws HTML_PROGRESS_ERROR_INVALID_INPUT * @see getValue() */ function setValue($val) { if (!is_int($val)) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$val', 'was' => gettype($val), 'expected' => 'integer', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } elseif ($val < $this->getMinimum()) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'error', array('var' => '$val', 'was' => $val, 'expected' => 'greater than $min = ' . $this->getMinimum(), 'paramnum' => 1), PEAR_ERROR_TRIGGER); } elseif ($val > $this->getMaximum()) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'error', array('var' => '$val', 'was' => $val, 'expected' => 'less than $max = ' . $this->getMaximum(), 'paramnum' => 1), PEAR_ERROR_TRIGGER); } $this->_value = $val; }
/** * Disables certain buttons for a page. * * Buttons [ = events] : back, next, cancel, reset, apply, help * * @param object $page Page where you want to activate buttons * @param array $events (optional) List of buttons * * @since 1.1 * @access public * @throws HTML_PROGRESS_ERROR_INVALID_INPUT * @see enableButton() */ function disableButton(&$page, $events = array()) { if (!is_a($page, 'HTML_QuickForm_Page')) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$page', 'was' => gettype($page), 'expected' => 'HTML_QuickForm_Page object', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } elseif (!is_array($events)) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$events', 'was' => gettype($events), 'expected' => 'array', 'paramnum' => 2), PEAR_ERROR_TRIGGER); } static $all; if (!isset($all)) { $all = array('back', 'next', 'cancel', 'reset', 'apply', 'help'); } $buttons = count($events) == 0 ? $all : $events; foreach ($buttons as $event) { $group =& $page->getElement('buttons'); $elements =& $group->getElements(); foreach (array_keys($elements) as $key) { if ($group->getElementName($key) == $page->getButtonName($event)) { $elements[$key]->updateAttributes(array('disabled' => 'true')); } } } }
/** * Re-assign an error to this error's package * * If an error is returned that needs to be re-defined for another package, * this method should be called from the pre-existing error in order to * transform it into another package's error * @param string package name of new error object * @param error|warning|notice|exception error severity * @param integer error code * @param array|null|false any new information for the error message, * otherwise existing information will be used. If passed false, * the new information will be set to null * @throws ERROR_RAISE_ERROR_INVALID_INPUT */ function &rePackageError($package, $type, $code, $extrainfo = null) { if (!is_string($type)) { return Error_Raise::exception('error_raise', ERROR_RAISE_ERROR_INVALID_INPUT, array('was' => gettype($type), 'expected' => 'string', 'param' => '$type', 'paramnum' => 2)); } $type = strtolower($type); if (!in_array($type, array('error', 'warning', 'notice', 'exception'))) { return Error_Raise::exception('error_raise', ERROR_RAISE_ERROR_INVALID_INPUT, array('was' => $type, 'expected' => array('error', 'warning', 'notice', 'exception'), 'param' => '$type', 'paramnum' => 2)); } if (!is_string($package)) { return Error_Raise::exception('error_raise', ERROR_RAISE_ERROR_INVALID_INPUT, array('was' => gettype($package), 'expected' => 'string', 'param' => '$package', 'paramnum' => 1)); } if (!class_exists($errorclass = $package . '_error')) { $errorclass = 'error_raise_error'; } $backtrace = false; if (function_exists('debug_backtrace')) { $backtrace = debug_backtrace(); } $e = new $errorclass($package, $type, $code, $extrainfo, $this->mode, $this->callback, $backtrace, $this); return $e; }
/** * Get an error message for Error_Util errors * @return string error message from error code * @param integer * @param array * @static */ function genErrorMessage($code, $args = array(), $state = ERROR_RAISE_TEXT) { if (!is_array($args)) { return 'Error: $args passed to Error_Util::genErrorMessage is ' . 'not an array but a ' . gettype($args); } $messages = array(ERROR_UTIL_ERROR_CLASS_DOESNT_EXIST => 'class "%cl%" does not exist', ERROR_UTIL_ERROR_INVALID_INPUT => 'invalid input, parameter #%paramnum% ' . '"%var%" was expecting ' . '"%expected%", instead got "%was%"', ERROR_UTIL_ERROR_METHOD_DOESNT_EXIST => 'method "%method%" doesn\'t exist in class "%class%"', ERROR_UTIL_ERROR_FUNCTION_DOESNT_EXIST => 'function "%function%" doesn\'t exist', ERROR_UTIL_ERROR_INTERNAL_FUNCTION => 'function "%function%" is an internal function, and ' . 'cannot be used as a callback'); if (is_int($code) && isset($messages[$code])) { $msg = $messages[$code]; return Error_Raise::sprintfErrorMessageWithState($msg, $args, $state); } else { return 'Error: code ' . $code . ' not found'; } }
/** * Error message generator for package HTML_Progress * * @param integer $code * @param array $args * @param integer $state * * @return string * @since 1.0 * @access private */ function _getErrorMessage($code, $args, $state) { $messages = array(HTML_PROGRESS_ERROR_INVALID_INPUT => 'invalid input, parameter #%paramnum% ' . '"%var%" was expecting ' . '"%expected%", instead got "%was%"', HTML_PROGRESS_ERROR_INVALID_CALLBACK => 'invalid callback, parameter #%paramnum% ' . '"%var%" expecting %element%,' . ' instead got "%was%" does not exists'); if (isset($messages[$code])) { $message = $messages[$code]; } else { $message = 'Code ' . $code . ' is not a valid error code'; } return Error_Raise::sprintfErrorMessageWithState($message, $args, $state); }
/** * Get an Error_Raise error message from a code * * This is the error message generator for the Error_Raise package, and * should not be used by other packages. It can be used as an example * of one way to generate an error message * @access private * @return string error message from error code * @param integer * @param array * @static */ function _getErrorMessage($code, $args = array(), $state = ERROR_RAISE_TEXT) { if (!is_array($args)) { return 'Error: $args passed to Error_Raise::_getErrorMessage is ' . 'not an array but a ' . gettype($args); } $messages = array(ERROR_RAISE_ERROR_CLASS_DOESNT_EXIST => 'class "%cl%" does not exist', ERROR_RAISE_ERROR_CODENOMSG => 'package "%package%" has no registered error message ' . 'generator', ERROR_RAISE_ERROR_ALREADY_INITIALIZED => 'package "%package%" has already been initialized', ERROR_RAISE_ERROR_INVALID_INPUT => 'invalid input, parameter #%paramnum% ' . '"%var%" was expecting ' . '"%expected%", instead got "%was%"', ERROR_RAISE_ERROR_INVALID_ERROR_CLASS => 'error class "%class%" in package ' . '"%package%," error type ' . '"%type%" does not descend from ' . 'Error_Raise_Error. Use ' . 'PEAR::raiseError() for normal PEAR_Error classes', ERROR_RAISE_ERROR_INVALID_ERRMSGGEN => 'Invalid callback passed to setErrorMsgGenerator() for package' . ' %package%', ERROR_RAISE_ERROR_INVALID_CONTEXT_GRABBER => 'An invalid callback was passed as a context grabber for ' . 'package %package%', ERROR_RAISE_ERROR_INVALID_CONTEXTGRABBER_RETURN => 'The context grabber for package %package% did not return an ' . 'array, but instead returned a %type%'); if (is_int($code) && isset($messages[$code])) { $msg = $messages[$code]; return Error_Raise::sprintfErrorMessageWithState($msg, $args, $state); } else { return 'Error: code ' . $code . ' not found'; } }
/** * Set the external JavaScript code (file) to manage progress element. * * @param string $url URL to the linked Progress JavaScript * * @return void * @since 1.0 * @access public * @throws HTML_PROGRESS_ERROR_INVALID_INPUT * @see getScript() */ function setScript($url) { if (!is_null($url)) { if (!is_string($url)) { Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$url', 'was' => gettype($url), 'expected' => 'string', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } elseif (!is_file($url) || $url == '.' || $url == '..') { Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'error', array('var' => '$url', 'was' => $url . ' file does not exists', 'expected' => 'javascript file exists', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } } /* - since version 0.5.0, - default javascript code comes from getScript() method - but may be overrided by external file. */ $this->_script = $url; }
/** * Uploads the files asynchronously, so the class can perform other operations * while files are being uploaded, such : * display a progress bar in indeterminate mode. * * @param string $dest Changes from current to the specified directory. * @param boolean $overwrite (optional) overwrite existing files. * * @return mixed a null array if all files transfered * @since 1.1 * @access public * @see FTP_Upload::setFiles() */ function moveTo($dest, $overwrite = false) { if (!is_string($dest)) { Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$dest', 'was' => gettype($dest), 'expected' => 'string', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } elseif (!is_bool($overwrite)) { Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$overwrite', 'was' => gettype($overwrite), 'expected' => 'boolean', 'paramnum' => 2), PEAR_ERROR_TRIGGER); } $dir = parent::_changeDir($dest); if (PEAR::isError($dir)) { return $dir; } $remoteFiles = ftp_nlist($this->_conn, '.'); if ($remoteFiles === false) { return PEAR::raiseError('Couldn\'t read directory ' . $dest); } $nomove = array(); // files not transfered on remote host foreach ($this->_files as $file) { if (!$overwrite && in_array(basename($file), $remoteFiles)) { // file already exists, skip to next one continue; } // writes file caption $status = ob_get_clean(); $status = '<script type="text/javascript">self.setStatus(\''; $status .= sprintf($this->captionMask, basename($file)); $status .= '\'); </script>'; echo $status; ob_start(); $ret = ftp_nb_put($this->_conn, basename($file), $file, FTP_BINARY); while ($ret == FTP_MOREDATA) { $this->_progress->display(); // sleep a bit ... for ($i = 0; $i < $this->_progress->_anim_speed * 1000; $i++) { } if ($this->_progress->getPercentComplete() == 1) { $this->_progress->setValue(0); } else { $this->_progress->incValue(); } // upload Continue ... $ret = ftp_nb_continue($this->_conn); } if ($ret != FTP_FINISHED) { $nomove[] = $file; } } return $nomove; }
/** * Display a caption on action in progress. * * The idea of a simple utility function for replacing variables * with values in an message template, come from sprintfErrorMessage * function of Error_Raise package by Greg Beaver. * * This simple str_replace-based function can be used to have an * order-independent sprintf, so messages can be passed in * with different grammar ordering, or other possibilities without * changing the source code. * * Variables should simply be surrounded by % as in %varname% * * @param string $caption (optional) message template * @param array $args (optional) associative array of * template var -> message text * @since 1.1 * @access public */ function setCaption($caption = ' ', $args = array()) { if (!is_string($caption)) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$caption', 'was' => gettype($caption), 'expected' => 'string', 'paramnum' => 1), PEAR_ERROR_TRIGGER); } elseif (!is_array($args)) { return Error_Raise::raise($this->_package, HTML_PROGRESS_ERROR_INVALID_INPUT, 'exception', array('var' => '$args', 'was' => gettype($args), 'expected' => 'array', 'paramnum' => 2), PEAR_ERROR_TRIGGER); } foreach ($args as $name => $value) { $caption = str_replace("%{$name}%", $value, $caption); } if (function_exists('ob_get_clean')) { $status = ob_get_clean(); // use for PHP 4.3+ } else { $status = ob_get_contents(); // use for PHP 4.2+ ob_end_clean(); } $status = '<script type="text/javascript">self.setStatus(\'' . $caption . '\'); </script>'; echo $status; ob_start(); }