0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ProfileController.php
Go to the documentation of this file.
1 <?
24 require_once('Zend/Auth.php');
25 require_once('ProfileTable.php');
26 require_once('Profile.php');
27 require_once('UserTable.php');
28 require_once('User.php');
29 
30 class ProfileController extends Zend_Controller_Action
31 {
32  public function getProfileForm() {
33  require_once('ProfileForm.php');
34  return new ProfileForm(array(
35  'action' => '/profile/process',
36  'method' => 'post',
37  ));
38  }
39 
40  public function preDispatch() {
41  // if user is not logged in: redirect to /index/index
42  if (!Zend_Auth::getInstance()->hasIdentity())
43  $this->_helper->redirector('index', 'index');
44  }
45 
46  public function indexAction() {
47  $profile=Zend_Auth::getInstance()->getIdentity();
48  $user=UserTable::load($profile->getId());
49  $this->view->username=$profile->getId();
50  $this->view->form=$this->getProfileForm();
51 
52  // get number of followers
53  $this->view->followers_count=$user->getFollowersCount();
54  // get most recent followers (max 5)
55  $this->view->followers=$user->getFollowers(0, 5);
56 
57  // get number of users i am following
58  $this->view->following_count=$user->getFollowingCount();
59  // get most recent followings (max 5)
60  $this->view->following=$user->getFollowing(0, 5);
61 
62  // get number of my tweets
63  $this->view->my_stream_count=$user->getMyStreamCount();
64  // get my most recent tweets (max 5)
65  $this->view->my_stream=$user->getMyStream(0, 5);
66  }
67 
68  public function processAction() {
69  $profile=Zend_Auth::getInstance()->getIdentity();
70  $request = $this->getRequest();
71  $this->view->username=$profile->getId();
72 
73  // Check if we have a POST request
74  if (!$request->isPost())
75  return $this->_helper->redirector('index', 'index');
76 
77  // Get our form and validate it
78  $form = $this->getProfileForm();
79  if (!$form->isValid($request->getPost())) {
80  $this->view->form = $form;
81  return $this->render('index');
82  }
83 
84  // store the values in the profile
85  $ar=$form->getValues();
86 
87  // just a sanity check
88  if ($ar['username']!=$profile->getId())
89  die("This should never happen...");
90 
91  if ($ar['password'])
92  $profile->setPasswordPlain($ar['password']);
93  if ($ar['display_name']!=$profile->getDisplayName())
94  $profile->setDisplayName($ar['display_name']);
95  if ($ar['email']!=$profile->getEmail())
96  $profile->setEmail($ar['email']);
97  if ($ar['location']!=$profile->getLocation())
98  $profile->setLocation($ar['location']);
99  if ($ar['webpage']!=$profile->getWebpage())
100  $profile->setWebpage($ar['webpage']);
101  if ($ar['avatar']!=$profile->getAvatar())
102  $profile->setAvatar($ar['avatar']);
103 
104  ProfileTable::store($profile);
105  $form->setDescription('Your profile was saved.');
106  $this->view->form = $form;
107  return $this->render('index');
108  }
109 
110  public function showAction() {
111  $request=$this->getRequest();
112  $other_id=basename($request->getRequestUri());
113  $other_profile=ProfileTable::load($other_id);
114  $this->view->profile=$other_profile;
115 
116  // check if we are following this user
117  $profile=Zend_Auth::getInstance()->getIdentity();
118  $user=UserTable::load($profile->getId());
119  if ($user->isFollowing($other_id))
120  $this->view->is_following=true;
121  else
122  $this->view->is_following=false;
123 
124  $other_user=UserTable::load($other_id);
125 
126  // get number of followers
127  $this->view->followers_count=$other_user->getFollowersCount();
128  // get most recent followers (max 5)
129  $this->view->followers=$other_user->getFollowers(0, 5);
130 
131  // get number of users i am following
132  $this->view->following_count=$other_user->getFollowingCount();
133  // get most recent followings (max 5)
134  $this->view->following=$other_user->getFollowing(0, 5);
135 
136  // get number of my tweets
137  $this->view->my_stream_count=$other_user->getMyStreamCount();
138  // get my most recent tweets (max 5)
139  $this->view->my_stream=$other_user->getMyStream(0, 5);
140  }
141 
142  public function followAction() {
143  $request=$this->getRequest();
144  $other=basename($request->getRequestUri());
145 
146  // load my own profile and user settings
147  $profile=Zend_Auth::getInstance()->getIdentity();
148  $user=UserTable::load($profile->getId());
149 
150  // check if we're already following this user; if yes, then return
151  // to the frontpage
152  if ($user->isFollowing($other))
153  return $this->_helper->redirector('index', 'index');
154 
155  // otherwise add 'other' as a follower and reload the current page
156  $user->follow($other);
157  return $this->_helper->redirector->gotoUrl("/profile/show/$other");
158  }
159 
160  public function unfollowAction() {
161  $request=$this->getRequest();
162  $other=basename($request->getRequestUri());
163 
164  // load my own profile and user settings
165  $profile=Zend_Auth::getInstance()->getIdentity();
166  $user=UserTable::load($profile->getId());
167 
168  // check if we're following this user; if not, then return
169  // to the frontpage
170  if (!$user->isFollowing($other)) {
171  return $this->_helper->redirector('index', 'index');
172  }
173 
174  // otherwise remove 'other' from the follower list
175  $user->unfollow($other);
176  return $this->_helper->redirector->gotoUrl("/profile/show/$other");
177  }
178 }
179 
180 ?>
Copyright (C) 2007-2015 Hypertable, Inc.
Definition: ProfileForm.php:24
load($userid)
Definition: UserTable.php:31
store($profile)
Copyright (C) 2007-2015 Hypertable, Inc.