You are here

function user_relationship_implications_form_user_relationships_ui_type_edit_alter in User Relationships 6

hook_form_alter()

File

user_relationship_implications/user_relationship_implications.module, line 31
Drupal Module: User Relationship Implications

Code

function user_relationship_implications_form_user_relationships_ui_type_edit_alter(&$form, &$form_state) {
  $relationship_type = isset($form['rtid']['#value']) ? user_relationships_type_load($form['rtid']['#value']) : NULL;
  $relationship_types = user_relationships_types_load();
  $implied_by = array();
  if ($relationship_type) {
    foreach ($relationship_type->implies as $rtid => $implies) {
      $values[$rtid]['strict'] = $implies->strict;
      $values[$rtid]['reverse'] = $implies->reverse;
    }
    foreach ($relationship_type->implied_by as $implied) {
      if (!$implied->reverse) {
        $implied_by[] = $implied->rtid;
      }
    }
  }
  foreach ($relationship_types as $type) {
    if (!$relationship_type || $type->rtid != $relationship_type->rtid && !in_array($type->rtid, $implied_by)) {
      $imp_name = "implies_{$type->rtid}";
      $form['implications']['opts'][$type->rtid][$imp_name] = array(
        '#title' => t('@type_name', array(
          '@type_name' => $type->name,
        )),
        '#type' => 'checkbox',
        '#return_value' => $type->rtid,
        '#default_value' => isset($form['#post'][$imp_name]) || isset($values[$type->rtid]),
      );
      $strict_name = "implied_{$type->rtid}_strict";
      $form['implications']['opts'][$type->rtid][$strict_name] = array(
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => isset($form['#post'][$strict_name]) || isset($values[$type->rtid]['strict']) ? $values[$type->rtid]['strict'] : FALSE,
      );
      $opp_name = "implied_{$type->rtid}_reverse";
      $form['implications']['opts'][$type->rtid][$opp_name] = array(
        '#type' => 'checkbox',
        '#return_value' => 1,
        '#default_value' => isset($form['#post'][$opp_name]) || isset($values[$type->rtid]['reverse']) ? $values[$type->rtid]['reverse'] : FALSE,
      );
    }
  }
  if ($form['implications']) {
    $implications_form = array(
      '#title' => t('This relationship implies'),
      '#type' => 'fieldset',
      '#weight' => 0,
      '#tree' => TRUE,
      '#theme' => 'user_relationship_implications_form_table',
      '#description' => t('
        <ul>
          <li>Users will automatically have these relationships created between them when the implying relationship is created. (ex: Manager implies Coworker).</li>
          <li>When the implied relationship is removed the implying relationship will not be removed. (ex: removing Coworker WILL NOT remove Manager)</li>
          <li>If "strict" is set the implying relationship will be removed when the implied relationship is removed. (ex: removing Coworker WILL remove Manager)</li>
          <li>Reverse is really only useful for one-way relationships. It allows things like Parent implies Offspring to work in the right direction</li>
        </ul>
      '),
    );
    $form['implications'] = array_merge($form['implications'], $implications_form);
    $form['#submit'][] = 'user_relationship_implications_edit_submit';
  }
}