This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
app_controller.php
318 lines (317 loc) · 10.1 KB
/
app_controller.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
<?php
/**
* Short description for file.
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2006, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.app
* @since CakePHP v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* AppController class
*
* @uses Controller
* @package cookbook
* @subpackage cookbook
*/
class AppController extends Controller {
/**
* components variable
*
* @var array
* @access public
*/
var $components = array('Auth', 'Users.Bakery', 'RequestHandler');
/**
* helpers variable
*
* @var array
* @access public
*/
var $helpers = array('Tree', 'Menu', 'Html', 'Form', 'Time', 'Javascript', 'Cache');
/**
* currentNode variable
*
* @var bool
* @access public
*/
var $currentNode = false;
/**
* currentPath variable
*
* @var bool
* @access public
*/
var $currentPath = array();
/**
* beforeFilter function
*
* @access public
* @return void
*/
function beforeFilter() {
// Store where they came from
$realReferer = $this->referer(null, true);
$sessionReferer = $this->Session->read('referer');
if ($this->name == 'App') {
} elseif (empty ($this->data) && !isset($this->params['requested'])) {
if ($realReferer) {
if ((!$sessionReferer) || ($realReferer != '/' . $this->params['url']['url'])) {
$this->Session->write('referer', $realReferer);
}
} elseif (!$sessionReferer) {
$this->Session->write('referer', $this->referer(array('action' => 'index')));
}
} elseif (!$sessionReferer) {
$this->Session->write('referer', $this->referer(array('action' => 'index')));
}
$defaultLang = Configure::read('Languages.default');
$this->params['theme'] = isset($this->params['theme'])?$this->params['theme']:'default';
$this->params['lang'] = isset($this->params['lang'])?$this->params['lang']:$defaultLang;
Configure::write('Config.language', $this->params['lang']);
if (($this->name != 'App') && !in_array($this->params['lang'], Configure::read('Languages.all'))) {
$this->Session->setFlash(__('Whoops, not a valid language.', true));
return $this->redirect($this->Session->read('referer'), 301, true);
}
if (isset($this->Node)) {
$this->Node->setLanguage($this->params['lang']);
} elseif (isset($this->{$this->modelClass}->Node)) {
$this->{$this->modelClass}->Node->setLanguage($this->params['lang']);
}
if (!$this->Auth->user()) {
if(!in_array($this->name, array('Users', 'CakeError')) && !isset($this->params['requested'])) {
$this->Session->write('Auth.redirect', '/' . $this->params['url']['url']);
}
$this->Auth->authError = __('Please login to continue', true);
}
$this->Auth->loginAction = array('lang' => $this->params['lang'], 'theme' => $this->params['theme'],
'plugin' => 'users', 'controller' => 'users', 'action' => 'login');
$this->Auth->autoRedirect = false;
$this->Auth->allow('display');
$this->{$this->modelClass}->currentUserId = $this->Auth->user('id');
}
/**
* beforeRender function
*
* @access public
* @return void
*/
function beforeRender() {
if (!isset ($this->viewVars['data'])) {
$this->set('data', $this->data);
}
$this->set('modelClass', $this->modelClass);
$this->set('isAdmin', isset($this->params['admin']));
if (!$this->RequestHandler->isAjax()) {
$this->layout = $this->params['theme'];
}
if ($this->name == 'App' && Configure::read()) {
$this->layout = 'error';
}
$this->set('defaultLang', Configure::read('Languages.default'));
}
/**
* redirect function
*
* @param mixed $url
* @param mixed $code
* @param bool $exit
* @access public
* @return void
*/
function redirect($url, $code = null, $exit = true) {
if (!is_array($url)) {
return parent::redirect($url, $code, $exit);
}
$defaults = array(
'controller' => Inflector::underscore($this->name),
'action' => $this->action,
'admin' => !empty($this->params['admin']),
'lang' => $this->params['lang'],
'theme' => $this->params['theme'],
);
$url = am($defaults, $url);
return parent::redirect($url, $code, $exit);
}
/**
* admin_add function
*
* @access public
* @return void
*/
function admin_add () {
if (!empty ($this->data)) {
$this->data['Revision']['user_id'] = $this->Auth->user('id');
if ($this->{$this->modelClass}->save($this->data)) {
$this->Session->setFlash($this->{$this->modelClass}->name . ' added');
$this->redirect($this->Session->read('referer'), null, true);
} else {
$this->Session->setFlash('Please correct the errors below.');
}
}
// Populate belongTo select list vars
foreach (array('belongsTo', 'hasAndBelongsToMany') as $type) {
foreach (array_keys($this->{$this->modelClass}->$type) as $model) {
if (is_array($this->{$this->modelClass}->$model->actsAs) && array_key_exists('Tree', $this->{$this->modelClass}->$model->actsAs)) {
$items = $this->{$this->modelClass}->$model->generateTreeList();
} else {
if (is_array($this->{$this->modelClass}->$model->displayField)) {
$order = implode($this->{$this->modelClass}->$model->displayField , ', ');
} else {
$order = $this->{$this->modelClass}->$model->alias . '.' . $this->{$this->modelClass}->$model->displayField;
}
$items = $this->{$this->modelClass}->$model->find('list', compact('order'));
}
$this->set(Inflector::underscore(Inflector::pluralize($model)), $items);
}
}
$this->render('admin_edit');
}
/**
* admin_delete function
*
* @param mixed $id
* @access public
* @return void
*/
function admin_delete($id) {
if ($this->{$this->modelClass}->del($id)) {
$this->Session->setFlash($this->modelClass . ' with id ' . $id . ' deleted');
} else {
$this->Session->setFlash('Can\'t delete ' . $this->modelClass . ' with id ' . $id);
}
$this->redirect($this->Session->read('referer'), null, true);
}
/**
* admin_edit function
*
* @param mixed $id
* @access public
* @return void
*/
function admin_edit($id) {
if (!$this->{$this->modelClass}->hasAny(array($this->{$this->modelClass}->primaryKey => $id))) {
$this->redirect(array('action' => 'index'), null, true);
}
if (!empty ($this->data)) {
//$this->data['Revision']['user_id'] = $this->Auth->user('id');
if ($this->{$this->modelClass}->save($this->data)) {
$this->Session->setFlash($this->{$this->modelClass}->alias . ' updated');
$this->redirect($this->Session->read('referer'), null, true);
} else {
$this->Session->setFlash('Please correct the errors below.');
}
} else {
$this->data = $this->{$this->modelClass}->read(null, $id);
}
// Populate belongTo select list vars
foreach (array('belongsTo', 'hasAndBelongsToMany') as $type) {
foreach (array_keys($this->{$this->modelClass}->$type) as $model) {
if (is_array($this->{$this->modelClass}->$model->actsAs) && array_key_exists('Tree', $this->{$this->modelClass}->$model->actsAs)) {
$items = $this->{$this->modelClass}->$model->generateTreeList();
} else {
if (is_array($this->{$this->modelClass}->$model->displayField)) {
$order = implode($this->{$this->modelClass}->$model->displayField , ', ');
} else {
$order = $this->{$this->modelClass}->$model->alias . '.' . $this->{$this->modelClass}->$model->displayField;
}
$items = $this->{$this->modelClass}->$model->find('list', compact('order'));
}
$this->set(Inflector::underscore(Inflector::pluralize($model)), $items);
}
}
}
/**
* admin_view function
*
* @param mixed $id
* @access public
* @return void
*/
function admin_view ($id) {
if (!$this->{$this->modelClass}->hasAny(array($this->{$this->modelClass}->primaryKey => $id))) {
$this->redirect(array('action' => 'index'), null, true);
}
$this->data = $this->{$this->modelClass}->read(null, $id);
if(!$this->data) {
$this->Session->setFlash('Invalid ' . $this->modelClass);
return $this->redirect($this->Session->read('referer'), null, true);
}
}
/**
* admin_index function
*
* @access public
* @return void
*/
function admin_index() {
if (!isset($this->__conditions)) {
App::import('Component', 'Filter');
$this->Filter =& new FilterComponent();
$this->Component->_loadComponents($this->Filter);
$this->Filter->startup($this);
$conditions = $this->Filter->parse();
} else {
$conditions = $this->__conditions;
}
unset($conditions[$this->modelClass . '.theme']);
unset($conditions[$this->modelClass . '.language']);
$Node = ClassRegistry::init('Node');
$collections = $Node->find('all', array('conditions' => array('Node.parent_id' => 1), 'fields' => 'Node.*, Revision.title'));
$books = $Node->find('all', array('conditions' => array('Node.depth' => 2), 'fields' => 'Node.*, Revision.title'));
$this->set(compact('collections', 'books'));
$this->data = $this->paginate($conditions);
}
/**
* admin_search method
*
* @param mixed $term
* @access public
* @return void
*/
function admin_search($term = null) {
if ($this->data) {
$term = trim($this->data[$this->modelClass]['query']);
$this->redirect(array(urlencode($term)));
}
if (!$term) {
$this->redirect(array('action' => 'index'));
}
$this->__conditions = $this->{$this->modelClass}->searchConditions($term);
$this->Session->setFlash(sprintf(__('All %s matching the term "%s"', true), Inflector::humanize($this->name), htmlspecialchars($term)));
$this->admin_index();
$this->render('admin_index');
}
/**
* appError method
*
* @param string $message
* @return void
* @access public
*/
function appError($message = 'x') {
if (Configure::read()) {
debug (func_get_args());
debug (Router::currentRoute()); die;
}
$this->Session->setFlash('Whoops! nothing to see there');
$this->redirect('/');
}
}
?>