source: trunk/patForms/Element/Date/Element.php @ 175

Revision 175, 13.3 KB checked in by argh, 9 years ago (diff)

Integrated PEAR::Date for date operations, changed date input/output format to ISO 8601 or timestamp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/**
3 * patForms Date subelement main class
4 *
5 * Each part of a date is a separate object - this class build the
6 * base structure of each date part, and which each part extends.
7 *
8 * $Id$
9 *
10 * @access      public
11 * @package     patForms
12 * @subpackage  patForms_Element
13 * @author      Sebastian 'The Argh' Mordziol <argh@php-tools.net>
14 */
15
16/**
17 * patForms Date subelement main class
18 *
19 * Each part of a date is a separate object - this class build the
20 * base structure of each date part, and which each part extends.
21 *
22 * $Id$
23 *
24 * @access      public
25 * @package     patForms
26 * @subpackage  patForms_Element
27 * @author      Sebastian 'The Argh' Mordziol <argh@php-tools.net>
28 */
29class patForms_Element_Date_Element
30{
31   /**
32    * Defines the element type that will be used depending on the date
33    * element's mode.
34    *
35    * @access   private
36    * @var      array
37    */
38    var $elementTypes = array(
39        'default'   =>  'String',
40        'presets'   =>  'Enum'
41    );
42   
43   /**
44    * Stores the mode of the element
45    *
46    * @access   private
47    * @var      string
48    */
49    var $mode = 'default';
50   
51   /**
52    * Stores the actual token from the date format that will be
53    * used as base to generate the according field (e.g. 'Y' for
54    * a 4-digit year )
55    *
56    * @access   private
57    * @var      string
58    */
59    var $token = null;
60   
61   /**
62    * Stores each subelement's supported tokens and their configuration
63    *
64    * @abstract
65    * @access   private
66    * @var      array
67    */
68    var $tokens = array();
69   
70   /**
71    * Stores a compatibility table of date tokens that will be converted if used
72    * to the alternate token specified.
73    *
74    * @abstract
75    * @access   private
76    * @var      array
77    */
78    var $compatTable = array();
79   
80   /**
81    * Stores the patForms element object that will be used to
82    * reperesent this part in the date format
83    *
84    * @access   private
85    * @var      object
86    */
87    var $element = null;
88   
89   /**
90    * Stores the ID of the parent element, which is always the
91    * date element itself.
92    *
93    * @access   private
94    * @var      string
95    */
96    var $parentID = null;
97   
98   /**
99    * Stores this element's ID within the date element - used to
100    * generate a unique ID for each of the date element's subelements.
101    *
102    * @access   private
103    * @var      int
104    */
105    var $id = null;
106   
107   /**
108    * Stores the name of the parent element, which is always the date
109    * element. Used to generate the names for each of the date element's
110    * subelements.
111    *
112    * @access   private
113    * @var      string
114    */
115    var $parentName = null;
116   
117   /**
118    * Stores a reference to the parent object for additional functions
119    * that should require it.
120    *
121    * @access   private
122    * @var      object
123    */
124    var $parent = null;
125   
126   /**
127    * Stores the locale from patForms, and which will be used for all date
128    * output that is locale-dependent.
129    *
130    * @access   private
131    * @var      string
132    */
133    var $locale = 'C';
134   
135   /**
136    * Stores the attributes collection for the patForms element that is
137    * used for this element.
138    *
139    * @access   private
140    * @var      array
141    */
142    var $attributes = array();
143   
144   /**
145    * Stores the default attributes collection for the patForms element that
146    * is used for this element. This is split into the modes the element
147    * supports, to make sure that the different field types do not hinder
148    * each other with conflicting attribute collections.
149    *
150    * @access   private
151    * @var      array
152    */
153    var $defaultAttributes = array(
154        'default' => array(
155            'size'      =>  2,
156            'maxlength' =>  2,
157        ),
158        'presets' => array(
159        )
160    );
161   
162   /**
163    * Stores the default date object.
164    *
165    * @access   private
166    * @var      object
167    */
168    var $defaultDate = null;
169   
170   /**
171    * Stores the max date object.
172    *
173    * @access   private
174    * @var      object
175    */
176    var $maxDate = null;
177   
178   /**
179    * Stores the min date object.
180    *
181    * @access   private
182    * @var      object
183    */
184    var $minDate = null;
185   
186   /**
187    * Stores whether the element has been submitted.
188    *
189    * @access   private
190    * @var      bool
191    */
192    var $submitted = false;
193   
194   /**
195    * Stores the element's date object (the element's value)
196    *
197    * @access   private
198    * @var      object
199    */
200    var $date = null;
201   
202   /**
203    * Stores attributes for which there are special setter methods
204    * which need to be called. This is mainly for date attributes
205    * that need to be converted to date objects internally.
206    *
207    * @access   private
208    * @var      array
209    */
210    var $setterAttribs = array(
211        'default',
212        'max',
213        'min'
214    );
215   
216    var $initDone = false;
217
218   /**
219    * Initializes the element by creating the base attribute collection
220    * from the default attributes and the subelement's own implementation
221    * of the {@link initAttributes()} method.
222    *
223    * Called automatically by the date element when serializing or when
224    * validating. Creates the patForms element that is used for the input.
225    *
226    * @access   public
227    */
228    function init()
229    {
230        if( $this->initDone ) {
231            return true;
232        }
233       
234        $elName = $this->parentName.'_'.$this->token;
235
236        // prefill attributes collection for the current mode with the default attributes
237        $this->_initAttributes();
238       
239        // create the patForms element
240        $this->element =& patForms::createElement( $elName, $this->elementTypes[$this->mode], $this->attributes );
241       
242        // inherit the submitted state
243        $this->element->setSubmitted( $this->submitted );
244       
245        $this->initDone = true;
246       
247        return true;
248    }
249   
250   /**
251    * Serializes this date subelement by calling the {@link init()} routine
252    * and returning the element's serialized contents.
253    *
254    * @access   public
255    * @return   mixed   $content    The content from the element.
256    */
257    function serialize()
258    {
259        // refresh attributes
260        $this->_initAttributes();
261       
262        // refresh the element's attributes
263        $this->element->setAttributes( $this->attributes );
264
265        // and serialize :)
266        return $this->element->serialize();
267    }
268
269   /**
270    * Retrieves the length of this element's value
271    *
272    * @access   public
273    * @return   int $length The requested length
274    */
275    function getLength()
276    {
277        return $this->tokens[$this->token]['length'];
278    }
279   
280   /**
281    * Sets the token to use for this date subelement.
282    *
283    * @access   public
284    * @param    string  $token  The token to use
285    * @see      $token
286    */
287    function setToken( $token )
288    {
289        $this->token = $token;
290    }
291   
292   /**
293    * Sets a reference to the parent object, which always is the date
294    * element. Used by some elements to use some special methods like
295    * the {@link patForms_Element_Date::tokenUsed()} method.
296    *
297    * @access   public
298    * @param    object  &$parent    The date element object.
299    */
300    function setParent( &$parent )
301    {
302        $this->parent =& $parent;
303    }
304   
305   /**
306    * Sets the ID of the parent element (always the ID of the date
307    * element), set automatically by the date element itself.
308    *
309    * @access   public
310    * @param    string  $id The ID string
311    */
312    function setParentID( $id )
313    {
314        $this->parentID = $id;
315    }
316   
317   /**
318    * Sets this subelement's ID within the subelements list, so a unique
319    * ID can be generated for each element. Set automatically by the date
320    * element itself.
321    *
322    * @access   public
323    * @param    int $id The ID
324    */
325    function setID( $id )
326    {
327        $this->id = $id;
328    }
329
330   /**
331    * Sets the name of the parent element, which is always the date element.
332    * Used to generate the names of each subelement. Automatically set by
333    * the date element itself.
334    *
335    * @access   public
336    * @param    string  $parentName The name of the date element
337    */
338    function setParentName( $parentName )
339    {
340        $this->parentName = $parentName;
341    }
342   
343   /**
344    * Sets the mode to use for the element - there are two modes which
345    * determine how the subelement will be rendered. Set automatically
346    * by the date element itself.
347    *
348    * @access   public
349    * @param    string  $mode   The mode to use.
350    * @see      $elementTypes
351    */
352    function setMode( $mode )
353    {
354        $this->mode = $mode;
355    }
356   
357   /**
358    * Sets the locale to use for the subelement. This is used to determine
359    * which strings to use when language-dependent string have to be used.
360    * Set automatically to the global patForms setting by the date element.
361    *
362    * @access   public
363    * @param    string  $locale The locale
364    */
365    function setLocale( $locale )
366    {
367        $this->locale = $locale;
368    }
369   
370   /**
371    * Initializes the attribute collection for the patForms element that will
372    * be used for this subelement by copying over the default attributes for
373    * the selected mode, creating the ID and retrieving values depending on the
374    * selected mode.
375    *
376    * @access   private
377    */
378    function _initAttributes()
379    {
380        $this->attributes = $this->defaultAttributes[$this->mode];
381
382        $this->attributes['id'] = $this->parentID.'-'.$this->id;
383
384        if( $this->mode == 'presets' ) {
385            $this->attributes['values'] = $this->getValues();
386        }
387
388        // give the element the possibility to init its own attributes
389        $this->initAttributes();
390    }
391   
392   /**
393    * Initializes the attribute collection. Abstract method that each subelement
394    * can use to intialize its own special attributes.
395    *
396    * @abstract
397    * @access   public
398    */
399    function initAttributes()
400    {
401        // code in the subelement class
402    }
403   
404   /**
405    * Checks whether a value exists in the element's value list. Used
406    * by the date element to check or validate date strings according
407    * to the specified format.
408    *
409    * @access   public
410    * @param    string  $value  The value to check
411    * @return   bool    $exists True if it exists, false otherwise.
412    */
413    function valueExists( $value )
414    {
415        if( $this->mode != 'presets' ) {
416            return true;
417        }
418       
419        foreach( $this->attributes['values'] as $row => $set ) {
420            if( $set['value'] == $value ) {
421                return true;
422            }
423        }
424       
425        return false;
426    }
427   
428   /**
429    * Retrieves the element's values as a flat string from the element's
430    * values collection. Used in error messages.
431    *
432    * @access   public
433    * @return   string  $value  The values list.
434    */
435    function getValuesFlat()
436    {
437        $els = array();
438        foreach( $this->attributes['values'] as $row => $set ) {
439            array_push( $els, $set['value'] );
440        }
441       
442        return implode( ', ', $els );
443    }
444   
445   /**
446    * Sets the default timestamp the element will use if no value
447    * is set.
448    *
449    * @access   public
450    * @param    string  $date   The timestamp
451    */
452    function setDefault( $date )
453    {
454        $this->defaultDate =& $date;
455
456        $methodName = $this->getterMethod();
457        $value = $this->defaultDate->$methodName();
458
459        if( $this->initDone ) {
460            $this->element->setAttribute( 'default', $value );
461        } else {
462            $this->setDefaultAttribute( 'default', $value );
463        }
464       
465        return true;
466    }
467   
468   /**
469    * Can be used to set/add an attribute from the default attributes
470    * collection, for the current mode
471    *
472    * @access   private
473    * @param    string  $attributeName  The attribute to set
474    * @param    mixed   $value          The valueto set the attribute to
475    * @see      $defaultAttributes
476    */
477    function setDefaultAttribute( $attributeName, $value )
478    {
479        $this->defaultAttributes[$this->mode][$attributeName] = $value;
480    }
481   
482   /**
483    * Sets the maximum timestamp the element will use for validation
484    *
485    * @access   public
486    * @param    string  $date   The timestamp
487    */
488    function setMax( $date )
489    {
490        $this->maxDate =& $date;
491        return true;
492    }
493
494   /**
495    * Sets the maximum timestamp the element will use for validation
496    *
497    * @access   public
498    * @param    string  $date   The timestamp
499    */
500    function setMin( $date )
501    {
502        $this->minDate =& $date;
503        return true;
504    }
505
506   /**
507    * Retrieves the compatible token for the current token if a compatible
508    * token is defined.
509    *
510    * @access   private
511    * @param    string  $token  Optional token to check
512    * @return   string  $token  Teh compatible token, teh original token otherwise
513    * @see      $compatTable
514    */
515    function getCompatToken( $token = null )
516    {
517        if( is_null( $token ) ) {
518            $token = $this->token;
519        }
520       
521        if( isset( $this->compatTable[$token] ) ) {
522            return $this->compatTable[$token];
523        }
524       
525        return $token;
526    }
527   
528   /**
529    * Makes an attribute of the date element be inherited by the patForms subelement.
530    *
531    * @access   public
532    * @param    string  $attribute  The name of the attribute
533    * @param    mixed   $value      The value of the attribute
534    */
535    function inheritAttribute( $attribute, $value )
536    {
537        foreach( $this->defaultAttributes as $mode => $attributes ) {
538            $this->defaultAttributes[$mode][$attribute] = $value;
539        }
540    }
541   
542   /**
543    * Wrapper for the patForms element's setSubmitted method. Stores the
544    * state internally when the element has not been created yet, and it
545    * will be set on creation.
546    *
547    * @access   public
548    * @param    bool    $state  True = submitted, false = not submitted
549    * @see      $submitted
550    */
551    function setSubmitted( $state ) 
552    {
553        $this->submitted = $state;
554       
555        if( !is_null( $this->element ) ) {
556            $this->element->setSubmitted( $state );
557        }
558    }
559   
560   /**
561    * Wrapper for the patForms element's getValue() method, in which the value
562    * is reformatted as the specified format requires to make sure there are no
563    * glitches.
564    *
565    * @access   public
566    * @return   string  $value  The value
567    */
568    function getValue()
569    {
570        $value = $this->element->getValue();
571        if( strlen( $value ) < 1 ) {
572            return null;
573        }
574       
575        return sprintf( $this->tokens[$this->getCompatToken()]['format'], $value );
576    }
577   
578   /**
579    * Sets the element's value. If the element has not been created yet,
580    * the value is stored internally and given the element on creation.
581    *
582    * @access   public
583    * @param    string  $date   The timestamp to create the value from 
584    */
585    function setValue( &$date )
586    {
587        $this->date =& $date;
588       
589        if( !is_null( $this->element ) ) {
590            $methodName = $this->getterMethod();
591            $value = sprintf( 
592                $this->tokens[$this->getCompatToken()]['format'], 
593                $this->date->$methodName()
594            );
595           
596            $this->element->setValue( $value );
597        }
598    }
599   
600    function getterMethod()
601    {
602        return $this->tokens[$this->token]['getter'];
603    }
604   
605    function setterMethod()
606    {
607        return $this->tokens[$this->token]['setter'];
608    }
609   
610   /**
611    * Validates the element.
612    *
613    * @abstract
614    * @access   public
615    * @return   bool    $valid  True if valid, false otherwise
616    */
617    function validate()
618    {
619        return true;
620    }
621}
622
623?>
Note: See TracBrowser for help on using the repository browser.