Ignore:
Timestamp:
Dec 14, 2005 7:07:43 AM (18 years ago)
Author:
scdev
Message:

updated messaging functionality in Upload::

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Upload.inc.php

    r36 r37  
    88 * @author  Quinn Comendant <quinn@strangecode.com>
    99 * @requires App.inc.php
    10  * @version 1.2
     10 * @version 1.3
    1111 */
     12
     13// Message Types.
     14define('UPLOAD_MSG_ERR', MSG_ERR);
     15define('UPLOAD_MSG_ERROR', MSG_ERROR);
     16define('UPLOAD_MSG_WARNING', MSG_WARNING);
     17define('UPLOAD_MSG_NOTICE', MSG_NOTICE);
     18define('UPLOAD_MSG_SUCCESS', MSG_SUCCESS);
     19define('UPLOAD_MSG_ALL', MSG_SUCCESS + MSG_NOTICE + MSG_WARNING + MSG_ERROR);
    1220
    1321require_once dirname(__FILE__) . '/App.inc.php';
     
    1826    var $_params = array(
    1927   
    20         // Display message with raiseMsg?
    21         'display_messages' => true,
     28        // Which messages do we pass to raiseMsg?
     29        'display_messages' => UPLOAD_MSG_ALL,
    2230       
    2331        // Existing files will be overwritten when there is a name conflict?
     
    267275
    268276    /**
    269      *
     277     * Process uploaded files. Processes files existing within the specified $_FILES['form_name'] array.
     278     * It tests for errors, cleans the filename, optionally sets custom file names. It will process
     279     * multiple files automatically if the file form element is an array (<input type="file" name="myfiles[]" />).
     280     *
     281     * @access  public
     282     * @param   string  $form_name          The name of the form to process.
     283     * @param   string  $custom_file_name   The new name of the file. does not work if processing multiple files.
     284     * @return  mixed   Returns FALSE if a major error occured preventing any file uploads.
     285     *                  Returns an empty array if any minor errors occured or no files were found.
     286     *                  Returns a multidimentional array of filenames, sizes and extentions, if one-or-more files succeeded uploading.
     287     *                  Note: this last option presents a problem in the case of when some files uploaded successfully, and some failed.
     288     *                        In this case it is necessary to check the Upload::anyErrors method to discover if any did fail.
    270289     */
    271290    function process($form_name, $custom_file_name=null)
     
    448467   
    449468    /**
    450      *
     469     * Remove file within upload path.
     470     *
     471     * @access  public
     472     * @param   string  $file_name  A name of a file.
     473     * @return  bool                Success of operation.
    451474     */
    452475    function deleteFile($file_name)
     
    466489            App::logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_DEBUG, __FILE__, __LINE__);
    467490        } else {
    468             $this->raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERROR, __FILE__, __LINE__);
     491            $this->raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERR, __FILE__, __LINE__);
    469492            App::logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
    470493            return false;
     
    473496   
    474497    /**
    475      *
     498     * Renames a file within the upload path.
     499     *
     500     * @access  public
     501     * @param   string  $old_name   The currently existing file name.
     502     * @param   string  $new_name   The new name for this file.
     503     * @return  bool                Success of operation.
    476504     */
    477505    function moveFile($old_name, $new_name)
     
    490518                App::logMsg(sprintf('File renamed from %s to %s', $old_file_path_name, $new_file_path_name), LOG_DEBUG, __FILE__, __LINE__);
    491519            } else {
    492                 $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__);
    493                 App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__);
     520                $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_WARNING, __FILE__, __LINE__);
     521                App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_WARNING, __FILE__, __LINE__);
    494522                return false;
    495523            }
    496524        } else {
    497             $this->raiseMsg(sprintf(_("Couldn't rename nonexistent file <strong>%s</strong>."), $old_name), MSG_ERR, __FILE__, __LINE__);
    498             App::logMsg(sprintf(_("Error renaming nonexistent file: %s"), $old_file_path_name), LOG_ERR, __FILE__, __LINE__);
     525            $this->raiseMsg(sprintf(_("Couldn't rename nonexistent file <strong>%s</strong>."), $old_name), MSG_WARNING, __FILE__, __LINE__);
     526            App::logMsg(sprintf(_("Error renaming nonexistent file: %s"), $old_file_path_name), LOG_WARNING, __FILE__, __LINE__);
    499527            return false;
    500528        }
     
    502530   
    503531    /**
    504      *
     532     * Tests if a file exists within the current upload_path.
     533     *
     534     * @access  public
     535     * @param   string  $file_name  A name of a file.
     536     * @return  bool                Existence of file.
    505537     */
    506538    function exists($file_name)
     
    536568
    537569    /**
    538      *
     570     * Returns an array of file names that failed uploading.
     571     *
     572     * @access  public
     573     * @return  array   List of file names.
    539574     */
    540575    function getErrors()
     
    544579
    545580    /**
    546      *
     581     * Determintes if any errors occured while calling the Upload::process method.
     582     *
     583     * @access  public
    547584     */
    548585    function anyErrors()
     
    552589
    553590    /**
    554      *
     591     * Removes unsafe characters from file name.
     592     *
     593     * @access  public
     594     * @param   string  $file_name  A name of a file.
     595     * @return  string              The same name, but cleaned.
    555596     */
    556597    function cleanFileName($file_name)
     
    565606    }
    566607
    567     /**
    568      *
     608
     609    /**
     610     * Returns the extention of a file name, or an empty string if non exists.
     611     *
     612     * @access  public
     613     * @param   string  $file_name  A name of a file, with extention after a dot.
     614     * @return  string              The value found after the dot
    569615     */
    570616    function getFilenameExtension($file_name)
    571617    {
    572         preg_match('/.*?\.(\w+)$/i', $file_name, $ext);
     618        preg_match('/.*?\.(\w+)$/i', trim($file_name), $ext);
    573619        return isset($ext[1]) ? $ext[1] : '';
    574620    }
    575621   
    576622    /**
    577      * An alias for App::raiseMsg that only sends messages if display_messages is true.
     623     * An alias for App::raiseMsg that only sends messages configured by display_messages.
    578624     *
    579625     * @access public
     
    587633    function raiseMsg($message, $type, $file, $line)
    588634    {
    589         if ($this->getParam('display_messages')) {
     635        if ($this->getParam('display_messages') === true || (is_int($this->getParam('display_messages')) && $this->getParam('display_messages') >= $type)) {
    590636            App::raiseMsg($message, $type, $file, $line);
    591637        }
Note: See TracChangeset for help on using the changeset viewer.