<?php
class blockComponents extends sfComponents {
 
  public function executeListcategory() {
    $this->filter = blogStore::getParam('modules/listcategory/filter', '/./');
  }

  public function executeLogin() {
  }

  public function executeTopmenu() {
  }

  public function executeWelcome() {
  }

  public function executeFooter() {
  }

  public function executeAdvfloat() {
  }

  public function executeUseronline() {
  }

  public function executeStatistics() {
  }

  public function executeChatboxmain() {
    $this->admin = 0;
    $this->userId = blogStore::$userId;
    $this->tblbloguser = new Tblbloguser();
    $bl_type = blogStore::getBlogStyle();
    $this->blogId = blogStore::$blogId;
    $tbluser = TbluserPeer::retrieveByPK(blogStore::$userId);
    if ($bl_type == 0) {	
      if ($this->userId != 0) {
        $this->tblbloguser = new Tblbloguser();
        $tbluser = TbluserPeer::retrieveByPK(blogStore::$userId);
        $this->role = $tbluser->getUsChecked()? 1: 0;
        $this->admin = $tbluser->getUsAclgroup()? 1: 0;
        if ($this->role == 1) {
          $this->chat = 1;
        } else {
          $this->chat = 0;
          $this->msg = "Bạn chưa được xác thực";
        }
      } else {
        $this->chat = 0;
        $this->msg = "Đăng nhập để tham gia";
      }
    }	else {
      if ($this->userId != 0) {
        $this->tblbloguser = new Tblbloguser();
        $tbluser = TbluserPeer::retrieveByPK(blogStore::$userId);
        $this->role = $tbluser->getUsChecked()? 1: 0;
        $this->admin = blogStore::isAdministator();
        if (sfContext::getInstance()->getUser()->isBlogMember()) {
          $this->chat = 1;
        } else {
          $this->chat = 0;
          $this->msg = "Bạn chưa gia nhập trang này";
        }
      } else {
        $this->chat = 0;
        $this->msg = "Đăng nhập để tham gia";
      }
    }
  }

  /**
   * List recent comments (for each blog)
   *
   */
  public function executeCommentrecent() {
      $c = new Criteria();
      $c->add(TblblogentrycommentPeer::CM_BLOG, blogStore::$blogId);
      $c->addDescendingOrderByColumn(TblblogentrycommentPeer::CM_DATE);
      $c->setLimit(20);
      $this->tblblogentrycomment = TblblogentrycommentPeer::doSelect($c);
  }

  /**
   * List recent comments (for all blogs)
   *
   */
  public function executeAllrecentcomment() {
      $c = new Criteria();
      $c->addDescendingOrderByColumn(TblblogentrycommentPeer::CM_DATE);
      $c->setLimit(20);
      $this->tblblogentrycomment = TblblogentrycommentPeer::doSelect($c);
  }

  public function executeShowpoll() {
  }

  public function executeShowdocimg() {
      $c = new Criteria();
      //get present latest
      $c->add(TblblogentryPeer::ENTRY_BLOG,blogStore::$blogId);
      $c->add(TblblogentryPeer::ENTRY_TYPE,1);
      $c->add(TblblogentryPeer::ENTRY_STATUS,2,Criteria::LESS_THAN);
      $c->addDescendingOrderByColumn(TblblogentryPeer::ENTRY_MODIFYDATE);
      $c->setLimit(20);
      $this->tblblogentry = TblblogentryPeer::doSelect($c);
  }

  public function executeSearch() {
    $this->thishost = blogStore::$blog->getRootUrl('', '');
    $this->dosearch = sfConfig::get('sf_search_function');
  }
  
  public function executeLeftmenu() {
  }

  /**
   * List all material on all blogs
   *
   */
  public function executeRefblogs() {
    $this->title = blogStore::getParam('modules/refblogs/name');
    $this->subPages = $this->params;
    $counts = sfConfig::get('app_thumbnail_dir_count');
    foreach ($this->subPages as $key => $value) {
      $count = $counts[$value['list']];
      if (!$count) continue;

      $cc = new Criteria();
      $cc->add(TblblogentryPeer::ENTRY_BLOG, $value['id']);
      $this->subPages[$key]['count'] = TblblogentryPeer::doCount($cc);

      $c = new Criteria();
      $c->add(TblblogentryPeer::ENTRY_BLOG, $value['id']);
      $c->add(TblblogentryPeer::ENTRY_STATUS, 0);
      $c->addDescendingOrderByColumn(TblblogentryPeer::ENTRY_MODIFYDATE);
      $c->addIndex('blogallindex');
      $c->setLimit($count); 
      $this->subPages[$key]['entries'] = TblblogentryPeer::doSelect($c);
    }
  }

  /**
   * List most active members
   *
   */
  public function executeActivemember() {
    if (!isset($this->active_type)) $this->active_type = 'day';
    $this->title = blogStore::getParam('modules/activemember/name');
    $this->userscores = blogStore::$blog->getObject()->listActiveMembers($this->active_type);
  }

  public function executeActiveblog() {
    if (!isset($this->active_type)) $this->active_type = 'day';
    $this->title = blogStore::getParam('modules/activeblog/name');
    $this->tblblogs = blogStore::$blog->getObject()->listActiveBlogs(2, $this->active_type);
  }

  public function executeActivepblog() {
    if (!isset($this->active_type)) $this->active_type = 'day';
    $this->title = blogStore::getParam('modules/activepblog/name');
    $this->tblblogs = blogStore::$blog->getObject()->listActiveBlogs(1, $this->active_type);
  }

  public function executeGreeting() {
  }

  public function executeSupport() {
    $this->supports = blogStore::getParam('modules/support/items');
  }

  public function executeSortdata() {
    $this->sort = $this->getUser()->getUserSort();
  }

  /**
   * Show reference news from other blog
   *
   */
  public function executeRefnews() {
    $config = blogStore::getParam('modules/refnews');
    if (isset($config)) {
      $blogName = @$config['refblog'];
      $blogHost = @$config['refhost'];

      if (isset($blogName)) {
          $c = new Criteria();
          $c->add(TblblogPeer::BL_SHORTNAME, $blogName);
          $c->setLimit(1);
          $tblblogs = TblblogPeer::doSelect($c);
          if (count($tblblogs)!=1) return sfView::NONE;
          $tblblog = $tblblogs[0];

          $c = new Criteria();
          $c->add(TblblogentryPeer::ENTRY_BLOG, $tblblog->getBlId());
          $c->add(TblblogentryPeer::ENTRY_STATUS, 2, Criteria::LESS_THAN);
          $c->add(TblblogentryPeer::ENTRY_TYPE, 2);
          $c->addDescendingOrderByColumn(TblblogentryPeer::ENTRY_MODIFYDATE);
          $c->setLimit(3);
          $this->tblblogentrys = TblblogentryPeer::doSelect($c);

          if ($blogHost==null) {
            $this->baseLink = '';
          } else {
            $this->baseLink = 'http://' . $blogHost . '/' . $blogName;
          }
      }
    } else {
      return sfView::NONE;
    }
  }

  /**
   * Show statitics chart of blog
   *
   * @return unknown
   */
  public function executeStatchart() {
    $this->type = $this->getRequestParameter('type', 'week');
    $this->view = $this->getRequestParameter('view', 'hit');
    $blogShortNames = $this->getRequestParameter('blog');
    if ($blogShortNames) {
      $blogShortNames = explode(',', urldecode($blogShortNames));
    } else {
      $blogShortNames = array();
    }

    for ($i=0; $i<4; $i++) {
      $more = $this->getRequestParameter('more_blog_'.$i);
      $more = strtolower($more);
      if ($more!=null) {
        if (preg_match('@http://[^/]*/([^/]*)/@', $more, $match)) {
          $more = $match[1];
        } else if (preg_match('@http://([a-z0-9_-]+)\.violet\.vn@', $more, $match)) {
          $more = $match[1];
        } else if (preg_match('@[a-z0-9_-]*@', $more, $match)) {
          $more = $match[0];
        }
        $blogShortNames[] = $more;
      }
    }

    if (!in_array(blogStore::$blogName, $blogShortNames)) $blogShortNames[] = blogStore::$blogName;
    //myUtility::log("Start chart ".join(",",$blogShortNames), "chart.log", "\n");

    $c = new Criteria();
    $c->add(TblblogPeer::BL_SHORTNAME, $blogShortNames, Criteria::IN);
    $blogs = TblblogPeer::doSelect($c);
    $blogMaps = array();
    $ids = array();
    foreach ($blogs as $blog) {
      $ids[strtolower($blog->getBlShortname())] = $blog->getBlId();
      $blogMaps[$blog->getBlId()] = $blog;
    }

    $this->ids = array();
    $this->blogShortNames = array();
    foreach ($blogShortNames as $short_name) {
      if (@$ids[$short_name]!=null) {
        $this->ids[] = $ids[$short_name];
        $this->blogShortNames[] = $short_name;
      }
    }

    //myUtility::log("Finish read blog info ".join(",",$blogShortNames), "chart.log");
    if (count($this->ids)==0) return sfView::NONE;

    $this->tblblog = $blogMaps[$this->ids[0]];
  }

  /**
   * Show content of category
   *
   */
  public function executeCategory() {
    if ($this->catItems == null) {
      $this->catItems = array();
      foreach ($this->params['cat_ids'] as $cat_id) {
        $this->catItems[$cat_id] = blogStore::$catTree->getCatItem($cat_id);
      }
    }
    $this->pagers = array();
    foreach ($this->catItems as $key => $treeItem) {
      if ($treeItem->type == 3) continue;
      $c = new Criteria();
      $c->add(TblblogcategoryentryPeer::CE_CATEGORY, $key);
      $c->add(TblblogcategoryentryPeer::CE_STATUS, 0);
      if ($this->getUser()->getUserSort() == 'download' && $treeItem->type != 2) {
        $c->addDescendingOrderByColumn(TblblogcategoryentryPeer::CE_HIT);
      } else {
        $c->addDescendingOrderByColumn(TblblogcategoryentryPeer::CE_MODIFYDATE);
      }
      $this->pagers[$key] = new PropelPager($c, 'TblblogcategoryentryPeer', 'doSelect', 1, blogStore::getDisplayCount($treeItem->type));
    }
  }

  public function executeAdvbanner() {
    $this->adv_banners = @blogStore::$moduleConfig['adverts'][$this->name];
    if (!isset($this->adv_banners)) return sfView::NONE;
  }

  public function executePreviewadv() {
    $this->adv_banners = @blogStore::$moduleConfig['adverts']['preview_float'];
    if (!isset($this->adv_banners)) return sfView::NONE;
    $expire_time = time()+60*60*24;
    $cookie_name = 'adfloat';
    foreach ($this->adv_banners as $banner) {
      $cookie_name .= @crc32($banner['title']);
      if (@$banner['embed']!=null) $expire_time = time() + intval($banner['embed']);
      break;
    }
    if (isset($_COOKIE[$cookie_name])) return sfView::NONE;
    setcookie($cookie_name, time(), $expire_time);
  }
}
?>
