OgCommonsSelectionHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 3 1
B buildEntityFieldQuery() 0 28 4
C entityFieldQueryAlter() 0 32 8
1
<?php
2
3
/**
4
 * @file
5
 * OG Commons groups selection handler.
6
 */
7
8
class OgCommonsSelectionHandler extends OgSelectionHandler {
9
10
  public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) {
11
    return new self($field, $instance, $entity_type, $entity);
12
  }
13
14
  /**
15
   * Overrides OgSelectionHandler::buildEntityFieldQuery().
16
   */
17
  public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') {
18
    $group_type = $this->field['settings']['target_type'];
19
20
    if (empty($this->instance['field_mode']) || $group_type != 'node' || user_is_anonymous()) {
21
      return parent::buildEntityFieldQuery($match, $match_operator);
22
    }
23
24
    $handler = EntityReference_SelectionHandler_Generic::getInstance($this->field, $this->instance, $this->entity_type, $this->entity);
25
    $query = $handler->buildEntityFieldQuery($match, $match_operator);
26
27
    // Show only the entities that are active groups.
28
    $query->fieldCondition(OG_GROUP_FIELD, 'value', 1);
29
    $query->fieldCondition('field_og_subscribe_settings', 'value', 'anyone');
30
31
    // Add this property to make sure we will have the {node} table later on in
32
    // OgCommonsSelectionHandler::entityFieldQueryAlter().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
    $query->propertyCondition('nid', 0, '>');
34
35
    $query->addMetaData('entityreference_selection_handler', $this);
36
37
    // FIXME: http://drupal.org/node/1325628
38
    unset($query->tags['node_access']);
39
40
    $query->addTag('entity_field_access');
41
    $query->addTag('og');
42
43
    return $query;
44
  }
45
46
  /**
47
   * Overrides OgSelectionHandler::entityFieldQueryAlter().
48
   *
49
   * Add the user's groups along with the rest of the "public" groups.
50
   */
51
  public function entityFieldQueryAlter(SelectQueryInterface $query) {
52
    $gids = og_get_entity_groups();
53
    if (empty($gids['node'])) {
54
      return;
55
    }
56
57
    $conditions = &$query->conditions();
58
    // Find the condition for the "field_data_field_privacy_settings" query, and
59
    // the one for the "node.nid", so we can later db_or() them.
60
    $public_condition = array();
61
    foreach ($conditions as $key => $condition) {
62
      if ($key !== '#conjunction' && is_string($condition['field'])) {
63
        if (strpos($condition['field'], 'field_data_field_og_subscribe_settings') === 0) {
64
          $public_condition = $condition;
65
          unset($conditions[$key]);
66
        }
67
68
        if ($condition['field'] === 'node.nid') {
69
          unset($conditions[$key]);
70
        }
71
      }
72
    }
73
74
    if (!$public_condition) {
75
      return;
76
    }
77
78
    $or = db_or();
79
    $or->condition($public_condition['field'], $public_condition['value'], $public_condition['operator']);
80
    $or->condition('node.nid', $gids['node'], 'IN');
81
    $query->condition($or);
82
  }
83
}
84