-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
398 lines (329 loc) · 12.6 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
// Copyright 2014 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.
if (!defined("IN_ESOTALK")) exit;
ET::$pluginInfo["Profiles"] = array(
"name" => "Profiles",
"description" => "Allows custom fields to be added to user profiles.",
"version" => ESOTALK_VERSION,
"author" => "Toby Zerner",
"authorEmail" => "[email protected]",
"authorURL" => "http://esotalk.org",
"license" => "GPLv2",
"dependencies" => array(
"esoTalk" => "1.0.0g4"
)
);
class ETPlugin_Profiles extends ETPlugin {
/*
|--------------------------------------------------------------------------
| Plugin Setup
|--------------------------------------------------------------------------
*/
public function setup($oldVersion = "")
{
// For the Profiles plugin, we need two tables.
// The first, profile_field, stores information about the custom profile fields that
// the administrator has defined.
$structure = ET::$database->structure();
$structure->table("profile_field")
->column("fieldId", "int(11) unsigned", false)
->column("name", "varchar(31)", false)
->column("description", "varchar(255)")
->column("type", "enum('text','textarea','select','radios','checkboxes','member')", "text")
->column("options", "text")
->column("showOnPosts", "tinyint(1)", 0)
->column("hideFromGuests", "tinyint(1)", 0)
->column("searchable", "tinyint(1)", 0)
->column("position", "int(11)", 0)
->key("fieldId", "primary")
->exec(false);
// The second, profile_data, stores the actual values of these fields that users
// have entered in their profiles.
$structure
->table("profile_data")
->column("memberId", "int(11) unsigned", false)
->column("fieldId", "int(11) unsigned", false)
->column("data", "text")
->key(array("memberId", "fieldId"), "primary")
->exec(false);
// If this is the first installation of the Profiles plugin (e.g. on a fresh
// esoTalk installation,) set up some default fields for the administrator.
if (!$oldVersion) $this->createDefaultFields();
// Version 1.0.0g4 of this plugin changed the way that profile data is stored
// (previously it was just stored in the user preferences blob.) If we're
// upgrading from an old version, we'll need to convert to the new format.
elseif (version_compare($oldVersion, "1.0.0g4", "<")) {
$this->createDefaultFields();
$model = ET::getInstance("profileFieldModel");
$result = ET::SQL()->select("memberId, preferences")->from("member")->exec();
while ($row = $result->nextRow()) {
ET::memberModel()->expand($row);
if (!empty($row["preferences"]["about"])) $model->setData($row["memberId"], 1, $row["preferences"]["about"]);
if (!empty($row["preferences"]["location"])) $model->setData($row["memberId"], 2, $row["preferences"]["location"]);
}
}
return true;
}
protected function createDefaultFields()
{
$model = ET::getInstance("profileFieldModel");
if ( ! $model->getById(1)) {
$model->create(array(
"fieldId" => 1,
"name" => "About",
"description" => "Write something about yourself.",
"type" => "textarea"
));
}
if ( ! $model->getById(2)) {
$model->create(array(
"fieldId" => 2,
"name" => "Location",
"type" => "text",
"showOnPosts" => true,
"searchable" => true
));
}
}
public function __construct($rootDirectory)
{
parent::__construct($rootDirectory);
// Register the profile_field model which provides convenient methods to
// manage profile data.
ETFactory::register("profileFieldModel", "ProfileFieldModel", dirname(__FILE__)."/ProfileFieldModel.class.php");
// Register the profiles admin controller which provides an interface for
// administrators to manage custom profile fields.
ETFactory::registerAdminController("profiles", "ProfilesAdminController", dirname(__FILE__)."/ProfilesAdminController.class.php");
}
/*
|--------------------------------------------------------------------------
| Admin Page
|--------------------------------------------------------------------------
*/
// When initializing any admin controller, add a link to the Profiles admin page
// in the admin menu.
public function handler_initAdmin($sender, $menu)
{
$menu->add("profiles", "<a href='".URL("admin/profiles")."'><i class='icon-smile'></i> ".T("Profiles")."</a>");
}
/*
|--------------------------------------------------------------------------
| Member About Pane
|--------------------------------------------------------------------------
*/
// When initializing a member profile page, add the 'about' pane.
public function handler_memberController_initProfile($sender, $member, $panes, $controls, $actions)
{
$panes->add("about", "<a href='".URL(memberURL($member["memberId"], $member["username"], "about"))."'>".T("About")."</a>", 0);
}
// Set the default member pane as the 'about' pane.
public function action_memberController_index($sender, $member = "")
{
$this->action_memberController_about($sender, $member);
}
// Render the 'about' member profile pane.
public function action_memberController_about($sender, $member = "")
{
if (!($member = $sender->profile($member, "about"))) return;
$model = ET::getInstance("profileFieldModel");
$fields = $model->getData($member["memberId"]);
foreach ($fields as $k => &$field) {
// If this field is hidden from guests and we're a guest, don't display it.
if ($field["hideFromGuests"] and !ET::$session->user) unset($fields[$k]);
switch ($field["type"]) {
case "textarea":
$field["data"] = ET::formatter()->init($field["data"])->format()->get();
break;
case "checkboxes":
$items = explode("\n", $field["data"]);
$items = array_map("sanitizeHTML", $items);
$field["data"] = implode("<br>", $items);
break;
case "member":
$field["data"] = "<a href='".URL("member/name/".urlencode($field["data"]))."'>".sanitizeHTML($field["data"])."</a>";
break;
default:
$field["data"] = ET::formatter()->init($field["data"])->inline(true)->format()->get();
}
}
$sender->data("fields", $fields);
$sender->renderProfile($this->view("about"));
}
/*
|--------------------------------------------------------------------------
| Display Profile Fields On Posts
|--------------------------------------------------------------------------
*/
// Get fields from the database and attach them to the posts.
public function handler_postModel_getPostsAfter($sender, &$posts)
{
$postsById = array();
foreach ($posts as &$post) {
$postsById[$post["postId"]] = &$post;
$post["fields"] = array();
}
if (!count($postsById)) return;
$result = ET::SQL()
->select("p.postId, f.fieldId, f.name, f.hideFromGuests, d.data")
->from("post p")
->from("profile_data d", "d.memberId=p.memberId", "left")
->from("profile_field f", "d.fieldId=f.fieldId", "left")
->where("p.postId IN (:ids)")
->where("f.showOnPosts")
->orderBy("f.position ASC")
->bind(":ids", array_keys($postsById))
->exec();
while ($row = $result->nextRow()) {
$postsById[$row["postId"]]["fields"][$row["fieldId"]] = array(
"name" => $row["name"],
"data" => $row["data"],
"hideFromGuests" => $row["hideFromGuests"],
);
}
}
// Insert a post's fields into its template array.
public function handler_conversationController_formatPostForTemplate($sender, &$formatted, $post, $conversation)
{
if ($post["deleteMemberId"]) return;
foreach ($post["fields"] as $fieldId => $field) {
// If this field is hidden from guests and we're a guest, don't display it.
if ($field["hideFromGuests"] and !ET::$session->user) continue;
// Truncate the field's contents to 30 characters and add it to the post's info array.
if (strlen($field["data"]) > 30) $field["data"] = substr($field["data"], 0, 30)."...";
$formatted["info"][] = "<span class='profile-".$fieldId."'>".sanitizeHTML($field["data"])."</span>";
}
}
/*
|--------------------------------------------------------------------------
| User Profile Settings
|--------------------------------------------------------------------------
*/
// On the settings/general page, add a field to the form for each of the custom profile fields.
public function handler_settingsController_initGeneral($sender, $form)
{
$model = ET::getInstance("profileFieldModel");
$fields = $model->getData(ET::$session->userId);
$plugin = $this; // for use in closures
$sender->addJSFile("core/js/autocomplete.js");
foreach ($fields as $field) {
$key = "profile_".$field["fieldId"];
$form->addSection($key, $field["name"]);
if ($field["type"] == "checkboxes") {
$data = explode("\n", $field["data"]);
foreach ($data as $checkbox) {
$form->setValue($key."[".$checkbox."]", true);
}
} else {
$form->setValue($key, $field["data"]);
}
$form->addField($key, $key, function($form) use ($plugin, $field)
{
return $plugin->field($form, $field);
}, array($this, "saveField"));
}
}
// When a profile field is saved, save it to the database.
public function saveField($form, $key, &$preferences)
{
$value = $form->getValue($key);
if (is_array($value)) $value = implode("\n", array_keys($value));
// Limit the value to 1000 characters.
$value = mb_substr($value, 0, 1000, "UTF-8");
$model = ET::getInstance("profileFieldModel");
$model->setData(ET::$session->userId, substr($key, 8), $value);
}
protected function parseOptions($options)
{
$lines = explode("\n", $options);
$options = array();
foreach ($lines as $line) {
$line = trim($line);
$options[$line] = $line;
}
return $options;
}
// Render a custom profile field in the settings form.
public function field($form, $field)
{
$key = "profile_".$field["fieldId"];
switch ($field["type"]) {
case "textarea":
$input = $form->input($key, "textarea", array("rows" => 3, "style" => "width:500px"));
break;
case "select":
$options = $this->parseOptions($field["options"]);
$input = $form->select($key, $this->parseOptions($field["options"]));
break;
case "radios":
$options = $this->parseOptions($field["options"]);
$input = "<div class='checkboxGroup'>";
foreach ($options as $option) {
$input .= "<label class='radio'>".$form->radio($key, $option)." ".$option."</label>";
}
$input .= "</div>";
break;
case "checkboxes":
$options = $this->parseOptions($field["options"]);
$input = "<div class='checkboxGroup'>";
foreach ($options as $option) {
$input .= "<label class='checkbox'>".$form->checkbox($key."[".$option."]")." ".$option."</label>";
}
$input .= "</div>";
break;
case "member":
$input = $form->input($key, "text");
$input .= '<script>new ETAutoCompletePopup($("input[name='.$key.']"), false, function(member) {
$("input[name='.$key.']").val(member.name);
});</script>';
break;
default:
$input = $form->input($key, "text");
}
return $input.($field["description"] ? "<br><small>".$field["description"]."</small>" : "");
}
/*
|--------------------------------------------------------------------------
| Allow searching the member list by Profile fields
|--------------------------------------------------------------------------
*/
// Add gambits for each of the searchable profile fields.
public function handler_membersController_constructGambitsMenu($controller, &$gambits)
{
$model = ET::getInstance("profileFieldModel");
$fields = $model->get(array("searchable" => 1));
$gambits["profile"] = array();
foreach ($fields as $field) {
addToArrayString($gambits["profile"], mb_strtolower($field["name"], "UTF-8").": ?", array("gambit-profile-".$field["fieldId"]), 1);
}
}
// On the settings/general page, add a field to the form for each of the custom profile fields.
public function handler_membersController_parseTerms($sender, &$terms, $sql, &$conditions)
{
$model = ET::getInstance("profileFieldModel");
$fields = $model->get(array("searchable" => 1));
foreach ($terms as $k => $term) {
$term = strtolower(trim($term));
foreach ($fields as $field) {
$prefix = strtolower($field["name"]).":";
if (strpos($term, $prefix) === 0) {
// Find all member IDs that have field data matching this term.
$rows = ET::SQL()
->select("DISTINCT memberId")
->from("profile_data")
->where("fieldId", $field["fieldId"])
->where("data LIKE :value")
->bind(":value", "%".trim(substr($term, strlen($prefix)))."%")
->exec()
->allRows("memberId");
$ids = array_keys($rows);
// Add these members to the results.
$conditions[] = "m.memberId IN (:field$k)";
$sql->bind(":field$k", count($ids) ? $ids : 0);
unset($terms[$k]);
break;
}
}
}
}
}