X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/8ecf916786c15d7bbe18bbb512b183c622be0bb4..88134e6891875e698d71e06806e9c0ddde5c03bd:/week.php diff --git a/week.php b/week.php index c2e85161..34efd56e 100644 --- a/week.php +++ b/week.php @@ -28,6 +28,8 @@ require_once('initialize.php'); import('form.Form'); +import('form.DefaultCellRenderer'); +import('form.Table'); import('ttUserHelper'); import('ttTeamHelper'); import('ttClientHelper'); @@ -40,7 +42,6 @@ if (!ttAccessCheck(right_data_entry)) { exit(); } -// TODO: redo this block to use week start date instead. // Initialize and store date in session. $cl_date = $request->getParameter('date', @$_SESSION['date']); $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date); @@ -50,6 +51,20 @@ if(!$cl_date) $cl_date = $selected_date->toString(DB_DATEFORMAT); $_SESSION['date'] = $cl_date; +// Determine selected week start and end dates. +$weekStartDay = $user->week_start; +$t_arr = localtime($selected_date->getTimestamp()); +$t_arr[5] = $t_arr[5] + 1900; +if ($t_arr[6] < $weekStartDay) + $startWeekBias = $weekStartDay - 7; +else + $startWeekBias = $weekStartDay; +$startDate = new DateAndTime(); +$startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5])); +$endDate = new DateAndTime(); +$endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5])); +// The above is needed to set date range (timestring) in page title. + // Use custom fields plugin if it is enabled. if ($user->isPluginEnabled('cf')) { require_once('plugins/CustomFields.class.php'); @@ -71,10 +86,6 @@ if ($user->isPluginEnabled('mq')){ } // Initialize variables. -$cl_start = trim($request->getParameter('start')); -$cl_finish = trim($request->getParameter('finish')); -$cl_duration = trim($request->getParameter('duration')); -$cl_note = trim($request->getParameter('note')); // Custom field. $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1']))); $_SESSION['cf_1'] = $cl_cf_1; @@ -95,8 +106,59 @@ $_SESSION['project'] = $cl_project; $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? null : @$_SESSION['task'])); $_SESSION['task'] = $cl_task; -// Elements of timeRecordForm. -$form = new Form('timeRecordForm'); +// Get the data we need to display week view. +// Get column headers, which are day numbers in month. +$dayHeaders = ttTimeHelper::getDayHeadersForWeek($startDate->toString(DB_DATEFORMAT)); +$lockedDays = ttTimeHelper::getLockedDaysForWeek($startDate->toString(DB_DATEFORMAT)); +// Build data array for the table. Format is described in the function.. +$dataArray = ttTimeHelper::getDataForWeekView($user->getActiveUser(), $startDate->toString(DB_DATEFORMAT), $endDate->toString(DB_DATEFORMAT), $dayHeaders); +// Build day totals (total durations for each day in week). +$dayTotals = ttTimeHelper::getDayTotals($dataArray, $dayHeaders); + +// TODO: refactoring ongoing down from here. + +// 1) Handle editable - not editable records properly meaning that UI should reflect this. +// 2) Start coding modification of existing records. +// 3) Then adding new records for existing rows. +// 4) Then add code and UI for adding a new row. + +// Actually this is work in progress at this point, even documenting the array, as we still miss control IDs, and +// editing entries is not yet implemented. When this is done, we will have to re-document the above. + +// Define rendering class for a label field to the left of durations. +class LabelCellRenderer extends DefaultCellRenderer { + function render(&$table, $value, $row, $column, $selected = false) { + $this->setOptions(array('width'=>200,'valign'=>'middle')); + $this->setValue(htmlspecialchars($value)); // This escapes HTML for output. + return $this->toString(); + } +} + +// Define rendering class for a single cell for time entry in week view table. +class TimeCellRenderer extends DefaultCellRenderer { + function render(&$table, $value, $row, $column, $selected = false) { + $field_name = $table->getValueAt($row,$column)['control_id']; // Our text field names (and ids) are like x_y (row_column). + $field = new TextField($field_name); + // Disable control if the date is locked. + global $lockedDays; + if ($lockedDays[$column-1]) + $field->setEnabled(false); + $field->setFormName($table->getFormName()); + $field->setSize(2); + $field->setValue($table->getValueAt($row,$column)['duration']); + // Disable control when time entry mode is TYPE_START_FINISH and there is no value in control + // because we can't supply start and finish times in week view - there are no fields for them. + global $user; + if (!$field->getValue() && TYPE_START_FINISH == $user->record_type) { + $field->setEnabled(false); + } + $this->setValue($field->getHtml()); + return $this->toString(); + } +} + +// Elements of weekTimeForm. +$form = new Form('weekTimeForm'); if ($user->canManageTeam()) { $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true)); @@ -112,6 +174,20 @@ if ($user->canManageTeam()) { } } +// Create week_durations table. +$table = new Table('week_durations'); +// $table->setIAScript('markModified'); // TODO: write a script to mark table or particular cells as modified. +$table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0')); +$table->setRowOptions(array('class'=>'tableHeaderCentered')); +$table->setData($dataArray); +// Add columns to table. +$table->addColumn(new TableColumn('label', '', new LabelCellRenderer(), $dayTotals['label'])); +for ($i = 0; $i < 7; $i++) { + $table->addColumn(new TableColumn($dayHeaders[$i], $dayHeaders[$i], new TimeCellRenderer(), $dayTotals[$dayHeaders[$i]])); +} +$table->setInteractive(false); +$form->addInputElement($table); + // Dropdown for clients in MODE_TIME. Use all active clients. if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) { $active_clients = ttTeamHelper::getActiveClients($user->team_id, true); @@ -196,7 +272,7 @@ $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar if ($user->isPluginEnabled('iv')) $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable)); -$form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click. +$form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'get_date()')); // User current date, which gets filled in on btn_submit click. $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit'))); // If we have custom fields - add controls for them. @@ -212,11 +288,14 @@ if ($custom_fields && $custom_fields->fields[0]) { 'empty'=>array(''=>$i18n->getKey('dropdown.select')))); } } +// TODO: the above needs to be refactored for week view. + + // Submit. if ($request->isPost()) { if ($request->getParameter('btn_submit')) { - +/* // Validate user input. if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client) $err->add($i18n->getKey('error.client')); @@ -276,7 +355,68 @@ if ($request->isPost()) { if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish)) $err->add($i18n->getKey('error.overlap')); } - +// TODO: refactor the above. +*/ + // Obtain values. Perhaps, it's best to iterate throigh posted parameters one by one, + // see if anything changed, and apply one change at a time until we see an error. + // TODO: check for locked days just in case. + $result = true; + $rowNumber = 0; + // Iterate through existing rows. + foreach ($dataArray as $row) { + // Iterate through days. + foreach ($dayHeaders as $key => $dayHeader) { + // Do not process locked days. + if ($lockedDays[$key]) continue; + // Make control id for the cell. + $control_id = $rowNumber.'_'.$dayHeader; + // Optain existing and posted durations. + $postedDuration = $request->getParameter($control_id); + $existingDuration = $dataArray[$rowNumber][$dayHeader]['duration']; + // If posted value is not null, check and normalize it. + if ($postedDuration) { + if (ttTimeHelper::isValidDuration($postedDuration)) { + $postedDuration = ttTimeHelper::normalizeDuration($postedDuration, false); // No leading zero. + } else { + $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration')); + $result = false; break; // Break out. Stop any further processing. + } + } + // Do not process if value has not changed. + if ($postedDuration == $existingDuration) + continue; + // Posted value is different. + if ($existingDuration == null) { + // Insert a new record here. + $fields = array(); + $fields['row_id'] = $dataArray[$rowNumber]['row_id']; + $fields['day_header'] = $dayHeader; + $fields['start_date'] = $startDate->toString(DB_DATEFORMAT); // To be able to determine date for the entry using $dayHeader. + $fields['duration'] = $postedDuration; + $fields['browser_today'] = $request->getParameter('browser_today', null); + $result = ttTimeHelper::insertDurationFromWeekView($fields, $err); + } elseif ($postedDuration == null || 0 == ttTimeHelper::toMinutes($postedDuration)) { + // Delete an already existing record here. + $result = ttTimeHelper::delete($dataArray[$rowNumber][$dayHeader]['tt_log_id'], $user->getActiveUser()); + } else { + $fields = array(); + $result = ttTimeHelper::modifyDurationFromWeekView($fields, $err); + //$result = ttTimeHelper::modifyDurationFromWeekView($dataArray[$rowNumber][$dayHeader]['tt_log_id'], $postedDuration, $user->getActiveUser()); + } + if (!$result) break; // Break out of the loop in case of first error. + } + if (!$result) break; // Break out of the loop in case of first error. + $rowNumber++; + } + if ($result) { + header('Location: week.php'); // Normal exit. + exit(); + } + $err->add($i18n->getKey('error.db')); + /* + // + // + // // Insert record. if ($err->no()) { $id = ttTimeHelper::insert(array( @@ -333,7 +473,7 @@ if ($request->isPost()) { // Cannot complete, redirect for manual edit. header('Location: time_edit.php?id='.$record_id); exit(); - } + }*/ } elseif ($request->getParameter('onBehalfUser')) { if($user->canManageTeam()) { @@ -352,24 +492,9 @@ if ($request->isPost()) { $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date); -// Determine selected week start and end dates. -$weekStartDay = $user->week_start; -$t_arr = localtime($selected_date->getTimestamp()); -$t_arr[5] = $t_arr[5] + 1900; -if ($t_arr[6] < $weekStartDay) - $startWeekBias = $weekStartDay - 7; -else - $startWeekBias = $weekStartDay; -$startDate = new DateAndTime(); -$startDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+$startWeekBias,$t_arr[5])); -$endDate = new DateAndTime(); -$endDate->setTimestamp(mktime(0,0,0,$t_arr[4]+1,$t_arr[3]-$t_arr[6]+6+$startWeekBias,$t_arr[5])); -// The above is needed to set date range (timestring) in page title. Consider refactoring, possibly moving into a function. - $smarty->assign('selected_date', $selected_date); $smarty->assign('week_total', $week_total); -$smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date)); -$smarty->assign('time_records', ttTimeHelper::getGroupedRecordsForInterval($user->getActiveUser(), $startDate->toString(DB_DATEFORMAT), $endDate->toString(DB_DATEFORMAT))); + $smarty->assign('client_list', $client_list); $smarty->assign('project_list', $project_list); $smarty->assign('task_list', $task_list); @@ -377,21 +502,6 @@ $smarty->assign('forms', array($form->getName()=>$form->toArray())); $smarty->assign('onload', 'onLoad="fillDropdowns()"'); $smarty->assign('timestring', $startDate->toString($user->date_format).' - '.$endDate->toString($user->date_format)); -// Prepare and assign date headers. Note how startDate moves to the end of the week, so it no longer holds correct start week value. -$smarty->assign('day_header_0', $startDate->toString($user->date_format)); -$startDate->incDay(); -$smarty->assign('day_header_1', $startDate->toString($user->date_format)); -$startDate->incDay(); -$smarty->assign('day_header_2', $startDate->toString($user->date_format)); -$startDate->incDay(); -$smarty->assign('day_header_3', $startDate->toString($user->date_format)); -$startDate->incDay(); -$smarty->assign('day_header_4', $startDate->toString($user->date_format)); -$startDate->incDay(); -$smarty->assign('day_header_5', $startDate->toString($user->date_format)); -$startDate->incDay(); -$smarty->assign('day_header_6', $startDate->toString($user->date_format)); - $smarty->assign('title', $i18n->getKey('title.time')); $smarty->assign('content_page_name', 'week.tpl'); $smarty->display('index.tpl');