Browse Source

Добавлен Модуль Импорт | Экспорт

master
SVarlamov 8 years ago
commit
e72fcee40e
  1. 17
      README.md
  2. 31
      data/sysblocks/sb_1.php
  3. 27
      data/sysblocks/sb_2.php
  4. 8
      index.php
  5. 463
      module.class.php
  6. 48
      module.php
  7. 200
      templates/import.php

17
README.md

@ -0,0 +1,17 @@
## faster
# Модуль Импорт | Экспорт
# Автор: Playmore (Сергей Батурин) pm@pmore.ru
## Ускоряем разработку сайтов, быстро создавая рубрики.
* Импорт | экспорт рубрик, запросов и системных блоков
## Перед копированием модуля в папку modules, удалите файл README.md, копируйте только корневую папку faster со всем ее содержимым внутри!
## Changelog:
22.09.2013 - версия 0.4 fix

31
data/sysblocks/sb_1.php

@ -0,0 +1,31 @@
<?php
return array(
'title' => 'Подобные цены',
'desc' => 'Позволяет отобразить тизеры документов из рубрики с подобными ценами',
'author' => 'Юрий Рахимов',
'db_name' => 'Подобные цены',
'db_text' => '<h3>За такуюже цену:</h3><br />
<?php
$last_count=5; //Количество
$rubric_field_id=29; //ID поля с ценой
$percent=10; //Проценты
$price=get_document_field($_REQUEST[\'id\'],$rubric_field_id);
$price_from=intval($price-$price*$percent/200);
$price_to=intval($price+$price*$percent/200);
$sql=$AVE_DB->Query("Select
d.document_id
FROM
".PREFIX."_document_fields d,
".PREFIX."_documents a
WHERE a.Id=d.document_id AND d.rubric_field_id=".$rubric_field_id." AND d.field_number_value>".$price_from." AND d.field_number_value<".$price_to." AND a.document_status=1
ORDER by d.field_number_value
LIMIT ".$last_count);
while ($row = $sql->FetchAssocArray()) {
$t=eval2var(\'?>\'.showteaser($row[\'document_id\']).\'<?\');
echo $t;
}
?>'
);
?>

27
data/sysblocks/sb_2.php

@ -0,0 +1,27 @@
<?php
return array(
'title' => 'Просмотренные документы',
'desc' => 'Выводит историю просмотренных документов - показывает их тизеры',
'author' => 'Юрий Рахимов',
'db_name' => 'Просмотренные документы',
'db_text' => '<?php
$last_count=5;
$rubrics=5; //можно указывать номера рубрик через запятую
$_SESSION[\'doc_view\'][$_REQUEST[\'id\']]=time();
$docs=$_SESSION[\'doc_view\'];
unset($docs[$_REQUEST[\'id\']]);
arsort($docs);
$ar=0;
foreach($docs as $k=>$v){
$sql=$AVE_DB->Query("SELECT Id from ".PREFIX."_documents WHERE Id=\'".intval($k)."\' AND rubric_id IN (".$rubrics.") and document_status=1")->GetCell();
$t=eval2var(\'?>\'.showteaser($sql).\'<?\');
if(!empty($t)){$ar++;echo $t;}
if($ar>=$last_count) break;
}
?>'
);
?>

8
index.php

@ -0,0 +1,8 @@
<?php
/**
* Файл-заглушка, предназначенный для запрета показа списка файлов в текущей директории,
* если через адресную строку браузера было прямое общращение к данной директории.
*/
header('Location:/');
exit;
?>

463
module.class.php

@ -0,0 +1,463 @@
<?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));
}
}
?>

48
module.php

@ -0,0 +1,48 @@
<?php
if(!defined('BASE_DIR')) exit;
if (defined('ACP'))
{
$modul['ModuleName'] = '&laquo; Импорт | Экспорт &raquo;';
$modul['ModuleSysName'] = 'faster';
$modul['ModuleVersion'] = '0.4fixed';
$modul['ModuleDescription'] = 'Ускоряем разработку сайтов, быстро создавая рубрики. Импорт | экспорт рубрик, запросов и системных блоков';
$modul['ModuleAutor'] = 'Playmore';
$modul['ModuleCopyright'] = '&copy; 2013 Playmore';
$modul['ModuleIsFunction'] = 0;
$modul['ModuleAdminEdit'] = 1;
$modul['ModuleFunction'] = '';
$modul['ModuleTag'] = '';
$modul['ModuleTagLink'] = null;
$modul['ModuleAveTag'] = '';
$modul['ModulePHPTag'] = "";
}
/**
* Организуем работу в админке
*/
if (defined('ACP') && !empty($_REQUEST['moduleaction']))
{
require_once(BASE_DIR.'/modules/faster/module.class.php');
if(class_exists('Faster')) {
$tpl_dir = BASE_DIR . '/modules/faster/templates/';
$act = $_REQUEST['moduleaction'];
$faster = new Faster($tpl_dir);
if($act == 1) $faster->show_import();
// вызываем метод модели по moduleaction
if(method_exists($faster, $act)){
call_user_func_array(array($faster, $act), array());
}
}
}
?>

200
templates/import.php

@ -0,0 +1,200 @@
<style>
.message_box{padding: 5px 10px; margin-top: 5px; border-right: 2px solid #000; display: none;}
.message_red{background-color: #DD6B2C; color: #fff; border-right: 2px solid #d62;}
.message_green{background-color: #B3DB57; color:#000; border-right: 2px solid #bd5;}
</style>
<div class="title"><h5>Импортируйте заранее готовые рубрики и запросы!</h5></div>
<div class="widget" style="margin-top: 0px;">
<div class="body">
По вопросам развития модуля обращайтесь: <b>pm@pmore.ru</b>
</div>
</div>
<div class="breadCrumbHolder module">
<div class="breadCrumb module">
<ul>
<li class="firstB"><a href="index.php" ></a></li>
<li><a href="<?=$this->uri(FALSE, FALSE)?>">Управление модулями</a></li>
<li>Импорт | экспорт рубрик, запросов и системных блоков</li>
</ul>
</div>
</div>
<div id="rubimex_message" class="message_box"></div>
<div class="widget first">
<ul class="tabs">
<li class="activeTab"><a href="#tab1">&laquo; Импорт рубрик</a></li>
<li class=""><a href="#tab2">&laquo; Импорт системных блоков</a></li>
<li class=""><a href="#tab3">Экспорт рубрик &raquo;</a></li>
</ul>
<div class="tab_container">
<div id="tab1" class="tab_content" style="display: block;">
<?php //$this->export_rubric(4,'static','Статические страницы','Рубрика для создания статических страниц с полем "Код". Запросы: список страниц с пагинацией, список страниц в виде постов.')?>
<?php if (!empty($files)) :?>
<form id="import_rub_form" action="<?=$this->uri('ajax_rub_import', TRUE)?>" method="POST">
<table class="tableStatic mainForm" style="width:100%">
<thead>
<tr>
<td width="1%"></td>
<td >Рубрика</td>
<td width="70%">Описание</td>
<td width="1%"></td>
</tr>
</thead>
<tbody>
<?php foreach($files as $data) :?>
<tr id="import_<?=$data['id']?>">
<td align="center">
<div class="jqTransformCheckboxWrapper">
<input class="jqTransformCheckbox" type="checkbox" name="files[]" value="<?=$data['filename']?>">
</div>
</td>
<td align="center"><b><?=$data['title']?></b></td>
<td><?=$data['desc']?></td>
<td align="center">
<a onclick="rubimex_delete('<?=$data['filename']?>', <?=$data['id']?>)" class="topleftDir icon_sprite ico_delete" href="javascript:void(0)" original-title="Удалить конфиг"></a>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="rowElem">
<input id="import_rub" type="button" class="basicBtn ConfirmSettings" value="Импортировать рубрики">
<input id="import_rub_req" type="button" class="basicBtn ConfirmSettings" value="Импортировать рубрики и запросы">
</div>
</form>
<?php else:?>
<div class="highlight yellow">Готовых к импорту рубрик не найдено. Экспортируйте рубрики со старых проектов и добавьте в модуль.</div>
<?php endif; ?>
</div>
<div id="tab2" class="tab_content" style="display: none;">
<?php if(!empty($sysblocks)) :?>
<form id="import_sys_form" action="<?=$this->uri('ajax_sys_import', TRUE)?>" method="POST">
<table class="tableStatic mainForm" style="width:100%">
<thead>
<tr>
<td width="1%"></td>
<td>Имя блока</td>
<td width="60%">Описание</td>
<td>Автор</td>
</tr>
</thead>
<tbody>
<?php foreach($sysblocks as $filename => $block) :?>
<tr>
<td align="center">
<div class="jqTransformCheckboxWrapper">
<input class="jqTransformCheckbox" type="checkbox" name="sys" value="<?=$filename?>">
</div>
</td>
<td align="center"><b><?=$block['title']?></b></td>
<td>
<div class="pm_rub_desc"><?=$block['desc']?></div>
</td>
<td align="center"><b><?=$block['author']?></b></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="rowElem"><input id="import_sys" type="button" class="basicBtn ConfirmSettings" value="Импортировать системные блоки"></div>
</form>
<?php else:?>
<div class="highlight yellow">Список системных блоков пуст.</div>
<?php endif;?>
</div>
<div id="tab3" class="tab_content" style="display: none;">
<?php if(!empty($rubrics)):?>
<form action="<?=$this->uri('ajax_rub_export', TRUE)?>" method="POST">
<table id="rub_export" class="tableStatic mainForm" style="width:100%">
<thead>
<tr>
<td width="1%"></td>
<td width="20%">Заголовок рубрики</td>
<td >Описание рубрики</td>
</tr>
</thead>
<tbody>
<?php foreach($rubrics as $r): ?>
<tr>
<td><input class="jqTransformCheckbox" type="checkbox" name="rubs[]" value="<?=$r['rub_id']?>"></td>
<td><b><?=$r['rubric_title']?></b>
<div style="font-size:10px; font-style: italic">
Полей: <b><?=$r['count_fields']?></b>
Запросов: <b><?=$r['count_req']?></b>
Алиас: <b><?=$r['rubric_alias']?></b>
</div>
</td>
<td><?=$r['rubric_description']?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="rowElem">
<input id="export_rubs" type="submit" class="basicBtn ConfirmSettings" value="Экспортировать">
</div>
</form>
<?php else:?>
<div class="highlight yellow">Готовых к экспорту рубрик в вашей системе не найдено. Создайте рубрики и затем экспортируйте их для повторного использования.</div>
<?php endif;?>
</div>
</div>
<div class="fix"></div>
</div>
<script type="text/javascript">
function rubimex_delete(filename, tr_id){
$.post('<?=$this->uri('ajax_rub_delete', TRUE)?>', {file: filename}, function(res){
if(res.type == 'success') $('#import_'+tr_id).fadeOut(400);
rubimex_result(res);
});
}
function rubimex_result(result){
console.log(result);
var type = 'message_green';
if(result.type == 'error') type = 'message_red';
if(result.message) {
$('#rubimex_message').hide().removeClass('message_green').addClass(type).text(result.message).fadeIn(400);
}
}
$(function(){
$('#import_rub').on('click', function(){
$.post('<?=$this->uri('ajax_rub_import', TRUE)?>', {filenames: $('#import_rub_form').serializeArray()}, function(res){
rubimex_result(res);
});
});
$('#import_rub_req').on('click', function(){
$.post('<?=$this->uri('ajax_rub_import', TRUE)?>', {filenames: $('#import_rub_form').serializeArray(), with_req: 1}, function(res){
rubimex_result(res);
});
});
$('#import_sys').on('click', function(){
$.post('<?=$this->uri('ajax_sys_import', TRUE)?>', {sys_blocks: $('#import_sys_form').serializeArray()}, function(res){
rubimex_result(res);
});
});
});
</script>
Loading…
Cancel
Save