front.class.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. <?php
  2. /**
  3. * ZenTaoPHP的前端类。
  4. * The front class file of ZenTaoPHP framework.
  5. *
  6. * The author disclaims copyright to this source code. In place of
  7. * a legal notice, here is a blessing:
  8. *
  9. * May you do good and not evil.
  10. * May you find forgiveness for yourself and forgive others.
  11. * May you share freely, never taking more than you give.
  12. */
  13. /**
  14. * html类,生成html标签。
  15. * The html class, to build html tags.
  16. *
  17. * @package framework
  18. */
  19. class baseHTML
  20. {
  21. /**
  22. * 生成title标签。
  23. * Create the title tag.
  24. *
  25. * @static
  26. * @access public
  27. * @return string.
  28. */
  29. public static function title($title)
  30. {
  31. return "<title>$title</title>\n";
  32. }
  33. /**
  34. * 生成meta标签。
  35. * Create a meta.
  36. *
  37. * @param mixed $name the meta name
  38. * @param mixed $value the meta value
  39. * @static
  40. * @access public
  41. * @return string
  42. */
  43. public static function meta($name, $value)
  44. {
  45. if($name == 'charset') return "<meta charset='$value'>\n";
  46. return "<meta name='$name' content='$value'>\n";
  47. }
  48. /**
  49. * 生成favicon标签。
  50. * Create favicon tag
  51. *
  52. * @param mixed $url the url of the icon.
  53. * @static
  54. * @access public
  55. * @return string
  56. */
  57. public static function favicon($url)
  58. {
  59. return "<link rel='icon' href='$url' type='image/x-icon' />\n<link rel='shortcut icon' href='$url' type='image/x-icon' />\n";
  60. }
  61. /**
  62. * 创建图标。
  63. * Create icon.
  64. *
  65. * @param name $name the name of the icon.
  66. * @param cssClass $class the extra css class of the icon.
  67. * @static
  68. * @access public
  69. * @return string
  70. */
  71. public static function icon($name, $class = '')
  72. {
  73. $class = empty($class) ? ('icon-' . $name) : ('icon-' . $name . ' ' . $class);
  74. return "<i class='$class'></i>";
  75. }
  76. /**
  77. * 生成rss标签。
  78. * Create the rss tag.
  79. *
  80. * @param string $url
  81. * @param string $title
  82. * @static
  83. * @access public
  84. * @return string
  85. */
  86. public static function rss($url, $title = '')
  87. {
  88. return "<link href='$url' title='$title' type='application/rss+xml' rel='alternate' />";
  89. }
  90. /**
  91. * 生成超链接。
  92. * Create tags like <a href="">text</a>
  93. *
  94. * @param string $href the link url.
  95. * @param string $title the link title.
  96. * @param string $misc other params.
  97. * @param string $newline
  98. * @static
  99. * @access public
  100. * @return string
  101. */
  102. static public function a($href = '', $title = '', $misc = '', $newline = true)
  103. {
  104. if(strlen(trim($title)) == 0) $title = $href;
  105. $newline = $newline ? "\n" : '';
  106. /* Make sure href is opened in the same tab. */
  107. if(strpos($misc, 'data-app=') === false)
  108. {
  109. global $app, $lang;
  110. $module = $app->rawModule;
  111. $dataApp = (isset($lang->navGroup->$module) and $lang->navGroup->$module != $app->tab) ? "data-app='{$app->tab}'" : '';
  112. $misc .= ' ' . $dataApp;
  113. }
  114. return "<a href='$href' $misc>$title</a>$newline";
  115. }
  116. /**
  117. * 生成邮件链接。
  118. * Create tags like <a href="mailto:">text</a>
  119. *
  120. * @param string $mail the email address
  121. * @param string $title the email title.
  122. * @static
  123. * @access public
  124. * @return string
  125. */
  126. static public function mailto($mail = '', $title = '')
  127. {
  128. $html = '';
  129. $mails = explode(',', $mail);
  130. $titles = explode(',', $title);
  131. foreach($mails as $key => $m)
  132. {
  133. if(empty($m)) continue;
  134. $t = empty($titles[$key]) ? $mail : $titles[$key];
  135. $html .= " <a href='mailto:$m'>$t</a>";
  136. }
  137. return $html;
  138. }
  139. /**
  140. * 生成select标签。
  141. * Create tags like "<select><option></option></select>"
  142. *
  143. * @param string $name the name of the select tag.
  144. * @param array $options the array to create select tag from.
  145. * @param string $selectedItems the item(s) to be selected, can like item1,item2.
  146. * @param string $attrib other params such as multiple, size and style.
  147. * @param string $append adjust if add options[$selectedItems].
  148. * @static
  149. * @access public
  150. * @return string
  151. */
  152. static public function select($name = '', $options = array(), $selectedItems = "", $attrib = "", $append = false)
  153. {
  154. $options = (array)($options);
  155. if($append and !isset($options[$selectedItems])) $options[$selectedItems] = $selectedItems;
  156. if(!is_array($options) or empty($options)) return false;
  157. /* The begin. */
  158. $id = $name;
  159. if(strpos($name, '[') !== false) $id = trim(str_replace(']', '', str_replace('[', '', $name)));
  160. $id = "id='{$id}'";
  161. if(strpos($attrib, 'id=') !== false) $id = '';
  162. $string = "<select name='$name' {$id} $attrib>\n";
  163. /* The options. */
  164. if(is_array($selectedItems)) $selectedItems = implode(',', $selectedItems);
  165. $selectedItems = ",$selectedItems,";
  166. foreach($options as $key => $value)
  167. {
  168. $selected = strpos($selectedItems, ",$key,") !== false ? " selected='selected'" : '';
  169. $string .= "<option value='$key'$selected>$value</option>\n";
  170. }
  171. /* End. */
  172. return $string .= "</select>\n";
  173. }
  174. /**
  175. * 生成带optgroup标签的select标签。
  176. * Create select with optgroup.
  177. *
  178. * @param string $name the name of the select tag.
  179. * @param array $groups the option groups.
  180. * @param string $selectedItems the item(s) to be selected, can like item1,item2.
  181. * @param string $attrib other params such as multiple, size and style.
  182. * @static
  183. * @access public
  184. * @return string
  185. */
  186. static public function selectGroup($name = '', $groups = array(), $selectedItems = "", $attrib = "")
  187. {
  188. if(!is_array($groups) or empty($groups)) return false;
  189. /* The begin. */
  190. $id = $name;
  191. if(strpos($name, '[') !== false) $id = trim(str_replace(']', '', str_replace('[', '', $name)));
  192. $string = "<select name='$name' id='$id' $attrib>\n";
  193. /* The options. */
  194. $selectedItems = ",$selectedItems,";
  195. foreach($groups as $groupName => $options)
  196. {
  197. $string .= "<optgroup label='$groupName'>\n";
  198. foreach($options as $key => $value)
  199. {
  200. $selected = strpos($selectedItems, ",$key,") !== false ? " selected='selected'" : '';
  201. $string .= "<option value='$key'$selected>$value</option>\n";
  202. }
  203. $string .= "</optgroup>\n";
  204. }
  205. /* End. */
  206. return $string .= "</select>\n";
  207. }
  208. /**
  209. * 生成单选按钮。
  210. * Create tags like "<input type='radio' />"
  211. *
  212. * @param string $name the name of the radio tag.
  213. * @param array $options the array to create radio tag from.
  214. * @param string $checked the value to checked by default.
  215. * @param string $attrib other attribs.
  216. * @param string $type inline or block
  217. * @static
  218. * @access public
  219. * @return string
  220. */
  221. static public function radio($name = '', $options = array(), $checked = '', $attrib = '', $type = 'inline')
  222. {
  223. $options = (array)($options);
  224. if(!is_array($options) or empty($options)) return false;
  225. $isBlock = $type == 'block';
  226. $string = '';
  227. foreach($options as $key => $value)
  228. {
  229. if($isBlock) $string .= "<div class='radio'><label>";
  230. else $string .= "<label class='radio-inline'>";
  231. $string .= "<input type='radio' name='$name' value='$key' ";
  232. $string .= ($key == $checked) ? " checked ='checked'" : "";
  233. $string .= $attrib;
  234. $string .= " id='$name$key' /> ";
  235. $string .= $value;
  236. if($isBlock) $string .= '</label></div>';
  237. else $string .= '</label>';
  238. }
  239. return $string;
  240. }
  241. /**
  242. * 生成多选按钮。
  243. * Create tags like "<input type='checkbox' />"
  244. *
  245. * @param string $name the name of the checkbox tag.
  246. * @param array $options the array to create checkbox tag from.
  247. * @param string $checked the value to checked by default, can be item1,item2
  248. * @param string $attrib other attribs.
  249. * @param string $type inline or block
  250. * @static
  251. * @access public
  252. * @return string
  253. */
  254. static public function checkbox($name, $options, $checked = "", $attrib = "", $type = 'inline')
  255. {
  256. $options = (array)($options);
  257. if(!is_array($options) or empty($options)) return false;
  258. if(is_array($checked)) $checked = implode(',', $checked);
  259. $string = '';
  260. $checked = ",$checked,";
  261. $isBlock = $type == 'block';
  262. foreach($options as $key => $value)
  263. {
  264. if($isBlock) $string .= "<div class='checkbox'><label>";
  265. else $string .= "<label class='checkbox-inline'>";
  266. $string .= "<input type='checkbox' name='{$name}[]' value='$key' ";
  267. $string .= (strpos($checked, ",$key,") !== false) ? " checked ='checked'" : "";
  268. $string .= $attrib;
  269. $string .= " id='$name$key' /> ";
  270. $string .= $value;
  271. if($isBlock) $string .= '</label></div>';
  272. else $string .= '</label>';
  273. }
  274. return $string;
  275. }
  276. /**
  277. * 生成input输入标签。
  278. * Create tags like "<input type='text' />"
  279. *
  280. * @param string $name the name of the text input tag.
  281. * @param string $value the default value.
  282. * @param string $attrib other attribs.
  283. * @static
  284. * @access public
  285. * @return string
  286. */
  287. static public function input($name, $value = "", $attrib = "")
  288. {
  289. $id = "id='$name'";
  290. if(strpos($attrib, 'id=') !== false) $id = '';
  291. $value = str_replace("'", '&#039;', $value);
  292. return "<input type='text' name='$name' {$id} value='$value' $attrib />\n";
  293. }
  294. /**
  295. * 生成隐藏的提交标签。
  296. * Create tags like "<input type='hidden' />"
  297. *
  298. * @param string $name the name of the text input tag.
  299. * @param string $value the default value.
  300. * @param string $attrib other attribs.
  301. * @static
  302. * @access public
  303. * @return string
  304. */
  305. static public function hidden($name, $value = "", $attrib = "")
  306. {
  307. $id = str_replace(array('[', ']'), "", $name);
  308. return "<input type='hidden' name='$name' id='$id' value='$value' $attrib />\n";
  309. }
  310. /**
  311. * 创建密码输入框。
  312. * Create tags like "<input type='password' />"
  313. *
  314. * @param string $name the name of the text input tag.
  315. * @param string $value the default value.
  316. * @param string $attrib other attribs.
  317. * @static
  318. * @access public
  319. * @return string
  320. */
  321. static public function password($name, $value = "", $attrib = "")
  322. {
  323. if(stripos($attrib, 'autocomplete') === false) $attrib .= " autocomplete='off'";
  324. $id = str_replace(array('[', ']'), "", $name);
  325. return "<input type='password' name='$name' id='$id' value='$value' $attrib />\n";
  326. }
  327. /**
  328. * 创建编辑器标签。
  329. * Create tags like "<textarea></textarea>"
  330. *
  331. * @param string $name the name of the textarea tag.
  332. * @param string $value the default value of the textarea tag.
  333. * @param string $attrib other attribs.
  334. * @static
  335. * @access public
  336. * @return string
  337. */
  338. static public function textarea($name, $value = "", $attrib = "")
  339. {
  340. $id = "id='$name'";
  341. $id = str_replace(array('[', ']'), "", $id);
  342. if(strpos($attrib, 'id=') !== false) $id = '';
  343. return "<textarea name='$name' $id $attrib>$value</textarea>\n";
  344. }
  345. /**
  346. * 创建文件上传标签。
  347. * Create tags like "<input type='file' />".
  348. *
  349. * @param string $name the name of the file name.
  350. * @param string $attrib other attribs.
  351. * @static
  352. * @access public
  353. * @return string
  354. */
  355. static public function file($name, $attrib = "")
  356. {
  357. return "<input type='file' name='$name' id='$name' $attrib />\n";
  358. }
  359. /**
  360. * 创建日期输入框。
  361. * Create date picker.
  362. *
  363. * @param string $name the name of the text input tag.
  364. * @param string $value the default value.
  365. * @param string $options
  366. * @param string $attrib
  367. * @static
  368. * @access public
  369. * @return void
  370. */
  371. static public function date($name, $value = "", $options = '', $attrib = '')
  372. {
  373. $id = str_replace(array('[', ']'), "", $name);
  374. $html = "<div class='input-append date date-picker' {$options}>";
  375. $html .= "<input type='text' name='{$name}' id='$id' value='$value' {$attrib} />\n";
  376. $html .= "<span class='add-on'><button class='btn' type='button'><i class='icon-calendar'></i></button></span></div>";
  377. return $html;
  378. }
  379. /**
  380. * 创建日期时间输入框。
  381. * Create dateTime picker.
  382. *
  383. * @param string $name the name of the text input tag.
  384. * @param string $value the default value.
  385. * @param string $options
  386. * @param string $attrib
  387. * @static
  388. * @access public
  389. * @return void
  390. */
  391. static public function dateTime($name, $value = "", $options = '', $attrib = '')
  392. {
  393. $id = str_replace(array('[', ']'), "", $name);
  394. $html = "<div class='input-append date time-picker' {$options}>";
  395. $html .= "<input type='text' name='{$name}' id='$id' value='$value' {$attrib} />\n";
  396. $html .= "<span class='add-on'><button class='btn' type='button'><i class='icon-calendar'></i></button></span></div>";
  397. return $html;
  398. }
  399. /**
  400. * 创建img标签。
  401. * create tags like "<img src='' />".
  402. *
  403. * @param string $name the name of the image name.
  404. * @param string $attrib other attribs.
  405. * @static
  406. * @access public
  407. * @return string
  408. */
  409. static public function image($image, $attrib = '')
  410. {
  411. return "<img src='$image' $attrib />\n";
  412. }
  413. /**
  414. * 创建提交按钮。
  415. * Create submit button.
  416. *
  417. * @param string $label the label of the button
  418. * @param string $class the class of the button
  419. * @param string $misc other params
  420. * @static
  421. * @access public
  422. * @return string the submit button tag.
  423. */
  424. public static function submitButton($label = '', $class = 'btn btn-primary', $misc = '')
  425. {
  426. global $lang;
  427. $label = empty($label) ? $lang->save : $label;
  428. $misc .= strpos($misc, 'data-loading') === false ? " data-loading='$lang->loading'" : '';
  429. return " <button type='submit' id='submit' class='$class' $misc>$label</button>";
  430. }
  431. /**
  432. * 创建重置按钮。
  433. * Create reset button.
  434. *
  435. * @param string $label
  436. * @param string $class
  437. * @static
  438. * @access public
  439. * @return string the reset button tag.
  440. */
  441. public static function resetButton($label = '', $class = 'btn-wide')
  442. {
  443. if(empty($label))
  444. {
  445. global $lang;
  446. $label = $lang->reset;
  447. }
  448. return " <button type='reset' id='reset' class='btn btn-reset $class'>$label</button>";
  449. }
  450. /**
  451. * Get goback link.
  452. * 获取返回按钮链接
  453. */
  454. public static function getGobackLink()
  455. {
  456. global $app, $config;
  457. $gobackLink = '';
  458. $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
  459. $refererParts = parse_url($referer);
  460. if($config->requestType == 'PATH_INFO' and empty($refererParts)) return $gobackLink;
  461. if($config->requestType == 'GET' and !isset($refererParts['query'])) return $gobackLink;
  462. $tab = $app->tab;
  463. $gobackList = isset($_COOKIE['goback']) ? json_decode($_COOKIE['goback'], true) : array();
  464. $gobackLink = isset($gobackList[$tab]) ? $gobackList[$tab] : '';
  465. /* Make sure href is opened in the same tab. */
  466. if(!empty($gobackLink)) $gobackLink .= "#app=$tab";
  467. /* If the link of the referer is not the link of the current page or the link of the index, the cookie and gobackLink will be updated. */
  468. $currentModule = $app->getModuleName();
  469. $currentMethod = $app->getMethodName();
  470. $refererLink = $config->requestType == 'PATH_INFO' ? $refererParts['path'] : $refererParts['query'];
  471. if(!preg_match("/(m=|\/)(index|search|$currentModule)(&f=|-)(index|buildquery|$currentMethod)(&|-|\.)?/", strtolower($refererLink)))
  472. {
  473. $gobackList[$tab] = $referer;
  474. $gobackLink = $referer;
  475. setcookie('goback', json_encode($gobackList), $config->cookieLife, $config->webRoot, '', $config->cookieSecure, false);
  476. }
  477. return empty($gobackLink) ? 'javascript:history.go(-1)' : $gobackLink;
  478. }
  479. /**
  480. * 创建返回按钮。
  481. * Back button.
  482. *
  483. * @param string $label
  484. * @param string $misc
  485. * @static
  486. * @access public
  487. * @return string the back button tag.
  488. */
  489. public static function backButton($label = '', $misc = '', $class = 'btn-wide')
  490. {
  491. if(helper::inOnlyBodyMode()) return false;
  492. if(empty($label))
  493. {
  494. global $lang;
  495. $label = $lang->goback;
  496. }
  497. return "<a href='javascript:history.go(-1)' class='btn btn-back $class' $misc>{$label}</a>";
  498. }
  499. /**
  500. * 创建通用按钮。
  501. * Create common button.
  502. *
  503. * @param string $label the label of the button
  504. * @param string $class the class of the button
  505. * @param string $misc other params
  506. * @param string $icon icon
  507. * @static
  508. * @access public
  509. * @return string the common button tag.
  510. */
  511. public static function commonButton($label = '', $class = 'btn', $misc = '', $icon = '')
  512. {
  513. if($icon) $label = "<i class='icon-" . $icon . "'></i> " . $label;
  514. return " <button type='button' class='$class' $misc>$label</button>";
  515. }
  516. /**
  517. * 创建一个带有链接的按钮。
  518. * create a button, when click, go to a link.
  519. *
  520. * @param string $label the link title
  521. * @param string $link the link url
  522. * @param string $class the link style
  523. * @param string $misc other params
  524. * @param string $target the target window
  525. * @static
  526. * @access public
  527. * @return string
  528. */
  529. public static function linkButton($label = '', $link = '', $class='btn', $misc = '', $target = 'self')
  530. {
  531. global $config, $lang;
  532. if(helper::inOnlyBodyMode() and $lang->goback == $label) return false;
  533. $link = helper::processOnlyBodyParam($link);
  534. return " <button type='button' class='$class' $misc onclick='$target.location.href=\"$link\"'>$label</button>";
  535. }
  536. /**
  537. * 创建关闭模态框按钮。
  538. * Create a button to close.
  539. *
  540. * @static
  541. * @access public
  542. */
  543. public static function closeButton()
  544. {
  545. return "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>";
  546. }
  547. /**
  548. * 创建全选标签。
  549. * Create tags like "<input type='$type' onclick='selectAll()'/>"
  550. *
  551. * @param string $scope the scope of select all.
  552. * @param string $type the type of input tag.
  553. * @param boolean $checked if the type is checkbox, set the checked attribute.
  554. * @param string $class
  555. * @static
  556. * @access public
  557. * @return string
  558. */
  559. static public function selectAll($scope = "", $type = "button", $checked = false, $class = '')
  560. {
  561. $string = <<<EOT
  562. <script>
  563. function selectAll(checker, scope, type)
  564. {
  565. if(scope)
  566. {
  567. if(type == 'button')
  568. {
  569. var check = $('#' + scope + ' input:checkbox').length == $('#' + scope + ' input:checkbox:checked').length ? false : true;
  570. $('#' + scope + ' input').each(function()
  571. {
  572. $(this).prop("checked", check)
  573. });
  574. $(checker).data('check', check == true ? false :true);
  575. }
  576. else if(type == 'checkbox')
  577. {
  578. $('#' + scope + ' input').each(function()
  579. {
  580. $(this).prop("checked", checker.checked)
  581. });
  582. }
  583. }
  584. else
  585. {
  586. if(type == 'button')
  587. {
  588. var check = $('input:checkbox').length == $('input:checkbox:checked').length ? false : true;
  589. $('input:checkbox').each(function()
  590. {
  591. $(this).prop("checked", check)
  592. });
  593. }
  594. else if(type == 'checkbox')
  595. {
  596. $('input:checkbox').each(function()
  597. {
  598. $(this).prop("checked", checker.checked)
  599. });
  600. }
  601. }
  602. }
  603. </script>
  604. EOT;
  605. global $lang;
  606. if($type == 'checkbox')
  607. {
  608. $string .= " <input type='checkbox' name='allchecker[]'" . ($checked ? " checked=$checked" : '') . " onclick='selectAll(this, \"$scope\", \"$type\")' />";
  609. }
  610. elseif($type == 'button')
  611. {
  612. $string .= "<input type='button' name='allchecker' id='allchecker' class='btn btn-select-all $class' value='{$lang->selectAll}' onclick='selectAll(this, \"$scope\", \"$type\")' />";
  613. }
  614. return $string;
  615. }
  616. /**
  617. * 创建反选标签。
  618. * Create tags like "<input type='button' onclick='selectReverse()'/>"
  619. *
  620. * @param string $scope the scope of select reverse.
  621. * @static
  622. * @access public
  623. * @return string
  624. */
  625. static public function selectReverse($scope = "")
  626. {
  627. $string = <<<EOT
  628. <script type="text/javascript">
  629. function selectReverse(scope)
  630. {
  631. if(scope)
  632. {
  633. $('#' + scope + ' input').each(function()
  634. {
  635. $(this).prop("checked", !$(this).prop("checked"))
  636. });
  637. }
  638. else
  639. {
  640. $('input:checkbox').each(function()
  641. {
  642. $(this).prop("checked", !$(this).prop("checked"))
  643. });
  644. }
  645. }
  646. </script>
  647. EOT;
  648. global $lang;
  649. $string .= "<input type='button' name='reversechecker' id='reversechecker' value='{$lang->selectReverse}' class='btn' onclick='selectReverse(\"$scope\")'/>";
  650. return $string;
  651. }
  652. /**
  653. * 创建全选、反选按钮组。
  654. * Create select buttons include 'selectAll' and 'selectReverse'.
  655. *
  656. * @param string $scope the scope of select reverse.
  657. * @param bool $asGroup
  658. * @param string $appendClass
  659. * @static
  660. * @access public
  661. * @return string
  662. */
  663. static public function selectButton($scope = "", $asGroup = true, $appendClass = '')
  664. {
  665. $string = <<<EOT
  666. <script>
  667. $(function()
  668. {
  669. if($('body').data('bindSelectBtn')) return;
  670. $('body').data('bindSelectBtn', true);
  671. $(document).on('click', '.check-all, .check-inverse, #allchecker, #reversechecker', function()
  672. {
  673. var e = $(this);
  674. if(e.closest('.datatable').length) return;
  675. scope = e.data('scope');
  676. scope = scope ? $('#' + scope) : e.closest('.table');
  677. if(!scope.length) scope = e.closest('form');
  678. scope.find('input:checkbox').each(e.hasClass('check-inverse') ? function() { $(this).prop("checked", !$(this).prop("checked"));} : function() { $(this).prop("checked", true);});
  679. });
  680. });
  681. </script>
  682. EOT;
  683. global $lang;
  684. if($asGroup) $string .= "<div class='btn-group'>";
  685. $string .= "<a id='allchecker' class='btn btn-select-all check-all $appendClass' data-scope='$scope' href='javascript:;' >{$lang->selectAll}</a>";
  686. $string .= "<a id='reversechecker' class='btn btn-select-reverse check-inverse $appendClass' data-scope='$scope' href='javascript:;'>{$lang->selectReverse}</a>";
  687. if($asGroup) $string .= "</div>";
  688. return $string;
  689. }
  690. /**
  691. * 打印星星。
  692. * Print the star images.
  693. *
  694. * @param float $stars 0 1 1.5 2 2.5 3 3.5 4 4.5 5
  695. * @access public
  696. * @static
  697. * @access public
  698. */
  699. public static function printStars($stars, $print = true)
  700. {
  701. $redStars = 0;
  702. $halfStars = 0;
  703. $whiteStars = 5;
  704. if($stars)
  705. {
  706. /* If stars more than max, then fix it. */
  707. if($stars > $whiteStars) $stars = $whiteStars;
  708. $redStars = floor($stars);
  709. $halfStars = $stars - $redStars ? 1 : 0;
  710. $whiteStars = 5 - ceil($stars);
  711. }
  712. $starsHtml = "<span class='stars-list'>";
  713. for($i = 1; $i <= $redStars; $i ++) $starsHtml .= "<i class='icon-star'></i>";
  714. for($i = 1; $i <= $halfStars; $i ++) $starsHtml .= "<i class='icon-star-half-full'></i>";
  715. for($i = 1; $i <= $whiteStars; $i ++) $starsHtml .= "<i class='icon-star-empty'></i>";
  716. $starsHtml .= '</span>';
  717. if(!$print) return $starsHtml;
  718. echo $starsHtml;
  719. }
  720. }
  721. /**
  722. * JS类。
  723. * JS class.
  724. *
  725. * @package front
  726. */
  727. class baseJS
  728. {
  729. /**
  730. * 引入一个js文件。
  731. * Import a js file.
  732. *
  733. * @param string $url
  734. * @param string $ieParam like 'lt IE 9'
  735. * @static
  736. * @access public
  737. * @return string
  738. */
  739. public static function import($url, $ieParam = '')
  740. {
  741. global $config;
  742. $pathInfo = parse_url($url);
  743. $mark = !empty($pathInfo['query']) ? '&' : '?';
  744. $hasLimit = ($ieParam and stripos($ieParam, 'ie') !== false);
  745. if($hasLimit) echo "<!--[if $ieParam]>\n";
  746. echo "<script src='$url{$mark}v={$config->version}'></script>\n";
  747. if($hasLimit) echo "<![endif]-->\n";
  748. }
  749. /**
  750. * 开始输出js。
  751. * The start of javascript.
  752. *
  753. * @param bool $full
  754. * @static
  755. * @access public
  756. */
  757. static public function start($full = true)
  758. {
  759. if($full) return "<html><meta charset='utf-8'/><style>body{background:white}</style><script>";
  760. return "<script>";
  761. }
  762. /**
  763. * 结束输出js。
  764. * The end of javascript.
  765. *
  766. * @param bool $newline
  767. * @static
  768. * @access public
  769. * @return void
  770. */
  771. static public function end($newline = true)
  772. {
  773. if($newline) return "\n</script>\n";
  774. return "</script>\n";
  775. }
  776. /**
  777. * 显示一个警告框。
  778. * Show a alert box.
  779. *
  780. * @param string $message
  781. * @param bool $full
  782. * @static
  783. * @access public
  784. * @return string
  785. */
  786. static public function alert($message = '', $full = true)
  787. {
  788. global $app;
  789. if($app->viewType == 'json')
  790. {
  791. $output = array();
  792. $output['status'] = 'success';
  793. $output['data'] = json_encode(array('message' => $message));
  794. $output['md5'] = md5($output['data']);
  795. return json_encode($output);
  796. }
  797. /* Convert ' to \'. */
  798. $message = str_replace("\\'", "'", $message);
  799. $message = str_replace("'", "\\'", $message);
  800. return static::start($full) . "window.alert('" . $message . "')" . static::end() . static::resetForm();
  801. }
  802. /**
  803. * 关闭浏览器窗口。
  804. * Close window
  805. *
  806. * @static
  807. * @access public
  808. * @return void
  809. */
  810. static public function close()
  811. {
  812. return static::start() . "window.close()" . static::end();
  813. }
  814. /**
  815. * 显示错误信息。
  816. * Show error info.
  817. *
  818. * @param bool $full
  819. * @static
  820. * @access public
  821. * @return string
  822. * @param string|mixed[] $message
  823. */
  824. static public function error($message, $full = true)
  825. {
  826. global $app;
  827. if($app->viewType == 'json')
  828. {
  829. $output = array();
  830. $output['status'] = 'success';
  831. $output['data'] = json_encode(array('result' => 'fail', 'message' => $message));
  832. $output['md5'] = md5($output['data']);
  833. return json_encode($output);
  834. }
  835. $alertMessage = '';
  836. if(is_array($message))
  837. {
  838. foreach($message as $item)
  839. {
  840. is_array($item) ? $alertMessage .= join('\n', $item) . '\n' : $alertMessage .= $item . '\n';
  841. }
  842. }
  843. else
  844. {
  845. $alertMessage = $message;
  846. }
  847. return static::alert($alertMessage, $full);
  848. }
  849. /**
  850. * 重置禁用的提交按钮。
  851. * Reset the submit form.
  852. *
  853. * @static
  854. * @access public
  855. */
  856. static public function resetForm()
  857. {
  858. return static::start() . 'if(window.parent) window.parent.$.enableForm();' . static::end();
  859. }
  860. /**
  861. * 显示一个确认框,点击确定跳转到$okURL,点击取消跳转到$cancelURL。
  862. * show a confirm box, press ok go to okURL, else go to cancleURL.
  863. *
  864. * @param string $message 显示的内容。 the text to be showed.
  865. * @param string $okURL 点击确定后跳转的地址。 the url to go to when press 'ok'.
  866. * @param string $cancleURL 点击取消后跳转的地址。 the url to go to when press 'cancle'.
  867. * @param string $okTarget 点击确定后跳转的target。 the target to go to when press 'ok'.
  868. * @param string $cancleTarget 点击取消后跳转的target。 the target to go to when press 'cancle'.
  869. * @param string $okOpenApp 点击确定后跳转的应用。 the app to go to when press 'ok'.
  870. * @param string $cancleOpenApp 点击取消后跳转的应用。 the app to go to when press 'cancle'.
  871. * @static
  872. * @access public
  873. * @return string
  874. */
  875. static public function confirm($message = '', $okURL = '', $cancleURL = '', $okTarget = "self", $cancleTarget = "self", $okOpenApp = '', $cancleOpenApp = '')
  876. {
  877. global $app;
  878. if($app->viewType == 'json')
  879. {
  880. $data = array();
  881. $data['message'] = $message;
  882. $data['okURL'] = common::getSysURL() . $okURL;
  883. $data['cancleURL'] = common::getSysURL() . $cancleURL;
  884. $data['okTarget'] = $okTarget;
  885. $data['cancleTarget'] = $cancleTarget;
  886. $output = array();
  887. $output['status'] = 'success';
  888. $output['data'] = json_encode($data);
  889. $output['md5'] = md5($output['data']);
  890. return json_encode($output);
  891. }
  892. $js = static::start();
  893. $confirmAction = '';
  894. if(strtolower($okURL) == "back")
  895. {
  896. $confirmAction = "history.back(-1);";
  897. }
  898. elseif(strpos($okTarget, '$.apps.open') !== false)
  899. {
  900. $confirmAction = "$okTarget('$okURL', '$okOpenApp');";
  901. }
  902. elseif(!empty($okURL))
  903. {
  904. $confirmAction = "$okTarget.location = '$okURL';";
  905. }
  906. $cancleAction = '';
  907. if(strtolower($cancleURL) == "back")
  908. {
  909. $cancleAction = "history.back(-1);";
  910. }
  911. elseif(strpos($cancleTarget, '$.apps.open') !== false)
  912. {
  913. $cancleAction = "$cancleTarget('$cancleURL', '$cancleOpenApp');";
  914. }
  915. elseif(!empty($cancleURL))
  916. {
  917. $cancleAction = "$cancleTarget.location = '$cancleURL';";
  918. }
  919. if(strpos((string) $_SERVER['HTTP_USER_AGENT'], 'xuanxuan') === false)
  920. {
  921. $js .= <<<EOT
  922. if(confirm("$message"))
  923. {
  924. $confirmAction
  925. }
  926. else
  927. {
  928. $cancleAction
  929. }
  930. EOT;
  931. }
  932. else
  933. {
  934. $js .= $confirmAction;
  935. }
  936. $js .= static::end();
  937. return $js;
  938. }
  939. /**
  940. * $target会跳转到$url指定的地址。
  941. * change the location of the $target window to the $URL.
  942. *
  943. * @param string $url the url will go to.
  944. * @param string $target the target of the url.
  945. * @static
  946. * @access public
  947. * @return string the javascript string.
  948. */
  949. static public function locate($url, $target = "self")
  950. {
  951. global $app;
  952. /* If the url if empty, goto the home page. */
  953. if(!$url)
  954. {
  955. global $config;
  956. $url = $config->webRoot;
  957. }
  958. if($app->apiVersion == 'v2')
  959. {
  960. if(strpos($url, 'login') === false)
  961. {
  962. helper::send(array("status" => "success"));
  963. }
  964. else
  965. {
  966. helper::send(array("status" => "fail", "message" => "Not allowed"), 401);
  967. }
  968. }
  969. elseif($app->viewType == 'json')
  970. {
  971. $data = strtolower((string) $url) == 'back' ? array('locate' => 'back') : array('locate' => common::getSysURL() . $url);
  972. $output = array();
  973. $output['status'] = 'success';
  974. $output['data'] = json_encode($data);
  975. $output['md5'] = md5($output['data']);
  976. return json_encode($output);
  977. }
  978. $js = static::start();
  979. if(strtolower((string) $url) == "back")
  980. {
  981. $js .= "history.back(-1);\n";
  982. }
  983. elseif($target === 'app' or strpos($target, '$.apps.open') !== false)
  984. {
  985. $js .= "parent.$target('$url')";
  986. }
  987. else
  988. {
  989. /* Can not locate the url that has '#app', so remove it. */
  990. if(strpos((string) $url, '#app=') !== false) $url = substr((string) $url, 0, strpos((string) $url, '#app='));
  991. $js .= "$target.location='$url';\n";
  992. }
  993. return $js . static::end();
  994. }
  995. /**
  996. * 关闭当前窗口。
  997. * Close current window.
  998. *
  999. * @static
  1000. * @access public
  1001. */
  1002. static public function closeWindow()
  1003. {
  1004. return static::start(). "window.close();" . static::end();
  1005. }
  1006. /**
  1007. * 经过一段时间后跳转到指定的页面。
  1008. * Goto a page after a timer.
  1009. *
  1010. * @param string $url the url will go to.
  1011. * @param string $target the target of the url.
  1012. * @param int $time the timer, msec.
  1013. * @static
  1014. * @access public
  1015. * @return string the javascript string.
  1016. */
  1017. static public function refresh($url, $target = "self", $time = 3000)
  1018. {
  1019. $js = static::start();
  1020. $js .= "setTimeout(\"$target.location='$url'\", $time);";
  1021. $js .= static::end();
  1022. return $js;
  1023. }
  1024. /**
  1025. * 重新加载窗口。
  1026. * Reload a window.
  1027. *
  1028. * @param string $window the window to reload.
  1029. * @static
  1030. * @access public
  1031. * @return string the javascript string.
  1032. */
  1033. static public function reload($window = 'self')
  1034. {
  1035. $js = static::start();
  1036. // See bug #2379 http://pms.zentao.net/bug-view-2379.html
  1037. if($window === 'parent.parent')
  1038. {
  1039. $js .= "if(parent.parent.loadCurrentPage) parent.parent.loadCurrentPage(); else parent.parent.location.reload(true);\n";
  1040. }
  1041. else if($window !== 'self' && $window !== 'window')
  1042. {
  1043. $js .= "if($window !== window) $window.location.reload(true);\n";
  1044. }
  1045. else
  1046. {
  1047. $js .= "$window.location.reload(true);\n";
  1048. }
  1049. $js .= static::end();
  1050. return $js;
  1051. }
  1052. /**
  1053. * 用Javascript关闭colorbox弹出框。
  1054. * Close colorbox in javascript.
  1055. * This is a obsolete method, you can use 'closeModal' instead.
  1056. *
  1057. * @param string $window
  1058. * @static
  1059. * @access public
  1060. * @return string
  1061. */
  1062. static public function closeColorbox($window = 'self')
  1063. {
  1064. return static::closeModal($window);
  1065. }
  1066. /**
  1067. * 用Javascript关闭模态框。
  1068. * Close modal with javascript.
  1069. *
  1070. * @param string $window
  1071. * @param string $location
  1072. * @param string $callback
  1073. * @static
  1074. * @access public
  1075. * @return string
  1076. */
  1077. static public function closeModal($window = 'self', $location = 'this', $callback = 'null')
  1078. {
  1079. $js = static::start();
  1080. $js .= "if($window.location.href == self.location.href){ $window.window.close();}";
  1081. $js .= "else if($window.zui) { $window.zui.Modal.hide(); " . ($callback && $callback != 'null' ? "($callback)();" : '') . '}';
  1082. $js .= "else{ $window.$.cookie('selfClose', 1);$window.$.closeModal($callback, '$location');}";
  1083. $js .= static::end();
  1084. return $js;
  1085. }
  1086. static function getJSConfigVars()
  1087. {
  1088. global $app, $config, $lang;
  1089. $defaultViewType = $app->getViewType();
  1090. $themeRoot = $app->getWebRoot() . 'theme/';
  1091. $moduleName = $app->getModuleName();
  1092. $methodName = $app->getMethodName();
  1093. $clientLang = $app->getClientLang();
  1094. $runMode = defined('RUN_MODE') ? RUN_MODE : '';
  1095. $requiredFields = '';
  1096. if(isset($config->$moduleName->$methodName->requiredFields)) $requiredFields = str_replace(' ', '', (string) $config->$moduleName->$methodName->requiredFields);
  1097. $jsConfig = new stdclass();
  1098. $jsConfig->webRoot = $config->webRoot;
  1099. $jsConfig->debug = $config->debug;
  1100. $jsConfig->appName = $app->getAppName();
  1101. $jsConfig->cookieLife = ceil(($config->cookieLife - time()) / 86400);
  1102. $jsConfig->requestType = $config->requestType;
  1103. $jsConfig->requestFix = $config->requestFix;
  1104. $jsConfig->moduleVar = $config->moduleVar;
  1105. $jsConfig->methodVar = $config->methodVar;
  1106. $jsConfig->viewVar = $config->viewVar;
  1107. $jsConfig->version = $config->version;
  1108. $jsConfig->vision = $config->vision;
  1109. $jsConfig->visions = $lang->visionList;
  1110. $jsConfig->defaultView = $defaultViewType;
  1111. $jsConfig->themeRoot = $themeRoot;
  1112. $jsConfig->currentModule = $moduleName;
  1113. $jsConfig->currentMethod = $methodName;
  1114. $jsConfig->rawModule = $app->rawModule;
  1115. $jsConfig->rawMethod = $app->rawMethod;
  1116. $jsConfig->clientLang = $clientLang;
  1117. $jsConfig->clientLangs = array_keys($config->langs);
  1118. $jsConfig->requiredFields = $requiredFields;
  1119. $jsConfig->router = $app->server->SCRIPT_NAME;
  1120. $jsConfig->save = $lang->save ?? '';
  1121. $jsConfig->expand = $lang->expand ?? '';
  1122. $jsConfig->runMode = $runMode;
  1123. $jsConfig->timeout = $config->timeout ?? '';
  1124. $jsConfig->pingInterval = $config->pingInterval ?? '';
  1125. $jsConfig->onlybody = zget($_GET, 'onlybody', 'no');
  1126. $jsConfig->clientCache = $config->clientCache;
  1127. $jsConfig->morphUpdate = $config->morphUpdate;
  1128. $jsConfig->account = isset($app->user->account) ? $app->user->account : '';
  1129. if($config->tabSession and helper::isWithTID()) $jsConfig->tid = zget($_GET, 'tid', '');
  1130. return $jsConfig;
  1131. }
  1132. /**
  1133. * 导出$config到js,因为js的createLink()方法需要获取config信息。
  1134. * Export the config vars for createLink() js version.
  1135. *
  1136. * @static
  1137. * @access public
  1138. * @return void
  1139. */
  1140. static public function exportConfigVars()
  1141. {
  1142. if(!function_exists('json_encode')) return false;
  1143. global $lang;
  1144. $jsConfig = static::getJSConfigVars();
  1145. $jsLang = new stdclass();
  1146. $jsLang->submitting = $lang->loading ?? '';
  1147. $jsLang->save = $jsConfig->save;
  1148. $jsLang->expand = $lang->expand ?? '';
  1149. $jsLang->timeout = $lang->timeout ?? '';
  1150. $jsLang->confirmDraft = $lang->confirmDraft ?? '';
  1151. $jsLang->resume = $lang->resume ?? '';
  1152. $jsLang->program = zget($lang->program, 'common', '');
  1153. $jsLang->project = zget($lang->project, 'common', '');
  1154. $jsLang->product = zget($lang->product, 'common', '');
  1155. $jsLang->task = zget($lang->task, 'common', '');
  1156. $jsLang->story = zget($lang->story, 'common', '');
  1157. $jsLang->bug = zget($lang->bug, 'common', '');
  1158. $jsLang->testcase = zget($lang->testcase, 'common', '');
  1159. $jsLang->zahost = zget($lang->zahost, 'common', '');
  1160. $jsLang->zanode = zget($lang->zanode, 'common', '');
  1161. $jsLang->gitlab = zget($lang->gitlab, 'common', '');
  1162. $jsLang->gogs = zget($lang->gogs, 'common', '');
  1163. $jsLang->gitea = zget($lang->gitea, 'common', '');
  1164. $jsLang->jenkins = zget($lang->jenkins, 'common', '');
  1165. $jsLang->sonarqube = zget($lang->sonarqube, 'common', '');
  1166. $jsLang->repo = zget($lang->repo, 'common', '');
  1167. $js = static::start(false);
  1168. $js .= 'window.config=' . json_encode($jsConfig) . ";\n";
  1169. $js .= 'window.lang=' . json_encode($jsLang) . ";\n";
  1170. $js .= static::end();
  1171. echo $js;
  1172. }
  1173. /**
  1174. * 执行js代码。
  1175. * Execute some js code.
  1176. *
  1177. * @param string $code
  1178. * @static
  1179. * @access public
  1180. * @return string
  1181. */
  1182. static public function execute($code)
  1183. {
  1184. $js = static::start($full = false);
  1185. $js .= $code;
  1186. $js .= static::end();
  1187. echo $js;
  1188. }
  1189. /**
  1190. * 设置Javascript变量值。
  1191. * Set js value.
  1192. *
  1193. * @param string $key
  1194. * @param mixed $value
  1195. * @static
  1196. * @access public
  1197. * @return string
  1198. */
  1199. static public function set($key, $value)
  1200. {
  1201. global $config;
  1202. $prefix = (isset($config->framework->jsWithPrefix) and $config->framework->jsWithPrefix == false) ? '' : 'v.';
  1203. static $viewOBJOut;
  1204. $js = static::start(false);
  1205. if(!$viewOBJOut and $prefix)
  1206. {
  1207. $js .= 'if(typeof(v) != "object") v = {};';
  1208. $viewOBJOut = true;
  1209. }
  1210. /* Fix value is '0123' error. */
  1211. if(is_numeric($value) and !preg_match('/^0[0-9]+/', $value))
  1212. {
  1213. $js .= "{$prefix}{$key} = {$value};";
  1214. }
  1215. elseif(is_array($value) or is_object($value) or is_string($value))
  1216. {
  1217. /* Fix for auto-complete when user is number.*/
  1218. if(is_array($value) or is_object($value))
  1219. {
  1220. $value = (array)$value;
  1221. foreach($value as $k => $v)
  1222. {
  1223. if(is_numeric($v)) $value[$k] = (string)$v;
  1224. }
  1225. }
  1226. $value = json_encode($value);
  1227. $js .= "{$prefix}{$key} = {$value};";
  1228. }
  1229. elseif(is_bool($value))
  1230. {
  1231. $value = $value ? 'true' : 'false';
  1232. $js .= "{$prefix}{$key} = $value;";
  1233. }
  1234. else
  1235. {
  1236. $value = addslashes($value);
  1237. $js .= "{$prefix}{$key} = '{$value}';";
  1238. }
  1239. $js .= static::end($newline = false);
  1240. echo $js;
  1241. }
  1242. }
  1243. /**
  1244. * css类。
  1245. * css class.
  1246. *
  1247. * @package front
  1248. */
  1249. class baseCSS
  1250. {
  1251. /**
  1252. * 引入css文件。
  1253. * Import a css file.
  1254. *
  1255. * @param string $url
  1256. * @access public
  1257. */
  1258. public static function import($url, $attrib = '')
  1259. {
  1260. global $config;
  1261. if(!empty($attrib)) $attrib = ' ' . $attrib;
  1262. echo "<link rel='stylesheet' href='$url?v={$config->version}' type='text/css' media='screen'{$attrib} />\n";
  1263. }
  1264. /**
  1265. * 打印css代码。
  1266. * Print a css code.
  1267. *
  1268. * @param string $css
  1269. * @static
  1270. * @access public
  1271. */
  1272. public static function internal($css)
  1273. {
  1274. echo "<style>$css</style>";
  1275. }
  1276. }