Elgg  Version 4.3
AnnotationWhereClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
5 use DateTime;
8 
13 
17  public $ids;
18 
22  public $entity_guids;
23 
27  public $owner_guids;
28 
32  public $enabled;
33 
37  public $access_ids;
38 
42  public $names;
43 
47  public $comparison = '=';
48 
52  public $values;
53 
58 
62  public $case_sensitive = true;
63 
68 
73 
78 
83 
88 
93 
97  public $viewer_guid;
98 
103  public function prepare(QueryBuilder $qb, $table_alias = null) {
104  $alias = function ($column) use ($table_alias) {
105  return $table_alias ? "{$table_alias}.{$column}" : $column;
106  };
107 
108  $wheres = [];
109  $wheres[] = parent::prepare($qb, $table_alias);
110 
111  $access = new AccessWhereClause();
112  $access->use_enabled_clause = $this->use_enabled_clause;
113  $access->ignore_access = $this->ignore_access;
114  $access->viewer_guid = $this->viewer_guid;
115  $access->guid_column = 'entity_guid';
116  $wheres[] = $access->prepare($qb, $table_alias);
117 
118  $wheres[] = $qb->compare($alias('id'), '=', $this->ids, ELGG_VALUE_ID);
119  $wheres[] = $qb->compare($alias('name'), '=', $this->names, ELGG_VALUE_STRING);
120  $wheres[] = $qb->compare($alias('value'), $this->comparison, $this->values, $this->value_type, $this->case_sensitive);
121  $wheres[] = $qb->compare($alias('entity_guid'), '=', $this->entity_guids, ELGG_VALUE_GUID);
122  $wheres[] = $qb->compare($alias('owner_guid'), '=', $this->owner_guids, ELGG_VALUE_GUID);
123  $wheres[] = $qb->compare($alias('enabled'), '=', $this->enabled, ELGG_VALUE_STRING);
124  $wheres[] = $qb->compare($alias('access_id'), '=', $this->access_ids, ELGG_VALUE_ID);
125  $wheres[] = $qb->between($alias('time_created'), $this->created_after, $this->created_before, ELGG_VALUE_TIMESTAMP);
126 
127  if ($this->sort_by_calculation) {
128  if (!in_array(strtolower($this->sort_by_calculation), QueryBuilder::$calculations)) {
129  throw new InvalidParameterException("'$this->sort_by_calculation' is not a valid numeric calculation formula");
130  }
131 
132  $calculation = "{$this->sort_by_calculation}(CAST({$alias('value')} AS DECIMAL(10, 2)))";
133  $select_alias = "annotation_calculation";
134 
135  $qb->addSelect("$calculation AS $select_alias");
136  $qb->addGroupBy($alias('entity_guid'));
137  $qb->addOrderBy($select_alias, $this->sort_by_direction);
138  } else if ($this->sort_by_direction) {
139  $column = $alias('value');
140  if ($this->value_type == ELGG_VALUE_INTEGER) {
141  $column = "CAST($column AS SIGNED)";
142  }
143  $qb->addOrderBy($column, $this->sort_by_direction);
144  }
145 
146  return $qb->merge($wheres);
147  }
148 
149 }
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:126
$column
Definition: add.php:10
const ELGG_VALUE_GUID
Definition: constants.php:128
Database abstraction query builder.
const ELGG_VALUE_ID
Definition: constants.php:129
Builds queries for matching annotations against their properties.
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:130
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:127
$qb
Definition: queue.php:11
Builds queries to restrict access.