FormElements.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\common\components;
  3. use Yii;
  4. class FormElements
  5. {
  6. static function getInstCls(){
  7. static $m_instCls = null;
  8. if (!isset($m_instCls)){$m_instCls = new FormElements();}
  9. return $m_instCls;
  10. }
  11. /**树状父级选择栏
  12. * @param $datas
  13. * @param int $top_id 顶级Id
  14. * @param int $menu_id 当前选中菜单ID
  15. * @param string $name 对应显示标题的字段
  16. * @param string $alt
  17. * @param string $id
  18. * @param string $property
  19. * @return string
  20. */
  21. public static function tree_select($datas,$top_id = 0, $menu_id = 0, $select_name = 'parent_id', $alt = '' , $select_id ='', $property = 'class=\'\'')
  22. {
  23. $tree = new Tree;
  24. $select_id = $select_id ? $select_id : $select_name;
  25. $string = "<select name='$select_name' id='$select_id' $property>\n<option value=''>$alt</option>\n";
  26. $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
  27. $tree->init($datas);
  28. $string .= $tree->get_tree($top_id, $str, $menu_id);
  29. $string .= '</select>';
  30. return $string;
  31. }
  32. //模型字段管理的一些初始参数
  33. public static function getFieldsInfo($arg)
  34. {
  35. $info=array(
  36. 'types' => array(
  37. 'text'=>'单行文本',
  38. 'textarea'=>'多行文本',
  39. 'editor'=>'编辑器',
  40. 'cat_id'=>'栏目',
  41. 'title'=>'标题',
  42. 'box'=>'选项',
  43. 'image'=>'图片',
  44. 'images'=>'多图片',
  45. 'number'=>'数字',
  46. 'datetime'=>'日期和时间',
  47. 'pos_id'=>'推荐位',
  48. 'keywords'=>'关键词',
  49. 'author'=>'作者',
  50. 'copy_from'=>'来源',
  51. 'group_id'=>'用户组',
  52. 'is_link'=>'转向链接',
  53. 'template'=>'模板',
  54. 'pages'=>'分页选择',
  55. 'type_id'=>'类别',
  56. 'read_price'=>'金额、金币、积分',
  57. 'linkmenu'=>'联动菜单',
  58. 'downfiles'=>'多文件上传',
  59. 'map'=>'地图',
  60. 'omnipotent'=>'万能字段',
  61. 'sysconfig'=>'系统参数',
  62. 'ext_type'=>'扩展配置',
  63. ),
  64. //不允许添加的字段类型,这些字段讲不会在字段添加处显示
  65. 'not_allow_fields' => array('cat_id','type_id','title','keywords','pos_id','template','user_name'),
  66. //允许添加但必须唯一的字段
  67. 'unique_fields' => array('pages','read_price','author','copy_from','is_link'),
  68. //禁止被禁用的字段列表
  69. 'forbid_fields' => array('cat_id','title','url','template','user_name','create_time','update_time','list_order','status','ext_type'),
  70. //禁止被删除的字段列表
  71. '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'),
  72. //可以追加 JS和CSS 的字段
  73. 'att_css_js' => array('text','textarea','box','number')
  74. );
  75. return $info[$arg];
  76. }
  77. /**
  78. * 下拉选择框
  79. */
  80. public static function select($array = array(), $id = 0, $default_option = '请选择' , $property = 'class=\'\'' )
  81. {
  82. $string = '<select '.$property.'>';
  83. $default_selected = (empty($id) && $default_option) ? 'selected' : '';
  84. if($default_option) $string .= "<option value='' $default_selected>$default_option</option>";
  85. if(!is_array($array) || count($array)== 0) return false;
  86. $ids = array();
  87. if(isset($id)) $ids = explode(',', $id);
  88. foreach($array as $key=>$value) {
  89. $selected = in_array($key, $ids) ? 'selected' : '';
  90. $string .= '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
  91. }
  92. $string .= '</select>';
  93. return $string;
  94. }
  95. /**
  96. * 单选框
  97. *
  98. * @param $array 选项 二维数组
  99. * @param $id 默认选中值
  100. * @param $property 属性
  101. * @param $inlayui 使用 layui 样式
  102. */
  103. public static function radio($array = array(), $id = 0, $property = '',$inlayui=0)
  104. {
  105. $string = '';
  106. foreach($array as $key=>$value) {
  107. $checked = trim($id)==trim($key) ? 'checked' : '';
  108. if($inlayui==1)
  109. {
  110. $string .= '<input type="radio" '.$property.' '.$checked.' value="'.$key.'" title="'.$value.'">';
  111. }
  112. else
  113. {
  114. $string .= '<div class="col-sm-auto">';
  115. $string .= '<div class="form-check mt10">';
  116. $string .= '<input type="radio" '.$property.' '.$checked.' value="'.$key.'" >';
  117. $string .= '<div class="form-check-label">'.$value.'</div>';
  118. $string .= '</div>';
  119. $string .= '</div>';
  120. }
  121. }
  122. return $string;
  123. }
  124. /**
  125. * 复选框
  126. *
  127. * @param $array 选项 二维数组
  128. * @param $id 默认选中值,多个用 '逗号'分割
  129. * @param $property 属性
  130. * @param $inlayui 使用 layui 样式
  131. */
  132. public static function checkbox($array = array(), $id = '', $property = '',$inlayui=0)
  133. {
  134. $string = '';
  135. $id = trim($id);
  136. if($id != '') $id = strpos($id, ',') ? explode(',', $id) : array($id);
  137. $i = 1;
  138. foreach($array as $key=>$value) {
  139. $key = trim($key);
  140. $checked = ($id && in_array($key, $id)) ? 'checked' : '';
  141. if($inlayui==1)
  142. {
  143. $string .= '<input type="checkbox" '.$property.' '.$checked.' value="'.htmlspecialchars($key).'" title="'.htmlspecialchars($value).'">';
  144. }
  145. else
  146. {
  147. $string .= '<div class="col-sm-auto">';
  148. $string .= '<div class="form-check mt10">';
  149. $string .= '<input type="checkbox" '.$property.' '.$checked.' value="'.$key.'" >';
  150. $string .= '<div class="form-check-label">'.$value.'</div>';
  151. $string .= '</div>';
  152. $string .= '</div>';
  153. }
  154. $i++;
  155. }
  156. return $string;
  157. }
  158. public static function switchbox($options = array(), $id = '', $property = '',$fieldname)
  159. {
  160. $string = '';
  161. $checked = $id ? 'checked' : '';
  162. $string .='<div class="col-sm-auto ">';
  163. $string .= '<input value="1" '.$property.' '.$checked.' type="checkbox" switch="success" class="row-switch" id="'.$fieldname.'" >';
  164. $string .= '<label class="switch-check-label mt10" for="'.$fieldname.'" data-on-label="'.$options[1].'" data-off-label="'.$options[0].'"></label>';
  165. $string .= '</div>';
  166. return $string;
  167. }
  168. //获得日期输入控件(依赖layui)
  169. public static function dateForm($name,$value,$array)
  170. {
  171. $name = $array['name']?$array['name']:$name;
  172. $dateFmt = $array['dateFmt'];
  173. $minDate = $array['minDate'];
  174. $maxDate = $array['maxDate'];
  175. $vel = $array['vel'];
  176. $velName = $array['velName'];
  177. $readOnly = $array['readOnly']==1?"readonly":'';
  178. $id = $array['id']?$array['id']:$array['defaultId'];
  179. $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'].'" >';
  180. return $string;
  181. }
  182. //详情页模板选择
  183. public static function select_template($themename, $device, $module, $id = '', $str = '', $pre = '') {
  184. $themePath = Yii::$app->params['themePath'];
  185. $templatedir = $themePath.$themename.DIRECTORY_SEPARATOR.$device.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
  186. $config_path = $themePath.$themename.DIRECTORY_SEPARATOR.'config.php';
  187. $localdir = 'themes|'.$themename.'|'.$device.'|views|'.$module;
  188. $templates = glob($templatedir.$pre.'*.php');
  189. if(empty($templates)) return '<span style="color: #ff0000">没有可用模板</span>';
  190. $files = @array_map('basename', $templates);
  191. $names = array();
  192. if(file_exists($config_path)) {
  193. $names = include $config_path;
  194. }
  195. $templates = array();
  196. if(is_array($files)) {
  197. foreach($files as $file) {
  198. $key = substr($file, 0, -4);
  199. $templates[$key] = isset($names['file_explain'][$localdir][$file]) && !empty($names['file_explain'][$localdir][$file]) ? $names['file_explain'][$localdir][$file].'('.$file.')' : $file;
  200. }
  201. }
  202. ksort($templates);
  203. return self::select($templates, $id, '', $str);
  204. }
  205. }
  206. ?>