You are here

function user_relationship_implications_user_relationships in User Relationships 5

Same name and namespace in other branches
  1. 5.2 plugins/user_relationship_implications/user_relationship_implications.module \user_relationship_implications_user_relationships()
  2. 6 user_relationship_implications/user_relationship_implications.module \user_relationship_implications_user_relationships()

hook_user_relationships()

File

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

Code

function user_relationship_implications_user_relationships($type, &$relationship, $category = NULL) {
  switch ($type) {
    case 'load type':
      if ($relationship->rtid) {
        static $loaded_relationship_implications = array();
        if (!is_array($loaded_relationship_implications[$relationship->rtid])) {
          $loaded_relationship_implications[$relationship->rtid] = array();
          $results = db_query("SELECT * FROM {user_relationship_implications} WHERE rtid = %d OR implies_rtid = %d", $relationship->rtid, $relationship->rtid);
          while ($implication = db_fetch_object($results)) {
            $loaded_relationship_implications[$relationship->rtid][] = $implication;
          }
        }
        $relationship->implies = array();
        $relationship->implied_by = array();
        foreach ($loaded_relationship_implications[$relationship->rtid] as $implication) {
          if ($implication->rtid == $relationship->rtid) {
            $relationship->implies[] = $implication;
          }
          else {
            $relationship->implied_by[] = $implication;
          }
        }
      }
      break;
    case 'delete type':
      if ($relationship->rtid) {
        $results = db_query("DELETE FROM {user_relationship_implications} WHERE rtid = %d OR implies_rtid = %d", $relationship->rtid, $relationship->rtid);

        // clean out the implications cache
        static $loaded_relationship_implications;
        unset($loaded_relationship_implications);
      }
      break;
    case 'insert':
    case 'update':
      if ($relationship->type->implies) {

        // if the type of the relationship we're inserting or updating implies other relationship type(s),
        // loop through the implied relationship types and do the right thing
        foreach ($relationship->type->implies as $implied) {

          // load all the pre-existing relationships between these two users of the implied relationship type
          $relationships = user_relationships_load_relationships(array(
            'uid1' => $relationship->requester_id,
            'uid2' => $relationship->requestee_id,
            'rtid' => $implied->implies_rtid,
          ));

          // if there aren't any, create one with the same approved status as the relationship we're inserting/updateing
          if (sizeof($relationships) == 0) {
            $implied = user_relationships_relationship_type_load(array(
              'rtid' => $implied->implies_rtid,
            ));
            user_relationships_request_relationship($relationship->requester, $relationship->requestee, $implied, $relationship->approved);
          }
          else {
            foreach ($relationships as $existing) {
              if ($relationship->approved && !$existing->approved) {

                // approve the relationship
                $approved = user_relationships_relationship_load($existing->rid);
                $approved->approved = TRUE;
                user_relationships_update_relationship($existing, $approved);

                // set the message informing the user (use requester and requestee from original relationship)
                $requester = user_load(array(
                  'uid' => $relationship->requester_id,
                ));
                $requestee = user_load(array(
                  'uid' => $relationship->requestee_id,
                ));
                drupal_set_message(user_relationships_get_message('accepted', array(
                  '!requester' => theme('username', $requester),
                  '!requestee' => theme('username', $requestee),
                  '%relationship_name' => $approved->name,
                  '%relationship_plural_name' => $approved->plural_name,
                )));
              }
            }
          }
        }
      }
      break;
    case 'delete':
      global $user;
      if ($relationship->type->implied_by) {

        // if the type of the relationship we're deleting is implied by other relationship type(s),
        // loop through the implying relationship types and do the right thing
        foreach ($relationship->type->implied_by as $implied_by) {

          // load all the pre-existing relationships between these two users of the implying relationship type
          $relationships = user_relationships_load_relationships(array(
            'uid1' => $relationship->requester_id,
            'uid2' => $relationship->requestee_id,
            'rtid' => $implied_by->rtid,
          ));

          // if they're not oneway relationships, or if they are and the requester is the one doing the deleting,
          // then delete the implying relationships
          foreach ($relationships as $existing) {
            $rtype = user_relationships_relationship_type_load(array(
              'rtid' => $existing->rtid,
            ));
            if (!$rtype->is_oneway || $relationship->requester_id == $user->uid) {

              // delete the relationship
              $relationship_to_delete = user_relationships_relationship_load($existing->rid);
              user_relationships_delete_relationship($relationship_to_delete, $relationship->deleted_by, $category);

              // set the message informing the user (use requester and requestee from original relationship)
              $requester = user_load(array(
                'uid' => $relationship->requester_id,
              ));
              $requestee = user_load(array(
                'uid' => $relationship->requestee_id,
              ));
              drupal_set_message(user_relationships_get_message('removed', array(
                '!requester' => theme('username', $requester),
                '!requestee' => theme('username', $requestee),
                '%relationship_name' => $relationship_to_delete->name,
                '%relationship_plural_name' => $relationship_to_delete->plural_name,
              )));
            }
          }
        }
      }
      break;
  }
}