0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
IndexController.php
Go to the documentation of this file.
1 <?php
23 require_once('Zend/Auth.php');
24 require_once('Tweet.php');
25 require_once('TweetTable.php');
26 require_once('Profile.php');
27 require_once('User.php');
28 require_once('UserTable.php');
29 
30 class IndexController extends Zend_Controller_Action
31 {
32  public function getSendForm() {
33  require_once('SendForm.php');
34  $request = $this->getRequest();
35  return new SendForm(array(
36  'action' => '/index/send',
37  'method' => 'post',
38  'goto' => $request->getRequestUri(),
39  ));
40  }
41 
42  public function preDispatch() {
43  // not logged in: forward to the login/signup screen
44  if (!Zend_Auth::getInstance()->hasIdentity())
45  $this->_helper->redirector('index', 'login');
46  }
47 
48  public function sentAction() {
49  $this->view->sendForm=$this->getSendForm();
50  return $this->render('index');
51  }
52 
53  // Ajax requests: return the number of tweets in the follow_stream
54  //
55  // This is a bit hackish because die() is intended for serious errors, but
56  // it really works fine for our purpose.
57  public function countAction() {
58  $profile=Zend_Auth::getInstance()->getIdentity();
59  $user=UserTable::load($profile->getId());
60  $newcount=$user->getFollowStreamCount();
61  // make sure that we close the database handles
63  die(''.$newcount);
64  }
65 
66  // show 10 items from the "follow" stream
67  public function indexAction() {
68  $limit=10;
69  $profile=Zend_Auth::getInstance()->getIdentity();
70  $request=$this->getRequest();
71  $this->view->sendForm=$this->getSendForm();
72  $user=UserTable::load($profile->getId());
73  if (!$user)
74  return $this->render('index');
75 
76  $this->view->data=$user->getFollowStream($request->getParam('cutoff'),
77  $limit);
78  if (count($this->view->data)<$limit)
79  $this->view->new_cutoff_time=0;
80  else
81  $this->view->new_cutoff_time=$user->getCutoffTime();
82  return $this->render('index');
83  }
84 
85  // show 10 items from the "my_stream" stream (all tweets of the current user)
86  public function mineAction() {
87  $limit=10;
88  $request=$this->getRequest();
89  $profile=Zend_Auth::getInstance()->getIdentity();
90  $this->view->sendForm=$this->getSendForm();
91  $this->view->show='mine';
92  $user=UserTable::load($profile->getId());
93  if (!$user)
94  return $this->render('index');
95 
96  $this->view->data=$user->getMyStream($request->getParam('cutoff'), $limit);
97  if (count($this->view->data)<$limit)
98  $this->view->new_cutoff_time=0;
99  else
100  $this->view->new_cutoff_time=$user->getCutoffTime();
101  return $this->render('index');
102  }
103 
104  // show 10 users that the current user is following
105  public function followingAction() {
106  $limit=10;
107  $request=$this->getRequest();
108  $profile=Zend_Auth::getInstance()->getIdentity();
109  $this->view->sendForm=$this->getSendForm();
110  $this->view->show='following';
111  $user=UserTable::load($profile->getId());
112  if (!$user)
113  return $this->render('index');
114 
115  $this->view->data=$user->getFollowing($request->getParam('cutoff'), $limit);
116  if (count($this->view->data)<$limit)
117  $this->view->new_cutoff_time=0;
118  else
119  $this->view->new_cutoff_time=$user->getCutoffTime();
120  return $this->render('index');
121  }
122 
123  // show 10 followers
124  public function followersAction() {
125  $limit=10;
126  $request=$this->getRequest();
127  $profile=Zend_Auth::getInstance()->getIdentity();
128  $this->view->sendForm=$this->getSendForm();
129  $this->view->show='followers';
130  $user=UserTable::load($profile->getId());
131  if (!$user)
132  return $this->render('index');
133 
134  $this->view->data=$user->getFollowers($request->getParam('cutoff'), $limit);
135  if (count($this->view->data)<$limit)
136  $this->view->new_cutoff_time=0;
137  else
138  $this->view->new_cutoff_time=$user->getCutoffTime();
139  return $this->render('index');
140  }
141 
142  // send a tweet
143  public function sendAction() {
144  $request = $this->getRequest();
145 
146  // Check if we have a POST request
147  if (!$request->isPost())
148  return $this->_helper->redirector('index', 'index');
149 
150  // Get our form and validate it
151  $form = $this->getSendForm();
152  if (!$form->isValid($request->getPost())) {
153  $this->view->form = $form;
154  $val=$form->getValues();
155  return $this->_helper->redirector->gotoUrl($val['goto']);
156  }
157 
158  $val=$form->getValues();
159 
160  // create a tweet and store it in the Database
161  $profile=Zend_Auth::getInstance()->getIdentity();
162  $t=new Tweet();
163  $t->setMessage($val['message']);
164 
165  TweetTable::store($t, $profile->getId());
166 
167  // then redirect to the previous page
168  return $this->_helper->redirector->gotoUrl($val['goto']);
169  }
170 
171 }
172 
store($tweet, $author)
Definition: TweetTable.php:47
Copyright (C) 2007-2015 Hypertable, Inc.
Definition: SendForm.php:24
load($userid)
Definition: UserTable.php:31
Copyright (C) 2007-2015 Hypertable, Inc.
Definition: Tweet.php:24
Copyright (C) 2007-2015 Hypertable, Inc.