php warnings
[Pman.MTrack] / DataObjects / Core_project.php
1 <?php
2
3 require_once 'Pman/Core/DataObjects/Core_project.php';
4
5 class Pman_MTrack_DataObjects_Core_project extends Pman_Core_DataObjects_Core_project
6 {
7      
8        /**
9      * check who is trying to access this. false == access denied..
10      */
11     function checkPerm($lvl, $au) 
12     {
13       // var_dump(array($lvl,$au));
14         if ($lvl == 'S') { 
15             if (!$au) {
16                  return true;
17             }
18             if (!$au->company_id) {
19                 return true;
20             }
21             if ($au->company()->comptype != 'OWNER') {
22                 return true;
23             }
24             // owner.. - we can rely on perms...
25
26             return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);
27    
28         }
29         // any other action on perms has to be done by company onwer at present..
30         if (!$au || !$au->company_id || $au->company()->comptype != 'OWNER') {
31             return false;
32         }
33         
34         return $au->hasPerm("Core.Projects_Member_Of",$lvl) || $au->hasPerm("Core.Projects_All",$lvl);
35     }
36     
37     function applyFilters($q, $au, $roo)
38     {
39        $tn = $this->tableName();
40        if (!$au) {
41             $this->code = '*PUBLIC';
42             $this->selectAdd();
43             $this->selectAs(array('id','name','code')); // public an only see so much data.?
44             
45             $roo->jdata($this->fetchAll(false,false,'toArray'));
46         }
47         // not sure why this used to say array().. for filter???
48         parent::applyFilters($q, $au, $roo);
49
50         if (!$au->hasPerm('Core.Projects_All', 'S')) { 
51               $pdtn = $this->factory('ProjectDirectory')->tableName();
52               
53               // default behaviour is to only allow access to projects
54               // where the user has a role..
55               
56               $this->whereAdd("
57                     {$tn}.id in  
58                         (SELECT {$pdtn}.project_id FROM {$pdtn} WHERE
59                             person_id = ". $au->id . " and role != ''
60                         )
61             ");
62             // can only see projects with repo's.. by default..
63         
64            
65         }
66           
67         if (isset($q['_with_tickets'])) {
68             $this->selectAdd("
69                 COALESCE((
70                     SELECT
71                         count(mtrack_ticket.id)
72                     FROM
73                         mtrack_ticket
74                     LEFT JOIN
75                        core_enum as join_status_id
76                     ON
77                         join_status_id.id = mtrack_ticket.status
78                     WHERE
79                         (join_status_id.name NOT IN('closed', 'on hold') OR  mtrack_ticket.status  = 0)
80                         AND
81                         mtrack_ticket.project_id = core_project.id
82                 ),0) as no_tickets, 
83                 IF (COALESCE((
84                     SELECT
85                         count(mtrack_ticket.id)
86                     FROM
87                         mtrack_ticket
88                     LEFT JOIN
89                        core_enum as join_status_id
90                     ON
91                         join_status_id.id = mtrack_ticket.status
92                     WHERE
93                         (join_status_id.name NOT IN('closed', 'on hold') OR  mtrack_ticket.status  = 0)
94                         AND
95                         mtrack_ticket.project_id = core_project.id
96                 ),0) > 1, 0 , 1) as has_no_tickets
97             ");
98             $this->_extra_cols = empty($this->_extra_cols) ? array() : $this->_extra_cols;
99             $this->_extra_cols += array('no_tickets','has_no_tickets');
100         }
101         
102         
103         if (isset($q['_mtrack_repos_only'])) { 
104         //?/ should we hide closed projects by default even from admin?
105             $this->whereAdd("{$tn}.id in (SELECT distinct(project_id) FROM mtrack_repos)");
106         }
107          
108         //$p->orderBy('{$tn}.name ASC');
109         //unset($p->client_id); // default projects serach enforces this..
110         //$ar = $p->fetchAll('id', 'name');
111         
112         
113         
114         
115     }
116      
117     
118     
119     
120     
121    
122     
123     
124     
125     function projectPerm($project_id, $what, $cando)
126     {
127         if (!$project_id) {
128             return false;
129         }
130         $p = DB_DataObject::factory('Projects');
131         $p->get($project_id);
132         if (!$this->authUser) {
133             if ($p->code != '*PUBLIC') {
134                 return false; // only public projects
135             }
136             if ($cando != 'S') {
137                 return false;
138             }
139             // all permissions to view public stuff.
140             return true;
141         }
142         if (!$this->authUser->hasPerm($what, $cando)) {
143             echo "NO PERMS $what $cando";
144             echo '<PRE>'; print_r($this->authUser->getPerms());
145             return false;
146         }
147         // membership rules?
148         //echo "COMPTYPE " . $this->authUser->company()->comptype ;
149         if ($this->authUser->company()->comptype == 'OWNER') {
150                 
151             if ($this->authUser->hasPerm('Core.Projects_All', $cando)) { // they can do what they like on all projects.
152                return true;
153             }
154            // return $p->hasPerm($what, $cando);
155         }
156         // otherwise they have to be a team member of that project.
157         
158         $pd = DB_DataObject::factory('ProjectDirectory');
159         $pd->project_id = $project_id;
160         $pd->user_id = $this->authUser->id;
161         $pd->whereAdd("role != ''");
162         
163         if (!$pd->count()) {
164             return false;
165         }
166         return true;
167          
168     }
169     
170     
171     function notifyENDOFDAYMAIL($rcpt, $last_sent_date, $notify, $force)
172     {
173         // fisrst use history to show a list of changes between the dates.
174         //die("building end of day mail");
175         //DB_DataObject::debugLevel(1);
176         $start = date('Y-m-d H:i:s', strtotime($notify->act_start . ' - 1 DAY'));
177         $end = date('Y-m-d H:i:s', strtotime($notify->act_start));
178         
179         $d = DB_DataObject::factory('mtrack_change');
180         $d->ontable  = 'mtrack_ticket';
181         $d->whereAdd(" mtrack_change.changedate >= '$start' AND mtrack_change.changedate <= '$end'");
182         $d->autoJoin();
183         $d->autoJoinObject('mtrack_ticket');
184         $d->whereAdd('join_project_id_id.id = '. $this->pid());
185         
186         $d->orderBy('mtrack_change.changedate ASC');
187         
188         $res = $d->fetchAll();
189         
190         
191         // group it by projects...
192         $tickets = array();
193         foreach($res as $o) {
194              
195             if (!isset($tickets[$o->ontable_id])) {
196                 $tickets[$o->ontable_id] = clone($o);
197                 $tickets[$o->ontable_id]->changes = array();
198             }
199             
200             $tickets[$o->ontable_id]->changes[] = clone($o);
201         }
202         
203          
204         
205         if (!$tickets) {
206             return true;
207         }
208         //print_r($obj);exit; 
209         $ret =   $rcpt->buildMail('mtrack_ticket_daily_changes', array(
210             'project' => $this,
211             'tickets' => $tickets,
212             'date' => date('d/M/Y', strtotime($notify->act_start)),
213             
214             ));
215         //print_r($ret);exit;
216         return $ret;
217         
218         
219         //$rcpt->sendTemplate('repo_daily_changes', $obj);
220         
221         //echo '<PRE>'.htmlspecialchars(print_r($mr,true));
222         
223         //exit;
224          
225         
226         
227         
228     }
229     
230     
231 }