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