Initial repo created
[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 import('ttTeamHelper');
30 import('ttUserHelper');
31
32 // Class ttNotificationHelper is used to help with notification related tasks.
33 class ttNotificationHelper {
34         
35   // get - gets notification details. 
36   static function get($id)
37   {
38     global $user;
39  
40     $mdb2 = getConnection();
41
42     $sql = "select c.id, c.cron_spec, c.report_id, c.email, c.status, fr.name from tt_cron c
43       left join tt_fav_reports fr on (fr.id = c.report_id)
44       where c.id = $id and c.team_id = $user->team_id";
45     $res = $mdb2->query($sql);
46     if (!is_a($res, 'PEAR_Error')) {
47       $val = $res->fetchRow();
48           if ($val && $val['id'])
49         return $val;
50     }
51     return false;
52   }
53   
54   // delete - deletes a notification from tt_cron table. 
55   static function delete($id) {
56     global $user;
57             
58     $mdb2 = getConnection();
59     
60     $sql = "delete from tt_cron where id = $id and team_id = $user->team_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   {
71     $mdb2 = getConnection();
72
73     $team_id = (int) $fields['team_id'];
74     $cron_spec = $fields['cron_spec'];
75     $next = (int) $fields['next'];
76     $report_id = (int) $fields['report_id'];
77     $email = $fields['email'];
78     $status = $fields['status'];
79     
80     $sql = "insert into tt_cron (team_id, cron_spec, next, report_id, email, status)
81       values ($team_id, ".$mdb2->quote($cron_spec).", $next, $report_id, ".$mdb2->quote($email).", ".$mdb2->quote($status).")";
82     $affected = $mdb2->exec($sql);
83     if (is_a($affected, 'PEAR_Error'))
84       return false;
85  
86     return true;
87   } 
88   
89   // update function - updates a notification in database.
90   static function update($fields)
91   {
92     $mdb2 = getConnection();
93     
94     $notification_id = (int) $fields['id'];
95     $team_id = (int) $fields['team_id'];
96     $cron_spec = $fields['cron_spec'];
97     $next = (int) $fields['next'];
98     $report_id = (int) $fields['report_id'];
99     $email = $fields['email'];
100     $status = $fields['status'];
101     
102     $sql = "update tt_cron set cron_spec = ".$mdb2->quote($cron_spec).", next = $next, report_id = $report_id, email = ".$mdb2->quote($email).", status = ".$mdb2->quote($status).
103       " where id = $notification_id and team_id = $team_id";
104     $affected = $mdb2->exec($sql);
105     return (!is_a($affected, 'PEAR_Error'));
106   }
107 }
108 ?>