You are here

function user_relationship_implications_form_alter in User Relationships 5.2

Same name and namespace in other branches
  1. 5 plugins/user_relationship_implications/user_relationship_implications.module \user_relationship_implications_form_alter()

hook_form_alter()

File

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

Code

function user_relationship_implications_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'user_relationships_type_edit':
      $rtid = $form['rtid']['#value'];
      $relationship_type = user_relationships_type_load($rtid);
      $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;
          }
        }
      }
      $form['implications'] = 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>
      '),
      );
      $list = FALSE;
      foreach ($relationship_types as $type) {
        if ($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),
            '#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' => $type->rtid,
            '#default_value' => isset($form['#post'][$strict_name]) || $values[$type->rtid]['strict'],
          );
          $opp_name = "implied_{$type->rtid}_reverse";
          $form['implications']['opts'][$type->rtid][$opp_name] = array(
            '#type' => 'checkbox',
            '#return_value' => $type->rtid,
            '#default_value' => isset($form['#post'][$opp_name]) || $values[$type->rtid]['reverse'],
          );
          $list = TRUE;
        }
      }
      if ($list) {
        $form['#submit']['user_relationship_implications_edit_submit'] = array();
      }
      else {
        unset($form['implications']);
      }
      break;
  }
}