<?php
// auto-generated by sfPropelCrud
// date: 2008/06/30 12:17:49
?>
<?php

/**
 * content actions.
 *
 * @package    tvtl
 * @subpackage content
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 3335 2007-01-23 16:19:56Z fabien $
 */
class blockActions extends sfMyActions
{
  public function executeUpdatepoll() {
    $this->choices = $this->getRequestParameter('choice');
    if($this->choices != null) {
      foreach($this->choices as $choice) {
        $this->tblblogpolldata = new Tblblogpolldata();
        $this->tblblogpolldata->setPdChoice($choice);
        $this->tblblogpolldata->setPdUser(blogStore::$userId);
        $this->tblblogpolldata->setPdIp($_SERVER['REMOTE_ADDR']);
        $this->tblblogpolldata->setPdPoll($this->getRequestParameter('poll_id'));
        $this->tblblogpolldata->setPdCreatedate(date("Y-m-d H:i:s"));
        $this->tblblogpolldata->save();
      }
    }
    $this->getUser()->clearVote();
    $this->redirect($this->getRequest()->getReferer());
  }

  public function executeResultpoll() {
    $this->poll_id = $this->getRequestParameter('poll_id');
    $this->forward404Unless($this->poll_id != null);
    $this->blogId = blogStore::$blogId;
    $this->result = array();

    $this->tblblogpoll = TblblogpollPeer::retrieveByPK($this->poll_id);
    if($this->tblblogpoll) {
      $questions = $this->tblblogpoll->getPollQuestions();
      $arrQuestion = split('\|',$questions);
      $this->total = 0;
      foreach ($arrQuestion as $key=>$question) {
        $c = new Criteria();
        $c->add(TblblogpolldataPeer::PD_CHOICE,$key);
        $c->add(TblblogpolldataPeer::PD_POLL,$this->poll_id);
        $quantity = TblblogpolldataPeer::doCount($c);
        $this->total += $quantity;
        $this->result[$key]['quantity'] = $quantity;
        $this->result[$key]['name'] = $question;
      }
    }
  }

  /**
   * Ajax blocks update
   */
  public function getActiveObject($obj_name) {
    sfLoader::loadHelpers('Partial');
    sfLoader::loadHelpers('tvtl');
    $type = $this->getRequestParameter('active_type', 'day');
    $id = ($type != 'day')? "_$type" : '';
    $blocks = sfConfig::get('sf_blocknocache');
    $timeout = @$blocks[$obj_name.$id];
    $param = array('active_type' => $type);
    return $this->renderText(cache_include_component('block', $obj_name, $param, $timeout, $id));
  }

  public function executeActivemember() {
    return $this->getActiveObject('activemember');
  }

  public function executeActiveblog() {
    return $this->getActiveObject('activeblog');
  }

  public function executeActivepblog() {
    return $this->getActiveObject('activepblog');
  }

  public function executeChart() {
  }

  /**
   * List all user score (1,2,3,4 users)
   *
   */
  public function executeStatchart() {
    $this->ids = $this->getRequestParameter('id', blogStore::$userId);
    if (!$this->ids) return sfView::NONE;
    $this->ids = explode(',', urldecode($this->ids));
    $this->type = $this->getRequestParameter('type', 'week');
    $this->view = $this->getRequestParameter('view', 'hit');

    //myUtility::log("Show flash of ".join(",", $this->ids), "chart.log");
    $blogs = TblblogPeer::retrieveByPKs($this->ids);
    $blogNames = array();
    $this->ids = array();
    foreach ($blogs as $blog) {
      $blogNames[$blog->getBlId()] = $blog->getBlShortname();
      $this->ids[] = $blog->getBlId();
    }

    $flashChart = new sfFlashMultiChart($this->ids, $blogNames);

    switch ($this->type) {
      case 'week':
      case 'month':
        if ($this->type=='week')
        $week = date('Y-m-d', strtotime('-7 day'));
        else
        $week = date('Y-m-d', strtotime('-1 month'));
        $today = date('Y-m-d');
        $c = new Criteria();
        if (count($this->ids)==1)
        $c->add(TblblogstatdayPeer::STD_BLOG, $this->ids[0]);
        else
        $c->add(TblblogstatdayPeer::STD_BLOG, $this->ids, Criteria::IN);
        $c->add(TblblogstatdayPeer::STD_DATE, $week, Criteria::GREATER_EQUAL);
        $c->addAscendingOrderByColumn(TblblogstatdayPeer::STD_DATE);
        $tblblogstatdays = TblblogstatdayPeer::doSelect($c);
        foreach ($tblblogstatdays as $tblblogstatday) {
          if ($this->view == 'page')
          $flashChart->addData($tblblogstatday->getStdBlog(),
          $tblblogstatday->getStdDate('d-m'), $tblblogstatday->getStdPageviews());
          else
          $flashChart->addData($tblblogstatday->getStdBlog(),
          $tblblogstatday->getStdDate('d-m'), $tblblogstatday->getStdVisitors());
        }
        break;
      case 'all':
        break;
    }

    return $this->renderText( $flashChart->render() );
  }

  public function executeAdvancesearch() {
    $host = blogStore::$blog->getBlHost();
    if (!$host) $host = sfConfig::get('app_base_host') . '/' . blogStore::$blogName;
    $this->hosts = '"site:'.$host.'"';
    return sfView::SUCCESS;
  }
}
