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 $access_ids;
32 
36  public $names;
37 
38  public string $comparison = '=';
39 
43  public $values;
44 
45  public string $value_type = ELGG_VALUE_STRING;
46 
47  public bool $case_sensitive = true;
48 
53 
58 
59  public ?string $sort_by_direction = null;
60 
61  public ?string $sort_by_calculation = null;
62 
63  public ?bool $ignore_access = null;
64 
65  public ?int $viewer_guid = null;
66 
71  public function prepare(QueryBuilder $qb, $table_alias = null) {
72  $alias = function ($column) use ($table_alias) {
73  return $table_alias ? "{$table_alias}.{$column}" : $column;
74  };
75 
76  $wheres = [];
77  $wheres[] = parent::prepare($qb, $table_alias);
78 
79  $access = new AccessWhereClause();
80  $access->ignore_access = $this->ignore_access;
81  $access->use_deleted_clause = false;
82  $access->use_enabled_clause = false;
83  $access->viewer_guid = $this->viewer_guid;
84  $access->guid_column = 'entity_guid';
85  $wheres[] = $access->prepare($qb, $table_alias);
86 
87  $wheres[] = $qb->compare($alias('id'), '=', $this->ids, ELGG_VALUE_ID);
88  $wheres[] = $qb->compare($alias('name'), '=', $this->names, ELGG_VALUE_STRING);
89  $wheres[] = $qb->compare($alias('value'), $this->comparison, $this->values, $this->value_type, $this->case_sensitive);
90  $wheres[] = $qb->compare($alias('entity_guid'), '=', $this->entity_guids, ELGG_VALUE_GUID);
91  $wheres[] = $qb->compare($alias('owner_guid'), '=', $this->owner_guids, ELGG_VALUE_GUID);
92  $wheres[] = $qb->compare($alias('access_id'), '=', $this->access_ids, ELGG_VALUE_ID);
93  $wheres[] = $qb->between($alias('time_created'), $this->created_after, $this->created_before, ELGG_VALUE_TIMESTAMP);
94 
95  if ($this->sort_by_calculation) {
96  if (!in_array(strtolower($this->sort_by_calculation), QueryBuilder::CALCULATIONS)) {
97  throw new DomainException("'{$this->sort_by_calculation}' is not a valid numeric calculation formula");
98  }
99 
100  $calculation = "{$this->sort_by_calculation}(CAST({$alias('value')} AS DECIMAL(10, 2)))";
101  $select_alias = 'annotation_calculation';
102 
103  $qb->addSelect("{$calculation} AS {$select_alias}");
104  $qb->addGroupBy($alias('entity_guid'));
105  $qb->addOrderBy($select_alias, $this->sort_by_direction);
106  } elseif ($this->sort_by_direction) {
107  $column = $alias('value');
108  if ($this->value_type == ELGG_VALUE_INTEGER) {
109  $column = "CAST({$column} AS SIGNED)";
110  }
111 
112  $qb->addOrderBy($column, $this->sort_by_direction);
113  }
114 
115  return $qb->merge($wheres);
116  }
117 }
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)
{}
compare(string $x, string $comparison, $y=null, string $type=null, bool $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:11
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
const ELGG_VALUE_STRING
Definition: constants.php:112
addOrderBy(string $sort,?string $order=null)
{}
$qb
Definition: queue.php:12
between(string $x, $lower=null, $upper=null, string $type=null)
Build a between clause.
Builds queries to restrict access.