whereAdd("etype = ''"); } } function onUpdate($old, $req) { $x = $this->factory($this->tableName()); $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); if ($old->etype != $this->etype) { $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')"); } } function beforeUpdate($old, $request,$roo) { $tn = $this->tableName(); $x = $this->factory($tn); if(!($old->etype == $request['etype'] && $old->name == $request['name'])){ $x->whereAdd("etype = '{$request['etype']}' AND name = '{$request['name']}'"); $x->find(true); if($x->count() > 0){ $roo->jerr('is exsiting'); } } } function beforeInsert($req, $roo) { $tn = $this->tableName(); $x = $this->factory($tn); if(empty($req['etype'])){ if($x->get('name', $req['name'])){ $roo->jerr('name is exsiting'); } }else{ $x->whereAdd("etype = '{$req['etype']}' AND name = '{$req['name']}'"); $x->find(true); if($x->count() > 0){ $roo->jerr('is exsiting'); } } } function onInsert($req) { $x = $this->factory($this->tableName()); $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); } function lookup($etype,$name) { $ce = DB_DataObject::Factory('core_enum'); $ce->etype = $etype; $ce->name = $name; if ($ce->find(true)) { return $ce->id; } return 0; } function lookupObject($etype,$name, $create= false) { static $cache = array(); $key = "$etype:$name"; if (isset($cache[$key]) ) { return $cache[$key]; } $ce = DB_DataObject::Factory('core_enum'); $ce->etype = $etype; $ce->name = $name; if ($ce->find(true)) { $cache[$key] = $ce; return $ce; } if ($create) { $ce->active = 1; $ce->insert(); $cache[$key] = $ce; return $ce->id; } return false; } /** * The base enums that the system always needs, * If we need to add modular enums, then this can be extended, and the two * base data applied. * * This might be moved to an external file later? - eg json... * */ function baseEnums() { // return array( array( 'etype' => '', 'name' => 'COMPTYPE', 'display_name' => 'Company Types', 'cn' => array( array( 'name' => 'OWNER', 'display_name' => 'Owner', 'seqid' => 999, // last... ) ) ) ); } function initEnums($data = false, $base = array()) { if ($data === false) { $this->initEnums($this->baseEnums()); return; } $seq_id = 0; if (!empty($base['etype'])) { $seq_id = 1; $t = DB_DAtaObject::Factory('core_enum'); $t->etype = $base['etype']; $t->selectAdD(); $t->selectAdD('max(seqid) as seqid'); if ($t->find(true)) { $seq_id = $t->seqid+1; } } foreach($data as $row) { $t = DB_DAtaObject::Factory('core_enum'); $t->setFrom($row); $t->setFrom($base); if (!$t->find(true)) { if (!empty($base['etype']) && empty($row['seqid'])) { $t->seqid = $seq_id; $seq_id++; } $t->insert(); } if (!empty($row['cn'])) { $this->initEnums($row['cn'], array('etype' => $t->name)); } } } }