summaryrefslogtreecommitdiff
path: root/app/Controller/HourlyRate.php
blob: f8f88d17e1be1cbe963d17d04badb029bf186c94 (plain)
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
<?php

namespace Controller;

/**
 * Hourly Rate controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class HourlyRate extends User
{
    /**
     * Display rate and form
     *
     * @access public
     */
    public function index(array $values = array(), array $errors = array())
    {
        $user = $this->getUser();

        $this->response->html($this->layout('hourlyrate/index', array(
            'rates' => $this->hourlyRate->getAllByUser($user['id']),
            'currencies_list' => $this->config->getCurrencies(),
            'values' => $values + array('user_id' => $user['id']),
            'errors' => $errors,
            'user' => $user,
        )));
    }

    /**
     * Validate and save a new rate
     *
     * @access public
     */
    public function save()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->hourlyRate->validateCreation($values);

        if ($valid) {

            if ($this->hourlyRate->create($values['user_id'], $values['rate'], $values['currency'], $values['date_effective'])) {
                $this->session->flash(t('Hourly rate created successfully.'));
                $this->response->redirect($this->helper->url('hourlyrate', 'index', array('user_id' => $values['user_id'])));
            }
            else {
                $this->session->flashError(t('Unable to save the hourly rate.'));
            }
        }

        $this->index($values, $errors);
    }

    /**
     * Confirmation dialag box to remove a row
     *
     * @access public
     */
    public function confirm()
    {
        $user = $this->getUser();

        $this->response->html($this->layout('hourlyrate/remove', array(
            'rate_id' => $this->request->getIntegerParam('rate_id'),
            'user' => $user,
        )));
    }

    /**
     * Remove a row
     *
     * @access public
     */
    public function remove()
    {
        $this->checkCSRFParam();
        $user = $this->getUser();

        if ($this->hourlyRate->remove($this->request->getIntegerParam('rate_id'))) {
            $this->session->flash(t('Rate removed successfully.'));
        }
        else {
            $this->session->flash(t('Unable to remove this rate.'));
        }

        $this->response->redirect($this->helper->url('hourlyrate', 'index', array('user_id' => $user['id'])));
    }
}