root/trunk/patForms/Rule.php

Revision 295, 6.4 kB (checked in by schst, 3 years ago)

Added new removeRule() method (Fixes bug #174)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms rule base class
4  *
5  * $Id$
6  *
7  * @access        protected
8  * @package        patForms
9  * @subpackage    Rules
10  */
11
12 /**
13  * patForms rule base class
14  *
15  * @access        protected
16  * @package        patForms
17  * @subpackage    Rules
18  * @author        Stephan Schmidt <schst@php-tools.net>
19  * @license        LGPL, see license.txt for details
20  * @link        http://www.php-tools.net
21  * @todo        implement javascript helper methods (set a javascript property plus an
22  *                array of keys that will be replaced by the properties of the rule)
23  */
24 class patForms_Rule
25 {
26    /**
27     * time when the rule should be applied
28     *
29     * Possible values are:
30     * -PATFORMS_RULE_BEFORE_VALIDATION
31     * -PATFORMS_RULE_AFTER_VALIDATION
32     * -PATFORMS_RULE_BOTH
33     *
34     * @access    private
35     * @var        integer
36     */
37     var $_time    =    PATFORMS_RULE_AFTER_VALIDATION;
38
39    /**
40     * script that will be displayed only once
41     *
42     * @access    private
43     * @var        array
44     */
45     var $globalScript    =    array();
46
47    /**
48     * script that will be displayed once per instance
49     *
50     * @access    private
51     * @var        array
52     */
53     var $instanceScript    =    array();
54
55    /**
56     * properties that have to be replaced in the instance script.
57     *
58     * @access    private
59     * @var        array
60     */
61     var $scriptPlaceholders    =    array();
62
63    /**
64     * store the container of the rule
65     *
66     * @access    private
67     * @var        object
68     */
69     var $container;
70
71    /**
72     * define error codes an messages for each form element
73     *
74     * @abstract
75     * @access    private
76     * @var        array
77     */
78     var    $validatorErrorCodes  =   array();
79
80    /**
81     * error code offset for the rule
82     *
83     * @abstract
84     * @access    private
85     */
86     var    $errorOffset;
87
88    /**
89     * format of the rule
90     *
91     * @abstract
92     * @access    private
93     */
94     var    $format    =    'html';
95
96    /**
97     * name of the rule
98     *
99     * @abstract
100     * @access    private
101     */
102     var    $ruleName = '';
103
104    /**
105     * Get the time when the rule should be applied.
106     *
107     * This has to be defined in the _time property of the rule.
108     *
109     * @access    public
110     * @return    integer
111     */
112     function getTime()
113     {
114         return $this->_time;
115     }
116
117    /**
118     * create a new rule object
119     *
120     * @access    public
121     * @param    string    id
122     */
123     function patForms_Rule( $id = null )
124     {
125         if ($id === null) {
126             $id    =    uniqid('');
127         }
128         $this->_id = $id;
129     }
130
131    /**
132     * set the id for the rule
133     *
134     * @access    public
135     * @param    string    id
136     */
137     function setId($id)
138     {
139         $this->_id = $id;
140     }
141
142    /**
143     * get the unique id of the rule
144     *
145     * @access public
146     * @return string
147     */
148     function getId()
149     {
150         return $this->_id;
151     }
152     
153    /**
154     * set the locale, this is needed to update the rule
155     * translations, that have been passed to the container
156     * element
157     *
158     * @access    public
159     * @param    string        new locale
160     * @return    boolean
161     */
162     function setLocale( $locale )
163     {
164         // rules do not store locale information
165         if (!patForms::isCustomLocale($locale)) {
166             return true;
167         }
168
169         $errorMessages = patForms::getCustomLocale($locale, 'Rule::' . $this->getRuleName());
170
171         if (is_array($errorMessages)) {
172             $this->validatorErrorCodes[$locale] = $errorMessages;
173         }
174
175         $this->container->addValidatorErrorCodes( $this->validatorErrorCodes, $this->errorOffset );
176
177         return true;
178     }
179
180    /**
181     * prepare the rule
182     *
183     * This method is used to initialize the rule.
184     * By default it adds it validatorErrorCodes
185     * to the container and stores a reference to the
186     * container.
187     *
188     * You may extend it in your custom rules, but should always be calling
189     * this method using:
190     *
191     * <code>
192     * patForms_Rule::prepareRule( $container );
193     * </code>
194     *
195     * @access    public
196     * @param    object    Either a patForms or patForms_Element object
197     */
198     function prepareRule( &$container )
199     {
200         $this->format        =    $container->getFormat();
201
202         $this->container    =    &$container;
203         $this->errorOffset    =    $container->getErrorOffset();
204
205         $container->addValidatorErrorCodes( $this->validatorErrorCodes, $this->errorOffset );
206
207         return true;
208     }
209
210    /**
211     * method called by patForms or any patForms_Element to validate the
212     * element or the form.
213     *
214     * @abstract
215     * @access    public
216     * @param    object        Either a patForms or patForms_Element object
217     * @return    boolean     true, if rule has been applied succesfully, false otherwise
218     */
219     function applyRule( &$container, $type = PATFORMS_RULE_BEFORE_VALIDATION )
220     {
221         // your code
222     }
223
224    /**
225     * addValidationError
226     *
227     * @access    private
228     * @param    integer    $code
229     * @param    array    $vars    fill named placeholder with values
230     * @return   boolean $result    true on success
231     */
232     function addValidationError( $code, $vars = array() )
233     {
234         $code= $this->errorOffset + $code;
235         return $this->container->addValidationError( $code, $vars );
236     }
237
238    /**
239     * get the name of the rule
240     *
241     * By default just return the classname, this is sufficient.
242     *
243     * @access    public
244     * @return    string
245     */
246     function getRuleName()
247     {
248         if (!empty($this->ruleName)) {
249             return $this->ruleName;
250         }
251         return get_class( $this );
252     }
253
254    /**
255     * Registers the scripts form this rule to the form
256     *
257     * @access    public
258     * @param    string $type   type of the element or rule
259     * @param    string $script path to a scriptfile
260     * @see        patForms::getScripts()
261     * @see        patForms_Element::registerJavascripts()
262     * @see        patForms_Rule::registerJavascripts()
263     */
264
265     function registerJavascripts(&$form)
266     {
267         if($script = $this->getGlobalJavascript()) {
268             $form->registerGlobalJavascript($this->getRuleName(), $script);
269         }
270
271         if($script = $this->getInstanceJavascript()) {
272             $form->registerInstanceJavascript($script);
273         }
274     }
275
276    /**
277     * get the global javascript of the rule
278     *
279     * @access    public
280     * @return    string
281     * @todo        Rules need to know the output format
282     */
283     function getGlobalJavascript()
284     {
285         if(isset($this->globalScript[$this->format])) {
286             return $this->globalScript[$this->format];
287         }
288     }
289
290    /**
291     * get the instance javascript of the rule
292     *
293     * @access    public
294     * @return    string
295     */
296     function getInstanceJavascript()
297     {
298         if(isset($this->instanceScript[$this->format])) {
299             $script    = $this->instanceScript[$this->format];
300             $script = str_replace('[RULE::ID]', $this->_id, $script);
301             if(method_exists($this->container, 'getId')) {
302                 $script = str_replace('[CONTAINER::ID]', $this->container->getId(), $script);
303             }
304             if(method_exists($this->container, 'getName')) {
305                 $script = str_replace('[CONTAINER::NAME]', $this->container->getName(), $script);
306             }
307             foreach($this->scriptPlaceholders as $placeholder => $property) {
308                 if(isset($this->$property)) {
309                     $script = str_replace('['.$placeholder.']', $this->$property, $script);
310                 } else {
311                     $script = str_replace('['.$placeholder.']', '', $script);
312                 }
313             }
314             return $script;
315         }
316     }
317 }
318 ?>
Note: See TracBrowser for help on using the browser.