Changeset 17866

Show
Ignore:
Timestamp:
10/02/08 14:25:42 (2 months ago)
Author:
adrenalin
Message:

Forward-porting r17550.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.helper.datamanager2/widget/jsdate.php

    r15254 r17866  
    5959     */ 
    6060    var $maxyear = 9999; 
     61 
     62    /** 
     63     * Used date format 
     64     *  
     65     * @var String 
     66     */ 
     67    var $format = '%Y-%m-%d %H:%M:%S'; 
    6168 
    6269    /** 
     
    258265        if ($this->show_time) 
    259266        { 
     267            $this->format = '%Y-%m-%d %H:%M:%S'; 
     268        } 
     269        else 
     270        { 
     271            $this->format = '%Y-%m-%d'; 
     272        } 
     273        return $this->_type->value->format($this->format); 
     274    } 
     275 
     276    /** 
     277     * Check against partially missing user input 
     278     *  
     279     * Be liberal with input, strict with output 
     280     * 
     281     * @access public 
     282     * @param mixed $input  User input 
     283     * @return String       Formatted date 
     284     */ 
     285    function check_user_input($input) 
     286    { 
     287        $input = trim($input); 
     288         
     289        static $valid_date_format = '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/'; 
     290        static $valid_datetime_format = '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/'; 
     291         
     292        // Input is strict ISO date with time 
     293        if (preg_match($valid_datetime_format, $input)) 
     294        { 
     295            return $input; 
     296        } 
     297         
     298        // Input is strict ISO date 
     299        if (preg_match($valid_date_format, $input)) 
     300        { 
     301            return "{$input} 00:00:00"; 
     302        } 
     303         
     304        // Value is numeric, expecting UNIXTIME 
     305        if (is_numeric($input)) 
     306        { 
     307            return strftime($this->format, $input); 
     308        } 
     309         
     310        $date = null; 
     311        $time = null; 
     312         
     313        // Check against missing leading zeros from years, months, and days 
     314        if (preg_match('/^([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})/', $input, $regs)) 
     315        { 
     316            $date = str_pad($regs[1], 4, date('Y')) . '-' . str_pad($regs[2], 2, '0') . '-' . str_pad($regs[3], 2, 0); 
     317        } 
     318         
     319        // Check against missing leading zeros from minutes and seconds 
     320        if (preg_match('/^[0-9]{2,4}-[0-9]{1,2}-[0-9]{1,2}\s*(.*)$/', $input, $regs)) 
     321        { 
     322            // Fill in the leading zeros to hours 
     323            if (!preg_match('/^[0-9]{2}/', $regs[1])) 
     324            { 
     325                $regs[1] = str_pad($regs[1], 2, '0'); 
     326            } 
     327             
     328            // The rest should be all about filling in the missing end of the timestamp 
     329            $time = $regs[1] . substr('00:00:00', strlen($regs[1])); 
     330        } 
     331         
     332        // Both date and time found, convert the input to hopefully full-fletched ISO datetime 
     333        if (   $date 
     334            && $time) 
     335        { 
     336            $input = "{$date} {$time}"; 
     337        } 
     338         
     339        // Try to convert the input string to date 
     340        $timestamp = strtotime($input); 
     341         
     342        // Expected output is higher than zero with strtotime 
     343        if ($timestamp > 0) 
     344        { 
     345            return strftime($this->format, $timestamp); 
     346        } 
     347         
     348        // Could not determine the datetime, give an empty date 
     349        return '0000-00-00 00:00:00'; 
     350    } 
     351 
     352    /** 
     353     * Tells the base date class instance to parse the value from the input field. 
     354     */ 
     355    function sync_type_with_widget($results) 
     356    { 
     357        // Try to fix the incorrect input 
     358        $date = $this->check_user_input($results[$this->name]); 
     359         
     360        $this->_type->value = new Date($date); 
     361    } 
     362 
     363    /** 
     364     * Renders the date in the ISO format. 
     365     */ 
     366    function render_content() 
     367    { 
     368        if ($this->show_time) 
     369        { 
    260370            $format = '%Y-%m-%d %H:%M:%S'; 
    261371        } 
     
    264374            $format = '%Y-%m-%d'; 
    265375        } 
    266         return $this->_type->value->format($format); 
    267     } 
    268  
    269     /** 
    270      * Tells the base date class instance to parse the value from the input field. 
    271      */ 
    272     function sync_type_with_widget($results) 
    273     { 
    274         $this->_type->value = new Date($results[$this->name]); 
    275     } 
    276  
    277     /** 
    278      * Renders the date in the ISO format. 
    279      */ 
    280     function render_content() 
    281     { 
    282         if ($this->show_time) 
    283         { 
    284             $format = '%Y-%m-%d %H:%M:%S'; 
    285         } 
    286         else 
    287         { 
    288             $format = '%Y-%m-%d'; 
    289         } 
    290376        echo $this->_type->value->format($format); 
    291377    } 
    292  
    293378} 
    294  
    295379?>