Default or Calculated Dependee Attributes
Appearance
|
ATK Howto: Default or Calculated Dependee Attributes
|
Example code of how to create dependee attributes which can be defaulted to a calculated value and edited or always calauated and read only
class.testdeps.inc
<?php
/**
*
* testdeps - test dependees
*
* attribute "number" can have any number entered
* attributes "depnoedit" and depnoro" are BOT dependeess of attribute "number"
* attribute "depnoedit" IF blank is set to a default value of half of attribute "number"
* attribute "depnoedit" is EDITABLE
* attribute "depnoro" is always half of attribute "number" and is READ ONLY
*
* TABLE create SQL
* CREATE TABLE `testdeps` (
* `pktestdep` INT(10) UNSIGNED NOT NULL DEFAULT '0',
* `number` INT(10) UNSIGNED NULL DEFAULT NULL,
* `depnoedit` INT(10) UNSIGNED NULL DEFAULT NULL,
* `depnoro` INT(10) UNSIGNED NULL DEFAULT NULL,
* PRIMARY KEY (`pktestdep`)
* )
* COLLATE=utf8_general_ci
* ENGINE=MyISAM
* ROW_FORMAT=DYNAMIC
* AVG_ROW_LENGTH=0
*/
useattrib("atknumberattribute");
class testdeps extends atkNode
{
function testdeps()
{
$this->atkNode("testdeps", NF_ADD_LINK|NF_ADDAFTERADD);
// Primary key
$this->add(new atkAttribute("pktestdep", AF_AUTOINCREMENT|AF_PRIMARY|AF_HIDE));
// value for "number" can be entered
$attr_number = &$this->add(new atkNumberAttribute("number"));
// Fields are dependent on "number"
$attr_number->addDependee("depnoedit");
$attr_number->addDependee("depnoro");
// If blank is set to a default value. Is always editable
$attr_depnoedit = &$this->add(new atkNumberAttribute("depnoedit"));
// Is always calculated from "number" and is always READ ONLY
$attr_depnoro = &$this->add(new atkNumberAttribute("depnoro",AF_READONLY));
$this->setOrder("pktestdep");
$this->setIndex("pktestdep");
$this->setTable("testdeps");
} // function testdeps
// Override normal attribute edit
function depnoedit_edit($record, $mode)
{
// Set default value IF attribute is empty
if ($record["depnoedit"]=='') {
$record["depnoedit"] = $record["number"] * 0.5;
}
// return editable value
$attr = &$this->getAttribute("depnoedit");
return $attr->edit($record, $mode);
} //function
// Overrode normal attribute display
function depnoro_display($record, $mode)
{
// Calculate value of attribute "depnoro"
$record["depnoro"] = $record["number"] * 0.5;
// return displayable value
$attr = &$this->getAttribute("depnoro");
return $attr->display($record, $mode);
}
} //Class
?>