root/trunk/apps/main/lib/myDateValidator.class.php

Revision 2, 3.1 kB (checked in by nperriault, 2 years ago)

First commit of the extracted code from production, I hope no passwd has been forgotten :-)

Line 
1 <?php
2 /**
3  * Custom date validator
4  *
5  */
6 class myDateValidator extends sfDateValidator
7 {
8   /**
9    * Execute this validator.
10    *
11    * @param mixed A file or parameter value/array
12    * @param error An error message reference
13    *
14    * @return bool true, if this validator executes successfully, otherwise false
15    */
16   public function execute(&$value, &$error)
17   {
18     $culture = $this->getContext()->getUser()->getCulture();
19
20     // Validate the given date
21     if ($this->getParameter('with_culture'))
22     {
23       $value1 = $this->getValidDate($value, $culture);
24     }
25     else
26     {
27       $value1 = strtotime($value);
28     }
29
30     if (!$value1 || -1 == $value1) // Before php 5.1, strtotime() returns -1 on fail
31     {
32       $error = $this->getParameter('date_error');
33       return false;
34     }
35
36     // Is there a compare to do?
37     $compareDateParam = $this->getParameter('compare');
38     $compareDate = $this->getContext()->getRequest()->getParameter($compareDateParam);
39
40     // If the compare date is given
41     if ($compareDate)
42     {
43       $operator = trim($this->getParameter('operator', '=='), '\'" ');
44       $value2 = $this->getValidDate($compareDate, $culture);
45
46       // If the check date is valid, compare it. Otherwise ignore the comparison
47       if ($value2)
48       {
49         $valid = false;
50         switch ($operator)
51         {
52           case '>':
53             $valid = $value1 $value2;
54             break;
55           case '>=':
56             $valid = $value1 >= $value2;
57             break;
58           case '==':
59             $valid = $value1 == $value2;
60             break;
61           case '<=':
62             $valid = $value1 <= $value2;
63             break;
64           case '<':
65             $valid = $value1 $value2;
66             break;
67
68           default:
69             throw new sfValidatorException(sprintf('Invalid date comparison operator "%s"', $operator));
70         }
71
72         if (!$valid)
73         {
74           $error = $this->getParameter('compare_error');
75
76           return false;
77         }
78       }
79     }
80
81     if (!$this->getParameter('allow_future') && $value1 > time())
82     {
83       $error = $this->getParameter('future_error');
84       return false;
85     }
86
87     if (!$this->getParameter('allow_past') && $value1 < time())
88     {
89       $error = $this->getParameter('past_error');
90       return false;
91     }
92
93     return true;
94   }
95
96   /**
97    * Initializes the validator.
98    *
99    * @param sfContext The current application context
100    * @param array   An associative array of initialization parameters
101    *
102    * @return bool true, if initialization completes successfully, otherwise false
103    */
104   public function initialize($context, $parameters = null)
105   {
106     // Initialize parent
107     parent::initialize($context, $parameters);
108
109     // Set defaults
110     $this->getParameterHolder()->set('with_culture', true);
111     $this->getParameterHolder()->set('allow_future', true);
112     $this->getParameterHolder()->set('future_error', 'Future dates not allowed');
113     $this->getParameterHolder()->set('allow_past', true);
114     $this->getParameterHolder()->set('past_error', 'Past dates not allowed');
115     $this->getParameterHolder()->add($parameters);
116
117     return true;
118   }
119 }
120
Note: See TracBrowser for help on using the browser.