<?php

/**
 * 通用功能模块 globals Model
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: globals.class.php 50 2011-12-05 02:56:32Z zhangwentao $
 * http://un.zhuna.cn；
 */
class globals extends Model
{

    private $db;
    private $class_table;
    private $article_table;
    private $city_table;
    private $ad_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->class_table = DB_PREFIX . 'article_class';
        $this->article_table = DB_PREFIX . 'article';
        $this->city_table = DB_PREFIX . 'city';
        $this->ad_table = DB_PREFIX . 'ad';
    }

    /**
     * 由pid 或 cityid得到city的info
     * @param <type> $id
     * @param <type> $type 默认p的时候就是pid $type = 'c' cityid得到
     * @return <type> 
     */
    public function getCityinfo($id, $type = 'p')
    {
        if ($type == 'p') {
            $sql = "SELECT * FROM $this->city_table WHERE province = $id LIMIT 0,1";
        } elseif ($type == 'pid') {
            $sql = "SELECT * FROM $this->city_table WHERE province = 1 AND pid = $id LIMIT 0,1";
        } else {
            $sql = "SELECT * FROM $this->city_table WHERE cityid = $id LIMIT 0,1";
        }
        $rs = $this->db->cacheGetRow($sql);
        return $rs;
    }

    /**
     * 删除非空文件夹
     * @param $dir;
     * return
     */
    public function deldir($dir)
    {
        $dh = opendir($dir);
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (!is_dir($fullpath)) {
                    unlink($fullpath);
                } else {
                    $this->deldir($fullpath);
                }
            }
        }

        closedir($dh);

        if (rmdir($dir)) {
            return true;
        } else {
            return false;
        }
    }

}