tpl.func.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. if (defined('IN_MOBILE')) {
  8. load()->app('tpl');
  9. } else {
  10. load()->web('tpl');
  11. }
  12. function tpl_form_field_date($name, $value = '', $withtime = false) {
  13. return _tpl_form_field_date($name, $value, $withtime);
  14. }
  15. function tpl_form_field_clock($name, $value = '') {
  16. $s = '';
  17. if(!defined('TPL_INIT_CLOCK_TIME')) {
  18. $s .= '
  19. <script type="text/javascript">
  20. require(["clockpicker"], function($){
  21. $(function(){
  22. $(".clockpicker").clockpicker({
  23. autoclose: true
  24. });
  25. });
  26. });
  27. </script>
  28. ';
  29. define('TPL_INIT_CLOCK_TIME', 1);
  30. }
  31. $time = date('H:i');
  32. if(!empty($value)) {
  33. if(!strexists($value, ':')) {
  34. $time = date('H:i', $value);
  35. } else {
  36. $time = $value;
  37. }
  38. }
  39. $s .= ' <div class="input-group clockpicker">
  40. <span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
  41. <input type="text" name="'.$name.'" value="'.$time.'" class="form-control">
  42. </div>';
  43. return $s;
  44. }
  45. function tpl_form_field_daterange($name, $value = array(), $time = false) {
  46. $s = '';
  47. if (empty($time) && !defined('TPL_INIT_DATERANGE_DATE')) {
  48. $s = '
  49. <script type="text/javascript">
  50. require(["daterangepicker"], function(){
  51. $(function(){
  52. $(".daterange.daterange-date").each(function(){
  53. var elm = this;
  54. $(this).daterangepicker({
  55. startDate: $(elm).prev().prev().val(),
  56. endDate: $(elm).prev().val(),
  57. format: "YYYY-MM-DD"
  58. }, function(start, end){
  59. $(elm).find(".date-title").html(start.toDateStr() + " 至 " + end.toDateStr());
  60. $(elm).prev().prev().val(start.toDateStr());
  61. $(elm).prev().val(end.toDateStr());
  62. });
  63. });
  64. });
  65. });
  66. </script>
  67. ';
  68. define('TPL_INIT_DATERANGE_DATE', true);
  69. }
  70. if (!empty($time) && !defined('TPL_INIT_DATERANGE_TIME')) {
  71. $s = '
  72. <script type="text/javascript">
  73. require(["daterangepicker"], function(){
  74. $(function(){
  75. $(".daterange.daterange-time").each(function(){
  76. var elm = this;
  77. $(this).daterangepicker({
  78. startDate: $(elm).prev().prev().val(),
  79. endDate: $(elm).prev().val(),
  80. format: "YYYY-MM-DD HH:mm",
  81. timePicker: true,
  82. timePicker12Hour : false,
  83. timePickerIncrement: 1,
  84. minuteStep: 1
  85. }, function(start, end){
  86. $(elm).find(".date-title").html(start.toDateTimeStr() + " 至 " + end.toDateTimeStr());
  87. $(elm).prev().prev().val(start.toDateTimeStr());
  88. $(elm).prev().val(end.toDateTimeStr());
  89. });
  90. });
  91. });
  92. });
  93. </script>
  94. ';
  95. define('TPL_INIT_DATERANGE_TIME', true);
  96. }
  97. if ($value['starttime'] !== false && $value['start'] !== false) {
  98. if($value['start']) {
  99. $value['starttime'] = empty($time) ? date('Y-m-d',strtotime($value['start'])) : date('Y-m-d H:i',strtotime($value['start']));
  100. }
  101. $value['starttime'] = empty($value['starttime']) ? (empty($time) ? date('Y-m-d') : date('Y-m-d H:i') ): $value['starttime'];
  102. } else {
  103. $value['starttime'] = '请选择';
  104. }
  105. if ($value['endtime'] !== false && $value['end'] !== false) {
  106. if($value['end']) {
  107. $value['endtime'] = empty($time) ? date('Y-m-d',strtotime($value['end'])) : date('Y-m-d H:i',strtotime($value['end']));
  108. }
  109. $value['endtime'] = empty($value['endtime']) ? $value['starttime'] : $value['endtime'];
  110. } else {
  111. $value['endtime'] = '请选择';
  112. }
  113. $s .= '
  114. <input name="'.$name . '[start]'.'" type="hidden" value="'. $value['starttime'].'" />
  115. <input name="'.$name . '[end]'.'" type="hidden" value="'. $value['endtime'].'" />
  116. <button class="btn btn-default daterange '.(!empty($time) ? 'daterange-time' : 'daterange-date').'" type="button"><span class="date-title">'.$value['starttime'].' 至 '.$value['endtime'].'</span> <i class="fa fa-calendar"></i></button>
  117. ';
  118. return $s;
  119. }
  120. function tpl_form_field_calendar($name, $values = array()) {
  121. $html = '';
  122. if (!defined('TPL_INIT_CALENDAR')) {
  123. $html .= '
  124. <script type="text/javascript">
  125. function handlerCalendar(elm) {
  126. require(["moment"], function(moment){
  127. var tpl = $(elm).parent().parent();
  128. var year = tpl.find("select.tpl-year").val();
  129. var month = tpl.find("select.tpl-month").val();
  130. var day = tpl.find("select.tpl-day");
  131. day[0].options.length = 1;
  132. if(year && month) {
  133. var date = moment(year + "-" + month, "YYYY-M");
  134. var days = date.daysInMonth();
  135. for(var i = 1; i <= days; i++) {
  136. var opt = new Option(i, i);
  137. day[0].options.add(opt);
  138. }
  139. if(day.attr("data-value")!=""){
  140. day.val(day.attr("data-value"));
  141. } else {
  142. day[0].options[0].selected = "selected";
  143. }
  144. }
  145. if($("select").niceSelect) {
  146. $("select").niceSelect("update");
  147. }
  148. });
  149. }
  150. require([""], function(){
  151. $(".tpl-calendar").each(function(){
  152. handlerCalendar($(this).find("select.tpl-year")[0]);
  153. });
  154. });
  155. </script>';
  156. define('TPL_INIT_CALENDAR', true);
  157. }
  158. if (empty($values) || !is_array($values)) {
  159. $values = array(0,0,0);
  160. }
  161. $values['year'] = intval($values['year']);
  162. $values['month'] = intval($values['month']);
  163. $values['day'] = intval($values['day']);
  164. if (empty($values['year'])) {
  165. $values['year'] = '1980';
  166. }
  167. $year = array(date('Y'), '1914');
  168. $html .= '<div class="row row-fix tpl-calendar">
  169. <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
  170. <select name="' . $name . '[year]" onchange="handlerCalendar(this)" class="form-control tpl-year">
  171. <option value="">年</option>';
  172. for ($i = $year[1]; $i <= $year[0]; $i++) {
  173. $html .= '<option value="' . $i . '"' . ($i == $values['year'] ? ' selected="selected"' : '') . '>' . $i . '</option>';
  174. }
  175. $html .= ' </select>
  176. </div>
  177. <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
  178. <select name="' . $name . '[month]" onchange="handlerCalendar(this)" class="form-control tpl-month">
  179. <option value="">月</option>';
  180. for ($i = 1; $i <= 12; $i++) {
  181. $html .= '<option value="' . $i . '"' . ($i == $values['month'] ? ' selected="selected"' : '') . '>' . $i . '</option>';
  182. }
  183. $html .= ' </select>
  184. </div>
  185. <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
  186. <select name="' . $name . '[day]" data-value="' . $values['day'] . '" class="form-control tpl-day">
  187. <option value="0">日</option>
  188. </select>
  189. </div>
  190. </div>';
  191. return $html;
  192. }
  193. function tpl_form_field_district($name, $values = array()) {
  194. $html = '';
  195. if (!defined('TPL_INIT_DISTRICT')) {
  196. $html .= '
  197. <script type="text/javascript">
  198. require(["district"], function(dis){
  199. $(".tpl-district-container").each(function(){
  200. var elms = {};
  201. elms.province = $(this).find(".tpl-province")[0];
  202. elms.city = $(this).find(".tpl-city")[0];
  203. elms.district = $(this).find(".tpl-district")[0];
  204. var vals = {};
  205. vals.province = $(elms.province).attr("data-value");
  206. vals.city = $(elms.city).attr("data-value");
  207. vals.district = $(elms.district).attr("data-value");
  208. dis.render(elms, vals, {withTitle: true});
  209. });
  210. });
  211. </script>';
  212. define('TPL_INIT_DISTRICT', true);
  213. }
  214. if (empty($values) || !is_array($values)) {
  215. $values = array('province'=>'','city'=>'','district'=>'');
  216. }
  217. if(empty($values['province'])) {
  218. $values['province'] = '';
  219. }
  220. if(empty($values['city'])) {
  221. $values['city'] = '';
  222. }
  223. if(empty($values['district'])) {
  224. $values['district'] = '';
  225. }
  226. $html .= '
  227. <div class="row row-fix tpl-district-container">
  228. <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
  229. <select name="' . $name . '[province]" data-value="' . $values['province'] . '" class="form-control tpl-province">
  230. </select>
  231. </div>
  232. <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
  233. <select name="' . $name . '[city]" data-value="' . $values['city'] . '" class="form-control tpl-city">
  234. </select>
  235. </div>
  236. <div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
  237. <select name="' . $name . '[district]" data-value="' . $values['district'] . '" class="form-control tpl-district">
  238. </select>
  239. </div>
  240. </div>';
  241. return $html;
  242. }
  243. function tpl_form_field_category_2level($name, $parents, $children, $parentid, $childid){
  244. $html = '
  245. <script type="text/javascript">
  246. window._' . $name . ' = ' . json_encode($children) . ';
  247. </script>';
  248. if (!defined('TPL_INIT_CATEGORY')) {
  249. $html .= '
  250. <script type="text/javascript">
  251. function renderCategory(obj, name){
  252. var index = obj.options[obj.selectedIndex].value;
  253. require([\'jquery\', \'util\'], function($, u){
  254. $selectChild = $(\'#\'+name+\'_child\');
  255. var html = \'<option value="0">请选择二级分类</option>\';
  256. if (!window[\'_\'+name] || !window[\'_\'+name][index]) {
  257. $selectChild.html(html);
  258. return false;
  259. }
  260. for(var i=0; i< window[\'_\'+name][index].length; i++){
  261. html += \'<option value="\'+window[\'_\'+name][index][i][\'id\']+\'">\'+window[\'_\'+name][index][i][\'name\']+\'</option>\';
  262. }
  263. $selectChild.html(html);
  264. if($("select").niceSelect) {
  265. $("select").niceSelect("update");
  266. }
  267. });
  268. }
  269. </script>
  270. ';
  271. define('TPL_INIT_CATEGORY', true);
  272. }
  273. $html .=
  274. '<div class="row row-fix tpl-category-container">
  275. <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
  276. <select class="form-control tpl-category-parent we7-select" id="' . $name . '_parent" name="' . $name . '[parentid]" onchange="renderCategory(this,\'' . $name . '\')">
  277. <option value="0">请选择一级分类</option>';
  278. $ops = '';
  279. if(!empty($parents)) {
  280. foreach ($parents as $row) {
  281. $html .= '
  282. <option value="' . $row['id'] . '" ' . (($row['id'] == $parentid) ? 'selected="selected"' : '') . '>' . $row['name'] . '</option>';
  283. }
  284. }
  285. $html .= '
  286. </select>
  287. </div>
  288. <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
  289. <select class="form-control tpl-category-child we7-select" id="' . $name . '_child" name="' . $name . '[childid]">
  290. <option value="0">请选择二级分类</option>';
  291. if (!empty($parentid) && !empty($children[$parentid])) {
  292. foreach ($children[$parentid] as $row) {
  293. $html .= '
  294. <option value="' . $row['id'] . '"' . (($row['id'] == $childid) ? 'selected="selected"' : '') . '>' . $row['name'] . '</option>';
  295. }
  296. }
  297. $html .= '
  298. </select>
  299. </div>
  300. </div>
  301. ';
  302. return $html;
  303. }
  304. function tpl_form_field_industry($name, $pvalue = '', $cvalue = '', $parentid = 'industry_1', $childid = 'industry_2'){
  305. $html = '
  306. <div class="row row-fix">
  307. <div class="col-sm-4">
  308. <select name="' . $name . '[parent]" id="' . $parentid . '" class="form-control" value="' . $pvalue . '"></select>
  309. </div>
  310. <div class="col-sm-4">
  311. <select name="' . $name . '[child]" id="' . $childid . '" class="form-control" value="' . $cvalue . '"></select>
  312. </div>
  313. <script type="text/javascript">
  314. require([\'industry\'], function(industry){
  315. industry.init("'. $parentid . '","' . $childid . '");
  316. });
  317. </script>
  318. </div>';
  319. return $html;
  320. }
  321. function tpl_form_field_coordinate($field, $value = array()) {
  322. $s = '';
  323. if(!defined('TPL_INIT_COORDINATE')) {
  324. $s .= '<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=F51571495f717ff1194de02366bb8da9&s=1"></script><script type="text/javascript">
  325. function showCoordinate(elm) {
  326. require(["util"], function(util){
  327. var val = {};
  328. val.lng = parseFloat($(elm).parent().prev().prev().find(":text").val());
  329. val.lat = parseFloat($(elm).parent().prev().find(":text").val());
  330. util.map(val, function(r){
  331. $(elm).parent().prev().prev().find(":text").val(r.lng);
  332. $(elm).parent().prev().find(":text").val(r.lat);
  333. });
  334. });
  335. }
  336. </script>';
  337. define('TPL_INIT_COORDINATE', true);
  338. }
  339. $s .= '
  340. <div class="row row-fix">
  341. <div class="col-xs-4 col-sm-4">
  342. <input type="text" name="' . $field . '[lng]" value="'.$value['lng'].'" placeholder="地理经度" class="form-control" />
  343. </div>
  344. <div class="col-xs-4 col-sm-4">
  345. <input type="text" name="' . $field . '[lat]" value="'.$value['lat'].'" placeholder="地理纬度" class="form-control" />
  346. </div>
  347. <div class="col-xs-4 col-sm-4">
  348. <button onclick="showCoordinate(this);" class="btn btn-default" type="button">选择坐标</button>
  349. </div>
  350. </div>';
  351. return $s;
  352. }
  353. function tpl_fans_form($field, $value = '') {
  354. switch ($field) {
  355. case 'avatar':
  356. $avatar_url = '../attachment/images/global/avatars/';
  357. $html = '';
  358. if (!defined('TPL_INIT_AVATAR')) {
  359. $html .= '
  360. <script type="text/javascript">
  361. function showAvatarDialog(elm, opts) {
  362. require(["util"], function(util){
  363. var btn = $(elm);
  364. var ipt = btn.parent().prev();
  365. var img = ipt.parent().next().children();
  366. var content = \'<div class="avatar-browser clearfix">\';
  367. for(var i = 1; i <= 12; i++) {
  368. content +=
  369. \'<div title="头像\' + i + \'" class="thumbnail">\' +
  370. \'<em><img src="' . $avatar_url . 'avatar_\' + i + \'.jpg" class="img-responsive"></em>\' +
  371. \'</div>\';
  372. }
  373. content += "</div>";
  374. var dialog = util.dialog("请选择头像", content);
  375. dialog.modal("show");
  376. dialog.find(".thumbnail").on("click", function(){
  377. var url = $(this).find("img").attr("src");
  378. img.get(0).src = url;
  379. ipt.val(url.replace(/^\.\.\/attachment\//, ""));
  380. dialog.modal("hide");
  381. });
  382. });
  383. }
  384. </script>';
  385. define('TPL_INIT_AVATAR', true);
  386. }
  387. if (!defined('TPL_INIT_IMAGE')) {
  388. global $_W;
  389. if (defined('IN_MOBILE')) {
  390. $html .= <<<EOF
  391. <script type="text/javascript">
  392. // in mobile
  393. function showImageDialog(elm) {
  394. require(["jquery", "util"], function($, util){
  395. var btn = $(elm);
  396. var ipt = btn.parent().prev();
  397. var val = ipt.val();
  398. var img = ipt.parent().next().children();
  399. util.image(elm, function(url){
  400. img.get(0).src = url.url;
  401. ipt.val(url.attachment);
  402. });
  403. });
  404. }
  405. </script>
  406. EOF;
  407. } else {
  408. $html .= <<<EOF
  409. <script type="text/javascript">
  410. // in web
  411. function showImageDialog(elm, opts) {
  412. require(["util"], function(util){
  413. var btn = $(elm);
  414. var ipt = btn.parent().prev();
  415. var val = ipt.val();
  416. var img = ipt.parent().next().find('img');
  417. util.image(val, function(url){
  418. img.get(0).src = url.url;
  419. ipt.val(url.attachment);
  420. }, {multiple:false,type:"image",direct:true}, opts);
  421. });
  422. }
  423. </script>
  424. EOF;
  425. }
  426. define('TPL_INIT_IMAGE', true);
  427. }
  428. $val = './resource/images/nopic.jpg';
  429. if (!empty($value)) {
  430. $val = tomedia($value);
  431. }
  432. $options = array();
  433. $options['width'] = '200';
  434. $options['height'] = '200';
  435. if (defined('IN_MOBILE')) {
  436. $html .= <<<EOF
  437. <div class="input-group">
  438. <input type="text" value="{$value}" name="{$field}" class="form-control" autocomplete="off">
  439. <span class="input-group-btn">
  440. <button class="btn btn-default" type="button" onclick="showImageDialog(this);">选择图片</button>
  441. <button class="btn btn-default" type="button" onclick="showAvatarDialog(this);">系统头像</button>
  442. </span>
  443. </div>
  444. <div class="input-group" style="margin-top:.5em;">
  445. <img src="{$val}" class="img-responsive img-thumbnail" width="150" style="max-height: 150px;"/>
  446. </div>
  447. EOF;
  448. } else {
  449. $html .= '
  450. <div class="input-group">
  451. <input type="text" value="' . $value . '" name="' . $field . '" class="form-control" autocomplete="off">
  452. <span class="input-group-btn">
  453. <button class="btn btn-default" type="button" onclick="showImageDialog(this, \'' . base64_encode(iserializer($options)) . '\');">选择图片</button>
  454. <button class="btn btn-default" type="button" onclick="showAvatarDialog(this);">系统头像</button>
  455. </span>
  456. </div>
  457. <div class="input-group" style="margin-top:.5em;">
  458. <img src="' . $val . '" class="img-responsive img-thumbnail" width="150" />
  459. </div>';
  460. }
  461. break;
  462. case 'birth':
  463. case 'birthyear':
  464. case 'birthmonth':
  465. case 'birthday':
  466. $html = tpl_form_field_calendar('birth', $value);
  467. break;
  468. case 'reside':
  469. case 'resideprovince':
  470. case 'residecity':
  471. case 'residedist':
  472. $html = tpl_form_field_district('reside', $value);
  473. break;
  474. case 'bio':
  475. case 'interest':
  476. $html = '<textarea name="' . $field . '" class="form-control">' . $value . '</textarea>';
  477. break;
  478. case 'gender':
  479. $html = '
  480. <select name="gender" class="form-control">
  481. <option value="0" ' . ($value == 0 ? 'selected ' : '') . '>保密</option>
  482. <option value="1" ' . ($value == 1 ? 'selected ' : '') . '>男</option>
  483. <option value="2" ' . ($value == 2 ? 'selected ' : '') . '>女</option>
  484. </select>';
  485. break;
  486. case 'education':
  487. case 'constellation':
  488. case 'zodiac':
  489. case 'bloodtype':
  490. if ($field == 'bloodtype') {
  491. $options = array('A', 'B', 'AB', 'O', '其它');
  492. } elseif ($field == 'zodiac') {
  493. $options = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
  494. } elseif ($field == 'constellation') {
  495. $options = array('水瓶座', '双鱼座', '白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座');
  496. } elseif ($field == 'education') {
  497. $options = array('博士', '硕士', '本科', '专科', '中学', '小学', '其它');
  498. }
  499. $html = '<select name="' . $field . '" class="form-control">';
  500. foreach ($options as $item) {
  501. $html .= '<option value="' . $item . '" ' . ($value == $item ? 'selected ' : '') . '>' . $item . '</option>';
  502. }
  503. $html .= '</select>';
  504. break;
  505. case 'nickname':
  506. case 'realname':
  507. case 'address':
  508. case 'mobile':
  509. case 'qq':
  510. case 'msn':
  511. case 'email':
  512. case 'telephone':
  513. case 'taobao':
  514. case 'alipay':
  515. case 'studentid':
  516. case 'grade':
  517. case 'graduateschool':
  518. case 'idcard':
  519. case 'zipcode':
  520. case 'site':
  521. case 'affectivestatus':
  522. case 'lookingfor':
  523. case 'nationality':
  524. case 'height':
  525. case 'weight':
  526. case 'company':
  527. case 'occupation':
  528. case 'position':
  529. case 'revenue':
  530. default:
  531. $html = '<input type="text" class="form-control" name="' . $field . '" value="' . $value . '" />';
  532. break;
  533. }
  534. return $html;
  535. }