public function loginAction()
 {
     $flash = '';
     if (Identity::isAuthenticated()) {
         return $this->redirect('/followings');
     } else {
         if (isset($_POST['username']) && isset($_POST['password'])) {
             try {
                 Identity::authenticate($_POST['username'], $_POST['password']);
             } catch (\Exception $e) {
                 $flash = $e->getMessage();
             }
             if (Identity::isAuthenticated()) {
                 return $this->redirect('/followings');
             } else {
                 $flash .= "Authentication failed. Please try again.";
             }
         }
     }
     return new ViewModel('user/login.phtml', array('flash' => $flash));
 }
 public function saveAction()
 {
     if (!Identity::isAuthenticated()) {
         return $this->redirect(UserController::ROUTE_LOGIN);
     }
     $followingsTable = $this->getFollowingTable();
     $collection = new FollowingCollection($_POST[self::FOLLOWINGS_FIELDSET_NAME]);
     $user_id = Identity::getId();
     if (!$user_id) {
         throw new \Exception("Error occurred. Unable to fetch signed user id.");
     }
     foreach ($collection as $item) {
         $following_name = $item->getFollowingName();
         if ($item->getFollowingId() && $following_name) {
             $followingsTable->saveFollowing($item, $user_id);
         } else {
             if ($item->getId() && empty($following_name)) {
                 $followingsTable->deleteFollowing($item->getId(), $user_id);
             }
         }
     }
     return $this->redirect('/');
 }