/* 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}%'
");
}
+ 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']);
+ $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 merge($id)
+ function merge($merge_to, $roo)
{
- $map = $this->links();
- print_R($map);exit;
+ $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');
+
}