cc1818d2952f6ceff378cce97786132582e08875
[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     $sql = "update tt_templates set status = null".
63       " where id = $id and group_id = $group_id and org_id = $org_id";
64     $affected = $mdb2->exec($sql);
65     if (is_a($affected, 'PEAR_Error'))
66       return false;
67
68     return true;
69   }
70
71   // insert function inserts a new template into database.
72   static function insert($fields) {
73     global $user;
74     $mdb2 = getConnection();
75
76     $group_id = $user->getGroup();
77     $org_id = $user->org_id;
78
79     $name = $fields['name'];
80     $description = $fields['description'];
81     $content = $fields['content'];
82
83     $sql = "insert into tt_templates (group_id, org_id, name, description, content)".
84       " values ($group_id, $org_id, ".$mdb2->quote($name).
85       ", ".$mdb2->quote($description).", ".$mdb2->quote($content).")";
86     $affected = $mdb2->exec($sql);
87     if (is_a($affected, 'PEAR_Error'))
88       return false;
89
90     return true;
91   }
92
93   // update function - updates a template in database.
94   static function update($fields) {
95     global $user;
96     $mdb2 = getConnection();
97
98     $group_id = $user->getGroup();
99     $org_id = $user->org_id;
100
101     $template_id = (int) $fields['id'];
102     $name = $fields['name'];
103     $description = $fields['description'];
104     $content = $fields['content'];
105     $status = (int) $fields['status'];
106
107     $sql = "update tt_templates set name = ".$mdb2->quote($name).
108       ", description = ".$mdb2->quote($description).
109       ", content = ".$mdb2->quote($content).
110       ", status = ".$status.
111       " where id = $template_id and group_id = $group_id and org_id = $org_id";
112     $affected = $mdb2->exec($sql);
113     return (!is_a($affected, 'PEAR_Error'));
114   }
115 }