From eb300930267977a1caac0355200467e4bdf8b655 Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Sun, 17 Feb 2019 18:45:40 +0000 Subject: [PATCH] More progress on creating a new timesheet. --- WEB-INF/lib/form/ActionForm.class.php | 14 +++++ WEB-INF/lib/ttTimesheetHelper.class.php | 81 +++++++++++++++++++++++++ WEB-INF/templates/footer.tpl | 2 +- WEB-INF/templates/report.tpl | 8 +-- report.php | 6 +- reports.php | 4 ++ timesheet_add.php | 23 ++++--- 7 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 WEB-INF/lib/ttTimesheetHelper.class.php diff --git a/WEB-INF/lib/form/ActionForm.class.php b/WEB-INF/lib/form/ActionForm.class.php index 97c9219c..8e2ad8af 100644 --- a/WEB-INF/lib/form/ActionForm.class.php +++ b/WEB-INF/lib/form/ActionForm.class.php @@ -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 index 00000000..a8276b55 --- /dev/null +++ b/WEB-INF/lib/ttTimesheetHelper.class.php @@ -0,0 +1,81 @@ +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; + } +} diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index e3d319ac..8b988eaf 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.37.4732 | Copyright © Anuko | +  Anuko Time Tracker 1.18.37.4733 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/WEB-INF/templates/report.tpl b/WEB-INF/templates/report.tpl index 4212431b..3cd8e40e 100644 --- a/WEB-INF/templates/report.tpl +++ b/WEB-INF/templates/report.tpl @@ -2,7 +2,7 @@ function chLocation(newLocation) { document.location = newLocation; } -{$forms.reportForm.open} +{$forms.reportViewForm.open}
@@ -162,7 +162,7 @@ @@ -171,14 +171,14 @@ {/if}
- +
{$i18n.label.mark_paid}: {$forms.reportForm.mark_paid_select_options.control} {$forms.reportForm.mark_paid_action_options.control} {$forms.reportForm.btn_mark_paid.control}
{$i18n.label.mark_paid}: {$forms.reportViewForm.mark_paid_select_options.control} {$forms.reportViewForm.mark_paid_action_options.control} {$forms.reportViewForm.btn_mark_paid.control}
- +
{$i18n.form.report.assign_to_invoice}: {$forms.reportForm.assign_invoice_select_options.control} {$forms.reportForm.recent_invoice.control} {$forms.reportForm.btn_assign.control}
{$i18n.form.report.assign_to_invoice}: {$forms.reportViewForm.assign_invoice_select_options.control} {$forms.reportViewForm.recent_invoice.control} {$forms.reportViewForm.btn_assign.control}
{/if} -{$forms.reportForm.close} +{$forms.reportViewForm.close} diff --git a/report.php b/report.php index 7ecabcc5..65d98e4e 100644 --- a/report.php +++ b/report.php @@ -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. diff --git a/reports.php b/reports.php index 10df3648..892b117e 100644 --- a/reports.php +++ b/reports.php @@ -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'); diff --git a/timesheet_add.php b/timesheet_add.php index c52fea6a..fa32ca7f 100644 --- a/timesheet_add.php +++ b/timesheet_add.php @@ -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'); -- 2.20.1