More progress on creating a new timesheet.
authorNik Okuntseff <support@anuko.com>
Sun, 17 Feb 2019 18:45:40 +0000 (18:45 +0000)
committerNik Okuntseff <support@anuko.com>
Sun, 17 Feb 2019 18:45:40 +0000 (18:45 +0000)
WEB-INF/lib/form/ActionForm.class.php
WEB-INF/lib/ttTimesheetHelper.class.php [new file with mode: 0644]
WEB-INF/templates/footer.tpl
WEB-INF/templates/report.tpl
report.php
reports.php
timesheet_add.php

index 97c9219..8e2ad8a 100644 (file)
@@ -166,6 +166,20 @@ class ActionForm {
        }
        //print_r($_SESSION);
     }
+
+  // saveDetachedAttribute saves a "detached" from form named attributed in session.
+  // There is no element in the form for it.
+  // Intended use is to add something to the session, when a form bean created on one page
+  // is used on other pages (ex.: reportForm).
+  // For example, to generate a timesheet we need a user_id, which is determined when a report
+  // is generated on report.php, using a bean created in reports.php.
+  function saveDetachedAttribute($name, $value) {
+    $_SESSION[$this->mSessionCell .'_'.$name] = $value;
+  }
+
+  function getDetachedAttribute($name) {
+    return $_SESSION[$this->mSessionCell.'_'.$name];
+  }
     
     function loadBean() {
        $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
diff --git a/WEB-INF/lib/ttTimesheetHelper.class.php b/WEB-INF/lib/ttTimesheetHelper.class.php
new file mode 100644 (file)
index 0000000..a8276b5
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+// +----------------------------------------------------------------------+
+// | Anuko Time Tracker
+// +----------------------------------------------------------------------+
+// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
+// +----------------------------------------------------------------------+
+// | LIBERAL FREEWARE LICENSE: This source code document may be used
+// | by anyone for any purpose, and freely redistributed alone or in
+// | combination with other software, provided that the license is obeyed.
+// |
+// | There are only two ways to violate the license:
+// |
+// | 1. To redistribute this code in source form, with the copyright
+// |    notice or license removed or altered. (Distributing in compiled
+// |    forms without embedded copyright notices is permitted).
+// |
+// | 2. To redistribute modified versions of this code in *any* form
+// |    that bears insufficient indications that the modifications are
+// |    not the work of the original author(s).
+// |
+// | This license applies to this document only, not any other software
+// | that it may be combined with.
+// |
+// +----------------------------------------------------------------------+
+// | Contributors:
+// | https://www.anuko.com/time_tracker/credits.htm
+// +----------------------------------------------------------------------+
+
+import('ttUserHelper');
+import('ttGroupHelper');
+
+// Class ttTimesheetHelper is used to help with project related tasks.
+class ttTimesheetHelper {
+
+  // The getTimesheetByName looks up a project by name.
+  static function getTimesheetByName($name, $user_id) {
+    global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    $sql = "select id from tt_timesheets".
+      " where group_id = $group_id and org_id = $org_id and user_id = $user_id and name = ".$mdb2->quote($name).
+      " and (status = 1 or status = 0)";
+    $res = $mdb2->query($sql);
+    if (!is_a($res, 'PEAR_Error')) {
+      $val = $res->fetchRow();
+      if ($val && $val['id'])
+        return $val;
+    }
+    return false;
+  }
+
+  // insert function inserts a new timesheet into database.
+  static function insert($fields)
+  {
+    global $user;
+    $mdb2 = getConnection();
+
+    $group_id = $user->getGroup();
+    $org_id = $user->org_id;
+
+    $user_id = $fields['user_id'];
+    $client_id = $fields['client_id'];
+    $name = $fields['name'];
+    $submitter_comment = $fields['comment'];
+
+    $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, name, submitter_comment)".
+      " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($name).", ".$mdb2->quote($submitter_comment).")";
+    $affected = $mdb2->exec($sql);
+    if (is_a($affected, 'PEAR_Error'))
+      return false;
+
+    $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
+
+    // TODO: Associate report items with new timesheet.
+
+    return $last_id;
+  }
+}
index e3d319a..8b988ea 100644 (file)
@@ -12,7 +12,7 @@
       <br>
       <table cellspacing="0" cellpadding="4" width="100%" border="0">
         <tr>
-          <td align="center">&nbsp;Anuko Time Tracker 1.18.37.4732 | Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
+          <td align="center">&nbsp;Anuko Time Tracker 1.18.37.4733 | Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
             <a href="https://www.anuko.com/lp/tt_4.htm" target="_blank">{$i18n.footer.credits}</a> |
             <a href="https://www.anuko.com/lp/tt_5.htm" target="_blank">{$i18n.footer.license}</a> |
             <a href="https://www.anuko.com/lp/tt_7.htm" target="_blank">{$i18n.footer.improve}</a>
index 4212431..3cd8e40 100644 (file)
@@ -2,7 +2,7 @@
   function chLocation(newLocation) { document.location = newLocation; }
 </script>
 
-{$forms.reportForm.open}
+{$forms.reportViewForm.open}
 <table width="720">
   <td valign="top">
     <table border="0" cellpadding="3" cellspacing="1" width="100%">
   <tr>
     <td align="right">
       <table>
-        <tr><td>{$i18n.label.mark_paid}: {$forms.reportForm.mark_paid_select_options.control} {$forms.reportForm.mark_paid_action_options.control} {$forms.reportForm.btn_mark_paid.control}</td></tr>
+        <tr><td>{$i18n.label.mark_paid}: {$forms.reportViewForm.mark_paid_select_options.control} {$forms.reportViewForm.mark_paid_action_options.control} {$forms.reportViewForm.btn_mark_paid.control}</td></tr>
       </table>
     </td>
   </tr>
   <tr>
     <td align="right">
       <table>
-        <tr><td>{$i18n.form.report.assign_to_invoice}: {$forms.reportForm.assign_invoice_select_options.control} {$forms.reportForm.recent_invoice.control} {$forms.reportForm.btn_assign.control}</td></tr>
+        <tr><td>{$i18n.form.report.assign_to_invoice}: {$forms.reportViewForm.assign_invoice_select_options.control} {$forms.reportViewForm.recent_invoice.control} {$forms.reportViewForm.btn_assign.control}</td></tr>
       </table>
     </td>
   </tr>
   {/if}
 </table>
 {/if}
-{$forms.reportForm.close}
+{$forms.reportViewForm.close}
 
 <table width="720" cellspacing="4" cellpadding="4" border="0">
 <tr>
index 7ecabcc..65d98e4 100644 (file)
@@ -58,10 +58,10 @@ if ($user->isPluginEnabled('cf')) {
   $smarty->assign('custom_fields', $custom_fields);
 }
 
-$form = new Form('reportForm');
+$form = new Form('reportViewForm');
 
 // Report settings are stored in session bean before we get here from reports.php.
-$bean = new ActionForm('reportBean', $form, $request);
+$bean = new ActionForm('reportBean', new Form('reportForm'), $request);
 // If we are in post, load the bean from session, as the constructor does it only in get.
 if ($request->isPost()) $bean->loadBean();
 
@@ -209,6 +209,8 @@ if ($user->isPluginEnabled('ts') && count($report_items) > 0 &&
       break;
     }
   }
+  // Save user_id in session.
+  $bean->saveDetachedAttribute('timesheet_user_id', $first_user_id);
 
   // TODO: Improve this for "view_all_reports" situation.
   // We may need to add "manage_all_timesheets" right.
index 10df364..892b117 100644 (file)
@@ -273,6 +273,10 @@ if ($showWorkUnits)
 if ($showTimesheet)
   $form->addInput(array('type'=>'checkbox','name'=>'chtimesheet'));
 
+// Add a hidden control for timesheet_user_id (who to generate a timesheet for).
+if ($showTimesheet)
+  $form->addInput(array('type'=>'hidden','name'=>'timesheet_user_id'));
+
 // Add group by control.
 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
index c52fea6..fa32ca7 100644 (file)
@@ -28,6 +28,7 @@
 
 require_once('initialize.php');
 import('form.Form');
+import('ttTimesheetHelper');
 
 // Access checks.
 if (!(ttAccessAllowed('manage_own_timesheets') || ttAccessAllowed('manage_timesheets'))) {
@@ -43,6 +44,10 @@ if (!$user->isPluginEnabled('ts')) {
 if ($request->isPost()) {
   $cl_name = trim($request->getParameter('timesheet_name'));
   $cl_comment = trim($request->getParameter('submitter_comment'));
+
+  // Report settings are stored in session bean before we get here.
+  $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
+  $bean->loadBean();
 }
 
 $form = new Form('timesheetForm');
@@ -56,25 +61,23 @@ if ($request->isPost()) {
   if (!ttValidString($cl_comment, true)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
 
   if ($err->no()) {
-    /*
-    if (!ttProjectHelper::getProjectByName($cl_name)) {
-      if (ttProjectHelper::insert(array('name' => $cl_name,
-        'description' => $cl_description,
-        'users' => $cl_users,
-        'tasks' => $cl_tasks,
-        'status' => ACTIVE))) {
-          header('Location: projects.php');
+    $user_id = $bean->getDetachedAttribute('timesheet_user_id');
+    if (!ttTimesheetHelper::getTimesheetByName($cl_name, $user_id)) {
+      if (ttTimesheetHelper::insert(array('user_id' => $user_id,
+        // 'client_id' => ?
+        'name' => $cl_name,
+        'comment' => $cl_comment))) {
+          header('Location: timesheets.php');
           exit();
         } else
           $err->add($i18n->get('error.db'));
     } else
       $err->add($i18n->get('error.object_exists'));
-    */
   }
 } // isPost
 
 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
-$smarty->assign('onload', 'onLoad="document.timesheetForm.timmesheet_name.focus()"');
+$smarty->assign('onload', 'onLoad="document.timesheetForm.timesheet_name.focus()"');
 $smarty->assign('title', $i18n->get('title.add_timesheet'));
 $smarty->assign('content_page_name', 'timesheet_add.tpl');
 $smarty->display('index.tpl');