<?php
// $Id:

/**
 * @file
 * Creates a unique Site Wide Contact form with out drop down menu for each of the Contact Categories.
 */

/**
 * Implements hook_menu().
 */
function contact_forms_menu() {

  $items['admin/structure/contact/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_forms_settings'),
    'access arguments' => array('access site-wide contact form'),
    /*'type' => MENU_NORMAL_ITEM,*/
  );

  return $items;
}

/**
 * Implementation of hook_help
 */
function contact_forms_help($path, $arg) {
  switch ($path) {

    case 'admin/help#contact_forms':
      $output = '';

      /*$output .= '<h3>' . t('About') . '</h3>';*/
      $output .= '<p>' . t('The Contact Forms module adds the following features to the Site Wide Contact Form generated by the core contact module.') . '</p>';
      $output .= '<dl>';
      $output .= '<dd>Unique Contact form for each category</dd>';
      $output .= '<dd>Abilty to set Additional information for each form or to use a global Additional information. </dd>';
      $output .= '<dd>Abilty to set Additional Information for each form or if left empty the global Additional Information will be used. </dd>';

      $output .= '</dl>';

      /*$output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';



      $output .= '</dl>';*/

      return $output;


    case 'admin/structure/contact/settings':
      $output = '';
      $output .= '<p>' . t('The Contact Forms module creates unique contact forms for each contact category with a unique path. Here you can set the default title and header which will be used if they are not specified on the category edit page.') . '</p>';
      $output .= '<p>' . t('More help can be found at  <a href="@help">Contact Forms help page</a>.', array('@help' => url('admin/help/contact_forms'))) . '</p>';

      return $output;

    case 'admin/structure/contact/edit/%':
      $output = '';
      $output .= '<p>' . t('Help can be found at  <a href="@help">Contact Forms help page</a>.', array('@help' => url('admin/help/contact_forms'))) . '</p>';

      return $output;
        /* ???? */
        case 'admin/structure/contact':
      $output = '<p><h3>' . t('Features added by Contact Forms Module.') . '</h3>' . t('Now that you have the Contact Forms module enabled you can add a unique title and additional text to each category on it\'s edit page or a global title and additional information on the <a href="@settings">Contact Forms settings page</a>.', array('@settings' => url('admin/structure/contact/settings'))) . '</p>';

      return $output;
  }
}

/**
 * Implementation of hook_settings
 */
function contact_forms_settings(){

  drupal_set_title(t('Contact Forms Settings'));

  $form['contact_forms_redirect'] = array(
    '#type' => 'textfield',
    '#title' => t('Contact Form redirect'),
    '#default_value' => variable_get('contact_forms_redirect', 'contact'),
    '#weight' => -3,
    '#maxlength' => 60,
    '#description' => t('The page you would like to redirect to if a contact/category path is entered that doesn\'t exist.'),
    '#required' => false,
  );

  $form['contact_forms_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Title for individual contact pages'),
    '#default_value' => variable_get('contact_forms_title', 'Contact @category'),
    '#weight' => -2,
    '#maxlength' => 60,
    '#description' => t('If a category doesn\'t have a page title specified this will be shown. To place the category name in the title use the wildcard "@category".'),
    '#required' => true,
  );

  $form['contact_forms_information'] = array(
    '#type' => 'textarea',
    '#title' => t('Default Additional Information for individual contact pages'),
    '#weight' => -1,
    '#default_value' => variable_get('contact_forms_information', t('You can send @category a message using the contact form below.')),
    '#description' => t('If a category doesn\'t have additional information specified this will be shown.  To place the category name in your message use the wildcard "@category" e.g. You can send @category a message using the contact form below.'),
   );

  return system_settings_form($form);
}


/**
 * Implementation of hook_form_alter()
 */
function contact_forms_form_alter(&$form, $form_state, $form_id) {
  $path = $_GET['q'];

  // redirect contact if another fall back page is defined
  if ($path == 'contact' && variable_get('contact_forms_redirect', 'contact') != 'contact') {
    drupal_goto(variable_get('contact_forms_redirect', 'contact'));
  }

  // Alter all contact forms except for /contact
  if ($form_id == 'contact_site_form' && $path != 'contact') {

    $category = str_replace( array('-','_') , ' ' , arg(1));
    $categories_data =  db_query("SELECT * FROM {contact} WHERE LOWER(category) = LOWER(:category)", array(':category' => $category,))->fetchObject();

     // if category doesn't exist redirect to 'contact' or User Defined Page
    if (!$categories_data) {
      drupal_goto(variable_get('contact_forms_redirect', 'contact'));
    }
    // Set Contact Form Title
    $contact_forms_title = (!$categories_data->page_title) ? variable_get('contact_forms_title', 'Contact @category') : $categories_data->page_title ;
    $contact_forms_title = str_replace( '@category', $categories_data->category, $contact_forms_title);
    drupal_set_title(check_plain($contact_forms_title));
    // Get Additional Info
    $additional_info = (!$categories_data->page_info) ? variable_get('contact_forms_information' , 'You can send @category a message using the contact form below.') : $categories_data->page_info ;
    $additional_info = str_replace( '@category', $categories_data->category, $additional_info);


    $form['contact_information'] = array(
      '#markup' => filter_xss_admin($additional_info),
      '#weight' => -1,
      '#prefix' => '<div class="form-item">',
      '#suffix' => '</div>',
     );

    $subject = str_replace( array('-','_') , ' ' , arg(2));

    $form['subject'] = array('#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 255,
      '#default_value' => $subject,
      '#required' => TRUE,
    );

    $form['cid'] = array(
      '#type' => 'hidden',
      '#value' => $categories_data->cid,
      '#required' => TRUE,
    );
  }

/**
 * Alter the contact_category_edit_form
 */
  if($form_id == 'contact_category_edit_form'){

    $cid = $form['cid']['#value'];

    if ($cid) {
      $query = db_query('SELECT * FROM {contact} WHERE cid = :cid', array(':cid' => $cid,));
      $contact = $query->fetchObject();
    }

    // Adds a text field that will hold category specific info for the contact page information
    $form['page_title'] = array(
      '#type' => 'textfield',
      '#title' => t('Page Title'),
      '#weight' => -1,
      '#default_value' => (isset($contact->page_title))? $contact->page_title : '',
      '#description' => t('Page Title for this individual contact page. If this is left empty the "Default Page Title" will be displayed'),
    );

    // Adds a text area that will hold category specific info for the contact page information
    $form['page_info'] = array(
      '#type' => 'textarea',
      '#title' => t('Additional Information'),
      '#weight' => 0,
      '#default_value' => (isset($contact->page_info))? $contact->page_info : '',
      '#description' => t('Information to show on the individual contact page. If this is left empty the "Default Additional Information" will be displayed'),
    );

    // Set the weight of the category name so It appears above our inserted info area
    $form['category']['#weight']='-2';
  }
}

/**
 * Implementation of hook_menu_alter
 */
 function contact_forms_menu_alter(&$items) {
  $items['contact/%'] = $items['contact'];

  $items['admin/structure/contact'] = array(
    'title' => 'Contact form',
    'description' => 'Create a system contact form and set up categories for the form to use.',
    'page callback' => 'contact_forms_category_list',
    'access arguments' => array('administer contact forms'),
  );

unset($items['admin/structure/contact']['file']);


}

/**
 * Categories/list tab.
 */
function contact_forms_category_list() {
  $header = array(
    t('Category'),
    t('Recipients'),
    t('Weight'),
    t('Selected'),
    t('Title'),
    t('Info'),
    array('data' => t('Operations'), 'colspan' => 2),
  );
  $rows = array();

  // Get all the contact categories from the database.
  $categories = db_query('SELECT cid, category, recipients, selected, page_title, weight,  page_info FROM {contact} ORDER BY weight, category')->fetchAll();

  // Loop through the categories and add them to the table.
  foreach ($categories as $category) {
    $title_status = ($category->page_title!='') ? 'Custom' : 'Default';
    $info_status = ($category->page_info!='') ? 'Custom' : 'Default';
    $rows[] = array(
      l(check_plain($category->category), 'contact/' . check_plain($category->category), array('attributes' => array('target' => '_blank'))),
      check_plain($category->recipients),
      $category->weight,
      ($category->selected ? t('Yes') : t('No')),
      $title_status,
      $info_status,
      l(t('Edit'), 'admin/structure/contact/edit/' . $category->cid),
      l(t('Delete'), 'admin/structure/contact/delete/' . $category->cid),
    );
  }

  if (!$rows) {
    $rows[] = array(array(
      'data' => t('No categories available.'),
      'colspan' => 7,
    ));
  }

  return theme('table', array('header' => $header, 'rows' => $rows));
}


/**
 * Implementation of hook_schema_alter
 */
function contact_forms_schema_alter(&$schema) {
  // Add field to existing schema.
  $schema['contact']['fields']['page_title'] = array(
    'type' => 'text',
	  'not null' => FALSE,
    'size' => 'normal',
    'description' => 'Page Title displayed on the individual contact form pages',
  );
  $schema['contact']['fields']['page_info'] = array(
    'type' => 'text',
	  'not null' => FALSE,
    'size' => 'big',
    'description' => 'Category Page Information Displayed on the individual contact form pages',
  );
}