ACC SHELL
<?php // -*- mode: php; indent-tabs-mode: t -*-
/* $Id$ */
class ArticlesController extends AppController
{
var $name = 'Articles';
var $components = array('Session', 'Pagination', 'Filter');
var $uses = array('Article', 'User', 'File');
var $helpers = array('Html', 'Javascript', 'Pagination', 'Filter');
/**
*
*/
function beforeFilter()
{
if (! empty($this->params['admin'])) {
if (! $this->Session->read('logged_user')) {
$this->Session->write('redir_to', '/admin/articles/index');
$this->redirect('/admin/login');
exit();
}
}
}
/**
*
*/
function parseFiles()
{
// note: register_globals
$r_files = array(
array('id' => 0, 'name' => '', 'size' => 0),
array('id' => 0, 'name' => '', 'size' => 0),
array('id' => 0, 'name' => '', 'size' => 0),
array('id' => 0, 'name' => '', 'size' => 0),
array('id' => 0, 'name' => '', 'size' => 0),
);
for ($i = 0; $i < 5; $i++) {
if (! empty($_REQUEST['files'][$i])
&& preg_match('#files/view/(\d+)#', $_REQUEST['files'][$i], $m)
&& ($file = $this->File->findById($m[1]))) {
$file = $file['File'];
$r_files[$i] = array(
'id' => $file['id'],
'name' => $file['file_name'],
'size' => $file['file_size']
);
}
}
return $r_files;
}
/* admin methods */
/**
*
*/
function admin_index()
{
$this->Article->recursive = 0;
$this->Filter->init($this);
$this->Filter->setFilter(aa('title', 'Nadpis'), NULL, a('~', '!~'));
$this->Filter->setFilter(aa('article_type', 'Typ'), array('Novinka', 'Výstavy', 'Publikace'), a('='));
$this->Filter->filter($f, $cond);
$this->set('filters', $f);
$this->Pagination->controller = &$this;
$this->Pagination->show = 35;
$this->Pagination->init(
$cond,
'Article',
NULL,
array('id', 'title', 'created_d', 'article_type'),
0
);
$this->set('articles', $this->Article->findAll($cond, NULL,
$this->Pagination->order,
$this->Pagination->show,
$this->Pagination->page
));
}
/**
*
*/
function admin_logout()
{
$this->Session->del('logged_user');
$this->redirect('/admin');
exit();
}
/**
*
*/
function admin_login()
{
// posunout jinam, tady to nema co delat!
// create separate admin controller?
if ($this->Session->read('logged_user')) {
if (! ($redir_to = $this->Session->read('redir_to'))) {
$redir_to = '/admin/articles/index';
}
$this->Session->del('redir_to');
$this->redirect($redir_to);
exit();
}
if (! empty($this->params['form']['f_submit'])) {
$user = $this->User->loginUser(
$this->params['form']['logname'],
$this->params['form']['password']
);
if (! $user) {
$this->Session->setFlash('Neplatné přihlašovací údaje!');
$this->Session->del('logged_user');
$this->redirect('/admin/login');
exit();
}
$this->Session->write('logged_user', $user);
if (! ($redir_to = $this->Session->read('redir_to'))) {
$redir_to = '/admin/articles/index';
}
$this->Session->del('redir_to');
$this->redirect($redir_to);
exit();
}
}
function admin_edit($id = NULL)
{
if (empty($this->data)) {
if ($data = $this->Article->read(null, $id)) {
$data['Article']['perex'] = _html2tmce($data['Article']['perex']);
$data['Article']['content'] = _html2tmce($data['Article']['content']);
}
if ($data['Article']['published']) {
$this->data['Article']['published'] = 1;
}
if ($data['Article']['news']) {
$this->data['Article']['news_1'] = 1;
$this->data['Article']['news_2'] = 1;
}
$this->set('articles', $data);
if (! ($files = @unserialize($data['Article']['files']))) {
$files = array(
array('id' => 0, 'comment' => '', 'size' => 0),
array('id' => 0, 'comment' => '', 'size' => 0),
array('id' => 0, 'comment' => '', 'size' => 0),
array('id' => 0, 'comment' => '', 'size' => 0),
array('id' => 0, 'comment' => '', 'size' => 0),
);
}
$this->set('files', $files);
}
else {
if ((empty($this->data['Article']['article_type'])
&& ! empty($this->data['Article']['news_1']))
|| (! empty($this->data['Article']['article_type'])
&& ! empty($this->data['Article']['news_2']))) {
$this->data['Article']['news'] = 1;
}
else {
$this->data['Article']['news'] = 0;
}
if ($this->data['Article']['article_type'] == 2) {
$this->data['Article']['created_d'] = fromDate($this->data['Article']['created_d']);
}
else if (! $id) {
$this->data['Article']['created_d'] = date('Y-m-d H:i:s');
}
else {
$row = $this->Article->findById($id);
$this->data['Article']['created_d'] = $row['Article']['created_d'];
}
$this->data['Article']['publish_from'] = fromDate($this->data['Article']['publish_from']);
$this->data['Article']['publish_to'] = fromDate($this->data['Article']['publish_to']);
$this->data['Article']['title_idx'] = indexFromName(
date('ymd', strtotime($this->data['Article']['created_d'])) . '-' .
$this->data['Article']['title']
);
$this->data['Article']['perex']
= fix_local_links($this->data['Article']['perex'], $this->base);
$this->data['Article']['content']
= fix_local_links($this->data['Article']['content'], $this->base);
$this->data['Article']['files'] = serialize($this->parseFiles());
if ($this->Article->save($this->data)) {
if(is_object($this->Session)) {
// $this->Session->setFlash('The Article has been saved');
$this->redirect('/admin/articles/index');
} else {
$this->flash('Article saved.', '/admin/articles/index');
}
} else {
if(is_object($this->Session)) {
$this->Session->setFlash('Please correct errors below.');
}
$this->set('files', unserialize($this->data['Article']['files']));
$data = $this->data;
$this->set('articles', $data);
}
}
}
function admin_delete($id) {
$this->Article->del($id);
$this->redirect('/admin/articles/index');
}
/**
*
*/
function admin_toggle_html()
{
if (empty($_SESSION['tinymce_disabled'])) {
$_SESSION['tinymce_disabled'] = 1;
}
else {
unset($_SESSION['tinymce_disabled']);
}
$this->admin_index();
$this->render('admin_index');
}
}
?>
ACC SHELL 2018