Просмотр файла mc-2.7.0/modules/index_page/controllers/index_page_admin.php

Размер файла: 11.07Kb
  1. <?php
  2.  
  3. /**
  4. * MobileCMS
  5. *
  6. * Open source content management system for mobile sites
  7. *
  8. * @author MobileCMS Team <support@mobilecms.pro>
  9. * @copyright Copyright (c) 2011-2019, MobileCMS Team
  10. * @link https://mobilecms.pro Official site
  11. * @license MIT license
  12. */
  13. defined('IN_SYSTEM') or die('<b>403<br />Запрет доступа!</b>');
  14.  
  15. //---------------------------------------------
  16.  
  17. /**
  18. * Контроллер управления главной страницей
  19. */
  20. class Index_Page_Admin_Controller extends Controller {
  21.  
  22. /**
  23. * Уровень пользовательского доступа
  24. */
  25. public $access_level = 10;
  26.  
  27. /**
  28. * Тема
  29. */
  30. public $template_theme = 'admin';
  31.  
  32. /**
  33. * Конструктор
  34. */
  35. public function __construct() {
  36. parent::__construct();
  37. a_import('modules/index_page/helpers/index_page');
  38.  
  39. # Чистка кэша главной страницы
  40. if (ROUTE_ACTION != '') {
  41. main::is_demo();
  42. if (!class_exists('File_Cache'))
  43. a_import('libraries/file_cache');
  44. ;
  45. $file_cache = new File_Cache(ROOT . 'cache/file_cache');
  46. $file_cache->clear('index_page');
  47. }
  48. }
  49.  
  50. /**
  51. * Метод по умолчанию
  52. */
  53. public function action_index() {
  54. $this->action_view_page();
  55. }
  56.  
  57. /**
  58. * Просмотр главной страницы
  59. */
  60. public function action_view_page() {
  61. $result = $this->db->query("SELECT * FROM #__index_page_blocks ORDER BY position ASC");
  62.  
  63. $blocks = array();
  64. while ($block = $this->db->fetch_array($result)) {
  65. # Получаем виджеты блока
  66. $block['widgets'] = $this->db->get_array("SELECT * FROM #__index_page_widgets WHERE block_id = '" . $block['block_id'] . "' ORDER BY position ASC");
  67.  
  68. $blocks[] = $block;
  69. }
  70.  
  71. $this->tpl->assign(array(
  72. 'blocks' => $blocks
  73. ));
  74.  
  75. $this->tpl->display('view_page');
  76. }
  77.  
  78. /**
  79. * Очистить кэш главной
  80. */
  81. public function action_cache_clear() {
  82. a_notice('Кэш главной очищен!', a_url('index_page/admin'));
  83. }
  84.  
  85. /**
  86. * Добавление виджета
  87. */
  88. public function action_widget_add() {
  89. if (!$block = $this->db->get_row("SELECT * FROM #__index_page_blocks WHERE block_id = '" . intval($_GET['block_id']) . "'"))
  90. a_error("Блок не найден!");
  91.  
  92. if (isset($_POST['submit'])) {
  93. if (!file_exists(ROOT . 'modules/' . $_POST['module'] . '/helpers/' . $_POST['module'] . '_widget.php')) {
  94. $this->error .= 'Не найден вспомогательный файл виджета<br />';
  95. }
  96. if (empty($_POST['title'])) {
  97. $this->error .= 'Укажите заголовок виджета<br />';
  98. }
  99.  
  100. if (!$this->error) {
  101. $position = $this->db->get_one("SELECT MAX(position) FROM #__index_page_widgets WHERE block_id = '" . intval($_GET['block_id']) . "'") + 1;
  102.  
  103. $this->db->query("INSERT INTO #__index_page_widgets SET
  104. block_id = '" . intval($_GET['block_id']) . "',
  105. title = '" . a_safe($_POST['title']) . "',
  106. module = '" . a_safe($_POST['module']) . "',
  107. position = '$position'
  108. ");
  109.  
  110. a_notice('Виджет успешно добавлен!', a_url('index_page/admin'));
  111. }
  112. }
  113. if (!isset($_POST['submit']) OR $this->error) {
  114. $widgets = index_page::get_widgets();
  115.  
  116. $this->tpl->assign(array(
  117. 'error' => $this->error,
  118. 'widgets' => $widgets
  119. ));
  120.  
  121. $this->tpl->display('widget_add');
  122. }
  123. }
  124.  
  125. /**
  126. * Настройка виджета
  127. */
  128. public function action_widget_setup() {
  129. if (!$widget = $this->db->get_row("SELECT * FROM #__index_page_widgets WHERE widget_id = '" . intval($_GET['widget_id']) . "'"))
  130. a_error('Виджет не найден!');
  131.  
  132. # Подключаем класс виджета
  133. if (!class_exists($widget['module'] . '_widget'))
  134. a_import('modules/' . $widget['module'] . '/helpers/' . $widget['module'] . '_widget.php');
  135. # Получаем setup виджета
  136. call_user_func(array($widget['module'] . '_widget', 'setup'), $widget);
  137. }
  138.  
  139. /**
  140. * Перемещение виджета вверх
  141. */
  142. public function action_widget_up() {
  143. if (!$widget = $this->db->get_row("SELECT * FROM #__index_page_widgets WHERE widget_id = '" . intval($_GET['widget_id']) . "'")) {
  144. a_error('Виджет не найден!');
  145. }
  146.  
  147. $min_position = $this->db->get_one("SELECT MIN(position) FROM #__index_page_widgets WHERE block_id = '" . $widget['block_id'] . "'");
  148.  
  149. if ($widget['position'] > $min_position) {
  150. # Меняем позиции
  151. $this->db->query("UPDATE #__index_page_widgets SET position = " . $widget['position'] . " WHERE block_id = '" . $widget['block_id'] . "' AND position = " . ($widget['position'] - 1));
  152. $this->db->query("UPDATE #__index_page_widgets SET position = " . ($widget['position'] - 1) . " WHERE block_id = '" . $widget['block_id'] . "' AND widget_id = " . intval($_GET['widget_id']));
  153. }
  154.  
  155. header("Location: " . a_url('index_page/admin'));
  156. exit;
  157. }
  158.  
  159. /**
  160. * Перемещение виджета вниз
  161. */
  162. public function action_widget_down() {
  163. if (!$widget = $this->db->get_row("SELECT * FROM #__index_page_widgets WHERE widget_id = '" . intval($_GET['widget_id']) . "'"))
  164. a_error('Виджет не найден!');
  165.  
  166. $max_position = $this->db->get_one("SELECT MAX(position) FROM #__index_page_widgets WHERE block_id = '" . $widget['block_id'] . "'");
  167.  
  168. if ($widget['position'] < $max_position) {
  169. # Меняем позиции
  170. $this->db->query("UPDATE #__index_page_widgets SET position = " . $widget['position'] . " WHERE block_id = '" . $widget['block_id'] . "' AND position = " . ($widget['position'] + 1));
  171. $this->db->query("UPDATE #__index_page_widgets SET position = " . ($widget['position'] + 1) . " WHERE block_id = '" . $widget['block_id'] . "' AND widget_id = " . intval($_GET['widget_id']));
  172. }
  173.  
  174. header("Location: " . a_url('index_page/admin'));
  175. exit;
  176. }
  177.  
  178. /**
  179. * Удаление виджета
  180. */
  181. public function action_widget_delete() {
  182. if (!$widget = $this->db->get_row("SELECT * FROM #__index_page_widgets WHERE widget_id = '" . intval($_GET['widget_id']) . "'"))
  183. a_error('Виджет не найден!');
  184.  
  185. $this->db->query("DELETE FROM #__index_page_widgets WHERE widget_id = '" . intval($_GET['widget_id']) . "'");
  186.  
  187. a_notice('Виджет успешно удален', a_url('index_page/admin'));
  188. }
  189.  
  190. /**
  191. * Перемещение блока вверх
  192. */
  193. public function action_block_up() {
  194. if (!$block = $this->db->get_row("SELECT * FROM #__index_page_blocks WHERE block_id = " . intval($_GET['block_id'])))
  195. a_error('Блок не найден!');
  196.  
  197. $min_position = $this->db->get_one("SELECT MIN(position) FROM #__index_page_blocks");
  198.  
  199. if ($block['position'] > $min_position) {
  200. # Меняем позиции
  201. $this->db->query("UPDATE #__index_page_blocks SET position = " . $block['position'] . " WHERE position = " . ($block['position'] - 1));
  202. $this->db->query("UPDATE #__index_page_blocks SET position = " . ($block['position'] - 1) . " WHERE block_id = " . intval($_GET['block_id']));
  203. }
  204.  
  205. header("Location: " . a_url('index_page/admin'));
  206. exit;
  207. }
  208.  
  209. /**
  210. * Перемещение блока вниз
  211. */
  212. public function action_block_down() {
  213. if (!$block = $this->db->get_row("SELECT * FROM #__index_page_blocks WHERE block_id = " . intval($_GET['block_id'])))
  214. a_error('Блок не найден!');
  215.  
  216. $max_position = $this->db->get_one("SELECT MAX(position) FROM #__index_page_blocks");
  217.  
  218. if ($block['position'] < $max_position) {
  219. # Меняем позиции
  220. $this->db->query("UPDATE #__index_page_blocks SET position = " . $block['position'] . " WHERE position = " . ($block['position'] + 1));
  221. $this->db->query("UPDATE #__index_page_blocks SET position = " . ($block['position'] + 1) . " WHERE block_id = " . intval($_GET['block_id']));
  222. }
  223.  
  224. header("Location: " . a_url('index_page/admin'));
  225. exit;
  226. }
  227.  
  228. /**
  229. * Удаление блока
  230. */
  231. public function action_block_delete() {
  232. if (!$block = $this->db->get_row("SELECT * FROM #__index_page_blocks WHERE block_id = " . intval($_GET['block_id'])))
  233. a_error('Блок не найден!');
  234.  
  235. # Удаляем блок
  236. $this->db->query("DELETE FROM #__index_page_blocks WHERE block_id = " . intval($_GET['block_id']));
  237. # Удаляем все виджеты данного блока
  238. $this->db->query("DELETE FROM #__index_page_widgets WHERE block_id = " . intval($_GET['block_id']));
  239.  
  240. header("Location: " . a_url('index_page/admin'));
  241. exit;
  242. }
  243.  
  244. /**
  245. * Добавление / Редактирование блока
  246. */
  247. public function action_block_edit() {
  248. if (is_numeric($_GET['block_id'])) {
  249. if (!$block = $this->db->get_row("SELECT * FROM #__index_page_blocks WHERE block_id = '" . intval($_GET['block_id']) . "'"))
  250. a_error("Блок не найден!");
  251. $action = 'edit';
  252. } else {
  253. $block = array();
  254. $action = 'add';
  255. }
  256.  
  257. if (isset($_POST['submit'])) {
  258. if (empty($_POST['title'])) {
  259. $this->error .= 'Укажите заголовок блока<br />';
  260. }
  261.  
  262. if (!$this->error) {
  263. if ($action == 'add') {
  264. $position = $this->db->get_one("SELECT MAX(position) FROM #__index_page_blocks") + 1;
  265.  
  266. $this->db->query("INSERT INTO #__index_page_blocks SET
  267. title = '" . a_safe($_POST['title']) . "',
  268. position = '$position'
  269. ");
  270. $message = "Блок успешно добавлен!";
  271. }
  272. if ($action == 'edit') {
  273. $this->db->query("UPDATE #__index_page_blocks SET
  274. title = '" . a_safe($_POST['title']) . "'
  275. WHERE block_id = '" . intval($_GET['block_id']) . "'
  276. ");
  277. $message = "Блок успешно изменен!";
  278. }
  279.  
  280. a_notice($message, a_url('index_page/admin'));
  281. }
  282. }
  283. if (!isset($_POST['submit']) OR $this->error) {
  284. $this->tpl->assign(array(
  285. 'error' => $this->error,
  286. 'block' => $block,
  287. 'action' => $action
  288. ));
  289.  
  290. $this->tpl->display('block_edit');
  291. }
  292. }
  293.  
  294. }
  295.  
  296. ?>