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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  30 // Class ttTemplateHelper is used to help with template related tasks.
 
  31 class ttTemplateHelper {
 
  33   // get - gets template details.
 
  34   static function get($id) {
 
  36     $mdb2 = getConnection();
 
  38     $group_id = $user->getGroup();
 
  39     $org_id = $user->org_id;
 
  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();
 
  54   // delete - marks a template as deleted in tt_templates table in database.
 
  55   static function delete($id) {
 
  57     $mdb2 = getConnection();
 
  59     $group_id = $user->getGroup();
 
  60     $org_id = $user->org_id;
 
  62     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
 
  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'))
 
  73   // insert function inserts a new template into database.
 
  74   static function insert($fields) {
 
  76     $mdb2 = getConnection();
 
  78     $group_id = $user->getGroup();
 
  79     $org_id = $user->org_id;
 
  81     $name = $fields['name'];
 
  82     $description = $fields['description'];
 
  83     $content = $fields['content'];
 
  85     $created_part = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
 
  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'))
 
  98   // update function - updates a template in database.
 
  99   static function update($fields) {
 
 101     $mdb2 = getConnection();
 
 103     $group_id = $user->getGroup();
 
 104     $org_id = $user->org_id;
 
 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'];
 
 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'));