Model

Beans

Hardhat uses objects known as beans (similar to Javabeans in Java), to contain the data. Hardhat's Beans extend from the sys_base_bean class and use getters and setters to access and update their data respectively.

A bean with no additional functionality would look something like this:


class core_widget_bean extends sys_base_bean
{
    // ------------------------------------- constructor
    public function __construct(){
      parent::__construct();
    }
}

Note that the bean extends the sys_base_bean class, and contains a contructor with only a call to it's parent.

It might seem like a waste of time to code something like this, but later when you want to jump in the middle of this, you'll be glad you did. Instead of updating every reference where you've instantiated sys_base_bean to contain widget data, you can just come here and do whatever you need to.

Without doing anything else, we can now do things like:


$widget = new core_widget_bean();

$widget->setCreateDate(time());
$formattedDate = $widget->getCreateDate('shortDate');

We can now start to add convenience functions such as:


 class core_widget_bean extends sys_base_bean
{
    // ------------------------------------- constructor
    public function __construct(){
      parent::__construct();
      $this->setExpirationPeriod(30);
    }
   public function getExpirationDate($parm1, $parm2) {
      return sys_base_bean::convert(
           strtotime($this->getCreateDate('shortdate').'+'
          .$this->getExpirationPeriod().'days'),$parm1,$parm2);  
   }
}

The sys_base_bean class makes use of the __call magic function to implement generic getters and setters. In this way, it is not necessary to code getters and setters for fields where the standard behavior is not being overriden.

The __get and __set magic functions are disabled and will result in a message like "invalid attempt to get/set property $var in class $className probably missing () on a method call". Code $object->getField() rather than $object->getField to avoid this.

Also note that the first character of the field is capitalized in the getters and setters, but the name itself is not. So for example, you would code: $object->getSomeValue() to access a field named someValue.

You can also use the generic get($field), and set($field,$value). These are helpful when you need to iterate through a list of fields. Note that in this case the first character of the field should be lower case (i.e. $object->get('myFieldName') is equivalent to $object->getMyFieldName();

Get Parameters

If you pass parameters to the getter method they will be used to modify the returned value in some way. For example you can code $myObject->getMyDate('shortdate') to get a formatted date. You can also call the convert static method to perform the same conversion. The possible values are:

Sub CommandParm2Description
dbdaten/a Format a date suitable for inserting into a database.
checkedcompare value Rreturns checked="checked" when $parm2 matchs the value of the specified field. This can be very useful when rendering checkboxes in the template.
datephpphp date formatting string Returns a date based on the php date formatting routine date(string, time). Useful for formatting dates within a template.
maxcompare value Returns the value of the field as long as it is less than the compare value otherwise it returns the compare value. Useful to set a limit.
moneyn/a Format a field as a money field with a leading dollar sign.
selectedcompare value Returns selected="selected" when $parm2 matchs the value of the specified field. This can be very useful when creating a dropdown in the template.
longdaten/a Returns a formatted version of a date field. (Sunday November 13, 2011)
shortdaten/a Returns a formatted version of a date field. (12/31/2012)
shortdatetimen/a Returns a formatted version of a date field, with the time appended.
websafen/a Returns an escaped version of the field that is safe for displaying in webpages. New Line characters are converted to <br /> tags Specifically, nl2br(strip_tags(htmlentities($field)))
websafenln/a Same as websafe but without adding the <br /> tags Specifically, strip_tags(htmlentities($field))
yesnon/a Returns Yes, No or Not Specified based on whether the value of the field is Y, N or something else respectively.
yesn/a Returns Yes if the value of the field is Y, otherwise blank.

Other sys_base_bean Methods

Here are some other methods of the sys_base_bean you can use:

MethodDescription
fieldExists($field) Determines if a particular field exists in the bean.
getBeanFields() Get an array of all the bean fields
setBeanFields() Set all the bean fields at once. Overlays anything that was there before.
dumpBeanFields() Dump the bean fields in a formatted arrangement suitable for outputting to the webpage for debugging.
get($field) Get the value of a bean field. Use this form if you need to access the fields from a variable for example: use $myField = 'id'; $object->get($myField) or $object->getId();
set($field,$value) Set the value of a bean field. Use this form if you need to set the field from a variable for example: use $myField = 'id';$n = 10; $object->set($myField,$n) or $object->setId(10);