コード例 #1
0
ファイル: actions.class.php プロジェクト: nazab/Blog-or-Not
 public function executeVote(sfWebRequest $request)
 {
     $this->forward404Unless($request->isMethod('post'));
     $user = $this->getUser();
     $blog_id = $user->getAttribute('blog_id');
     $vote_form = new VoteForm();
     $bind_value = array('blog_id' => $blog_id, 'value' => $request->getParameter('vote'));
     $vote_form->bind($bind_value);
     if ($vote_form->isValid()) {
         $vote = new Vote();
         $vote->setBlog_id($vote_form->getValue('blog_id'));
         $vote->setValue($vote_form->getValue('value'));
         $vote->save();
         // Update vote_SUM
         Doctrine_Query::create()->update('Blog b')->set('b.vote_sum', 'b.vote_sum + ' . $vote_form->getValue('value'))->where('b.id = ?', $vote_form->getValue('blog_id'))->execute();
         // Update vote_COUNT
         Doctrine_Query::create()->update('Blog b')->set('b.vote_count', 'b.vote_count + 1')->where('b.id = ?', $vote_form->getValue('blog_id'))->execute();
         if (sfConfig::get('sf_exclusion_list')) {
             // Exclusion list!
             $session_list = $user->getAttribute('already_voted_list');
             if (is_array($session_list) && count($session_list) > 0) {
             } else {
                 //No data in the array
                 $session_list = array();
             }
             $session_list[] = $vote_form->getValue('blog_id');
             $user->setAttribute('already_voted_list', $session_list);
         }
         // Check if the vote history is empty it it is initiate an empty list
         $vote_history = $user->getAttribute('vote_history');
         if (is_array($vote_history) && count($vote_history) > 0) {
         } else {
             $vote_history = array();
         }
         // Add the just rated blog to the vote_history
         $voted_blog = Doctrine::getTable('Blog')->find($vote_form->getValue('blog_id'));
         $vote_avg = $voted_blog->getVote_sum() / $voted_blog->getVote_count();
         $vote_info = array('my_vote' => $vote_form->getValue('value'), 'vote_avg' => $vote_avg, 'vote_count' => $voted_blog->getVote_count(), 'vote_sum' => $voted_blog->getVote_sum(), 'blog_url' => $voted_blog->getUrl(), 'blog_thmbnail' => $voted_blog->getThumbnail_url());
         array_unshift($vote_history, $vote_info);
         $user->setAttribute('vote_history', $vote_history);
         $this->redirect('blog/index');
     }
 }