-
Notifications
You must be signed in to change notification settings - Fork 36
/
app_controller.php
356 lines (303 loc) · 10.8 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* AppController
*
* @package
* @author dogmatic
* @copyright Copyright (c) 2009
* @version $Id$
* @access public
*/
class AppController extends Controller
{
var $helpers = array(
'Html', 'Form', 'Javascript',
'Core.Status'
);
var $components = array(
// cake components
'Session',
//core components
'DebugKit.Toolbar', 'Core.Cron', 'Core.Config',
// components
'Filter.Filter' => array(
'actions' => array('admin_index')
)
);
/**
* actions where viewable will work.
*/
var $viewableActions = array(
'view'
);
function beforeFilter()
{
parent::beforeFilter();
//$this->Session->write( 'Auth', ClassRegistry::init( 'Core.User' )->find( 'first', array( 'conditions' => array( 'User.id' => 1 ) ) ) );
$this->__checkUrl();
$this->__setupLayout();
$this->__setupCache();
$this->set( 'commentModel', 'Comment' );
if ( isset( $this->params['prefix'] ) && $this->params['prefix'] == 'admin' && !in_array( $this->params['action'], $this->viewableActions ) )
{
$this->{$this->modelClass}->Behaviors->detach( 'Viewable' );
}
}
/**
* Check the url is www.
*
* will redirect to www. if it is not set.
*
* @return true;
*/
private function __checkUrl()
{
}
/**
* Setup layout based on the prefix.
*
* Sets the layout to the corect var based on what path the user visits.
*
* @return bool true
*/
private function __setupLayout()
{
$prefix = '';
if ( isset( $this->params['prefix'] ) )
{
$prefix = $this->params['prefix'];
}
switch( $prefix )
{
case 'admin':
$this->layout = 'admin';
break;
case 'client':
$this->layout = 'client';
break;
default:
$this->layout = 'default';
} // switch
return true;
}
private function __setupCache()
{
Cache::config(
'cms',
array(
'engine' => 'File',
'duration'=> 3600,
'probability'=> 100,
'prefix' => '',
'lock' => false,
'serialize' => true,
'path' => CACHE.'cms'
)
);
Cache::config(
'core',
array(
'engine' => 'File',
'duration'=> 3600,
'probability'=> 100,
'prefix' => '',
'lock' => false,
'serialize' => true,
'path' => CACHE.'core'
)
);
Cache::config(
'blog',
array(
'engine' => 'File',
'duration'=> 3600,
'probability'=> 100,
'prefix' => '',
'lock' => false,
'serialize' => true,
'path' => CACHE.'blog'
)
);
}
/**
* Common methods for the app
*/
protected function comment()
{
if ( !empty( $this->data ) )
{
if ( !isset( $this->data[Inflector::classify( $this->name )]['id'] ) )
{
$this->Session->setFlash( __( 'There is a problem with your comment.', true ) );
$this->redirect( $this->referer() );
}
$this->data['Comment']['foreign_id'] = $this->data[Inflector::classify( $this->name )]['id'];
$this->data['Comment']['class'] = $this->__getClassName();
$comment['Comment'] = $this->data['Comment'];
ClassRegistry::init( 'Core.Comment' )->create();
if ( ClassRegistry::init( 'Core.Comment' )->save( $comment ) )
{
$this->Session->setFlash( __( 'Your comment has been saved for review.', true ) );
$this->redirect( $this->referer() );
}
$this->Session->setFlash( __( 'Your comment could not be saved, please try again.', true ) );
$this->redirect( $this->referer() );
}
}
function __getClassName()
{
if ( isset( $this->params['plugin'] ) )
{
return Inflector::classify( $this->params['plugin'] ).'.'.Inflector::classify( $this->name );
}
else
{
return Inflector::classify( $this->name );
}
}
/**
* reorder records.
*
* uses named paramiters can use the following:
* - up: moves the record up.
* - down: moves the record down.
* - position: sets the position for the record.
*
* @param int $id the id of the record to move.
*
* @return does a redirect to the referer.
*/
protected function admin_reorder( $id = null )
{
$model = $this->modelNames[0];
if ( !$id )
{
$this->Session->setFlash( 'That '.$model.' could not be found', true );
$this->redirect( $this->referer() );
}
$this->$model->id = $id;
if ( !isset( $this->params['named']['direction'] ) )
{
$this->Session->setFlash( __( 'Please select the direction you would like to move the record.', true ) );
$this->redirect( $this->referer() );
}
$amount = ( isset( $this->params['named']['amount'] ) ) ? $this->params['named']['amount'] : 1;
switch( $this->params['named']['direction'] )
{
case 'position':
/**
* @todo set the position of the record after add
*/
break;
case 'up':
$this->$model->moveup( $id, $amount );
break;
case 'down':
$this->$model->movedown( $id, $amount );
break;
} // switch
$this->redirect( $this->referer() );
}
/**
* toggle records with an active table that is tinyint(1).
*
* @todo -cAppController Implement AppController.
* - check the table has "active" field
* - check its tinyint(1)
* - make better with saveField and not reading the whole record.
*
* @param mixed $id the id of the record
* @return n/a, redirects with different messages in {@see Session::setFlash}
*/
protected function admin_toggle( $id = null )
{
$model = $this->modelNames[0];
if ( !$id )
{
$this->Session->setFlash( 'That '.$model.' could not be found', true );
$this->redirect( $this->referer() );
}
$this->$model->id = $id;
$this->$model->recursive = -1;
$__data = $this->$model->read();
$__data[$model]['active'] = ( $__data[$model]['active'] ) ? 0 : 1;
if ( $this->$model->save( $__data, array( 'validate' => false ) ) )
{
$this->Session->setFlash( sprintf( __( 'The '.$model.' is now %s', true ), ( ( $__data[$model]['active'] ) ? __( 'active', true ) : __( 'disabled', true ) ) ) );
$this->redirect( $this->referer() );
}
$this->Session->setFlash( 'That '.$model.' could not be toggled', true );
$this->redirect( $this->referer() );
}
/**
* delete records.
*
* delete records throughout the app.
*
* @todo -cAppController Implement AppController.
* - make a confirm if the js box does not happen. eg open delete in new
* window there is no confirm, just delete.
* - undo thing... maybe save the whole record in the session and if click
* undo just save it back, or use soft delete and purge
*
* @param mixed $id the id of the record.
* @return n/a just redirects with different messages in {@see Session::setFlash}
*/
protected function admin_delete( $id = null )
{
$model = $this->modelNames[0];
if ( !$id )
{
$this->Session->setFlash( 'That '.$model.' could not be found', true );
$this->redirect( $this->referer() );
}
if ( $this->$model->delete( $id ) )
{
$this->Session->setFlash( __( 'The '.$model.' has been deleted', true ) );
$this->redirect( array( 'action' => 'index' ) );
}
}
function admin_commentPurge( $class = null )
{
if ( !$class )
{
$this->Session->setFlash( __( 'Nothing chosen to purge', true ) );
$this->redirect( $this->referer() );
}
if ( !Configure::read( 'Comments.purge' ) )
{
$this->Session->setFlash( __( 'Purge is disabled', true ) );
$this->redirect( $this->referer() );
}
$ids = ClassRegistry::init( 'Core.Comment' )->find(
'list',
array(
'fields' => array(
'Comment.id',
'Comment.id'
),
'conditions' => array(
'Comment.class' => $class,
'Comment.active' => 0,
'Comment.created < ' => date( 'Y-m-d H:i:s', strtotime( '-'.Configure::read( 'Comments.purge' ) ) )
)
)
);
if ( empty( $ids ) )
{
$this->Session->setFlash( __( 'Nothing to purge', true ) );
$this->redirect( $this->referer() );
}
$counter = 0;
foreach( $ids as $id )
{
if ( ClassRegistry::init( 'Core.Comment' )->delete( $id ) )
{
$counter++;
}
}
$this->Session->setFlash( sprintf( __( '%s comments were purged.', true ), $counter ) );
$this->redirect( $this->referer() );
}
}
?>