tpl.func.php 18 KB

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