You are here

contextual_view_modes_users.module in Contextual View Modes 7.3

File

modules/contextual_view_modes_users/contextual_view_modes_users.module
View source
<?php

/**
 * Implementation of hook_perm().
 */
function contextual_view_modes_users_permission() {
  $perms = array(
    'set view modes per user' => array(
      'title' => t('Contextual View Modes Per User'),
      'description' => t('Allow changing/setting contextual view mode per user'),
    ),
  );
  return $perms;
}

/**
 * Implements hook_form_alter().
 *
 * Adds options for view mode switching to the user edit form.
 *
 */
function contextual_view_modes_users_form_user_profile_form_alter(&$form, &$form_state) {
  $user = isset($form['#user']) ? $form['#user'] : NULL;
  list($uid) = entity_extract_ids('user', $user);

  // Add the view modes vertical tab.
  $form['contextual_view_modes'] = array(
    '#type' => 'fieldset',
    '#title' => t('Contextual View Modes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#access' => user_access('set view modes per user'),
    '#weight' => 50,
  );

  // Gather information.
  $context_options = contextual_view_modes_get_context_options();
  $view_modes_options = contextual_view_modes_get_view_mode_options('user');

  // If there are no contexts available than let the user know.
  $count_context = count($context_options);
  if ($count_context <= 1) {
    $vm['nothing']['#markup'] = "<p>" . t("Please %context_link before using contextual view modes.", array(
      "%context_link" => l('create a context', 'admin/structure/context/add'),
    )) . "</p>";
    return;
  }

  // If we have contexts explain how to use them.
  $form['contextual_view_modes']['help']['#markup'] = "<p><b>" . t("Instructions") . ":</b><p>" . t("This module uses the context module in order to trigger the view mode change. First, create a context with the conditions you wish to trigger the view mode change with. For example, if I wanted to change the view mode of this user by wether or not the user was logged in I would create a context with the condition of user role of authenticated. There is no need to add anything to the reactions portion of the context configuration. From the options below you can then select the name of that context and the view mode you wish to display to authenticated users. You can create additional view modes using the display suite module.") . "</p>";

  // Wrapper for ajax return easyness.
  $form['contextual_view_modes']['cvm'] = array(
    '#type' => 'container',
    '#id' => "edit-cvm-wrapper",
    '#tree' => TRUE,
  );

  // Convenience variable.
  $vm =& $form['contextual_view_modes']['cvm'];
  $settings = variable_get("contextual_view_modes_user", array());

  // Keep count of how many options there is set for the add another functions.
  if (empty($form_state['num_contexts'])) {
    $form_state['num_contexts'] = isset($settings[$uid]) ? count($settings[$uid]) : 0;
  }

  // Loop through and create form groups.
  for ($delta = 0; $delta <= $form_state['num_contexts']; $delta++) {

    // Grouping like settings for easy saving.
    $vm['grouping'][$delta] = array(
      '#type' => 'fieldset',
      '#title' => t('Condition #') . ($delta + 1),
      '#collapsed' => FALSE,
      '#collapsible' => TRUE,
    );
    $vm['grouping'][$delta]['context_name'] = array(
      '#type' => 'select',
      '#options' => $context_options,
      '#title' => t('Context'),
      '#description' => t("When this context is active and the condition are met display the view mode below."),
      '#default_value' => isset($settings[$uid][$delta]['context_name']) ? $settings[$uid][$delta]['context_name'] : "",
    );
    $vm['grouping'][$delta]['view_mode'] = array(
      '#type' => 'select',
      '#options' => $view_modes_options,
      '#title' => t('View mode'),
      '#description' => t("Display this view mode when the above context is active."),
      '#default_value' => isset($settings[$uid][$delta]['view_mode']) ? $settings[$uid][$delta]['view_mode'] : "",
    );
  }
  $vm['add_nuther'] = array(
    '#type' => 'submit',
    '#value' => t('Add another'),
    '#name' => 'add-nuther' . $delta,
    '#submit' => array(
      'contextual_view_modes_user_add_nuther',
    ),
    '#ajax' => array(
      'callback' => 'contextual_view_modes_user_add_nuther_callback',
      'wrapper' => 'edit-cvm-wrapper',
    ),
  );

  // Add new submit handlers to this form before the user is saved.
  array_unshift($form["#submit"], "contextual_view_modes_user_form_user_form_submit");
}

/**
 * Renders the part of the form for the ajax callback.
 *
 * @param array $form
 *   The form array
 * @param  array $form_state
 *   The form state array.
 *
 * @return array
 *   The contextual view mode vertical tab portion of the form.
 */
function contextual_view_modes_user_add_nuther_callback($form, $form_state) {
  return $form['contextual_view_modes']['cvm'];
}

/**
 * Submit handler for the "add-one-more" button.
 *
 * Increments the max counter and causes a rebuild.
 * @param array $form
 *   The form array
 * @param  array $form_state
 *   The form state array.
 */
function contextual_view_modes_user_add_nuther($form, &$form_state) {
  $form_state['num_contexts']++;
  $form_state['rebuild'] = TRUE;
}

/**
 * Validate and remove bad merge groupings then save.
 *
 * @param array $form
 *   The form array
 * @param  array $form_state
 *   The form state array.
 */
function contextual_view_modes_user_form_user_form_submit($form, &$form_state) {

  // Form submit values.
  $values = $form_state['values'];

  // Remove empty options.
  foreach ($values['cvm']['grouping'] as $delta => $settings) {
    if (empty($settings['context_name']) || $settings['context_name'] == "none") {
      unset($form_state['values']['cvm']['grouping'][$delta]);
      unset($form_state['input']['cvm']['grouping'][$delta]);
      continue;
    }
    if (empty($settings['view_mode']) || $settings['view_mode'] == "none") {
      unset($form_state['values']['cvm']['grouping'][$delta]);
      unset($form_state['input']['cvm']['grouping'][$delta]);
    }
  }

  // Re-key the values to avoid any gaps.
  sort($form_state['values']['cvm']['grouping']);
  $form_state['user']->cvm = $form_state['values']['cvm']['grouping'];
}

/**
 * Implements hook_user_insert().
 *
 * Pass through to update.
 *
 * @param  Object $user
 *   The user object about to be saved.
 */
function contextual_view_modes_users_user_insert($user) {
  contextual_view_modes_users_user_update($user);
}

/**
 * Implements hook_user_update().
 *
 * Merge in the settings for the user on save.
 *
 * @param  Object $user
 *   The user object about to be saved.
 *
 */
function contextual_view_modes_users_user_update($user) {
  $uid = $user['original']->uid;
  $settings = variable_get("contextual_view_modes_user", array());

  // If cvm is not around just go away.
  if (!isset($user->cvm)) {
    return;
  }
  $settings[$uid] = $user['cvm']['grouping'];
  variable_set("contextual_view_modes_user", $settings);
}

/**
 * Implements hook_user_delete().
 *
 * Delete the contextual view mode settings for the node.
 *
 */
function contextual_view_modes_users_user_delete($account) {
  $id = $account->uid;
  $settings = variable_get("contextual_view_modes_user", array());
  if (isset($settings[$id])) {
    unset($settings[$id]);
    variable_set("contextual_view_modes_user", $settings);
  }
}