summaryrefslogtreecommitdiff
path: root/app/Controller/Currency.php
blob: fac34a301f00bb74dd5b4870e1d3af861960fba0 (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;

/**
 * Currency controller
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Currency extends Base
{
    /**
     * Common layout for config views
     *
     * @access private
     * @param  string    $template   Template name
     * @param  array     $params     Template parameters
     * @return string
     */
    private function layout($template, array $params)
    {
        $params['board_selector'] = $this->projectPermission->getAllowedProjects($this->userSession->getId());
        $params['config_content_for_layout'] = $this->template->render($template, $params);

        return $this->template->layout('config/layout', $params);
    }

    /**
     * Display all currency rates and form
     *
     * @access public
     */
    public function index(array $values = array(), array $errors = array())
    {
        $this->response->html($this->layout('currency/index', array(
            'config_values' => array('application_currency' => $this->config->get('application_currency')),
            'values' => $values,
            'errors' => $errors,
            'rates' => $this->currency->getAll(),
            'currencies' => $this->config->getCurrencies(),
            'title' => t('Settings').' &gt; '.t('Currency rates'),
        )));
    }

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

        if ($valid) {

            if ($this->currency->create($values['currency'], $values['rate'])) {
                $this->session->flash(t('The currency rate have been added successfully.'));
                $this->response->redirect($this->helper->url('currency', 'index'));
            }
            else {
                $this->session->flashError(t('Unable to add this currency rate.'));
            }
        }

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

    /**
     * Save reference currency
     *
     * @access public
     */
    public function reference()
    {
        $values = $this->request->getValues();

        if ($this->config->save($values)) {
            $this->config->reload();
            $this->session->flash(t('Settings saved successfully.'));
        }
        else {
            $this->session->flashError(t('Unable to save your settings.'));
        }

        $this->response->redirect($this->helper->url('currency', 'index'));
    }
}