<?php namespace Phpcmf\Library;


/**
 * http://www.xunruicms.com
 * 本文件是框架系统文件，二次开发时不可以修改本文件，可以通过继承类方法来重写此文件
 **/



/**
 * 静态生成
 */

class Html
{
    protected $webpath;
    protected $psize = 20; // 每页生成多少条

    // 栏目的数量统计
    public function get_category_data($app, $cat) {

        // 获取生成栏目
        if (!$cat) {
            \Phpcmf\Service::C()->_json(0, '没有可用生成的栏目数据');
        }

        $list = [];
        foreach ($cat as $i => $t) {
            if ($t['tid'] == 0) {
                // 单网页
                $list[$t['mid']][] = [
                    'id' => $t['id'],
                    'mid' => $t['mid'],
                    'url' => $t['url'],
                    'page' => 0,
                    'name' => $t['name'],
                    'html' => $t['setting']['html'],
                ];
            } elseif ($t['tid'] == 1) {
                // 模块
                // 判断模块表是否存在被安装
                if (!\Phpcmf\Service::M()->db->tableExists(\Phpcmf\Service::M()->dbprefix(dr_module_table_prefix($t['mid'])))) {
                    unset($list[$t['mid']]);
                    continue;
                }
                if ($t['child'] && !$t['pcatpost'] && $t['setting']['template']['list'] != $t['setting']['template']['category']) {
                    // 判断是封面页面
                    $list[$t['mid']][] = [
                        'id' => $t['id'],
                        'url' => $t['url'],
                        'mid' => $t['mid'],
                        'page' => 0,
                        'name' => $t['name'],
                        'html' => $t['setting']['html'],
                    ];
                } else {
                    // 内容列表页面
                    $find = 1;
                    // 查找mwhere目录
                    if (is_dir(dr_get_app_dir($t['mid']).'Mwhere/')) {
                        $files = dr_file_map(dr_get_app_dir($t['mid']).'Mwhere/');
                        if ($files) {
                            $db = \Phpcmf\Service::M()->db->table(dr_module_table_prefix($t['mid']));
                            if ($t['child'] && $t['childids']) {
                                $db->whereIn('catid', explode(',', $t['childids']));
                            } else {
                                $db->where('catid', (int)$t['id']);
                            }
                            $mid = $t['mid'];
                            $field = \Phpcmf\Service::L('cache')->get('table-'.SITE_ID, \Phpcmf\Service::M()->dbprefix(dr_module_table_prefix($t['mid'])));
                            $siteid = SITE_ID;
                            foreach ($files as $f) {
                                $w = require dr_get_app_dir($t['mid']).'Mwhere/'.$f;
                                if ($w) {
                                    $db->where($w);
                                }
                            }
                            $find = 0;
                        }
                    }
                    if ($find) {
                        $db = \Phpcmf\Service::M()->db->table(dr_module_table_prefix($t['mid']).'_index');
                        if ($t['child'] && $t['childids']) {
                            $db->whereIn('catid', explode(',', $t['childids']));
                        } else {
                            $db->where('catid', (int)$t['id']);
                        }
                    }
                    $total = $db->countAllResults(); // 统计栏目的数据量
                    $list[$t['mid']][] = [
                        'id' => $t['id'],
                        'mid' => $t['mid'],
                        'url' => $t['url'],
                        'page' => 0,
                        'name' => $t['name'],
                        'html' => $t['setting']['html'],
                    ];
                    if ($total) {
                        // 分页
                        if (\Phpcmf\Service::V()->_is_mobile) {
                            $pagesize = (int)$t['setting']['template']['mpagesize']; // 每页数量
                        } else {
                            $pagesize = (int)$t['setting']['template']['pagesize']; // 每页数量
                        }
                        !$pagesize && $pagesize = 10; // 默认10条分页
                        $count = ceil($total/$pagesize); // 计算总页数
                        if ($count > 1) {
                            for ($i = 1; $i <= $count; $i++) {
                                $list[$t['mid']][] = [
                                    'id' => $t['id'],
                                    'mid' => $t['mid'],
                                    'url' => $t['url'],
                                    'page' => $i,
                                    'name' => $t['name'].'【第'.$i.'页】',
                                    'html' => $t['setting']['html'],
                                ];
                            }
                        }
                    }
                }
            }
        }

        if (!dr_count($list)) {
            \Phpcmf\Service::C()->_json(0, '没有可用生成的栏目数据');
        }

        $name = 'category-'.$app.'-html-file';
        $ct = 0;

        $cache = [];
        foreach ($list as $data) {
            $ct+= dr_count($data);
            $arr = array_chunk($data, $this->psize);
            $cache = dr_array2array($cache, $arr);
        }
        foreach ($cache as $i => $t) {
            \Phpcmf\Service::L('cache')->set_data($name.'-'.($i+1), $t, 36000);
        }

        $count = dr_count($cache);

        \Phpcmf\Service::L('cache')->set_data($name, $count, 36000);

        \Phpcmf\Service::C()->_json(1, '共'.$ct.'个，分'.$count.'页');
    }

    // 内容的数量统计
    public function get_show_data($app, $param) {

        // 获取生成栏目
        $cids = [];
        $mids = [];
        if ($param['catids']) {
            $catids = explode(',', $param['catids']);
            if ($catids) {
                $cats = \Phpcmf\Service::C()->get_cache('module-'.SITE_ID.'-'.($app ? $app : 'share'), 'category');
                foreach ($catids as $id) {
                    if ($cats[$id]) {
                        $cids = dr_array2array($cids, explode(',', $cats[$id]['childids']));
                        $cats[$id]['mid'] && $mids[$cats[$id]['mid']] = $cats[$id]['mid'];
                    }
                }
                $cids = array_unique($cids);
            }
        }

        if (!$app) {
            // 共享
            if ($cids) {
                if (!$mids) {
                    \Phpcmf\Service::C()->_json(0, '没有可用生成的内容模块');
                }
                $data = [];
                foreach ($mids as $mid) {
                    $db = \Phpcmf\Service::M()->db->table(dr_module_table_prefix($mid))->select('id,catid,title,url');
                    if (isset($param['date_form']) && $param['date_form']) {
                        $db->where('`updatetime` BETWEEN ' . strtotime($param['date_form'].' 00:00:00') . ' AND ' . ($param['date_to'] ? strtotime($param['date_to'].' 23:59:59') : SYS_TIME));
                    } elseif (isset($param['date_to']) && $param['date_to']) {
                        $db->where('`updatetime` BETWEEN 0 AND ' . strtotime($param['date_to'].' 23:59:59'));
                    }
                    if (isset($param['id_to']) && $param['id_form']) {
                        $db->where('`id` BETWEEN '.(int)$param['id_form'].' AND ' . (int)$param['id_to']);
                    }
                    $db->where('catid IN ('. implode(',', $cids).')');
                    // 查找mwhere目录
                    $mwhere = \Phpcmf\Service::Mwhere_Apps();
                    if ($mwhere) {
                        $field = \Phpcmf\Service::L('cache')->get('table-'.SITE_ID, \Phpcmf\Service::M()->dbprefix(dr_module_table_prefix($mid)));
                        $siteid = SITE_ID;
                        foreach ($mwhere as $mapp) {
                            $w = require dr_get_app_dir($mapp).'Config/Mwhere.php';
                            if ($w) {
                                $db->where($w);
                            }
                        }
                    }
                    $rows = $db->get()->getResultArray(); // 获取需要生成的内容索引
                    if ($rows) {
                        foreach ($rows as $r) {
                            $r['is_module_dirname'] = $mid;
                            $data[] = $r;
                        }
                    }
                }
            } else {
                \Phpcmf\Service::C()->_json(0, '模块参数app不存在');
            }
        } else {
            // 独立
            $db = \Phpcmf\Service::M()->db->table(dr_module_table_prefix($app))->select('id,catid,title,url');
            if (isset($param['date_form']) && $param['date_form']) {
                $db->where('`updatetime` BETWEEN ' . strtotime($param['date_form'].' 00:00:00') . ' AND ' . ($param['date_to'] ? strtotime($param['date_to'].' 23:59:59') : SYS_TIME));
            } elseif (isset($param['date_to']) && $param['date_to']) {
                $db->where('`updatetime` BETWEEN 0 AND ' . strtotime($param['date_to'].' 23:59:59'));
            }
            if (isset($param['id_to']) && $param['id_form']) {
                $db->where('`id` BETWEEN '.(int)$param['id_form'].' AND ' . (int)$param['id_to']);
            }
            if ($cids) {
                $db->where('catid IN ('. implode(',', $cids).')');
            }
            // 查找mwhere目录
            $mwhere = \Phpcmf\Service::Mwhere_Apps();
            if ($mwhere) {
                $field = \Phpcmf\Service::L('cache')->get('table-'.SITE_ID, \Phpcmf\Service::M()->dbprefix(dr_module_table_prefix($app)));
                $siteid = SITE_ID;
                foreach ($mwhere as $mapp) {
                    $w = require dr_get_app_dir($mapp).'Config/Mwhere.php';
                    if ($w) {
                        $db->where($w);
                    }
                }
            }
            $data = $db->get()->getResultArray(); // 获取需要生成的内容索引
        }

        if (!dr_count($data)) {
            \Phpcmf\Service::C()->_json(0, '没有可用生成的内容数据');
        }

        $name = 'show-'.$app.'-html-file';

        $arr = array_chunk($data, $param['pagesize'] ? $param['pagesize'] : $this->psize);
        $count = dr_count($arr);
        foreach ($arr as $i => $t) {
            \Phpcmf\Service::L('cache')->set_data($name.'-'.($i+1), $t, 36000);
        }

        \Phpcmf\Service::L('cache')->set_data($name, $count, 36000);

        \Phpcmf\Service::C()->_json(1, '共'.dr_count($data).'条，分'.$count.'页');
    }

    // 网站文件生成地址
    public function get_webpath($siteid, $mid, $file = '') {

        if (!$this->webpath) {
            $this->webpath = require WRITEPATH.'config/webpath.php';
        }

        $webpath = WEBPATH;
        if (isset($this->webpath[$siteid][$mid]) && $this->webpath[$siteid][$mid]) {
            $webpath = $this->webpath[$siteid][$mid];
        }

        return $webpath.$file;
    }

}