Refactored ttNotificationHelper class for subgroups.
[timetracker.git] / WEB-INF / lib / ttNotificationHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // Class ttNotificationHelper is used to help with notification related tasks.
30 class ttNotificationHelper {
31         
32   // get - gets notification details. 
33   static function get($id) {
34     global $user;
35     $mdb2 = getConnection();
36
37     $group_id = $user->getGroup();
38     $org_id = $user->org_id;
39
40     $sql = "select c.id, c.cron_spec, c.report_id, c.email, c.cc, c.subject, c.report_condition, c.status, fr.name from tt_cron c".
41       " left join tt_fav_reports fr on (fr.id = c.report_id)".
42       " where c.id = $id and c.group_id = $group_id and c.org_id = $org_id";
43     $res = $mdb2->query($sql);
44     if (!is_a($res, 'PEAR_Error')) {
45       $val = $res->fetchRow();
46           if ($val && $val['id'])
47         return $val;
48     }
49     return false;
50   }
51   
52   // delete - deletes a notification from tt_cron table. 
53   static function delete($id) {
54     global $user;
55     $mdb2 = getConnection();
56
57     $group_id = $user->getGroup();
58     $org_id = $user->org_id;
59
60     $sql = "delete from tt_cron where id = $id and group_id = $group_id and org_id = $org_id";
61     $affected = $mdb2->exec($sql);
62     if (is_a($affected, 'PEAR_Error'))
63       return false;
64
65     return true;
66   }
67   
68   // insert function inserts a new notification into database.
69   static function insert($fields) {
70     global $user;
71     $mdb2 = getConnection();
72
73     $group_id = $user->getGroup();
74     $org_id = $user->org_id;
75
76     $cron_spec = $fields['cron_spec'];
77     $next = (int) $fields['next'];
78     $report_id = (int) $fields['report_id'];
79     $email = $fields['email'];
80     $cc = $fields['cc'];
81     $subject = $fields['subject'];
82     $report_condition = $fields['report_condition'];
83     $status = $fields['status'];
84     
85     $sql = "insert into tt_cron (group_id, org_id, cron_spec, next, report_id, email, cc, subject, report_condition, status)".
86       " values ($group_id, $org_id, ".$mdb2->quote($cron_spec).", $next, $report_id, ".$mdb2->quote($email).", ".$mdb2->quote($cc).", ".$mdb2->quote($subject).", ".$mdb2->quote($report_condition).", ".$mdb2->quote($status).")";
87     $affected = $mdb2->exec($sql);
88     if (is_a($affected, 'PEAR_Error'))
89       return false;
90  
91     return true;
92   } 
93   
94   // update function - updates a notification in database.
95   static function update($fields) {
96     global $user;
97     $mdb2 = getConnection();
98
99     $group_id = $user->getGroup();
100     $org_id = $user->org_id;
101
102     $notification_id = (int) $fields['id'];
103     $cron_spec = $fields['cron_spec'];
104     $next = (int) $fields['next'];
105     $report_id = (int) $fields['report_id'];
106     $email = $fields['email'];
107     $cc = $fields['cc'];
108     $subject = $fields['subject'];
109     $report_condition = $fields['report_condition'];
110     $status = $fields['status'];
111     
112     $sql = "update tt_cron".
113       " set cron_spec = ".$mdb2->quote($cron_spec).", next = $next, report_id = $report_id".
114       ",  email = ".$mdb2->quote($email).", cc = ".$mdb2->quote($cc).", subject = ".$mdb2->quote($subject).
115       ", report_condition = ".$mdb2->quote($report_condition).", status = ".$mdb2->quote($status).
116       " where id = $notification_id and group_id = $group_id and org_id = $org_id";
117     $affected = $mdb2->exec($sql);
118     return (!is_a($affected, 'PEAR_Error'));
119   }
120 }