ProfileController.php 1.9 KB

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