Refactoring. Moved 2 more functions to ttWeekViewHelper class.
[timetracker.git] / WEB-INF / lib / ttWeekViewHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // ttWeekViewHelper class groups together functions used in week view.
30 class ttWeekViewHelper {
31
32   // getDayHeadersForWeek - obtains day column headers for week view, which are simply day numbers in month.
33   static function getDayHeadersForWeek($start_date) {
34     $dayHeaders = array();
35     $objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
36     $dayHeaders[] = (string) $objDate->getDate(); // It returns an int on first call.
37     if (strlen($dayHeaders[0]) == 1)              // Which is an implementation detail of DateAndTime class.
38       $dayHeaders[0] = '0'.$dayHeaders[0];        // Add a 0 for single digit day.
39     $objDate->incDay();
40     $dayHeaders[] = $objDate->getDate(); // After incDay it returns a string with leading 0, when necessary.
41     $objDate->incDay();
42     $dayHeaders[] = $objDate->getDate();
43     $objDate->incDay();
44     $dayHeaders[] = $objDate->getDate();
45     $objDate->incDay();
46     $dayHeaders[] = $objDate->getDate();
47     $objDate->incDay();
48     $dayHeaders[] = $objDate->getDate();
49     $objDate->incDay();
50     $dayHeaders[] = $objDate->getDate();
51     unset($objDate);
52     return $dayHeaders;
53   }
54
55   // getLockedDaysForWeek - builds an array of locked days in week.
56   static function getLockedDaysForWeek($start_date) {
57     global $user;
58     $lockedDays = array();
59     $objDate = new DateAndTime(DB_DATEFORMAT, $start_date);
60     for ($i = 0; $i < 7; $i++) {
61       $lockedDays[] = $user->isDateLocked($objDate);
62       $objDate->incDay();
63     }
64     unset($objDate);
65     return $lockedDays;
66   }
67
68   // makeRowLabel - builds a human readable label for a row in week view,
69   // which is a combination ot record properties.
70   // Client - Project - Task - Custom field 1.
71   // Note that billable property is not part of the label. Instead,
72   // we identify such records with a different color in week view.
73   static function makeRowLabel($record) {
74     global $user;
75     // Start with client.
76     if ($user->isPluginEnabled('cl'))
77       $label = $record['client'];
78
79     // Add project.
80     if (!empty($label) && !empty($record['project'])) $label .= ' - ';
81     $label .= $record['project'];
82
83     // Add task.
84     if (!empty($label) && !empty($record['task'])) $label .= ' - ';
85     $label .= $record['task'];
86
87     // Add custom field 1.
88     if ($user->isPluginEnabled('cf')) {
89       if (!empty($label) && !empty($record['cf_1_value'])) $label .= ' - ';
90       $label .= $record['cf_1_value'];
91     }
92
93     return $label;
94   }
95
96   // parseFromWeekViewRow - obtains field value encoded in row identifier.
97   // For example, for a row id like "cl:546,bl:0,pr:23456,ts:27464,cf_1:example text"
98   // requesting a client "cl" should return 546.
99   static function parseFromWeekViewRow($row_id, $field_label) {
100     // Find beginning of label.
101     $pos = strpos($row_id, $field_label);
102     if ($pos === false) return null; // Not found.
103
104     // Strip suffix from row id.
105     $suffixPos = strrpos($row_id, '_');
106     if ($suffixPos)
107       $remaninder = substr($row_id, 0, $suffixPos);
108
109     // Find beginning of value.
110     $posBegin = 1 + strpos($remaninder, ':', $pos);
111     // Find end of value.
112     $posEnd = strpos($remaninder, ',', $posBegin);
113     if ($posEnd === false) $posEnd = strlen($remaninder);
114     // Return value.
115     return substr($remaninder, $posBegin, $posEnd - $posBegin);
116   }
117 }