readonlymode.module 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @file
  4. * The Read Only Mode module adds another option to the Site Maintenance page
  5. * With this option, it is possible to keep the site online but only disable content moderation.
  6. */
  7. /**
  8. * Implementation of hook_form_alter().
  9. * Permit posting of content
  10. */
  11. function readonlymode_form_alter(&$form, $form_state, $form_id) {
  12. if (variable_get('site_readonly', FALSE)) {
  13. switch ($form_id) {
  14. case 'webform_client_form_' . @$form['#node']->nid: // Disable webforms
  15. case 'comment_form': // Disable comments
  16. case 'user_register': // Disable user creation
  17. case 'user_profile_form': // Disable user edit
  18. case @$form['#node']->type .'_node_form': // Disable node/add
  19. case 'node_admin_content': // Disable admin content moderation
  20. case 'comment_admin_overview': // Disable admin comment moderation
  21. global $user;
  22. if ($user->uid != 1) {
  23. $form = array();
  24. if ($url = variable_get('site_readonly_url', '')) {
  25. drupal_goto($url);
  26. }
  27. else{
  28. $form['notice'] = array('#value' => _readonlymode_notice());
  29. }
  30. }
  31. else {
  32. drupal_set_message(t('The site is currently set to read-only, content moderation is disabled for all users but you.'), 'error');
  33. }
  34. break;
  35. }
  36. }
  37. return $form;
  38. }
  39. /**
  40. * Implementation of hook_form_FORM_ID_alter().
  41. * Alter the Site Maintenance form
  42. */
  43. function readonlymode_form_system_site_maintenance_mode_alter(&$form, $form_state) {
  44. if (!variable_get('site_offline', 0)) {
  45. $form["read_only"] = array(
  46. "#title" => "Read Only Mode",
  47. "#type" => "fieldset",
  48. "#weight" => 0,
  49. "#collapsible" => TRUE,
  50. "#collapsed" => variable_get('site_readonly', FALSE) ? FALSE : TRUE,
  51. );
  52. $form["read_only"]["site_readonly"] = array(
  53. "#type" => "checkbox",
  54. "#title" => t("Read only mode"),
  55. "#description" => t('Put the site in read-only mode. When set to "Read-only", all content moderation (add/edit) will be impossible.'),
  56. "#weight" => 0,
  57. "#default_value" => variable_get('site_readonly', FALSE),
  58. );
  59. $form["read_only"]["site_readonly_message"] = array(
  60. "#type" => "textarea",
  61. "#title" => t("Site read-only message"),
  62. "#description" => t("The module will replace the forms for posting/editing with the message entered here."),
  63. "#default_value" => _readonlymode_notice(),
  64. "#required" => TRUE,
  65. );
  66. $form["read_only"]["site_readonly_url"] = array(
  67. "#type" => "textfield",
  68. "#title" => t("Redirect path"),
  69. "#description" => t("When given, Drupal will redirect the user to this URL when a user tries to add/edit content instead of displaying the message above."),
  70. "#default_value" => variable_get('site_readonly_url', ''),
  71. );
  72. $form['#validate'][] = 'readonlymode_form_validate_url';
  73. }
  74. else{
  75. $form["site_offline"]["#description"] .= " <strong>" . ('To set the website in Read-Only Mode, you have to set the site status to Online first.') . "</strong>";
  76. }
  77. }
  78. /**
  79. * Implementation of hook_form_validate().
  80. */
  81. function readonlymode_form_validate_url(&$form, $form_state) {
  82. if ($path = $form_state['values']['site_readonly_url']) {
  83. $item = menu_get_item($path);
  84. if (!$item || !$item['access']) {
  85. form_set_error('site_readonly_url', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $path)));
  86. }
  87. }
  88. }
  89. function _readonlymode_notice() {
  90. return variable_get('site_readonly_message', t("@sitename is currently in maintenance. During this maintenance it is not possible to add or edit content (like comments and pages).", array("@sitename" => variable_get('site_name', 'drupal'))));
  91. }