root/branches/gettext/patForms/Rule.php

Revision 394, 6.4 kB (checked in by gerd, 2 years ago)

Switched to patI18n

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