<?php

/**
 * user actions.
 *
 * @package    tvtl
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class userActions extends autouserActions
{
	public function executeDocuments(){
		$this->redirect('document/list?filter=filter&filters[doc_user]='.$this->getRequestParameter('us_id'));
	}

	/**
	 * admin create account for other user
	 *
	 */
	public  function executeCreate() {
	    if ($this->getRequest()->getMethod() == sfRequest::POST){
	        $username = $this->getRequestParameter('username');
	        $password = $this->getRequestParameter('password');
	        $email = $this->getRequestParameter('email');
	        $group = $this->getRequestParameter('group');
	        $school = $this->getRequestParameter('school', '');
          $firstname = $this->getRequestParameter('firstname', '');
          $checked = $this->getRequestParameter('checked', 0);
            //$this->setAttribute('username', $username, 'subscriber');

	        //check username and email
	        $c = new Criteria();
	        $c->add(TbluserPeer::US_USERNAME ,$username);
	        $checkUsername = TbluserPeer::doSelectOne($c);

	        //check email
	        $c->clear();
	        $c->add(TbluserPeer::US_EMAIL, $email);
	        $checkEmail = TbluserPeer::doSelectOne($c);

          $user = new Tbluser();
	        $user->setUsUsername($username);
	        $user->setUsPassword(md5($password));
	        $user->setUsEmail($email);
	        $user->setUsAclgroup($group);
	        $user->setUsSchool($school);
	        $user->setUsFirstname($firstname);
	        $user->setUsLastname('');
	        $user->setUsStatus(1); //active a account when admin create this account
	        $user->setUsChecked($checked);
	        $user->save();
	        //send mail to new member
	        $mail = new myMail();

            $mail->addAddress($user->getUsEmail());
            $mail->setFrom('tvtl@bachkim.vn');
            $mail->setSubject('Thu vien tu lieu: Confirm email');

            $mail->setPriority(1);
            //$mail->addEmbeddedImage(SF_WEB_DIR.'/images/askeet_logo.gif', 'CID1', 'Askeet Logo', 'base64', 'image/gif');

            $controller = sfContext::getInstance()->getController();
            $link = $controller->genUrl('/user/confirm?active=' . md5($user->getUsId()), true);
            $mail->setBody(
              'Wellcome to Thu vien tu lieu <br />' .
              "Username: ".$user->getUsUsername(). "<br/>".
              "Password: $password <br />".
              'Please click on this link to activate your account <br />' .
              "<a href='$link'>$link</a>"
            );

            $this->mail = $mail;
            $mail->send();

	        $this->setFlash('notice', 'Your modifications have been saved');
	        $this->redirect("user/edit?us_id=".$user->getUsId());

	    }//if

	}

    public function handleError()
    {
        return sfView::ERROR;
    }

    public function handleErrorCreate()
    {
    	return sfView::SUCCESS;
    }

    public  function executePermision(){
        if ($this->getRequest()->getMethod() == sfRequest::POST){
            $username = $this->getRequestParameter('username');
            $subject = $this->getRequestParameter('subject');

            $c = new Criteria();
            $c->add(TblcategoryPeer::CG_ID,$subject);
            $tblcategory = TblcategoryPeer::doSelectOne($c);
            $cg_id = $tblcategory->getCgId();

            $c = new Criteria();
            $c->add(TbluserPeer::US_ID, $username);
            $tbluser = TbluserPeer::doSelectOne($c);
            $us_id = $tbluser->getUsId();

            $c = new Criteria();
            $c->add(TblcategorymodPeer::CM_CATEGORY, $cg_id);
            $c->add(TblcategorymodPeer::CM_MOD, $us_id);
            $tblcategorymod = TblcategorymodPeer::doSelect($c);
            if ($tblcategorymod == null) {
              $cm = new Tblcategorymod();
              $cm->setCmCategory($cg_id);
              $cm->setCmMod($us_id);
              $cm->save();
              //update permision
              $this->setFlash('notice', 'Your modifications have been saved');
            }else {
              $this->setFlash('notice', 'Your modifications already created');
            }
        }//if
        else {
        }//else
    }

    public function executeDel() {
      $id_list = $this->getRequestParameter('idList');
      $page = $this->getRequestParameter('page');
      $id_array = explode(",", $id_list);

      $c = new  Criteria();
      $c->add(TbluserPeer::US_ID , $id_array, Criteria::IN );
      $fileUnlinks = TbluserPeer::doSelect($c);
      //var_dump($fileUnlink);
      if(TbluserPeer::doDelete($c)) {
          $this->redirect("user/list?page=$page");
      }
      else {
          $this->getRequest()->setError('delete', "System error: count not delete");
          return $this->forward('user', 'list');
      }
      //return sfView::NONE ;


  }

  public function executeCheck() {
    $id_list = $this->getRequestParameter('idList');
    $page = $this->getRequestParameter('page');
    $id_array = explode(",", $id_list);

    $c = new  Criteria();
    $c->add(TbluserPeer::US_ID , $id_array, Criteria::IN );
    $fileUnlinks = TbluserPeer::doSelect($c);
    //var_dump($fileUnlink);
    if(TbluserPeer::doDelete($c)) {
      $this->redirect("user/list?page=$page");
    }
    else {
      $this->getRequest()->setError('delete', "System error: count not delete");
      return $this->forward('user', 'list');
    }
  }

  protected function updateTbluserFromRequest()
  {
    $tbluser = $this->getRequestParameter('tbluser');
    $fullname = $this->getRequestParameter('fullname');

    $name = split(' ',$fullname);
    $firstname = array_pop($name);
    $lastname = join($name, ' ');
    $this->tbluser->setUsFirstname($firstname);
    $this->tbluser->setUsLastname($lastname);

    if (isset($tbluser['us_username']))
    {
      $this->tbluser->setUsUsername($tbluser['us_username']);
    }
    if (isset($tbluser['us_email']))
    {
      $this->tbluser->setUsEmail($tbluser['us_email']);
    }
    if ($tbluser['us_aclgroup']!='')
    {
      $this->tbluser->setUsAclgroup($tbluser['us_aclgroup']);
    }
    if (isset($tbluser['us_rating']))
    {
      $this->tbluser->setUsRating($tbluser['us_rating']);
    }
    if (isset($tbluser['us_status']))
    {
      $this->tbluser->setUsStatus($tbluser['us_status']);
    }
    if (isset($tbluser['us_checked']))
    {
      $this->tbluser->setUsChecked($tbluser['us_checked']);
    }
  }

  /*Add by ManhTL From 2008-08025*/
  public function executeFindFromCsv() {
	  ini_set("max_execution_time",3000);
	  if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			// display the form
			$this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
			$this->reports = '';
		}
		else {
		  $dir = sfConfig::get('sf_upload_dir');
		  //handle the form submit
		  if(!is_dir("$dir/temp")){
		    mkdir("$dir/temp");
		    chmod("$dir/temp", 777);
		  }

		  if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('createbatch')) {
        $filename = $this->getRequest()->getFileName('createbatch');
        @move_uploaded_file($this->getRequest()->getFileValue('createbatch', 'tmp_name'), "$dir/temp/$filename");
		  }

	    $strContent = file_get_contents("$dir/temp/$filename");
	    $lines = explode("\r\n", $strContent);

	    foreach ($lines as $id => $line) {
        if($id == 0)
          continue;
	      if($line == "\r\n")
          continue;
        $values = explode(",", $line);
        $this->counter = 0;
        foreach ($values as $key => $value) {
          if($key == 0) {
            preg_match('@_(.*)_\.eml@', $value, $email);
            //var_dump($email[1]); exit();
            if (count($email)>1) {
              $c = new Criteria();
              $c->add(TbluserPeer::US_EMAIL, $email[1]);
              $tbluser = TbluserPeer::doSelectOne($c);
              //var_dump($tbluser); exit();
              if ($tbluser) {
                $tbluser->setUsStatus(1);
                $tbluser->save();
                $this->counter ++;
              }
            }
          }
        }//foreach
	    }
      //var_dump($username); exit();
	    // pager
      /*$this->pager = new sfPropelPager('Tbluser', 20);
      $c = new Criteria();
      $c->add(TbluserPeer::US_USERNAME ,$username, Criteria::IN );
      $this->addSortCriteria($c);
      $this->addFiltersCriteria($c);
      $this->pager->setCriteria($c);
      $this->pager->setPage($this->getRequestParameter('page', 1));
      $this->pager->init();

	    $this->setTemplate('list');
	    */
		}//else
	}
  /*End ManhTL*/
}
