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