DataObjects/Core_company.php
[Pman.Core] / DataObjects / ProjectDirectory.php
1 <?php
2 /**
3  * Table Definition for ProjectDirectory
4  *
5  * Note - projectdirectory is linked to this - due to an issue with postgres - we should keep to lowercase names only for tables..
6  * 
7  */
8 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
9
10 class Pman_Core_DataObjects_ProjectDirectory extends DB_DataObject 
11 {
12     ###START_AUTOCODE
13     /* the code below is auto generated do not remove the above tag */
14
15     public $__table = 'ProjectDirectory';                // table name
16     public $id;                              // int(11)  not_null primary_key auto_increment
17     public $project_id;                      // int(11)  not_null
18     public $person_id;                       // int(11)  not_null
19     public $ispm;                            // int(11)  not_null
20     public $role;                            // string(16)  not_null
21
22     
23     /* the code above is auto generated do not remove the tag below */
24     ###END_AUTOCODE
25     
26     
27     function person()
28     {
29         $p = DB_DataObject::factory('core_person');
30         $p->get($this->person_id);
31         return $p;
32     }
33     
34     function toEventString() {
35         $p = $this->person();
36         // this is weird... company is in the person.. - effieciency??
37         // for seaching??
38         $c = DB_DataObject::factory('core_company');
39         $c->get($this->company_id);
40         $pr = DB_DataObject::factory('core_project');
41         $pr->get($this->project_id);
42         
43         return $pr->code . ' '. $p->name . '('. $c->name .')';
44     }
45     
46     function personMemberOf($pe, $pr) {
47         $this->person_id = $pe->id;
48         $this->project_id = $pr->id;
49         $this->limit(1);
50         if ($this->find(true)) {
51             return true;
52         }
53         return false;
54     }
55  
56     function ensureProjectMember($pr, $pe) // used where?
57     {
58         if ($this->personMemberOf($pe, $pr)) {
59            return;
60         }
61         $this->company_id = $pe->company_id;
62         $this->office_id = $pe->office_id;
63         $this->role = $pe->role;
64         $this->insert();
65          
66     }
67     /**
68      * project id's for a user.
69      * @param DB_DataObject_Core_Person|array - who, or list of people.
70      * @return array id's of the project they are a member of..
71      */
72     function projects($au)
73     {
74         if (empty($au)) {
75             $p = DB_DataObject::Factory('core_project');
76             $p->get('code',  '*PUBLIC');
77             return array($p->id);          
78             
79         } 
80         $c = clone ($this);
81         
82         if (is_array($au)) {
83             $c->whereAddIn('person_id', $au, 'int');
84         } else {
85             $c->person_id = $au->id;
86         }
87         $c->selectAdd();
88         // people may have multiple roles for a project..
89         $c->selectAdd("distinct({$this->tableName()}.project_id) as project_id");
90         return $c->fetchAll('project_id');
91     }
92         /**
93      * project id's for a user.
94      * @param DB_DataObject_Core_Person - who
95      * @return array id's of the project they are a member of..
96      */
97     function people($pr)
98     {
99         $c = clone ($this);
100         //echo '<PRE>';print_R($this);exit;
101         
102         if (is_array($pr)) {
103             $c->whereAddIn("{$this->tableName()}.project_id", $pr, 'int');
104         } else {
105             $c->project_id = $pr->id;
106         }
107         $c->selectAdd();
108         $c->selectAdd("{$this->tableName()}.person_id as person_id");
109         return $c->fetchAll('person_id');
110         
111          
112     }
113     
114     
115     function checkPerm($lvl, $au) 
116     {
117         return $au->hasPerm('Documents.Project_Directory', $lvl);
118     } 
119     function setFromRoo($ar,$roo)
120     {
121         $this->setFrom($ar);
122         
123         if ($this->id && 
124             ($this->project_id == $roo->old->project_id) &&
125             ($this->person_id  == $roo->old->person_id) &&
126             ($this->company_id == $roo->old->company_id) )
127         {
128             return true;
129         }
130
131         $xx = DB_Dataobject::factory('ProjectDirectory');
132         $xx->setFrom(array(
133             'project_id' => $this->project_id,
134             'person_id'  => $this->person_id,
135             'company_id' => $this->company_id,
136         ));
137         
138         if ($xx->count()) {
139             return "Duplicate entry found Project Directory entry";
140         }
141         return true;
142
143     }
144     function applyFilters($q, $au)
145     {
146         //DB_DAtaObject::debugLevel(1);  var_dump($q);
147        
148         // otherwise only the project they are involved with..
149          
150         // can  see - their projects + their personal mail...
151         if (!empty($q['project_id_ar'])) {
152             // can filter projects!
153             $this->whereAddIn('ProjectDirectory.project_id', explode(',',$q['project_id_ar']), 'int');
154         }
155         
156         
157          if (!empty($q['query']['company_ids'])) {
158              $this->whereAddIn('ProjectDirectory.company_id', explode(',',$q['query']['company_ids']), 'int');
159         }
160         
161         // whos should they see as far as personal contacts.!?!?
162         // their projects... and their mail or.. just their mail if no projects..
163         
164         
165         /// ------------ PERMISSION FILTERERIN!!!!!!
166         
167         if ($au->hasPerm('Core.Projects_All', 'S')) {
168             return; // can see it all!!!
169         }
170         
171         
172         $pr = DB_DataObject::factory('core_project');
173         $pr->whereAdd("Projects.type IN ('N','X')");
174         $prjs = $pr->fetchAll('id');
175         
176         
177         $pd = DB_DataObject::factory('ProjectDirectory');
178         $pd->joinAdd(DB_DataObject::factory('core_project'), 'LEFT');
179         $pd->whereAdd("Projects.type NOT IN ('N','X')");
180         $pd->person_id = $au->id;
181         
182         $prjs = array_merge($prjs, $pd->fetchAll('project_id'));
183         if (count($prjs)) {
184             $this->whereAdd("
185                     (ProjectDirectory.project_id IN (".implode(',', $prjs).")) 
186                   
187                 
188             ");
189         }  else {
190             $this->whereAdd("1=0"); // can see nothing!!!
191         }
192        
193         
194         
195          
196          
197         
198     }
199     
200     
201     
202 }