public function admin_edit($id = null)
{
if (!$id && empty($this->request->data)) {
$this->Session->setFlash(__d('croogo', 'Invalid content'), 'flash', array('class' => 'error'));
return $this->redirect(array('action' => 'index'));
}
$Node = $this->{$this->modelClass};
$Node->id = $id;
$typeAlias = $Node->field('type');
$type = $Node->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
if (!empty($this->request->data)) {
if ($Node->saveNode($this->request->data, $typeAlias)) {
Croogo::dispatchEvent('Controller.Nodes.afterEdit', $this, compact('data'));
$this->Session->setFlash(__d('croogo', '%s has been saved', $type['Type']['title']), 'flash', array('class' => 'success'));
$this->Croogo->redirect(array('action' => 'edit', $Node->id));
} else {
$this->Session->setFlash(__d('croogo', '%s could not be saved. Please, try again.', $type['Type']['title']), 'flash', array('class' => 'error'));
}
}
if (empty($this->request->data)) {
$this->Croogo->setReferer();
$data = $Node->read(null, $id);
if (empty($data)) {
throw new NotFoundException('Invalid id: ' . $id);
}
$data['Role']['Role'] = $Node->decodeData($data[$Node->alias]['visibility_roles']);
$this->request->data = $data;
}
$this->set('title_for_layout', __d('croogo', 'Edit %s: %s', $type['Type']['title'], $this->request->data[$Node->alias]['title']));
$this->_setCommonVariables($type);
}