审查视图

application/admin/common.php 5.9 KB
Karson authored
1 2
<?php
3
use app\common\model\Category;
Karson authored
4
use fast\Form;
5
use fast\Tree;
Karson authored
6 7
use think\Db;
8
/**
Karson authored
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * 生成下拉列表
 * @param string $name
 * @param mixed $options
 * @param mixed $selected
 * @param mixed $attr
 * @return string
 */
function build_select($name, $options, $selected = [], $attr = [])
{
    $options = is_array($options) ? $options : explode(',', $options);
    $selected = is_array($selected) ? $selected : explode(',', $selected);
    return Form::select($name, $options, $selected, $attr);
}

/**
 * 生成单选按钮组
 * @param string $name
 * @param array $list
 * @param mixed $selected
 * @return string
 */
function build_radios($name, $list = [], $selected = null)
{
    $html = [];
    $selected = is_null($selected) ? key($list) : $selected;
34
    $selected = is_array($selected) ? $selected : explode(',', $selected);
Karson authored
35 36
    foreach ($list as $k => $v)
    {
37
        $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
Karson authored
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    }
    return implode(' ', $html);
}

/**
 * 生成复选按钮组
 * @param string $name
 * @param array $list
 * @param mixed $selected
 * @return string
 */
function build_checkboxs($name, $list = [], $selected = null)
{
    $html = [];
    $selected = is_null($selected) ? [] : $selected;
53
    $selected = is_array($selected) ? $selected : explode(',', $selected);
Karson authored
54 55
    foreach ($list as $k => $v)
    {
56
        $html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox($name, $k, in_array($k, $selected), ['id' => "{$name}-{$k}"]));
Karson authored
57 58 59 60 61
    }
    return implode(' ', $html);
}

/**
62 63 64 65 66 67 68
 * 生成分类下拉列表框
 * @param string $name
 * @param string $type
 * @param mixed $selected
 * @param array $attr
 * @return string
 */
69
function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
70 71 72 73
{
    $tree = Tree::instance();
    $tree->init(Category::getCategoryArray($type), 'pid');
    $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
74
    $categorydata = $header ? $header : [];
75 76 77 78 79 80 81 82 83
    foreach ($categorylist as $k => $v)
    {
        $categorydata[$v['id']] = $v['name'];
    }
    $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
    return build_select($name, $categorydata, $selected, $attr);
}

/**
Karson authored
84
 * 生成表格操作按钮栏
85 86
 * @param array $btns 按钮组
 * @param array $attr 按钮属性值
Karson authored
87 88
 * @return string
 */
89
function build_toolbar($btns = NULL, $attr = [])
Karson authored
90 91 92
{
    $btns = $btns ? $btns : ['refresh', 'add', 'edit', 'delete'];
    $btns = is_array($btns) ? $btns : explode(',', $btns);
93 94 95 96
    $btnAttr = [
        'refresh' => ['javascript:;', 'btn btn-primary btn-refresh', 'fa fa-refresh', ''],
        'add'     => ['javascript:;', 'btn btn-success btn-add', 'fa fa-plus', __('Add')],
        'edit'    => ['javascript:;', 'btn btn-success btn-edit btn-disabled disabled', 'fa fa-pencil', __('Edit')],
97
        'delete'  => ['javascript:;', 'btn btn-danger btn-del btn-disabled disabled', 'fa fa-trash', __('Delete')],
98 99
    ];
    $btnAttr = array_merge($btnAttr, $attr);
Karson authored
100
    $html = [];
101
    foreach ($btns as $k => $v)
Karson authored
102
    {
103 104 105 106 107 108
        if (!isset($btnAttr[$v]))
        {
            continue;
        }
        list($href, $class, $icon, $text) = $btnAttr[$v];
        $html[] = '<a href="' . $href . '" class="' . $class . '" ><i class="' . $icon . '"></i> ' . $text . '</a>';
Karson authored
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    }
    return implode(' ', $html);
}

/**
 * 生成页面Heading
 *
 * @param string $title
 * @param string $content
 * @return string
 */
function build_heading($title = NULL, $content = NULL)
{
    if (is_null($title) && is_null($content))
    {
Karson authored
124 125
        $path = request()->pathinfo();
        $path = $path[0] == '/' ? $path : '/' . $path;
Karson authored
126
        // 根据当前的URI自动匹配父节点的标题和备注
Karson authored
127
        $data = Db::name('auth_rule')->where('name', $path)->field('title,remark')->find();
Karson authored
128 129 130 131 132 133 134 135 136 137
        if ($data)
        {
            $title = $data['title'];
            $content = $data['remark'];
        }
    }
    if (!$content)
        return '';
    return '<div class="panel-heading"><div class="panel-lead"><em>' . $title . '</em>' . $content . '</div></div>';
}
Karson authored
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

/**
 * 判断文件或文件夹是否可写
 * @param	string
 * @return	bool
 */
function is_really_writable($file)
{
    if (DIRECTORY_SEPARATOR === '/')
    {
        return is_writable($file);
    }
    if (is_dir($file))
    {
        $file = rtrim($file, '/') . '/' . md5(mt_rand());
        if (($fp = @fopen($file, 'ab')) === FALSE)
        {
            return FALSE;
        }
        fclose($fp);
        @chmod($file, 0777);
        @unlink($file);
        return TRUE;
    }
    elseif (!is_file($file) OR ( $fp = @fopen($file, 'ab')) === FALSE)
    {
        return FALSE;
    }
    fclose($fp);
    return TRUE;
}

/**
 * 删除文件夹
 * @param string $dirname
 * @return boolean
 */
function rmdirs($dirname)
{
    if (!is_dir($dirname))
        return false;
    $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST
    );

    foreach ($files as $fileinfo)
    {
        $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
        $todo($fileinfo->getRealPath());
    }
    @rmdir($dirname);
    return true;
}

/**
 * 复制文件夹
 * @param string $source 源文件夹
 * @param string $dest 目标文件夹
 */
function copydirs($source, $dest)
{
    if (!is_dir($dest))
    {
        mkdir($dest, 0755);
    }
    foreach (
    $iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item
    )
    {
        if ($item->isDir())
        {
            $sontDir = $dest . DS . $iterator->getSubPathName();
            if (!is_dir($sontDir))
            {
                mkdir($sontDir);
            }
        }
        else
        {
            copy($item, $dest . DS . $iterator->getSubPathName());
        }
    }
}