Elgg  Version 6.0
EntitySortByClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
11 
16 
20  public $property;
21 
25  public $signed;
26 
30  public $join_type;
31 
36 
41 
46 
52  public function prepare(QueryBuilder $qb, $table_alias = null) {
53 
54  if (!isset($this->property_type)) {
55  if (in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
56  $this->property_type = 'attribute';
57  } else {
58  $this->property_type = 'metadata';
59  }
60  }
61 
62  // get correct base GUID column
63  // default assumes the main table is 'entities'
64  $from_column = 'guid';
65  switch ($qb->getTableName()) {
68  $from_column = 'entity_guid';
69  break;
71  $from_column = 'guid_one';
72  if ((bool) $this->inverse_relationship) {
73  $from_column = 'guid_two';
74  }
75  break;
76  }
77 
78  switch ($this->property_type) {
79  case 'metadata':
80  $md_alias = $qb->joinMetadataTable($table_alias, $from_column, $this->property, $this->join_type);
81  $column = "{$md_alias}.value";
82  break;
83 
84  case 'attribute':
85  if (!in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
86  throw new DomainException("'{$this->property}' is not a valid entity attribute");
87  }
88 
89  if ($qb->getTableName() !== EntityTable::TABLE_NAME) {
90  $e_alias = $qb->joinEntitiesTable($table_alias, $from_column, $this->join_type);
91  } else {
92  $e_alias = $table_alias;
93  }
94 
95  $column = "{$e_alias}.{$this->property}";
96  break;
97 
98  case 'annotation':
99  $an_alias = $qb->joinAnnotationTable($table_alias, $from_column, $this->property, $this->join_type);
100  $column = "{$an_alias}.value";
101  break;
102 
103  case 'relationship':
105  $inverse = (bool) $this->inverse_relationship;
106  $er_alias = $qb->joinRelationshipTable($table_alias, $from_column, $this->property, $inverse, $this->join_type);
107  if (!empty($this->relationship_guid)) {
108  $guid_column = $inverse ? 'guid_two' : 'guid_one';
109  $qb->andWhere($qb->compare("{$er_alias}.{$guid_column}", '=', $this->relationship_guid, ELGG_VALUE_GUID));
110  }
111  } else {
112  $er_alias = $table_alias;
113  }
114 
115  $column = "{$er_alias}.time_created";
116 
117  break;
118 
119  default:
120  elgg_log("'{$this->property_type}' is not a valid entity property type. Sorting ignored.");
121  return null;
122  }
123 
124  if ($this->signed) {
125  $column = "CAST({$column} AS SIGNED)";
126  }
127 
128  $this->expr = $column;
129 
130  parent::prepare($qb, $table_alias);
131  }
132 }
getTableName()
Returns the name of the primary table.
joinAnnotationTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join annotations table from alias and return joined table alias.
prepare(QueryBuilder $qb, $table_alias=null)
{}
const PRIMARY_ATTR_NAMES
Definition: ElggEntity.php:53
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.
Extends QueryBuilder with clauses necessary to sort entity lists by entity properties.
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
compare(string $x, string $comparison, $y=null, string $type=null, bool $case_sensitive=null)
Build value comparison clause.
joinEntitiesTable(string $from_alias= '', string $from_column= 'guid',?string $join_type= 'inner', string $joined_alias=null)
Join entity table from alias and return joined table alias.
joinRelationshipTable(string $from_alias= '', string $from_column= 'guid', $name=null, bool $inverse=false,?string $join_type= 'inner', string $joined_alias=null)
Join relationship table from alias and return joined table alias.
Extends QueryBuilder with ORDER BY clauses.
joinMetadataTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join metadata table from alias and return joined table alias.
$qb
Definition: queue.php:12