diff --git a/modules/redhen_activity/redhen_activity.module b/modules/redhen_activity/redhen_activity.module index 4103582..2a5d398 100644 --- a/modules/redhen_activity/redhen_activity.module +++ b/modules/redhen_activity/redhen_activity.module @@ -436,13 +436,13 @@ function redhen_activity_message_save($entity, $type, $op) { // During delete operations the endpoint entity may be gone, so check // before creating a message: if ($endpoint_entity) { - if (in_array($endpoint_entity->entityType(), array('redhen_contact', 'redhen_org'))) { - $label1 = $endpoint_entities[0] ? $endpoint_entities[0]->label() : + if (method_exists($endpoint_entity, 'entityType') && in_array($endpoint_entity->entityType(), array('redhen_contact', 'redhen_org'))) { + $label1 = $endpoint_entities[0] ? entity_label($endpoints[0]['entity_type'], $endpoint_entities[0]) : t('Entity !type with an ID of !id', array( '!type' => $endpoints[0]['entity_type'], '!id' => $endpoints[0]['entity_id'], )); - $label2 = $endpoint_entities[1] ? $endpoint_entities[1]->label() : + $label2 = $endpoint_entities[1] ? entity_label($endpoints[1]['entity_type'], $endpoint_entities[1]) : t('Entity !type with an ID of !id', array( '!type' => $endpoints[1]['entity_type'], '!id' => $endpoints[1]['entity_id'], diff --git a/modules/redhen_contact/redhen_contact.module b/modules/redhen_contact/redhen_contact.module index 572f78a..ab5147c 100644 --- a/modules/redhen_contact/redhen_contact.module +++ b/modules/redhen_contact/redhen_contact.module @@ -853,7 +853,7 @@ function redhen_contact_query_redhen_contact_label_alter(QueryAlterableInterface foreach ($where as $id => $condition) { // If we're trying to search for redhen_contact.label, alter the query to // search in both first and last name fields. - if ($condition['field'] == 'redhen_contact.label') { + if (is_string($condition['field']) && $condition['field'] == 'redhen_contact.label') { unset($where[$id]); $or = db_or() ->condition('redhen_contact.first_name', $condition['value'], $condition['operator']) diff --git a/modules/redhen_registration/redhen_registration.module b/modules/redhen_registration/redhen_registration.module index 2c0ae95..eedf614 100644 --- a/modules/redhen_registration/redhen_registration.module +++ b/modules/redhen_registration/redhen_registration.module @@ -39,7 +39,10 @@ function redhen_registration_page(RedhenContact $contact) { $query->propertyCondition('user_uid', $user->uid, '='); } else { - $query->propertyCondition('anon_mail', $contact->allEmail(), 'IN'); + $all_email = $contact->allEmail(); + if (!empty($all_email)) { + $query->propertyCondition('anon_mail', $contact->allEmail(), 'IN'); + } } $result = $query->execute(); diff --git a/modules/redhen_relation/redhen_relation.module b/modules/redhen_relation/redhen_relation.module index 8ce782c..04cb0ee 100644 --- a/modules/redhen_relation/redhen_relation.module +++ b/modules/redhen_relation/redhen_relation.module @@ -158,6 +158,8 @@ function redhen_relation_access($op, $entity) { */ function redhen_relation_connections_page($entity) { $related = redhen_relation_relations($entity); + $related_types = redhen_relation_relation_types($entity); + if (!empty($related)) { $header = array( @@ -181,17 +183,22 @@ function redhen_relation_connections_page($entity) { $relation_uri = entity_uri('relation', $relation); $reversed = FALSE; + $endpoint_types = array(); $endpoints = field_get_items('relation', $relation, 'endpoints'); foreach ($endpoints as $endpoint) { if ($endpoint['entity_type'] == $entity->entityType() && $endpoint['r_index']) { $reversed = TRUE; break; } + elseif ($endpoint['entity_type'] != $entity->entityType()) { + $endpoint_types[] = $endpoint['entity_type']; + } } + $i = 0; foreach ($related_entities as $related_entity) { - $object_label = entity_label($related_entity->entityType(), $related_entity); - $object_uri = entity_uri($related_entity->entityType(), $related_entity); + $object_label = entity_label($related_types[$relation_id][$i], $related_entity); + $object_uri = entity_uri($related_types[$relation_id][$i], $related_entity); $author = user_load($relation->uid); if (strpos($relation->relation_type, 'redhen_') === FALSE) { $active = t('NA', array(), array('context' => 'redhen_relation')); @@ -200,7 +207,7 @@ function redhen_relation_connections_page($entity) { $items = field_get_items('relation', $relation, REDHEN_RELATION_STATUS_FIELD); $active = ($items[0]['value']) ? t('Active', array(), array('context' => 'redhen_relation')) : t('Inactive', array(), array('context' => 'redhen_relation')); } - $related_entity_info = entity_get_info($related_entity->entityType()); + $related_entity_info = entity_get_info($related_types[$relation_id][$i]); $data = array( 'connection' => array( @@ -225,7 +232,7 @@ function redhen_relation_connections_page($entity) { $row_classes = array(); // Add primary contact handling for redhen_orgs and contacts. - if ($entity->entityType() == 'redhen_org' && $related_entity->entityType() == 'redhen_contact') { + if ($entity->entityType() == 'redhen_org' && $related_types[$relation_id][$i] == 'redhen_contact') { if ($entity->primary_contact_id !== $related_entity->contact_id) { $entity_uri = entity_uri('redhen_org', $entity); $data['primary'] = array( @@ -297,6 +304,7 @@ function redhen_relation_connections_page($entity) { } $rows[$relation_id] = array('data' => $data, 'class' => $row_classes); + ++$i; } } @@ -381,6 +389,74 @@ function redhen_relation_relations($entity, $relation_type = NULL, $active = FAL } /** + * Return entity related types to a given entity excluding self and duplicates. + * + * @param object $entity + * @param string $relation_type + * Filter on a given relation type if provided. + * @param bool $active + * Optionally limit relations to those that are active. + * + * @return array + * Keyed by the relation id which contains an array of entities. + * Array ( + * 1 => array($entity1, $entity2), + * 3 => array($entity3, $entity4) + * ) + */ +function redhen_relation_relation_types($entity, $relation_type = NULL, $active = FALSE) { + $related_entity_types = &drupal_static(__FUNCTION__ . $entity->entityType() . $entity->internalIdentifier() . $relation_type . $active, array()); + + if ($related_entity_types) { + return $related_entity_types; + } + + $query = relation_query($entity->entityType(), $entity->internalIdentifier()); + if ($relation_type) { + $query->propertyCondition('relation_type', $relation_type); + } + + // Optionally limit to only active relations. + if ($active) { + $query->fieldCondition(REDHEN_RELATION_STATUS_FIELD, 'value', TRUE); + } + + // this hideous hack is to avoid triggering the node access system which we + // need to to in order to avoid infinite recursion in + // redhen_org_group:node_grants(). + // time came from http://drupal.stackexchange.com/questions/3927/how-to-bypass-node-access-when-using-entityfieldquery + // ticket pointing to issue http://drupal.org/node/1541236. + $query->addMetaData('account', user_load(1)); + + $query->addTag('redhen_relation'); + $results = $query->execute(); + + if ($results) { + $relations = relation_load_multiple(array_keys($results)); + foreach ($relations as $relation) { + $rid = entity_id('relation', $relation); + $related_entity_types[$rid] = array(); + // To make sure duplicates of $entity get included in object list. + $duplicate = FALSE; + + $endpoints = field_get_items('relation', $relation, 'endpoints'); + foreach ($endpoints as $endpoint) { + // Add all entities that aren't this entity or duplicates, unless the + // relation only has this entity as endpoint ($relation->arity = 1). + if ($relation->arity > 1 && $endpoint['entity_type'] == $entity->entityType() && $endpoint['entity_id'] == $entity->internalIdentifier() && $duplicate == FALSE) { + $duplicate = TRUE; + } + else { + $related_entity_types[$rid][] = $endpoint['entity_type']; + } + } + } + } + + return $related_entity_types; +} + +/** * Return a list of entities the given entity can be associated with. * * @param $entity