/** * Get the feed list and the posts of the feed we are looking at now * * @return void */ public function indexAction() { $viewData = array(); $flashMessenger = $this->flashMessenger(); $username = $this->params()->fromRoute('username'); $currentFeedId = $this->params()->fromRoute('feed_id'); $userData = ApiClient::getUser($username); if ($userData !== FALSE) { $hydrator = new ClassMethods(); $user = $hydrator->hydrate($userData, new User()); } else { $this->getResponse()->setStatusCode(404); return; } $subscribeForm = new SubscribeForm(); $unsubscribeForm = new UnsubscribeForm(); $subscribeForm->setAttribute('action', $this->url()->fromRoute('feeds-subscribe', array('username' => $username))); $unsubscribeForm->setAttribute('action', $this->url()->fromRoute('feeds-unsubscribe', array('username' => $username))); $hydrator = new ClassMethods(); $response = ApiClient::getFeeds($username); $feeds = array(); foreach ($response as $r) { $feeds[$r['id']] = $hydrator->hydrate($r, new Feed()); } if ($currentFeedId === null && !empty($feeds)) { $currentFeedId = reset($feeds)->getId(); } $feedsMenu = new Navigation(); $router = $this->getEvent()->getRouter(); $routeMatch = $this->getEvent()->getRouteMatch()->setParam('feed_id', $currentFeedId); foreach ($feeds as $f) { $feedsMenu->addPage(AbstractPage::factory(array('title' => $f->getTitle(), 'icon' => $f->getIcon(), 'route' => 'feeds', 'routeMatch' => $routeMatch, 'router' => $router, 'params' => array('username' => $username, 'feed_id' => $f->getId())))); } $currentFeed = $currentFeedId != null ? $feeds[$currentFeedId] : null; if ($currentFeed != null) { $paginator = new Paginator(new ArrayAdapter($currentFeed->getArticles())); $paginator->setItemCountPerPage(5); $paginator->setCurrentPageNumber($this->params()->fromRoute('page')); $viewData['paginator'] = $paginator; $viewData['feedId'] = $currentFeedId; } $unsubscribeForm->get('feed_id')->setValue($currentFeedId); $viewData['subscribeForm'] = $subscribeForm; $viewData['unsubscribeForm'] = $unsubscribeForm; $viewData['username'] = $username; $viewData['feedsMenu'] = $feedsMenu; $viewData['user'] = $user; $viewData['paginator'] = $paginator; $viewData['feedId'] = $currentFeedId; $viewData['feed'] = $currentFeed; $this->layout()->username = $username; if ($flashMessenger->hasMessages()) { $viewData['flashMessages'] = $flashMessenger->getMessages(); } return $viewData; }
/** * Performs an authentication attempt * * @return \Zend\Authentication\Result * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface * If authentication cannot be performed */ public function authenticate() { $result = ApiClient::authenticate(array('username' => $this->username, 'password' => $this->password)); if ($result['result'] === true) { $hydrator = new ClassMethods(); $user = $hydrator->hydrate(ApiClient::getUser($this->username), new User()); $response = new Result(Result::SUCCESS, $user, array('Authentication successful.')); } else { $response = new Result(Result::FAILURE, NULL, array('Invalid credentials.')); } return $response; }
/** * Performs an authentication attempt * * @return \Zend\Authentication\Result * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface * If authentication cannot be performed */ public function authenticate() { $result = ApiClient::authenticate(array('username' => $this->username, 'password' => $this->password)); if (array_key_exists('access_token', $result) && !empty($result['access_token'])) { $hydrator = new ClassMethods(); $user = $hydrator->hydrate(ApiClient::getUser($this->username), new User()); $session = new Container('oauth_session'); $session->setExpirationSeconds($result['expires_in']); $session->accessToken = $result['access_token']; $response = new Result(Result::SUCCESS, $user, array('Authentication successful.')); } else { $response = new Result(Result::FAILURE, NULL, array('Invalid credentials.')); } return $response; }
public function indexAction() { $viewData = array(); $flashMessenger = $this->flashMessenger(); $auth = new AuthenticationService(); $loggedInUser = $auth->getIdentity(); if ($loggedInUser === null) { return; } $username = $this->params()->fromRoute('username'); $userData = ApiClient::getUser($username); if ($userData !== FALSE) { $hydrator = new ClassMethods(); $user = $hydrator->hydrate($userData, new User()); } else { $this->getResponse()->setStatusCode(404); return; } $hydrator = new ClassMethods(); $wallData = ApiClient::getWall($username); $wall = $hydrator->hydrate($wallData, new Wall()); $paginator = new Paginator(new ArrayAdapter($wall->getFeed())); $paginator->setItemCountPerPage(5); $paginator->setCurrentPageNumber($this->params()->fromRoute('page')); //Check if we are submitting content $request = $this->getRequest(); $statusForm = new TextStatusForm(); $imageForm = new ImageForm(); $linkForm = new LinkForm(); $commentForm = new CommentForm(); if ($request->isPost()) { $data = $request->getPost()->toArray(); if (array_key_exists('status', $data)) { $result = $this->createStatus($statusForm, $user, $data); } if (!empty($request->getFiles()->image)) { $data = array_merge_recursive($data, $request->getFiles()->toArray()); $result = $this->createImage($imageForm, $user, $data); } if (array_key_exists('url', $data)) { $result = $this->createLink($linkForm, $user, $data); } if (array_key_exists('comment', $data)) { $result = $this->createComment($commentForm, $loggedInUser, $data); } switch (true) { case $result instanceof TextStatusForm: $statusForm = $result; break; case $result instanceof ImageForm: $imageForm = $result; break; case $result instanceof LinkForm: $linkForm = $result; break; case $result instanceof CommentForm: $commentForm = $result; break; default: if ($result == true) { $flashMessenger->addMessage('New content posted!'); return $this->redirect()->toRoute('wall', array('username' => $user->getUsername())); } else { return $this->getResponse()->setStatusCode(500); } break; } } $statusForm->setAttribute('action', $this->url()->fromRoute('wall', array('username' => $user->getUsername()))); $imageForm->setAttribute('action', $this->url()->fromRoute('wall', array('username' => $user->getUsername()))); $linkForm->setAttribute('action', $this->url()->fromRoute('wall', array('username' => $user->getUsername()))); $commentForm->setAttribute('action', $this->url()->fromRoute('wall', array('username' => $user->getUsername()))); $viewData['user'] = $user; $viewData['textContentForm'] = $statusForm; $viewData['imageContentForm'] = $imageForm; $viewData['linkContentForm'] = $linkForm; $viewData['commentContentForm'] = $commentForm; $viewData['isMyWall'] = !empty($loggedInUser) ? $loggedInUser->getUsername() == $username : false; $viewData['paginator'] = $paginator; if ($flashMessenger->hasMessages()) { $viewData['flashMessages'] = $flashMessenger->getMessages(); } return $viewData; }