<?php
include HOST_ROOT.'kernel/prototypes/tree_structure.php';
function articles () {
$Articles = new Articles();
$Articles->__set('object', $Articles);
$Articles->process();
return $Articles->__get('output');
}
class Articles EXTENDS dynamicModule {
protected $catId = 0;
protected $artId = 0;
protected $url = null;
protected $structure = null;
public function detectAction() {
Meta::addPath('<a href="'.HTTP_ROOT.'/articles/">Статьи</a>');
$this->structure = new ArticlesStructure();
$matches = array();
if($_SERVER['REQUEST_URI'] == '/articles/') {
$this->catId = 0;
$this->action = 'getCategories';
} elseif(preg_match('!/articles/(.*)?/!', $_SERVER['REQUEST_URI'], $matches) && strpos($_SERVER['REQUEST_URI'], '.html') === false) {
if(isset($matches[1])) {
$this->url = $matches[1];
$this->action = 'listCategory';
}
} elseif(preg_match('!/(.*)?.html!', $_SERVER['REQUEST_URI'], $matches)) {
$url = explode('/', $matches[1]);
$index = sizeof($url);
$this->url = $url[$index-1];
$this->action = 'getArticle';
} else {
header('HTTP/1.0 404 Not Found');
header('Location: '.HTTP_ROOT.'/errors/404.html');
}
}
protected function getCategories() {
$categories = $this->structure->listNodes($this->catId);
if(is_array($categories) && sizeof($categories)) {
$catWrapper = Template::getView(HOST_ROOT.'app/modules/articles/views/wrapper.categories.list');
$catItem = Template::getView(HOST_ROOT.'app/modules/articles/views/single.categories.list');
$cats = '';
foreach ($categories as $category) {
$cats .= Template::mountVars($catItem, $category);
}
$this->output .= Template::wrapContent($catWrapper, $cats);
unset($category, $categories, $catItem, $catWrapper);
}
}
protected function createCategory() {
$data['title'] = addslashes($_POST['title']);
$data['description'] = addslashes($_POST['description']);
$data['parent_id'] = (int) $_POST['parent_id'];
$this->structure->addNode($data);
}
protected function updateCategory() {
$data['title'] = addslashes($_POST['title']);
$data['description'] = addslashes($_POST['description']);
$data['parent_id'] = (int) $_POST['parent_id'];
$data['url'] = addslashes($_POST['url']);
$this->structure->saveNode((int) $_POST['cat_id'], $data);
}
protected function listCategory() {
$query = 'SELECT `id` FROM %sinfo_categories WHERE url="'.$this->url.'"';
$category = $this->db->getItem($query);
$this->catId = isset($category['id']) ? (int) $category['id'] : 0;
$this->getCategories();
$this->getArticles();
}
protected function getArticles() {
$articles = $this->structure->listItems($this->catId);
if(!is_array($articles) || !sizeof($articles)) {
$this->output = 'Ошибка! Данная категория не содержит материалов!<br />
Вы можете вернуться к предыдущей странице и выбрать другую ссылку.';
return ;
}
$artWrapper = Template::getView(HOST_ROOT.'app/modules/articles/views/wrapper.articles.list');
$artItem = Template::getView(HOST_ROOT.'app/modules/articles/views/single.articles.list');
$articlesList = '';
foreach ($articles as $article) {
$articlesList .= Template::mountVars($artItem, $article);
}
$articlesList = Template::wrapContent($artWrapper, $articlesList);
// Additional Data
$data['REQUEST_URI'] = $_SERVER['REQUEST_URI'];
$this->output = Template::mountVars($articlesList, $data);
}
protected function getArticle() {
$query = sprintf('SELECT * FROM %sinfo_articles WHERE url="%s"', DB_PREFIX, $this->url);
$article = $this->db->getItem($query);
print_r($article);
}
}
class ArticlesStructure EXTENDS TreeStructure {
protected function setData() {
$this->_Q_NODES_TABLE = 'info_categories';
$this->_Q_ITEMS_TABLE = 'info_articles';
}
}