<?php

/**
 * home actions.
 *
 * @package    tvtl
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 3335 2007-01-23 16:19:56Z fabien $
 */
class homeActions extends sfMyActions
{
  public function executeIndex() {
    $index = @blogStore::$moduleConfig['config']['index'];
    if (isset($index)) $this->redirect($index);
    else return $this->forward('home', 'show');
  }

  public function executeShow() {
    $blocks = sfConfig::get('sf_blocknocache');
    $this->catItems = blogStore::$catTree->tree;
    $this->cacheType = $this->getUser()->getUserSort().blogStore::getUserStatus();
    $this->timeout = $blocks['category_'.$this->cacheType];
  }
  
  public function executeShowsearch() {
    $ids = explode(',', $this->getRequestParameter('ids'));
    $keys = explode(',', $this->getRequestParameter('keys'));
    $this->entrys = array();
    foreach ($ids as $i => $id) {
      $key = $keys[$i];
      if ($key == 'pr_id') $entry = TblpresentationPeer::retrieveByPK($id);
      if ($key == 'doc_id') $entry = TbldocumentPeer::retrieveByPK($id);
      if ($key == 'entry_id') $entry = TblblogentryPeer::retrieveByPK($id);
      $this->entrys[] = array('id'=>$id, 'entry'=>$entry, 'url'=>"/entry/showglobal?$key=$id");
    }
  }

  public function executeGooglesearch() {
    $keyword = urldecode($this->getRequestParameter('q')).' site:violet.vn inurl:show/entry_id';
    $userIP = $_SERVER['REMOTE_ADDR'];

    // build url for search and init curl object to recognition search results
    $url = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=0&rsz=8&q='.urlencode($keyword).'&userip='.$userIP;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, 'violet.vn');
    $arrResults = json_decode(curl_exec($ch), true);
    curl_close($ch);

    $this->entrys = array();
    foreach ($arrResults['responseData']['results'] as $result){
      if (!preg_match('/entry_id[\/=]([0-9]+)/', urldecode($result['url']), $match)) continue;
      $this->entrys[] = array(
      'entry' => TblblogentryPeer::retrieveByPk($match[1]),
      'url' => '/entry/showglobal?entry_id='.$match[1],
      'detail' => array($result['visibleUrl'], $result['content']));
    }
    $this->glink = "http://google.com.vn/?#q=$keyword";
  }

  public function executeSid() {
    if (blogStore::$blogName!='main') exit();
    $this->setLayout(false);
    $this->ret = urldecode($this->getRequestParameter('ret'));
    $this->ret = str_replace('__', '.', $this->ret);
  }

  public function executeListsubblog() {
    return $this->redirect('/blog/listsubblog');
  }
}
