Fixed issue #4 - copy-paste error in fillDropdowns().
[timetracker.git] / WEB-INF / templates / time.tpl
1 <script>
2 // We need a few arrays to populate project and task dropdowns.
3 // When client selection changes, the project dropdown must be re-populated with only relevant projects.
4 // When project selection changes, the task dropdown must be repopulated similarly.
5 // Format:
6 // project_ids[143] = "325,370,390,400";  // Comma-separated list of project ids for client.
7 // project_names[325] = "Time Tracker";   // Project name.
8 // task_ids[325] = "100,101,302,303,304"; // Comma-separated list ot task ids for project.
9 // task_names[100] = "Coding";            // Task name.
10
11 // Prepare an array of project ids for clients.
12 project_ids = new Array();
13 {foreach $client_list as $client}
14   project_ids[{$client.id}] = "{$client.projects}";
15 {/foreach}
16 // Prepare an array of project names.
17 project_names = new Array();
18 {foreach $project_list as $project}
19   project_names[{$project.id}] = "{$project.name|escape:'javascript'}";
20 {/foreach}
21 // We'll use this array to populate project dropdown when client is not selected.
22 var idx = 0;
23 projects = new Array();
24 {foreach $project_list as $project}
25   projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
26   idx++;
27 {/foreach}
28
29 // Prepare an array of task ids for projects.
30 task_ids = new Array();
31 {foreach $project_list as $project}
32   task_ids[{$project.id}] = "{$project.tasks}";
33 {/foreach}
34 // Prepare an array of task names.
35 task_names = new Array();
36 {foreach $task_list as $task}
37   task_names[{$task.id}] = "{$task.name|escape:'javascript'}";
38 {/foreach}
39
40 // Mandatory top options for project and task dropdowns.
41 empty_label_project = '{$i18n.dropdown.select|escape:'javascript'}';
42 empty_label_task = '{$i18n.dropdown.select|escape:'javascript'}';
43
44 // The fillDropdowns function populates the "project" and "task" dropdown controls
45 // with relevant values.
46 function fillDropdowns() {
47   if(document.body.contains(document.timeRecordForm.client))
48     fillProjectDropdown(document.timeRecordForm.client.value);
49
50   fillTaskDropdown(document.timeRecordForm.project.value);
51 }
52
53 // The fillProjectDropdown function populates the project combo box with
54 // projects associated with a selected client (client id is passed here as id).
55 function fillProjectDropdown(id) {
56   var str_ids = project_ids[id];
57   var dropdown = document.getElementById("project");
58   // Determine previously selected item.
59   var selected_item = dropdown.options[dropdown.selectedIndex].value;
60
61   // Remove existing content.
62   dropdown.length = 0;
63   var project_reset = true;
64   // Add mandatory top option.
65   dropdown.options[0] = new Option(empty_label_project, '', true);
66
67   // Populate project dropdown.
68   if (!id) {
69     // If we are here, client is not selected.
70     var len = projects.length;
71     for (var i = 0; i < len; i++) {
72       dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
73       if (dropdown.options[i+1].value == selected_item)  {
74         dropdown.options[i+1].selected = true;
75         project_reset = false;
76       }
77     }
78   } else if (str_ids) {
79     var ids = new Array();
80     ids = str_ids.split(",");
81     var len = ids.length;
82
83     for (var i = 0; i < len; i++) {
84       var p_id = ids[i];
85       dropdown.options[i+1] = new Option(project_names[p_id], p_id);
86       if (dropdown.options[i+1].value == selected_item)  {
87         dropdown.options[i+1].selected = true;
88         project_reset = false;
89       }
90     }
91   }
92
93   // If project selection was reset - clear the tasks dropdown.
94   if (project_reset) {
95     dropdown = document.getElementById("task");
96     dropdown.length = 0;
97     dropdown.options[0] = new Option(empty_label_task, '', true);
98   }
99 }
100
101 // The fillTaskDropdown function populates the task combo box with
102 // tasks associated with a selected project (project id is passed here as id).
103 function fillTaskDropdown(id) {
104   var str_ids = task_ids[id];
105
106   var dropdown = document.getElementById("task");
107   if (dropdown == null) return; // Nothing to do.
108
109   // Determine previously selected item.
110   var selected_item = dropdown.options[dropdown.selectedIndex].value;
111
112   // Remove existing content.
113   dropdown.length = 0;
114   // Add mandatory top option.
115   dropdown.options[0] = new Option(empty_label_task, '', true);
116
117   // Populate the dropdown from the task_names array.
118   if (str_ids) {
119     var ids = new Array();
120     ids = str_ids.split(",");
121     var len = ids.length;
122
123     var idx = 1;
124     for (var i = 0; i < len; i++) {
125       var t_id = ids[i];
126       if (task_names[t_id]) {
127         dropdown.options[idx] = new Option(task_names[t_id], t_id);
128         idx++;
129       }
130     }
131
132     // If a previously selected item is still in dropdown - select it.
133     if (dropdown.options.length > 0) {
134       for (var i = 0; i < dropdown.options.length; i++) {
135         if (dropdown.options[i].value == selected_item)  {
136           dropdown.options[i].selected = true;
137         }
138       }
139     }
140   }
141 }
142
143 // The formDisable function disables some fields depending on what we have in other fields.
144 function formDisable(formField) {
145   formFieldValue = eval("document.timeRecordForm." + formField + ".value");
146   formFieldName = eval("document.timeRecordForm." + formField + ".name");
147
148   if (((formFieldValue != "") && (formFieldName == "start")) || ((formFieldValue != "") && (formFieldName == "finish"))) {
149     var x = eval("document.timeRecordForm.duration");
150     x.value = "";
151     x.disabled = true;
152     x.style.background = "#e9e9e9";
153   }
154
155   if (((formFieldValue == "") && (formFieldName == "start") && (document.timeRecordForm.finish.value == "")) || ((formFieldValue == "") && (formFieldName == "finish") && (document.timeRecordForm.start.value == ""))) {
156     var x = eval("document.timeRecordForm.duration");
157     x.value = "";
158     x.disabled = false;
159     x.style.background = "white";
160   }
161
162   if ((formFieldValue != "") && (formFieldName == "duration")) {
163     var x = eval("document.timeRecordForm.start");
164     x.value = "";
165     x.disabled = true;
166     x.style.background = "#e9e9e9";
167     var x = eval("document.timeRecordForm.finish");
168     x.value = "";
169     x.disabled = true;
170     x.style.background = "#e9e9e9";
171   }
172
173   if ((formFieldValue == "") && (formFieldName == "duration")) {
174     var x = eval("document.timeRecordForm.start");
175     x.disabled = false;
176     x.style.background = "white";
177     var x = eval("document.timeRecordForm.finish");
178     x.disabled = false;
179     x.style.background = "white";
180   }
181 }
182
183 // The setNow function fills a given field with current time.
184 function setNow(formField) {
185   var x = eval("document.timeRecordForm.start");
186   x.disabled = false;
187   x.style.background = "white";
188   var x = eval("document.timeRecordForm.finish");
189   x.disabled = false;
190   x.style.background = "white";
191   var today = new Date();
192   var time_format = '{$user->time_format}';
193   var obj = eval("document.timeRecordForm." + formField);
194   obj.value = today.strftime(time_format);
195   formDisable(formField);
196 }
197
198 function get_date() {
199   var date = new Date();
200   return date.strftime("%Y-%m-%d");
201 }
202
203 function get_time() {
204   var date = new Date();
205   return date.strftime("%H:%M");
206 }
207 </script>
208
209 <style>
210 .not_billable td {
211   color: #ff6666;
212 }
213 </style>
214
215 {$forms.timeRecordForm.open}
216 <table cellspacing="4" cellpadding="0" border="0">
217   <tr>
218     <td valign="top">
219       <table>
220 {if $on_behalf_control}
221         <tr>
222           <td align="right">{$i18n.label.user}:</td>
223           <td>{$forms.timeRecordForm.onBehalfUser.control}</td>
224         </tr>
225 {/if}
226 {if $user->isPluginEnabled('cl')}
227         <tr>
228           <td align="right">{$i18n.label.client}{if $user->isPluginEnabled('cm')} (*){/if}:</td>
229           <td>{$forms.timeRecordForm.client.control}</td>
230         </tr>
231 {/if}
232 {if $user->isPluginEnabled('iv')}
233         <tr>
234           <td align="right">&nbsp;</td>
235           <td><label>{$forms.timeRecordForm.billable.control}{$i18n.form.time.billable}</label></td>
236         </tr>
237 {/if}
238 {if ($custom_fields && $custom_fields->fields[0])}
239         <tr>
240           <td align="right">{$custom_fields->fields[0]['label']|escape:'html'}{if $custom_fields->fields[0]['required']} (*){/if}:</td><td>{$forms.timeRecordForm.cf_1.control}</td>
241         </tr>
242 {/if}
243 {if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
244         <tr>
245           <td align="right">{$i18n.label.project} (*):</td>
246           <td>{$forms.timeRecordForm.project.control}</td>
247         </tr>
248 {/if}
249 {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
250         <tr>
251           <td align="right">{$i18n.label.task} (*):</td>
252           <td>{$forms.timeRecordForm.task.control}</td>
253         </tr>
254 {/if}
255 {if (($smarty.const.TYPE_START_FINISH == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}
256         <tr>
257           <td align="right">{$i18n.label.start}:</td>
258           <td>{$forms.timeRecordForm.start.control}&nbsp;<input onclick="setNow('start');" type="button" tabindex="-1" value="{$i18n.button.now}"></td>
259         </tr>
260         <tr>
261           <td align="right">{$i18n.label.finish}:</td>
262           <td>{$forms.timeRecordForm.finish.control}&nbsp;<input onclick="setNow('finish');" type="button" tabindex="-1" value="{$i18n.button.now}"></td>
263         </tr>
264 {/if}
265 {if (($smarty.const.TYPE_DURATION == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}
266         <tr>
267           <td align="right">{$i18n.label.duration}:</td>
268           <td>{$forms.timeRecordForm.duration.control}&nbsp;{$i18n.form.time.duration_format}</td>
269         </tr>
270 {/if}
271       </table>
272     </td>
273     <td valign="top">
274       <table>
275         <tr><td>{$forms.timeRecordForm.date.control}</td></tr>
276       </table>
277     </td>
278   </tr>
279 </table>
280
281 <table>
282   <tr>
283     <td align="right">{$i18n.label.note}:</td>
284     <td align="left">{$forms.timeRecordForm.note.control}</td>
285   </tr>
286   <tr>
287     <td align="center" colspan="2">{$forms.timeRecordForm.btn_submit.control}</td>
288   </tr>
289 </table>
290
291 <table width="720">
292 <tr>
293   <td valign="top">
294 {if $time_records}
295       <table border='0' cellpadding='3' cellspacing='1' width="100%">
296       <tr>
297   {if $user->isPluginEnabled('cl')}
298         <td width="20%" class="tableHeader">{$i18n.label.client}</td>
299   {/if}
300   {if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
301         <td class="tableHeader">{$i18n.label.project}</td>
302   {/if}
303   {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
304         <td class="tableHeader">{$i18n.label.task}</td>
305   {/if}
306   {if (($smarty.const.TYPE_START_FINISH == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}
307         <td width="5%" class="tableHeader" align='right'>{$i18n.label.start}</td>
308         <td width="5%" class="tableHeader" align='right'>{$i18n.label.finish}</td>
309   {/if}
310         <td width="5%" class="tableHeader">{$i18n.label.duration}</td>
311         <td class="tableHeader">{$i18n.label.note}</td>
312         <td width="5%" class="tableHeader">{$i18n.label.edit}</td>
313       </tr>
314   {foreach $time_records as $record}
315       <tr bgcolor="{cycle values="#f5f5f5,#ccccce"}" {if !$record.billable} class="not_billable" {/if}>
316     {if $user->isPluginEnabled('cl')}
317         <td valign='top'>{$record.client|escape:'html'}</td>
318     {/if}
319     {if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
320         <td valign='top'>{$record.project|escape:'html'}</td>
321     {/if}
322     {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
323         <td valign='top'>{$record.task|escape:'html'}</td>
324     {/if}
325     {if (($smarty.const.TYPE_START_FINISH == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}
326         <td nowrap align='right' valign='top'>{if $record.start}{$record.start}{else}&nbsp;{/if}</td>
327         <td nowrap align='right' valign='top'>{if $record.finish}{$record.finish}{else}&nbsp;{/if}</td>
328     {/if}
329         <td align='right' valign='top'>{if $record.duration <> '0:00'}{$record.duration}{else}<font color="#ff0000">{$i18n.form.time.uncompleted}</font>{/if}</td>
330         <td valign='top'>{if $record.comment}{$record.comment|escape:'html'}{else}&nbsp;{/if}</td>
331         <td valign='top' align='center'>
332     {if $record.invoice_id}
333           &nbsp;
334     {else}
335           <a href='time_edit.php?id={$record.id}'>{$i18n.label.edit}</a>
336       {if $record.duration == '0:00'}
337           <input type='hidden' name='record_id' value='{$record.id}'>
338           <input type='hidden' name='browser_date' value=''>
339           <input type='hidden' name='browser_time' value=''>
340           <input type='submit' id='btn_stop' name='btn_stop' onclick='browser_date.value=get_date();browser_time.value=get_time()' value='{$i18n.button.stop}'>
341       {/if}
342     {/if}
343         </td>
344       </tr>
345   {/foreach}
346     </table>
347 {/if}
348   </td>
349 </tr>
350 </table>
351 {if $time_records}
352 <table cellpadding="3" cellspacing="1" width="720">
353   <tr>
354     <td align="left">{$i18n.label.week_total}: {$week_total}</td>
355     <td align="right">{$i18n.label.day_total}: {$day_total}</td>
356   </tr>
357 </table>
358 {/if}
359 {$forms.timeRecordForm.close}