Added audit info for template create and modify events.
[timetracker.git] / WEB-INF / lib / ttTemplateHelper.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
30 // Class ttTemplateHelper is used to help with template related tasks.
31 class ttTemplateHelper {
32
33   // get - gets template details.
34   static function get($id) {
35     global $user;
36     $mdb2 = getConnection();
37
38     $group_id = $user->getGroup();
39     $org_id = $user->org_id;
40
41     $sql = "select id, name, description, content, status from tt_templates".
42       " where id = $id and group_id = $group_id and org_id = $org_id".
43       " and status is not null";
44     $res = $mdb2->query($sql);
45     if (!is_a($res, 'PEAR_Error')) {
46       $val = $res->fetchRow();
47       if ($val) {
48         return $val;
49       }
50     }
51     return false;
52   }
53
54   // delete - marks a template as deleted in tt_templates table in database.
55   static function delete($id) {
56     global $user;
57     $mdb2 = getConnection();
58
59     $group_id = $user->getGroup();
60     $org_id = $user->org_id;
61
62     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
63
64     $sql = "update tt_templates set status = null".$modified_part.
65       " where id = $id and group_id = $group_id and org_id = $org_id";
66     $affected = $mdb2->exec($sql);
67     if (is_a($affected, 'PEAR_Error'))
68       return false;
69
70     return true;
71   }
72
73   // insert function inserts a new template into database.
74   static function insert($fields) {
75     global $user;
76     $mdb2 = getConnection();
77
78     $group_id = $user->getGroup();
79     $org_id = $user->org_id;
80
81     $name = $fields['name'];
82     $description = $fields['description'];
83     $content = $fields['content'];
84
85     $created_part = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
86
87     $sql = "insert into tt_templates (group_id, org_id, name, description, content,".
88       " created, created_ip, created_by)".
89       " values ($group_id, $org_id, ".$mdb2->quote($name).
90       ", ".$mdb2->quote($description).", ".$mdb2->quote($content).$created_part.")";
91     $affected = $mdb2->exec($sql);
92     if (is_a($affected, 'PEAR_Error'))
93       return false;
94
95     return true;
96   }
97
98   // update function - updates a template in database.
99   static function update($fields) {
100     global $user;
101     $mdb2 = getConnection();
102
103     $group_id = $user->getGroup();
104     $org_id = $user->org_id;
105
106     $template_id = (int) $fields['id'];
107     $name = $fields['name'];
108     $description = $fields['description'];
109     $content = $fields['content'];
110     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
111     $status = (int) $fields['status'];
112
113     $sql = "update tt_templates set name = ".$mdb2->quote($name).
114       ", description = ".$mdb2->quote($description).
115       ", content = ".$mdb2->quote($content).$modified_part.
116       ", status = ".$status.
117       " where id = $template_id and group_id = $group_id and org_id = $org_id";
118     $affected = $mdb2->exec($sql);
119     return (!is_a($affected, 'PEAR_Error'));
120   }
121 }