<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tools extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->permission->is_admin();
        $this->load->model('Area_model');
        $this->load->model('InfoStructures_model');
        $this->load->model('Information_model');
        $this->load->model('InfoMetas_model');
        $this->site_code = $this->config->item('site_code');
    }
    
    /**
     * 
     * 公有函数：扫描已有缓存文件并更新。
     * 
     * 必要参数：
     * @param String $site_code
     * 
     */ 
    public function old()
    {
        $data['yeardiff'] = $this->input->get_post('yeardiff');
        $data['nopub'] = $this->input->get_post('nopub');
        $data['emptyinfo'] = $this->input->get_post('emptyinfo');
        $data['csv'] = $this->input->get_post('csv');
        //参数
        if (!$data['yeardiff']) $data['yeardiff'] = 3;
        //老旧信息
        $data['info'] = $this->Information_model->get_oldest_info($data['yeardiff'], $data['nopub'], $data['emptyinfo']);
        
        //视图
        if ($data['csv'])
        {
            $csv_header = array('id', 'file', 'date', 'public');
            $csv_data = array();
            foreach ($data['info'] as $key => $it) 
            {
                $temp = array();
                $temp[0] = $key;
                $temp[1] = $it->ic_url;
                $temp[2] = date('Y-m-d', strtotime($it->ic_datetime));
                $it->ic_status ? $temp[3] = 'yes' : $temp[3] = 'no';
                array_push($csv_data, $temp);
            }
            $this->csv($csv_header, $csv_data, 'old_info.csv');
        } else {
            $this->load->view('tools/old', $data);
        }
    }

    /**
     * 
     * 公有函数：AMP转化表格。
     * 
     * 必要参数：
     * @param String $site_code
     * 
     */ 
    public function amp_check()
    {
        $data['amp'] = $this->input->get_post('amp');
        if (!$data['amp']) $data['amp'] = 'yes';
        //AMP转化列表
        if ($data['amp'] == 'yes') {
            $data['info'] = $this->InfoMetas_model->list_amp($this->site_code);
        } else {
            $data['info'] = $this->InfoMetas_model->list_no_amp($this->site_code);
        }
        
        //视图
        $this->load->view('tools/amp_check', $data);
    }
    
    /**
    * 导出CSV文件
    * @param array $data        数据
    * @param array $header_data 首行数据
    * @param string $file_name  文件名称
    * @return string
    */
    public function csv($header_data, $data, $file_name)
    {
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename='.$file_name);
        header('Cache-Control: max-age=0');
        $fp = fopen('php://output', 'a');
        if (!empty($header_data)) {
            fputcsv($fp, $header_data);
        }
        $count = count($data);
        if ($count > 0) 
        {
            for ($i = 0; $i < $count; $i++) 
            {
                $row = $data[$i];
                fputcsv($fp, $row);
            }
        }
        fclose($fp);
    }
}
//end of Cache
