Skip to content

Commit

Permalink
serialising data for ajax calls and adjusting notice to set the sessi…
Browse files Browse the repository at this point in the history
…on::flash() so that it is sent back as json when doing ajax calls
  • Loading branch information
dogmatic69 committed Jul 16, 2012
1 parent a15264b commit 86cd9ec
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,12 @@ public function beforeRender() {
parent::beforeRender();

switch(true) {
case $this->request->is('ajax'):
case (!empty($this->request->params['ext']) && $this->request->params['ext'] == 'json'):
$this->viewVars['json'] = array('json' => $this->viewVars['json']);
$this->set('_serialize', 'json');
if(isset($this->viewVars['json'])) {
$this->viewVars['json'] = array('json' => $this->viewVars['json']);
$this->set('_serialize', 'json');
}
//Configure::write('debug', 0);
break;

Expand Down Expand Up @@ -634,13 +637,30 @@ public function notice($message, $config = array()) {
if(isset($this->request->params['admin']) && $this->request->params['admin']) {
$element = 'messages/admin/' . $config['level'];
}
$this->Session->setFlash($message, $element, $vars);

$_redirect = false;
if($config['redirect'] || $config['redirect'] === '') {
if($config['redirect'] === true) {
$config['redirect'] = $this->referer();
}
$this->redirect($config['redirect']);
$_redirect = $config['redirect'];
}

if(!$this->request->is('ajax')) {
$this->Session->setFlash($message, $element, $vars);
if($_redirect !== false) {
$this->redirect($_redirect);
}
} else {
$vars['level'] = $config['level'];
$vars['message'] = $message;
$this->set(
'json',
array(
'flash' => $vars,
'redirect' => $_redirect
)
);
}

unset($_default, $config, $vars);
Expand Down

0 comments on commit 86cd9ec

Please sign in to comment.