/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
- function applyFilters($q, $au)
+ function applyFilters($q, $au, $roo)
{
// DB_DataObject::debugLevel(1);
if (!empty($q['query']['empty_etype'])) {
- $this->whereAdd("etype = ''");
+ $this->whereAdd("core_enum.etype = ''");
}
// this should be handled by roo... using '!name[0]' ....
core_enum.display_name LIKE '%{$name}%'
");
}
-
-// print_r($this);
- if(!empty($q['id'])){ // this should never be needed?!?!?!?!??
- $this->id = $q['id'];
+ if(!empty($q['query']['search_begins'])) {
+ $name = $this->escape($q['query']['search_begins']);
+ // ilike on postgres?!?
+ $this->whereAdd("
+ core_enum.name LIKE '{$name}%'
+ OR
+ core_enum.display_name LIKE '{$name}%'
+ ");
}
- if(!empty($q['_ids'])){
- $this->whereAddIn('core_enum.id', explode(',', $q['_ids']) , 'int');
+ if (isset($q['_etypes'])) {
+ $this->whereAddIn('core_enum.etype', explode(',', $q['_etypes']), 'string');
}
}
function beforeUpdate($old, $request,$roo)
{
+ if(!empty($request['_merge_id'])){
+ $this->merge($request['_merge_id'], $roo);
+ }
+
$tn = $this->tableName();
$x = $this->factory($tn);
- if(!($old->etype == $request['etype'] && $old->name == $request['name'])){
+ // check if record exists?
+ if(isset($request['etype']) && !($old->etype == $request['etype'] && $old->name == $request['name'])){
$x->whereAdd("etype = '{$this->escape($request['etype'])}' AND name = '{$this->escape($request['name'])}'");
+ $x->whereAdd("id != ".((int) $this->id));
$x->find(true);
if($x->count() > 0){
- $roo->jerr('is exsiting');
+ $roo->jerr('a duplicate record already exists');
}
}
}
}
}
- function onInsert($req)
+ function onInsert($req, $roo)
{
$x = $this->factory($this->tableName());
$x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
}
+
function onUpdate($old, $req)
{
$x = $this->factory($this->tableName());
if ($old->etype != $this->etype) {
$x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
}
- if($this->name != $old->name && empty($old->etype) && empty($this->etype)){
+
+ if($this->name != $old->name && !empty($old->name) && empty($old->etype) && empty($this->etype)){
$x->query("UPDATE core_enum SET etype = '". $this->escape($this->name)
."' WHERE etype = '". $this->escape($old->name)."'");
}
}
function lookupCreate($etype,$name, $display_name=false) {
+
+ // check
+ $ce = DB_DataObject::Factory('core_enum');
+ $ce->setFrom(array(
+ 'etype' => '',
+ 'name' => $etype
+ ));
+ if (!$ce->find(true)) {
+ $ce->display_name = $etype;
+ $ce->insert();
+ }
+
$ce = DB_DataObject::Factory('core_enum');
$ce->etype = $etype;
$ce->name = $name;
if ($ce->find(true)) {
return $ce->id;
}
+ $ce->active = 1;
$ce->display_name = $display_name === false ? $ce->name : $display_name;
return $ce->insert();
}
+ function merge($merge_to, $roo)
+ {
+ $affects = array();
+
+ $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database];
+
+ foreach($all_links as $tbl => $links) {
+ foreach($links as $col => $totbl_col) {
+ $to = explode(':', $totbl_col);
+ if ($to[0] != $this->tableName()) {
+ continue;
+ }
+
+ $affects[$tbl .'.' . $col] = true;
+ }
+ }
+
+ foreach($affects as $k => $true) {
+ $ka = explode('.', $k);
+
+ $chk = DB_DataObject::factory($ka[0]);
+
+ if (!is_a($chk,'DB_DataObject')) {
+ $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
+ }
+
+ $chk->{$ka[1]} = $this->id;
+
+ foreach ($chk->fetchAll() as $c){
+ $cc = clone ($c);
+ $c->{$ka[1]} = $merge_to;
+ $c->update($cc);
+ }
+ }
+
+ $this->delete();
+
+ $roo->jok('Merged');
+
+ }
+
}