DataObjects/Core_watch.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 $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('Companies');
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('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->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         if (!empty($q['query']['project_search'])) {
94             $s = $this->escape($q['query']['project_search']);
95             $this->whereAdd(" (Projects.code LIKE '$s%') OR (Projects.name LIKE '%$s%')");
96         }
97         // types of project to list ... - default is only the open ones...
98         if (!empty($q['query']['project_indaterange'])) {
99             switch($q['query']['project_indaterange']) {
100                 case 'A': // all
101                     break; 
102                 case 'C': // current
103                      $this->whereAdd('Projects.close_date >= NOW()');
104                     break;
105                 case 'O': // old
106                     $this->whereAdd('Projects.close_date < NOW()');
107                     break;
108                }
109         }
110         
111         if (empty($q['query']['project_filter'])  || $q['query']['project_filter'] != 'ALL') {
112             
113                
114             $pf = empty($q['query']['project_filter']) ? 'P,N,U' : $q['query']['project_filter'];
115             $bits= explode(',' ,$pf);
116             foreach($bits as $i=>$k) {
117                 $bits[$i] = $this->escape($k);
118             }
119             $this->whereAdd("Projects.type in ('". implode("','", $bits) . "')");
120         }
121          // user projects!!!! - make sure they can only see project they are suppsed to..
122          // only applies to document stuff..
123         
124         //&& $au->hasPerm('Documents.Documents','S') << this is dependant on the doc modules
125           
126         if (!$au->hasPerm('Core.Projects_All','S') ) {
127             
128             
129             
130             $pr = DB_DataObject::factory('Projects');
131             $pr->whereAdd("Projects.type IN ('N','X')");
132             $prjs = $pr->fetchAll('id');
133             
134             //DB_DataObject::debugLevel(1);
135             $pd = DB_DataObject::factory('ProjectDirectory');
136             $pd->joinAdd(DB_DataObject::factory('Projects'), 'LEFT');
137             $pd->whereAdd("Projects.type NOT IN ('N','X')");
138             $pd->person_id = $au->id;
139             
140             $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
141             if (count($prjs)) {
142                 $this->whereAdd("
143                      (Projects.id IN (".implode(',', $prjs).")) 
144                 ");
145             }  else {
146                 $this->whereAdd("1=0"); // can see nothing!!!
147             }
148         }
149         
150         if (!empty($q['query']['distinct_client_id'])) {
151           // DB_DataObjecT::debuglevel(1);
152             $this->selectAdd();
153             $this->selectAdd('distinct(client_id)');
154             $this->selectAs(DB_DataObject::factory('Companies'), 'client_id_%s','join_client_id_id');
155             $this->groupBy('client_id');
156              
157         }
158         
159         // this is clipping related..  -- we should have an API for addons like this.. (and docs)
160         
161        
162         
163         
164         
165                  
166         
167         
168         
169     }
170     function whereAddIn($key, $list, $type) {
171         $ar = array();
172         foreach($list as $k) {
173             $ar[] = $type =='int' ? (int)$k : $this->escape($k);
174         }
175         if (!$ar) {
176             return;
177         }
178         $this->whereAdd("$key IN (". implode(',', $ar). ')');
179     }
180     function onInsert()
181     {
182         $oo = clone($this);
183         if (empty($this->code)) {
184             $this->code = 'C' + $this->client_id + '-P' + $this->id;
185             $dt = new DateTime();
186             $this->updated_dt = $dt->format('Y-m-d H:i:s');
187             $this->update($oo);
188         }
189     }
190     
191     function onUpdate($old)
192     {
193         $oo = clone($this);
194         if (empty($this->code)) {
195             $this->code = 'C' + $this->client_id + '-P' + $this->id;
196             $dt = new DateTime();
197             $this->updated_dt = $dt->format('Y-m-d H:i:s');
198             $this->update($oo);
199         }
200         
201         if ($old->code == $this->code) {
202             return;
203         }
204         
205         
206         $opts = PEAR::getStaticProperty('Pman', 'options');
207         
208         $olddir =  $opts['storedir'] . '/' . $old->code;
209         $newdir =  $opts['storedir'] . '/' . $this->code;
210         if ( file_exists($olddir)) {
211             move($olddir, $newdir);
212         }
213          
214         
215     }
216     
217     function prune()
218     {
219         if (!$this->prune) { // non-expiring..
220             return;
221         }
222         
223         $d = DB_DataObject::factory('Document');
224         $d->whereAdd("date_rec < NOW - INTERVAL {$this->expires} DAYS"); 
225         $d->find();
226         while ($d->fetch()) {
227             $d->prune();
228         }
229         
230         
231         
232         
233         
234         
235         
236     }
237     /**
238      * our camp interface uses the format Cxxx-Pyyyyyy to refer to the project.
239      */
240     function getByCodeRef($str)
241     {
242         $bits = explode('-', $str);
243         if ((count($bits) != 2) || $bits[0][0] != 'C' ||  $bits[1][0] != 'P' ) {
244             return false;
245         }
246         $comp = substr($bits[0], 1);
247         $id = (int) substr($bits[1], 1);
248         return $id && $this->get($id);
249         
250     }
251     
252    
253     
254     
255    
256     function setFromRoo($q) 
257     {
258         $this->setFrom($q);
259         if (isset($q['open_date'])) {
260             $this->open_date = date('Y-m-d', strtotime(
261                 implode('-', array_reverse(explode('/', $q['open_date'])))
262             ));
263         }
264         return true;
265     }
266     
267     
268     /**
269      * fetch a list of user projects.
270      * if you need to filter open/closed.. then add whereAdds before calling
271      */
272     function userProjects($au, $data='id') // COMPANY BASED!!!!
273     {
274         
275         $id = (int) $au->company_id;
276         
277         $this->whereAdd("
278             (client_id= $id) OR (agency_id= $id)
279         ");
280         
281         return empty($data) ? $this->fetchAll() :$this->fetchAll($data); 
282          
283             
284     }
285     
286     // DEPRICATED - use userProjects
287     
288     function getUserProjects($au, $data='id') // COMPANY BASED!!!!
289     {
290         return $this->userProjects($au, $data);
291          
292             
293     }
294     
295     
296     function client()
297     {
298         $c = DB_DataObject::factory('Companies');
299         $c->get($this->client_id);
300         return $c;
301     }
302     
303     /**
304      * check who is trying to access this. false == access denied..
305      */
306     function checkPerm($lvl, $au) 
307     {
308         return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);
309     }
310     
311 }