<?php
// $Id: user_titles.admin.inc,v 1.1.2.1 2010/02/02 11:20:15 agileware Exp $

/**
 * @file
 * Admin page callback file for the user_titles module.
 */

/**
 * Form to determine titles and levels.
 *
 * @return
 *   A form array.
 */
function user_titles_settings_form() {
  $form = array();

  $types = user_titles_get_allowed_types();

  foreach (node_type_get_types() as $type => $info) {
    $nodes[$type] = $info->name;
  }

  $hook_module = user_titles_get_hook_module();
  $hook_modules = module_invoke_all('user_titles', 'register');


  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => 'Settings',
  );
  $form['settings']['hook_module'] = array(
    '#type' => 'radios',
    '#title' => 'Point scheme',
    '#default_value' => $hook_module,
    '#options' => array(),
    '#attributes' => array('class' => array('user-titles-hook-module')),
  );
  foreach ($hook_modules as $module) {
    $name = module_invoke($module, 'user_titles', 'name');
    $form['settings']['hook_module']['#options'][$module] = array(
      'name' => $name,
      'description' => module_invoke($module, 'user_titles', 'description'),
      'url' => module_invoke($module, 'user_titles', 'url'),
    );
    $name_index[$module] = $name;
  }

  array_multisort($name_index, SORT_ASC, SORT_STRING, $form['settings']['hook_module']['#options']);

  // Implementation of user_titles' default behavior
  $form['settings']['types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Counted node types'),
    '#description' => t('Only the checked node types will be counted'),
    '#options' => $nodes,
    '#default_value' => $types,
    '#disabled' => $hook_module !== 'user_titles',
    '#attributes' => array('class' => array('user-titles-types')),
  );
  $form['settings']['types_disabled'] = array(
    '#type' => 'hidden',
    '#default_value' => $hook_module !== 'user_titles',
  );
  // Directory to save images
  $form['settings']['image_dir'] = array(
    '#type' => 'textfield',
    '#title' => t('Image directory'),
    '#description' => t('Subdirectory in the directory %dir where images will be stored.', array('%dir' => variable_get('file_public_path', conf_path() . '/files') .'/')),
    '#default_value' => variable_get('user_titles_image_dir', DEFAULT_IMAGE_DIR),
    '#size' => 35,
    '#attributes' => array('class' => array('user-titles-image-dir')),
  );

  $form['roles-description'] = array(
    '#type' => 'item',
    '#markup' => t('Set the priority of your roles here.  If a user has multiple roles they will get the titles of highest role in this list that they are a member of.  If the users highest role has no titles that user will not get titles.'),
  );
  $form['roles'] = array(
    '#tree' => TRUE,
  );

  $query = db_select('role', 'r');
  $query->join('user_titles_roles', 'ro', 'r.rid = ro.rid');
  $query->addField('r', 'name');
  $query->fields('ro'); //all fields from user_titles_roles
  $query->condition('r.rid', 1, '!=');
  $query->orderBy('ro.weight');
  $result = $query->execute();
  while ($role = $result->fetchAssoc()) {
    $form['roles'][$role['rid']]['name'] = array(
      '#markup' => check_plain($role['name']),
    );
    $form['roles'][$role['rid']]['weight'] = array(
      '#type' => 'weight',
      '#delta' => 30,
      '#default_value' => $role['weight'],
    );
  }

  $form['display'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display'),
  );
  $form['display']['display_on_profile'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display title on user profile'),
    '#default_value' => variable_get('user_titles_display_on_profile', TRUE),
    '#description' => t("If checked, the user's title will be displayed on their profile page."),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

/**
 * Submit function for the user titles setting form.
 */
function user_titles_settings_form_submit($form, &$form_state) {
  // If they change the allowed types, wipe the existing counts so each one will be fresh.
  $types = user_titles_get_allowed_types();

  if ($types != $form_state['values']['types']) {
  	db_truncate('user_titles_posts')->execute();
  }


  if (!$form_state['values']['types_disabled']) {
    user_titles_set_allowed_types(array_keys(array_filter($form_state['values']['types'])));
  }

  user_titles_set_hook_module($form_state['values']['hook_module']);
  variable_set('user_titles_image_dir', $form_state['values']['image_dir']);
  variable_set('user_titles_display_on_profile', $form_state['values']['display_on_profile']);

	$dir = 'public://' . variable_get('user_titles_image_dir', DEFAULT_IMAGE_DIR);
	
  $created = file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  if (!$created) {
  	form_set_error('image_dir', t('Error creating directory'));
  }

  // Role orders
  foreach ($form_state['values']['roles'] as $rid => $role) {
  	
		$select = db_select('user_titles_roles', 'weight')
			->fields('weight')
			->condition('rid', $rid)
			->execute();
		
		if ($select) {
			if ($select->fetchAssoc() != $role['weight']) { //check if we even need to update
				db_update('user_titles_roles')
					->fields(array('weight' => $role['weight'],))
					->condition('rid', $rid)
					->execute();
			}
		}else{
			db_insert('user_titles_roles')
        ->fields(array(
          'rid' => $rid,
          'weight' => $role['weight'],
        )
      )
      ->execute();
		}
  }
  drupal_set_message(t('The configuration has been updated.'));
}

/**
 * Form that lists the user titles.
 *
 * @return
 *   A form array.
 */
function user_titles_titles_form() {
  $form = array();

  $form['roles']['#tree'] = TRUE;
  
  $query = db_select('role', 'r');
  $query->addField('r', 'name');
  $query->join('user_titles_roles', 'ro', 'r.rid = ro.rid');
  $query->fields('ro');
  $query->condition('r.rid', 1, '!=');
  $query->orderBy('ro.weight');
  $rows = $query->execute();
  while ($role = $rows->fetchAssoc()) {
    $titles = user_titles_get_titles_by_role($role['rid']);
    
    $form['roles'][$role['rid']]['name'] = array(
      '#type' => 'markup',
      '#markup' => t($role['name']),
    );
    $form['roles'][$role['rid']]['add_title'] = array(
      '#type' => 'item',
      '#markup' => l(t('Add another title'), 'admin/config/people/user-titles/add/title', array('query' => array('rid' => $role['rid']))),
    );

    if (!empty($titles)) {
      foreach ($titles as $i => $title) {
        $form['roles'][$role['rid']]['titles'][$i]['value'] = array(
          '#type' => 'markup',
          '#markup' => isset($title['value']) ? $title['value'] : '',
        );
        $form['roles'][$role['rid']]['titles'][$i]['title'] = array(
          '#type' => 'markup',
          '#markup' => isset($title['title']) ? $title['title'] : '',
        );
        $image = isset($title['image']) ? $title['image'] : '';
        $alt = isset($title['image_title']) ? $title['image_title'] : '';
        $form['roles'][$role['rid']]['titles'][$i]['image'] = array(
          '#type' => 'markup',
          '#markup' => theme('user_titles_image', array('filepath' => $image, 'alt' => $alt)),
        );
        $form['roles'][$role['rid']]['titles'][$i]['tid'] = array(
          '#type' => 'hidden',
          '#markup' => isset($title['tid']) ? $title['tid'] : '',
        );
      }
    }
  }

  return $form;
}

/**
 * Form for adding/editing user titles.
 *
 * @param $title
 *   A title object if editing.  NULL if adding a new title.
 *
 * @return
 *   A form array.
 */
function user_titles_add_title_form($form, &$form_state, $title = NULL) {
  $path = drupal_get_path('module', 'user_titles');
  drupal_add_css("$path/admin.css");

  // For file field to work.
  $form['#attributes'] = array('enctype' => 'multipart/form-data');

  $roles = user_titles_get_roles();
  $rid = is_numeric(isset($_GET['rid'])) ? $_GET['rid'] : 0;
  $default_role = (isset($title->rid)) ? $title->rid : $rid;
  $form['role'] = array(
    '#type' => 'select',
    '#title' => t('Role'),
    '#required' => TRUE,
    '#default_value' => $default_role,
    '#options' => $roles,
  );

  $units = module_invoke(user_titles_get_hook_module(), 'user_titles', 'units');
  $form['value'] = array(
    '#type' => 'textfield',
    '#title' => t($units),
    '#size' => 5,
    '#maxlength' => 10,
    '#required' => TRUE,
    '#default_value' => (isset($title->value)) ? $title->value : '',
  );

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#size' => 40,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => (isset($title->title)) ? $title->title : '',
  );

  $form['image'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image'),
  );

  $img_vars = array(
    'filepath' => (isset($title->image)) ? $title->image : '',
    'alt' => (isset($title->image_title)) ? $title->image_title : '',
  );
  $form['image']['image_display'] = array(
    '#type' => 'item',
    '#markup' => theme('user_titles_image', $img_vars),
  );

  if (isset($title->image)) {
    $form['image']['remove_image'] = array(
      '#type' => 'checkbox',
      '#title' => t('Remove image'),
      '#description' => t('If checked this image will be removed from this title on submit.'),
    );
  }

  $form['image']['image_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Image title/alt text'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => isset($title->image_title) ? $title->image_title : '',
    '#description' => t('This text will be used as the title and alt text for the image when it is displayed.'),
  );

  $form['image']['image_upload'] = array(
    '#type' => 'file',
    '#title' => t('Upload new image'),
    '#size' => 40,
    '#description' => t('Upload an image that can be displayed with this user title.  This image will replace any existing image attached to this title.'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  if (isset($title->tid)) {
  	if ($title->tid) {
      $form['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete'),
      );
      $form['tid'] = array(
        '#type' => 'value',
        '#value' => $title->tid,
      );
  	}
  }
	
  return $form;
}

/**
 * Validate function for the add title form.
 */
function user_titles_add_title_form_validate($form, &$form_state) {
	
  $existing_titles = user_titles_get_titles_by_role($form_state['values']['role']);

  if (!is_numeric($form_state['values']['value'])) {
    form_set_error('value', t('Number of posts must be a number!'));
  }

  foreach ($existing_titles as $title) {
    if ((!isset($form['values']['tid'])) || ($title['tid'] != $form_state['values']['tid'])) {
			if ($title['title'] == $form_state['values']['title']) {
       form_set_error('title', t('This title already exists for the selected role.  Titles must be unique.'));
      }
      if ($title['value'] == $form_state['values']['value']) {
        form_set_error('value', t('There is already a title for this number of posts for the selected role.  Posts must be unique.'));
      }
    }
  }
	
}

/**
 * Submit function for the add title form.
 */
function user_titles_add_title_form_submit($form, &$form_state) {
  // Do file processing if a file has been added.
  $image = '';

  // Get directory
  $dir = 'public://' . variable_get('user_titles_image_dir', DEFAULT_IMAGE_DIR);
  $created = file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
  if (!$created) {
  	form_set_error('image_upload', t('Directory error'));
  }
  
  // Save file
  $validators = array(
    'file_validate_is_image' => array(),
  );
  if ($file = file_save_upload('image_upload', $validators, $dir, TRUE)) {
  	$file->status = FILE_STATUS_PERMANENT;
  	file_save($file);
    $image = $file->uri;
    drupal_set_message(t('The file: <strong>"' . $file->filename . '"</strong> is successfully uploaded'));
  }
 // else if ($file === 0) {
  //  drupal_set_message(t('ERROR uploading the file to ' . $dir . '.  The user title has been created without an image.'), 'error');
 // }
 
  // Save title information
  if (isset($form_state['values']['tid'])) {
    // Only update image information if an image was uploaded so images don't get lost.
    if ($image != '') {
      db_update('user_titles')
        ->fields(
          array(
            'title' => $form_state['values']['title'],
            'value' => $form_state['values']['value'],
            'image' => $image,
            'image_title' => $form_state['values']['image_title'],
            'rid' => $form_state['values']['role'],
          )
        )
        ->condition('tid', $form_state['values']['tid'])
        ->execute();
    }
    elseif ($form_state['values']['remove_image']) {
      db_update('user_titles')
        ->fields(
          array(
            'title' => $form_state['values']['title'],
            'value' => $form_state['values']['value'],
            'image' => '',
            'image_title' => '',
            'rid' => $form_state['values']['role'],
          )
        )
        ->condition('tid', $form_state['values']['tid'])
        ->execute();
    }
    else {
      db_update('user_titles')
        ->fields(
          array(
            'title' => $form_state['values']['title'],
            'value' => $form_state['values']['value'],
            'image_title' => $form_state['values']['image_title'],
            'rid' => $form_state['values']['role'],
          )
        )
        ->condition('tid', $form_state['values']['tid'])
        ->execute();	
    }
    drupal_set_message(t('Updated user title %title.', array('%title' => $form_state['values']['title'])));
    $form_state['redirect'] = 'admin/config/people/user-titles/titles';
  }
  else {
  	db_insert('user_titles')
        ->fields(
          array(
            'title' => $form_state['values']['title'],
            'value' => $form_state['values']['value'],
            'image' => $image,
            'image_title' => $form_state['values']['image_title'],
            'rid' => $form_state['values']['role'],
          )
        )
        ->execute();
    drupal_set_message(t('Created new user title %title.', array('%title' => $form_state['values']['title'])));
    $form_state['redirect'] = 'admin/config/people/user-titles/add/title';
  }
  
  return;
}

/**
 * Page to edit a user title.
 * Redirects to either the add title form or the delete title form.
 *
 * @param $title
 *   A title object to edit or delete.
 */
function user_titles_edit_title_form($title) {
  if (isset($_POST)) {
    if (isset($_POST['op']) && $_POST['op'] == t('Delete') || (isset($_POST['confirm']) && $_POST['confirm'])) {
      return drupal_get_form('user_titles_delete_title_form', $title);
    }
  }
  if ($title) {
    return drupal_get_form('user_titles_add_title_form', $title);
  }
  return drupal_not_found();
}

/**
 * Confirm deletion of user title form.
 *
 * @param $title
 *   A title object to delete.
 */
function user_titles_delete_title_form($form, &$form_state, $title) {
  $form = array();
  $form['tid'] = array(
    '#type' => 'hidden',
    '#value' => $title->tid,
  );

  $form['title'] = array(
    '#type' => 'hidden',
    '#value' => $title->title,
  );

  $confirm_form = confirm_form($form,
    t('Are you sure you want to delete the user title: %title?', array('%title' => $title->title)),
    'admin/config/people/user-titles/titles',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel'));

  return $confirm_form;
}

/**
 * Submit function for the delete user titles form.
 */
function user_titles_delete_title_form_submit($form, &$form_state) {
  if ($form_state['values']['tid'] && is_numeric($form_state['values']['tid'])) {
  	db_delete('user_titles')
  	  ->condition('tid', $form_state['values']['tid'])
  	  ->execute();
  }

  drupal_set_message("User title '" . $form_state['values']['title'] . "' has been deleted successfully.", 'status');

  $form_state['redirect'] = 'admin/config/people/user-titles/titles';
  return;
}

/**
 * Theme function for the user title settings form.
 *
 * @param $variables
 *   $variables['form'] - The form array of the user titles settings form.
 * 
 * @return
 *   A string containing the html output for the user titles settings form.
 *
 * @ingroup themeable
 */
function theme_user_titles_settings_form($variables) {
  $form = $variables['form'];
  $output = '';
  // Properly format hook_module
  foreach ($form['settings']['hook_module']['#options'] as $module => $info) {
    if ($info['url']) {
      $name = l($info['name'], $info['url']);
    }
    else {
      $name = check_plain($info['name']);
    }
    // drupal_render uses this area, not the original
    $form['settings']['hook_module'][$module]['#title'] = '<strong>' . $name . '</strong><div class="description">' . $info['description'] . '</div>';
  }

  $output .= '<div class="settings">';
  $output .= drupal_render($form['settings']);
  $output .= '</div>';

  $rows = array();

  foreach (element_children($form['roles']) as $key) {
    if (isset($form['roles'][$key]['name'])) {
      $role = &$form['roles'][$key];
      $row = array();
      $row[] = drupal_render($role['name']);
      if (isset($role['weight'])) {
        $role['weight']['#attributes']['class'] = array('role-weight');
        $row[] = drupal_render($role['weight']);
      }
      $rows[] = array('data' => $row, 'class' => array('draggable'));
    }
  }
  if (empty($rows)) {
    $rows[] = array(array('data' => t('No roles available.')));
  }

  $header = array(t('Role'), t('Weight'));
  drupal_add_tabledrag('roles', 'order', 'sibling', 'role-weight');

  $output .= '<fieldset class="roles-table">';
  $output .= '<legend>' . t('Role priority') . '</legend>';
  $output .= drupal_render($form['roles-description']);
  $output .= theme('table', array(
    'header' => $header, 
    'rows' => $rows, 
    'attributes' => array('id' => 'roles'),
  ));
  $output .= '</fieldset>';

  $output .= '<div class="clearer">&nbsp;</div>';
  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Theme function for the user title settings form.
 *
 * @param $variables
 *   $variables['form'] - The form array of the user titles titles form.
 * 
 * @return
 *   A string containing the html output for the user titles titles form.
 *
 * @ingroup themeable
 */
function theme_user_titles_titles_form($variables) {
  $form = $variables['form'];
  drupal_add_js('misc/collapse.js');
  $output = '';
  $units = module_invoke(user_titles_get_hook_module(), 'user_titles', 'units');
  $header = array(
    array('data' => $units, 'class' => array('num-posts')),
    array('data' => t('Title'), 'class' => array('user-title')),
    array('data' => t('Image'), 'class' => array('user-title-image')),
    array('data' => t('Operations'), 'colspan' => 2),
  );

  foreach (element_children($form['roles']) as $rid) {
    $role = &$form['roles'][$rid];
    $rows = array();
    if (isset($role['titles'])) {
      foreach (element_children($form['roles'][$rid]['titles']) as $key) {
        // set a reference so that the drupal_render gets remembered.
        unset($elem);
        $elem = &$form['roles'][$rid]['titles'][$key];

        $rows[] = array(
          array('data' => drupal_render($elem['value']), 'class' => array('num-posts')),
          array('data' => drupal_render($elem['title']), 'class' => array('user-title')),
          array('data' => drupal_render($elem['image']), 'class' => array('user-title-image')),
          'edit' => l(t('edit'), "admin/config/people/user-titles/edit/title/" . $elem['tid']['#markup']),
          'delete' => l(t('delete'), "admin/config/people/user-titles/delete/title/" . $elem['tid']['#markup']),
        );
      }
    }
    if (empty($rows)) {
      $rows[] = array(array('data' => t('No titles set.'), 'colspan' => '5'));
    }

    $output .= '<fieldset class="collapsible collapsed title-table ' . $role['name']['#markup'] . '">';
    $output .= '<legend>' . drupal_render($role['name']) . '</legend>';
    $output .= theme('table', array('header' => $header, 'rows' => $rows));
    $output .= drupal_render($role['add_title']);
    $output .= '</fieldset>';
  }

  $output .= '<div class="clearer">&nbsp;</div>';
  $output .= drupal_render_children($form);
  return $output;
}
