Skip to content

Commit 566c112

Browse files
committed
Add changing user profile biography
1 parent c31e3a0 commit 566c112

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

src/controllers/User/Update.php

+35
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use \BNETDocs\Libraries\Authentication;
66
use \BNETDocs\Libraries\EventTypes;
7+
use \BNETDocs\Libraries\Exceptions\UserProfileNotFoundException;
78
use \BNETDocs\Libraries\Logger;
89
use \BNETDocs\Libraries\User;
10+
use \BNETDocs\Libraries\UserProfile;
911
use \BNETDocs\Models\User\Update as UserUpdateModel;
1012

1113
use \CarlBennett\MVC\Libraries\Common;
@@ -43,6 +45,18 @@ public function &run(Router &$router, View &$view, array &$args) {
4345
$model->display_name = Authentication::$user->getDisplayName();
4446
$model->display_name_error = [null, null];
4547

48+
try {
49+
$model->profile = new UserProfile( Authentication::$user->getId() );
50+
} catch (UserProfileNotFoundException $e) {
51+
$model->profile = null;
52+
}
53+
54+
if ( $model->profile ) {
55+
56+
$model->biography = $model->profile->getBiography();
57+
58+
}
59+
4660
// process request
4761

4862
if ($router->getRequestMethod() == 'POST') {
@@ -65,6 +79,10 @@ public function &run(Router &$router, View &$view, array &$args) {
6579
isset($data['display_name']) ? $data['display_name'] : null
6680
);
6781

82+
$model->biography = (
83+
isset($data['biography']) ? $data['biography'] : null
84+
);
85+
6886
// process input
6987

7088
if ($model->username !== Authentication::$user->getUsername()) {
@@ -164,6 +182,21 @@ public function &run(Router &$router, View &$view, array &$args) {
164182

165183
}
166184

185+
$profile_changed = false;
186+
187+
if ($model->biography !== $model->profile->getBiography()) {
188+
189+
// biography change request
190+
191+
$model->profile->setBiography($model->biography);
192+
$profile_changed = true;
193+
194+
}
195+
196+
if ($profile_changed) {
197+
$model->profile->save();
198+
}
199+
167200
Logger::logEvent(
168201
EventTypes::USER_EDITED,
169202
Authentication::$user->getId(),
@@ -177,6 +210,8 @@ public function &run(Router &$router, View &$view, array &$args) {
177210
'email_1' => $model->email_1,
178211
'email_2' => $model->email_2,
179212
'display_name' => $display_name,
213+
'profile_changed' => $profile_changed,
214+
'biography' => $model->biography,
180215
])
181216
);
182217

src/libraries/UserProfile.php

+66
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,70 @@ public function refresh() {
252252
return false;
253253
}
254254

255+
public function save() {
256+
if (!isset(Common::$database)) {
257+
Common::$database = DatabaseDriver::getDatabaseObject();
258+
}
259+
try {
260+
$stmt = Common::$database->prepare('
261+
UPDATE
262+
`user_profiles`
263+
SET
264+
`biography` = :bio,
265+
`facebook_username` = :fb,
266+
`github_username` = :github,
267+
`instagram_username` = :ig,
268+
`phone` = :ph,
269+
`reddit_username` = :reddit,
270+
`skype_username` = :skype,
271+
`steam_id` = :steam,
272+
`twitter_username` = :twitter,
273+
`website` = :website
274+
WHERE
275+
`user_id` = :id
276+
LIMIT 1;
277+
');
278+
$stmt->bindParam(':bio', $this->biography, PDO::PARAM_STR);
279+
$stmt->bindParam(':fb', $this->facebook_username, PDO::PARAM_STR);
280+
$stmt->bindParam(':github', $this->github_username, PDO::PARAM_STR);
281+
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
282+
$stmt->bindParam(':ig', $this->instagram_username, PDO::PARAM_STR);
283+
$stmt->bindParam(':ph', $this->phone, PDO::PARAM_STR);
284+
$stmt->bindParam(':reddit', $this->reddit_username, PDO::PARAM_STR);
285+
$stmt->bindParam(':skype', $this->skype_username, PDO::PARAM_STR);
286+
$stmt->bindParam(':steam', $this->steam_id, PDO::PARAM_STR);
287+
$stmt->bindParam(':twitter', $this->twitter_username, PDO::PARAM_STR);
288+
$stmt->bindParam(':website', $this->website, PDO::PARAM_STR);
289+
if (!$stmt->execute()) {
290+
throw new QueryException('Cannot save user profile');
291+
}
292+
$stmt->closeCursor();
293+
294+
$object = new StdClass();
295+
$object->biography = $this->biography;
296+
$object->facebook_username = $this->facebook_username;
297+
$object->github_username = $this->github_username;
298+
$object->id = $this->id;
299+
$object->instagram_username = $this->instagram_username;
300+
$object->phone = $this->phone;
301+
$object->reddit_username = $this->reddit_username;
302+
$object->skype_username = $this->skype_username;
303+
$object->steam_id = $this->steam_id;
304+
$object->twitter_username = $this->twitter_username;
305+
$object->website = $this->website;
306+
307+
$cache_key = 'bnetdocs-userprofile-' . $this->id;
308+
Common::$cache->set($cache_key, serialize($object), 300);
309+
310+
return true;
311+
} catch (PDOException $e) {
312+
throw new QueryException('Cannot save user profile', $e);
313+
}
314+
return false;
315+
}
316+
317+
public function setBiography($value) {
318+
$this->biography = $value;
319+
}
320+
255321
}

src/models/User/Update.php

+5
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ class Update extends Model {
1818
public $username_error;
1919
public $username_max_len;
2020

21+
public $profile;
22+
23+
public $biography;
24+
public $biography_max_len = 255; // table design: varchar(255)
25+
2126
}

src/templates/User/Update.phtml

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ require('./header.inc.phtml');
140140
echo '<p>' . $display_name_error . '</p>';
141141
} ?>
142142
</section>
143+
<section>
144+
<hr/>
145+
<label for="biography">Biography:</label>
146+
<textarea maxlength="<?php echo $this->getContext()->biography_max_len; ?>" name="biography" id="biography"><?php echo filter_var($this->getContext()->biography, FILTER_SANITIZE_FULL_SPECIAL_CHARS); ?></textarea>
147+
</section>
143148
<section>
144149
<hr/>
145150
<input tabindex="5" type="submit" class="bg-green" value="Update"/>

0 commit comments

Comments
 (0)