DataObjects/Core_domain.php
[Pman.Core] / DataObjects / Office.php
index 31e2eb9..75d47c2 100644 (file)
@@ -26,37 +26,12 @@ class Pman_Core_DataObjects_Office extends DB_DataObject
     ###END_AUTOCODE
     function applyFilters($q, $au)
     {
-        DB_DataObject::debugLevel(1);
-        
-        if(!empty($q['_join_country_with_person'])){
-            $tn = $this->tableName();
-            //$this->selectAdd(" i18n_translate('c' , 'CN', 'en') as country_name");
-            $p = DB_DataObject::factory('Person');
-            $p->whereAdd("office_id > 0");
-            $p->selectAdd();
-            $p->selectAdd("DISTINCT(office_id) as office_id");
-            $officeIds = $p->fetchAll('office_id');
-            $this->whereAddIn('Office.id', $officeIds, 'INT');
-            
-            $this->joinAddCountryWithPerson();
-        }
+        //DB_DataObject::debugLevel(1);
+        $tn  = $this->tableName();
+        $this->selectAdd("   i18n_translate('c' , {$tn}.country, 'en') as office_id_country_name");
     }
     
-    function joinAddCountryWithPerson()
-    {
-        $this->_join .= "
-            LEFT JOIN
-                i18n AS join_country
-            ON
-                (join_country.lkey = Office.country AND join_country.inlang = 'en')
-            GROUP BY
-                Office.country
-                WHERE
-                Office.id = 2
-        ";
-        $item = DB_DataObject::Factory('I18n');
-        $this->selectAs($item, 'country_id_%s', 'join_country');
-    }
+    
     function toEventString() {
         return $this->name;
     }
@@ -66,5 +41,40 @@ class Pman_Core_DataObjects_Office extends DB_DataObject
     function checkPerm($lvl, $au) 
     {
         return $au->hasPerm("Core.Offices", $lvl);    
-    } 
+    }
+    
+    function company()
+    {
+        $c = DB_DataObject::Factory('Companies');
+        $c->get($this->company_id);
+        return $c;
+        
+    }
+    
+    function beforeDelete($dependants_array, $roo)
+    {
+        if (count($dependants_array) != 1) {
+            //$roo->jerr("more than one dependant type....");
+            return true; // standard error message.
+        }
+        
+        $p = DB_DAtaObject::Factory('Person');
+        
+        if (!is_a($dependants_array[0], get_class($p))) {
+            $roo->jerr("dep is not a person..");
+            return true;
+        }
+        
+        $p->office_id = $this->id;
+        if ($p->count() > 1) {
+            return true; // default err..
+        }
+        $p->find(true);
+        $pp = clone($p);
+        $p->office_id = 0;
+        $p->update($pp);
+        return true;
+    }
+    
+    
 }
\ No newline at end of file