global.func.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. /**
  4. * 生成模板中所有PC标签的MD5
  5. * @param $file 模板文件地址
  6. */
  7. function tag_md5($file) {
  8. $data = file_get_contents($file);
  9. preg_match_all("/\{pc:(\w+)\s+([^}]+)\}/i", stripslashes($data),$matches);
  10. $arr = array();
  11. if(is_array($matches) && !empty($matches)) foreach($matches[0] as $k=>$v) {
  12. if (!$v) continue;
  13. $md5 = md5($v);
  14. $arr[0][$k] = $md5;
  15. $arr[1][$md5] = $v;
  16. }
  17. return $arr;
  18. }
  19. /**
  20. * 生成pc标签
  21. * @param $op 操作名
  22. * @param $data 数据
  23. */
  24. function creat_pc_tag($op, $data) {
  25. $str = '{pc:'.$op.' ';
  26. if (is_array($data)) {
  27. foreach ($data as $k=>$v) {
  28. if ($v) $str .= $str ? " $k=\"$v\"" : "$k=\"$v\"";
  29. }
  30. } else {
  31. $str .= $data;
  32. }
  33. return $str.'}';
  34. }
  35. /**
  36. * 替换模板中的PC标签
  37. * @param $filepath 文件地址
  38. * @param $old_tag 老PC标签
  39. * @param $new_tag 新PC标签
  40. * @param $style 风格
  41. * @param $dir 目录名
  42. */
  43. function replace_pc_tag($filepath, $old_tag, $new_tag, $style, $dir) {
  44. if (file_exists($filepath)) {
  45. creat_template_bak($filepath, $style, $dir);
  46. $data = @file_get_contents($filepath);
  47. $data = str_replace($old_tag, $new_tag, $data);
  48. if (!is_writable($filepath)) return false;
  49. @file_put_contents($filepath, $data);
  50. return true;
  51. }
  52. }
  53. /**
  54. * 生成模板临时文件
  55. * @param $filepath 文件地址
  56. * @param $style 风格
  57. * @param $dir 目录名
  58. */
  59. function creat_template_bak($filepath, $style, $dir) {
  60. $filename = basename($filepath);
  61. $template_bak_db = pc_base::load_model('template_bak_model');
  62. $template_bak_db->insert(array('creat_at'=>SYS_TIME,'fileid'=>$style."_".$dir."_".$filename, 'userid'=>param::get_cookie('userid'), 'username'=>param::get_cookie('admin_username'), 'template'=>new_addslashes(file_get_contents($filepath))));
  63. }
  64. /**
  65. * 生成标签选项
  66. * @param $id HTML ID号
  67. * @param $data 生成条件
  68. * @param $value 当前值
  69. * @param $op 操作名
  70. * @return html 返回HTML代码
  71. */
  72. function creat_form($id, $data, $value = '', $op = '') {
  73. pc_base::load_sys_class('form', '', 0);
  74. if (empty($value)) $value = $data['defaultvalue'];
  75. $str = $ajax = '';
  76. if($data['ajax']['name']) {
  77. if($data['ajax']['m']) {
  78. $url = '$.get(\'?m=content&c=push&a=public_ajax_get\', {html: this.value, id:\''.$data['ajax']['id'].'\', action: \''.$data['ajax']['action'].'\', module: \''.$data['ajax']['m'].'\', pc_hash: \''.$_SESSION['pc_hash'].'\'}, function(data) {$(\'#'.$id.'_td\').html(data)});';
  79. } else {
  80. $url = '$.get(\'?m=template&c=file&a=public_ajax_get\', { html: this.value, id:\''.$data['ajax']['id'].'\', action: \''.$data['ajax']['action'].'\', op: \''.$op.'\', style: \'default\', pc_hash: \''.$_SESSION['pc_hash'].'\'}, function(data) {$(\'#'.$id.'_td\').html(data)});';
  81. }
  82. }
  83. switch ($data['htmltype']) {
  84. case 'input':
  85. if($data['ajax']['name']) {
  86. $ajax = 'onblur="'.$url.'"';
  87. }
  88. $str .= '<input type="text" name="'.$id.'" id="'.$id.'" value="'.$value.'" size="30" />';
  89. break;
  90. case 'select':
  91. if($data['ajax']['name']) {
  92. $ajax = 'onchange="'.$url.'"';
  93. }
  94. $str .= form::select($data['data'], $value, "name='$id' id='$id' $ajax");
  95. break;
  96. case 'checkbox':
  97. if($data['ajax']['name']) {
  98. $ajax = ' onclick="'.$url.'"';
  99. }
  100. if (is_array($value)) implode(',', $value);
  101. $str .= form::checkbox($data['data'], $value, "name='".$id."[]'".$ajax, '', '120');
  102. break;
  103. case 'radio':
  104. if($data['ajax']['name']) {
  105. $ajax = ' onclick="'.$url.'"';
  106. }
  107. $str .= form::radio($data['data'], $value, "name='$id'$ajax", '', '120');
  108. break;
  109. case 'input_select':
  110. if($data['ajax']['name']) {
  111. $ajax = ';'.$url;
  112. }
  113. $str .= '<input type="text" name="'.$id.'" id="'.$id.'" value="'.$value.'" size="30" />'.form::select($data['data'], $value, "name='select_$id' id='select_$id' onchange=\"$('#$id').val(this.value);$ajax\"");
  114. break;
  115. case 'input_select_category':
  116. if($data['ajax']['name']) {
  117. $ajax = ';'.$url;
  118. }
  119. $str .= '<input type="text" name="'.$id.'" id="'.$id.'" value="'.$value.'" size="30" />'.form::select_category('', $value, "name='select_$id' id='select_$id' onchange=\"$('#$id').val(this.value);$ajax\"", '', (isset($data['data']['modelid']) ? $data['data']['modelid'] : 0), (isset($data['data']['type']) ? $data['data']['type'] : -1), (isset($data['data']['onlysub']) ? $data['data']['onlysub'] : 0));
  120. break;
  121. case 'select_yp_model':
  122. if($data['ajax']['name']) {
  123. $ajax = ';'.$url;
  124. }
  125. $yp_models = getcache('yp_model', 'commons');
  126. $d = array(L('please_select'));
  127. if (is_array($yp_models) && !empty($yp_models)) {
  128. foreach ($yp_models as $k =>$v) {
  129. $d[$k] = $v['name'];
  130. }
  131. }
  132. $str .= '<input type="text" name="'.$id.'" id="'.$id.'" value="'.$value.'" size="30" />'.form::select($d, $value, "name='select_$id' id='select_$id' onchange=\"$('#$id').val(this.value);$ajax\"");
  133. break;
  134. }
  135. if (!empty($data['validator'])) {
  136. $str .= '<script type="text/javascript">$(function(){$("#'.$id.'").formValidator({onshow:"'.L('input').$data['name'].'。",onfocus:"'.L('input').$data['name'].'。"'.($data['empty'] ? ',empty:true' : '').'})';
  137. if ($data['htmltype'] != 'select' && (isset($data['validator']['min']) || isset($data['validator']['max']))) {
  138. $str .= ".inputValidator({".(isset($data['validator']['min']) ? 'min:'.$data['validator']['min'].',' : '').(isset($data['validator']['max']) ? 'max:'.$data['validator']['max'].',' : '')." onerror:'".$data['name'].L('should', '', 'template').(isset($data['validator']['min']) ? ' '.L('is_greater_than', '', 'template').$data['validator']['min'].L('lambda', '', 'template') : '').(isset($data['validator']['max']) ? ' '.L('less_than', '', 'template').$data['validator']['max'].L('lambda', '', 'template') : '')."。'})";
  139. }
  140. if ($data['htmltype'] != 'checkbox' && $data['htmltype'] != 'radio' && isset($data['validator']['reg'])) {
  141. $str .= '.regexValidator({regexp:"'.$data['validator']['reg'].'"'.(isset($data['validator']['reg_param']) ? ",param:'".$data['validator']['reg_param']."'" : '').(isset($data['validator']['reg_msg']) ? ',onerror:"'.$data['validator']['reg_msg'].'"' : '').'})';
  142. }
  143. $str .=";});</script>";
  144. }
  145. return $str;
  146. }
  147. /**
  148. * 编辑PC标签时,生成跳转URL地址
  149. * @param $action 操作
  150. */
  151. function creat_url($action) {
  152. $url = '';
  153. foreach ($_GET as $k=>$v) {
  154. if ($k=='action') $v = $action;
  155. $url .= $url ? "&$k=$v" : "$k=$v";
  156. }
  157. return $url;
  158. }
  159. /**
  160. * 生成可视化模板
  161. * @param $html 模板代码
  162. * @param $style 风格
  163. * @param $dir 目录
  164. * @param $file 文件名
  165. */
  166. function visualization($html, $style = '', $dir = '', $file = '') {
  167. $change = "<link href=\"".CSS_PATH."dialog.css\" rel=\"stylesheet\" type=\"text/css\" />
  168. <link rel=\"stylesheet\" type=\"text/css\" href=\"".CSS_PATH."admin_visualization.css\" />
  169. <script language=\"javascript\" type=\"text/javascript\" src=\"".JS_PATH."dialog.js\"></script>
  170. <script type='text/javascript' src='".JS_PATH."jquery.min.js'></script>
  171. <script type='text/javascript'>
  172. var pc_hash = '".$_SESSION['pc_hash']."';
  173. $(function(){
  174. $('a').attr('href', 'javascript:void(0)').attr('target', '');
  175. $('.admin_piao_edit').click(function(){
  176. var url = '?m=template&c=file&a=edit_pc_tag';
  177. if($(this).parent('.admin_piao').attr('pc_action') == 'block') url = '?m=block&c=block_admin&a=add';
  178. window.top.art.dialog({title:'".L('pc_tag','' ,'template')."',id:'edit',iframe:url+'&style=$style&dir=$dir&file=$file&'+$(this).parent('.admin_piao').attr('data'),width:'700',height:'500'}, function(){var d = window.top.art.dialog({id:'edit'}).data.iframe;d.document.getElementById('dosubmit').click();return false;}, function(){window.top.art.dialog({id:'edit'}).close()});})
  179. $('.admin_block').click(function(){
  180. window.top.art.dialog({title:'".L('pc_tag','' ,'template')."',id:'edit',iframe:'?m=block&c=block_admin&a=block_update&id='+$(this).attr('blockid'),width:'700',height:'500'}, function(){var d = window.top.art.dialog({id:'edit'}).data.iframe;d.document.getElementById('dosubmit').click();return false;}, function(){window.top.art.dialog({id:'edit'}).close()});
  181. });
  182. })</script><div id=\"PC__contentHeight\" style=\"display:none\">80</div>";
  183. $html = str_replace('</body>', $change.'</body>', $html, $num);
  184. if (!$num) $html .= $change;
  185. return $html;
  186. }