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