DataObjects/Core_company.php
[Pman.Core] / DataObjects / Core_company.php
index bbabcc6..b913629 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Table Definition for Companies
  */
-require_once 'DB/DataObject.php';
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
 
 class Pman_Core_DataObjects_Core_Company extends DB_DataObject 
 {
@@ -245,14 +245,22 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject
     function beforeInsert($q, $roo)
     {
         if(!empty($q['_check_name'])){
-            $this->checkName($roo);
+            if($this->checkName()){
+                $roo->jok('OK');
+            }
+            
+            $roo->jerr('EXIST');
         }
     }
     
     function beforeUpdate($old, $q,$roo)
     {
         if(!empty($q['_check_name'])){
-            $this->checkName($roo);
+            if($this->checkName()){
+                $roo->jok('OK');
+            }
+            
+            $roo->jerr('EXIST');
         }
         
         if(!empty($q['_merge_id'])){
@@ -423,7 +431,7 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject
         $companies->insert();
         $companies->onInsert(array(), $roo);
     }
-    function lookupOwner()
+    static function lookupOwner()
     {
         $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER'  );
         $companies = DB_DataObject::factory('core_company');
@@ -438,7 +446,7 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject
     {
         $affects  = array();
         
-        $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database];
+        $all_links = method_exists($this, 'databaseLinks') ? $x->databaseLinks() : $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database];
         
         foreach($all_links as $tbl => $links) {
             foreach($links as $col => $totbl_col) {
@@ -474,4 +482,22 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject
         $roo->jok('Merged');
         
     }
+    
+    function checkName()
+    {
+        $company = DB_DataObject::factory('core_company');
+        $company->setFrom(array(
+            'name' => $this->name
+        ));
+        
+        if(!empty($this->id)){
+            $company->whereAdd("id != {$this->id}");
+        }
+        
+        if(!$company->find(true)){
+            return true;
+        }
+        
+        return false;
+    }
 }