summaryrefslogtreecommitdiff
path: root/app/Helper/LayoutHelper.php
blob: 9384da1b0f0781612b7948dbeffd7b8bb0d5eb1d (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
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
<?php

namespace Kanboard\Helper;

use Kanboard\Core\Base;

/**
 * Layout Helper
 *
 * @package helper
 * @author  Frederic Guillot
 */
class LayoutHelper extends Base
{
    /**
     * Render a template without the layout if Ajax request
     *
     * @access public
     * @param  string $template Template name
     * @param  array  $params   Template parameters
     * @return string
     */
    public function app($template, array $params = array())
    {
        if ($this->request->isAjax()) {
            return $this->template->render($template, $params);
        }

        if (! isset($params['no_layout']) && ! isset($params['board_selector'])) {
            $params['board_selector'] = $this->projectUserRole->getActiveProjectsByUser($this->userSession->getId());
        }

        return $this->pageLayout($template, $params);
    }

    /**
     * Common layout for user views
     *
     * @access public
     * @param  string $template Template name
     * @param  array  $params   Template parameters
     * @return string
     */
    public function user($template, array $params)
    {
        if (isset($params['user'])) {
            $params['title'] = '#'.$params['user']['id'].' '.($params['user']['name'] ?: $params['user']['username']);
        }

        return $this->subLayout('user/layout', 'user/sidebar', $template, $params);
    }

    /**
     * Common layout for task views
     *
     * @access public
     * @param  string $template Template name
     * @param  array  $params   Template parameters
     * @return string
     */
    public function task($template, array $params)
    {
        $params['title'] = $params['task']['project_name'];
        return $this->subLayout('task/layout', 'task/sidebar', $template, $params);
    }

    /**
     * Common layout for project views
     *
     * @access public
     * @param  string $template
     * @param  array  $params
     * @param  string $sidebar
     * @return string
     */
    public function project($template, array $params, $sidebar = 'project/sidebar')
    {
        if (empty($params['title'])) {
            $params['title'] = $params['project']['name'];
        } elseif ($params['project']['name'] !== $params['title']) {
            $params['title'] = $params['project']['name'].' &gt; '.$params['title'];
        }

        return $this->subLayout('project/layout', $sidebar, $template, $params);
    }

    /**
     * Common layout for project user views
     *
     * @access public
     * @param  string $template
     * @param  array  $params
     * @return string
     */
    public function projectUser($template, array $params)
    {
        $params['filter'] = array('user_id' => $params['user_id']);
        return $this->subLayout('project_user/layout', 'project_user/sidebar', $template, $params);
    }

    /**
     * Common layout for config views
     *
     * @access public
     * @param  string $template
     * @param  array  $params
     * @return string
     */
    public function config($template, array $params)
    {
        if (! isset($params['values'])) {
            $params['values'] = $this->config->getAll();
        }

        if (! isset($params['errors'])) {
            $params['errors'] = array();
        }

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

    /**
     * Common layout for dashboard views
     *
     * @access public
     * @param  string $template
     * @param  array  $params
     * @return string
     */
    public function dashboard($template, array $params)
    {
        return $this->subLayout('app/layout', 'app/sidebar', $template, $params);
    }

    /**
     * Common layout for analytic views
     *
     * @access public
     * @param  string $template
     * @param  array  $params
     * @return string
     */
    public function analytic($template, array $params)
    {
        return $this->subLayout('analytic/layout', 'analytic/sidebar', $template, $params);
    }

    /**
     * Render page layout
     *
     * @access public
     * @param  string   $template   Template name
     * @param  array    $params     Key/value dictionary
     * @param  string   $layout     Layout name
     * @return string
     */
    public function pageLayout($template, array $params = array(), $layout = 'layout')
    {
        return $this->template->render(
            $layout,
            $params + array('content_for_layout' => $this->template->render($template, $params))
        );
    }

    /**
     * Common method to generate a sub-layout
     *
     * @access public
     * @param  string $sublayout
     * @param  string $sidebar
     * @param  string $template
     * @param  array  $params
     * @return string
     */
    public function subLayout($sublayout, $sidebar, $template, array $params = array())
    {
        $content = $this->template->render($template, $params);

        if ($this->request->isAjax()) {
            return $content;
        }

        $params['content_for_sublayout'] = $content;
        $params['sidebar_template'] = $sidebar;

        return $this->app($sublayout, $params);
    }
}