Elgg  Version 4.3
EntitySortByClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
7 use ElggEntity;
8 
13 
17  public $property;
18 
22  public $signed;
23 
27  public $join_type;
28 
33 
38 
43 
47  public function prepare(QueryBuilder $qb, $table_alias = null) {
48 
49  if (!isset($this->property_type)) {
50  if (in_array($this->property, ElggEntity::PRIMARY_ATTR_NAMES)) {
51  $this->property_type = 'attribute';
52  } else {
53  $this->property_type = 'metadata';
54  }
55  }
56 
57  // get correct base GUID column
58  // default assumes the main table is 'entities'
59  $from_column = 'guid';
61  $from_column = 'guid_one';
62  if ((bool) $this->inverse_relationship) {
63  $from_column = 'guid_two';
64  }
65  }
66 
67  switch ($this->property_type) {
68  case 'metadata':
69  $md_alias = $qb->joinMetadataTable($table_alias, $from_column, $this->property, $this->join_type);
70  $column = "{$md_alias}.value";
71  break;
72 
73  case 'attribute':
74  if (!in_array($this->property, ElggEntity::PRIMARY_ATTR_NAMES)) {
75  throw new InvalidParameterException("'{$this->property}' is not a valid entity attribute");
76  }
77 
79  $e_alias = $qb->joinEntitiesTable($table_alias, $from_column, $this->join_type);
80  } else {
81  $e_alias = $table_alias;
82  }
83 
84  $column = "{$e_alias}.{$this->property}";
85  break;
86 
87  case 'private_setting':
88  $ps_alias = $qb->joinPrivateSettingsTable($table_alias, $from_column, $this->property, $this->join_type);
89  $column = "{$ps_alias}.value";
90  break;
91 
92  case 'annotation':
93  $an_alias = $qb->joinAnnotationTable($table_alias, $from_column, $this->property, $this->join_type);
94  $column = "{$an_alias}.value";
95  break;
96 
97  case 'relationship':
99  $er_alias = $qb->joinRelationshipTable($table_alias, $from_column, $this->property, $this->inverse_relationship, $this->join_type);
100  if (!empty($this->relationship_guid)) {
101  $guid_column = $this->inverse_relationship ? 'guid_two' : 'guid_one';
102  $qb->andWhere($qb->compare("{$er_alias}.{$guid_column}", '=', $this->relationship_guid, ELGG_VALUE_GUID));
103  }
104  } else {
105  $er_alias = $table_alias;
106  }
107  $column = "{$er_alias}.time_created";
108 
109  break;
110 
111  default:
112  elgg_log("'{$this->property_type}' is not a valid entity property type. Sorting ignored.");
113  return null;
114  }
115 
116  if ($this->signed) {
117  $column = "CAST({$column} AS SIGNED)";
118  }
119 
120  $this->expr = $column;
121 
122  return parent::prepare($qb, $table_alias);
123  }
124 }
getTableName()
Returns the name of the primary table.
prepare(QueryBuilder $qb, $table_alias=null)
{}
const PRIMARY_ATTR_NAMES
Definition: ElggEntity.php:50
$column
Definition: add.php:10
const ELGG_VALUE_GUID
Definition: constants.php:128
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:399
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
elgg ElggEntity
Definition: deprecated.js:437
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.
joinPrivateSettingsTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join private settings table from alias and return joined table alias.
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