DataObjects/Core_notify.php
[Pman.Core] / DataObjects / Companies.php
index 6ddcbca..163e131 100644 (file)
@@ -11,7 +11,7 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
 
     public $__table = 'Companies';                       // table name
     public $code;                            // string(32)  not_null
-    public $name;                            // string(128)  multiple_key
+    public $name;                            // string(128)  
     public $remarks;                         // blob(65535)  blob
     public $owner_id;                        // int(11)  not_null
     public $address;                         // blob(65535)  blob
@@ -23,7 +23,6 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
     public $logo_id;                         // int(11)  not_null
     public $background_color;                // string(8)  not_null
     public $comptype;                        // string(8)  not_null
-    public $ava_craft;                       // string(254)  
     public $url;                             // string(254)  not_null
     public $main_office_id;                  // int(11)  not_null
     public $created_by;                      // int(11)  not_null
@@ -31,6 +30,9 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
     public $updated_by;                      // int(11)  not_null
     public $updated_dt;                      // datetime(19)  not_null binary
     public $passwd;                          // string(64)  not_null
+    public $dispatch_port;                   // string(255)  not_null
+    public $province;                        // string(255)  not_null
+    public $country;                         // string(4)  not_null
 
     
     /* the code above is auto generated do not remove the tag below */
@@ -38,8 +40,10 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
     
     function applyFilters($q, $au)
     {
+        $tn = $this->tableName();
+        //DB_DataObject::debugLevel(1);
         $x = DB_DataObject::factory('Companies');
-        $x->isOwner = 1;
+        $x->comptype= 'OWNER';
         $x->find(true);
         
         if (!empty($q['query']['company_project_id'])) {
@@ -66,13 +70,38 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
             $this->whereAdd("Companies.id IN (
                 SELECT distinct(company_id) FROM ProjectDirectory where project_id IN ($pids)
             ) $add" );
+             
+        }
+        if (!empty($q['query']['comptype'])) {
+           
+            $this->whereAddIn('comptype', explode(',', $q['query']['comptype']), 'string');
             
-           // DB_DataObject::debugLevel(1);
+        }
+        
+        // depricated - should be moved to module specific (texon afair)
+        
+         if (!empty($q['query']['province'])) {
+             $prov = $this->escape($q['query']['province']);
+            $this->whereAdd("province LIKE '$prov%'");
             
             
         }
+        // ADD comptype_display name.. = for combos..
+        $this->selectAdd("
+            (SELECT display_name
+                FROM
+                    core_enum
+                WHERE
+                    etype='comptype'
+                    AND
+                    name={$tn}.comptype
+                LIMIT 1
+                ) as comptype_display_name
+        ");
         
+         
     }
+    
     function toEventString() {
         return $this->name;
     }
@@ -223,6 +252,14 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
      */
     function checkPerm($lvl, $au, $changes = false) 
     {
+        
+        // do we have an empty system..
+        if ($au && $au->id == -1) {
+            return true;
+        }
+        
+        
+        
         if ($au->company()->comptype != 'OWNER') {
             
             // hacking!
@@ -233,39 +270,68 @@ class Pman_Core_DataObjects_Companies extends DB_DataObject
             return $this->id == $au->company_id;
         }
         
-        return $au->hasPerm("Core.".$this->tableName(), $lvl);    
-    } 
-    function whereAddIn($key, $list, $type= 'int') 
+        return $au->hasPerm("Core.Companies", $lvl);    
+    }
+    
+    function logoImageToHTML($size)
     {
-        $ar = array();
-        foreach($list as $k) {
-            $ar[] = $type =='int' ? (int)$k : $this->escape($k);
+        $i = DB_DataObject::factory('Images');
+        if (!$this->logo_id || !$i->get($this->logo_id)) {
+            return '';
         }
-        if (!$ar) {
-            return;
+        return $i->toHTML($size);
+        
+    }
+     function firstImage($filter='image/%')
+    {
+        $i = DB_DataObject::factory('Images');
+        //DB_DataObject::debugLevel(1);
+        $im = $i->gather($this, $filter);
+        if (empty($im)) {
+            return false;
         }
-        return $this->whereAdd("$key IN (". implode(',', $ar). ')');
+        return $im[0];
     }
-    function fetchAll($k= false, $v = false) 
+    
+    function firstImageTag($size =-1, $base="/Images/Thumb", $filter='image/%')
     {
-        if ($k !== false) {
-            $this->selectAdd();
-            $this->selectAdd($k);
-            if ($v !== false) {
-                $this->selectAdd($v);
-            }
+        $fm = $this->firstImage($filter);
+         if (empty($fm)) {
+            return '';
         }
-        
-        $this->find();
-        $ret = array();
-        while ($this->fetch()) {
-            if ($v !== false) {
-                $ret[$this->$k] = $this->$v;
-                continue;
-            }
-            $ret[] = $k === false ? clone($this) : $this->$k;
+        return $fm->toHTML($size, $base);
+    }
+    
+    function toRooSingleArray($authUser, $request)
+    {
+        $ret = $this->toArray();
+       // DB_DataObject::debugLevel(1);
+        // get the comptype display
+        $e = DB_DataObject::Factory('core_enum');
+        $e->etype = 'COMPTYPE';
+        $e->name = $this->comptype;
+        $ret['comptype_display'] = $ret['comptype'];
+        if ($e->find(true) && !empty($e->name_display)) {
+            $ret['comptype_display'] = $e->name_display;
         }
+        
+        
         return $ret;
-         
     }
+    
+    function initCompanies($roo, $name, $type)
+    {
+        $companies = DB_DataObject::factory('companies');
+        $companies->setFrom(array(
+            'name' => $name,
+            'comptype' => $type,
+            'background_color' => '',
+            'created_dt' => $this->sqlValue('NOW()'),
+            'updated_dt' => $this->sqlValue('NOW()')
+        ));
+        
+        $companies->insert();
+        $companies->onInsert(array(), $roo);
+    }
+    
 }