Creating custom attributes
From Achievo/ATK Wiki
|
ATK Howto: Creating custom attributes
|
There are many ways in which attributes can be extended. Below are a few examples of attributes that extend other attributes, but add some logic.
Custom attributes can be placed in the attributes/ subdirectory of your modules, and then used by putting
useattrib("yourmodule.yourattrib");
in the node file. If you do that, in the node constructor it's only a matter of:
$this->add(new yourCustomAttribute(.......));
Contents |
Display override
class boldAttribute extends atkAttribute { function display($record, $mode="") { $org = parent::display($record, $mode); return '<b>'.$org.'</b>'; } }
Custom label
class clickableLabelAttribute extends atkAttribute { function getLabel() { $label = parent::getLabel(); $url = "http://www.achievo.org/atk"; return href($url, $label); } }
Edit override
Override the edit() function to alter what the user sees in add and edit modes, the function should return the HTML to be used to edit this attribute. This example is from the existing attribute atkIpAttribute - four input boxes are generated and used as the return value.
function edit($record, $fieldprefix="") { if ($this->hasFlag(AF_IP_SINGLEFIELD)) return parent::edit($record, $fieldprefix); $inputs = array(); $values = empty($record[$this->fieldName()]) ? NULL : explode('.', $record[$this->fieldName()]); for ($i = 0; $i < 4; $i++) { $name = $fieldprefix.$this->fieldName().'['.$i.']'; $value = isset($values[$i]) ? $values[$i] : ''; $inputs[] = '<input type="text" name="'.$name.'" value="'.$value.'" maxlength="3" size="3" />'; } return implode('.', $inputs); }
In the same way, you can override the edit function of the new attribute to return whatever input is needed. Note the call to the parent::edit() function too - this can be really helpful when you want to slightly alter or wrap the output rather than duplicate it.
Data conversion
Converting data right before it goes in the db, or right after it's loaded from the db:
class encryptedAttribute extends atkAttribute { function value2db($record) { $value = parent::value2db($record); $value = someEncryptionMethod($value); return $value; } function db2value($record) { $value = parent::db2value($record); $value = someDescryptionMethod($value); return $value; } }
Custom load/save
todo..