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
<?php

/**
 * @author Roman Chvanikoff <[email protected]>
 * @copyright 2011
 */

/**
 * The class that is extended by all controllers
 * 
 * @class Controller_Front
 */
class Controller_Front extends Controller {
    
    /**
     * Controller's response data
     * 
     * @var mixed $content
     */
    private $content = FALSE;
    
    /**
     * if current request have POST data - this var will be set to Validation object for the data
     * 
     * @var Validation $post
     */
    private $post = FALSE;
    
    /**
     * Name of the layout to use. After Controller finished its job - Layout object.
     * 
     * @var string|Layout $layout
     */
    protected $_layout = 'default';
    
    /**
	 * Automatically executed before the controller action. Can be used to set
	 * class properties, do authorization checks, and execute other custom code.
	 *
	 * @return  void
	 */
    public function before()
    {
        parent::before();
        
        /**
         * Set the language
         * 
         * @todo When multilanguage will be implemented - change it to current user's language
         */
        I18n::lang(Kohana::$config->load('application')->language);
        
        if ($this->request->post())
        {
            $this->post = Validation::factory($this->request->post());
        }
        // If request is not initial - we dont need layout
        if ($this->request->is_initial())
        {
            $this->_layout = Layout::factory($this->_layout);
        }
    }
    
    /**
	 * Automatically executed after the controller action. Can be used to apply
	 * transformation to the request response, add extra output, and execute
	 * other custom code.
	 *
	 * @return  void
	 */
    public function after()
    {
        if ($this->content === FALSE)
        {
            // Set the content by default as view called DIRECTORY/CONTROLLER/ACTION
            $this->set_content();
        }
        
        if ($this->request->is_ajax())
        {
            $this->response_ajax();
        }
        elseif ($this->request->is_initial())
        {
            if ($this->content instanceof View)
            {
                $this->content
                    ->set('post', $this->post);
            }
            $this->_layout
                ->set('content', $this->content)
                ->set('user', Auth::instance()->get_user());
        }
        else
        {
            $this->response->body($this->content);
        }
        
        parent::after();
    }
    
    /**
     * Set the content.
     *      If called without params:
     *      $this->set_content()
     *      content will be set as View object with name
     *      (CURRENT_REQUEST_DIRECTORY/)CURRENT_REQUEST_CONTROLLER/CURRENT_REQUEST_ACTION
     * 
     * @param mixed $data
     * @param bool $data_is_view
     * @param array $view_data
     * @return void
     */
    public function set_content($data = NULL, $data_is_view = TRUE, $view_data = array())
    {
        if ($data_is_view)
        {
            if ($data === NULL)
            {
                $data = ($this->request->directory() === '')
                    ? ''
                    : $this->request->directory().'/';
                $data .= $this->request->controller().'/';
                $data .= $this->request->action();
            }
            
            $data = View::factory($data, $view_data);
        }
        
        $this->content = $data;
    }
    
    /**
     * Response JSON to AJAX requests and send JSON headers
     * If any errors occured while executing controller, data will not be sent.
     * 
     * @return void
     */
    private function response_ajax()
    {
        $data = array('errors' => Errors::get_errors(FALSE));
        if ($data['errors'] === NULL)
        {
            $data['data'] = $this->content;
        }
        $this->response
            ->headers('Content-type', File::mime_by_ext('json'))
            ->body(json_encode($data));
    }
}