Add EmailLogListPage
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / I18nDatabaseObjectList.class.php
1 <?php
2
3 namespace wcf\data;
4
5 use wcf\system\WCF;
6 use wcf\util\StringUtil;
7
8 /**
9 * Abstract class for a list of database objects with better sorting of i18n-based columns.
10 *
11 * @author Florian Gail
12 * @copyright 2001-2019 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\Data
15 * @since 3.1
16 */
17 abstract class I18nDatabaseObjectList extends DatabaseObjectList
18 {
19 /**
20 * Array of column names, that are eventually filled with language items.
21 * Those additional joins are going to slow down the system; you should use this
22 * class only when it's really needed.
23 * The key represents the original field name.
24 * The value represents the new field name containing the localized field-value.
25 *
26 * @example [ 'title' => 'titleSortField' ]
27 *
28 * @var string[]
29 */
30 public $i18nFields = [];
31
32 /**
33 * @inheritDoc
34 * @param int $languageID id of the language that should be used
35 * @throws \DomainException
36 */
37 public function __construct($languageID = null)
38 {
39 parent::__construct();
40
41 if ($languageID === null) {
42 $languageID = WCF::getUser()->languageID;
43 }
44
45 if (!empty($this->i18nFields)) {
46 if (\count($this->i18nFields) !== \count(\array_flip($this->i18nFields))) {
47 throw new \DomainException("Array values of '" . $this->className . "::\$i18nFields' must be unique.");
48 }
49
50 foreach ($this->i18nFields as $key => $value) {
51 if (!\preg_match('/^[a-z][a-zA-Z0-9]*$/', $key) || !\preg_match('/^[a-z][a-zA-Z0-9]*$/', $value)) {
52 throw new \DomainException("Array keys and values of '" . $this->className . "::\$i18nFields' must start with a small letter and consist of letters and number only.");
53 }
54
55 $matchTable = 'i18n_' . StringUtil::getHash($key);
56
57 $this->sqlSelects .= (!empty($this->sqlSelects) ? ', ' : '') . "COALESCE(" . $matchTable . ".languageItemValue, " . $this->getDatabaseTableAlias() . "." . $key . ") AS " . $value;
58 $this->sqlJoins .= "
59 LEFT JOIN wcf" . WCF_N . "_language_item " . $matchTable . "
60 ON " . $matchTable . ".languageItem = " . $this->getDatabaseTableAlias() . "." . $key . "
61 AND " . $matchTable . ".languageID = " . $languageID;
62 }
63 }
64 }
65 }