DataObjects/Core_domain.php
[Pman.Core] / DataObjects / Companies.php
index c384a7a..7aebca3 100644 (file)
@@ -33,13 +33,16 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
     public $dispatch_port;                   // string(255)  not_null
     public $province;                        // string(255)  not_null
     public $country;                         // string(4)  not_null
-
+    public $is_system;                       // int(2)
     
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
     
     function applyFilters($q, $au)
     {
+       $tn = $this->tableName();
+        $this->selectAdd("i18n_translate('c' , {$tn}.country, 'en') as country_display_name ");
+      
         $tn = $this->tableName();
         //DB_DataObject::debugLevel(1);
         $x = DB_DataObject::factory('Companies');
@@ -49,10 +52,10 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
         if (!empty($q['query']['company_project_id'])) {
             $add = '';
             if (!empty($q['query']['company_include_self'])) {
-                $add = ' OR Companies.id = ' . $x->id;
+                $add = " OR {$tn}.id = {$x->id}";
             }
             if (!empty($q['query']['company_not_self'])) {
-                $add = ' AND Companies.id != ' . $x->id;
+                $add = " AND {$tn}.id != {$x->id}";
             }
             $pids = array();
             $pid = $q['query']['company_project_id'];
@@ -67,7 +70,7 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
             
             
             $pids = implode(',', $pids);
-            $this->whereAdd("Companies.id IN (
+            $this->whereAdd("{$tn}.id IN (
                 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
             ) $add" );
              
@@ -99,7 +102,18 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
                 ) as comptype_display_name
         ");
         
-         
+        if(!empty($q['search']['name'])){
+            $s = $this->escape($q['search']['name']);
+            $this->whereAdd("
+                {$tn}.name LIKE '%$s%'
+            ");
+        }
+          if(!empty($q['search']['name_starts'])){
+            $s = $this->escape($q['search']['name_starts']);
+            $this->whereAdd("
+                {$tn}.name LIKE '$s%'
+            ");
+        }
     }
     
     function toEventString() {
@@ -231,10 +245,23 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
         
     }
     
-    function beforeDelete()
+    function beforeUpdate($old, $q,$roo)
+    {
+        if(!empty($this->is_system) && 
+            ($old->code != $this->code  ) // used to be not allowed to change name..
+        ){
+            $roo->jerr('This company is not allow to editing Ref. or Company Name...');
+        }
+    }
+    
+    function beforeDelete($req, $roo)
     {
         // should check for members....
-        
+        if(!empty($this->is_system) && 
+            ($old->code != $this->code || $old->name != $this->name)
+        ){
+            $roo->jerr('This company is not allow to delete');
+        }
         $img = DB_DataObject::factory('Images');
         $img->ontable = 'Companies';
         $img->onid = $this->id;
@@ -310,7 +337,7 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
         $e = DB_DataObject::Factory('core_enum')->lookupObject('COMPTYPE', $this->comptype);
         
         $ret['comptype_display'] = $ret['comptype'];
-        if ($e->find(true) && !empty($e->name_display)) {
+        if ($e   && !empty($e->name_display)) {
             $ret['comptype_display'] = $e->name_display;
         }
         
@@ -332,10 +359,11 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
         
         foreach($data as $d){
             $com = DB_DataObject::factory($tn);
-            $com->setForm($d);
+            $com->setFrom($d);
             if(!$com->find(true)){
                 $com->created_dt = Date('Y-m-d H:i:s');
                 $com->updated_dt = Date('Y-m-d H:i:s');
+                $com->is_system = 1;// new column.. block the user changing the code and name..
                 $com->insert();
             }
         }
@@ -383,9 +411,9 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
         $companies->insert();
         $companies->onInsert(array(), $roo);
     }
-    function    lookupOwner()
+    function lookupOwner()
     {
-        $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', $ctype  );
+        $enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER'  );
         $companies = DB_DataObject::factory('companies');
         $companies->comptype_id = $enum;
         if ($companies->find(true)) {