123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace app\common\components;
- use Yii;
- class FormElements
- {
- static function getInstCls(){
- static $m_instCls = null;
- if (!isset($m_instCls)){$m_instCls = new FormElements();}
- return $m_instCls;
- }
- /**树状父级选择栏
- * @param $datas
- * @param int $top_id 顶级Id
- * @param int $menu_id 当前选中菜单ID
- * @param string $name 对应显示标题的字段
- * @param string $alt
- * @param string $id
- * @param string $property
- * @return string
- */
- public static function tree_select($datas,$top_id = 0, $menu_id = 0, $select_name = 'parent_id', $alt = '' , $select_id ='', $property = 'class=\'\'')
- {
- $tree = new Tree;
- $select_id = $select_id ? $select_id : $select_name;
- $string = "<select name='$select_name' id='$select_id' $property>\n<option value=''>$alt</option>\n";
- $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
- $tree->init($datas);
- $string .= $tree->get_tree($top_id, $str, $menu_id);
- $string .= '</select>';
- return $string;
- }
- //模型字段管理的一些初始参数
- public static function getFieldsInfo($arg)
- {
- $info=array(
- 'types' => array(
- 'text'=>'单行文本',
- 'textarea'=>'多行文本',
- 'editor'=>'编辑器',
- 'cat_id'=>'栏目',
- 'title'=>'标题',
- 'box'=>'选项',
- 'image'=>'图片',
- 'images'=>'多图片',
- 'number'=>'数字',
- 'datetime'=>'日期和时间',
- 'pos_id'=>'推荐位',
- 'keywords'=>'关键词',
- 'author'=>'作者',
- 'copy_from'=>'来源',
- 'group_id'=>'用户组',
- 'is_link'=>'转向链接',
- 'template'=>'模板',
- 'pages'=>'分页选择',
- 'type_id'=>'类别',
- 'read_price'=>'金额、金币、积分',
- 'linkmenu'=>'联动菜单',
- 'downfiles'=>'多文件上传',
- 'map'=>'地图',
- 'omnipotent'=>'万能字段',
- 'sysconfig'=>'系统参数',
- 'ext_type'=>'扩展配置',
- ),
- //不允许添加的字段类型,这些字段讲不会在字段添加处显示
- 'not_allow_fields' => array('cat_id','type_id','title','keywords','pos_id','template','user_name'),
- //允许添加但必须唯一的字段
- 'unique_fields' => array('pages','read_price','author','copy_from','is_link'),
- //禁止被禁用的字段列表
- 'forbid_fields' => array('cat_id','title','url','template','user_name','create_time','update_time','list_order','status','ext_type'),
- //禁止被删除的字段列表
- 'forbid_delete' => array('cat_id','type_id','title','thumb','keywords','description','is_position','url','status','template','user_name','list_order','update_time','create_time','ext_type'),
- //可以追加 JS和CSS 的字段
- 'att_css_js' => array('text','textarea','box','number')
- );
- return $info[$arg];
- }
- /**
- * 下拉选择框
- */
- public static function select($array = array(), $id = 0, $default_option = '请选择' , $property = 'class=\'\'' )
- {
- $string = '<select '.$property.'>';
- $default_selected = (empty($id) && $default_option) ? 'selected' : '';
- if($default_option) $string .= "<option value='' $default_selected>$default_option</option>";
- if(!is_array($array) || count($array)== 0) return false;
- $ids = array();
- if(isset($id)) $ids = explode(',', $id);
- foreach($array as $key=>$value) {
- $selected = in_array($key, $ids) ? 'selected' : '';
- $string .= '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
- }
- $string .= '</select>';
- return $string;
- }
- /**
- * 单选框
- *
- * @param $array 选项 二维数组
- * @param $id 默认选中值
- * @param $property 属性
- * @param $inlayui 使用 layui 样式
- */
- public static function radio($array = array(), $id = 0, $property = '',$inlayui=0)
- {
- $string = '';
- foreach($array as $key=>$value) {
- $checked = trim($id)==trim($key) ? 'checked' : '';
- if($inlayui==1)
- {
- $string .= '<input type="radio" '.$property.' '.$checked.' value="'.$key.'" title="'.$value.'">';
- }
- else
- {
- $string .= '<div class="col-sm-auto">';
- $string .= '<div class="form-check mt10">';
- $string .= '<input type="radio" '.$property.' '.$checked.' value="'.$key.'" >';
- $string .= '<div class="form-check-label">'.$value.'</div>';
- $string .= '</div>';
- $string .= '</div>';
- }
- }
- return $string;
- }
- /**
- * 复选框
- *
- * @param $array 选项 二维数组
- * @param $id 默认选中值,多个用 '逗号'分割
- * @param $property 属性
- * @param $inlayui 使用 layui 样式
- */
- public static function checkbox($array = array(), $id = '', $property = '',$inlayui=0)
- {
- $string = '';
- $id = trim($id);
- if($id != '') $id = strpos($id, ',') ? explode(',', $id) : array($id);
- $i = 1;
- foreach($array as $key=>$value) {
- $key = trim($key);
- $checked = ($id && in_array($key, $id)) ? 'checked' : '';
- if($inlayui==1)
- {
- $string .= '<input type="checkbox" '.$property.' '.$checked.' value="'.htmlspecialchars($key).'" title="'.htmlspecialchars($value).'">';
- }
- else
- {
- $string .= '<div class="col-sm-auto">';
- $string .= '<div class="form-check mt10">';
- $string .= '<input type="checkbox" '.$property.' '.$checked.' value="'.$key.'" >';
- $string .= '<div class="form-check-label">'.$value.'</div>';
- $string .= '</div>';
- $string .= '</div>';
- }
- $i++;
- }
- return $string;
- }
- public static function switchbox($options = array(), $id = '', $property = '',$fieldname)
- {
- $string = '';
- $checked = $id ? 'checked' : '';
- $string .='<div class="col-sm-auto ">';
- $string .= '<input value="1" '.$property.' '.$checked.' type="checkbox" switch="success" class="row-switch" id="'.$fieldname.'" >';
- $string .= '<label class="switch-check-label mt10" for="'.$fieldname.'" data-on-label="'.$options[1].'" data-off-label="'.$options[0].'"></label>';
- $string .= '</div>';
- return $string;
- }
- //获得日期输入控件(依赖layui)
- public static function dateForm($name,$value,$array)
- {
- $name = $array['name']?$array['name']:$name;
- $dateFmt = $array['dateFmt'];
- $minDate = $array['minDate'];
- $maxDate = $array['maxDate'];
- $vel = $array['vel'];
- $velName = $array['velName'];
- $readOnly = $array['readOnly']==1?"readonly":'';
- $id = $array['id']?$array['id']:$array['defaultId'];
- $string = '<input type="text" '.$readOnly.' value="'.$value.'" class="layui-input chooseDate form-control '.$array['require'].'" id="'.$id.'" name="'.$name.'" data-format="'.$dateFmt.'" autocomplete="off" data-pattern = "'.$array['pattern'].'" data-errtips = "'.$array['errortips'].'" >';
- return $string;
- }
- //详情页模板选择
- public static function select_template($themename, $device, $module, $id = '', $str = '', $pre = '') {
- $themePath = Yii::$app->params['themePath'];
- $templatedir = $themePath.$themename.DIRECTORY_SEPARATOR.$device.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
- $config_path = $themePath.$themename.DIRECTORY_SEPARATOR.'config.php';
- $localdir = 'themes|'.$themename.'|'.$device.'|views|'.$module;
- $templates = glob($templatedir.$pre.'*.php');
- if(empty($templates)) return '<span style="color: #ff0000">没有可用模板</span>';
- $files = @array_map('basename', $templates);
- $names = array();
- if(file_exists($config_path)) {
- $names = include $config_path;
- }
- $templates = array();
- if(is_array($files)) {
- foreach($files as $file) {
- $key = substr($file, 0, -4);
- $templates[$key] = isset($names['file_explain'][$localdir][$file]) && !empty($names['file_explain'][$localdir][$file]) ? $names['file_explain'][$localdir][$file].'('.$file.')' : $file;
- }
- }
- ksort($templates);
- return self::select($templates, $id, '', $str);
- }
- }
- ?>
|