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