ProfileController.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\controllers;
  3. use app\models\User;
  4. use \lithium\security\Auth;
  5. class ProfileController extends \lithium\action\Controller {
  6. public $publicActions = array("view");
  7. public function view($username)
  8. {
  9. //Find the user profile
  10. $user = User::find('first', array('conditions' => compact('username')));
  11. //If the user variable isn't empty a user was found.
  12. if (!empty($user))
  13. {
  14. //The only issue(?) is that this will update the profile views even if the user views their own profile, maybe we should fix that.
  15. $user->incrementViews();
  16. $photo = null;
  17. $profile = $user->profile;
  18. return compact('user', 'photo', 'profile');
  19. }
  20. else
  21. {
  22. //Tell the user that user wasn't found.
  23. }
  24. }
  25. public function create()
  26. {
  27. }
  28. public function edit($username)
  29. {
  30. $user = Auth::check('default');
  31. if ($user) {
  32. $user = User::find('first', array('conditions' => array('username' => $user['username'])));
  33. $profile = $user->profile;
  34. $photo;
  35. if (empty($this->request->data)) {
  36. return compact('profile', 'user');
  37. }
  38. //If a photo was uploaded
  39. if (isset($this->request->data['photo']))
  40. {
  41. /* Update / Add the profile picture */
  42. //Store the image in grid FS
  43. $photo = Photo::create($this->request->data["photo"]);
  44. //We don't to resave the photo in the profile data
  45. unset($this->request->data["photo"]);
  46. //Save the photo
  47. $photo->save();
  48. //Since images are accessed via /image/mongoid we just store the mongo id so we
  49. //can substitue it in the <img src=""> tag.
  50. $user->profilepic = $photo->_id->__toString();
  51. }
  52. $user->profile = $this->request->data;
  53. if ($user->save(null, array('validate' =>false))) {
  54. return $this->redirect("/profile/view/$user->username");
  55. }
  56. }
  57. }
  58. }