X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/bdbadef4880db22d573cc3f154af3eb833942d56..88134e6891875e698d71e06806e9c0ddde5c03bd:/week.php diff --git a/week.php b/week.php index 9e1d5180..34efd56e 100644 --- a/week.php +++ b/week.php @@ -65,78 +65,6 @@ $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. -// Get column headers. -$dayHeaders = ttTimeHelper::getDayHeadersForWeek($startDate->toString(DB_DATEFORMAT)); - -// TODO: replace these two sample arrays with real data. -$durations_with_labels = array( - array( // Row 0. - 'id' => 'something goes here too', // Row identifier. - 'label' => 'This is a label for row 0', - 'day_0' => array('id' => '0_0', 'duration' => '00:00'), - 'day_1' => array('id' => '0_1', 'duration' => '01:00'), - 'day_2' => array('id' => '0_2', 'duration' => '02:00'), - 'day_3' => array('id' => '0_3', 'duration' => null), - 'day_4' => array('id' => '0_4', 'duration' => '04:00') - ), - array( // Row 1. - 'label' => 'This is a label for row 1', - 'day_0' => array('id' => '1_0', 'duration' => '00:30'), - 'day_1' => array('id' => '1_1', 'duration' => '01:30'), - 'day_2' => array('id' => '1_2', 'duration' => '02:30'), - ) -); - -$totals = array( - 'label' => 'Total:', - 'day_0' => '00:30', - 'day_1' => '02:30', - 'day_2' => '04:30', - 'day_3' => null, - 'day_4' => '04:00', - 'day_5' => null, - 'day_6' => null -); - -// 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)); - 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->getValueAtName($row,$column)['id']; // Our text field names (and ids) are like x_y (row_column). - $field = new TextField($field_name); - $field->setFormName($table->getFormName()); - $field->setSize(2); - $field->setValue($table->getValueAt($row,$column)['duration']); - $this->setValue($field->getHtml()); - return $this->toString(); - } -} - - - - - - -//$durations = ttTimeHelper::getDurationsForWeek($user->getActiveUser(), $startDate->toString(DB_DATEFORMAT), $endDate->toString(DB_DATEFORMAT)); - - - -$groupedRecords = ttTimeHelper::getGroupedRecordsForInterval($user->getActiveUser(), $startDate->toString(DB_DATEFORMAT), $endDate->toString(DB_DATEFORMAT)); - - - - - -$dayTotals = ttTimeHelper::getGroupedRecordsTotals($groupedRecords); - // Use custom fields plugin if it is enabled. if ($user->isPluginEnabled('cf')) { require_once('plugins/CustomFields.class.php'); @@ -158,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; @@ -182,6 +106,57 @@ $_SESSION['project'] = $cl_project; $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? null : @$_SESSION['task'])); $_SESSION['task'] = $cl_task; +// 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'); @@ -203,21 +178,16 @@ if ($user->canManageTeam()) { $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('valign'=>'top','class'=>'tableHeader')); -$table->setData($durations_with_labels); +$table->setRowOptions(array('class'=>'tableHeaderCentered')); +$table->setData($dataArray); // Add columns to table. -$table->addColumn(new TableColumn('label', '', new LabelCellRenderer(), $totals['label'])); -$table->addColumn(new TableColumn('day_0', $dayHeaders['day_header_0'], new TimeCellRenderer(), $totals['day_0'])); -$table->addColumn(new TableColumn('day_1', $dayHeaders['day_header_1'], new TimeCellRenderer(), $totals['day_1'])); -$table->addColumn(new TableColumn('day_2', $dayHeaders['day_header_2'], new TimeCellRenderer(), $totals['day_2'])); -$table->addColumn(new TableColumn('day_3', $dayHeaders['day_header_3'], new TimeCellRenderer(), $totals['day_3'])); -$table->addColumn(new TableColumn('day_4', $dayHeaders['day_header_4'], new TimeCellRenderer(), $totals['day_4'])); -$table->addColumn(new TableColumn('day_5', $dayHeaders['day_header_5'], new TimeCellRenderer())); -$table->addColumn(new TableColumn('day_6', $dayHeaders['day_header_6'], new TimeCellRenderer())); +$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); @@ -302,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. @@ -318,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')); @@ -382,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( @@ -439,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()) { @@ -458,14 +492,8 @@ if ($request->isPost()) { $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date); - - $smarty->assign('selected_date', $selected_date); $smarty->assign('week_total', $week_total); -$smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date)); -//$groupedRecords = ttTimeHelper::getGroupedRecordsForInterval($user->getActiveUser(), $startDate->toString(DB_DATEFORMAT), $endDate->toString(DB_DATEFORMAT)); -$smarty->assign('grouped_records', $groupedRecords); -$smarty->assign('grouped_records_totals', ttTimeHelper::getGroupedRecordsTotals($groupedRecords)); $smarty->assign('client_list', $client_list); $smarty->assign('project_list', $project_list); @@ -474,28 +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('date_0', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_0', $startDate->getDate()); -$startDate->incDay(); -$smarty->assign('date_1', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_1', $startDate->getDate()); -$startDate->incDay(); -$smarty->assign('date_2', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_2', $startDate->getDate()); -$startDate->incDay(); -$smarty->assign('date_3', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_3', $startDate->getDate()); -$startDate->incDay(); -$smarty->assign('date_4', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_4', $startDate->getDate()); -$startDate->incDay(); -$smarty->assign('date_5', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_5', $startDate->getDate()); -$startDate->incDay(); -$smarty->assign('date_6', $startDate->toString(DB_DATEFORMAT)); -$smarty->assign('day_header_6', $startDate->getDate()); - $smarty->assign('title', $i18n->getKey('title.time')); $smarty->assign('content_page_name', 'week.tpl'); $smarty->display('index.tpl');