DataObjects/Core_company.php
[Pman.Core] / DataObjects / Core_company.php
index c709f3a..a1de8f5 100644 (file)
@@ -247,6 +247,14 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject
     
     function beforeUpdate($old, $q,$roo)
     {
+        if(!empty($q['_check_name'])){
+            $this->checkName($roo);
+        }
+        
+        if(!empty($q['_merge_id'])){
+            $this->merge($q['_merge_id'], $roo);
+        }
+        
         if(!empty($this->is_system) && 
             ($old->code != $this->code  ) // used to be not allowed to change name..
         ){
@@ -421,4 +429,45 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject
         }
         return false;
     }
+    
+    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');
+        
+    }
 }