fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_project.php
1 <?php
2 /**
3  * Table Definition for Projects
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Core_project extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'core_project';            // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $name;                            // string(254)  not_null
15     public $remarks;                         // blob(65535)  not_null blob
16     public $owner_id;                        // int(11)  
17     public $code;                            // string(32)  not_null multiple_key
18     public $active;                          // int(11)  
19     public $type;                            // string(1)  not_null
20     public $client_id;                       // int(11)  not_null
21     public $team_id;                         // int(11)  not_null
22     public $file_location;                   // string(254)  not_null
23     public $open_date;                       // date(10)  binary
24     public $open_by;                         // int(11)  not_null
25     public $close_date;                      // date(10)  binary
26     public $countries;                       // string(128)  not_null
27     public $languages;                       // string(128)  not_null
28     public $agency_id;                       // int(11)  not_null
29     public $updated_dt;                      // datetime(19)  not_null binary
30     public $deleted_by;                      // INT
31     public $deleted_dt;                      // datetime(19)  not_null binary
32
33     
34     /* the code above is auto generated do not remove the tag below */
35     ###END_AUTOCODE
36     function getProjectManagers()
37     {
38         $c = DB_DataObject::factory('core_company');
39         $c->isOwner = 1;
40         if (!$c->find(true)) {
41             return array();
42         }
43         
44         
45         $pmids = array();
46         $pd = DB_DataObject::factory('ProjectDirectory');
47         $pd->project_id = $this->id;
48         $pd->company_id = $c->id;
49         $pd->ispm = 1;
50         if (!$pd->count()) {
51             return array();
52         }
53         $pd->selectAdd();
54         $pd->selectAdd('distinct (person_id)');
55         
56         $pd->find();
57         while ($pd->fetch()) {
58             $pmids[] = $pd->person_id;
59             
60         }
61         $ret = array();
62         $p =  DB_DataObject::factory('core_person');
63         $p->whereAdd('id IN ('. implode(',', $pmids) .')');
64         $p->find();
65         while ($p->fetch()) {
66             $ret[] = clone($p);
67         }
68         return $ret;
69         
70     }
71
72     function toEventString() {
73         $c = $this->client();
74         return ($c && $c->id ? $c->toEventString() : '??'). ':' . $this->name;
75     }
76     
77     /**
78      * apply filter arguemnts
79      * @param $query - see below
80      * @param $authUser  - authenticated user
81      * 
82      * Query specs:
83      *   [query]
84      *       project_search = text string.
85      *       project_indaterange - a/c/o 
86      *       project_filter = ALL || P,N,U ....
87      * 
88      * // to get a users valid project list - just use array('query' => array('project_filter'=> 'ALL'));
89      * 
90      */
91     
92     function applyFilters($q, $au, $roo)
93     {
94         if (empty($q['_show_deleted']) && empty($q['_is_update_request'])) {
95             $this->whereAdd('core_project.deleted_by = 0');
96         }
97         
98         $tn = $this->tableName();
99         if (!empty($q['query']['project_search'])) {
100             $s = $this->escape($q['query']['project_search']);
101             $this->whereAdd(" ({$tn}.code LIKE '$s%')
102                             OR
103                             ({$tn}.name LIKE '%$s%')
104                             OR
105                             join_client_id_id.name LIKE '%$s%'
106                 ");
107         }
108         // types of project to list ... - default is only the open ones...
109         if (!empty($q['query']['project_indaterange'])) {
110             switch($q['query']['project_indaterange']) {
111                 case 'A': // all
112                     break; 
113                 case 'C': // current
114                      $this->whereAdd("{$tn}.close_date is NULL OR {$tn}.close_date >= NOW()");
115                     break;
116                 case 'O': // old
117                     $this->whereAdd("{$tn}.close_date < NOW()");
118                     break;
119                }
120         }
121         
122         if (empty($q['_is_update_request']) &&
123             
124             (empty($q['query']['project_filter'])  || $q['query']['project_filter'] != 'ALL')) {
125             
126                
127             $pf = empty($q['query']['project_filter']) ? 'P,N,U' : $q['query']['project_filter'];
128         
129          
130         
131             $this->whereAddIn("{$tn}.type", explode(',', $pf), 'string');
132         }
133          // user projects!!!! - make sure they can only see project they are suppsed to..
134          // only applies to document stuff..
135         
136         //&& $au->hasPerm('Documents.Documents','S') << this is dependant on the doc modules
137           
138         if (php_sapi_name() != 'cli' && !$au->hasPerm('Core.Projects_All','S') ) {
139             
140             
141             
142             $pr = DB_DataObject::factory($tn);
143             $pr->whereAdd("{$tn}.type IN ('N','X')");
144             $prjs = $pr->fetchAll('id');
145             
146             //DB_DataObject::debugLevel(1);
147             $pd = DB_DataObject::factory('ProjectDirectory');
148             $pd->joinAdd(DB_DataObject::factory($tn), 'LEFT');
149             $pd->whereAdd("{$tn}.type NOT IN ('N','X')");
150             $pd->person_id = $au->id;
151             
152             $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
153             if (count($prjs)) {
154                 $this->whereAdd("
155                      ({$tn}.id IN (".implode(',', $prjs).")) 
156                 ");
157             }  else {
158                 $this->whereAdd("1=0"); // can see nothing!!!
159             }
160         }
161         
162         if (!empty($q['query']['distinct_client_id'])) {
163           // DB_DataObjecT::debuglevel(1);
164             $this->selectAdd();
165             $this->selectAdd('distinct(client_id)');
166             $this->selectAs(DB_DataObject::factory('core_company'), 'client_id_%s','join_client_id_id');
167             $this->groupBy('client_id');
168              
169         }
170         
171         // this is clipping related..  -- we should have an API for addons like this.. (and docs)
172         
173        
174         
175         
176         
177                  
178         
179         
180         
181     }
182     
183     function beforeUpdate($old, $q, $roo)
184     {
185         if (!empty($q['_flag_deleted'])) {
186             //DB_DataObject::DebugLevel(1);
187             $this->deleted_by = $roo->getAuthUser()->id;
188             $this->deleted_dt = date("Y-m-d H:i:s");
189         }
190         if (!empty($q['_flag_undeleted'])) {
191             $this->deleted_by = 0;
192             $this->deleted_dt = '1000-01-01 00:00:00';
193         }
194     }
195     
196  
197     function onInsert($request,$roo,$event)
198     {
199         $oo = clone($this);
200         if (empty($this->code)) {
201             $this->code = 'C' + $this->client_id + '-P' + $this->id;
202             $dt = new DateTime();
203             $this->updated_dt = $dt->format('Y-m-d H:i:s');
204             $this->update($oo);
205         }
206     }
207     
208     function onUpdate($old, $request, $roo,$event)
209     {
210         $oo = clone($this);
211         if (empty($this->code)) {
212             $this->code = 'C' + $this->client_id + '-P' + $this->id;
213             $dt = new DateTime();
214             $this->updated_dt = $dt->format('Y-m-d H:i:s');
215             $this->update($oo);
216         }
217         
218         if ($old->code == $this->code) {
219             return;
220         }
221         
222         $opts = HTML_FlexyFramework::get()->Pman;
223         
224         $olddir =  $opts['storedir'] . '/' . $old->code;
225         $newdir =  $opts['storedir'] . '/' . $this->code;
226         if ( file_exists($olddir)) {
227             rename($olddir, $newdir);
228         }
229          
230         
231     }
232     
233     function prune()
234     {
235         if (!$this->prune) { // non-expiring..
236             return;
237         }
238         
239         $d = DB_DataObject::factory('Document');
240         $d->whereAdd("date_rec < NOW - INTERVAL {$this->expires} DAYS"); 
241         $d->find();
242         while ($d->fetch()) {
243             $d->prune();
244         }
245         
246         
247         
248         
249         
250         
251         
252     }
253     /**
254      * our camp interface uses the format Cxxx-Pyyyyyy to refer to the project.
255      */
256     function getByCodeRef($str)
257     {
258         $bits = explode('-', $str);
259         if ((count($bits) != 2) || $bits[0][0] != 'C' ||  $bits[1][0] != 'P' ) {
260             return false;
261         }
262         $comp = substr($bits[0], 1);
263         $id = (int) substr($bits[1], 1);
264         return $id && $this->get($id);
265         
266     }
267     
268    
269     
270     
271    
272     function setFromRoo($q) 
273     {
274         $this->setFrom($q);
275         if (isset($q['open_date'])) {
276             $this->open_date = date('Y-m-d', strtotime(
277                 implode('-', array_reverse(explode('/', $q['open_date'])))
278             ));
279         }
280         return true;
281     }
282     
283     
284     /**
285      * fetch a list of user projects.
286      * if you need to filter open/closed.. then add whereAdds before calling
287      */
288     function userProjects($au, $data='id') // COMPANY BASED!!!!
289     {
290         
291         $id = (int) $au->company_id;
292         
293         $this->whereAdd("
294             (client_id= $id) OR (agency_id= $id)
295         ");
296         
297         return empty($data) ? $this->fetchAll() :$this->fetchAll($data); 
298          
299             
300     }
301     
302     
303     
304     function client()
305     {
306         if (!$this->client_id) {
307             return false;
308         }
309         $c = DB_DataObject::factory('core_company');
310         $c->get($this->client_id);
311         return $c;
312     }
313     
314     
315     
316     function team()
317     {
318         $c = DB_DataObject::factory('core_group');
319         $c->get($this->team_id);
320         return $c;
321     }
322     
323     
324     
325     // DEPRICATED - use userProjects
326     
327     function getUserProjects($au, $data='id') // COMPANY BASED!!!!
328     {
329         return $this->userProjects($au, $data);
330          
331             
332     }
333     
334     
335     /**
336      * check who is trying to access this. false == access denied..
337      */
338     function checkPerm($lvl, $au) 
339     {
340         return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);
341     }
342     
343     static $cache = array();
344     function cacheLoad($id)
345     {
346         if (isset(self::$cache[$id])) {
347             return self::$cache[$id];
348         }
349         $n = $this->factorySelf();
350         $n->get($id);
351         $n->cacheSave();
352         return $n;
353     }
354     
355     function cacheSave()
356     {
357         self::$cache[$this->id] = $this;
358     }
359     
360     
361     
362 }