<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Import extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        //$this->permission->is_admin();
        //$this->output->enable_profiler(TRUE);
        // $this->load->model('Area_model');
        $this->load->model('Import_model');
        // $this->load->model('InfoContents_model');
         $this->load->model('InfoStructures_model');
        $this->load->library('simple_html_dom_lib');
    }

    public function index()
    {
        //@set_time_limit(0);
        ini_set('max_execution_time', '0');
        // $this->Import_model->import_from_other_database(69690,63564);
        //$this->Import_model->import_clear();
        //$this->Import_model->import_info();
        //$this->Import_model->import_xxoo();
        //$this->Import_model->update_root();
        /*$data = $this->Import_model->import_test();
        foreach ($data as $item)
        {
             for ($i = 0; $i <= $item->is_level; $i++)
             {
                 echo '&nbsp;&nbsp;';
             }
             echo $item->ic_url_title . '<br/>';
        }*/
    }
    
    public function html_to_database($sitecode)
    {
        ini_set('max_execution_time', '0');
        $file_array=$this->Import_model->not_done_html_info($sitecode);
        if (empty($file_array)) {
            echo 'HTML文件都处理完毕!';
            return;
        }
        
        foreach ($file_array as $item) {
            $h_id=$item->h_id;
            $file=$item->h_file;
            $h_done=1;
            //$file='f:\asiahighlights\myanmar\top-restaurants';
            if (!is_file($file)) {
                echo '文件不存在:'.$file;
                continue;
            }
            $html_object=file_get_html($file);
            if (empty($html_object)) {
                echo '文件加载失败:'.$file;
                continue;
            }
			$h_source=$html_object->outertext;
            $h_content=$html_object->find('div[id=mainContentRight]', 0);
            if (empty($h_content)) {
                $h_content=$html_object->find('div[id=content]', 0);
            }
			if (empty($h_content)) {
			    $h_content=$html_object->find('div.Information', 0);
			}
			if (empty($h_content)) {
			    $h_content=$html_object->find('div[id=content_main]', 0);
			}
            if (empty($h_content)) {
                $h_content=$html_object->find('div[id=mainContent]', 0);
            }
            if (empty($h_content)) {
                $h_content=$html_object->find('div[id=contentRight]', 0);
            }
            if (empty($h_content)) {
                $h_content=$html_object->find('div[id=MainhotelContent]', 0);
            }
            if (empty($h_content)) {
                $h_content=$html_object->find('div.col-md-19', 0);
            }
            if (empty($h_content)) {
                $h_content=$html_object->find('div[id=historyLine]', 0);
            }
            if (empty($h_content)) {
                $h_content=$html_object->find('div.container', 0);
            }
            if (empty($h_content)) {
                $h_content='';
                $h_done=2;
            } else {
                $h_content=$h_content->innertext;
            }
//echo $h_content;die();
            $h_url=str_replace('\\', '/', $file);
            $h_url=str_replace('F:/recovery-html/chinahighlights/', '/', $h_url);
            $h_url=str_replace('F:/recovery-html/asiahighlights/', '/', $h_url);
			$h_url=str_replace('d:/asiahighlights-htm/', '/', $h_url);
            $h_url=str_replace('index-md.htm', '', $h_url);
            $h_url=str_replace('-md.htm', '.htm', $h_url);
            
            foreach ($html_object->find('meta') as $item) {
                if ($item->name == 'description' && !empty($item->content)) {
                    $h_description = $item->content;
                    break;
                }
            }
            if (empty($h_description)) {
                $h_description='';
                $h_done=2;
            }
            
            $h_title=$html_object->find('title', 0);
            if (empty($h_title)) {
                $h_title='';
                $h_done=2;
            } else {
                $h_title=$h_title->innertext;
            }
            
            $h_h1=$html_object->find('h1', 0);
            if (empty($h_h1)) {
                $h_h1='';
                $h_done=2;
            } else {
                $h_h1=$h_h1->innertext;
            }

            echo 'file:'.$file.'</br>';
            echo 'h_title:'.$h_title.'</br>';
            echo 'h_url:'.$h_url.'</br>';
            echo 'h_description:'.$h_description.'</br>';
            echo '<hr/>';
            //echo $h_content;
            $this->Import_model->update_html_info($h_id, $h_h1, $h_title, $h_url, $h_description, $h_content,$h_source, $h_done);
        }
    }

	//给节点分组
	public function allot_group($sitecode){
		$this->HT = $this->load->database('INFO', TRUE);
		$sql=' select top 10 hg.*,isnull((select top 1 hg2.g_is_id from html_group hg2 where hg2.g_id=hg.g_panrent_id),0) as panrent_is_id from html_group hg  where hg.g_done=0 and hg.g_sitecode=? order by hg.g_id asc ';
		$query=$this->HT->query($sql,$sitecode);
		if(empty($query->result())){
			echo '处理完毕';
			return;
		}
		
		foreach ($query->result() as $item) {
			//是根节点
			if($item->g_panrent_id==0){
				$this->HT->query('update Tourmanager.dbo.infoStructures set is_parent_id=-99 ,is_path= cast(is_id as varchar(max))+\',\',is_level=0,is_sort=0 where is_id=?',$item->g_is_id);
			}else{
				//修改分类结构
				$panrent_is=$this->InfoStructures_model->Detail($item->panrent_is_id);
				$is_parent_id=$panrent_is->is_id;
				$is_path=$panrent_is->is_path.$item->g_is_id.',';
				$is_id=$item->g_is_id;
				$this->HT->query('update Tourmanager.dbo.infoStructures set is_parent_id=? ,is_path= ?,is_level=1 where is_id=?',array($is_parent_id,$is_path,$is_id));
				
				$url_arr=explode(',',$item->g_url);
				print_r($url_arr);
				foreach($url_arr as $item_url){
					$query_like=$this->HT->query('select ic_id,ic_title,ic_url,is_id,is_parent_id,is_path,is_level from Tourmanager.dbo.infoContents inner join Tourmanager.dbo.infoStructures on is_ic_id=ic_id and ic_sitecode=is_sitecode
where ic_sitecode=? and ic_url like \'%'.trim($item_url).'%\' order by ic_id asc',$sitecode);
					if(!empty($query_like->result())){
						foreach($query_like->result() as $item_child){
							$child_is_id=$item_child->is_id;
							$child_is_parent_id=$is_id;
							$child_is_path=$is_path.$child_is_id.',';
							$this->HT->query('update Tourmanager.dbo.infoStructures set is_parent_id=? ,is_path= ?,is_level=2 where is_id=?',array($child_is_parent_id,$child_is_path,$child_is_id));
						}
					}
				}
			}
			print_r($this->HT->queries);
			$this->HT->query('update html_group set g_done=1 where g_id=?',$item->g_id);
		}
	}


	public function clear_css_html(){
        $this->HT = $this->load->database('HT', true);
        $html_string='';
        $sql= " select top 500 ic_id,ic_url,ic_content from infoContents where ic_sitecode='ch' and (ic_content like '%tourPublishers%' or ic_content like '%class=\"monthWeather\"%') ";
        $query=$this->HT->query($sql);
        if($query->num_rows() > 0){
            foreach ($query->result() as $item) {
                $html_string=str_replace('class="monthWeather"','class="infotable"' ,$item->ic_content);
                $html_object=str_get_html( $html_string);
                if (empty($html_object)) {
                    echo '文件加载失败:'.$item->ic_url.'<br/>';
                    continue;
                }else{
                    foreach ($html_object->find('.tourPublishers') as $hidden_item) {
                        $hidden_item->outertext = '';
                    }
                    $html_string=$html_object->save();
                    $sql_update='update infoContents set ic_content = N? where ic_id=?';
                    $this->HT->query($sql_update,array($html_string,$item->ic_id));
                    //print_r($this->HT->queries);
                    echo $item->ic_url;echo '<br/>';
                    //echo $html_string;
                }

            }
        }else{
            echo 'done';
        }
    }


    public function clear_site()
    {
        $this->Import_model->import_clear_site();
        echo 'clear_site';
    }

    public function reset_path()
    {
        @set_time_limit(0);
        return false;
        $this->Import_model->reset_all_path();
    }

    public function to_sqlite()
    {
        $this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
        $this->HT = $this->load->database('HT', true);
        $sql_array = array();
        $sql_ht = "SELECT ic.ic_id, \n"
                . "       ic.ic_url, \n"
                . "       ic.ic_url_title, \n"
                . "       ic.ic_type, \n"
                . "       ic.ic_title, \n"
                . "       ic.ic_content, \n"
                . "       ic.ic_summary, \n"
                . "       ic.ic_seo_title, \n"
                . "       ic.ic_seo_description, \n"
                . "       ic.ic_seo_keywords, \n"
                . "       ic.ic_show_bread_crumbs, \n"
                . "       ic.ic_status, \n"
                . "       ic.ic_template, \n"
                . "       ic.ic_photo, \n"
                . "       ic.ic_photo_width, \n"
                . "       ic.ic_photo_height, \n"
                . "       ic.ic_sitecode, \n"
                . "       ic.ic_recommend_tours, \n"
                . "       ic.ic_recommend_packages, \n"
                . "       ic.ic_datetime, \n"
                . "       ic.ic_ht_area_id, \n"
                . "       ic.ic_ht_area_type, \n"
                . "       ic.ic_ht_product_id, \n"
                . "       ic.ic_ht_product_type, \n"
                . "       is1.is_id, \n"
                . "       is1.is_parent_id, \n"
                . "       is1.is_path, \n"
                . "       is1.is_level, \n"
                . "       is1.is_sort, \n"
                . "       is1.is_sitecode, \n"
                . "       is1.is_datetime, \n"
                . "       is1.is_ic_id \n"
                . "FROM   infoContents ic \n"
                . "       INNER JOIN infoStructures is1 ON is1.is_ic_id = ic.ic_id \n"
                . "            AND is1.is_sitecode = 'cht' \n"
                . "WHERE  ic.ic_ht_area_type = 'c' \n"
                . "       AND ic.ic_ht_area_id IN (177, 54, 1, 3, 7)";
        $sql_ht_query = $this->HT->query($sql_ht);
        foreach ($sql_ht_query->result() as $item) {
            $sql_content = "INSERT INTO information \n"
                    . "  ( \n"
                    . "	ic_id, ic_url_title, ic_type,ic_content, ic_title,  \n"
                    . "	 ic_show_bread_crumbs,ic_status, ic_template,ic_photo, ic_photo_width,  \n"
                    . "	  ic_photo_height,ic_datetime,ic_ht_area_id, ic_ht_area_type,  \n"
                    . "  is_id, is_parent_id, is_path, is_level, is_sort \n"
                    . "  ) \n"
                    . "VALUES \n"
                    . "  (  \n"
                    . $this->HT->escape($item->ic_id) . ' , '
                    . $this->HT->escape($item->ic_url_title) . ' , '
                    . $this->HT->escape($item->ic_type) . ' , '
                    . $this->HT->escape($item->ic_content) . ' , '
                    . $this->HT->escape($item->ic_title) . ' , '
                    . $this->HT->escape($item->ic_show_bread_crumbs) . ' , '
                    . $this->HT->escape($item->ic_status) . ' , '
                    . $this->HT->escape($item->ic_template) . ' , '
                    . $this->HT->escape($item->ic_photo) . ' , '
                    . $this->HT->escape($item->ic_photo_width) . ' , '
                    . $this->HT->escape($item->ic_photo_height) . ' , '
                    . $this->HT->escape($item->ic_datetime) . ' , '
                    . $this->HT->escape($item->ic_ht_area_id) . ' , '
                    . $this->HT->escape($item->ic_ht_area_type) . ' , '
                    . $this->HT->escape($item->is_id) . ' , '
                    . $this->HT->escape($item->is_parent_id) . ' , '
                    . $this->HT->escape($item->is_path) . ' , '
                    . $this->HT->escape($item->is_level) . ' , '
                    . $this->HT->escape($item->is_sort)
                    . "  );";
            $sql_array[] = $sql_content;
            // echo $sql_content.';';
            //$this->cache->save(md5($item->ic_id), $item->ic_content, 60 * 60 * 24 * 30);
        }
        file_put_contents('import.sql', implode("\n", $sql_array));
        //print_r($sql_array);
    }
}
