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 $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
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() {
74 * apply filter arguemnts
75 * @param $query - see below
76 * @param $authUser - authenticated user
80 * project_search = text string.
81 * project_indaterange - a/c/o
82 * project_filter = ALL || P,N,U ....
84 * // to get a users valid project list - just use array('query' => array('project_filter'=> 'ALL'));
88 function applyFilters($q, $au)
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%')");
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']) {
101 $this->whereAdd('Projects.close_date >= NOW()');
104 $this->whereAdd('Projects.close_date < NOW()');
109 if (empty($q['query']['project_filter']) || $q['query']['project_filter'] != 'ALL') {
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);
117 $this->whereAdd("Projects.type in ('". implode("','", $bits) . "')");
119 // user projects!!!! - make sure they can only see project they are suppsed to..
120 // only applies to document stuff..
122 if (!$au->hasPerm('Core.Projects_All','S') &&
123 $au->hasPerm('Documents.Documents','S')) {
127 $pr = DB_DataObject::factory('Projects');
128 $pr->whereAdd("Projects.type IN ('N','X')");
129 $prjs = $pr->fetchAll('id');
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;
137 $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
140 (Projects.id IN (".implode(',', $prjs)."))
143 $this->whereAdd("1=0"); // can see nothing!!!
147 if (!empty($q['query']['distinct_client_id'])) {
148 // DB_DataObjecT::debuglevel(1);
150 $this->selectAdd('distinct(client_id)');
151 $this->selectAs(DB_DataObject::factory('Companies'), 'client_id_%s','join_client_id_id');
152 $this->groupBy('client_id');
156 // this is clipping related.. -- we should have an API for addons like this.. (and docs)
158 if ($au->company()->comptype == 'SUPPLIER') {
159 $pr = DB_DataObject::factory('CampaignAssign');
160 $pr->supplier_id = $au->company_id;
161 $prjs = $pr->fetchAll('project_id');
164 (Projects.id IN (".implode(',', $prjs)."))
167 $this->whereAdd("1=0"); // can see nothing!!!
170 if ($au->company()->comptype == 'CLIENT') {
171 $this->client_id = $au->company()->id; // can see nothing!!!
179 function whereAddIn($key, $list, $type) {
181 foreach($list as $k) {
182 $ar[] = $type =='int' ? (int)$k : $this->escape($k);
187 $this->whereAdd("$key IN (". implode(',', $ar). ')');
192 if (empty($this->code)) {
193 $this->code = 'C' + $this->client_id + '-P' + $this->id;
198 function onUpdate($old)
201 if (empty($this->code)) {
202 $this->code = 'C' + $this->client_id + '-P' + $this->id;
206 if ($old->code == $this->code) {
211 $opts = PEAR::getStaticProperty('Pman', 'options');
213 $olddir = $opts['storedir'] . '/' . $old->code;
214 $newdir = $opts['storedir'] . '/' . $this->code;
215 if ( file_exists($olddir)) {
216 move ($olddir, $newdir);
224 if (!$this->prune) { // non-expiring..
228 $d = DB_DataObject::factory('Document');
229 $d->whereAdd("date_rec < NOW - INTERVAL {$this->expires} DAYS");
231 while ($d->fetch()) {
243 * our camp interface uses the format Cxxx-Pyyyyyy to refer to the project.
245 function getByCodeRef($str)
247 $bits = explode('-', $str);
248 if ((count($bits) != 2) || $bits[0][0] != 'C' || $bits[1][0] != 'P' ) {
251 $comp = substr($bits[0], 1);
252 $id = (int) substr($bits[1], 1);
253 return $id && $this->get($id);
258 function i18toArray($type, $str)
268 $u = DB_DataObject::factory('Person');
269 $au =$u->getAuthUser();
270 $lang = empty($au->lang ) ? 'en' : $au->lang;
271 $lbits = explode('_', strtoupper($lang));
272 // no validation here!!!!
273 require_once 'I18Nv2/Language.php';
274 require_once 'I18Nv2/Country.php';
275 $langs = new I18Nv2_Language($lbits[0]); // locale support not there??
276 $cts = new I18Nv2_Country($lbits[0]); // lo
279 $lk = $type == 'c' ? $cts : $langs;
280 $ar =explode(',', $str);
283 $ret[] = array('code'=>$k, 'title' => $lk->getName($k));
286 // work out locale...
294 function toRooArray($f='%s') {
295 $ret = parent::toArray($f);
297 $ret['countrylist'] = $this->I18toArray('c',$ret['countries']);
298 $ret['languagelist'] = $this->I18toArray('l',$ret['languages']);
301 function setFromRoo($q)
304 if (isset($q['open_date'])) {
305 $this->open_date = date('Y-m-d', strtotime(
306 implode('-', array_reverse(explode('/', $q['open_date'])))
312 function fetchAll($k= false) {
315 $this->selectAdd($k);
320 while ($this->fetch()) {
321 $ret[] = $k === false ? clone($this) : $this->$k;
328 * fetch a list of user projects.
329 * if you need to filter open/closed.. then add whereAdds before calling
331 function getUserProjects($au, $data='id') // COMPANY BASED!!!!
333 $id = (int) $au->company_id;
335 (client_id= $id) OR (agency_id= $id)
339 $this->selectAdd($data);
343 while ($this->fetch()) {
344 $ret[] = empty($data) ? clone($this) : $this->$data;
352 * check who is trying to access this. false == access denied..
354 function checkPerm($lvl, $au)
356 return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);