a4fa5e6dc2358c0a771707e85a45fa9bc7dff100
[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   {
35     global $user;
36  
37     $mdb2 = getConnection();
38
39     $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
40       left join tt_fav_reports fr on (fr.id = c.report_id)
41       where c.id = $id and c.group_id = ".$user->getActiveGroup();
42     $res = $mdb2->query($sql);
43     if (!is_a($res, 'PEAR_Error')) {
44       $val = $res->fetchRow();
45           if ($val && $val['id'])
46         return $val;
47     }
48     return false;
49   }
50   
51   // delete - deletes a notification from tt_cron table. 
52   static function delete($id) {
53     global $user;
54             
55     $mdb2 = getConnection();
56     
57     $sql = "delete from tt_cron where id = $id and group_id = ".$user->getActiveGroup();
58     $affected = $mdb2->exec($sql);
59     if (is_a($affected, 'PEAR_Error'))
60       return false;
61
62         return true;
63   }
64   
65   // insert function inserts a new notification into database.
66   static function insert($fields)
67   {
68     global $user;
69     $mdb2 = getConnection();
70
71     $group_id = $user->getActiveGroup();
72     $org_id = $user->org_id;
73     $cron_spec = $fields['cron_spec'];
74     $next = (int) $fields['next'];
75     $report_id = (int) $fields['report_id'];
76     $email = $fields['email'];
77     $cc = $fields['cc'];
78     $subject = $fields['subject'];
79     $report_condition = $fields['report_condition'];
80     $status = $fields['status'];
81     
82     $sql = "insert into tt_cron (group_id, org_id, cron_spec, next, report_id, email, cc, subject, report_condition, status)".
83       " 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).")";
84     $affected = $mdb2->exec($sql);
85     if (is_a($affected, 'PEAR_Error'))
86       return false;
87  
88     return true;
89   } 
90   
91   // update function - updates a notification in database.
92   static function update($fields)
93   {
94     $mdb2 = getConnection();
95     
96     $notification_id = (int) $fields['id'];
97     $group_id = (int) $fields['group_id'];
98     $cron_spec = $fields['cron_spec'];
99     $next = (int) $fields['next'];
100     $report_id = (int) $fields['report_id'];
101     $email = $fields['email'];
102     $cc = $fields['cc'];
103     $subject = $fields['subject'];
104     $report_condition = $fields['report_condition'];
105     $status = $fields['status'];
106     
107     $sql = "update tt_cron set cron_spec = ".$mdb2->quote($cron_spec).", next = $next, report_id = $report_id, email = ".$mdb2->quote($email).", cc = ".$mdb2->quote($cc).", subject = ".$mdb2->quote($subject).", report_condition = ".$mdb2->quote($report_condition).", status = ".$mdb2->quote($status).
108       " where id = $notification_id and group_id = $group_id";
109     $affected = $mdb2->exec($sql);
110     return (!is_a($affected, 'PEAR_Error'));
111   }
112 }