From e72fcee40e798026de790d4ef69ece03a600f31a Mon Sep 17 00:00:00 2001 From: SVarlamov Date: Sat, 11 Jun 2016 13:06:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D0=98=D0=BC?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=82=20|=20=D0=AD=D0=BA=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D1=80=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++ data/sysblocks/sb_1.php | 31 +++ data/sysblocks/sb_2.php | 27 +++ index.php | 8 + module.class.php | 463 ++++++++++++++++++++++++++++++++++++++++ module.php | 48 +++++ templates/import.php | 200 +++++++++++++++++ 7 files changed, 794 insertions(+) create mode 100644 README.md create mode 100644 data/sysblocks/sb_1.php create mode 100644 data/sysblocks/sb_2.php create mode 100644 index.php create mode 100644 module.class.php create mode 100644 module.php create mode 100644 templates/import.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f1805c --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +## faster + +# Модуль Импорт | Экспорт + +# Автор: Playmore (Сергей Батурин) pm@pmore.ru + + +## Ускоряем разработку сайтов, быстро создавая рубрики. + + * Импорт | экспорт рубрик, запросов и системных блоков + + +## Перед копированием модуля в папку modules, удалите файл README.md, копируйте только корневую папку faster со всем ее содержимым внутри! + +## Changelog: + +22.09.2013 - версия 0.4 fix \ No newline at end of file diff --git a/data/sysblocks/sb_1.php b/data/sysblocks/sb_1.php new file mode 100644 index 0000000..8764340 --- /dev/null +++ b/data/sysblocks/sb_1.php @@ -0,0 +1,31 @@ + 'Подобные цены', + 'desc' => 'Позволяет отобразить тизеры документов из рубрики с подобными ценами', + 'author' => 'Юрий Рахимов', + 'db_name' => 'Подобные цены', + 'db_text' => '

За такуюже цену:


+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\']).\'' +); + +?> \ No newline at end of file diff --git a/data/sysblocks/sb_2.php b/data/sysblocks/sb_2.php new file mode 100644 index 0000000..ccb5836 --- /dev/null +++ b/data/sysblocks/sb_2.php @@ -0,0 +1,27 @@ + 'Просмотренные документы', + 'desc' => 'Выводит историю просмотренных документов - показывает их тизеры', + 'author' => 'Юрий Рахимов', + 'db_name' => 'Просмотренные документы', + 'db_text' => '$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).\'=$last_count) break; +} + +?>' +); + +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..9b7e3ef --- /dev/null +++ b/index.php @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/module.class.php b/module.class.php new file mode 100644 index 0000000..e62ff0b --- /dev/null +++ b/module.class.php @@ -0,0 +1,463 @@ +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)); + } + +} + +?> \ No newline at end of file diff --git a/module.php b/module.php new file mode 100644 index 0000000..95e78d5 --- /dev/null +++ b/module.php @@ -0,0 +1,48 @@ +show_import(); + + // вызываем метод модели по moduleaction + if(method_exists($faster, $act)){ + + call_user_func_array(array($faster, $act), array()); + } + + } + +} + + +?> \ No newline at end of file diff --git a/templates/import.php b/templates/import.php new file mode 100644 index 0000000..b45f629 --- /dev/null +++ b/templates/import.php @@ -0,0 +1,200 @@ + + +
Импортируйте заранее готовые рубрики и запросы!
+ +
+
+ По вопросам развития модуля обращайтесь: pm@pmore.ru +
+
+ + +
+
+ +
+
+ export_rubric(4,'static','Статические страницы','Рубрика для создания статических страниц с полем "Код". Запросы: список страниц с пагинацией, список страниц в виде постов.')?> + + +
+ + + + + + + + + + + + + + + + + + + +
РубрикаОписание
+
+ +
+
+ +
+ +
+ + +
+
+ +
Готовых к импорту рубрик не найдено. Экспортируйте рубрики со старых проектов и добавьте в модуль.
+ +
+ + + + +
+ +
+
+ + \ No newline at end of file