Skip to content

Commit 88134e6

Browse files
committed
More progress on week view. Implemented adding records to existing rows without custom fields.
1 parent 7596217 commit 88134e6

4 files changed

Lines changed: 91 additions & 25 deletions

File tree

WEB-INF/lib/ttTimeHelper.class.php

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ static function makeRecordIdentifier($record) {
832832
global $user;
833833
// Start with client.
834834
if ($user->isPluginEnabled('cl'))
835-
$record_identifier = $record['client_id'] ? 'cl'.$record['client_id'] : '';
835+
$record_identifier = $record['client_id'] ? 'cl:'.$record['client_id'] : '';
836836
// Add billable flag.
837837
if (!empty($record_identifier)) $record_identifier .= ',';
838838
$record_identifier .= 'bl:'.$record['billable'];
@@ -851,6 +851,28 @@ static function makeRecordIdentifier($record) {
851851
return $record_identifier;
852852
}
853853

854+
// parseFromWeekViewRow - obtains field value encoded in row identifier.
855+
// For example, for a row id like "cl:546,bl:0,pr:23456,ts:27464,cf_1:example text"
856+
// requesting a client "cl" should return 546.
857+
static function parseFromWeekViewRow($row_id, $field_label) {
858+
// Find beginning of label.
859+
$pos = strpos($row_id, $field_label);
860+
if ($pos === false) return null; // Not found.
861+
862+
// Strip suffix from row id.
863+
$suffixPos = strrpos($row_id, '_');
864+
if ($suffixPos)
865+
$remaninder = substr($row_id, 0, $suffixPos);
866+
867+
// Find beginning of value.
868+
$posBegin = 1 + strpos($remaninder, ':', $pos);
869+
// Find end of value.
870+
$posEnd = strpos($remaninder, ',', $posBegin);
871+
if ($posEnd === false) $posEnd = strlen($remaninder);
872+
// Return value.
873+
return substr($remaninder, $posBegin, $posEnd - $posBegin);
874+
}
875+
854876
// makeRecordLabel - builds a human readable label for a row in week view,
855877
// which is a combination ot record properties.
856878
// Client - Project - Task - Custom field 1.
@@ -902,24 +924,15 @@ static function getDayHeadersForWeek($start_date) {
902924
return $dayHeaders;
903925
}
904926

905-
// getLockedDaysForWeek - builds an arrays of locked days in week.
927+
// getLockedDaysForWeek - builds an array of locked days in week.
906928
static function getLockedDaysForWeek($start_date) {
907929
global $user;
908930
$lockedDays = array();
909931
$objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
910-
$lockedDays[] = $user->isDateLocked($objDate);
911-
$objDate->incDay();
912-
$lockedDays[] = $user->isDateLocked($objDate);
913-
$objDate->incDay();
914-
$lockedDays[] = $user->isDateLocked($objDate);
915-
$objDate->incDay();
916-
$lockedDays[] = $user->isDateLocked($objDate);
917-
$objDate->incDay();
918-
$lockedDays[] = $user->isDateLocked($objDate);
919-
$objDate->incDay();
920-
$lockedDays[] = $user->isDateLocked($objDate);
921-
$objDate->incDay();
922-
$lockedDays[] = $user->isDateLocked($objDate);
932+
for ($i = 0; $i < 7; $i++) {
933+
$lockedDays[] = $user->isDateLocked($objDate);
934+
$objDate->incDay();
935+
}
923936
unset($objDate);
924937
return $lockedDays;
925938
}
@@ -947,12 +960,64 @@ static function getDayTotals($dataArray, $dayHeaders) {
947960
return $dayTotals;
948961
}
949962

963+
// dateFromDayHeader calculates date from start date and day header in week view.
964+
static function dateFromDayHeader($start_date, $day_header) {
965+
$objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
966+
$currentDayHeader = (string) $objDate->getDate(); // It returns an int on first call.
967+
if (strlen($currentDayHeader) == 1) // Which is an implementation detail of DateAndTime class.
968+
$currentDayHeader = '0'.$currentDayHeader; // Add a 0 for single digit day.
969+
$i = 1;
970+
while ($currentDayHeader != $day_header && $i < 7) {
971+
// Iterate through remaining days to find a match.
972+
$objDate->incDay();
973+
$currentDayHeader = $objDate->getDate(); // After incDay it returns a string with leading 0, when necessary.
974+
$i++;
975+
}
976+
return $objDate->toString(DB_DATEFORMAT);
977+
}
978+
950979
// insertDurationFromWeekView - inserts a new record in log tables from a week view post.
951980
static function insertDurationFromWeekView($fields, $err) {
952-
$err->add("Week view is work in progress. Inserting records is not yet implemented. Try again later.");
953-
// $row_id, $day_header, $posted_duration, $start_date) { // TODO: potential fields?
981+
global $i18n;
982+
global $user;
983+
984+
// Determine date for a new entry.
985+
$entry_date = ttTimeHelper::dateFromDayHeader($fields['start_date'], $fields['day_header']);
986+
$objEntryDate = new DateAndTime(DB_DATEFORMAT, $entry_date);
987+
988+
// Prohibit creating entries in future.
989+
if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES) && $fields['browser_today']) {
990+
$objBrowserToday = new DateAndTime(DB_DATEFORMAT, $fields['browser_today']);
991+
if ($objEntryDate->after($objBrowserToday)) {
992+
$err->add($i18n->getKey('error.future_date'));
993+
return false;
994+
}
995+
}
996+
997+
// Temporary check for custom field and exit if one is found, as this is not yet implemented.
998+
$temp = ttTimeHelper::parseFromWeekViewRow($fields['row_id'], 'cf_1');
999+
if ($temp) {
1000+
$err->add("Week view is work in progress. Inserting records with custom fields is not yet implemented. Try again later.");
1001+
return false;
1002+
}
1003+
1004+
// Prepare an array of fields for regular insert function.
1005+
$fields4insert = array();
1006+
$fields4insert['user_id'] = $user->getActiveUser();
1007+
$fields4insert['date'] = $entry_date;
1008+
$fields4insert['duration'] = $fields['duration'];
1009+
$fields4insert['client'] = ttTimeHelper::parseFromWeekViewRow($fields['row_id'], 'cl');
1010+
$fields4insert['billable'] = ttTimeHelper::parseFromWeekViewRow($fields['row_id'], 'bl');
1011+
$fields4insert['project'] = ttTimeHelper::parseFromWeekViewRow($fields['row_id'], 'pr');
1012+
$fields4insert['task'] = ttTimeHelper::parseFromWeekViewRow($fields['row_id'], 'ts');
1013+
1014+
// Try to insert a record.
1015+
$id = ttTimeHelper::insert($fields4insert);
1016+
if (!$id) return false; // Something failed.
1017+
1018+
// TODO: Deal with custom fieeld log here. Currently not implemented.
9541019

955-
return false; // Not implemented.
1020+
return true; // Not implemented.
9561021
}
9571022

9581023

WEB-INF/lib/ttUser.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ function isDateLocked($date)
196196
require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
197197

198198
// Calculate the last occurrence of a lock.
199-
$last = tdCron::getLastOccurrence($this->lock_spec, mktime());
199+
$last = tdCron::getLastOccurrence($this->lock_spec, time());
200200
$lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
201201
if ($date->before($lockdate)) {
202202
return true;

WEB-INF/templates/footer.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<br>
1313
<table cellspacing="0" cellpadding="4" width="100%" border="0">
1414
<tr>
15-
<td align="center">&nbsp;Anuko Time Tracker 1.13.0.3703| Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
15+
<td align="center">&nbsp;Anuko Time Tracker 1.13.0.3704| Copyright &copy; <a href="https://www.anuko.com/lp/tt_3.htm" target="_blank">Anuko</a> |
1616
<a href="https://www.anuko.com/lp/tt_4.htm" target="_blank">{$i18n.footer.credits}</a> |
1717
<a href="https://www.anuko.com/lp/tt_5.htm" target="_blank">{$i18n.footer.license}</a> |
1818
<a href="https://www.anuko.com/lp/tt_7.htm" target="_blank">{$i18n.footer.improve}</a>

week.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ function render(&$table, $value, $row, $column, $selected = false) {
272272
$form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
273273
if ($user->isPluginEnabled('iv'))
274274
$form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
275-
$form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
275+
$form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'get_date()')); // User current date, which gets filled in on btn_submit click.
276276
$form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
277277

278278
// If we have custom fields - add controls for them.
@@ -389,11 +389,12 @@ function render(&$table, $value, $row, $column, $selected = false) {
389389
if ($existingDuration == null) {
390390
// Insert a new record here.
391391
$fields = array();
392+
$fields['row_id'] = $dataArray[$rowNumber]['row_id'];
393+
$fields['day_header'] = $dayHeader;
394+
$fields['start_date'] = $startDate->toString(DB_DATEFORMAT); // To be able to determine date for the entry using $dayHeader.
395+
$fields['duration'] = $postedDuration;
396+
$fields['browser_today'] = $request->getParameter('browser_today', null);
392397
$result = ttTimeHelper::insertDurationFromWeekView($fields, $err);
393-
//$dataArray[$rowNumber]['row_id'],
394-
//$dayHeader,
395-
//$postedDuration,
396-
//$startDate->toString(DB_DATEFORMAT));
397398
} elseif ($postedDuration == null || 0 == ttTimeHelper::toMinutes($postedDuration)) {
398399
// Delete an already existing record here.
399400
$result = ttTimeHelper::delete($dataArray[$rowNumber][$dayHeader]['tt_log_id'], $user->getActiveUser());

0 commit comments

Comments
 (0)