A bit of refactoring.
[timetracker.git] / WEB-INF / templates / reports.tpl
1 <script>
2 // We need a couple of array-like objects, one for associated task ids, another for task names.
3 // For performance, and because associated arrays are frowned upon in JavaScript, we'll use a simple object
4 // with properties for project tasks. Format:
5
6 // obj_tasks.p325 = "100,101,302,303,304"; // Tasks ids for project 325 are "100,101,302,303,304".
7 // obj_tasks.p408 = "100,302";  // Tasks ids for project 408 are "100,302".
8
9 // Create an object for task ids.
10 obj_tasks = {};
11 var project_prefix = "p"; // Prefix for project property.
12 var project_property;
13
14 // Populate obj_tasks with task ids for each relevant project.
15 {foreach $project_list as $project}
16   project_property = project_prefix + {$project.id};
17   obj_tasks[project_property] = "{$project.tasks}";
18 {/foreach}
19
20 // Prepare an array of task names.
21 // Format: task_names[0] = Array(100, 'Coding'), task_names[1] = Array(302, 'Debugging'), etc...
22 // First element = task_id, second element = task name.
23 task_names = new Array();
24 var idx = 0;
25 {foreach $task_list as $task}
26   task_names[idx] = new Array({$task.id}, "{$task.name|escape:'javascript'}");
27   idx++;
28 {/foreach}
29
30
31 // empty_label is the mandatory top option in the tasks dropdown.
32 empty_label = '{$i18n.dropdown.all|escape:'javascript'}';
33
34 // inArray - determines whether needle is in haystack array.
35 function inArray(needle, haystack) {
36   var length = haystack.length;
37   for(var i = 0; i < length; i++) {
38     if(haystack[i] == needle) return true;
39   }
40   return false;
41 }
42
43 // The fillTaskDropdown function populates the task combo box with
44 // tasks associated with a selected project_id.
45 function fillTaskDropdown(project_id) {
46   var str_task_ids;
47   // Get a string of comma-separated task ids.
48   if (project_id) {  
49     var property = "p" + project_id;
50     str_task_ids = obj_tasks[property];
51   }
52   if (str_task_ids) {
53     var task_ids = new Array(); // Array of task ids.
54     task_ids = str_task_ids.split(",");
55   }
56
57   var dropdown = document.getElementById("task");
58   // Determine previously selected item.
59   var selected_item = dropdown.options[dropdown.selectedIndex].value;
60
61   // Remove existing content.
62   dropdown.length = 0;
63   // Add mandatory top option.
64   dropdown.options[0] = new Option(empty_label, '', true);
65
66   // Populate the dropdown with associated tasks.
67   len = task_names.length;
68   var dropdown_idx = 0;
69   for (var i = 0; i < len; i++) {
70     if (!project_id) {
71       // No project is selected. Fill in all tasks.
72       dropdown.options[dropdown_idx+1] = new Option(task_names[i][1], task_names[i][0]);
73       dropdown_idx++;
74     } else if (str_task_ids) {
75       // Project is selected and has associated tasks. Fill them in.
76       if (inArray(task_names[i][0], task_ids)) {
77         dropdown.options[dropdown_idx+1] = new Option(task_names[i][1], task_names[i][0]);
78         dropdown_idx++;
79       }
80     }
81   }
82
83   // If a previously selected item is still in dropdown - select it.
84   if (dropdown.options.length > 0) {
85     for (var i = 0; i < dropdown.options.length; i++) {
86       if (dropdown.options[i].value == selected_item)  {
87         dropdown.options[i].selected = true;
88       }
89     }
90   }
91 }
92
93 // Build JavaScript array for assigned projects out of passed in PHP array.
94 var assigned_projects = new Array();
95 {if $assigned_projects}
96   {foreach $assigned_projects as $user_id => $projects}
97     assigned_projects[{$user_id}] = new Array();
98     {if $projects}
99       {foreach $projects as $idx => $project_id}
100         assigned_projects[{$user_id}][{$idx}] = {$project_id};
101       {/foreach}
102     {/if}
103   {/foreach}
104 {/if}
105
106 // selectAssignedUsers is called when a project is changed in project dropdown.
107 // It selects users on the form who are assigned to this project.
108 function selectAssignedUsers(project_id) {
109   var user_id;
110   var len;
111
112   for (var i = 0; i < document.reportForm.elements.length; i++) {
113     if ((document.reportForm.elements[i].type == 'checkbox') && (document.reportForm.elements[i].name == 'users[]')) {
114       user_id = document.reportForm.elements[i].value;
115       if (project_id)
116         document.reportForm.elements[i].checked = false;
117       else
118         document.reportForm.elements[i].checked = true;
119
120       if(assigned_projects[user_id] != undefined)
121         len = assigned_projects[user_id].length;
122       else
123         len = 0;
124
125       if (project_id != '')
126         for (var j = 0; j < len; j++) {
127           if (project_id == assigned_projects[user_id][j]) {
128             document.reportForm.elements[i].checked = true;
129             break;
130           }
131         }
132     }
133   }
134 }
135
136 // handleCheckboxes - unmarks and disables the "Totals only" checkbox when
137 // "no grouping" is selected in the associated dropdown.
138 // In future we need to improve this function and hide not relevant elements completely.
139 function handleCheckboxes() {
140   var totalsOnlyCheckbox = document.getElementById("chtotalsonly");
141   if ("no_grouping" == document.getElementById("group_by").value) {
142     // Unmark and disable the "Totals only" checkbox.
143     totalsOnlyCheckbox.checked = false;
144     totalsOnlyCheckbox.disabled = true;
145   } else
146     totalsOnlyCheckbox.disabled = false;
147 }
148 </script>
149
150 {$forms.reportForm.open}
151 <div style="padding: 0 0 10 0;">
152   <table border="0" class="divider">
153     <tr>
154       <td>
155         <table cellspacing="1" cellpadding="3" border="0">
156           <tr>
157             <td>{$i18n.label.fav_report}:</td><td>{$forms.reportForm.favorite_report.control}</td>
158             <td>{$forms.reportForm.btn_generate.control}&nbsp;{$forms.reportForm.btn_delete.control}</td>
159           </tr>
160         </table>
161       </td>
162     </tr>
163   </table>
164 </div>
165
166 <table cellspacing="4" cellpadding="7" border="0">
167   <tr>
168     <td valign="top" colspan="2" align="center">
169       <table border="0" cellpadding="3">
170 {if (($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id)) || ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN))}
171         <tr>
172   {if $user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id)}<td><b>{$i18n.label.client}</b></td>{else}<td>&nbsp;</td>{/if}
173           <td>&nbsp;</td>
174   {if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)}<td><b>{$i18n.label.option}</b></td>{else}<td>&nbsp;</td>{/if}
175         </tr>
176         <tr>
177           <td>{$forms.reportForm.client.control}</td>
178           <td>&nbsp;</td>
179           <td>{$forms.reportForm.option.control}</td>
180         </tr>
181 {/if}
182 {if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
183         <tr>
184           <td><b>{$i18n.label.project}</b></td>
185           <td>&nbsp;</td>
186   {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
187           <td><b>{$i18n.label.task}</b></td>
188   {/if}
189         </tr>
190 {/if}
191 {if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
192         <tr>
193           <td>{$forms.reportForm.project.control}</td>
194           <td>&nbsp;</td>
195   {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}
196           <td>{$forms.reportForm.task.control}</td>
197   {/if}
198         </tr>
199 {/if}
200 {if $user->isPluginEnabled('iv')}
201         <tr>
202           <td><b>{$i18n.form.time.billable}</b></td>
203           <td>&nbsp;</td>
204           <td><b>{$i18n.label.invoice}</b></td>
205         </tr>
206         <tr valign="top">
207           <td>{$forms.reportForm.include_records.control}</td>
208           <td>&nbsp;</td>
209           <td>{$forms.reportForm.invoice.control}</td>
210         </tr>
211 {/if}
212 {if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))}
213         <tr>
214           <td><b>{$i18n.label.paid_status}</b></td>
215         </tr>
216         <tr>
217           <td>{$forms.reportForm.paid_status.control}</td>
218         </tr>
219 {/if}
220 {if $user->canManageTeam() || $user->isClient()}
221         <tr>
222           <td colspan="3"><b>{$i18n.label.users}</b></td>
223         </tr>
224         <tr>
225           <td colspan="3">{$forms.reportForm.users.control}</td>
226         </tr>
227 {/if}
228         <tr>
229           <td><b>{$i18n.form.reports.select_period}</b></td>
230           <td>&nbsp;</td>
231           <td><b>{$i18n.form.reports.set_period}</b></td>
232         </tr>
233         <tr valign="top">
234           <td>{$forms.reportForm.period.control}</td>
235           <td align="right">{$i18n.label.start_date}:</td>
236           <td>{$forms.reportForm.start_date.control}</td>
237         </tr>
238         <tr>
239           <td></td>
240           <td align="right">{$i18n.label.end_date}:</td>
241           <td>{$forms.reportForm.end_date.control}</td>
242         </tr>
243         <tr><td colspan="3"><b>{$i18n.form.reports.show_fields}</b></td></tr>
244         <tr>
245           <td colspan="3">
246             <table border="0" width="100%">
247 {if $user->canManageTeam() || $user->isPluginEnabled('cl') || $user->isPluginEnabled('iv') || $user->isPluginEnabled('ps')}
248               <tr>
249   {if $user->isPluginEnabled('cl')}
250                 <td width="25%"><label>{$forms.reportForm.chclient.control}&nbsp;{$i18n.label.client}</label></td>
251   {/if}
252   {if ($user->canManageTeam() || $user->isClient()) && $user->isPluginEnabled('iv')}
253                 <td width="25%"><label>{$forms.reportForm.chinvoice.control}&nbsp;{$i18n.label.invoice}</label></td>
254   {/if}
255   {if ($user->canManageTeam() && $user->isPluginEnabled('ps'))}
256                 <td width="25%"><label>{$forms.reportForm.chpaid.control}&nbsp;{$i18n.label.paid}</label></td>
257   {/if}
258   {if $user->canManageTeam()}
259                 <td width="25%"><label>{$forms.reportForm.chip.control}&nbsp;{$i18n.label.ip}</label></td>
260   {/if}
261               </tr>
262 {/if}
263               <tr>
264                 <td width="25%">{if ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}<label>{$forms.reportForm.chproject.control}&nbsp;{$i18n.label.project}</label>{/if}</td>
265                 <td width="25%">{if (($smarty.const.TYPE_START_FINISH == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}<label>{$forms.reportForm.chstart.control}&nbsp;{$i18n.label.start}</label>{/if}</td>
266                 <td width="25%"><label>{$forms.reportForm.chduration.control}&nbsp;{$i18n.label.duration}</label></td>
267 {if ($user->can('manage_invoices') || $user->isClient()) || $user->isPluginEnabled('ex')}
268                   <td width="25%"><label>{$forms.reportForm.chcost.control}&nbsp;{$i18n.label.cost}</label></td>
269 {else}
270                   <td></td>
271 {/if}
272               </tr>
273               <tr>
274                 <td>{if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)}<label>{$forms.reportForm.chtask.control}&nbsp;{$i18n.label.task}</label>{/if}</td>
275                 <td>{if (($smarty.const.TYPE_START_FINISH == $user->record_type) || ($smarty.const.TYPE_ALL == $user->record_type))}<label>{$forms.reportForm.chfinish.control}&nbsp;{$i18n.label.finish}</label>{/if}</td>
276                 <td><label>{$forms.reportForm.chnote.control}&nbsp;{$i18n.label.note}</label></td>
277 {if ($custom_fields && $custom_fields->fields[0])}
278                 <td><label>{$forms.reportForm.chcf_1.control}&nbsp;{$custom_fields->fields[0]['label']|escape}</label></td>
279 {else}
280                 <td></td>
281 {/if}
282               </tr>
283             </table>
284           </td>
285         </tr>
286         <tr>
287           <td><b>{$i18n.form.reports.group_by}</b></td>
288         </tr>
289         <tr valign="top">
290           <td>{$forms.reportForm.group_by.control} <label>{$forms.reportForm.chtotalsonly.control} {$i18n.form.reports.totals_only}</label></td>
291         </tr>
292       </table>
293
294 <div style="padding: 10 0 10 0;">
295   <table border="0" class="divider">
296     <tr>
297       <td align="center">
298         <table cellspacing="1" cellpadding="3" border="0">
299           <tr>
300             <td>{$i18n.form.reports.save_as_favorite}:</td><td>{$forms.reportForm.new_fav_report.control}</td>
301             <td>{$forms.reportForm.btn_save.control}</td>
302           </tr>
303         </table>
304       </td>
305     </tr>
306   </table>
307 </div>
308
309       <table border="0" cellpadding="3" width="100%">
310         <tr><td colspan="3" height="50" align="center">{$forms.reportForm.btn_generate.control}</td></tr>
311       </table>
312     </td>
313   </tr>
314 </table>
315 {$forms.reportForm.close}