<?php

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

class Infoshare extends CI_Controller {

    function __construct() {
        parent::__construct();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('InfoMetas_model');
        $this->load->model('InfoStructures_model');
        $this->load->model('Infosharedata_model');
    }
    
    public function index() {
        //如果找不到结构根节点则自动创建
        $root_sid = $this->Infosharedata_model->root_sid();
        if (empty($root_sid)) {
            $InfoStructures_insert_id = $this->InfoStructures_model->add(0);
            $this->Infosharedata_model->add(0, '', '信息分享', '', '', '信息分享', '', $InfoStructures_insert_id);
            $root_sid = $InfoStructures_insert_id;
        }
        redirect(site_url('infoshare/detail/' . $root_sid));
    }

    public function detail($is_id) {
        $data = array();
        $data['infoshare'] = $this->Infosharedata_model->detail($is_id);
        $data['structures_list'] = $this->Infosharedata_model->structures();
        $data['attachments_list'] = $this->InfoMetas_model->detail($data['infoshare']->isd_id, 'meta_infoshare_attachment');
        //如果是首页的话显示最新的分享记录
        if ($is_id == 240000066) {
            $data['new_infoshate'] = $this->Infosharedata_model->search('');
            
        }
        if (empty($data['infoshare'])) {
            
        } else {
            $this->load->view('bootstrap/header', $data);
            $this->load->view('bootstrap/infoshare');
            $this->load->view('bootstrap/footer');
        }
    }

    //检查邮件更新，如果有更新则添加入库
    public function check_mail() {
        //@set_time_limit(0);
        $this->load->library('ImapMailbox_lib');
        $root_sid = $this->Infosharedata_model->root_sid();
        $last_mailid = $this->Infosharedata_model->last_mailid();
        //echo $last_mailid;
        //$last_mailid=11120;
        //echo $last_mailid;
        $mails = array();
        $mailbox = new ImapMailbox('{202.103.68.122:995/ssl/pop3/novalidate-cert}INBOX', MAILBOX_EMAIL, MAILBOX_PASSWORD, MAILBOX_ATTACHMENTS_DIR, 'utf-8');

        $mailsIds = $mailbox->searchMailBox('ALL');

        if ($mailsIds) {
            foreach ($mailsIds as $item) {
                if ($item > $last_mailid) {
                    //先获取头部信息，根据标题判断是否要下载邮件，防止出现太多附件F
                    $headerinfo = $mailbox->getMailsInfo(array($item));
                    if (substr_count($headerinfo[0]->subject, '#') >= 2) {
                        echo $headerinfo[0]->subject . '<br/>';
                        $mail = $mailbox->getMail($item);
                        $InfoStructures_insert_id = $this->InfoStructures_model->add($root_sid);
                        $Infosharedata_insert_id = $this->Infosharedata_model->add($mail->id, $mail->date, $mail->subject, $mail->fromName, $mail->textHtml, str_ireplace(array('#信息分享#', 'FW:', '答复:', '转发:', 're:'), array('', '', '', '', ''), $mail->subject), '', $InfoStructures_insert_id);
                        //处理附件
                        foreach ($mail->getAttachments() as $attachment) {
                            $this->InfoMetas_model->add($Infosharedata_insert_id, 'meta_infoshare_attachment', $attachment->id . '~@~' . $attachment->name . '~@~' . str_ireplace(MAILBOX_ATTACHMENTS_DIR, '/mailbox_attachments', $attachment->filePath));
                        }
                    }
                }
            }
        }
        echo 'done!';
    }

    //移动结构顺序
    public function move() {
        //网站会提交一个同级节点id列表字符串，按照这个去排序
        $parent_id = $this->input->post('pid');
        $idsStr = $this->input->post('ids');
        $idsArray = explode(',', $idsStr);
        foreach ($idsArray as $key => $value) {
            if ($value) {
                //设置排序
                $this->InfoStructures_model->set_sort($value, $key);
                //设置path
                $this->InfoStructures_model->set_path($parent_id, $value);
            }
        }
        $data[] = array('name' => 'ok', 'value' => $this->lang->line('structures_success_move'));
        echo json_encode($data);
        return TRUE;
    }

    //添加分类
    public function add($is_parent_id) {
        $InfoStructures_insert_id = $this->InfoStructures_model->add($is_parent_id);
        $Infosharedata_insert_id = $this->Infosharedata_model->add(0, '', 'new', '', '', 'new', '', $InfoStructures_insert_id);
        $data[] = array('name' => 'ok', 'value' => $InfoStructures_insert_id);
        echo json_encode($data);
        return TRUE;
    }

    //更新信息
    function update() {
        $infoshare = $this->Infosharedata_model->detail_by_id($this->input->post('isd_id'));
        if (empty($infoshare)) {
            $data[] = array('name' => 'no', 'value' => $this->lang->line('form_info_error'));
        } else {
            $this->Infosharedata_model->update($infoshare->isd_id, $this->input->post('isd_title'), $this->input->post('isd_memo'), $this->input->post('isd_html'));
            $data[] = array('name' => 'ok_go', 'value' => site_url('infoshare/detail/' . $infoshare->isd_is_id));
        }
        echo json_encode($data);
        return TRUE;
    }

    //搜索
    public function search() {
        $data = array();
        $data['keywords'] = $this->input->post('keywords');
        $data['search_list'] = $this->Infosharedata_model->search($data['keywords']);

        $this->load->view('bootstrap/header', $data);
        $this->load->view('bootstrap/infoshare_search');
        $this->load->view('bootstrap/footer');
    }

    //搜索一周的信息并发公告
    public function search_week() {
        $data = array();
        $data['search_list'] = $this->Infosharedata_model->search_week();
        if (!empty($data['search_list'])) {
            //$this->load->view('bootstrap/header', $data);
            $mail_body = $this->load->view('bootstrap/infoshare_week', $data, true);
            //$this->load->view('bootstrap/footer');
            //发送邮件
            $this->load->library('Phpmailer_lib');
            $mail = new PHPMailer();
            $mail->IsSMTP();
            $mail->Host = '202.103.68.122:25';
            $mail->SMTPAuth = true;
            $mail->Username = "cht58@citsguilin.com";
            $mail->Password = "B047f21d654e07";
            $mail->From = "aas@citsguilin.com";
            $mail->SMTPSecure = 'tls';
            $mail->CharSet = "utf-8";
            $mail->Encoding = "base64";
            $mail->AddAddress('aas@citsguilin.com');
            //$mail->AddAddress('cht01@citsguilin.com');
            //$mail->AddAddress('cht58@citsguilin.com');
            $mail->IsHTML(true);
            $mail->Subject = '每周信息分享';
            $mail->Body = $mail_body;
            if (!$mail->Send()) {
                echo "邮件发送有误 <p>";
                echo "邮件错误信息: " . $mail->ErrorInfo;
            } else {
                echo " 邮件发送成功!<br />";
            }
        }
    }

    //清空回收站
    public function clear() {
        $this->Infosharedata_model->delete_by_parent(240000129);
        $this->index();
    }

    //编辑内容
    public function edit($is_id) {
        $data = array();
        $data['infoshare'] = $this->Infosharedata_model->detail($is_id);

        $this->load->view('bootstrap/header', $data);
        if (substr($data['infoshare']->isd_title, -3) == '.md') {
            $this->load->view('bootstrap/infoshare_edit_markdown');
        } else {
            $this->load->view('bootstrap/infoshare_edit');
        }
        $this->load->view('bootstrap/footer');
    }
    
    

}
