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      * project id's for a user.
66      * @param DB_DataObject_Core_Person|array - who, or list of people.
67      * @return array id's of the project they are a member of..
68      */
69     function projects($au)
70     {
71         if (empty($au)) {
72             $p = DB_DataObject::Factory('Projects');
73             $p->get('code',  '*PUBLIC');
74             return array($p->id);          
75             
76         } 
77         $c = clone ($this);
78         
79         if (is_array($au)) {
80             $c->whereAddIn('person_id', $au, 'int');
81         } else {
82             $c->person_id = $au->id;
83         }
84         $c->selectAdd();
85         // people may have multiple roles for a project..
86         $c->selectAdd('distinct(project_id) as project_id');
87         return $c->fetchAll('project_id');
88     }
89         /**
90      * project id's for a user.
91      * @param DB_DataObject_Core_Person - who
92      * @return array id's of the project they are a member of..
93      */
94     function people($pr)
95     {
96         $c = clone ($this);
97         $c->autoJoin();
98         if (is_array($pr)) {
99             $c->whereAddIn('project_id', $pr, 'int');
100         } else {
101             $c->project_id = $pr->id;
102         }
103         $c->selectAdd();
104         $c->selectAdd('person_id)';
105         return $c->fetchAll('person_id');
106         
107          
108     }
109     
110     
111     function checkPerm($lvl, $au) 
112     {
113         return $au->hasPerm('Documents.Project_Directory', $lvl);
114     } 
115     function setFromRoo($ar,$roo)
116     {
117         $this->setFrom($ar);
118         
119         if ($this->id && 
120             ($this->project_id == $roo->old->project_id) &&
121             ($this->person_id  == $roo->old->person_id) &&
122             ($this->company_id == $roo->old->company_id) )
123         {
124             return true;
125         }
126
127         $xx = DB_Dataobject::factory('ProjectDirectory');
128         $xx->setFrom(array(
129             'project_id' => $this->project_id,
130             'person_id'  => $this->person_id,
131             'company_id' => $this->company_id,
132         ));
133         
134         if ($xx->count()) {
135             return "Duplicate entry found Project Directory entry";
136         }
137         return true;
138
139     }
140     function applyFilters($q, $au)
141     {
142         //DB_DAtaObject::debugLevel(1);  var_dump($q);
143        
144         // otherwise only the project they are involved with..
145          
146         // can  see - their projects + their personal mail...
147         if (!empty($q['project_id_ar'])) {
148             // can filter projects!
149             $this->whereAddIn('ProjectDirectory.project_id', explode(',',$q['project_id_ar']), 'int');
150         }
151         
152         
153          if (!empty($q['query']['company_ids'])) {
154              $this->whereAddIn('ProjectDirectory.company_id', explode(',',$q['query']['company_ids']), 'int');
155         }
156         
157         // whos should they see as far as personal contacts.!?!?
158         // their projects... and their mail or.. just their mail if no projects..
159         
160         
161         /// ------------ PERMISSION FILTERERIN!!!!!!
162         
163         if ($au->hasPerm('Core.Projects_All', 'S')) {
164             return; // can see it all!!!
165         }
166         
167         
168         $pr = DB_DataObject::factory('Projects');
169         $pr->whereAdd("Projects.type IN ('N','X')");
170         $prjs = $pr->fetchAll('id');
171         
172         
173         $pd = DB_DataObject::factory('ProjectDirectory');
174         $pd->joinAdd(DB_DataObject::factory('Projects'), 'LEFT');
175         $pd->whereAdd("Projects.type NOT IN ('N','X')");
176         $pd->person_id = $au->id;
177         
178         $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
179         if (count($prjs)) {
180             $this->whereAdd("
181                     (ProjectDirectory.project_id IN (".implode(',', $prjs).")) 
182                   
183                 
184             ");
185         }  else {
186             $this->whereAdd("1=0"); // can see nothing!!!
187         }
188        
189         
190         
191          
192          
193         
194     }
195     
196     
197     
198 }