bit dojo labs : file explorer

Inspecting: home > misc > sample > sample_symfony.php (download)
<?php

//
// Symfony PHP Web Framework
// Sample code of a Controller
//
// From the Which Project
// http://which.bitdojo.net
//
// Original filename: actions.class.php
//

class mainActions extends sfActions
{
  /**
   * Executes index action
   */
  public function executeIndex()
  {
    
    $past_topic = false;
    
    if($topic_id = $this->getRequestParameter('id'))
    {
      $topic = TopicPeer::retrieveByPK($topic_id);
      if(!$topic) $this->forward404('Unable to find the Topic with ID='.$topic_id);
      $past_topic = true;
    }
    else
    {
      $topic = TopicPeer::getLatest();
    }
    
    $c = new Criteria();
    $c->addDescendingOrderByColumn(CommentPeer::CREATED_AT);
    
  	$comments = $topic->getComments($c);
  	$options = $topic->getSidesArray();
  	
  	foreach($options as $oid => $o)
  	{
  	  $sorted_comments[$oid] = array();
  	  foreach($comments as $c)
  	  {
  	     if ($c->getSideId() == $oid)
  	     {
  	       $sorted_comments[$oid][] = $c;
  	     }
  	  }
  	}
  	
  	$this->topic = $topic;
  	$this->comments = $comments;
  	$this->sorted_comments = $sorted_comments;
  	$this->options = $options; 
  	$this->past_topic = $past_topic;
  	
  	if(!$past_topic)
  	{
  	  $this->vote = new Comment();
  	
  	  //$hasVoted = $this->getUser()->getAttribute('voted_id', -1) == $topic->getId();
  	  $hasVoted = $this->getRequest()->getCookie('voted_id') == $topic->getId();
  	  $this->hasVoted = $hasVoted;
  	  
  	  #$g = new Captcha();
      #$this->getUser()->setAttribute('captcha', $g->generate());
  	  
  	}
  	
  	$e = each($sorted_comments); 
  	$this->comments_side1 = $e['value'];
  	$e = each($sorted_comments);
    $this->comments_side2 = $e['value'];
  	
    //$visitor_country = ip2Country::getInstance()->getCountryCode();
    //$this->visitor_country = $visitor_country; 
    
  }
  
  public function executeVote()
  {
    if ( $this->getRequest()->getMethod() == sfRequest::POST 
         && TopicPeer::getLatest()->getId() == $this->getRequestParameter('topic_id') )
    {
      $comment = new Comment();
      $comment->setTopicId($this->getRequestParameter('topic_id'));
      $comment->setSideId($this->getRequestParameter('side_id'));
      $comment->setAuthor($this->getRequestParameter('author'));
      //$comment->setEmail($this->getRequestParameter('email'));
      $comment->setBody($this->getRequestParameter('body'));
      
      $visitor_ip = ip2Country::getInstance()->getRemoteAddress();
      $visitor_country = ip2Country::getInstance()->getCountryCode();
      
      $comment->setVisitorIp($visitor_ip);
      $comment->setVisitorCountry(strtolower($visitor_country));
      
      $comment->save();
      
      if(!$comment->isNew())
      {
        //$this->getUser()->setAttribute('voted_id', $comment->getTopicId());
        $this->getResponse()->setCookie('voted_id', $comment->getTopicId(), time()+24*60*60);
      }
    }
    $this->redirect('@homepage');
  }
  
  public function executeView(){
    
    if($this->getRequestParameter('id') == TopicPeer::getLatest()->getId())
    {
      $this->redirect('@homepage');
    } 
    else
    {
      $this->forward('main', 'index');
    }
  }
  
  public function executeList()
  {
    $this->topics = TopicPeer::doSelect(new Criteria()); 
  }
  
  public function executeReset(){
    //$this->getUser()->setAttribute('voted_id', -1);
    $this->getResponse()->setCookie('voted_id', -1);
    $this->redirect('@homepage');
  }
  
  public function handleErrorVote()
  {
    if( $this->getRequest()->getMethod() == sfRequest::POST )
    {
      $this->forward('main', 'index');
    }
    else
    {
      $this->redirect('@homepage');
    }
  }
  
  public function executeTest1(){
    
    $visitor_ip = ip2Country::getInstance()->getRemoteAddress();
    $visitor_country = ip2Country::getInstance()->getCountryCode();
    
    if(!$visitor_ip) $visitor_ip = 'unknown';
    if(!$visitor_country) $visitor_country = 'unknown'; 
    
    return $this->renderText('ip='.$visitor_ip.', country='.$visitor_country);
    
  }
}