PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/qa-include/pages/admin/admin-widgets.php

http://github.com/q2a/question2answer
PHP | 346 lines | 239 code | 81 blank | 26 comment | 45 complexity | 4ba5dd50ed886ce151271fe705fd80ef MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. Question2Answer by Gideon Greenspan and contributors
  4. http://www.question2answer.org/
  5. Description: Controller for admin page for editing widgets
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. More about this license: http://www.question2answer.org/license.php
  15. */
  16. if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
  17. header('Location: ../../../');
  18. exit;
  19. }
  20. require_once QA_INCLUDE_DIR . 'app/admin.php';
  21. require_once QA_INCLUDE_DIR . 'db/selects.php';
  22. // Get current list of widgets and determine the state of this admin page
  23. $widgetid = qa_post_text('edit');
  24. if (!strlen($widgetid))
  25. $widgetid = qa_get('edit');
  26. list($widgets, $pages) = qa_db_select_with_pending(
  27. qa_db_widgets_selectspec(),
  28. qa_db_pages_selectspec()
  29. );
  30. if (isset($widgetid)) {
  31. $editwidget = null;
  32. foreach ($widgets as $widget) {
  33. if ($widget['widgetid'] == $widgetid)
  34. $editwidget = $widget;
  35. }
  36. } else {
  37. $editwidget = array('title' => qa_post_text('title'));
  38. if (!isset($editwidget['title']))
  39. $editwidget['title'] = qa_get('title');
  40. }
  41. $module = qa_load_module('widget', @$editwidget['title']);
  42. $widgetfound = isset($module);
  43. // Check admin privileges (do late to allow one DB query)
  44. if (!qa_admin_check_privileges($qa_content))
  45. return $qa_content;
  46. // Define an array of relevant templates we can use
  47. $templatelangkeys = array(
  48. 'question' => 'admin/question_pages',
  49. 'qa' => 'main/recent_qs_as_title',
  50. 'activity' => 'main/recent_activity_title',
  51. 'questions' => 'admin/question_lists',
  52. 'hot' => 'main/hot_qs_title',
  53. 'unanswered' => 'main/unanswered_qs_title',
  54. 'tags' => 'main/popular_tags',
  55. 'categories' => 'misc/browse_categories',
  56. 'users' => 'main/highest_users',
  57. 'ask' => 'question/ask_title',
  58. 'tag' => 'admin/tag_pages',
  59. 'user' => 'admin/user_pages',
  60. 'message' => 'misc/private_message_title',
  61. 'search' => 'main/search_title',
  62. 'feedback' => 'misc/feedback_title',
  63. 'login' => 'users/login_title',
  64. 'register' => 'users/register_title',
  65. 'account' => 'profile/my_account_title',
  66. 'favorites' => 'misc/my_favorites_title',
  67. 'updates' => 'misc/recent_updates_title',
  68. 'ip' => 'admin/ip_address_pages',
  69. 'admin' => 'admin/admin_title',
  70. );
  71. $templateoptions = array();
  72. if (isset($module) && method_exists($module, 'allow_template')) {
  73. foreach ($templatelangkeys as $template => $langkey) {
  74. if ($module->allow_template($template))
  75. $templateoptions[$template] = qa_lang_html($langkey);
  76. }
  77. if ($module->allow_template('custom')) {
  78. $pagemodules = qa_load_modules_with('page', 'match_request');
  79. foreach ($pages as $page) {
  80. // check if this is a page plugin by fetching all plugin classes and matching requests - currently quite convoluted!
  81. $isPagePlugin = false;
  82. foreach ($pagemodules as $pagemodule) {
  83. if ($pagemodule->match_request($page['tags'])) {
  84. $isPagePlugin = true;
  85. }
  86. }
  87. if ($isPagePlugin || !($page['flags'] & QA_PAGE_FLAGS_EXTERNAL))
  88. $templateoptions['custom-' . $page['pageid']] = qa_html($page['title']);
  89. }
  90. }
  91. }
  92. // Process saving an old or new widget
  93. $securityexpired = false;
  94. if (qa_clicked('docancel'))
  95. qa_redirect('admin/layout');
  96. elseif (qa_clicked('dosavewidget')) {
  97. require_once QA_INCLUDE_DIR . 'db/admin.php';
  98. if (!qa_check_form_security_code('admin/widgets', qa_post_text('code')))
  99. $securityexpired = true;
  100. else {
  101. if (qa_post_text('dodelete')) {
  102. qa_db_widget_delete($editwidget['widgetid']);
  103. qa_redirect('admin/layout');
  104. } else {
  105. if ($widgetfound) {
  106. $intitle = qa_post_text('title');
  107. $inposition = qa_post_text('position');
  108. $intemplates = array();
  109. if (qa_post_text('template_all'))
  110. $intemplates[] = 'all';
  111. foreach (array_keys($templateoptions) as $template) {
  112. if (qa_post_text('template_' . $template))
  113. $intemplates[] = $template;
  114. }
  115. $intags = implode(',', $intemplates);
  116. // Perform appropriate database action
  117. if (isset($editwidget['widgetid'])) { // changing existing widget
  118. $widgetid = $editwidget['widgetid'];
  119. qa_db_widget_set_fields($widgetid, $intags);
  120. } else
  121. $widgetid = qa_db_widget_create($intitle, $intags);
  122. qa_db_widget_move($widgetid, substr($inposition, 0, 2), substr($inposition, 2));
  123. }
  124. qa_redirect('admin/layout');
  125. }
  126. }
  127. }
  128. // Prepare content for theme
  129. $qa_content = qa_content_prepare();
  130. $qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/layout_title');
  131. $qa_content['error'] = $securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
  132. $positionoptions = array();
  133. $placeoptionhtml = qa_admin_place_options();
  134. $regioncodes = array(
  135. 'F' => 'full',
  136. 'M' => 'main',
  137. 'S' => 'side',
  138. );
  139. foreach ($placeoptionhtml as $place => $optionhtml) {
  140. $region = $regioncodes[substr($place, 0, 1)];
  141. $widgetallowed = method_exists($module, 'allow_region') && $module->allow_region($region);
  142. if ($widgetallowed) {
  143. foreach ($widgets as $widget) {
  144. if ($widget['place'] == $place && $widget['title'] == $editwidget['title'] && $widget['widgetid'] !== @$editwidget['widgetid'])
  145. $widgetallowed = false; // don't allow two instances of same widget in same place
  146. }
  147. }
  148. if ($widgetallowed) {
  149. $previous = null;
  150. $passedself = false;
  151. $maxposition = 0;
  152. foreach ($widgets as $widget) {
  153. if ($widget['place'] == $place) {
  154. $positionhtml = $optionhtml;
  155. if (isset($previous))
  156. $positionhtml .= ' - ' . qa_lang_html_sub('admin/after_x', qa_html($passedself ? $widget['title'] : $previous['title']));
  157. if ($widget['widgetid'] == @$editwidget['widgetid'])
  158. $passedself = true;
  159. $maxposition = max($maxposition, $widget['position']);
  160. $positionoptions[$place . $widget['position']] = $positionhtml;
  161. $previous = $widget;
  162. }
  163. }
  164. if (!isset($editwidget['widgetid']) || $place != @$editwidget['place']) {
  165. $positionhtml = $optionhtml;
  166. if (isset($previous))
  167. $positionhtml .= ' - ' . qa_lang_html_sub('admin/after_x', $previous['title']);
  168. $positionoptions[$place . (isset($previous) ? (1 + $maxposition) : 1)] = $positionhtml;
  169. }
  170. }
  171. }
  172. $positionvalue = @$positionoptions[$editwidget['place'] . $editwidget['position']];
  173. $qa_content['form'] = array(
  174. 'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
  175. 'style' => 'tall',
  176. 'fields' => array(
  177. 'title' => array(
  178. 'label' => qa_lang_html('admin/widget_name') . ' &nbsp; ' . qa_html($editwidget['title']),
  179. 'type' => 'static',
  180. 'tight' => true,
  181. ),
  182. 'position' => array(
  183. 'id' => 'position_display',
  184. 'tags' => 'name="position"',
  185. 'label' => qa_lang_html('admin/position'),
  186. 'type' => 'select',
  187. 'options' => $positionoptions,
  188. 'value' => $positionvalue,
  189. ),
  190. 'delete' => array(
  191. 'tags' => 'name="dodelete" id="dodelete"',
  192. 'label' => qa_lang_html('admin/delete_widget_position'),
  193. 'value' => 0,
  194. 'type' => 'checkbox',
  195. ),
  196. 'all' => array(
  197. 'id' => 'all_display',
  198. 'label' => qa_lang_html('admin/widget_all_pages'),
  199. 'type' => 'checkbox',
  200. 'tags' => 'name="template_all" id="template_all"',
  201. 'value' => is_numeric(strpos(',' . @$editwidget['tags'] . ',', ',all,')),
  202. ),
  203. 'templates' => array(
  204. 'id' => 'templates_display',
  205. 'label' => qa_lang_html('admin/widget_pages_explanation'),
  206. 'type' => 'custom',
  207. 'html' => '',
  208. ),
  209. ),
  210. 'buttons' => array(
  211. 'save' => array(
  212. 'label' => qa_lang_html(isset($editwidget['widgetid']) ? 'main/save_button' : ('admin/add_widget_button')),
  213. ),
  214. 'cancel' => array(
  215. 'tags' => 'name="docancel"',
  216. 'label' => qa_lang_html('main/cancel_button'),
  217. ),
  218. ),
  219. 'hidden' => array(
  220. 'dosavewidget' => '1', // for IE
  221. 'edit' => @$editwidget['widgetid'],
  222. 'title' => @$editwidget['title'],
  223. 'code' => qa_get_form_security_code('admin/widgets'),
  224. ),
  225. );
  226. foreach ($templateoptions as $template => $optionhtml) {
  227. $qa_content['form']['fields']['templates']['html'] .=
  228. '<input type="checkbox" name="template_' . qa_html($template) . '"' .
  229. (is_numeric(strpos(',' . @$editwidget['tags'] . ',', ',' . $template . ',')) ? ' checked' : '') .
  230. '/> ' . $optionhtml . '<br/>';
  231. }
  232. if (isset($editwidget['widgetid'])) {
  233. qa_set_display_rules($qa_content, array(
  234. 'templates_display' => '!(dodelete||template_all)',
  235. 'all_display' => '!dodelete',
  236. ));
  237. } else {
  238. unset($qa_content['form']['fields']['delete']);
  239. qa_set_display_rules($qa_content, array(
  240. 'templates_display' => '!template_all',
  241. ));
  242. }
  243. if (!$widgetfound) {
  244. unset($qa_content['form']['fields']['title']['tight']);
  245. $qa_content['form']['fields']['title']['error'] = qa_lang_html('admin/widget_not_available');
  246. unset($qa_content['form']['fields']['position']);
  247. unset($qa_content['form']['fields']['all']);
  248. unset($qa_content['form']['fields']['templates']);
  249. if (!isset($editwidget['widgetid']))
  250. unset($qa_content['form']['buttons']['save']);
  251. } elseif (!count($positionoptions)) {
  252. unset($qa_content['form']['fields']['title']['tight']);
  253. $qa_content['form']['fields']['title']['error'] = qa_lang_html('admin/widget_no_positions');
  254. unset($qa_content['form']['fields']['position']);
  255. unset($qa_content['form']['fields']['all']);
  256. unset($qa_content['form']['fields']['templates']);
  257. unset($qa_content['form']['buttons']['save']);
  258. }
  259. $qa_content['navigation']['sub'] = qa_admin_sub_navigation();
  260. return $qa_content;