Added detailed list of received/given likes in user profiles
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / DatabaseObject.class.php
1 <?php
2 namespace wcf\data;
3 use wcf\system\WCF;
4
5 /**
6 * Abstract class for all data holder classes.
7 *
8 * @author Marcel Werk
9 * @copyright 2001-2014 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package com.woltlab.wcf
12 * @subpackage data
13 * @category Community Framework
14 */
15 abstract class DatabaseObject implements IStorableObject {
16 /**
17 * database table for this object
18 * @var string
19 */
20 protected static $databaseTableName = '';
21
22 /**
23 * indicates if database table index is an identity column
24 * @var boolean
25 */
26 protected static $databaseTableIndexIsIdentity = true;
27
28 /**
29 * name of the primary index column
30 * @var string
31 */
32 protected static $databaseTableIndexName = '';
33
34 /**
35 * sort field
36 * @var mixed
37 */
38 protected static $sortBy = null;
39
40 /**
41 * sort order
42 * @var mixed
43 */
44 protected static $sortOrder = null;
45
46 /**
47 * object data
48 * @var array
49 */
50 protected $data = null;
51
52 /**
53 * Creates a new instance of the DatabaseObject class.
54 *
55 * @param mixed $id
56 * @param array $row
57 * @param \wcf\data\DatabaseObject $object
58 */
59 public function __construct($id, array $row = null, DatabaseObject $object = null) {
60 if ($id !== null) {
61 $sql = "SELECT *
62 FROM ".static::getDatabaseTableName()."
63 WHERE ".static::getDatabaseTableIndexName()." = ?";
64 $statement = WCF::getDB()->prepareStatement($sql);
65 $statement->execute(array($id));
66 $row = $statement->fetchArray();
67
68 // enforce data type 'array'
69 if ($row === false) $row = array();
70 }
71 else if ($object !== null) {
72 $row = $object->data;
73 }
74
75 $this->handleData($row);
76 }
77
78 /**
79 * Stores the data of a database row.
80 *
81 * @param array $data
82 */
83 protected function handleData($data) {
84 // provide a logical false value for - assumed numeric - primary index
85 if (!isset($data[static::getDatabaseTableIndexName()])) {
86 $data[static::getDatabaseTableIndexName()] = 0;
87 }
88
89 $this->data = $data;
90 }
91
92 /**
93 * @see \wcf\data\IStorableObject::__get()
94 */
95 public function __get($name) {
96 if (isset($this->data[$name])) {
97 return $this->data[$name];
98 }
99 else {
100 return null;
101 }
102 }
103
104 /**
105 * Returns the id of the object.
106 *
107 * @return mixed
108 */
109 public function getObjectID() {
110 return $this->data[static::getDatabaseTableIndexName()];
111 }
112
113 /**
114 * @see \wcf\data\IStorableObject::__isset()
115 */
116 public function __isset($name) {
117 return isset($this->data[$name]);
118 }
119
120 /**
121 * @see \wcf\data\IStorableObject::getData()
122 */
123 public function getData() {
124 return $this->data;
125 }
126
127 /**
128 * @see \wcf\data\IStorableObject::getDatabaseTableName()
129 */
130 public static function getDatabaseTableName() {
131 return 'wcf'.WCF_N.'_'.static::$databaseTableName;
132 }
133
134 /**
135 * @see \wcf\data\IStorableObject::getDatabaseTableAlias()
136 */
137 public static function getDatabaseTableAlias() {
138 return static::$databaseTableName;
139 }
140
141 /**
142 * @see \wcf\data\IStorableObject::getDatabaseTableIndexIsIdentity()
143 */
144 public static function getDatabaseTableIndexIsIdentity() {
145 return static::$databaseTableIndexIsIdentity;
146 }
147
148 /**
149 * @see \wcf\data\IStorableObject::getDatabaseTableIndexName()
150 */
151 public static function getDatabaseTableIndexName() {
152 return static::$databaseTableIndexName;
153 }
154
155 /**
156 * Sorts a list of database objects.
157 *
158 * @param array<\wcf\data\DatabaseObject> $objects
159 * @param mixed $sortBy
160 * @param string $sortOrder
161 * @return boolean
162 */
163 public static function sort(&$objects, $sortBy, $sortOrder = 'ASC', $maintainIndexAssociation = true) {
164 $sortArray = $objects2 = array();
165 foreach ($objects as $idx => $obj) {
166 $sortArray[$idx] = $obj->$sortBy;
167
168 // array_multisort will drop index association if key is not a string
169 if ($maintainIndexAssociation) {
170 $objects2[$idx.'x'] = $obj;
171 }
172 }
173
174 if ($maintainIndexAssociation) {
175 $objects = array();
176 array_multisort($sortArray, $sortOrder == 'ASC' ? SORT_ASC : SORT_DESC, $objects2);
177
178 $objects = array();
179 foreach ($objects2 as $idx => $obj) {
180 $objects[substr($idx, 0, -1)] = $obj;
181 }
182 }
183 else {
184 array_multisort($sortArray, $sortOrder == 'ASC' ? SORT_ASC : SORT_DESC, $objects);
185 }
186 }
187 }