Creating custom attributes: Difference between revisions
No edit summary |
adding a bit about overriding the edit method |
||
| (9 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
{{Howto|Advanced|[[Ivo Jansch]] <ivo@achievo.org>}} | |||
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. | 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 | Custom attributes can be placed in the attributes/ subdirectory of your modules, and then used by putting | ||
< | |||
<syntaxhighlight lang="php"> | |||
useattrib("yourmodule.yourattrib"); | useattrib("yourmodule.yourattrib"); | ||
</ | </syntaxhighlight> | ||
in the node file. If you do that, in the node constructor it's only a matter of: | in the node file. If you do that, in the node constructor it's only a matter of: | ||
< | |||
<syntaxhighlight lang="php"> | |||
$this->add(new yourCustomAttribute(.......)); | $this->add(new yourCustomAttribute(.......)); | ||
</ | </syntaxhighlight> | ||
==Display override== | ==Display override== | ||
< | |||
<syntaxhighlight lang="php"> | |||
class boldAttribute extends atkAttribute | class boldAttribute extends atkAttribute | ||
{ | { | ||
| Line 19: | Line 26: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Custom label== | ==Custom label== | ||
< | |||
class | <syntaxhighlight lang="php"> | ||
class clickableLabelAttribute extends atkAttribute | |||
{ | { | ||
function getLabel() | function getLabel() | ||
| Line 31: | Line 40: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Edit override== | ==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. | |||
<syntaxhighlight lang="php"> | |||
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); | |||
} | |||
</syntaxhighlight> | |||
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== | ==Data conversion== | ||
Converting data right before it goes in the db, or right after it's loaded from the db: | Converting data right before it goes in the db, or right after it's loaded from the db: | ||
< | |||
<syntaxhighlight lang="php"> | |||
class encryptedAttribute extends atkAttribute | class encryptedAttribute extends atkAttribute | ||
{ | { | ||
| Line 56: | Line 89: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
==Custom load/save== | ==Custom load/save== | ||
todo.. | todo.. | ||
Latest revision as of 11:55, 12 September 2008
|
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(.......));
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..