DataObjects/ProjectDirectory.php
[Pman.Core] / DataObjects / ProjectDirectory.php
1 <?php
2 /**
3  * Table Definition for ProjectDirectory
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_ProjectDirectory extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'ProjectDirectory';                // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $project_id;                      // int(11)  not_null
15     public $person_id;                       // int(11)  not_null
16     public $ispm;                            // int(11)  not_null
17     public $role;                            // string(16)  not_null
18
19     
20     /* the code above is auto generated do not remove the tag below */
21     ###END_AUTOCODE
22     
23     
24     function person()
25     {
26         $p = DB_DataObject::factory('Person');
27         $p->get($this->person_id);
28         return $p;
29     }
30     
31     function toEventString() {
32         $p = $this->person();
33         // this is weird... company is in the person.. - effieciency??
34         // for seaching??
35         $c = DB_DataObject::factory('Companies');
36         $c->get($this->company_id);
37         $pr = DB_DataObject::factory('Projects');
38         $pr->get($this->project_id);
39         
40         return $pr->code . ' '. $p->name . '('. $c->name .')';
41     }
42     
43     function personMemberOf($pe, $pr) {
44         $this->person_id = $pe->id;
45         $this->project_id = $pr->id;
46         $this->limit(1);
47         if ($this->find(true)) {
48             return true;
49         }
50         return false;
51     }
52  
53     function ensureProjectMember($pr, $pe) // used where?
54     {
55         if ($this->personMemberOf($pe, $pr)) {
56            return;
57         }
58         $this->company_id = $pe->company_id;
59         $this->office_id = $pe->office_id;
60         $this->role = $pe->role;
61         $this->insert();
62         
63         
64         
65     }
66     function checkPerm($lvl, $au) 
67     {
68         return $au->hasPerm('Documents.Project_Directory', $lvl);
69     } 
70     function setFromRoo($ar,$roo)
71     {
72         $this->setFrom($ar);
73         
74         if ($this->id && 
75             ($this->project_id == $roo->old->project_id) &&
76             ($this->person_id == $roo->old->person_id) &&
77             ($this->company_id == $roo->old->company_id) )
78         {
79             return true;
80         }
81
82         $xx = DB_Dataobject::factory('ProjectDirectory');
83         $xx->setFrom(array(
84             'project_id' => $this->project_id,
85             'person_id' => $this->person_id,
86             'company_id' => $this->company_id,
87         ));
88         
89         if ($xx->count()) {
90             return "Duplicate entry found Project Directory entry";
91         }
92         return true;
93
94     }
95     function applyFilters($q, $au)
96     {
97         //DB_DAtaObject::debugLevel(1);  var_dump($q);
98        
99         // otherwise only the project they are involved with..
100          
101         // can  see - their projects + their personal mail...
102         if (!empty($q['project_id_ar'])) {
103             // can filter projects!
104             $this->whereAddIn('ProjectDirectory.project_id', explode(',',$q['project_id_ar']), 'int');
105         }
106         
107         
108          if (!empty($q['query']['company_ids'])) {
109              $this->whereAddIn('ProjectDirectory.company_id', explode(',',$q['query']['company_ids']), 'int');
110         }
111         
112         // whos should they see as far as personal contacts.!?!?
113         // their projects... and their mail or.. just their mail if no projects..
114         
115         
116         /// ------------ PERMISSION FILTERERIN!!!!!!
117         
118         if ($au->hasPerm('Core.Projects_All', 'S')) {
119             return; // can see it all!!!
120         }
121         
122         
123         $pr = DB_DataObject::factory('Projects');
124         $pr->whereAdd("Projects.type IN ('N','X')");
125         $prjs = $pr->fetchAll('id');
126         
127         
128         $pd = DB_DataObject::factory('ProjectDirectory');
129         $pd->joinAdd(DB_DataObject::factory('Projects'), 'LEFT');
130         $pd->whereAdd("Projects.type NOT IN ('N','X')");
131         $pd->person_id = $au->id;
132         
133         $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
134         if (count($prjs)) {
135             $this->whereAdd("
136                     (ProjectDirectory.project_id IN (".implode(',', $prjs).")) 
137                   
138                 
139             ");
140         }  else {
141             $this->whereAdd("1=0"); // can see nothing!!!
142         }
143        
144         
145         
146          
147          
148         
149     }
150     
151     
152     
153 }