<?php
class userComponents extends sfComponents {

  public function executeUserinfo() {
    if (!isset($this->linkName)) $this->linkName = false;
    if (!isset($this->getScName)) $this->getScName = 'getScName';
    $this->fields = sfConfig::get('app_'.$this->allow);
    $this->ccuid = blogStore::$userId;
    $this->us_id = $this->tbluser->getUsId();
    $download = $this->tbluser->getUsUpDown();
    $this->numDown = $download[1];
    $this->numUp = $download[2];
    $this->tblblogcheckinfo = $this->tbluser->getTblblogcheckinfo();
  }

  public function executeCheckList() {
    $this->approve = sfContext::getInstance()->getRequest()->getParameter('status', 0);
    if (!$this->tbluser->getUsChecked()) { //dem so nguoi da nho xac thuc
      $c = new Criteria();
      $c->add(TblblogusercheckPeer::UC_USER, $this->tbluser->getUsId());
      $c->add(TblblogusercheckPeer::UC_APPROVE, 0);
      $this->numChecker = TblblogusercheckPeer::doCount($c);
    }
  }

  public function executeSameUser() {
    $this->us_id = $this->getUser()->getUserId();
    $this->tbluser = TbluserPeer::retrieveByPK($this->us_id);
    $this->tblblogcheckinfo = TblblogcheckinfoPeer::retrieveByPK($this->us_id);
    if (!$this->tbluser) $this->forward404Unless($this->tbluser, 'Account does not exists');
    $this->keyword = $this->getRequestParameter('keyword', '');
    //filter theo status: chua xac thuc, da xac thuc
    $this->approve = $this->getRequestParameter('status', 0);
    
    $this->tblusers = null;
    $this->page = $this->getRequestParameter('page', 1);
    $this->isSearch = false;
    $this->isSame = false;
    $this->isInviteCheck = false;
    if ($this->keyword != '') {//tim kiem nhung user da duoc dinh danh
      $c = new Criteria();
      //tim ca username, firtname, lastname
      /*
      $criterion = $c->getNewCriterion(TbluserPeer::US_USERNAME , "%$this->keyword%", Criteria::LIKE);
      $criterion->addOr($c->getNewCriterion(TbluserPeer::US_EMAIL, "%$this->keyword%", Criteria::LIKE));
      $criterion->addOr($c->getNewCriterion(TbluserPeer::US_FIRSTNAME, "%$this->keyword%", Criteria::LIKE));
      $c->add($criterion);
      */
      $c->add(TbluserPeer::US_USERNAME, "$this->keyword");
      $c->add(TbluserPeer::US_CHECKED, 1-$this->tbluser->getUsChecked());
      if ($this->tblblogcheckinfo->getCiPosition() < 40) {
        $c->add(TblblogcheckinfoPeer::CI_POSITION, 40, Criteria::LESS_THAN);
      }
      $this->pager = new PropelPager($c, 'TblblogcheckinfoPeer', 'doSelectJoinTbluser', $this->page, 20);
      $this->isSearch = true;
      $this->currentTab = -1;
    } else {
      if ($this->tbluser->getUsChecked() == 1) {
        $this->type = $this->getRequestParameter('type', 3);
        //list danh sach nhung nguoi can ban dinh danh
        $this->currentTab = 3-$this->type;
        if ($this->type != 3) {
          //list user cung truong, hoac phong hoac so chua duoc dinh danh
          $this->pager = $this->getSameUser($this->tblblogcheckinfo, $this->type);
          $this->tblbloguserchecks = null;
          $this->isSame = true;
        } else {//list nhung nguoi dang nho ban dinh danh
          
          $c = new Criteria();
          $c->add(TblblogusercheckPeer::UC_CHECKER, $this->getUser()->getUserId());
          if ($this->approve !== 'all')
            $c->add(TblblogusercheckPeer::UC_APPROVE, $this->approve);
          $this->pager = new PropelPager($c, 'TblblogusercheckPeer', 'doSelectJoinTbluser', $this->page, 20);
          $this->isInviteCheck = true;
        }
      } else {
        $this->type = $this->getRequestParameter('type', 2);
        $this->currentTab = 2-$this->type;
        if ($this->tblblogcheckinfo->getCiPosition() < 20 && $this->type>1 ) $this->type = 1;
        if ($this->tblblogcheckinfo->getCiPosition() < 10 && $this->type>0 ) $this->type = 0;
        //list user cung truong, hoac phong hoac so da duoc dinh danh
        $this->pager = $this->getSameUser($this->tblblogcheckinfo, $this->type, true);
        $this->tblbloguserchecks = null;
        $this->isSame = true;
      }
    }
  }

  /**
   * Enter description here...
   *
   * @param unknown_type $tbluser
   * @param unknown_type $type
   * @param unknown_type $checked
   * @return unknown
   */
  public function getSameUser($tblblogcheckinfo, $type, $checked = false) {
    /**
     * $type = 0 : cung so giao duc
     * $type = 1 : cung phong giao duc
     * $type = 2 : cung truong
     */
    if ($tblblogcheckinfo) {
      $c = new Criteria();
      switch ($type) {
        case 0:
          $c->add(TblblogcheckinfoPeer::CI_PROVINCE, $tblblogcheckinfo->getCiProvince());
          break;
        case 1:
          $c->add(TblblogcheckinfoPeer::CI_DISTRICT, $tblblogcheckinfo->getCiDistrict());
          break;
        case 2:
          $c->add(TblblogcheckinfoPeer::CI_SCHOOL, $tblblogcheckinfo->getCiSchool());
          break;
      }
      $c->add(TblblogcheckinfoPeer::CI_USERID, $tblblogcheckinfo->getCiUserid(), Criteria::NOT_EQUAL );
      if ($checked) {
        $c->add(TbluserPeer::US_CHECKED , 1);
        if ($tblblogcheckinfo->getCiPosition() < 40) {
          $c->add(TblblogcheckinfoPeer::CI_POSITION, 40, Criteria::LESS_THAN);
        }
      } else {
        $c->add(TbluserPeer::US_CHECKED , 0);
      }
      if ($this->currentTab == 0) {
        $pos = $tblblogcheckinfo->getCiPosition();
        if ($pos < 10) $c->add(TblblogcheckinfoPeer::CI_POSITION, 10, Criteria::LESS_THAN);
        elseif ($pos < 20) {
          $c->add(TblblogcheckinfoPeer::CI_POSITION, 10, Criteria::GREATER_EQUAL);
          $c->add(TblblogcheckinfoPeer::CI_POSITION, 20, Criteria::LESS_THAN);
        }
      }
    }
    $this->page = $this->getRequestParameter('page', 1);
    return new PropelPager($c, 'TblblogcheckinfoPeer', 'doSelectJoinTbluser', $this->page, 20);
  }

  /**
   * Enter description here...
   *
   */
  public function executeScorechart() {
    $this->ids = $this->getRequestParameter('id', blogStore::$userId);
    $this->type = $this->getRequestParameter('type', 'week');
    if (!$this->ids) return sfView::NONE;
    $this->ids = explode(',', $this->ids);
    $this->user_id = $this->ids[0];
    $this->tbluser = TbluserPeer::retrieveByPK($this->user_id);
    if (!$this->tbluser) return sfView::NONE;
  }
}
?>
