Skip to content

Commit 0cc1dc5

Browse files
committed
Moved week view related functions in ttWeekViewHelper class.
1 parent ee5e708 commit 0cc1dc5

4 files changed

Lines changed: 188 additions & 190 deletions

File tree

WEB-INF/lib/ttTimeHelper.class.php

Lines changed: 0 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -587,56 +587,6 @@ static function overlaps($user_id, $date, $start, $finish, $record_id = null) {
587587
return false;
588588
}
589589

590-
// wvCanModify (weekViewCanModify) - determines if an already existing tt_log record
591-
// can be modified with a new user-provided duration.
592-
static function wvCanModify($tt_log_id, $new_duration, $err) {
593-
global $i18n;
594-
$mdb2 = getConnection();
595-
596-
// Determine if we have start time in record, as further checking does not makes sense otherwise.
597-
$sql = "select user_id, date, start, duration from tt_log where id = $tt_log_id";
598-
$res = $mdb2->query($sql);
599-
if (!is_a($res, 'PEAR_Error')) {
600-
if (!$res->numRows()) {
601-
$err->add($i18n->getKey('error.db')); // This is not expected.
602-
return false;
603-
}
604-
$val = $res->fetchRow();
605-
$oldDuration = $val['duration'];
606-
if (!$val['start'])
607-
return true; // There is no start time in the record, therefore safe to modify.
608-
}
609-
610-
// We do have start time.
611-
// Quick test if new duration is less then already existing.
612-
$newMinutes = ttTimeHelper::toMinutes($new_duration);
613-
$oldMinutes = ttTimeHelper::toMinutes($oldDuration);
614-
if ($newMinutes < $oldMinutes)
615-
return true; // Safe to modify.
616-
617-
// Does the new duration put the record beyond 24:00 boundary?
618-
$startMinutes = ttTimeHelper::toMinutes($val['start']);
619-
$newEndMinutes = $startMinutes + $newMinutes;
620-
if ($newEndMinutes > 1440) {
621-
// Invalid duration, as new duration puts the record beyond current day.
622-
$err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
623-
return false;
624-
}
625-
626-
// Does the new duration causes the record to overlap with others?
627-
$user_id = $val['user_id'];
628-
$date = $val['date'];
629-
$startMinutes = ttTimeHelper::toMinutes($val['start']);
630-
$start = ttTimeHelper::toAbsDuration($startMinutes);
631-
$finish = ttTimeHelper::toAbsDuration($newEndMinutes);
632-
if (ttTimeHelper::overlaps($user_id, $date, $start, $finish, $tt_log_id)) {
633-
$err->add($i18n->getKey('error.overlap'));
634-
return false;
635-
}
636-
637-
return true; // There are no conflicts, safe to modify.
638-
}
639-
640590
// getRecord - retrieves a time record identified by its id.
641591
static function getRecord($id, $user_id) {
642592
global $user;
@@ -724,137 +674,4 @@ static function getRecords($user_id, $date) {
724674

725675
return $result;
726676
}
727-
728-
// cellExists is a helper function for getDataForWeekView() to see if a cell with a given label
729-
// and a day header already exists.
730-
static function cellExists($row_id, $day_header, $dataArray) {
731-
foreach($dataArray as $row) {
732-
if ($row['row_id'] == $row_id && !empty($row[$day_header]['duration']))
733-
return true;
734-
}
735-
return false;
736-
}
737-
738-
// findRow returns an existing row position in $dataArray, -1 otherwise.
739-
static function findRow($row_id, $dataArray) {
740-
$pos = 0; // Row position in array.
741-
foreach($dataArray as $row) {
742-
if ($row['row_id'] == $row_id)
743-
return $pos;
744-
$pos++; // Increment for search.
745-
}
746-
return -1; // Row not found.
747-
}
748-
749-
// getDayTotals calculates total durations for each day from the existing data in $dataArray.
750-
static function getDayTotals($dataArray, $dayHeaders) {
751-
$dayTotals = array();
752-
753-
// Insert label.
754-
global $i18n;
755-
$dayTotals['label'] = $i18n->getKey('label.day_total');
756-
757-
foreach ($dataArray as $row) {
758-
foreach($dayHeaders as $dayHeader) {
759-
if (array_key_exists($dayHeader, $row)) {
760-
$minutes = ttTimeHelper::toMinutes($row[$dayHeader]['duration']);
761-
$dayTotals[$dayHeader] += $minutes;
762-
}
763-
}
764-
}
765-
// Convert minutes to hh:mm for display.
766-
foreach($dayHeaders as $dayHeader) {
767-
$dayTotals[$dayHeader] = ttTimeHelper::toAbsDuration($dayTotals[$dayHeader]);
768-
}
769-
return $dayTotals;
770-
}
771-
772-
// dateFromDayHeader calculates date from start date and day header in week view.
773-
static function dateFromDayHeader($start_date, $day_header) {
774-
$objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
775-
$currentDayHeader = (string) $objDate->getDate(); // It returns an int on first call.
776-
if (strlen($currentDayHeader) == 1) // Which is an implementation detail of DateAndTime class.
777-
$currentDayHeader = '0'.$currentDayHeader; // Add a 0 for single digit day.
778-
$i = 1;
779-
while ($currentDayHeader != $day_header && $i < 7) {
780-
// Iterate through remaining days to find a match.
781-
$objDate->incDay();
782-
$currentDayHeader = $objDate->getDate(); // After incDay it returns a string with leading 0, when necessary.
783-
$i++;
784-
}
785-
return $objDate->toString(DB_DATEFORMAT);
786-
}
787-
788-
// insertDurationFromWeekView - inserts a new record in log tables from a week view post.
789-
static function insertDurationFromWeekView($fields, $custom_fields, $err) {
790-
global $i18n;
791-
global $user;
792-
793-
// Determine date for a new entry.
794-
$entry_date = ttTimeHelper::dateFromDayHeader($fields['start_date'], $fields['day_header']);
795-
$objEntryDate = new DateAndTime(DB_DATEFORMAT, $entry_date);
796-
797-
// Prohibit creating entries in future.
798-
if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES) && $fields['browser_today']) {
799-
$objBrowserToday = new DateAndTime(DB_DATEFORMAT, $fields['browser_today']);
800-
if ($objEntryDate->after($objBrowserToday)) {
801-
$err->add($i18n->getKey('error.future_date'));
802-
return false;
803-
}
804-
}
805-
806-
// Prepare an array of fields for regular insert function.
807-
$fields4insert = array();
808-
$fields4insert['user_id'] = $user->getActiveUser();
809-
$fields4insert['date'] = $entry_date;
810-
$fields4insert['duration'] = $fields['duration'];
811-
$fields4insert['client'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'cl');
812-
$fields4insert['billable'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'bl');
813-
$fields4insert['project'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'pr');
814-
$fields4insert['task'] = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'ts');
815-
$fields4insert['note'] = $fields['note'];
816-
817-
// Try to insert a record.
818-
$id = ttTimeHelper::insert($fields4insert);
819-
if (!$id) return false; // Something failed.
820-
821-
// Insert custom field if we have it.
822-
$result = true;
823-
$cf_1 = ttWeekViewHelper::parseFromWeekViewRow($fields['row_id'], 'cf_1');
824-
if ($custom_fields && $cf_1) {
825-
if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
826-
$result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cf_1);
827-
elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
828-
$result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cf_1, null);
829-
}
830-
831-
return $result;
832-
}
833-
834-
835-
// modifyFromWeekView - modifies a duration of an existing record from a week view post.
836-
static function modifyDurationFromWeekView($fields, $err) {
837-
global $i18n;
838-
global $user;
839-
840-
// Possible errors: 1) Overlap if the existing record has start time. 2) Going beyond 24 hour boundary.
841-
// TODO: rename this function.
842-
// Handle different errors with specific error messages.
843-
if (!ttTimeHelper::wvCanModify($fields['tt_log_id'], $fields['duration'], $err)) {
844-
// $err->add($i18n->getKey('error.overlap'));
845-
return false;
846-
}
847-
848-
$mdb2 = getConnection();
849-
$duration = $fields['duration'];
850-
$tt_log_id = $fields['tt_log_id'];
851-
$user_id = $user->getActiveUser();
852-
$sql = "update tt_log set duration = '$duration' where id = $tt_log_id and user_id = $user_id";
853-
$affected = $mdb2->exec($sql);
854-
if (is_a($affected, 'PEAR_Error'))
855-
return false;
856-
857-
return true;
858-
}
859677
}
860-

0 commit comments

Comments
 (0)