DataObjects/Projects.php
[Pman.Core] / DataObjects / Projects.php
1 <?php
2 /**
3  * Table Definition for Projects
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Projects extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'Projects';                        // 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 $countries;                       // string(128)  not_null
26     public $languages;                       // string(128)  not_null
27     public $close_date;                      // date(10)  binary
28     public $agency_id;                       // int(11)  not_null
29
30     
31     /* the code above is auto generated do not remove the tag below */
32     ###END_AUTOCODE
33     function getProjectManagers()
34     {
35         $c = DB_DataObject::factory('Companies');
36         $c->isOwner = 1;
37         if (!$c->find(true)) {
38             return array();
39         }
40         
41         
42         $pmids = array();
43         $pd = DB_DataObject::factory('ProjectDirectory');
44         $pd->project_id = $this->id;
45         $pd->company_id = $c->id;
46         $pd->ispm = 1;
47         if (!$pd->count()) {
48             return array();
49         }
50         $pd->selectAdd();
51         $pd->selectAdd('distinct (person_id)');
52         
53         $pd->find();
54         while ($pd->fetch()) {
55             $pmids[] = $pd->person_id;
56             
57         }
58         $ret = array();
59         $p =  DB_DataObject::factory('Person');
60         $p->whereAdd('id IN ('. implode(',', $pmids) .')');
61         $p->find();
62         while ($p->fetch()) {
63             $ret[] = clone($p);
64         }
65         return $ret;
66         
67     }
68
69     function toEventString() {
70         $c = $this->client();
71         return ($c->id ? $c->toEventString() : '??'). ':' . $this->name;
72     }
73     
74     /**
75      * apply filter arguemnts
76      * @param $query - see below
77      * @param $authUser  - authenticated user
78      * 
79      * Query specs:
80      *   [query]
81      *       project_search = text string.
82      *       project_indaterange - a/c/o 
83      *       project_filter = ALL || P,N,U ....
84      * 
85      * // to get a users valid project list - just use array('query' => array('project_filter'=> 'ALL'));
86      * 
87      */
88     
89     function applyFilters($q, $au)
90     {
91          
92         if (!empty($q['query']['project_search'])) {
93             $s = $this->escape($q['query']['project_search']);
94             $this->whereAdd(" (Projects.code LIKE '$s%') OR (Projects.name LIKE '%$s%')");
95         }
96         // types of project to list ... - default is only the open ones...
97         if (!empty($q['query']['project_indaterange'])) {
98             switch($q['query']['project_indaterange']) {
99                 case 'A': // all
100                     break; 
101                 case 'C': // current
102                      $this->whereAdd('Projects.close_date >= NOW()');
103                     break;
104                 case 'O': // old
105                     $this->whereAdd('Projects.close_date < NOW()');
106                     break;
107                }
108         }
109         
110         if (empty($q['query']['project_filter'])  || $q['query']['project_filter'] != 'ALL') {
111             
112                
113             $pf = empty($q['query']['project_filter']) ? 'P,N,U' : $q['query']['project_filter'];
114             $bits= explode(',' ,$pf);
115             foreach($bits as $i=>$k) {
116                 $bits[$i] = $this->escape($k);
117             }
118             $this->whereAdd("Projects.type in ('". implode("','", $bits) . "')");
119         }
120          // user projects!!!! - make sure they can only see project they are suppsed to..
121          // only applies to document stuff..
122           
123         if (!$au->hasPerm('Core.Projects_All','S') &&
124             $au->hasPerm('Documents.Documents','S')) {
125             
126             
127             
128             $pr = DB_DataObject::factory('Projects');
129             $pr->whereAdd("Projects.type IN ('N','X')");
130             $prjs = $pr->fetchAll('id');
131             
132             
133             $pd = DB_DataObject::factory('ProjectDirectory');
134             $pd->joinAdd(DB_DataObject::factory('Projects'), 'LEFT');
135             $pd->whereAdd("Projects.type NOT IN ('N','X')");
136             $pd->person_id = $au->id;
137             
138             $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
139             if (count($prjs)) {
140                 $this->whereAdd("
141                      (Projects.id IN (".implode(',', $prjs).")) 
142                 ");
143             }  else {
144                 $this->whereAdd("1=0"); // can see nothing!!!
145             }
146         }
147         
148         if (!empty($q['query']['distinct_client_id'])) {
149           // DB_DataObjecT::debuglevel(1);
150             $this->selectAdd();
151             $this->selectAdd('distinct(client_id)');
152             $this->selectAs(DB_DataObject::factory('Companies'), 'client_id_%s','join_client_id_id');
153             $this->groupBy('client_id');
154              
155         }
156         
157         // this is clipping related..  -- we should have an API for addons like this.. (and docs)
158         
159         if ($au->company()->comptype == 'SUPPLIER') {
160             $pr = DB_DataObject::factory('CampaignAssign');
161             $pr->supplier_id = $au->company_id;
162             $prjs = $pr->fetchAll('project_id');
163              if (count($prjs)) {
164                 $this->whereAdd("
165                      (Projects.id IN (".implode(',', $prjs).")) 
166                 ");
167             }  else {
168                 $this->whereAdd("1=0"); // can see nothing!!!
169             }
170         }
171         if ($au->company()->comptype == 'CLIENT') {
172             $this->client_id = $au->company()->id; // can see nothing!!!
173             
174         }
175         
176         if (!empty($q['query']['project_member_of'])) {
177             DB_DataObject::debugLevel(1);
178             $do = DB_DataObject::Factory('ProjectDirectory');
179             $this->joinAdd('LEFT', $do);
180             $this->selectAdd('ProjectDirectory.id as is_member');
181                 
182                 
183             if (!empty($q['query']['project_member_filter'])) {
184                 $this->whereAdd('id IN (
185                     SELECT person_id from 
186                         ProjectDirectory 
187                     WHERE project_id = ' . ((int) $q['query']['project_member_of']) .')');
188                 // this is also a flag to return if they are a member..
189             
190         }
191         
192         
193                  
194         
195         
196         
197     }
198     function whereAddIn($key, $list, $type) {
199         $ar = array();
200         foreach($list as $k) {
201             $ar[] = $type =='int' ? (int)$k : $this->escape($k);
202         }
203         if (!$ar) {
204             return;
205         }
206         $this->whereAdd("$key IN (". implode(',', $ar). ')');
207     }
208     function onInsert()
209     {
210         $oo = clone($this);
211         if (empty($this->code)) {
212             $this->code = 'C' + $this->client_id + '-P' + $this->id;
213             $this->update($oo);
214         }
215     }
216     
217     function onUpdate($old)
218     {
219         $oo = clone($this);
220         if (empty($this->code)) {
221             $this->code = 'C' + $this->client_id + '-P' + $this->id;
222             $this->update($oo);
223         }
224         
225         if ($old->code == $this->code) {
226             return;
227         }
228         
229         
230         $opts = PEAR::getStaticProperty('Pman', 'options');
231         
232         $olddir =  $opts['storedir'] . '/' . $old->code;
233         $newdir =  $opts['storedir'] . '/' . $this->code;
234         if ( file_exists($olddir)) {
235             move ($olddir, $newdir);
236         }
237          
238         
239     }
240     
241     function prune()
242     {
243         if (!$this->prune) { // non-expiring..
244             return;
245         }
246         
247         $d = DB_DataObject::factory('Document');
248         $d->whereAdd("date_rec < NOW - INTERVAL {$this->expires} DAYS"); 
249         $d->find();
250         while ($d->fetch()) {
251             $d->prune();
252         }
253         
254         
255         
256         
257         
258         
259         
260     }
261     /**
262      * our camp interface uses the format Cxxx-Pyyyyyy to refer to the project.
263      */
264     function getByCodeRef($str)
265     {
266         $bits = explode('-', $str);
267         if ((count($bits) != 2) || $bits[0][0] != 'C' ||  $bits[1][0] != 'P' ) {
268             return false;
269         }
270         $comp = substr($bits[0], 1);
271         $id = (int) substr($bits[1], 1);
272         return $id && $this->get($id);
273         
274     }
275     
276     
277     function i18toArray($type, $str) 
278     {
279         if (empty($str)) {
280             return array();
281         }
282         static $au;
283         static $langs;
284         static $cts;
285         
286         if (!$au) {
287             $u = DB_DataObject::factory('Person');
288             $au =$u->getAuthUser();
289             $lang = empty($au->lang ) ? 'en' : $au->lang;
290             $lbits = explode('_', strtoupper($lang));
291             // no validation here!!!!
292             require_once 'I18Nv2/Language.php';
293             require_once 'I18Nv2/Country.php';
294             $langs = new I18Nv2_Language($lbits[0]); // locale support not there??
295             $cts = new I18Nv2_Country($lbits[0]); // lo
296             
297         }
298         $lk = $type == 'c' ? $cts : $langs;
299         $ar  =explode(',', $str);
300         $ret = array();
301         foreach($ar as $k) {
302             $ret[] = array('code'=>$k, 'title' => $lk->getName($k));
303         }
304         return $ret;
305         // work out locale...
306         
307         
308         
309         
310     }
311     
312     
313     function toRooArray($req= array()) {
314         $ret = parent::toArray();
315         // sor tout 
316         $ret['countrylist'] = $this->I18toArray('c',$ret['countries']);
317         $ret['languagelist'] = $this->I18toArray('l',$ret['languages']);
318         return $ret;
319     }
320     function setFromRoo($q) 
321     {
322         $this->setFrom($q);
323         if (isset($q['open_date'])) {
324             $this->open_date = date('Y-m-d', strtotime(
325                 implode('-', array_reverse(explode('/', $q['open_date'])))
326             ));
327         }
328         return true;
329     }
330     
331     function fetchAll($k= false) {
332         if ($k !== false) {
333             $this->selectAdd();
334             $this->selectAdd($k);
335         }
336         
337         $this->find();
338         $ret = array();
339         while ($this->fetch()) {
340             $ret[] = $k === false ? clone($this) : $this->$k;
341         }
342         return $ret;
343          
344     }
345     
346     /**
347      * fetch a list of user projects.
348      * if you need to filter open/closed.. then add whereAdds before calling
349      */
350     function getUserProjects($au, $data='id') // COMPANY BASED!!!!
351     {
352         $id = (int) $au->company_id;
353         $this->whereAdd("
354             (client_id= $id) OR (agency_id= $id)
355         ");
356         if (!empty($data)) {
357             $this->selectAdd();
358             $this->selectAdd($data);
359         }
360         $this->find();
361         $ret = array();
362         while ($this->fetch()) {
363             $ret[] = empty($data) ? clone($this) : $this->$data;
364         }
365         return $ret;
366             
367     }
368     
369     
370     function client()
371     {
372         $c = DB_DataObject::factory('Companies');
373         $c->get($this->client_id);
374         return $c;
375     }
376     
377     /**
378      * check who is trying to access this. false == access denied..
379      */
380     function checkPerm($lvl, $au) 
381     {
382         return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);
383     }
384 }