/[drupal]/contributions/modules/hidden/hidden.admin.inc
ViewVC logotype

Contents of /contributions/modules/hidden/hidden.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Dec 18 15:29:07 2008 UTC (11 months, 1 week ago) by ekes
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +164 -160 lines
File MIME type: text/x-php
#276732 Continuing D6 migration
  admin pages forms upgraded to D6
  tests written for admin pages
  filters section reworked and using drupal_write_record
1 <?php
2 // $Id: hidden.admin.inc,v 1.1 2008/12/10 23:03:47 ekes Exp $
3
4 /**
5 * @file
6 * Hidden configuration administration.
7 */
8
9 /**
10 * Menu callback; Module Configuration.
11 *
12 * @ingroup forms
13 * @see theme_hidden_admin_settings()
14 */
15 function hidden_admin_settings(&$form_state) {
16 $form['hidden_logging'] = array(
17 '#tree' => 'TRUE',
18 );
19 $form['hidden_logging']['header'] = array(
20 '#value' => array(t('Type'), t('Logged')),
21 );
22 $logging = _hidden_settings_logging();
23 $logform = array(
24 HIDDEN_LOG_ERROR => array(
25 'title' => t('Errors'),
26 'description' => t('Error messages. You probably want to log these.'),
27 ),
28 HIDDEN_LOG_DEBUG_FILTER => array(
29 'title' => t('Filter Debug'),
30 'description' => t('Logs when content is filtered. You may want this if filters are not working as you expect.'),
31 ),
32 HIDDEN_LOG_DEBUG => array(
33 'title' => t('Debug'),
34 'description' => t('Debug messages. You probably don\'t want these at all.'),
35 ),
36 );
37 foreach ($logform as $row => $content) {
38 $form['hidden_logging'][$row]['name'] = array(
39 '#type' => 'item',
40 '#value' => $content['title'],
41 '#description' => $content['description']
42 );
43 $form['hidden_logging'][$row]['enabled'] = array(
44 '#type' => 'checkbox',
45 '#default_value' => $logging[$row]['enabled'],
46 );
47 }
48
49 $email = _hidden_settings_mail();
50 $frequency = drupal_map_assoc(array(21600, 43200, 64800, 86400, 172800), 'format_interval');
51 $form['hidden_email'] = array(
52 '#tree' => 'TRUE',
53 );
54 $form['hidden_email']['domail'] = array(
55 '#type' => 'checkbox',
56 '#title' => t('enable e-mailing'),
57 '#default_value' => $email['domail'],
58 '#description' => t('if you want e-mail of hides, reports etc. enabled.'),
59 );
60 $form['hidden_email']['address'] = array(
61 '#type' => 'textfield',
62 '#title' => t('recipient address'),
63 '#size' => 80,
64 '#maxlength' => 255,
65 '#default_value' => $email['address'],
66 '#description' => t('e-mail address for hidden reports to be sent to'),
67 );
68 $form['hidden_email']['frequency'] = array(
69 '#type' => 'select',
70 '#title' => t('Periodic e-mail frequency'),
71 '#options' => $frequency,
72 '#default_value' => $email['frequency'],
73 '#description' => t('How often any periodic e-mails should be sent out'),
74 );
75 $form['hidden_email']['header'] = array(
76 '#value' => array(t('Log message'), t('e-mail')),
77 );
78 $emailform = array(
79 HIDDEN_LOG_DELETE => array(
80 'title' => t('Delete hidden content'),
81 'description' => t('When hidden content is deleted'),
82 ),
83 HIDDEN_LOG_HIDE => array(
84 'title' => t('Hide content'),
85 'description' => t('When content is hidden'),
86 ),
87 HIDDEN_LOG_UNHIDE => array(
88 'title' => t('Unhide content'),
89 'description' => t('When hidden content is unhidden'),
90 ),
91 HIDDEN_LOG_REPORTED => array(
92 'title' => t('Report content'),
93 'description' => t('When content is reported for hiding'),
94 ),
95 HIDDEN_LOG_SEEN => array(
96 'title' => t('Reported content marked as seen'),
97 'description' => t('When content that has been reported is marked as seen'),
98 ),
99 HIDDEN_LOG_REASONS => array(
100 'title' => t('Changes to hidden reasons'),
101 'description' => t('When the reason options for hiding content are changed'),
102 ),
103 );
104 if (module_exists('spam')) {
105 $emailform[HIDDEN_LOG_SPAM] = array(
106 'title' => t('Reported content marked as spam'),
107 'description' => t('When reported content is marked as spam, only works from the hidden admin pages'),
108 );
109 }
110 foreach ($emailform as $row => $content) {
111 if (is_array($content)) {
112 $form['hidden_email'][$row]['name'] = array(
113 '#type' => 'item',
114 '#title' => $content['title'],
115 '#description' => $content['description'],
116 );
117 $form['hidden_email'][$row]['when'] = array(
118 '#type' => 'radios',
119 '#default_value' => $email[$row]['when'],
120 '#options' => array(t('don\'t mail'), t('mail immediately'), t('mail periodically')),
121 );
122 }
123 }
124 return system_settings_form($form);
125 }
126
127 /********************************************************************
128 * Reasons
129 */
130
131 /**
132 * Menu callback; Hidden admin change reasons for hiding
133 */
134 function hidden_reasons_admin() {
135 $enabled = array();
136 $disabled = array();
137 $reasons = hidden_reason_get_all(FALSE);
138 foreach ($reasons as $reason) {
139 if ($reason->enabled) {
140 $enabled[] = array(
141 check_plain($reason->title),
142 l(t('edit'), 'admin/content/hidden/reasons/edit/'. $reason->rid),
143 l(t('disable'), 'admin/content/hidden/reasons/disable/'. $reason->rid),
144 );
145 }
146 else {
147 $disabled[] = array(
148 check_plain($reason->title),
149 l(t('edit'), 'admin/content/hidden/reasons/edit/'. $reason->rid),
150 l(t('enable'), 'admin/content/hidden/reasons/enable/'. $reason->rid),
151 );
152 }
153 }
154 if (count($enabled) == 0) {
155 $enabled[] = array(t('No enabled reasons'), '', '');
156 }
157 if (count($disabled) == 0) {
158 $disabled[] = array(t('No disabled reasons'), '', '');
159 }
160
161 $table = theme('item_list', array(l(t('Add new reason'), 'admin/content/hidden/reasons/add/'))); // yes I know one item
162 $header = array(t('Reason'), array('data' => t('Operations'), 'colspan' => '2'));
163 $table .= theme('table', $header, $enabled);
164 $page = theme('box', t('Reasons for hiding'), $table);
165 $table = theme('table', $header, $disabled);
166 $page .= theme('box', t('Disabled reasons for hiding'), $table);
167
168 return $page;
169 }
170
171 /**
172 * Menu callback; Admin new or edit hidden reason
173 *
174 * @ingroup forms
175 * @see hidden_reasons_admin_form_submit
176 */
177 function hidden_reasons_admin_form(&$form_state, $op, $rid=0) {
178 $rid = (int)$rid;
179 if ($op == 'edit') {
180 if (!($item = db_fetch_array(db_query('SELECT * FROM {hidden_reasons} WHERE rid = %d', $rid)))) {
181 drupal_not_found();
182 return;
183 }
184 }
185 elseif ($op == 'add') {
186 $item=array('rid' => '0', 'enabled' => '1');
187 }
188 else {
189 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_reasons_admin_form() passed invalid $op');
190 drupal_goto('admin/content/hidden/reasons');
191 }
192
193 $form['title'] = array(
194 '#type' => 'textfield',
195 '#title' => t('Title'),
196 '#default_value' => check_plain($item['title']),
197 '#maxlength' => 255,
198 '#description' => t('The name of the reason for hiding.'),
199 '#required' => TRUE,
200 );
201 $form['description'] = array(
202 '#type' => 'textarea',
203 '#title' => t('Description'),
204 '#default_value' => $item['description'], // I want to filter this
205 '#description' => t('The description of the reason. This could include a link to editorial guidelines.'),
206 );
207 $form['enabled'] = array(
208 '#type' => 'checkbox',
209 '#title' => t('Enabled'),
210 '#default_value' => $item['enabled'],
211 '#description' => t('If this reason is available to be chosen from the hiding menu.'),
212 );
213 $form['rid'] = array('#type' => 'value', '#value' => $item['rid']);
214 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
215
216 return $form;
217 }
218
219 /**
220 * Implementation of form API hook; submit hidden_reasons_admin_form().
221 */
222 function hidden_reasons_admin_form_submit($form, &$form_state) {
223 $title = (string) $form_state['values']['title'];
224 $description = (string) $form_state['values']['description'];
225 $rid = (int) $form_state['values']['rid'];
226 $enabled = (bool) $form_state['values']['enabled'];
227
228 if ($rid) {
229 $result = hidden_reason_update($title, $description, $enabled, $rid);
230 }
231 else {
232 $result = hidden_reason_create($title, $description, $enabled);
233 }
234
235 $t_args = array('%title' => $title);
236 if ($result == FALSE) {
237 drupal_set_message(t('Error saving reason %title', $t_args), 'error');
238 _hidden_log(HIDDEN_LOG_ERROR, t('Error saving reason %title', $t_args));
239 }
240 elseif ($result == $rid) {
241 drupal_set_message(t('The reason %title has been updated.', $t_args));
242 _hidden_log(HIDDEN_LOG_REASONS, t('Reason %title changed.', $t_args), 'reason', $rid);
243 }
244 else {
245 drupal_set_message(t('The reason %title has been added.', $t_args));
246 _hidden_log(HIDDEN_LOG_REASONS, t('Reason %title added.', $t_args), 'reason', $rid);
247 }
248
249 $form_state['redirect'] = 'admin/content/hidden/reasons';
250 }
251
252 /**
253 * Menu callback; hidden_reasons_admin en/dis-able a reason
254 */
255 function hidden_reasons_admin_able($action, $rid) {
256 $rid = (int)$rid;
257
258 if ($action == 'enable') {
259 $result = hidden_reason_enable($rid);
260 }
261 elseif ($action == 'disable') {
262 $result = hidden_reason_disable($rid);
263 }
264 else {
265 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_reasons_admin_able() called with invalid $action');
266 }
267
268 if ($result) {
269 $action = ($action == 'enable') ? 'enabled' : 'disabled';
270 drupal_set_message(t('Reason %action', array('%action' => $action)));
271 }
272 else {
273 $action = ($action == 'enable') ? 'enabling' : 'disabling';
274 drupal_set_message(t('Error %action reason', array('%action' => $action)));
275 }
276
277 drupal_goto('admin/content/hidden/reasons');
278 }
279
280 /********************************************************************
281 * Filters
282 */
283
284 /**
285 * Menu callback; administer filters.
286 */
287 function hidden_filters_admin() {
288 $enabled = theme('item_list', array(l(t('Add new filter'), 'admin/content/hidden/filters/add')));
289 $enabled .= drupal_get_form('hidden_filters_admin_enabled_form');
290 $page = theme('box', t('Enabled filters'), $enabled);
291
292 $result = hidden_filter_all_get_pager(0, 0);
293 $header = array(t('filter name'), t('hits'), t('last hit'), array('data' => t('operations'), '#colspan' => 2));
294 $rows = array();
295 while ($filter = db_fetch_object($result)) {
296 $rows[] = array(
297 check_plain($filter->title),
298 $filter->hits,
299 ($filter->date==0) ? t('Never') : format_date($filter->date, 'small'),
300 l(t('edit'), 'admin/content/hidden/filters/edit/'. $filter->hfid),
301 l(t('enable'), 'admin/content/hidden/filters/enable/'. $filter->hfid),
302 );
303 }
304 if (count($rows)==0) {
305 $rows[] = array(t('No disabled filters'), '', '', '', '');
306 }
307 $disabled = theme('table', $header, $rows);
308 $page .= theme('box', t('Disabled filters'), $disabled);
309
310 return $page;
311 }
312
313 /**
314 * Menu callback; form to add or edit a filter
315 *
316 * @ingroup forms
317 * @see hidden_filters_admin_form_validate()
318 * @see hidden_filters_admin_form_submit()
319 */
320 function hidden_filters_admin_form(&$form_state, $op, $hfid=0) {
321 $hfid = (int) $hfid;
322 if ($op == 'edit') {
323 if (! $item=hidden_filter_load($hfid)) {
324 drupal_not_found();
325 return;
326 }
327 }
328 elseif ($op == 'add') {
329 $item = (object)array('hfid' => 0, 'enabled' => 1, 'rid' => 0, 'delay' => 1, 'type' => HIDDEN_FILTER_PLAIN, 'weight' => 0);
330 }
331 else {
332 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_filters_admin_form() passed invalid $op');
333 drupal_goto('admin/content/hidden/filters');
334 }
335
336 $form['title'] = array(
337 '#type' => 'textfield',
338 '#title' => t('Title'),
339 '#default_value' => isset($item->title) ? check_plain($item->title) : '',
340 '#maxlength' => 255,
341 '#description' => t('A name for the filter.'),
342 '#required' => TRUE,
343 );
344 $form['filter'] = array(
345 '#type' => 'textfield',
346 '#title' => t('Filter'),
347 '#default_value' => isset($item->filter) ? $item->filter : '',
348 '#maxlength' => 255,
349 '#description' => t('What to filter for.'),
350 '#required' => TRUE,
351 );
352 $form['type'] = array(
353 '#type' => 'radios',
354 '#title' => t('Type of filter'),
355 '#default_value' => $item->type,
356 '#options' => array(HIDDEN_FILTER_PLAIN => t('Plain text'), HIDDEN_FILTER_PREG => t('Regular Expression')),
357 '#description' => t('Plain text will search contents for the filter. A regular expression needs to be in the form of a Perl Compatible Regular Expression enclosed in /'),
358 '#required' => TRUE,
359 );
360 $form['delay'] = array(
361 '#type' => 'select',
362 '#title' => t('Delay'),
363 '#default_value' => $item->delay,
364 '#options' => array(0 => t('immediate'), 1 => t('next cron job'), 1800 => t('30 minutes'), 3600 => t('1 hour'), 7200 => t('2 hours')),
365 '#description' => t('If the hiding should be delayed. Immediate will report the hide to the poster. Others depend on cron so may take longer depending on how often cron is run'),
366 );
367 $form['weight'] = array(
368 '#type' => 'weight',
369 '#title' => t('Weight'),
370 '#default_value' => $item->weight,
371 '#description' => t('The weight of the filter. Run is descending order. Needed if certain filters should be run first. Filtering stops when first match is found.'),
372 );
373 $form['enabled'] = array(
374 '#type' => 'checkbox',
375 '#title' => t('Enabled'),
376 '#default_value' => $item->enabled,
377 '#description' => t('If this filter is enabled.'),
378 );
379 if (isset($item->uid)) {
380 $hide_user = user_load($item->uid);
381 $item->name = is_object($hide_user) ? $hide_user->name : '';
382 }
383 // @TODO move _hidden_hide_form?
384 require_once('hidden.action-pages.inc');
385 global $user;
386 $form['hidden'] = _hidden_hide_form($user, $item);
387 $form['hidden']['#type'] = 'fieldset';
388 $form['hidden']['#title'] = t('Reason to use for hiding');
389 $form['hfid'] = array('#type' => 'value', '#value' => $item->hfid);
390 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
391 return $form;
392 }
393
394 /**
395 * Implementation of form API hook; validate hidden_filters_admin_form().
396 */
397 function hidden_filters_admin_form_validate($form, &$form_state) {
398 if ($form_state['values']['type']==HIDDEN_FILTER_PREG) {
399 $msg = '';
400 if (! _hidden_filter_preg_test($form_state['values']['filter'], $msg)) {
401 form_set_error('filter', $msg);
402 }
403 }
404 }
405
406 /**
407 * Implementation of form API hook; submit hidden_filters_admin_form().
408 */
409 function hidden_filters_admin_form_submit($form, &$form_state) {
410 $filter = new stdClass();
411 $filter->hfid = (int) $form_state['values']['hfid'];
412 $filter->title = (string) $form_state['values']['title'];
413 $filter->filter = (string) $form_state['values']['filter'];
414 $filter->type = (int) $form_state['values']['type'];
415 $filter->delay = (int) $form_state['values']['delay'];
416 $filter->weight = (int) $form_state['values']['weight'];
417 $filter->enabled = (int) $form_state['values']['enabled'];
418 if (user_access('administer hidden')) {
419 $user = user_load(array('name' => $form_state['values']['user']));
420 $filter->uid = is_object($user) ? $user->uid : 0;
421 }
422 else {
423 global $user;
424 $filter->uid = $user->uid;
425 }
426 $filter->rid = (int) $form_state['values']['reason'];
427 if (! hidden_reason_check($filter->rid)) {
428 $filter->rid = 0;
429 }
430 $filter->publicnote = (string) $form_state['values']['publictext'];
431 $filter->privatenote = (string) $form_state['values']['privatetext'];
432
433 $result = hidden_filter_save($filter);
434 if ($result == FALSE) {
435 drupal_set_message('Error saving filter.');
436 }
437
438 $form_state['redirect'] = 'admin/content/hidden/filters';
439 }
440
441 /**
442 * Menu callback; execute sql to enable or disable a filter
443 *
444 * @param $action
445 * string 'enable' or 'disable'
446 * @param $hfid
447 * int hidden filter id
448 */
449 function hidden_filters_admin_able($action, $hfid) {
450 $hfid = (int)$hfid;
451
452 if ($action == 'enable') {
453 $able = 1;
454 }
455 elseif ($action == 'disable') {
456 $able = 0;
457 }
458 else {
459 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_filters_admin_able() aclled with invalid $action');
460 drupal_goto('admin/content/hidden/filters');
461 }
462
463 if ($filter = hidden_filter_load($hfid)) {
464 $t_args = array('%title' => check_plain($filter->title), '%action' => $action);
465 if (! db_query('UPDATE {hidden_filters} SET enabled=%d WHERE hfid=%d', $able, $hfid)) {
466 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_filters_admin_able() failed to UPDATE to '. $action, 'filter', $hfid);
467 drupal_set_message(t('Database error trying to %action reason %title', $t_args));
468 }
469 else {
470 _hidden_log(HIDDEN_LOG_DEBUG_FILTER, t('Filter %title changed.', $t_args), 'filter', $hfid);
471 drupal_set_message(t('Filter %title updated', $t_args));
472 }
473 }
474 else {
475 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_admin_filter_able() passed $hfid that does not exist', 'filter', $hfid);
476 drupal_set_message(t('Tried to update a filter that does not exist'), 'error');
477 }
478
479 drupal_goto('admin/content/hidden/filters');
480 }
481
482 /**
483 * form; list enabled filters, submission changes weight order.
484 *
485 * @ingroup forms
486 * @see hidden_filters_admin_enabled_form_submit()
487 */
488 function hidden_filters_admin_enabled_form() {
489 $form['header'] = array(
490 '#type' => 'value',
491 '#value' => array(
492 array('data' => t('filter name')),
493 array('data' => t('hits')),
494 array('data' => t('date')),
495 array('data' => t('weight')),
496 array('data' => t('operations'), 'colspan' => 2),
497 )
498 );
499 $result = hidden_filter_all_get_pager(0, 1);
500 $form['weight'] = array('#tree' => TRUE);
501 while ($filter = db_fetch_object($result)) {
502 $form['title'][$filter->hfid] = array('#value' => check_plain($filter->title));
503 $form['hits'][$filter->hfid] = array('#value' => $filter->hits);
504 $form['date'][$filter->hfid] = array('#value' => ($filter->date==0) ? t('Never') : format_date($filter->date, 'small'));
505 $form['weight'][$filter->hfid] = array('#type' => 'weight', '#default_value' => $filter->weight, '#tree' => TRUE);
506 $form['edit'][$filter->hfid] = array('#value' => l(t('edit'), 'admin/content/hidden/filters/edit/'. $filter->hfid));
507 $form['enable'][$filter->hfid] = array( '#value' => l(t('disable'), 'admin/content/hidden/filters/disable/'. $filter->hfid));
508 }
509
510 $form['submit'] = array('#value' => t('Update weights'), '#type' => 'submit');
511 return $form;
512 }
513
514 /**
515 * theme hidden_filters_admin_enabled_form().
516 *
517 * @ingroup themeable
518 */
519 function theme_hidden_filters_admin_enabled_form($form) {
520 $rows = array();
521 if (isset($form['title']) && is_array($form['title'])) {
522 foreach (element_children($form['title']) as $key) {
523 $row = array();
524 $row[] = drupal_render($form['title'][$key]);
525 $row[] = drupal_render($form['hits'][$key]);
526 $row[] = drupal_render($form['date'][$key]);
527 $row[] = drupal_render($form['weight'][$key]);
528 $row[] = drupal_render($form['edit'][$key]);
529 $row[] = drupal_render($form['enable'][$key]);
530 $rows[] = $row;
531 }
532 }
533 else {
534 $rows[] = array(array('data' => t('No filters enabled'), 'colspan' => '6'));
535 }
536
537 $output = theme('table', $form['header']['#value'], $rows);
538 $output .= drupal_render($form);
539 return $output;
540 }
541
542 /**
543 * Implementation of form API hook; submit hidden_filters_admin_form().
544 */
545 function hidden_filters_admin_enabled_form_submit($form, &$form_state) {
546 $error = FALSE;
547 foreach ($form_state['values']['weight'] as $hfid => $weight) {
548 $weight = array('hfid' => $hfid, 'weight' => $weight);
549 if (! drupal_write_record('hidden_filters', $weight, array('hfid'))) {
550 $error = TRUE;
551 }
552 }
553
554 if ($error) {
555 drupal_set_message(t('Problem updating weights'), 'error');
556 }
557 else {
558 drupal_set_message(t('Weights updated'));
559 }
560 }
561
562 /***********************************************************************
563 * Theme
564 */
565
566 /**
567 * theme hidden_admin_settings() form.
568 *
569 * @ingroup themeable.
570 */
571 function theme_hidden_admin_settings($form) {
572 $rows = array();
573 foreach (element_children($form['hidden_logging']) as $key) {
574 $row = array();
575 if (is_array($form['hidden_logging'][$key]['name'])) {
576 $row[] = drupal_render($form['hidden_logging'][$key]['name']);
577 $row[] = drupal_render($form['hidden_logging'][$key]['enabled']);
578 }
579 $rows[] = $row;
580 }
581 $logging = theme('table', $form['hidden_logging']['header']['#value'], $rows);
582 unset($form['hidden_logging']['header']);
583
584 $rows = array();
585 foreach (element_children($form['hidden_email']) as $key) {
586 $row = array();
587 if (is_array($form['hidden_email'][$key]['when'])) {
588 $row[] = drupal_render($form['hidden_email'][$key]['name']);
589 $row[] = drupal_render($form['hidden_email'][$key]['when']);
590 }
591 $rows[] = $row;
592 }
593 $table = theme('table', $form['hidden_email']['header']['#value'], $rows);
594 unset($form['hidden_email']['header']);
595 $email = drupal_render($form['hidden_email']) . $table;
596
597 $output = theme('box', t('Logging'), $logging);
598 $output .= theme('box', t('e-mailing'), $email);
599
600 $output .= drupal_render($form);
601
602 return $output;
603 }

  ViewVC Help
Powered by ViewVC 1.1.2