Elgg  Version master
AnnotationWhereClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
7 
12 
16  public $ids;
17 
21  public $entity_guids;
22 
26  public $owner_guids;
27 
31  public $enabled;
32 
36  public $access_ids;
37 
41  public $names;
42 
46  public $comparison = '=';
47 
51  public $values;
52 
57 
61  public $case_sensitive = true;
62 
67 
72 
77 
82 
87 
92 
96  public $viewer_guid;
97 
102  public function prepare(QueryBuilder $qb, $table_alias = null) {
103  $alias = function ($column) use ($table_alias) {
104  return $table_alias ? "{$table_alias}.{$column}" : $column;
105  };
106 
107  $wheres = [];
108  $wheres[] = parent::prepare($qb, $table_alias);
109 
110  $access = new AccessWhereClause();
111  $access->use_enabled_clause = $this->use_enabled_clause;
112  $access->ignore_access = $this->ignore_access;
113  $access->viewer_guid = $this->viewer_guid;
114  $access->guid_column = 'entity_guid';
115  $wheres[] = $access->prepare($qb, $table_alias);
116 
117  $wheres[] = $qb->compare($alias('id'), '=', $this->ids, ELGG_VALUE_ID);
118  $wheres[] = $qb->compare($alias('name'), '=', $this->names, ELGG_VALUE_STRING);
119  $wheres[] = $qb->compare($alias('value'), $this->comparison, $this->values, $this->value_type, $this->case_sensitive);
120  $wheres[] = $qb->compare($alias('entity_guid'), '=', $this->entity_guids, ELGG_VALUE_GUID);
121  $wheres[] = $qb->compare($alias('owner_guid'), '=', $this->owner_guids, ELGG_VALUE_GUID);
122  $wheres[] = $qb->compare($alias('enabled'), '=', $this->enabled, ELGG_VALUE_STRING);
123  $wheres[] = $qb->compare($alias('access_id'), '=', $this->access_ids, ELGG_VALUE_ID);
124  $wheres[] = $qb->between($alias('time_created'), $this->created_after, $this->created_before, ELGG_VALUE_TIMESTAMP);
125 
126  if ($this->sort_by_calculation) {
127  if (!in_array(strtolower($this->sort_by_calculation), QueryBuilder::CALCULATIONS)) {
128  throw new DomainException("'{$this->sort_by_calculation}' is not a valid numeric calculation formula");
129  }
130 
131  $calculation = "{$this->sort_by_calculation}(CAST({$alias('value')} AS DECIMAL(10, 2)))";
132  $select_alias = 'annotation_calculation';
133 
134  $qb->addSelect("{$calculation} AS {$select_alias}");
135  $qb->addGroupBy($alias('entity_guid'));
136  $qb->addOrderBy($select_alias, $this->sort_by_direction);
137  } elseif ($this->sort_by_direction) {
138  $column = $alias('value');
139  if ($this->value_type == ELGG_VALUE_INTEGER) {
140  $column = "CAST({$column} AS SIGNED)";
141  }
142 
143  $qb->addOrderBy($column, $this->sort_by_direction);
144  }
145 
146  return $qb->merge($wheres);
147  }
148 }
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
Exception thrown if a value does not adhere to a defined valid data domain.
$column
Definition: add.php:10
const ELGG_VALUE_GUID
Definition: constants.php:113
Database abstraction query builder.
const ELGG_VALUE_ID
Definition: constants.php:114
Builds queries for matching annotations against their properties.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
prepare(QueryBuilder $qb, $table_alias=null)
{}
between($x, $lower=null, $upper=null, $type=null)
Build a between clause.
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
Builds a clause from closure or composite expression.
Definition: WhereClause.php:12
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
const ELGG_VALUE_STRING
Definition: constants.php:112
$qb
Definition: queue.php:11
Builds queries to restrict access.