<?php 

/**
 * @file
 * The Read Only Mode module adds another option to the Site Maintenance page
 * With this option, it is possible to keep the site online but only disable content moderation.
 */

/**
* Implementation of hook_form_alter().
* Permit posting of content
*/
function readonlymode_form_alter(&$form, $form_state, $form_id) {
  if (variable_get('site_readonly', FALSE)) {
    switch ($form_id) {
      case 'webform_client_form_' . @$form['#node']->nid:  // Disable webforms
      case 'comment_form':                      // Disable comments
      case 'user_register':                     // Disable user creation
      case 'user_profile_form':                 // Disable user edit
      case @$form['#node']->type .'_node_form':  // Disable node/add
      case 'node_admin_content':                // Disable admin content moderation
      case 'comment_admin_overview':            // Disable admin comment moderation
        global $user;
        if ($user->uid != 1) {
          $form = array();
          if ($url = variable_get('site_readonly_url', '')) {
            drupal_goto($url);
          }
          else{
            $form['notice'] = array('#value' => _readonlymode_notice());
          }
        }
        else {
          drupal_set_message(t('The site is currently set to read-only, content moderation is disabled for all users but you.'), 'error');
        }
        break;
    }
  }
  return $form;
}

/**
* Implementation of hook_form_FORM_ID_alter().
* Alter the Site Maintenance form
*/
function readonlymode_form_system_site_maintenance_mode_alter(&$form, $form_state) {
  if (!variable_get('site_offline', 0)) {
    $form["read_only"] = array(
      "#title" => "Read Only Mode",
      "#type" => "fieldset",
      "#weight" => 0,
      "#collapsible" => TRUE,
      "#collapsed" => variable_get('site_readonly', FALSE) ? FALSE : TRUE,
    );
    $form["read_only"]["site_readonly"] = array(
      "#type" => "checkbox",
      "#title" => t("Read only mode"),
      "#description" => t('Put the site in read-only mode. When set to "Read-only", all content moderation (add/edit) will be impossible.'),
      "#weight" => 0,
      "#default_value" => variable_get('site_readonly', FALSE),
    );
    $form["read_only"]["site_readonly_message"] = array(
      "#type" => "textarea",
      "#title" => t("Site read-only message"),
      "#description" => t("The module will replace the forms for posting/editing with the message entered here."),
      "#default_value" => _readonlymode_notice(),
      "#required" => TRUE,
    );
    $form["read_only"]["site_readonly_url"] = array(
      "#type" => "textfield",
      "#title" => t("Redirect path"),
      "#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."),
      "#default_value" => variable_get('site_readonly_url', ''),
    );
    $form['#validate'][] = 'readonlymode_form_validate_url';
  }
  else{
    $form["site_offline"]["#description"] .= " <strong>" . ('To set the website in Read-Only Mode, you have to set the site status to Online first.') . "</strong>";
  }
}

/**
* Implementation of hook_form_validate().
*/
function readonlymode_form_validate_url(&$form, $form_state) {
  if ($path = $form_state['values']['site_readonly_url']) {
    $item = menu_get_item($path);
    if (!$item || !$item['access']) {
      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)));
    }
  }
}

function _readonlymode_notice() {
  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'))));
}