admin-widgets.php 9.52 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Controller for admin page for editing widgets


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
29 30


Scott committed
31
// Get current list of widgets and determine the state of this admin page
Scott committed
32

Scott committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
$widgetid = qa_post_text('edit');
if (!strlen($widgetid))
	$widgetid = qa_get('edit');

list($widgets, $pages) = qa_db_select_with_pending(
	qa_db_widgets_selectspec(),
	qa_db_pages_selectspec()
);

if (isset($widgetid)) {
	$editwidget = null;
	foreach ($widgets as $widget) {
		if ($widget['widgetid'] == $widgetid)
			$editwidget = $widget;
Scott committed
47 48
	}

Scott committed
49 50 51 52 53
} else {
	$editwidget = array('title' => qa_post_text('title'));
	if (!isset($editwidget['title']))
		$editwidget['title'] = qa_get('title');
}
Scott committed
54

Scott committed
55 56 57
$module = qa_load_module('widget', @$editwidget['title']);

$widgetfound = isset($module);
Scott committed
58 59


Scott committed
60
// Check admin privileges (do late to allow one DB query)
Scott committed
61

Scott committed
62 63
if (!qa_admin_check_privileges($qa_content))
	return $qa_content;
Scott committed
64 65


Scott committed
66
// Define an array of relevant templates we can use
Scott committed
67

Scott committed
68 69
$templatelangkeys = array(
	'question' => 'admin/question_pages',
70

Scott committed
71 72 73 74 75
	'qa' => 'main/recent_qs_as_title',
	'activity' => 'main/recent_activity_title',
	'questions' => 'admin/question_lists',
	'hot' => 'main/hot_qs_title',
	'unanswered' => 'main/unanswered_qs_title',
76

Scott committed
77 78 79 80
	'tags' => 'main/popular_tags',
	'categories' => 'misc/browse_categories',
	'users' => 'main/highest_users',
	'ask' => 'question/ask_title',
Scott committed
81

Scott committed
82 83 84
	'tag' => 'admin/tag_pages',
	'user' => 'admin/user_pages',
	'message' => 'misc/private_message_title',
Scott committed
85

Scott committed
86 87 88 89 90 91 92 93
	'search' => 'main/search_title',
	'feedback' => 'misc/feedback_title',

	'login' => 'users/login_title',
	'register' => 'users/register_title',
	'account' => 'profile/my_account_title',
	'favorites' => 'misc/my_favorites_title',
	'updates' => 'misc/recent_updates_title',
Scott committed
94

Scott committed
95 96 97
	'ip' => 'admin/ip_address_pages',
	'admin' => 'admin/admin_title',
);
Scott committed
98

Scott committed
99
$templateoptions = array();
Scott committed
100

Scott committed
101 102 103 104 105
if (isset($module) && method_exists($module, 'allow_template')) {
	foreach ($templatelangkeys as $template => $langkey) {
		if ($module->allow_template($template))
			$templateoptions[$template] = qa_lang_html($langkey);
	}
Scott committed
106

Scott committed
107 108 109 110 111 112 113 114 115 116
	if ($module->allow_template('custom')) {
		$pagemodules = qa_load_modules_with('page', 'match_request');
		foreach ($pages as $page) {
			// check if this is a page plugin by fetching all plugin classes and matching requests - currently quite convoluted!
			$isPagePlugin = false;
			foreach ($pagemodules as $pagemodule) {
				if ($pagemodule->match_request($page['tags'])) {
					$isPagePlugin = true;
				}
			}
Scott committed
117

Scott committed
118 119 120
			if ($isPagePlugin || !($page['flags'] & QA_PAGE_FLAGS_EXTERNAL))
				$templateoptions['custom-' . $page['pageid']] = qa_html($page['title']);
		}
Scott committed
121

Scott committed
122 123
	}
}
Scott committed
124 125


Scott committed
126
// Process saving an old or new widget
Scott committed
127

Scott committed
128
$securityexpired = false;
Scott committed
129

Scott committed
130 131
if (qa_clicked('docancel'))
	qa_redirect('admin/layout');
Scott committed
132

Scott committed
133 134
elseif (qa_clicked('dosavewidget')) {
	require_once QA_INCLUDE_DIR . 'db/admin.php';
Scott committed
135

Scott committed
136 137
	if (!qa_check_form_security_code('admin/widgets', qa_post_text('code')))
		$securityexpired = true;
Scott committed
138

Scott committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	else {
		if (qa_post_text('dodelete')) {
			qa_db_widget_delete($editwidget['widgetid']);
			qa_redirect('admin/layout');

		} else {
			if ($widgetfound) {
				$intitle = qa_post_text('title');
				$inposition = qa_post_text('position');
				$intemplates = array();

				if (qa_post_text('template_all'))
					$intemplates[] = 'all';

				foreach (array_keys($templateoptions) as $template) {
					if (qa_post_text('template_' . $template))
						$intemplates[] = $template;
Scott committed
156 157
				}

Scott committed
158 159
				$intags = implode(',', $intemplates);

Scott committed
160
				// Perform appropriate database action
Scott committed
161 162 163 164 165 166 167 168 169

				if (isset($editwidget['widgetid'])) { // changing existing widget
					$widgetid = $editwidget['widgetid'];
					qa_db_widget_set_fields($widgetid, $intags);

				} else
					$widgetid = qa_db_widget_create($intitle, $intags);

				qa_db_widget_move($widgetid, substr($inposition, 0, 2), substr($inposition, 2));
Scott committed
170
			}
Scott committed
171 172

			qa_redirect('admin/layout');
Scott committed
173 174
		}
	}
Scott committed
175
}
Scott committed
176 177


Scott committed
178
// Prepare content for theme
Scott committed
179

Scott committed
180
$qa_content = qa_content_prepare();
Scott committed
181

Scott committed
182 183
$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/layout_title');
$qa_content['error'] = $securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Scott committed
184

Scott committed
185
$positionoptions = array();
Scott committed
186

Scott committed
187
$placeoptionhtml = qa_admin_place_options();
Scott committed
188

Scott committed
189 190 191 192 193
$regioncodes = array(
	'F' => 'full',
	'M' => 'main',
	'S' => 'side',
);
Scott committed
194

Scott committed
195 196
foreach ($placeoptionhtml as $place => $optionhtml) {
	$region = $regioncodes[substr($place, 0, 1)];
Scott committed
197

Scott committed
198
	$widgetallowed = method_exists($module, 'allow_region') && $module->allow_region($region);
Scott committed
199

Scott committed
200 201 202 203 204 205
	if ($widgetallowed) {
		foreach ($widgets as $widget) {
			if ($widget['place'] == $place && $widget['title'] == $editwidget['title'] && $widget['widgetid'] !== @$editwidget['widgetid'])
				$widgetallowed = false; // don't allow two instances of same widget in same place
		}
	}
Scott committed
206

Scott committed
207 208 209 210
	if ($widgetallowed) {
		$previous = null;
		$passedself = false;
		$maxposition = 0;
Scott committed
211

Scott committed
212 213 214
		foreach ($widgets as $widget) {
			if ($widget['place'] == $place) {
				$positionhtml = $optionhtml;
Scott committed
215

Scott committed
216 217
				if (isset($previous))
					$positionhtml .= ' - ' . qa_lang_html_sub('admin/after_x', qa_html($passedself ? $widget['title'] : $previous['title']));
Scott committed
218

Scott committed
219 220
				if ($widget['widgetid'] == @$editwidget['widgetid'])
					$passedself = true;
Scott committed
221

Scott committed
222 223
				$maxposition = max($maxposition, $widget['position']);
				$positionoptions[$place . $widget['position']] = $positionhtml;
Scott committed
224

Scott committed
225 226 227
				$previous = $widget;
			}
		}
Scott committed
228

Scott committed
229 230
		if (!isset($editwidget['widgetid']) || $place != @$editwidget['place']) {
			$positionhtml = $optionhtml;
Scott committed
231

Scott committed
232 233
			if (isset($previous))
				$positionhtml .= ' - ' . qa_lang_html_sub('admin/after_x', $previous['title']);
Scott committed
234

Scott committed
235
			$positionoptions[$place . (isset($previous) ? (1 + $maxposition) : 1)] = $positionhtml;
Scott committed
236 237
		}
	}
Scott committed
238
}
Scott committed
239

Scott committed
240 241 242 243
$positionvalue = @$positionoptions[$editwidget['place'] . $editwidget['position']];

$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_path_html(qa_request()) . '"',
Scott committed
244

Scott committed
245
	'style' => 'tall',
Scott committed
246

Scott committed
247 248 249 250 251
	'fields' => array(
		'title' => array(
			'label' => qa_lang_html('admin/widget_name') . ' &nbsp; ' . qa_html($editwidget['title']),
			'type' => 'static',
			'tight' => true,
Scott committed
252 253
		),

Scott committed
254 255 256 257 258 259 260
		'position' => array(
			'id' => 'position_display',
			'tags' => 'name="position"',
			'label' => qa_lang_html('admin/position'),
			'type' => 'select',
			'options' => $positionoptions,
			'value' => $positionvalue,
Scott committed
261 262
		),

Scott committed
263 264 265 266 267 268
		'delete' => array(
			'tags' => 'name="dodelete" id="dodelete"',
			'label' => qa_lang_html('admin/delete_widget_position'),
			'value' => 0,
			'type' => 'checkbox',
		),
Scott committed
269

Scott committed
270 271 272 273 274 275 276
		'all' => array(
			'id' => 'all_display',
			'label' => qa_lang_html('admin/widget_all_pages'),
			'type' => 'checkbox',
			'tags' => 'name="template_all" id="template_all"',
			'value' => is_numeric(strpos(',' . @$editwidget['tags'] . ',', ',all,')),
		),
Scott committed
277

Scott committed
278 279 280 281 282 283 284
		'templates' => array(
			'id' => 'templates_display',
			'label' => qa_lang_html('admin/widget_pages_explanation'),
			'type' => 'custom',
			'html' => '',
		),
	),
Scott committed
285

Scott committed
286 287 288 289
	'buttons' => array(
		'save' => array(
			'label' => qa_lang_html(isset($editwidget['widgetid']) ? 'main/save_button' : ('admin/add_widget_button')),
		),
Scott committed
290

Scott committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
		'cancel' => array(
			'tags' => 'name="docancel"',
			'label' => qa_lang_html('main/cancel_button'),
		),
	),

	'hidden' => array(
		'dosavewidget' => '1', // for IE
		'edit' => @$editwidget['widgetid'],
		'title' => @$editwidget['title'],
		'code' => qa_get_form_security_code('admin/widgets'),
	),
);

foreach ($templateoptions as $template => $optionhtml) {
	$qa_content['form']['fields']['templates']['html'] .=
		'<input type="checkbox" name="template_' . qa_html($template) . '"' .
		(is_numeric(strpos(',' . @$editwidget['tags'] . ',', ',' . $template . ',')) ? ' checked' : '') .
		'/> ' . $optionhtml . '<br/>';
}

if (isset($editwidget['widgetid'])) {
	qa_set_display_rules($qa_content, array(
		'templates_display' => '!(dodelete||template_all)',
		'all_display' => '!dodelete',
	));

} else {
	unset($qa_content['form']['fields']['delete']);
	qa_set_display_rules($qa_content, array(
		'templates_display' => '!template_all',
	));
}

if (!$widgetfound) {
	unset($qa_content['form']['fields']['title']['tight']);
	$qa_content['form']['fields']['title']['error'] = qa_lang_html('admin/widget_not_available');
	unset($qa_content['form']['fields']['position']);
	unset($qa_content['form']['fields']['all']);
	unset($qa_content['form']['fields']['templates']);
	if (!isset($editwidget['widgetid']))
		unset($qa_content['form']['buttons']['save']);
Scott committed
333

Scott committed
334 335 336 337 338 339 340 341
} elseif (!count($positionoptions)) {
	unset($qa_content['form']['fields']['title']['tight']);
	$qa_content['form']['fields']['title']['error'] = qa_lang_html('admin/widget_no_positions');
	unset($qa_content['form']['fields']['position']);
	unset($qa_content['form']['fields']['all']);
	unset($qa_content['form']['fields']['templates']);
	unset($qa_content['form']['buttons']['save']);
}
Scott committed
342

Scott committed
343
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
344 345


Scott committed
346
return $qa_content;