/
Handler.php
582 lines (526 loc) · 19.3 KB
/
Handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* Defines the AJAX actions used in Hermes.
*
* Copyright 2012-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/gpl GPL
* @package Hermes
*/
class Hermes_Ajax_Application_Handler extends Horde_Core_Ajax_Application_Handler
{
/**
* Add a new timer. Expects the following in $this->vars:
* - desc: The timer description.
* - client_id:
* - deliverable_id:
* - jobtype_id:
*
* @return array An array with an 'id' key.
*/
public function addTimer()
{
$id = Hermes::newTimer($this->vars->desc, $this->vars);
$timer = Hermes::getTimer($id);
return $timer;
}
/**
* Create a new jobtype. Takes the following from $this->vars:
* - name: (string) They type name.
* - billable: (boolean) Is this type billable?
* - enabled: (boolean) Is this type enabled?
* - rate: (double) The default hourly rate to use for this type.
*
* @return integer The type id of the newly created type.
*/
public function createJobType()
{
$job = array(
'id' => 0,
'name' => $this->vars->name,
'billable' => $this->vars->billable == 'on',
'enabled' => $this->vars->enabled == 'on',
'rate' => $this->vars->rate);
try {
$id = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->updateJobType($job);
$GLOBALS['notification']->push(_("Job type successfully added."), 'horde.success');
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e->getMessage(), 'horde.error');
}
return $id;
}
/**
* Create a new deliverable. Takes the following in $this->vars:
* - deliverable_id: The id of the deliverable if editing.
* - name: (string) The deliverable name.
* - active: (boolean) Is the deliverable active?
* - estimate: (integer) The estimate for this deliverable.
* - desc: (string) The description.
* - client_id: (string) The client id this deliverable is for.
*/
public function updateDeliverable()
{
// Only local Hermes deliverables are editable.
$deliverable_id = str_replace('hermes:', '', $this->vars->deliverable_id);
$deliverable = array(
'id' => empty($this->vars->deliverable_id) ? 0 : $deliverable_id,
'name' => $this->vars->name,
'active' => $this->vars->active == 'on',
'estimate' => $this->vars->estimate,
'description' => $this->vars->desc,
'client_id' => $this->vars->client_id);
try {
$result = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->updateDeliverable($deliverable);
$GLOBALS['notification']->push(_("Deliverable successfully added."), 'horde.success');
return $result;
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e->getMessage(), 'horde.error');
}
}
public function deleteDeliverable()
{
// Only local Hermes deliverables are editable.
$deliverable_id = str_replace('hermes:', '', $this->vars->deliverable_id);
try {
$GLOBALS['injector']
->getInstance('Hermes_Driver')
->deleteDeliverable($deliverable_id);
$GLOBALS['notification']->push(_("Deliverable successfully deleted."), 'horde.success');
return true;
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e->getMessage(), 'horde.error');
}
}
/**
* Delete a jobtype. Takes the following in $this->vars:
* - id: The jobtype id to delete.
*
*/
public function deleteJobType()
{
try {
$GLOBALS['injector']->getInstance('Hermes_Driver')
->deleteJobType($this->vars->id);
$GLOBALS['notification']->push(_("Job type successfully deleted."), 'horde.success');
return true;
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e->getMessage(), 'horde.error');
}
}
/**
* Remove a slice. Expects the following in $this->vars:
* - id: The slice id
*
* @return boolean
*/
public function deleteSlice()
{
$sid = array('id' => $this->vars->id, 'delete' => true);
try {
$result = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->updateTime(array($sid));
$GLOBALS['notification']->push(
_("Your time entry was successfully deleted."), 'horde.success');
return $result;
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e, 'horde.error');
}
}
/**
* Enter a new time slice. @see Hermes_Slice::readForm for the data expected
* to be sent in the posted form.
*
* @return array The new timeslice
*/
public function enterTime()
{
$slice = new Hermes_Slice();
$slice->readForm();
try {
$id = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->enterTime($slice['employee'], $slice);
$new = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->getHours(array('id' => $id));
if ($slice['employee'] == $GLOBALS['registry']->getAuth()) {
$GLOBALS['notification']
->push(_("Your time was successfully entered."), 'horde.success');
return current($new)->toJson();
} else {
$GLOBALS['notification']
->push(sprintf(_("The time was successfully entered for %s."), $slice['employee']), 'horde.success');
return true;
}
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e, 'horde.error');
}
}
/**
* Get a list of client deliverables suitable for building a select list.
* Expects the following in $this->vars:
* - c: The client id, or an array of client ids if querying for specific
* clients. Returns all deliverables otherwise.
*
* @return array @see Hermes::getCostObjectType
*/
public function listDeliverablesSelect()
{
$client = !empty($this->vars->c) ? $this->vars->c : null;
return Hermes::getCostObjectType($client);
}
/**
* Get the list of Hermes-only deliverables for the requested client.
* - c: The client id
* - id: The optional deliverable id, if requesting a specific deliverable.
*/
public function listDeliverables()
{
global $injector;
if (!empty($this->vars->id)) {
$params = array('id' => $this->vars->id);
return array_values($injector->getInstance('Hermes_Driver')->listDeliverables($params));
}
// Only poll Hermes' deliverables if we have a client id since they
// are ALWAYS tied to a client. Otherwise, just return the list of
// external cost objects.
$client_id = !empty($this->vars->c) ? $this->vars->c : null;
if (!empty($client_id)) {
$objs = array_values($injector->getInstance('Hermes_Driver')
->listDeliverables(array('client_id' => $client_id)));
foreach ($objs as &$obj) {
$obj['id'] = 'hermes:' . $obj['id'];
}
return $objs;
}
$elts = array();
foreach (Hermes::getCostObjects($client_id, true) as $category) {
Horde_Array::arraySort($category['objects'], 'name');
foreach ($category['objects'] as $object) {
$elts[] = array(
'id' => $object['id'],
'client_id' => false,
'name' => sprintf('%s (%s)', htmlspecialchars(Horde_String::truncate($object['name'], 80)), htmlspecialchars($category['category'])),
'parent' => empty($object['parent']) ? 0 : $object['parent'],
'estimate' => empty($object['estimate']) ? 0 : $object['estimate'],
'active' => true,
'is_external' => true
);
}
}
return array_values($elts);
}
public function getDeliverableDetail()
{
global $injector;
$filters = array(
'costobject' => $this->vars->id
);
$hours = $injector->getInstance('Hermes_Driver')->getHours($filters);
$json = array();
foreach ($hours as $h) {
$json[] = $h->toJson();
}
return $json;
}
/**
* Return descriptions of job types. If $this->vars->id is present it is
* used to filter by the requested id.
*
* @return array An array describing the type.
*/
public function listJobTypes()
{
$id = !empty($this->vars->id) ? $this->vars->id : null;
return array_values($GLOBALS['injector']
->getInstance('Hermes_Driver')
->listJobTypes(array('id' => $id)));
}
/**
* Return the current list of timers.
*
* @param boolean $running_only Only return running timers if true.
*
* @return array An array of timer arrays. @see Hermes::listTimers()
*/
public function listTimers($running_only = false)
{
return Hermes::listTimers($running_only);
}
/**
* Fetch a collection of time slices. For now, just allows a search for
* all of a single employees time. Either submitted or not submitted.
* Expects the following values in $this->vars:
* - e: The employee id
* - s: Include submitted slices if true
* - sort: The sort-by value
* - dir: The sort direction
*
* @return array An array of time slice data.
*/
public function loadSlices()
{
$params = array(
'employee' => $this->vars->e,
'submitted' => $this->vars->s);
$json = array();
try {
$slices = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->getHours($params);
foreach ($slices as $slice) {
$json[] = $slice->toJson();
}
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e, 'horde.error');
}
return $json;
}
/**
* Pause a timer. Expects the following data in $this->vars:
* - t: The timer id
*
* @return boolean
*/
public function pauseTimer()
{
try {
$timer = Hermes::getTimer($this->vars->t);
} catch (Horde_Exception_NotFound $e) {
$GLOBALS['notification']->push(_("Invalid timer requested"), 'horde.error');
return false;
}
// Avoid pausing the same timer twice.
if ($timer['paused'] || $timer['time'] == 0) {
return true;
}
$timer['paused'] = true;
$timer['elapsed'] += time() - $timer['time'];
$timer['time'] = 0;
Hermes::updateTimer($this->vars->t, $timer);
return true;
}
/**
* Poll the server. Currently also returns the list of current timer data
* so the UI can be updated periodically.
*
* @return array An array of timer arrays. @see self::listTimers()
*/
public function poll()
{
// Return any elapsed time for timers
return $this->listTimers(true);
}
/**
* Perform a slice search.
*
* @return array The search results.
*/
public function search()
{
$criteria = $this->_readSearchForm();
$slices = $GLOBALS['injector']
->getInstance('Hermes_Driver')
->getHours($criteria);
$json = array();
foreach ($slices as $slice) {
$json[] = $slice->toJson();
}
return $json;
}
/**
* Restart a paused timer. Expects the following data in $this->vars:
* - t: The timer id.
*
* @return boolean
*/
public function startTimer()
{
try {
$timer = Hermes::getTimer($this->vars->t);
} catch (Horde_Exception_NotFound $e) {
$GLOBALS['notification']->push(_("Invalid timer requested"), 'horde.error');
return false;
}
$timer['paused'] = false;
$timer['time'] = time();
Hermes::updateTimer($this->vars->t, $timer);
return true;
}
/**
* Stop a timer. Expects the following in $this->vars:
* - t: The timer id.
* - restart:
*
* @return array An array describing the current timer state. Contains:
* - h: The total number of hours elapsed so far.
* - n: A note to apply to the description field of a time slice.
* - t: The new timer title, if restarting.
*/
public function stopTimer()
{
global $prefs;
try {
$timer = Hermes::getTimer($this->vars->t);
} catch (Horde_Exception_NotFound $e) {
$GLOBALS['notification']->push(_("Invalid timer requested"), 'horde.error');
return false;
}
$results = $timer;
$tname = $timer['name'];
$elapsed = ((!$timer['paused']) ? time() - $timer['time'] : 0 ) + $timer['elapsed'];
$results['h'] = round((float)$elapsed / 3600, 2);
if ($prefs->getValue('add_description')) {
$results['n'] = sprintf(_("Using the \"%s\" stop watch from %s %s to %s %s"), $tname, strftime($prefs->getValue('date_format_mini'), $this->vars->t), strftime($prefs->getValue('time_format'), $this->vars->t), strftime($prefs->getValue('date_format_mini'), time()), strftime($prefs->getValue('time_format'), time()));
} else {
$results['n'] = '';
}
$GLOBALS['notification']->push(sprintf(_("The stop watch \"%s\" has been stopped."), $tname), 'horde.success');
Hermes::clearTimer($this->vars->t);
if ($this->vars->restart == 'true') {
$now = time();
$timer['elapsed'] = 0;
$timer['paused'] = $results['paused'] = true;
$timer['time'] = $now;
Hermes::updateTimer($this->vars->t, $timer);
}
return $results;
}
/**
* Mark slices as submitted. Expects the following in $this->vars:
* - items: The slice ids to submit.
*
* @return boolean
*/
public function submitSlices()
{
$time = array();
foreach (explode(':', $this->vars->items) as $id) {
$time[] = array('id' => $id);
}
try {
$GLOBALS['injector']
->getInstance('Hermes_Driver')
->markAs('submitted', $time);
} catch (Horde_Exception $e) {
$GLOBALS['notification']->push(sprintf(_("There was an error submitting your time: %s"), $e->getMessage()), 'horde.error');
return false;
}
$GLOBALS['notification']->push(_("Your time was successfully submitted."), 'horde.success');
return true;
}
/**
* Update an existing jobtype. Takes the following from $this->vars:
* - id: (integer) The type id of the exsting type.
* - name: (string) They type name.
* - billable: (boolean) Is this type billable?
* - enabled: (boolean) Is this type enabled?
* - rate: (double) The default hourly rate to use for this type.
*
* @return boolean True on success/false on failure.
*/
public function updateJobType()
{
$job = array(
'id' => $this->vars->job_id,
'name' => $this->vars->name,
'billable' => $this->vars->billable == 'on',
'enabled' => $this->vars->enabled == 'on',
'rate' => $this->vars->rate);
try {
$GLOBALS['injector']
->getInstance('Hermes_Driver')
->updateJobType($job);
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e->getMessage(), 'horde.error');
return false;
}
$GLOBALS['notification']->push(_("Job type successfully updated."), 'horde.success');
return true;
}
/**
* Update a slice. @see Hermes_Slice::readForm() for the data expeted in
* the posted form.
*
* @return array The new slice data.
*/
public function updateSlice()
{
$slice = new Hermes_Slice();
$slice->readForm();
try {
$GLOBALS['injector']->getInstance('Hermes_Driver')->updateTime(array($slice));
if ($slice['employee'] == $GLOBALS['registry']->getAuth()) {
$GLOBALS['notification']->push(_("Your time was successfully updated."), 'horde.success');
} else {
$GLOBALS['notification']->push(sprintf(_("The time was successfully updated and saved to the time sheet of %s."), $slice['employee']), 'horde.success');
}
$new = $GLOBALS['injector']->getInstance('Hermes_Driver')->getHours(array('id' => $slice['id']));
return current($new)->toJson();
} catch (Hermes_Exception $e) {
$GLOBALS['notification']->push($e, 'horde.error');
}
}
/**
* Reads the search form submitted and return the search criteria
*
*/
protected function _readSearchForm()
{
$perms = $GLOBALS['injector']->getInstance('Horde_Perms');
$vars = $this->vars;
$criteria = array();
if ($GLOBALS['registry']->isAdmin(array('permission' => 'hermes:review'))) {
if (!empty($vars->employees[0])) {
$auth = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Auth')->create();
if (!$auth->hasCapability('list')) {
$criteria['employee'] = explode(',', $vars->employees[0]);
if (empty($criteria['employee'])) {
unset($criteria['employee']);
}
} else {
$criteria['employee'] = $vars->employees;
}
}
} else {
$criteria['employee'] = $GLOBALS['registry']->getAuth();
}
if (!empty($vars->client)) {
$criteria['client'] = $vars->client;
}
if (!empty($vars->type)) {
$criteria['jobtype'] = $vars->type;
}
if (!empty($vars->costobject)) {
$criteria['costobject'] = $vars->costobject;
}
if (!empty($vars->after_date)) {
$dt = new Horde_Date($vars->after_date);
$criteria['start'] = $dt->timestamp();
}
if (!empty($vars->before_date)) {
$dt = new Horde_Date($vars->before_date);
$criteria['end'] = $dt->add(86400)->timestamp();
}
if ($vars->billable !== '') {
$criteria['billable'] = $vars->billable;
}
if ($vars->submitted !== '') {
$criteria['submitted'] = $vars->submitted;
}
if ($vars->exported !== '') {
$criteria['exported'] = $vars->exported;
}
return $criteria;
}
}