<?php

/**
 * @file
 * Opigno messaging app module functions file.
 */

function opigno_messaging_app_permission() {
  return array(
    'message all groups' => array(
      'title' => t("Message to all groups"),
      'description' => t("Allows messaging all the groups"),
    ),
  );
}

function opigno_messaging_app_form_alter(&$form, &$form_state, $form_id) {
  if (($form['form_id']['#value'] == 'privatemsg_new') && (strpos($form['#action'], '/messages/new') !== FALSE)) {
    $form['#attached']['js'][] = drupal_add_js(drupal_get_path('module', 'opigno_messaging_app') . '/js/multiselect_messaging.js');
    global $user;
    if (user_access("message all groups", $user)) {
      $groups = og_get_all_group('node');
      // Force og_get_all_group() structure to be the same as og_get_groups_by_user()
      foreach ($groups as $group_index => $groupid) {
        $group1['node'][$groupid] = $groupid;
      }
      $groups = $group1;
      $group1 = NULL;
      /////////////////////////////////////////////////////
    }
    else {
      $groups = og_get_groups_by_user($user, NULL);
    }

    $form['groups'] = array(
      '#type' => 'select',
      '#title' => t('Courses and Classes'),
      '#options' => array(
        'All' => t('All'),
      ),
      '#default_value' => t('All'),
      '#description' => t('Choose class or course to filter'),
      '#weight' => -7,
      '#ajax' => array(
        'callback' => 'messaging_user_update_callback',
        'wrapper' => 'recipient1-div',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );

    $cache = array();
    foreach ($groups['node'] as $groupid => $group_id) {
      $node = node_load($group_id);
      if (!isset($cache[$node->type])) {
        $typeinfo = node_type_load($node->type);
        $cache[$node->type] = $typeinfo->name; //check if object
      }
      $form['groups']['#options'][$cache[$node->type]][$groupid] = $node->title;
    }

    unset($form['recipient']['#required']);
    if (!isset($form['recipient1'])) {
      $form['recipient1'] = array(
        '#type' => 'multiselect',
        '#title' => t('Select receipients'),
        '#default_value' => NULL,
        '#size' => 10,
        '#required' => TRUE,
        '#weight' => -6,
        '#prefix' => '<div id="recipient1-div">',
        '#suffix' => '</div>',
        '#validated' => TRUE,
      );
    }

    if (user_access("message all groups", $user)) {
      $users = entity_load('user');
      foreach ($users as $user_) {
        if (($user_->uid != 0) && ($user_->uid != $user->uid)) {
          $form['recipient1']['#options'][$user_->name] = $user_->name;
        }
      }
    }
    else {
      $groups = og_get_groups_by_user($user, NULL);
      if (is_array($groups)) {
        foreach ($groups['node'] as $groupid => $group_id) {
          $users = entity_load('user');
          foreach ($users as $user_) {
            if (($user_->uid != 0) && ($user_->uid != $user->uid)) {
              if (og_is_member('node', $group_id, 'user', $user_)) {
                $nd = node_load($group_id);
                $form['recipient1']['#options'][$user_->name] = $user_->name;
              }
            }
          }
        }
      }
    }

    $form['body']['#required'] = TRUE;
    $form['subject']['#required'] = TRUE;
  }
}

function messaging_user_update_callback($form, &$form_state) {
  $form['recipient1']['#options'] = NULL;
  $group = $form_state['input']['groups'];
  global $user;
  $users = entity_load('user');
  foreach ($users as $user_) {
    if (($user_->uid != 0) && ($user_->uid != $user->uid)) {
      if (og_is_member('node', $group, 'user', $user_)) {
        $nd = node_load($group);
        $form['recipient1']['#options'][$user_->name] = $user_->name;
      }
    }
  }
  if ($group === t('All')) {
    if (user_access("message all groups", $user)) {
      $users = entity_load('user');
      foreach ($users as $user_) {
        if (($user_->uid != 0) && ($user_->uid != $user->uid)) {
          $form['recipient1']['#options'][$user_->name] = $user_->name;
        }
      }
    }
    else {
      $groups = og_get_groups_by_user($user, NULL);
      if (is_array($groups)) {
        foreach ($groups['node'] as $groupid => $group_id) {
          $users = entity_load('user');
          foreach ($users as $user_) {
            if (($user_->uid != 0) && ($user_->uid != $user->uid)) {
              if (og_is_member('node', $groupid, 'user', $user_)) {
                $nd = node_load($groupid);
                $form['recipient1']['#options'][$user_->name] = $user_->name;
              }
            }
          }
        }
      }
    }
  }
  return $form['recipient1'];
}