You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
463 lines
14 KiB
463 lines
14 KiB
9 years ago
|
<?php
|
||
|
|
||
|
class Faster {
|
||
|
|
||
|
const VERSION = '0.5';
|
||
|
|
||
|
public $tdir = NULL;
|
||
|
private $session = NULL;
|
||
|
|
||
|
/**
|
||
|
* @var AVE_DB
|
||
|
*/
|
||
|
public $db = FALSE;
|
||
|
|
||
|
/**
|
||
|
* @var AVE_Template
|
||
|
*/
|
||
|
public $template = FALSE;
|
||
|
protected $datadir = 'data';
|
||
|
|
||
|
public function __construct($tpl_dir) {
|
||
|
|
||
|
global $AVE_DB, $AVE_Template;
|
||
|
$this->db = $AVE_DB;
|
||
|
$this->template = $AVE_Template;
|
||
|
$this->tdir = $tpl_dir;
|
||
|
$this->datadir = BASE_DIR . '/modules/faster/' . $this->datadir . '/';
|
||
|
$this->session = $this->template->get_template_vars('sess');
|
||
|
}
|
||
|
|
||
|
/* --------------
|
||
|
Actions
|
||
|
---------------- */
|
||
|
/*
|
||
|
* Стартовая страница
|
||
|
*/
|
||
|
|
||
|
public function show_import() {
|
||
|
|
||
|
$d['files'] = $this->get_data_files();
|
||
|
|
||
|
$d['rubrics'] = $this->get_rubrics();
|
||
|
|
||
|
$d['sysblocks'] = $this->get_sys_blocks();
|
||
|
|
||
|
$this->template->assign('content', $this->view('import', $d));
|
||
|
}
|
||
|
|
||
|
public function ajax_rub_delete() {
|
||
|
|
||
|
/* $this->is_ajax() OR exit('no ajax');
|
||
|
!empty($_POST['file']) OR exit('post empty');
|
||
|
file_exists($this->datadir.$_POST['file'].'.php') OR exit('file not found'); */
|
||
|
|
||
|
if ($this->is_ajax() AND !empty($_POST['file']) AND file_exists($this->datadir . $_POST['file'] . '.php')) {
|
||
|
|
||
|
if (unlink($this->datadir . $_POST['file'] . '.php'))
|
||
|
$this->send_json(array('type' => 'success', 'message' => 'Файл конфигурации удален'));
|
||
|
else
|
||
|
$this->send_json(array('type' => 'error', 'message' => 'Файл найден но не может быть удален.'));
|
||
|
}
|
||
|
else
|
||
|
$this->send_json(array('type' => 'error', 'message' => 'Файл не найден или не получен параметр.'));
|
||
|
}
|
||
|
|
||
|
public function ajax_rub_export() {
|
||
|
|
||
|
if (!empty($_POST['rubs']) AND is_array($_POST['rubs'])) {
|
||
|
|
||
|
$c = 0;
|
||
|
foreach ($_POST['rubs'] as $id) {
|
||
|
|
||
|
if ($this->export_rubric($id))
|
||
|
$c++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if ($this->is_ajax())
|
||
|
return $this->send_json(array('type' => 'success', 'message' => 'Экспортировано: ' . $c));
|
||
|
|
||
|
header('Location: ' . $this->uri('1', TRUE));
|
||
|
}
|
||
|
|
||
|
public function ajax_rub_import() {
|
||
|
|
||
|
$req = (empty($_POST['with_req'])) ? FALSE : TRUE;
|
||
|
|
||
|
if (!empty($_POST['files']) AND is_array($_POST['files'])) {
|
||
|
$c = 0;
|
||
|
foreach($_POST['files'] as $file){
|
||
|
|
||
|
if($this->import_rubric($file, $req)) $c++;
|
||
|
}
|
||
|
header('Location: ' . $this->uri('1', TRUE));
|
||
|
}
|
||
|
|
||
|
if($this->is_ajax() AND !empty($_POST['filenames'])){
|
||
|
//$this->send_json(array('type' => 'success', 'message' => var_export($_POST['filenames'][0]['value'], TRUE)));
|
||
|
$c = 0;
|
||
|
foreach($_POST['filenames'] as $file){
|
||
|
|
||
|
if($this->import_rubric($file['value'], $req)) $c++;
|
||
|
}
|
||
|
return $this->send_json(array('type' => 'success', 'message' => 'Импортировано: ' . $c));
|
||
|
}
|
||
|
|
||
|
return $this->send_json(array('type' => 'error', 'message' => 'Выберите рубрики для импорта в систему'));
|
||
|
}
|
||
|
|
||
|
public function ajax_sys_import() {
|
||
|
|
||
|
if(!empty($_POST['sys_blocks']) AND $this->is_ajax()){
|
||
|
|
||
|
$c = 0;
|
||
|
foreach($_POST['sys_blocks'] as $file){
|
||
|
|
||
|
if($this->import_sysblock($file['value'])) $c++;
|
||
|
}
|
||
|
return $this->send_json(array('type' => 'success', 'message' => 'Импортировано: ' . $c));
|
||
|
}
|
||
|
|
||
|
return $this->send_json(array('type' => 'error', 'message' => 'Выберите системные блоки для импорта в систему'));
|
||
|
}
|
||
|
|
||
|
/* ----------------
|
||
|
Methods
|
||
|
------------------ */
|
||
|
|
||
|
/**
|
||
|
* Экспортируем рубрику+поля рубрики+запросы
|
||
|
* (пока без условий в запросах)
|
||
|
* @param type $rub_id
|
||
|
* @param type $filename
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function export_rubric($rub_id) {
|
||
|
|
||
|
$rub_id = (int) $rub_id;
|
||
|
$file['r_self'] = $this->db->Query('SELECT
|
||
|
rubric_title,
|
||
|
rubric_alias,
|
||
|
rubric_template,
|
||
|
rubric_template_id,
|
||
|
rubric_author_id,
|
||
|
rubric_created,
|
||
|
rubric_docs_active,
|
||
|
rubric_code_start,
|
||
|
rubric_code_end,
|
||
|
rubric_teaser_template,
|
||
|
rubric_admin_teaser_template,
|
||
|
rubric_header_template,
|
||
|
rubric_linked_rubric,
|
||
|
rubric_description,
|
||
|
rubric_position
|
||
|
FROM ' . PREFIX . '_rubrics WHERE Id = ' . $rub_id)->FetchAssocArray();
|
||
|
|
||
|
if (!empty($file['r_self'])) {
|
||
|
|
||
|
$file['rub_export_id'] = $rub_id;
|
||
|
$file['title'] = $file['r_self']['rubric_title'];
|
||
|
$file['desc'] = $file['r_self']['rubric_description'];
|
||
|
|
||
|
$file['r_fields'] = $this->query('SELECT
|
||
|
Id as old_id,
|
||
|
rubric_field_alias,
|
||
|
rubric_field_template,
|
||
|
rubric_field_template_request,
|
||
|
rubric_field_title,
|
||
|
rubric_field_type,
|
||
|
rubric_field_position,
|
||
|
rubric_field_default
|
||
|
FROM ' . PREFIX . '_rubric_fields WHERE rubric_id = ' . $rub_id);
|
||
|
|
||
|
$file['r_requests'] = $this->query('SELECT
|
||
|
request_items_per_page,
|
||
|
request_title,
|
||
|
request_template_item,
|
||
|
request_template_main,
|
||
|
request_order_by,
|
||
|
request_order_by_nat,
|
||
|
request_author_id,
|
||
|
request_created,
|
||
|
request_description,
|
||
|
request_asc_desc,
|
||
|
request_show_pagination,
|
||
|
request_where_cond,
|
||
|
request_cache_lifetime,
|
||
|
request_lang
|
||
|
FROM ' . PREFIX . '_request WHERE rubric_id = ' . $rub_id);
|
||
|
|
||
|
$filename = md5(serialize($file['r_fields']).$rub_id); //имя файла по набору полей
|
||
|
//var_export($file);
|
||
|
return file_put_contents($this->datadir . $filename . '.php', serialize($file));
|
||
|
}
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Импортируем рубрику из указанного файла (без расширения)
|
||
|
* @param type $filename
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function import_rubric($filename, $req_import = TRUE) {
|
||
|
|
||
|
$file = $this->datadir . $filename . '.php';
|
||
|
|
||
|
if (file_exists($file)) {
|
||
|
|
||
|
$file = file_get_contents($file);
|
||
|
$file = @unserialize($file);
|
||
|
|
||
|
//array
|
||
|
if (!empty($file['r_self']) ) {
|
||
|
|
||
|
$file['r_self'] = $this->text_fields($file['r_self'], array(
|
||
|
'rubric_title',
|
||
|
'rubric_alias',
|
||
|
'rubric_template',
|
||
|
'rubric_code_start',
|
||
|
'rubric_code_end',
|
||
|
'rubric_teaser_template',
|
||
|
'rubric_admin_teaser_template',
|
||
|
'rubric_header_template',
|
||
|
'rubric_linked_rubric',
|
||
|
'rubric_description'
|
||
|
));
|
||
|
$fields = implode(',', array_keys($file['r_self']));
|
||
|
$values = implode(',', array_values($file['r_self']));
|
||
|
// импортируем рубрику
|
||
|
$this->db->Query('INSERT INTO ' . PREFIX . '_rubrics (' . $fields . ') VALUES (' . $values . ')');
|
||
|
$new_id = $this->db->InsertId();
|
||
|
|
||
|
// импортируем поля, запоминаем связи старых и новых
|
||
|
$fields = '';
|
||
|
if (count($file['r_fields']) AND !empty($file['r_fields'][0]) AND is_array($file['r_fields'][0])) {
|
||
|
|
||
|
$replace = array();
|
||
|
foreach ($file['r_fields'] as &$frow) {
|
||
|
$frow = $this->text_fields($frow, array(
|
||
|
'rubric_field_alias',
|
||
|
'rubric_field_title',
|
||
|
'rubric_field_type',
|
||
|
'rubric_field_default',
|
||
|
'rubric_field_template',
|
||
|
'rubric_field_template_request'
|
||
|
));
|
||
|
$frow['rubric_id'] = $new_id; //new rubric id
|
||
|
$old_id = $frow['old_id']; unset($frow['old_id']);
|
||
|
$fields = implode(',', array_keys($frow)); // из за лишнего старого old_id
|
||
|
$values = '(' . implode(',', array_values($frow)) . ')';
|
||
|
$this->db->Query('INSERT INTO ' . PREFIX . '_rubric_fields (' . $fields . ') VALUES ' .$values);
|
||
|
$f_id = $this->db->InsertId();
|
||
|
$replace['tag:fld:'.$old_id] = 'tag:fld:'.$f_id; // для шаблона рубрики
|
||
|
$replace['tag:rfld:'.$old_id] = 'tag:rfld:'.$f_id; // для шаблонов запросов
|
||
|
}
|
||
|
// переписываем шаблон рубрики с новыми полями
|
||
|
if(count($replace)) $template = str_replace(array_keys($replace), array_values($replace), $file['r_self']['rubric_template']);
|
||
|
|
||
|
$this->db->Query('UPDATE '.PREFIX.'_rubrics SET rubric_template = '.$template.' WHERE Id = '.$new_id.' LIMIT 1');
|
||
|
}
|
||
|
|
||
|
// импортируем запросы. переписываем поля в шаблонах
|
||
|
$fields = '';
|
||
|
if (count($file['r_requests']) AND !empty($file['r_requests'][0]) AND is_array($file['r_requests'][0]) AND $req_import === TRUE) {
|
||
|
|
||
|
$values = array();
|
||
|
foreach ($file['r_requests'] as &$rrow) {
|
||
|
$rrow = $this->text_fields($rrow, array(
|
||
|
'request_title',
|
||
|
'request_template_item',
|
||
|
'request_template_main',
|
||
|
'request_order_by',
|
||
|
'request_description',
|
||
|
'request_asc_desc',
|
||
|
'request_show_pagination',
|
||
|
'request_where_cond',
|
||
|
'request_lang'
|
||
|
));
|
||
|
$rrow['rubric_id'] = $new_id; //new rubric id
|
||
|
if(count($replace)) $rrow['request_template_item'] = str_replace(array_keys($replace), array_values($replace), $rrow['request_template_item']);
|
||
|
$values[] = '(' . implode(',', array_values($rrow)) . ')';
|
||
|
}
|
||
|
$fields = implode(',', array_keys($file['r_requests'][0]));
|
||
|
$this->db->Query('INSERT INTO ' . PREFIX . '_request (' . $fields . ') VALUES ' . implode(',', $values));
|
||
|
}
|
||
|
|
||
|
if ($new_id)
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
public function import_sysblock($filename) {
|
||
|
|
||
|
$filename = $this->datadir.'sysblocks/'.$filename.'.php';
|
||
|
|
||
|
if(file_exists($filename)){
|
||
|
|
||
|
$filename = (array) require_once $filename;
|
||
|
|
||
|
if(!empty($filename['db_name']) AND !empty($filename['db_text'])){
|
||
|
$values = array($filename['db_name'], $filename['db_text'], time());
|
||
|
$values = $this->text_fields($values, array(0,1));
|
||
|
|
||
|
$this->db->Query('INSERT INTO '.PREFIX.'_sysblocks (sysblock_name, sysblock_text, sysblock_created) VALUES ('.implode(',', $values).')');
|
||
|
return TRUE;
|
||
|
|
||
|
} else return $this->send_json(array('type' => 'error', 'message' => 'Обнаружены пустые данные в системном блоке!'));
|
||
|
} else return $this->send_json(array('type' => 'error', 'message' => 'Удален файл из списка. Обновите страницу!'));
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Получаем данные по рубрикам (рубрика + колво полей + колво запросов)
|
||
|
*/
|
||
|
|
||
|
public function get_rubrics() {
|
||
|
|
||
|
$rubric_fields = $this->query('SELECT ' . PREFIX . '_rubrics.Id as rub_id, rubric_title, rubric_alias, rubric_description, COUNT(rubric_id) as count_fields
|
||
|
FROM ' . PREFIX . '_rubrics
|
||
|
JOIN ' . PREFIX . '_rubric_fields ON ' . PREFIX . '_rubrics.Id = ' . PREFIX . '_rubric_fields.rubric_id
|
||
|
GROUP BY rubric_id
|
||
|
');
|
||
|
$count_req = $this->query('SELECT rubric_id, COUNT(Id) as count_req FROM ' . PREFIX . '_request GROUP BY rubric_id');
|
||
|
$result = array();
|
||
|
foreach ($rubric_fields as &$row) {
|
||
|
if (empty($row['rubric_description']))
|
||
|
$row['rubric_description'] = '#Нет описания#';
|
||
|
$result[$row['rub_id']] = $row;
|
||
|
$result[$row['rub_id']]['count_req'] = 0;
|
||
|
}
|
||
|
foreach ($count_req as $row) {
|
||
|
if (isset($result[$row['rubric_id']]))
|
||
|
$result[$row['rubric_id']]['count_req'] = $row['count_req'];
|
||
|
}
|
||
|
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Получаем данные из файлов и формируем массив для вывода
|
||
|
*/
|
||
|
|
||
|
public function get_data_files() {
|
||
|
|
||
|
$files = array();
|
||
|
$id = 1;
|
||
|
foreach (glob($this->datadir . '/*.php') as $file) {
|
||
|
|
||
|
$f = file_get_contents($file);
|
||
|
$f = @unserialize($f);
|
||
|
$nfile = str_replace('.php', '', basename($file));
|
||
|
$files[$nfile] = array(
|
||
|
'id' => $id++,
|
||
|
'filename' => $nfile,
|
||
|
'title' => $f['title'],
|
||
|
'desc' => $f['desc'],
|
||
|
'path' => $file
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return $files;
|
||
|
}
|
||
|
|
||
|
public function get_sys_blocks() {
|
||
|
|
||
|
$files = array();
|
||
|
foreach(glob($this->datadir . 'sysblocks/sb_*.php') as $file){
|
||
|
|
||
|
$nfile = str_replace('.php', '', basename($file));
|
||
|
$files[$nfile] = (array) require_once $file;
|
||
|
}
|
||
|
|
||
|
return $files;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Собираем УРЛ
|
||
|
*/
|
||
|
|
||
|
public function uri($modact = FALSE, $inside = FALSE) {
|
||
|
|
||
|
$build_with[] = 'do=modules';
|
||
|
if ($inside === TRUE) {
|
||
|
$build_with[] = 'action=modedit';
|
||
|
$build_with[] = 'mod=faster';
|
||
|
}
|
||
|
if ($modact)
|
||
|
$build_with[] = 'moduleaction=' . $modact;
|
||
|
$build_with[] = 'cp=' . $this->session;
|
||
|
|
||
|
return '/admin/index.php?' . implode('&', $build_with);
|
||
|
}
|
||
|
|
||
|
/* Ассоциативный массив записей */
|
||
|
|
||
|
private function query($query) {
|
||
|
|
||
|
$q = $this->db->Query($query);
|
||
|
|
||
|
$result = array();
|
||
|
while ($row = $q->FetchAssocArray()) {
|
||
|
|
||
|
$result[] = $row;
|
||
|
}
|
||
|
|
||
|
unset($q);
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
/* Отображение из папки templates (без шаблонизатора) */
|
||
|
|
||
|
private function view($name, $data = array()) {
|
||
|
|
||
|
if (!empty($data) AND is_array($data))
|
||
|
extract($data);
|
||
|
|
||
|
ob_start();
|
||
|
require $this->tdir . $name . '.php';
|
||
|
$view = ob_get_clean();
|
||
|
|
||
|
return $view;
|
||
|
}
|
||
|
|
||
|
/* Чтобы собрать запрос по ассоциативному массиву через функцию implode
|
||
|
* нужно обернуть текстовые поля в кавычки и поставить слэши, иначе error
|
||
|
* Особенно полезна для полей с PHP кодом
|
||
|
*/
|
||
|
private function text_fields($cur_row, $tfields = array()) {
|
||
|
|
||
|
$cur_row = (array) $cur_row;
|
||
|
foreach ($cur_row as $f => &$v) {
|
||
|
|
||
|
if (in_array($f, $tfields))
|
||
|
$v = "'" . addslashes($v) . "'";
|
||
|
}
|
||
|
|
||
|
return $cur_row;
|
||
|
}
|
||
|
|
||
|
// проверяем что к нам пришел АЯКС запрос
|
||
|
public function is_ajax() {
|
||
|
|
||
|
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND
|
||
|
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
||
|
}
|
||
|
|
||
|
// отправляем формат json
|
||
|
public function send_json($data) {
|
||
|
|
||
|
header('Content-Type: application/json');
|
||
|
exit(json_encode($data));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
?>
|