FecadminbaseBlock.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin;
  10. use fec\helpers\CRequest;
  11. use fec\helpers\CUrl;
  12. /**
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class FecadminbaseBlock{
  17. public $_param = [];
  18. # 默认值
  19. public $_pageNum = 1;
  20. public $_numPerPage = 50;
  21. # 主键 默认为id
  22. public $_paramKey = 'id';
  23. # 默认 排序字段 和 排序方式
  24. public $_sortDirection = 'desc';
  25. public $_orderField ; # 如果不设置,会默认使用主键排序;
  26. # 各种url
  27. public $_currentParamUrl;
  28. public $_currentUrlKey;
  29. public $_addUrl;
  30. public $_editUrl;
  31. public $_deleteUrl;
  32. # 当前的数据对象
  33. public $_obj ;
  34. public function __construct(){
  35. $this->initParam();
  36. }
  37. # 初始化param
  38. public function initParam(){
  39. $param = \fec\helpers\CRequest::param();
  40. if(empty($param['pageNum'])) $param['pageNum'] = $this->_pageNum ;
  41. if(empty($param['numPerPage'])) $param['numPerPage'] = $this->_numPerPage ;
  42. if(empty($param['orderField'])) $param['orderField'] = $this->_orderField ;
  43. if(empty($param['orderField'])) $param['orderField'] = $this->_paramKey ;
  44. if(empty($param['orderDirection'])) $param['orderDirection'] = $this->_sortDirection ;
  45. if(is_array($param) && !empty($param)){
  46. $this->_param = array_merge($this->_param, $param) ;
  47. }
  48. $this->_currentUrl = CUrl::getCurrentUrlNoParam();
  49. $this->_currentParamUrl = CUrl::getCurrentUrl();
  50. $this->_addUrl = $this->_addUrl ? $this->_addUrl : $this->_currentUrl;
  51. $this->_editUrl = $this->_editUrl ? $this->_editUrl : $this->_currentUrl;
  52. $this->_deleteUrl = $this->_deleteUrl ? $this->_deleteUrl : $this->_currentUrl;
  53. }
  54. # 顶部隐藏域
  55. public function getPagerForm(){
  56. $str = "";
  57. if(is_array($this->_param) && !empty($this->_param)){
  58. foreach($this->_param as $k=>$v){
  59. if($k != "_csrf"){
  60. $str .='<input type="hidden" name="'.$k.'" value="'.$v.'">';
  61. }
  62. }
  63. }
  64. return $str;
  65. }
  66. # 表单 搜索部分html生成 1
  67. public function getSearchBarHtml($data){
  68. //echo 1;exit;
  69. if(is_array($data) && !empty($data)){
  70. $r_data = [];
  71. $i = 0;
  72. foreach($data as $k=>$d){
  73. $type11 = $d['type'];
  74. if($type11 == 'select'){
  75. $value = $d['value'];
  76. $name = $d['name'];
  77. $title = $d['title'];
  78. $d['value'] = $this->getSearchBarSelectHtml($name,$value,$title);
  79. }else if($type11 == 'chosen_select'){
  80. $i++;
  81. $value = $d['value'];
  82. $name = $d['name'];
  83. $title = $d['title'];
  84. $d['value'] = $this->getSearchBarChosenSelectHtml($name,$value,$title,$i);
  85. }
  86. $r_data[$k] = $d;
  87. }
  88. }
  89. $searchBar = $this->getDbSearchBarHtml($r_data);
  90. return $searchBar;
  91. }
  92. # 表单 搜索部分html生成 2
  93. # select的name,option数据,option value为空的显示值,默认选中的值
  94. public function getSearchBarSelectHtml($name,$data,$null_value){
  95. if(is_array($data) && !empty($data)){
  96. $alibaba_account_select = '<select class="combox" name="'.$name.'">';
  97. $alibaba_account_select .= '<option value="">'.$null_value.'</option>';
  98. $selected = $this->_param[$name];
  99. if(is_array($selected) ){
  100. $selected = $selected['$regex'];
  101. }
  102. foreach($data as $k=>$v){
  103. //echo "$selected == $k";
  104. if($selected == $k){
  105. $alibaba_account_select .= '<option selected="selected" value="'.$k.'">'.$v.'</option>';
  106. }else{
  107. $alibaba_account_select .= '<option value="'.$k.'">'.$v.'</option>';
  108. }
  109. }
  110. $alibaba_account_select .= '</select>';
  111. return $alibaba_account_select;
  112. }else{
  113. return '';
  114. }
  115. }
  116. # 表单 搜索部分html生成 2
  117. # select的name,option数据,option value为空的显示值,默认选中的值
  118. public function getSearchBarChosenSelectHtml($name,$data,$title,$id=1){
  119. if(is_array($data) && !empty($data)){
  120. $alibaba_account_select .= '<script type="text/javascript">
  121. var config = {
  122. \'.chosen-select'.$id.'\' : {},
  123. \'.chosen-select'.$id.'-deselect\' : {allow_single_deselect:true},
  124. \'.chosen-select'.$id.'-no-single\' : {disable_search_threshold:10},
  125. \'.chosen-select'.$id.'-no-results\': {no_results_text:\'Oops, nothing found!\'},
  126. \'.chosen-select'.$id.'-width\' : {width:"95%"}
  127. }
  128. for (var selector in config) {
  129. $(selector).chosen(config[selector]);
  130. }
  131. </script>
  132. ';
  133. $alibaba_account_select .= '<select data-placeholder="Your Favorite Type of Bear" class="chosen-select'.$id.'" tabindex="7" name="'.$name.'">';
  134. $alibaba_account_select .= '<option value="">'.$title.'</option>';
  135. $selected = $this->_param[$name];
  136. if(is_array($selected) ){
  137. $selected = $selected['$regex'];
  138. }
  139. foreach($data as $k=>$v){
  140. //echo "$selected == $k";
  141. if($k){
  142. if($selected == $k){
  143. $alibaba_account_select .= '<option selected value="'.$k.'">'.$v.'</option>';
  144. }else{
  145. $alibaba_account_select .= '<option value="'.$k.'">'.$v.'</option>';
  146. }
  147. }
  148. }
  149. $alibaba_account_select .= '</select>';
  150. return $alibaba_account_select;
  151. }else{
  152. return '';
  153. }
  154. }
  155. public function customSearchBarHtml(){
  156. return '';
  157. }
  158. # 表单 搜索部分html生成 3
  159. public function getDbSearchBarHtml($data){
  160. $searchBar = '';
  161. if(!empty($data)){
  162. $searchBar .= '<input type="hidden" name="search_type" value="search" />';
  163. $searchBar .='<table class="searchContent">
  164. <tr>';
  165. foreach($data as $d){
  166. $type = $d['type'];
  167. $name = $d['name'];
  168. $title = $d['title'];
  169. $value = $d['value'];
  170. if($d['type'] == 'select'){
  171. $searchBar .= '<td>
  172. '.$value.'
  173. </td>';
  174. }else if($d['type'] == 'chosen_select'){
  175. $searchBar .= '<td>
  176. '.$value.'
  177. </td>';
  178. }else if($d['type'] == 'inputtext'){
  179. $searchBar .= '<td>
  180. '.$title.':<input type="text" value="'.(is_array($this->_param[$name]) ? $this->_param[$name]['$regex'] : $this->_param[$name]).'" name="'.$name.'" />
  181. </td>';
  182. }else if($d['type'] == 'inputdate'){
  183. $searchBar .= '<td>
  184. '.$title.'<input type="text" value="'.$this->_param[$name].'" name="'.$name.'" class="date" readonly="true" />
  185. </td>';
  186. }else if($d['type'] == 'inputdatefilter'){
  187. $value = $d['value'];
  188. if(is_array($value)){
  189. foreach($value as $t=>$title){
  190. $searchBar .= '<td>
  191. '.$title.'<input type="text" value="'.$this->_param[$name.'_'.$t].'" name="'.$name.'_'.$t.'" class="date" readonly="true" />
  192. </td>';
  193. }
  194. }
  195. }else if($d['type'] == 'inputfilter'){
  196. $value = $d['value'];
  197. if(is_array($value)){
  198. foreach($value as $t=>$title){
  199. $searchBar .= '<td>
  200. '.$title.'<input type="text" value="'.$this->_param[$name.'_'.$t].'" name="'.$name.'_'.$t.'" />
  201. </td>';
  202. }
  203. }
  204. }
  205. }
  206. $customSearchHtml = $this->customSearchBarHtml();
  207. $searchBar .= $customSearchHtml;
  208. $searchBar .= '</tr>
  209. </table>
  210. <div class="subBar">
  211. <ul>
  212. <li><div class="buttonActive"><div class="buttonContent"><button type="submit">检索</button></div></div></li>
  213. <!-- <li><a class="button" href="#" target="dialog" mask="true" title="查询框"><span>高级检索</span></a></li> -->
  214. </ul>
  215. </div>';
  216. }
  217. return $searchBar;
  218. }
  219. # 搜索部分
  220. public function getSearchBar(){
  221. $data = $this->getSearchArr();
  222. return $this->getSearchBarHtml($data);
  223. }
  224. # 搜索 生成where
  225. public function initDateWhere(&$query,$searchArr){
  226. foreach($searchArr as $field){
  227. $type = $field['type'];
  228. $name = $field['name'];
  229. $columns_type = isset($field['columns_type']) ? $field['columns_type'] : '';
  230. if($this->_param[$name] || $this->_param[$name.'_get'] || $this->_param[$name.'_lt']){
  231. if($type == 'inputtext' || $type == 'select' || $type == 'chosen_select'){
  232. if($columns_type == 'string'){
  233. if($query->where){
  234. $query->andWhere(['like', $name, $this->_param[$name]]);
  235. }else{
  236. $query->where(['like', $name, $this->_param[$name]]);
  237. //echo $name.$this->_param[$name];exit;
  238. }
  239. }else if($columns_type == 'int'){
  240. if($query->where){
  241. $query->andWhere([$name => (int)$this->_param[$name]]);
  242. }else{
  243. $query->where([$name => (int)$this->_param[$name]]);
  244. }
  245. }else if($columns_type == 'float'){
  246. if($query->where){
  247. $query->andWhere([$name => (float)$this->_param[$name]]);
  248. }else{
  249. $query->where([$name => (float)$this->_param[$name]]);
  250. }
  251. }else if($columns_type == 'date'){
  252. if($query->where){
  253. $query->andWhere([$name => $this->_param[$name]]);
  254. }else{
  255. $query->where([$name => $this->_param[$name]]);
  256. }
  257. }else{
  258. if($query->where){
  259. $query->andWhere([$name => $this->_param[$name]]);
  260. }else{
  261. $query->where([$name => $this->_param[$name]]);
  262. }
  263. }
  264. }else if($type == 'inputdatefilter'){
  265. $_gte = $this->_param[$name.'_gte'];
  266. $_lt = $this->_param[$name.'_lt'];
  267. if($columns_type == 'float'){
  268. $_gte = strtotime($_gte);
  269. $_lt = strtotime($_lt);
  270. }
  271. if($query->where){
  272. if($_gte){
  273. $query->andWhere(['>=', $name, $_gte]);
  274. }
  275. if($_lt){
  276. $query->andWhere(['<', $name, $_lt]);
  277. }
  278. }else{
  279. if($_gte){
  280. $query->where(['>=', $name, $_gte]);
  281. }
  282. if($_lt){
  283. $query->andWhere(['<', $name, $_lt]);
  284. }
  285. }
  286. //var_dump($query->where);
  287. }else if($type == 'inputfilter'){
  288. $_gte = $this->_param[$name.'_gte'];
  289. $_lt = $this->_param[$name.'_lt'];
  290. if($columns_type == 'int'){
  291. $_gte = (int)$_gte;
  292. $_lt = (int)$_lt;
  293. }else if($columns_type == 'float'){
  294. $_gte = (float)$_gte;
  295. $_lt = (float)$_lt;
  296. }
  297. if($query->where){
  298. if($_gte){
  299. $query->andWhere(['>=', $name, $_gte]);
  300. }
  301. if($_lt){
  302. $query->andWhere(['<', $name, $_lt]);
  303. }
  304. }else{
  305. if($_gte){
  306. $query->where(['>=', $name, $_gte]);
  307. }
  308. if($_lt){
  309. $query->andWhere(['<', $name, $_lt]);
  310. }
  311. }
  312. }else{
  313. if($query->where){
  314. $query->andWhere([$name => $this->_param[$name]]);
  315. }else{
  316. $query->where([$name => $this->_param[$name]]);
  317. }
  318. }
  319. }
  320. }
  321. // var_dump($query->where);exit;
  322. }
  323. # 编辑部分
  324. public function getEditBar(){
  325. if(!strstr($this->_currentParamUrl,"?")){
  326. $csvUrl = $this->_currentParamUrl."?type=export";
  327. }else{
  328. $csvUrl = $this->_currentParamUrl."&type=export";
  329. }
  330. return '<ul class="toolBar">
  331. <li><a class="add" href="'.$this->_editUrl.'" target="dialog" height="580" width="1000" drawable="true" mask="true"><span>添加</span></a></li>
  332. <li><a target="dialog" height="580" width="1000" drawable="true" mask="true" class="edit" href="'.$this->_editUrl.'?'.$this->_paramKey.'={sid_user}" ><span>修改</span></a></li>
  333. <li><a title="确实要删除这些记录吗?" target="selectedTodo" rel="'.$this->_paramKey.'s" postType="string" href="'.$this->_deleteUrl.'" class="delete"><span>批量删除</span></a></li>
  334. <li class="line">line</li>
  335. <li><a class="icon csvdownload" href="'.$csvUrl.'" target="dwzExport" targetType="navTab" title="实要导出这些记录吗?"><span>导出EXCEL</span></a></li>
  336. </ul>';
  337. }
  338. public function getToolBar($numCount,$pageNum,$numPerPage){
  339. return '<div class="pages">
  340. <span>显示</span>
  341. <select class="combox" name="numPerPage" onchange="navTabPageBreak({numPerPage:this.value})">
  342. <option '.($numPerPage == 2 ? 'selected': '' ).' value="2">2</option>
  343. <option '.($numPerPage == 6 ? 'selected': '' ).' value="6">6</option>
  344. <option '.($numPerPage == 20 ? 'selected': '' ).' value="20">20</option>
  345. <option '.($numPerPage == 50 ? 'selected': '' ).' value="50">50</option>
  346. <option '.($numPerPage == 100 ? 'selected': '' ).' value="100">100</option>
  347. <option '.($numPerPage == 200 ? 'selected': '' ).' value="200">200</option>
  348. </select>
  349. <span>条,共'.$numCount.'条</span>
  350. </div>
  351. <div class="pagination" targetType="navTab" totalCount="'.$numCount.'" numPerPage="'.$numPerPage.'" pageNumShown="10" currentPage="'.$pageNum.'"></div>
  352. ';
  353. }
  354. # 得到表格的头部
  355. public function getTableThead(){
  356. $table_th_bar = $this->getTableFieldArr();
  357. return $this->getTableTheadHtml($table_th_bar);
  358. }
  359. # table 表 标题 1
  360. public function getTableTheadHtml($table_th_bar){
  361. $table_th_bar = $this->getTableTheadArrInit($table_th_bar);
  362. $this->_param['orderField'] = $this->_param['orderField'] ? $this->_param['orderField'] : $this->_paramKey;
  363. $this->_param['orderDirection'] = $this->_param['orderDirection'] ? $this->_param['orderDirection'] : $this->_defaultDirection;
  364. foreach($table_th_bar as $k => $field){
  365. if($field['orderField'] == $this->_param['orderField']){
  366. $table_th_bar[$k]['class'] = $this->_param['orderDirection'];
  367. }
  368. }
  369. $str = '<thead><tr>';
  370. $str .= '<th width="22"><input type="checkbox" group="'.$this->_paramKey.'s" class="checkboxCtrl"></th>';
  371. foreach($table_th_bar as $b){
  372. $width = $b['width'];
  373. $label = $b['label'];
  374. $orderField = $b['orderField'];
  375. $class = isset($b['class']) ? $b['class'] : '';
  376. $align = isset($b['align']) ? 'align="'.$b['align'].'"' : '';
  377. $str .= '<th width="'.$width.'" '.$align.' orderField="'.$orderField.'" class="'.$class.'">'.$label.'</th>';
  378. }
  379. $str .= '<th width="80" >编辑</th>';
  380. $str .= '</tr></thead>';
  381. return $str;
  382. }
  383. # table 表 标题 2
  384. public function getTableTheadArrInit($table_columns){
  385. foreach($table_columns as $field){
  386. $d = [
  387. 'orderField' => $field['orderField'],
  388. // 'label' => $this->_obj->getAttributeLabel($field['orderField']) ,
  389. 'width' => $field['width'],
  390. 'align' => $field['align'],
  391. ];
  392. $d['label'] = $field['label'] ? $field['label'] : '';
  393. if(empty($d['label'] )){
  394. if($this->_obj){
  395. $d['label'] = $this->_obj->getAttributeLabel($field['orderField']) ;
  396. }else{
  397. $d['label'] = $field['orderField'];
  398. }
  399. }
  400. $table_th_bar[] = $d;
  401. }
  402. return $table_th_bar;
  403. }
  404. # 得到表格的内容部分
  405. public function getTableTbody(){
  406. $obj = $this->_obj;
  407. $searchArr = $this->getSearchArr();
  408. $query = $obj::find();
  409. if(is_array($searchArr) && !empty($searchArr)){
  410. $this->initDateWhere($query,$searchArr);
  411. }
  412. $this->_param['numCount'] = $query->count();
  413. $query->limit = $this->_param['numPerPage'];
  414. # 偏离值
  415. $query->offset = ($this->_param['pageNum'] -1)*$this->_param['numPerPage'] ;
  416. $query->orderBy([$this->_param['orderField']=> (($this->_param['orderDirection'] == 'desc') ? SORT_DESC : SORT_ASC)]);
  417. $data = $query->all();
  418. return $this->getTableTbodyHtml($data);
  419. }
  420. # table 内容部分
  421. public function getTableTbodyHtml($data){
  422. $fileds = $this->getTableFieldArr();
  423. $str .= '';
  424. $csrfString = \fec\helpers\CRequest::getCsrfString();
  425. foreach($data as $one){
  426. $str .= '<tr target="sid_user" rel="'.$one[$this->_paramKey].'">';
  427. $str .= '<td><input name="'.$this->_paramKey.'s" value="'.$one[$this->_paramKey].'" type="checkbox"></td>';
  428. foreach($fileds as $field){
  429. $orderField = $field['orderField'];
  430. $display = $field['display'];
  431. $val = $one[$orderField];
  432. if($val){
  433. if(isset($field['display']) && !empty($field['display'])){
  434. $display = $field['display'];
  435. $val = $display[$val] ? $display[$val] : $val;
  436. }
  437. if(isset($field['convert']) && !empty($field['convert'])){
  438. $convert = $field['convert'];
  439. foreach($convert as $origin =>$to){
  440. if(strstr($origin,'mongodate')){
  441. if(isset($val->sec)){
  442. $timestramp = $val->sec;
  443. if($to == 'date'){
  444. $val = date('Y-m-d',$timestramp);
  445. }else if($to == 'datetime'){
  446. $val = date('Y-m-d H:i:s',$timestramp);
  447. }else if($to == 'int'){
  448. $val = $timestramp;
  449. }
  450. }
  451. }else if(strstr($origin,'date')){
  452. if($to == 'date'){
  453. $val = date('Y-m-d',strtotime($val));
  454. }else if($to == 'datetime'){
  455. $val = date('Y-m-d H:i:s',strtotime($val));
  456. }else if($to == 'int'){
  457. $val = strtotime($val);
  458. }
  459. }else if($origin == 'int'){
  460. if($to == 'date'){
  461. $val = date('Y-m-d',$val);
  462. }else if($to == 'datetime'){
  463. $val = date('Y-m-d H:i:s',$val);
  464. }else if($to == 'int'){
  465. $val = $val;
  466. }
  467. }else if($origin == 'string'){
  468. if($to == 'img'){
  469. $t_width = isset($field['img_width']) ? $field['img_width'] : '100';
  470. $t_height = isset($field['img_height']) ? $field['img_height'] : '100';
  471. $val = '<img style="width:'.$t_width.'px;height:'.$t_height.'px" src="'.$val.'" />';;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. $str .= '<td>'.$val.'</td>';
  478. }
  479. $str .= '<td>
  480. <a title="编辑" target="dialog" class="btnEdit" mask="true" drawable="true" width="1000" height="580" href="'.$this->_editUrl.'?'.$this->_paramKey.'='.$one[$this->_paramKey].'" >编辑</a>
  481. <a title="删除" target="ajaxTodo" href="'.$this->_deleteUrl.'?'.$csrfString.'&'.$this->_paramKey.'='.$one[$this->_paramKey].'" class="btnDel">删除</a>
  482. </td>';
  483. $str .= '</tr>';
  484. }
  485. return $str ;
  486. }
  487. /*
  488. # 定义表格显示部分的配置
  489. public function getTableFieldArr(){
  490. $table_th_bar = [
  491. [
  492. 'orderField' => '_id',
  493. 'label' => 'ID',
  494. 'width' => '40',
  495. 'align' => 'center',
  496. ],
  497. [
  498. 'orderField' => 'keyword',
  499. 'label' => '关键字',
  500. 'width' => '110',
  501. 'align' => 'left',
  502. ],
  503. # select 选择类型 display 对应的是一个数组,通过key 对应值
  504. # 一般是状态,譬如 1 对应激活,2对应关闭等。
  505. [
  506. 'orderField' => 'unit',
  507. 'label' => '站点',
  508. 'width' => '110',
  509. 'align' => 'left',
  510. 'display' => CConfig::param("channel_type"),
  511. ],
  512. # 图片类型:
  513. [
  514. 'orderField' => 'img',
  515. 'label' => '图片',
  516. 'width' => '110',
  517. 'align' => 'left',
  518. 'convert' => ['string' => 'img'],
  519. 'img_width' => '100', # 图片宽度
  520. 'img_height' => '100', # 图片高度
  521. ],
  522. [
  523. 'orderField' => 'created_at',
  524. 'label' => '创建时间',
  525. 'width' => '190',
  526. 'align' => 'center',
  527. //'convert' => ['datetime' =>'date'],
  528. ],
  529. # 把 datetime(Y-m-d H:i:s) 转化成datetime(Y-m-d)
  530. [
  531. 'orderField' => 'updated_at',
  532. 'label' => '更新时间',
  533. 'width' => '190',
  534. 'align' => 'center',
  535. 'convert' => ['datetime' =>'date'], # int date datetime 显示的转换
  536. ],
  537. [
  538. 'orderField' => 'updated_at',
  539. 'label' => '更新时间',
  540. 'width' => '190',
  541. 'align' => 'center',
  542. 'convert' => ['datetime' =>'int'], # 时间戳转换成datetime格式
  543. ],
  544. ];
  545. return $table_th_bar ;
  546. }
  547. */
  548. }