3 * Table Definition for Projects
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Projects extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
31 /* the code above is auto generated do not remove the tag below */
33 function getProjectManagers()
35 $c = DB_DataObject::factory('Companies');
37 if (!$c->find(true)) {
43 $pd = DB_DataObject::factory('ProjectDirectory');
44 $pd->project_id = $this->id;
45 $pd->company_id = $c->id;
51 $pd->selectAdd('distinct (person_id)');
54 while ($pd->fetch()) {
55 $pmids[] = $pd->person_id;
59 $p = DB_DataObject::factory('Person');
60 $p->whereAdd('id IN ('. implode(',', $pmids) .')');
69 function toEventString() {
71 return ($c->id ? $c->toEventString() : '??'). ':' . $this->name;
75 * apply filter arguemnts
76 * @param $query - see below
77 * @param $authUser - authenticated user
81 * project_search = text string.
82 * project_indaterange - a/c/o
83 * project_filter = ALL || P,N,U ....
85 * // to get a users valid project list - just use array('query' => array('project_filter'=> 'ALL'));
89 function applyFilters($q, $au)
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%')");
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']) {
102 $this->whereAdd('Projects.close_date >= NOW()');
105 $this->whereAdd('Projects.close_date < NOW()');
110 if (empty($q['query']['project_filter']) || $q['query']['project_filter'] != 'ALL') {
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);
118 $this->whereAdd("Projects.type in ('". implode("','", $bits) . "')");
120 // user projects!!!! - make sure they can only see project they are suppsed to..
121 // only applies to document stuff..
123 //&& $au->hasPerm('Documents.Documents','S') << this is dependant on the doc modules
125 if (!$au->hasPerm('Core.Projects_All','S') ) {
129 $pr = DB_DataObject::factory('Projects');
130 $pr->whereAdd("Projects.type IN ('N','X')");
131 $prjs = $pr->fetchAll('id');
133 //DB_DataObject::debugLevel(1);
134 $pd = DB_DataObject::factory('ProjectDirectory');
135 $pd->joinAdd(DB_DataObject::factory('Projects'), 'LEFT');
136 $pd->whereAdd("Projects.type NOT IN ('N','X')");
137 $pd->person_id = $au->id;
139 $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
142 (Projects.id IN (".implode(',', $prjs)."))
145 $this->whereAdd("1=0"); // can see nothing!!!
149 if (!empty($q['query']['distinct_client_id'])) {
150 // DB_DataObjecT::debuglevel(1);
152 $this->selectAdd('distinct(client_id)');
153 $this->selectAs(DB_DataObject::factory('Companies'), 'client_id_%s','join_client_id_id');
154 $this->groupBy('client_id');
158 // this is clipping related.. -- we should have an API for addons like this.. (and docs)
169 function whereAddIn($key, $list, $type) {
171 foreach($list as $k) {
172 $ar[] = $type =='int' ? (int)$k : $this->escape($k);
177 $this->whereAdd("$key IN (". implode(',', $ar). ')');
182 if (empty($this->code)) {
183 $this->code = 'C' + $this->client_id + '-P' + $this->id;
188 function onUpdate($old)
191 if (empty($this->code)) {
192 $this->code = 'C' + $this->client_id + '-P' + $this->id;
196 if ($old->code == $this->code) {
201 $opts = PEAR::getStaticProperty('Pman', 'options');
203 $olddir = $opts['storedir'] . '/' . $old->code;
204 $newdir = $opts['storedir'] . '/' . $this->code;
205 if ( file_exists($olddir)) {
206 move ($olddir, $newdir);
214 if (!$this->prune) { // non-expiring..
218 $d = DB_DataObject::factory('Document');
219 $d->whereAdd("date_rec < NOW - INTERVAL {$this->expires} DAYS");
221 while ($d->fetch()) {
233 * our camp interface uses the format Cxxx-Pyyyyyy to refer to the project.
235 function getByCodeRef($str)
237 $bits = explode('-', $str);
238 if ((count($bits) != 2) || $bits[0][0] != 'C' || $bits[1][0] != 'P' ) {
241 $comp = substr($bits[0], 1);
242 $id = (int) substr($bits[1], 1);
243 return $id && $this->get($id);
248 function i18toArray($type, $str)
258 $u = DB_DataObject::factory('Person');
259 $au =$u->getAuthUser();
260 $lang = empty($au->lang ) ? 'en' : $au->lang;
261 $lbits = explode('_', strtoupper($lang));
262 // no validation here!!!!
263 require_once 'I18Nv2/Language.php';
264 require_once 'I18Nv2/Country.php';
265 $langs = new I18Nv2_Language($lbits[0]); // locale support not there??
266 $cts = new I18Nv2_Country($lbits[0]); // lo
269 $lk = $type == 'c' ? $cts : $langs;
270 $ar =explode(',', $str);
273 $ret[] = array('code'=>$k, 'title' => $lk->getName($k));
276 // work out locale...
284 function toRooArray($req= array()) {
285 $ret = parent::toArray();
287 $ret['countrylist'] = $this->I18toArray('c',$ret['countries']);
288 $ret['languagelist'] = $this->I18toArray('l',$ret['languages']);
291 function setFromRoo($q)
294 if (isset($q['open_date'])) {
295 $this->open_date = date('Y-m-d', strtotime(
296 implode('-', array_reverse(explode('/', $q['open_date'])))
304 * fetch a list of user projects.
305 * if you need to filter open/closed.. then add whereAdds before calling
307 function userProjects($au, $data='id') // COMPANY BASED!!!!
310 $id = (int) $au->company_id;
313 (client_id= $id) OR (agency_id= $id)
316 return empty($data) ? $this->fetchAll() :$this->fetchAll($data);
321 // DEPRICATED - use userProjects
323 function getUserProjects($au, $data='id') // COMPANY BASED!!!!
325 return $this->userProjects($au, $data);
333 $c = DB_DataObject::factory('Companies');
334 $c->get($this->client_id);
339 * check who is trying to access this. false == access denied..
341 function checkPerm($lvl, $au)
343 return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);