Skip to content

Commit 7c74b77

Browse files
committed
Add discord username to user profile
1 parent 9362135 commit 7c74b77

File tree

8 files changed

+95
-10
lines changed

8 files changed

+95
-10
lines changed

etc/database.sample.sql

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ DROP TABLE IF EXISTS `user_profiles`;
667667
/*!40101 SET character_set_client = utf8 */;
668668
CREATE TABLE `user_profiles` (
669669
`user_id` bigint(20) unsigned NOT NULL,
670+
`discord_username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
670671
`github_username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
671672
`reddit_username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
672673
`steam_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

src/controllers/User/Update.php

+25
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function &run(Router &$router, View &$view, array &$args) {
5454
if ( $model->profile ) {
5555

5656
$model->biography = $model->profile->getBiography();
57+
$model->discord_username = $model->profile->getDiscordUsername();
5758
$model->facebook_username = $model->profile->getFacebookUsername();
5859
$model->github_username = $model->profile->getGitHubUsername();
5960
$model->instagram_username = $model->profile->getInstagramUsername();
@@ -92,6 +93,10 @@ public function &run(Router &$router, View &$view, array &$args) {
9293
isset($data['biography']) ? $data['biography'] : null
9394
);
9495

96+
$model->discord_username = (
97+
isset($data['discord_username']) ? $data['discord_username'] : null
98+
);
99+
95100
$model->facebook_username = (
96101
isset($data['facebook_username']) ? $data['facebook_username'] : null
97102
);
@@ -244,6 +249,24 @@ public function &run(Router &$router, View &$view, array &$args) {
244249

245250
}
246251

252+
if (
253+
$model->discord_username !== $model->profile->getDiscordUsername()
254+
) {
255+
256+
// discord username change request
257+
258+
if (strlen($model->discord_username) >
259+
$model->discord_username_max_len
260+
) {
261+
$model->discord_username_error = ['red', 'TOO_LONG'];
262+
} else {
263+
$model->profile->setDiscordUsername($model->discord_username);
264+
$model->discord_username_error = ['green', 'CHANGE_SUCCESS'];
265+
$profile_changed = true;
266+
}
267+
268+
}
269+
247270
if (
248271
$model->facebook_username !== $model->profile->getFacebookUsername()
249272
) {
@@ -399,6 +422,7 @@ public function &run(Router &$router, View &$view, array &$args) {
399422
'email_error' => $model->email_error,
400423
'display_name_error' => $model->display_name_error,
401424
'biography_error' => $model->biography_error,
425+
'discord_username_error' => $model->discord_username_error,
402426
'facebook_username_error' => $model->facebook_username_error,
403427
'github_username_error' => $model->github_username_error,
404428
'instagram_username_error' => $model->instagram_username_error,
@@ -415,6 +439,7 @@ public function &run(Router &$router, View &$view, array &$args) {
415439
'display_name' => $display_name,
416440
'profile_changed' => $profile_changed,
417441
'biography' => $model->biography,
442+
'discord_username' => $model->discord_username,
418443
'facebook_username' => $model->facebook_username,
419444
'github_username' => $model->github_username,
420445
'instagram_username' => $model->instagram_username,

src/controllers/User/View.php

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ protected function getUserInfo(UserViewModel &$model) {
5555

5656
$model->biography = $model->user_profile->getBiography();
5757

58+
$model->discord = $model->user_profile->getDiscordUsername();
59+
5860
$model->facebook = $model->user_profile->getFacebookUsername();
5961
$model->facebook_uri = $model->user_profile->getFacebookURI();
6062

src/libraries/UserProfile.php

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class UserProfile {
1616

1717
protected $biography;
18+
protected $discord_username;
1819
protected $facebook_username;
1920
protected $github_username;
2021
protected $id;
@@ -29,6 +30,7 @@ class UserProfile {
2930
public function __construct($data) {
3031
if (is_numeric($data)) {
3132
$this->biography = null;
33+
$this->discord_username = null;
3234
$this->facebook_username = null;
3335
$this->github_username = null;
3436
$this->id = (int) $data;
@@ -43,6 +45,7 @@ public function __construct($data) {
4345
} else if ($data instanceof StdClass) {
4446
self::normalize($data);
4547
$this->biography = $data->biography;
48+
$this->discord_username = $data->discord_username;
4649
$this->facebook_username = $data->facebook_username;
4750
$this->github_username = $data->github_username;
4851
$this->id = $data->id;
@@ -62,6 +65,10 @@ public function getBiography() {
6265
return $this->biography;
6366
}
6467

68+
public function getDiscordUsername() {
69+
return $this->discord_username;
70+
}
71+
6572
public function getFacebookURI() {
6673
return "https://www.facebook.com/" . $this->getFacebookUsername();
6774
}
@@ -164,6 +171,9 @@ protected static function normalize(StdClass &$data) {
164171
if (!is_null($data->biography))
165172
$data->biography = (string) $data->biography;
166173

174+
if (!is_null($data->discord_username))
175+
$data->discord_username = (string) $data->discord_username;
176+
167177
if (!is_null($data->facebook_username))
168178
$data->facebook_username = (string) $data->facebook_username;
169179

@@ -200,6 +210,7 @@ public function refresh() {
200210
if ($cache_val !== false) {
201211
$cache_val = unserialize($cache_val);
202212
$this->biography = $cache_val->biography;
213+
$this->discord_username = $cache_val->discord_username;
203214
$this->facebook_username = $cache_val->facebook_username;
204215
$this->github_username = $cache_val->github_username;
205216
$this->instagram_username = $cache_val->instagram_username;
@@ -218,6 +229,7 @@ public function refresh() {
218229
$stmt = Common::$database->prepare("
219230
SELECT
220231
`biography`,
232+
`discord_username`,
221233
`facebook_username`,
222234
`github_username`,
223235
`instagram_username`,
@@ -242,6 +254,7 @@ public function refresh() {
242254
$stmt->closeCursor();
243255
self::normalize($row);
244256
$this->biography = $row->biography;
257+
$this->discord_username = $row->discord_username;
245258
$this->facebook_username = $row->facebook_username;
246259
$this->github_username = $row->github_username;
247260
$this->instagram_username = $row->instagram_username;
@@ -269,6 +282,7 @@ public function save() {
269282
`user_profiles`
270283
SET
271284
`biography` = :bio,
285+
`discord_username` = :discord,
272286
`facebook_username` = :fb,
273287
`github_username` = :github,
274288
`instagram_username` = :ig,
@@ -283,6 +297,7 @@ public function save() {
283297
LIMIT 1;
284298
');
285299
$stmt->bindParam(':bio', $this->biography, PDO::PARAM_STR);
300+
$stmt->bindParam(':discord', $this->discord_username, PDO::PARAM_STR);
286301
$stmt->bindParam(':fb', $this->facebook_username, PDO::PARAM_STR);
287302
$stmt->bindParam(':github', $this->github_username, PDO::PARAM_STR);
288303
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
@@ -327,6 +342,10 @@ public function setBiography($value) {
327342
$this->biography = $value;
328343
}
329344

345+
public function setDiscordUsername($value) {
346+
$this->discord_username = $value;
347+
}
348+
330349
public function setFacebookUsername($value) {
331350
$this->facebook_username = $value;
332351
}

src/models/User/Update.php

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class Update extends Model {
2626
public $biography_error;
2727
public $biography_max_len = self::MAX_LEN;
2828

29+
public $discord_username;
30+
public $discord_username_error;
31+
public $discord_username_max_len = self::MAX_LEN;
32+
2933
public $facebook_username;
3034
public $facebook_username_error;
3135
public $facebook_username_max_len = self::MAX_LEN;

src/models/User/View.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class View extends Model {
88

99
public $biography;
1010
public $contributions;
11+
public $discord;
1112
public $documents;
1213
public $facebook;
1314
public $facebook_uri;

src/templates/User/Update.phtml

+40-10
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ switch ($this->getContext()->biography_error[1]) {
6969
. $this->getContext()->biography_error[1];
7070
}
7171

72+
switch ($this->getContext()->discord_username_error[1]) {
73+
case null: case '': $discord_username_error = ''; break;
74+
case 'TOO_LONG':
75+
$discord_username_error = 'Your Discord username is too long.'; break;
76+
case 'CHANGE_SUCCESS':
77+
$discord_username_error = 'Your Discord username was updated.'; break;
78+
default:
79+
$discord_username_error = 'Internal error: '
80+
. $this->getContext()->discord_username_error[1];
81+
}
82+
7283
switch ($this->getContext()->facebook_username_error[1]) {
7384
case null: case '': $facebook_username_error = ''; break;
7485
case 'TOO_LONG':
@@ -267,14 +278,33 @@ require('./header.inc.phtml');
267278
echo '<p>' . $biography_error . '</p>';
268279
} ?>
269280
</section>
281+
<section<?php
282+
if ($discord_username_error) {
283+
echo ' class="' .
284+
$this->getContext()->discord_username_error[0] . '"';
285+
} ?>>
286+
<hr/>
287+
<label for="discord_username">Discord Username:</label>
288+
<input tabindex="6"
289+
maxlength="<?php echo
290+
$this->getContext()->discord_username_max_len; ?>"
291+
type="text" name="discord_username" id="discord_username"
292+
value="<?php
293+
echo filter_var($this->getContext()->discord_username,
294+
FILTER_SANITIZE_FULL_SPECIAL_CHARS);
295+
?>"/>
296+
<?php if ($discord_username_error) {
297+
echo '<p>' . $discord_username_error . '</p>';
298+
} ?>
299+
</section>
270300
<section<?php
271301
if ($facebook_username_error) {
272302
echo ' class="' .
273303
$this->getContext()->facebook_username_error[0] . '"';
274304
} ?>>
275305
<hr/>
276306
<label for="facebook_username">Facebook Username:</label>
277-
<input tabindex="6"
307+
<input tabindex="7"
278308
maxlength="<?php echo
279309
$this->getContext()->facebook_username_max_len; ?>"
280310
type="text" name="facebook_username" id="facebook_username"
@@ -293,7 +323,7 @@ require('./header.inc.phtml');
293323
} ?>>
294324
<hr/>
295325
<label for="github_username">Github Username:</label>
296-
<input tabindex="7"
326+
<input tabindex="8"
297327
maxlength="<?php echo
298328
$this->getContext()->github_username_max_len; ?>"
299329
type="text" name="github_username" id="github_username"
@@ -312,7 +342,7 @@ require('./header.inc.phtml');
312342
} ?>>
313343
<hr/>
314344
<label for="instagram_username">Instagram Username:</label>
315-
<input tabindex="8"
345+
<input tabindex="9"
316346
maxlength="<?php echo
317347
$this->getContext()->instagram_username_max_len; ?>"
318348
type="text" name="instagram_username" id="instagram_username"
@@ -330,7 +360,7 @@ require('./header.inc.phtml');
330360
} ?>>
331361
<hr/>
332362
<label for="phone">Phone Number:</label>
333-
<input tabindex="9" maxlength="<?php echo
363+
<input tabindex="10" maxlength="<?php echo
334364
$this->getContext()->phone_max_len; ?>"
335365
type="text" name="phone" id="phone"
336366
value="<?php
@@ -346,7 +376,7 @@ require('./header.inc.phtml');
346376
} ?>>
347377
<hr/>
348378
<label for="reddit_username">Reddit Username:</label>
349-
<input tabindex="10"
379+
<input tabindex="11"
350380
maxlength="<?php echo
351381
$this->getContext()->reddit_username_max_len; ?>"
352382
type="text" name="reddit_username" id="reddit_username"
@@ -365,7 +395,7 @@ require('./header.inc.phtml');
365395
} ?>>
366396
<hr/>
367397
<label for="skype_username">Skype Username:</label>
368-
<input tabindex="11"
398+
<input tabindex="12"
369399
maxlength="<?php echo
370400
$this->getContext()->skype_username_max_len; ?>"
371401
type="text" name="skype_username" id="skype_username"
@@ -384,7 +414,7 @@ require('./header.inc.phtml');
384414
} ?>>
385415
<hr/>
386416
<label for="steam_id">Steam Id:</label>
387-
<input tabindex="12"
417+
<input tabindex="13"
388418
maxlength="<?php echo
389419
$this->getContext()->steam_id_max_len; ?>"
390420
type="text" name="steam_id" id="steam_id"
@@ -403,7 +433,7 @@ require('./header.inc.phtml');
403433
} ?>>
404434
<hr/>
405435
<label for="twitter_username">Twitter Username:</label>
406-
<input tabindex="13"
436+
<input tabindex="14"
407437
maxlength="<?php echo
408438
$this->getContext()->twitter_username_max_len; ?>"
409439
type="text" name="twitter_username" id="twitter_username"
@@ -422,7 +452,7 @@ require('./header.inc.phtml');
422452
} ?>>
423453
<hr/>
424454
<label for="website">Website:</label>
425-
<input tabindex="14"
455+
<input tabindex="15"
426456
maxlength="<?php echo
427457
$this->getContext()->website_max_len; ?>"
428458
type="text" name="website" id="website"
@@ -436,7 +466,7 @@ require('./header.inc.phtml');
436466
</section>
437467
<section>
438468
<hr/>
439-
<input tabindex="15" type="submit" class="bg-green" value="Update"/>
469+
<input tabindex="16" type="submit" class="bg-green" value="Update"/>
440470
</section>
441471
</form>
442472
<?php } ?>

src/templates/User/View.phtml

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ require("./header.inc.phtml");
5252
</section>
5353
<?php if ($this->getContext()->profiledata) { ?>
5454
<section class="profiledata">
55+
<?php if ($this->getContext()->discord) { ?>
56+
<span><strong>Discord:</strong> <?php echo filter_var($this->getContext()->discord, FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></span>
57+
<?php } ?>
5558
<?php if ($this->getContext()->github) { ?>
5659
<span><strong>GitHub:</strong> <a href="<?php echo $this->getContext()->github_uri; ?>"><?php echo filter_var($this->getContext()->github, FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></a></span>
5760
<?php } ?>

0 commit comments

Comments
 (0)