Elgg  Version 3.0
AnnotationWhereClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
5 use DateTime;
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 
101  public function prepare(QueryBuilder $qb, $table_alias = null) {
102  $alias = function ($column) use ($table_alias) {
103  return $table_alias ? "{$table_alias}.{$column}" : $column;
104  };
105 
106  $wheres = [];
107  $wheres[] = parent::prepare($qb, $table_alias);
108 
109  $access = new AccessWhereClause();
110  $access->use_enabled_clause = $this->use_enabled_clause;
111  $access->ignore_access = $this->ignore_access;
112  $access->viewer_guid = $this->viewer_guid;
113  $access->guid_column = 'entity_guid';
114  $wheres[] = $access->prepare($qb, $table_alias);
115 
116  $wheres[] = $qb->compare($alias('id'), '=', $this->ids, ELGG_VALUE_ID);
117  $wheres[] = $qb->compare($alias('name'), '=', $this->names, ELGG_VALUE_STRING);
118  $wheres[] = $qb->compare($alias('value'), $this->comparison, $this->values, $this->value_type, $this->case_sensitive);
119  $wheres[] = $qb->compare($alias('entity_guid'), '=', $this->entity_guids, ELGG_VALUE_GUID);
120  $wheres[] = $qb->compare($alias('owner_guid'), '=', $this->owner_guids, ELGG_VALUE_GUID);
121  $wheres[] = $qb->compare($alias('enabled'), '=', $this->enabled, ELGG_VALUE_STRING);
122  $wheres[] = $qb->compare($alias('access_id'), '=', $this->access_ids, ELGG_VALUE_ID);
123  $wheres[] = $qb->between($alias('time_created'), $this->created_after, $this->created_before, ELGG_VALUE_TIMESTAMP);
124 
125  if ($this->sort_by_calculation) {
126  if (!in_array(strtolower($this->sort_by_calculation), QueryBuilder::$calculations)) {
127  throw new \InvalidParameterException("'$this->sort_by_calculation' is not a valid numeric calculation formula");
128  }
129 
130  $calculation = "{$this->sort_by_calculation}(CAST({$alias('value')} AS DECIMAL(10, 2)))";
131  $select_alias = "annotation_calculation";
132 
133  $qb->addSelect("$calculation AS $select_alias");
134  $qb->addGroupBy($alias('entity_guid'));
135  $qb->addOrderBy($select_alias, $this->sort_by_direction);
136  } else if ($this->sort_by_direction) {
137  $column = $alias('value');
138  if ($this->value_type == ELGG_VALUE_INTEGER) {
139  $column = "CAST($column AS SIGNED)";
140  }
141  $qb->addOrderBy($column, $this->sort_by_direction);
142  }
143 
144  return $qb->merge($wheres);
145  }
146 
147 }
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
$column
Definition: add.php:10
const ELGG_VALUE_GUID
Definition: constants.php:140
Database abstraction query builder.
const ELGG_VALUE_ID
Definition: constants.php:141
Builds queries for matching annotations against their properties.
prepare(QueryBuilder $qb, $table_alias=null)
{Build an expression and/or apply it to an instance of query builder.Query builder Table aliasComposi...
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:142
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:139
Builds queries to restrict access.
$access
Definition: save.php:18