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