<?php

/**
 * school actions.
 *
 * @package    tvtl
 * @subpackage school
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class schoolActions extends autoschoolActions
{
  function executeImport() {
    if ($this->getRequest()->getMethod() == sfRequest::POST) {
      echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
      ini_set("max_execution_time",3000);
      $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");
      if (ord($strContent[0])==255 && ord($strContent[1])==254)
		      $strContent = $this->unicode_to_utf8($strContent);

      $lines = explode("\r\n", $strContent);
      $counter = 0;
      foreach ($lines as $key => $line) {
        $counter ++;
        $chomInfo = explode("\t", $line);
        $this->message .= "===================================================<br/>";
        $this->message .= $counter . ' ' . $line. '<br/>';
        $parent_id = 0;
        $exsited = false;
        for ($i = 0; $i<3; $i++) {//bo qua column luu thong tin loai truong
          if (trim($chomInfo[3-$i]) != '') {
            $c = new Criteria();
            $c->add(TblblogschoolsPeer::SC_NAME, trim($chomInfo[3-$i]));
            if ($i == 2) {//phan loai truong
              $type = $chomInfo[0];
            } else {//truong hop input tinh, huyen
              $type = $i;
            }
            //$c->add(TblblogschoolsPeer::SC_TYPE, $type);
            $c->add(TblblogschoolsPeer::SC_PARENT, $parent_id);
            $tblblogschool = TblblogschoolsPeer::doSelectOne($c);

            if ($tblblogschool == null) {//chua ton tai
              $tblblogschool = new Tblblogschools();
              $tblblogschool->setScName(trim($chomInfo[3-$i]));
              $tblblogschool->setScType($type);
              $tblblogschool->setScApprove(1);
              $tblblogschool->setScParent($parent_id);
              $tblblogschool->save();
              $parent_id = $tblblogschool->getScId();
              $this->message .= $i . ' success<br/>';
            } else {//update lai type and approved
              //var_dump($tblblogschool); exit();
              $tblblogschool->setScType($type);
              $tblblogschool->setScApprove(1);
              $tblblogschool->save();
              $this->message .= $i . ' exsited and updated ' . $type . '<br/>';
              $parent_id = $tblblogschool->getScId();
            }
          }
        }//for
      }//foreach
      //return sfView::NONE ;
    } else {
      $this->message = '';
    }
  }//executeImport()


  protected function unicode_to_utf8($str) {

  $utf8 = '';

  //foreach( $str as $unicode ) {
  for ($i = 2; $i < strlen( $str ); $i+=2 ) {

      $unicode = ord($str[$i]) + (ord($str[$i+1]) << 8);
      if ( $unicode < 128 ) {

          $utf8.= chr( $unicode );

      } elseif ( $unicode < 2048 ) {

          $utf8.= chr( 192 +  ( ( $unicode - ( $unicode % 64 ) ) / 64 ) );
          $utf8.= chr( 128 + ( $unicode % 64 ) );

      } else {

          $utf8.= chr( 224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
          $utf8.= chr( 128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) / 64 ) );
          $utf8.= chr( 128 + ( $unicode % 64 ) );

      } // if

  } // for

  return $utf8;

  } // unicode_to_utf8

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['sc_province_is_empty']))
    {
      $criterion = $c->getNewCriterion(TblblogschoolsPeer::SC_PROVINCE, '');
      $criterion->addOr($c->getNewCriterion(TblblogschoolsPeer::SC_PROVINCE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['sc_province']) && $this->filters['sc_province'] !== '')
    {
      //$criterion = $c->getNewCriterion(TblblogschoolsPeer::SC_ID, $this->filters['sc_province']);
      //$criterion->addOr($c->getNewCriterion(TblblogschoolsPeer::SC_PARENT, $this->filters['sc_province']));
      //$c->add($criterion);
      $c1 = new Criteria();
      $c1->add(TblblogschoolsPeer::SC_PARENT, $this->filters['sc_province']);
      $c1->add(TblblogschoolsPeer::SC_TYPE, 1);
      $tblblogschools_district = TblblogschoolsPeer::doSelect($c1);
      $district_id = array();
      foreach ($tblblogschools_district as $tblblogschools) {
        $district_id[] = $tblblogschools->getScId();
      }
      $c->add(TblblogschoolsPeer::SC_PARENT, $district_id, Criteria::IN);
      $c->add(TblblogschoolsPeer::SC_TYPE, 2, Criteria::GREATER_EQUAL);
    }
    if (isset($this->filters['sc_district_is_empty']))
    {
      $criterion = $c->getNewCriterion(TblblogschoolsPeer::SC_DISTRICT, '');
      $criterion->addOr($c->getNewCriterion(TblblogschoolsPeer::SC_DISTRICT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['sc_district']) && $this->filters['sc_district'] !== '')
    {
      $c->clear();
      $c->add(TblblogschoolsPeer::SC_PARENT, $this->filters['sc_district']);
      $c->add(TblblogschoolsPeer::SC_TYPE, 2, Criteria::GREATER_EQUAL);
    }
    if (isset($this->filters['sc_school_name_is_empty']))
    {
      $criterion = $c->getNewCriterion(TblblogschoolsPeer::SC_SCHOOL_NAME, '');
      $criterion->addOr($c->getNewCriterion(TblblogschoolsPeer::SC_SCHOOL_NAME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['sc_school_name']) && $this->filters['sc_school_name'] !== '')
    {
      $c->add(TblblogschoolsPeer::SC_NAME, '%'.$this->filters['sc_school_name'].'%', Criteria::LIKE);
    }
  }

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

    foreach ($id_array as $id) {
      //check xem da su dung trong chuc nang xac thuc chua
      $c = new Criteria();
      $c->add(TblblogcheckinfoPeer::CI_SCHOOL, $id);
      $tblblogcheckinfo = TblblogcheckinfoPeer::doCount($c);

      //check xem da su dung trong chuc nang kich hoat volet chua
      $c->clear();
      $c->add(VioletactivePeer::VA_CUSTOMERID, $id);
      $violetactive = VioletactivePeer::doCount($c);

      //check xem co child ko
      $c->clear();
      $c->add(TblblogschoolsPeer::SC_PARENT, $id);
      $haveChild = TblblogschoolsPeer::doCount($c);

      if ($tblblogcheckinfo == 0 && $violetactive == 0 && $haveChild == 0) {
        $tblblogchool = TblblogschoolsPeer::retrieveByPK($id);
        $tblblogchool->delete();
      }
    }
    $this->redirect("school/list?page=$page");
  }
}
