Orion PHP  0.11.12
The PHP5.3 framework
field.php
Go to the documentation of this file.
00001 <?php
00002 
00003 namespace Orion\Core\Model;
00004 
00005 /**
00006  * Base model field class.
00007  *
00008  * Extend this abstract class when creating a new model field class. (Customfield extends Field). 
00009  * <b>Be sure to specify at least $bind and $type attributes</b>
00010  */
00011 abstract class Field
00012 {
00013 
00014     /**
00015      * Sets field visibility in Form creation
00016      * @var boolean
00017      */
00018     protected $visible = true;
00019 
00020     /**
00021      * Set this to true if it's a field type used for table linkage
00022      * @var boolean
00023      */
00024     protected $linked = false;
00025 
00026     /**
00027      * Set this to true if this field need the form type to be multipart (like a file uploader for example).
00028      * @var boolean
00029      */
00030     protected $multipart = false;
00031 
00032     /**
00033      * The field's value
00034      * @var mixed
00035      */
00036     protected $value;
00037 
00038     /**
00039      * Standard field type identifier
00040      * @var string
00041      */
00042     protected $type;
00043 
00044     /**
00045      * Field identifier (used as DB field name, for field ID, and such)
00046      * @var string
00047      */
00048     protected $bind;
00049 
00050     /**
00051      * Field label, used for display and form labelling purposes
00052      * @var string
00053      */
00054     protected $label = 'OrionField';
00055 
00056     /**
00057      * Set this to true if the field is a primary DB key
00058      * @var boolean
00059      */
00060     protected $primary = false;
00061 
00062     /**
00063      * Set this to true if it's a required field
00064      * @var boolean
00065      */
00066     protected $required = false;
00067 
00068     /**
00069      * Retreive field's label
00070      * @return string
00071      */
00072     public function getLabel()
00073     {
00074         return $this->label;
00075     }
00076 
00077     /**
00078      * Retreive field's type
00079      * @return string
00080      */
00081     public function getType()
00082     {
00083 
00084         return $this->type;
00085     }
00086 
00087     /**
00088      * Retreive field's binding (field ID)
00089      * @return string
00090      */
00091     public function getBinding()
00092     {
00093         return $this->bind;
00094     }
00095 
00096     /**
00097      * Same as getBinding(), just a more common name. Retreive field's binding (field ID)
00098      * @return string
00099      */
00100     public function getName()
00101     {
00102         return $this->bind;
00103     }
00104 
00105     /**
00106      * Retreive field's value
00107      * @return string
00108      */
00109     public function getValue()
00110     {
00111         return $this->value;
00112     }
00113 
00114     /**
00115      * Set field's value
00116      */
00117     public function setValue( $val )
00118     {
00119         $this->value = $val;
00120     }
00121 
00122     /**
00123      * Set field's form visibility
00124      */
00125     public function setVisibility( $bool )
00126     {
00127         $this->visible = $bool;
00128     }
00129 
00130     /**
00131      * Check if the field's value is empty (override for specific cases)
00132      * @return boolean
00133      */
00134     public function isEmptyValue( $value )
00135     {
00136         return ($value == null || $value == '' || $value == "''");
00137     }
00138 
00139     /**
00140      * Check if the field is linked
00141      * @return boolean
00142      */
00143     public function isLinked()
00144     {
00145         return $this->linked;
00146     }
00147 
00148     /**
00149      * Check if the field needs a multipart form type
00150      * @return boolean
00151      */
00152     public function isMultipart()
00153     {
00154         return $this->multipart;
00155     }
00156 
00157     /**
00158      * Check if the field is a primary DB key
00159      * @return boolean
00160      */
00161     public function isPrimary()
00162     {
00163         return $this->primary;
00164     }
00165 
00166     /**
00167      * Check if the field is required
00168      * @return boolean
00169      */
00170     public function isRequired()
00171     {
00172         return $this->required;
00173     }
00174 
00175     /**
00176      * Check if the field is visible in form creation
00177      * @return boolean
00178      */
00179     public function isVisible()
00180     {
00181         return $this->visible;
00182     }
00183 
00184     /**
00185      * This method is called before every field value usage in DB transaction.
00186      * Override this method to define a specific way of preparing the field value for DB insertion
00187      * @return string
00188      */
00189     public function prepare( $value )
00190     {
00191         return "'" . $value . "'";
00192     }
00193 
00194     /**
00195      * This method is called before every field value usage in DB transaction.
00196      * Override this method to use a specific value validation process for DB insertion
00197      * @return boolean
00198      */
00199     public function validate( $value )
00200     {
00201         return true;
00202     }
00203 
00204     public function __toString()
00205     {
00206         return '[Field] ' . $this->label . ' bound to ' . $this->bind;
00207     }
00208 
00209     /**
00210      * This method is called before DB deletion action.
00211      * Override this method to define a specific pre-deletion behaviour
00212      * @param mixed $oldvalue The old field value (before deletion occurs)
00213      */
00214 //    public function onDelete( $oldvalue )
00215 //    {
00216 //        //-
00217 //    }
00218 
00219     /**
00220      * This method is called before DB insertion action
00221      * Override this method to define a specific pre-insertion behaviour
00222      * @param mixed $value The new field value (before insertion occurs)
00223      */
00224 //    public function onSave( $value )
00225 //    {
00226 //        //-
00227 //    }
00228 
00229     /**
00230      * This method is called before DB update action
00231      * Override this method to define a specific pre-update behaviour
00232      * @param mixed $oldvalue The old field value (before update occurs)
00233      * @param mixed $value The new field value (before update occurs)
00234      */
00235 //    public function onUpdate( $oldvalue, $newvalue )
00236 //    {
00237 //        //-
00238 //    }
00239 
00240     /**
00241      * Override this function to define a specific (x)HTML form field to use during model-to-form translation.
00242      * By default, this function returns a hidden form field.
00243      * @param boolean $XHTML
00244      * @return string
00245      */
00246     public function toHtml( $XHTML=true )
00247     {
00248         if ( $XHTML )
00249             $tag = ' /';
00250         else
00251             $tag = '';
00252 
00253         return '<input name="' . $this->bind . '" type="hidden" value="' . $this->value . '"' . $tag . '>';
00254     }
00255 
00256 }
00257 
00258 ?>