1<?php
2/**
3 * Houses list.
4 *
5 * Loaded directly by the root houses.php (theme_file('pages/houses.php')),
6 * not through page.php - so this file opens and closes the theme itself.
7 */
8$h = static function ($v): string { return htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); };
9
10theme_open();
11
12if ($config['log_ip'])
13 znote_visitor_insert_detailed_data(3);
14
15$querystring_id = &$_GET['id'];
16$townid = ($querystring_id) ? (int)$_GET['id'] : $config['houseConfig']['HouseListDefaultTown'];
17$towns = $config['towns'];
18
19$order = &$_GET['order'];
20$type = &$_GET['type'];
21?>
22<div class="znx-acct">
23
24<div class="znx-acct-head"><?= t('common.town') ?></div>
25
26<form action="" method="get" class="znx-acct-toolbar">
27 <div class="znx-acct-toolbar__field">
28 <select name="id" class="znx-acct-select">
29 <?php foreach ($towns as $id => $name): ?>
30 <option value="<?= $h($id) ?>" <?= $townid == $id ? 'selected' : '' ?>><?= $h($name) ?></option>
31 <?php endforeach; ?>
32 </select>
33 </div>
34
35 <?php
36 $order_allowed = array('id', 'name', 'size', 'beds', 'rent', 'owner');
37 $order_labels = array('id' => 'ID', 'name' => t('common.name'), 'size' => t('house.size'), 'beds' => t('house.beds'), 'rent' => t('house.rent'), 'owner' => t('house.owner'));
38 ?>
39 <div class="znx-acct-toolbar__field">
40 <select name="order" class="znx-acct-select">
41 <?php foreach ($order_allowed as $o): ?>
42 <option value="<?= $h($o) ?>" <?= $o == $order ? 'selected' : '' ?>><?= $h($order_labels[$o]) ?></option>
43 <?php endforeach; ?>
44 </select>
45 </div>
46
47 <?php $type_allowed = array('desc', 'asc'); ?>
48 <div class="znx-acct-toolbar__field">
49 <select name="type" class="znx-acct-select">
50 <?php foreach ($type_allowed as $t): ?>
51 <option value="<?= $h($t) ?>" <?= $t == $type ? 'selected' : '' ?>><?= $t == 'desc' ? t('houses.descending') : t('houses.ascending') ?></option>
52 <?php endforeach; ?>
53 </select>
54 </div>
55
56 <div class="znx-acct-toolbar__submit"><button type="submit" class="znx-acct-btn"><?= t('houses.fetch') ?></button></div>
57</form>
58
59<?php
60if (!in_array($order, $order_allowed))
61 $order = 'id';
62
63if (!in_array($type, $type_allowed))
64 $type = 'desc';
65
66$cache = new Cache('engine/cache/houses/houses-' . $order . '-' . $type);
67$houses = array();
68
69if ($cache->hasExpired()) {
70
71 $houses = db()->fetchAll("
72 SELECT
73 `id`, `owner`, `paid`, `warnings`, `name`, `rent`, `town_id`,
74 `size`, `beds`, " . houseSelect(array('bid','bid_end','last_bid','highest_bidder')) . "
75 FROM `houses`
76 ORDER BY {$order} {$type};
77 ");
78
79 if ($houses !== false) {
80 $playerlist = array();
81
82 foreach ($houses as $houseRow)
83 if ($houseRow['owner'] > 0)
84 $playerlist[] = (int)$houseRow['owner'];
85
86 if (!empty($playerlist)) {
87 $placeholders = implode(',', array_fill(0, count($playerlist), '?'));
88 $tmpPlayers = db()->fetchAll("SELECT `id`, `name` FROM players WHERE `id` IN ($placeholders);", $playerlist);
89
90 $tmpById = array();
91 foreach ($tmpPlayers as $p)
92 $tmpById[$p['id']] = $p['name'];
93
94 for ($i = 0; $i < count($houses); $i++)
95 if ($houses[$i]['owner'] > 0)
96 $houses[$i]['ownername'] = $tmpById[$houses[$i]['owner']];
97 }
98
99 $cache->setContent($houses);
100 $cache->save();
101 }
102} else
103 $houses = $cache->load();
104
105if ($houses !== false || !empty($houses)):
106 ?>
107 <div class="znx-acct-table-wrap">
108 <table id="housetable" class="znx-acct-table">
109 <tr class="yellow">
110 <th><?= t('common.name') ?></th>
111 <th><?= t('house.size') ?></th>
112 <th><?= t('house.beds') ?></th>
113 <th><?= t('house.rent') ?></th>
114 <th><?= t('house.owner') ?></th>
115 <th><?= t('common.town') ?></th>
116 </tr>
117 <?php foreach ($houses as $house): ?>
118 <?php if ($house['town_id'] == $townid): ?>
119 <?php $town_name = $towns[$house['town_id']] ?? null; ?>
120 <tr>
121 <td><a href="house.php?id=<?= (int) $house['id'] ?>"><?= $h($house['name']) ?></a></td>
122 <td><?= (int) $house['size'] ?></td>
123 <td><?= (int) $house['beds'] ?></td>
124 <td><?= (int) $house['rent'] ?></td>
125 <?php if ($house['owner'] != 0): ?>
126 <td><a href="characterprofile.php?name=<?= $h($house['ownername']) ?>" target="_blank"><?= $h($house['ownername']) ?></a></td>
127 <?php else: ?>
128 <td><?= ($house['highest_bidder'] == 0) ? $h(t('common.none')) : '<b>' . $h(t_default('houses.selling', 'Selling')) . '</b>' ?></td>
129 <?php endif; ?>
130 <td><?= $town_name ? $h($town_name) : $h(t_default('houses.unknown_town', 'Specify town id') . ' ' . $house['town_id'] . ' ' . t_default('houses.unknown_town_suffix', 'name in config.php first.')) ?></td>
131 </tr>
132 <?php endif; ?>
133 <?php endforeach; ?>
134 </table>
135 </div>
136 <?php
137else:
138 ?>
139 <div class="znx-acct-head"><?= t('houses.fetch_failed') ?></div>
140 <div class="znx-empty-box"><?= t('houses.empty') ?></div>
141 <?php
142endif; ?>
143
144</div><!-- .znx-acct -->
145<?php
146theme_close();
147