buildform.html.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. /**
  3. * The buildform view of search module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package search
  9. * @version $Id: buildform.html.php 4129 2013-01-18 01:58:14Z wwccss $
  10. * @link https://www.zentao.net
  11. */
  12. ?>
  13. <?php
  14. $jsRoot = $this->app->getWebRoot() . "js/";
  15. include '../../common/view/datepicker.html.php';
  16. include '../../common/view/chosen.html.php';
  17. $formId = 'searchForm-' . uniqid('');
  18. ?>
  19. <style>
  20. #save-query .text {font-weight: 400; font-size: 14px;}
  21. #selectPeriod {padding: 4px 0; height: 197px; min-width: 120px}
  22. #selectPeriod > .dropdown-header {background: #f1f1f1; display: block; text-align: center; padding: 4px 0; line-height: 20px; margin: 5px 10px; font-size: 14px; border-radius: 2px; color: #333; font-size: 12px}
  23. #groupAndOr {display: inline-block;}
  24. #<?php echo $formId;?> > table {margin: 0 auto;}
  25. #<?php echo $formId;?> > table > tbody > tr > td {padding: 8px;}
  26. #<?php echo $formId;?> .form-actions {padding-bottom: 20px; padding-top: 0;}
  27. <?php if(common::checkNotCN()):?>
  28. #<?php echo $formId;?> [id^="valueBox"] .chosen-container .chosen-single {min-width: 70px;}
  29. <?php else:?>
  30. #<?php echo $formId;?> [id^="valueBox"] .chosen-container .chosen-single {min-width: 100px;}
  31. <?php endif;?>
  32. #<?php echo $formId;?> .chosen-container .chosen-drop ul.chosen-results li {white-space:normal}
  33. #<?php echo $formId;?> input.date::-webkit-input-placeholder {color: #838A9D; opacity: 1;}
  34. #<?php echo $formId;?> input.date::-moz-placeholder {color: #838A9D; opacity: 1;}
  35. #<?php echo $formId;?> input.date:-ms-input-placeholder {color: #838A9D; opacity: 1;}
  36. #<?php echo $formId;?> .btn-expand-form {background: transparent;}
  37. #<?php echo $formId;?> .btn-expand-form:hover {background: #e9f2fb;}
  38. .showmore .btn-expand-form .icon-chevron-double-down:before {content: '\e959';}
  39. #queryBox select[id^="operator"] {padding-right:2px; padding-left:5px;}
  40. #queryBox select#groupAndOr {padding-right:2px; padding-left:5px;}
  41. #queryBox .chosen-container-single .chosen-single > span {margin-right:5px;}
  42. #queryBox .form-actions .btn {margin-right: 5px;}
  43. @media screen and (max-width: 1366px) { #userQueries {width: 130px!important;} }
  44. #userQueries {border-left: 1px solid #eee; vertical-align: top;}
  45. #userQueries > h4 {margin: 0 0 6px;}
  46. #userQueries ul {list-style: none; padding-left: 0; margin: 0; max-height:75px; overflow:auto;}
  47. .showmore #userQueries ul {max-height:170px;}
  48. #userQueries ul li + li {margin-top: 5px;}
  49. #userQueries .label {line-height: 24px; padding: 0 20px 0 8px; display: inline-block; background-color: #EEEEEE; color: #A6AAB8; border-radius: 12px; max-width: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; position: relative;}
  50. #userQueries .label:hover {background-color: #aaa; color: #fff;}
  51. #userQueries .label > .icon-close {position: absolute; top: 2px; right: 2px; border-radius: 9px; font-size: 12px; line-height: 18px; width: 18px; display: inline-block;}
  52. #userQueries .label > .icon-close:hover {background-color: #ff5d5d; color: #fff;}
  53. @media (max-width: 1050px) {#userQueries, #toggle-queries {display: none}}
  54. <?php if($style == 'simple'):?>
  55. #<?php echo $formId;?> .form-actions {text-align: left; padding: 0!important; max-width: 200px; vertical-align: middle; width: 100px;}
  56. #queryBox.show {min-height: 66px;}
  57. <?php endif;?>
  58. #toggle-queries {position: absolute; right: 0px; top: 40px; width: 13px; background: #79cdfb; border-radius: 6px; height: 30px;cursor: pointer}
  59. #toggle-queries .icon {position: absolute; top: 6px; right: -2px; color: #fff;}
  60. .fieldWidth {width: 130px !important;}
  61. .operatorWidth {width: 110px !important;}
  62. html[lang^='zh-'] .fieldWidth {width: 110px !important;}
  63. html[lang^='zh-'] .operatorWidth {width: 90px !important;}
  64. .table tbody tr td input {display: block !important;}
  65. #save-query {float: unset !important; position: absolute; right: 50px;}
  66. #save-query .text {top: 0px;}
  67. #save-query .text:after {border-bottom: 0px solid #0c64eb;}
  68. #<?php echo $formId;?> [id^='valueBox'] > div.picker span.picker-selection-text {padding-right: 10px;}
  69. </style>
  70. <?php if($style != 'simple'):?>
  71. <div id='toggle-queries'>
  72. <i class='icon icon-angle-left'></i>
  73. </div>
  74. <?php endif;?>
  75. <form method='post' action='<?php echo $this->createLink('search', 'buildQuery');?>' target='hiddenwin' id='<?php echo $formId;?>' class='search-form no-stash<?php if($style == 'simple') echo ' search-form-simple';?>'>
  76. <div class='hidden'>
  77. <?php
  78. /* Print every field as an html object, select or input. Thus when setFiled is called, copy it's html to build the search form. */
  79. foreach($fieldParams as $fieldName => $param)
  80. {
  81. echo "<div id='box$fieldName'>";
  82. if($param['control'] == 'select') echo html::select('field' . $fieldName, $param['values'], '', "class='form-control searchSelect'");
  83. if($param['control'] == 'input') echo html::input('field' . $fieldName, '', "class='form-control searchInput'");
  84. echo '</div>';
  85. }
  86. ?>
  87. </div>
  88. <table class='table table-condensed table-form' id='<?php echo "{$module}-search";?>'>
  89. <tbody>
  90. <tr>
  91. <td class='w-400px'>
  92. <table class='table table-form table-fixed'>
  93. <tbody>
  94. <?php
  95. $formSessionName = $module . 'Form';
  96. $formSession = $_SESSION[$formSessionName];
  97. $fieldNO = 1;
  98. for($i = 1; $i <= $groupItems; $i ++)
  99. {
  100. $spanClass = $i == 1 ? '' : 'hidden';
  101. echo "<tr id='searchbox$fieldNO' class='$spanClass'>";
  102. /* Get params of current field. */
  103. $currentField = $formSession["field$fieldNO"];
  104. if(!isset($fieldParams[$currentField]))
  105. {
  106. $currentField = key($searchFields);
  107. $formSession["field$fieldNO"] = $currentField;
  108. $formSession["operator$fieldNO"] = isset($fieldParams[$currentField]['operator']) ? $fieldParams[$currentField]['operator'] : '=';
  109. $formSession["value$fieldNO"] = '';
  110. }
  111. $param = $fieldParams[$currentField];
  112. /* Print and or. */
  113. echo "<td class='text-right w-80px'>";
  114. if($i == 1) echo "<span id='searchgroup1'><strong>{$lang->search->group1}</strong></span>" . html::hidden("andOr$fieldNO", 'AND');
  115. if($i > 1) echo html::select("andOr$fieldNO", $lang->search->andor, $formSession["andOr$fieldNO"], "class='form-control'");
  116. echo '</td>';
  117. /* Print field. */
  118. echo "<td class='fieldWidth' style='overflow: visible'>" . html::select("field$fieldNO", $searchFields, $formSession["field$fieldNO"], "onchange='setField(this, $fieldNO, {$module}params)' class='form-control chosen'") . '</td>';
  119. /* Print operator. */
  120. echo "<td class='operatorWidth'>" . html::select("operator$fieldNO", $lang->search->operators, $formSession["operator$fieldNO"], "class='form-control' onchange='setPlaceHolder($fieldNO)'") . '</td>';
  121. /* Print value. */
  122. echo "<td id='valueBox$fieldNO' style='overflow:visible'>";
  123. if(isset($config->moreLinks["field{$currentField}"])) $config->moreLinks["value$fieldNO"] = $config->moreLinks["field{$currentField}"];
  124. if($param['control'] == 'select') echo html::select("value$fieldNO", $param['values'], $formSession["value$fieldNO"], "class='form-control searchSelect chosen' data-max_drop_width='0'");
  125. if($param['control'] == 'input')
  126. {
  127. $fieldName = $formSession["field$fieldNO"];
  128. $fieldValue = $formSession["value$fieldNO"];
  129. $extraClass = isset($param['class']) ? $param['class'] : '';
  130. $placeholder = '';
  131. if($fieldValue && strpos('$lastWeek,$thisWeek,$today,$yesterday,$thisMonth,$lastMonth',$fieldValue) !== false)
  132. {
  133. $placeholder = "placeholder='{$fieldValue}'";
  134. }
  135. elseif($fieldName == 'id' and $formSession["operator$fieldNO"] == '=')
  136. {
  137. $placeholder = "placeholder='{$lang->search->queryTips}'";;
  138. }
  139. echo html::input("value$fieldNO", $fieldValue, "class='form-control $extraClass searchInput' $placeholder");
  140. }
  141. echo '</td>';
  142. $fieldNO ++;
  143. echo '</tr>';
  144. }
  145. ?>
  146. </tbody>
  147. </table>
  148. </td>
  149. <td class='text-center nobr w-70px'><?php echo html::select('groupAndOr', $lang->search->andor, $formSession['groupAndOr'], "class='form-control'")?></td>
  150. <td class='w-400px'>
  151. <table class='table table-form'>
  152. <tbody>
  153. <?php
  154. for($i = 1; $i <= $groupItems; $i ++)
  155. {
  156. $spanClass = $i == 1 ? '' : 'hidden';
  157. echo "<tr id='searchbox$fieldNO' class='$spanClass'>";
  158. /* Get params of current field. */
  159. $currentField = $formSession["field$fieldNO"];
  160. if(!isset($fieldParams[$currentField]))
  161. {
  162. $currentField = key($searchFields);
  163. $formSession["field$fieldNO"] = $currentField;
  164. $formSession["operator$fieldNO"] = isset($fieldParams[$currentField]['operator']) ? $fieldParams[$currentField]['operator'] : '=';
  165. $formSession["value$fieldNO"] = '';
  166. }
  167. $param = $fieldParams[$currentField];
  168. /* Print and or. */
  169. echo "<td class='text-right w-80px'>";
  170. if($i == 1) echo "<span id='searchgroup2'><strong>{$lang->search->group2}</strong></span>" . html::hidden("andOr$fieldNO", 'AND');
  171. if($i > 1) echo html::select("andOr$fieldNO", $lang->search->andor, $formSession["andOr$fieldNO"], "class='form-control'");
  172. echo '</td>';
  173. /* Print field. */
  174. echo "<td class='fieldWidth' style='overflow: visible'>" . html::select("field$fieldNO", $searchFields, $formSession["field$fieldNO"], "onchange='setField(this, $fieldNO, {$module}params)' class='form-control chosen'") . '</td>';
  175. /* Print operator. */
  176. echo "<td class='operatorWidth'>" . html::select("operator$fieldNO", $lang->search->operators, $formSession["operator$fieldNO"], "class='form-control' onchange='setPlaceHolder($fieldNO)'") . '</td>';
  177. /* Print value. */
  178. echo "<td id='valueBox$fieldNO'>";
  179. if(isset($config->moreLinks["field{$currentField}"]))
  180. {
  181. $selected = $formSession["value$fieldNO"];
  182. if(!isset($param['values'][$selected])) $config->moreLinks["value$fieldNO"] = $config->moreLinks["field{$currentField}"];
  183. }
  184. if($param['control'] == 'select') echo html::select("value$fieldNO", $param['values'], $formSession["value$fieldNO"], "class='form-control searchSelect chosen' data-max_drop_width='0'");
  185. if($param['control'] == 'input')
  186. {
  187. $fieldName = $formSession["field$fieldNO"];
  188. $fieldValue = $formSession["value$fieldNO"];
  189. $extraClass = isset($param['class']) ? $param['class'] : '';
  190. $placeholder = '';
  191. if($fieldValue && strpos('$lastWeek,$thisWeek,$today,$yesterday,$thisMonth,$lastMonth',$fieldValue) !== false)
  192. {
  193. $placeholder = "placeholder='{$fieldValue}'";
  194. }
  195. elseif($fieldName == 'id' and $formSession["operator$fieldNO"] == '=')
  196. {
  197. $placeholder = "placeholder='{$lang->search->queryTips}'";;
  198. }
  199. echo html::input("value$fieldNO", $fieldValue, "class='form-control $extraClass searchInput' $placeholder data-max_drop_width='0'");
  200. }
  201. echo '</td>';
  202. $fieldNO ++;
  203. echo '</tr>';
  204. }
  205. ?>
  206. </tbody>
  207. </table>
  208. </td>
  209. <?php if($style != 'simple'):?>
  210. <td class='w-160px hidden' rowspan='2' id='userQueries'>
  211. <h4><?php echo $lang->search->savedQuery;?></h4>
  212. <ul>
  213. <?php foreach($queries as $query):?>
  214. <?php if(empty($query->id)) continue;?>
  215. <li><?php echo html::a("javascript:executeQuery($query->id)", $query->title . ((common::hasPriv('search', 'deleteQuery') and $this->app->user->account == $query->account) ? '<i class="icon icon-close"></i>' : ''), '', "class='label user-query' data-query-id='$query->id' title='{$query->title}'");?></li>
  216. <?php endforeach;?>
  217. </ul>
  218. </td>
  219. </tr>
  220. <tr>
  221. <?php endif;?>
  222. <td colspan='3' class='text-center form-actions'>
  223. <?php
  224. echo html::hidden('module', $module);
  225. echo html::hidden('actionURL', $actionURL);
  226. echo html::hidden('groupItems', $groupItems);
  227. echo html::submitButton($lang->search->common, '', 'btn btn-primary') . " &nbsp; ";
  228. if($style != 'simple') echo html::commonButton($lang->search->reset, '', 'btn-reset-form btn');
  229. echo html::commonButton('<i class="icon icon-chevron-double-down"></i>', '', 'btn-expand-form btn btn-info pull-right');
  230. if($style != 'simple' and common::hasPriv('search', 'saveQuery')) echo html::a($this->createLink('search', 'saveQuery', "module=$module&onMenuBar=$onMenuBar"), '<span class="text"><i class="icon-bug-confirm icon-save"></i> ' . $lang->search->saveCondition . '</span>', '', "class='btn-save-form btn btn-link btn-active-text text iframe' id='save-query'");
  231. echo html::hidden('formType', zget($formSession, 'formType', 'lite'));
  232. ?>
  233. </td>
  234. </tr>
  235. </tbody>
  236. </table>
  237. </form>
  238. <?php js::set('searchCustom', $lang->search->custom);?>
  239. <?php js::set('canSaveQuery', !empty($_SESSION[$module . 'Query']));?>
  240. <script>
  241. var dtOptions =
  242. {
  243. language: '<?php echo $this->app->getClientLang();?>',
  244. weekStart: 1,
  245. todayBtn: 1,
  246. autoclose: 1,
  247. todayHighlight: 1,
  248. startView: 2,
  249. minView: 2,
  250. forceParse: 0,
  251. format: 'yyyy-mm-dd'
  252. };
  253. var <?php echo $module . 'params'?> = <?php echo empty($fieldParams) ? '{}' : json_encode($fieldParams);?>;
  254. var groupItems = <?php echo $config->search->groupItems;?>;
  255. var setQueryTitle = '<?php echo $lang->search->setQueryTitle;?>';
  256. var module = '<?php echo $module;?>';
  257. var actionURL = '<?php echo $actionURL;?>';
  258. function executeQuery(queryID)
  259. {
  260. if(!queryID) return;
  261. location.href = actionURL.replace('myQueryID', queryID);
  262. }
  263. $(function()
  264. {
  265. if(!canSaveQuery)
  266. {
  267. $('.btn-save-form').attr('disabled', 'disabled');
  268. $('.btn-save-form').css('pointer-events', 'none');
  269. }
  270. var $searchForm = $('#<?php echo $formId;?>');
  271. $searchForm.find('select.chosen').chosen().on('chosen:showing_dropdown', function()
  272. {
  273. var $this = $(this);
  274. var $chosen = $this.next('.chosen-container').removeClass('chosen-up');
  275. var $drop = $chosen.find('.chosen-drop');
  276. if($this.data('drop_direction') === 'auto') $chosen.toggleClass('chosen-up', $drop.height() + $drop.offset().top - $(document).scrollTop() > $(window).height());
  277. });
  278. $searchForm.find('.picker-select').each(function()
  279. {
  280. var $select = $(this);
  281. var pickerOptions = {chosenMode: true}
  282. if($select.attr('data-pickertype') == 'remote') pickerOptions.remote = $select.attr('data-pickerremote');
  283. $select.picker(pickerOptions);
  284. });
  285. $('#queryBox select, #queryBox input').change(function()
  286. {
  287. $('#save-query').attr("disabled", "disabled");
  288. })
  289. /* Toggle user queries action. */
  290. $('#toggle-queries').click(function()
  291. {
  292. $('#userQueries').toggleClass('hidden');
  293. if(!$('#userQueries').hasClass('hidden'))
  294. {
  295. $('#toggle-queries .icon').removeClass('icon-angle-left');
  296. $('#toggle-queries .icon').addClass('icon-angle-right');
  297. $('#toggle-queries').css('right', $('#userQueries').outerWidth());
  298. $('#save-query').css('right', $('#userQueries').outerWidth() + 50);
  299. }
  300. else
  301. {
  302. $('#toggle-queries .icon').removeClass('icon-angle-right');
  303. $('#toggle-queries .icon').addClass('icon-angle-left');
  304. $('#toggle-queries').css('right', '0px');
  305. $('#save-query').css('right', 50);
  306. }
  307. });
  308. $('.sidebar-toggle').click(function()
  309. {
  310. if(!$('#userQueries').hasClass('hidden')) $('#toggle-queries').click();
  311. })
  312. $(window).resize(function()
  313. {
  314. if(!$('#userQueries').hasClass('hidden'))
  315. {
  316. $('#toggle-queries').css('right', $('#userQueries').outerWidth());
  317. $('#save-query').css('right', $('#userQueries').outerWidth() + 50);
  318. }
  319. })
  320. /*
  321. * Load queries form
  322. */
  323. var loadQueries = window.loadQueries = function(queryID, shortcut, name)
  324. {
  325. $('#userQueries ul').load($.createLink('search', 'ajaxGetQuery', 'module=' + module + '&queryID=' + queryID));
  326. if(shortcut)
  327. {
  328. if($('#mainMenu .btn-toolbar.pull-left #query').size() == 0)
  329. {
  330. var html = '<div class="btn-group" id="query"><a href="javascript:;" data-toggle="dropdown" class="btn btn-link " style="border-radius: 2px;">' + searchCustom + ' <span class="caret"></span></a><ul class="dropdown-menu"></ul></div>';
  331. html += '<style>#mainMenu #query.btn-group li {position: relative;} #mainMenu #query.btn-group li a{margin-right:20px;} #mainMenu #query.btn-group li .btn-delete{ padding:0 7px; position: absolute; right: -10px; top: -3px; display: block; width: 20px; text-align: center; } </style>';
  332. html += "<script> function removeQueryFromMenu(obj) { var $obj = $(obj); var link = createLink('search', 'ajaxRemoveMenu', 'queryID=' + $obj.data('id')); $.get(link, function() { $obj.closest('li').remove(); if($('#mainMenu #query.btn-group').find('li').length == 0) $('#mainMenu #query.btn-group').remove(); })}<\/script>";
  333. $('#mainMenu .btn-toolbar.pull-left #bysearchTab').before(html);
  334. }
  335. html = "<li><a href='" + actionURL.replace('myQueryID', queryID) + "'>" + name + "</a>";
  336. html += "<a href='###' class='btn-delete' data-id='" + queryID + "' onclick='removeQueryFromMenu(this)'><i class='icon icon-close'></i></a></li>";
  337. $('#mainMenu .btn-toolbar.pull-left #query ul.dropdown-menu').append(html);
  338. }
  339. };
  340. /*
  341. * Expand or collapse form
  342. *
  343. * @param expand true for expand form, false for collapse form
  344. */
  345. var expandForm = function(expand)
  346. {
  347. if (expand === undefined) expand = !$searchForm.hasClass('showmore');
  348. $searchForm.toggleClass('showmore', expand);
  349. for(i = 1; i <= groupItems * 2; i ++)
  350. {
  351. if(i != 1 && i != groupItems + 1 )
  352. {
  353. $searchForm.find('#searchbox' + i).toggleClass('hidden', !expand);
  354. }
  355. }
  356. $searchForm.find('#formType').val(expand ? 'more' : 'lite');
  357. };
  358. /**
  359. * Set date field
  360. *
  361. * @param string $query
  362. * @return void
  363. */
  364. var setDateField = function(query, fieldNO)
  365. {
  366. var $period = $('#selectPeriod');
  367. <?php
  368. $selectPeriod = "<ul id='selectPeriod' class='dropdown-menu'>";
  369. $selectPeriod .= "<li class='dropdown-header'>{$lang->datepicker->dpText->TEXT_OR} {$lang->datepicker->dpText->TEXT_DATE}</li>";
  370. $selectPeriod .= "<li><a href='#lastWeek'>{$lang->datepicker->dpText->TEXT_PREV_WEEK}</a></li>";
  371. $selectPeriod .= "<li><a href='#thisWeek'>{$lang->datepicker->dpText->TEXT_THIS_WEEK}</a></li>";
  372. $selectPeriod .= "<li><a href='#yesterday'>{$lang->datepicker->dpText->TEXT_YESTERDAY}</a></li>";
  373. $selectPeriod .= "<li><a href='#today'>{$lang->datepicker->dpText->TEXT_TODAY}</a></li>";
  374. $selectPeriod .= "<li><a href='#lastMonth'>{$lang->datepicker->dpText->TEXT_PREV_MONTH}</a></li>";
  375. $selectPeriod .= "<li><a href='#thisMonth'>{$lang->datepicker->dpText->TEXT_THIS_MONTH}</a></li></ul>";
  376. ?>
  377. $period = $(<?php echo json_encode($selectPeriod);?>).appendTo('body');
  378. $period.find('li > a').click(function(event)
  379. {
  380. var target = $(query).closest('form').find('#' + $period.data('target'));
  381. if(target.length)
  382. {
  383. if(target.next('input[type=hidden]').length)
  384. {
  385. target.next('input[type=hidden]').val($(this).attr('href').replace('#', '$'));
  386. }
  387. else
  388. {
  389. target.val($(this).attr('href').replace('#', '$'));
  390. }
  391. target.attr('placeholder', $(this).attr('href').replace('#', '$'));
  392. $(query).closest('form').find('#operator' + $period.data('fieldNO')).val('between');
  393. $period.hide();
  394. }
  395. event.stopPropagation();
  396. return false;
  397. });
  398. $(query).datetimepicker('remove').datepicker(dtOptions).on('show', function(e)
  399. {
  400. var $e = $(e.target);
  401. var ePos = $e.offset();
  402. $period.css({'left': ePos.left - 120, 'top': ePos.top + 29, 'min-height': $('.datetimepicker').outerHeight()}).show().data('target', $e.attr('id')).data('fieldNO', fieldNO).find('li.active').removeClass('active');
  403. if($e.attr('placeholder'))
  404. {
  405. $period.find("li > a[href='" + $e.attr('placeholder').replace('$', '#') + "']").closest('li').addClass('active');
  406. }
  407. else
  408. {
  409. $period.find("li > a[href='" + $e.val().replace('$', '#') + "']").closest('li').addClass('active');
  410. }
  411. }).on('changeDate', function()
  412. {
  413. var opt = $(query).closest('form').find('#operator' + $period.data('fieldNO'));
  414. var target = $('#' + $period.data('target'));
  415. if(target.length)
  416. {
  417. if(target.next('input[type=hidden]').length)
  418. {
  419. target.next('input[type=hidden]').val(target.val());
  420. }
  421. }
  422. if(opt.val() == 'between') opt.val('<=');
  423. $period.hide();
  424. }).on('hide', function(){setTimeout(function(){$period.hide();}, 200);});
  425. }
  426. /**
  427. * When the value of the fields select changed, set the operator and value of the new field.
  428. *
  429. * @param string $obj
  430. * @param int $fieldNO
  431. * @access public
  432. * @return void
  433. */
  434. var setField = window.setField = function(obj, fieldNO, moduleparams)
  435. {
  436. var params = moduleparams;
  437. var $obj = $(obj);
  438. var fieldName = $obj.val();
  439. $searchForm.find('#operator' + fieldNO).val(params[fieldName]['operator']); // Set the operator according the param setting.
  440. $searchForm.find('#valueBox' + fieldNO).html($searchForm.find('#box' + fieldName).children().clone());
  441. $searchForm.find('#valueBox' + fieldNO).children().attr({name : 'value' + fieldNO, id : 'value' + fieldNO});
  442. if(fieldName == 'id') setPlaceHolder(fieldNO);
  443. if(typeof(params[fieldName]['class']) != undefined && params[fieldName]['class'] == 'date')
  444. {
  445. setDateField($searchForm.find("#value" + fieldNO), fieldNO);
  446. $searchForm.find("#value" + fieldNO).addClass('date'); // Shortcut the width of the datepicker to make sure align with others.
  447. var maxNO = 2 * groupItems;
  448. var nextNO = fieldNO > groupItems ? fieldNO - groupItems + 1 : fieldNO + groupItems;
  449. var nextValue = $searchForm.find('#value' + nextNO).val();
  450. var operator = $searchForm.find("#operator" + fieldNO).val();
  451. if(nextNO <= maxNO && fieldNO < maxNO && (nextValue == '' || nextValue == 0) && operator == ">=")
  452. {
  453. $searchForm.find('#field' + nextNO).val($searchForm.find('#field' + fieldNO).val());
  454. $searchForm.find('#operator' + nextNO).val('<=');
  455. $searchForm.find('#valueBox' + nextNO).html($searchForm.find('#box' + fieldName).children().clone());
  456. $searchForm.find('#valueBox' + nextNO).children().attr({name : 'value' + nextNO, id : 'value' + nextNO});
  457. setDateField($searchForm.find("#value" + nextNO), nextNO);
  458. $searchForm.find("#value" + nextNO).addClass('date');
  459. }
  460. }
  461. else if(params[fieldName]['control'] == 'select')
  462. {
  463. $searchForm.find(".picker#value" + fieldNO).remove();
  464. if($searchForm.find("#value" + fieldNO).attr('data-pickertype') == 'remote')
  465. {
  466. $searchForm.find("#value" + fieldNO).picker(
  467. {
  468. chosenMode: true,
  469. dropWidth: 'auto',
  470. minAutoDropWidth: '100%',
  471. maxAutoDropWidth: 350,
  472. remote: $searchForm.find("#value" + fieldNO).attr('data-pickerremote')
  473. });
  474. }
  475. else
  476. {
  477. $searchForm.find("#value" + fieldNO).picker(
  478. {
  479. chosenMode: true,
  480. dropWidth: '100%'
  481. });
  482. }
  483. }
  484. };
  485. /**
  486. * When the value of the operator select changed, set the placeholder for the valueBox.
  487. *
  488. * @param int $fieldNO
  489. * @access public
  490. * @return void
  491. */
  492. var setPlaceHolder = window.setPlaceHolder = function(fieldNO)
  493. {
  494. var operator = $('#operator' + fieldNO).val();
  495. var fieldName = $('#field' + fieldNO).val();
  496. if(operator == '=' && fieldName == 'id')
  497. {
  498. $('#value' + fieldNO).attr("placeholder","<?php echo $lang->search->queryTips;?>");
  499. }
  500. else
  501. {
  502. $('#value' + fieldNO).attr("placeholder","");
  503. }
  504. }
  505. /*
  506. * Reset form
  507. */
  508. window.resetForm = function()
  509. {
  510. for(i = 1; i <= groupItems * 2; i ++)
  511. {
  512. if(!$searchForm.find('#value' + i).hasClass('picker-select')) $searchForm.find('#value' + i).val('').trigger('chosen:updated');
  513. if($searchForm.find('#value' + i).hasClass('picker-select')) $searchForm.find('#value' + i).data('zui.picker').setValue('');
  514. $searchForm.find('#value' + i + '.date').val('').attr('placeholder', '');
  515. }
  516. };
  517. $searchForm.on('click', '.btn-expand-form', function() {expandForm();});
  518. $searchForm.on('click', '.btn-reset-form', function() {resetForm();});
  519. $searchForm.on('change', 'select[id^="operator"]', function()
  520. {
  521. var $select = $(this);
  522. var value = $select.val();
  523. var $tr = $select.closest('tr');
  524. if(value == '>=' && $tr.find('input[id^="value"].date').length)
  525. {
  526. var fieldNO = parseInt($(this).attr('id').replace('operator', ''));
  527. var fieldName = $tr.find("select[id^='field']").val();
  528. var maxNO = 2 * groupItems;
  529. var nextNO = fieldNO > groupItems ? fieldNO - groupItems + 1 : fieldNO + groupItems;
  530. var nextValue = searchForm.find('#value' + nextNO).val();
  531. if(nextNO <= maxNO && fieldNO < maxNO && (nextValue == '' || nextValue == 0))
  532. {
  533. searchForm.find('#field' + nextNO).val(searchForm.find('#field' + fieldNO).val());
  534. searchForm.find('#operator' + nextNO).val('<=');
  535. searchForm.find('#valueBox' + nextNO).html(searchForm.find('#box' + fieldName).children().clone());
  536. searchForm.find('#valueBox' + nextNO).children().attr({name : 'value' + nextNO, id : 'value' + nextNO});
  537. setDateField(searchForm.find("#value" + nextNO), nextNO);
  538. searchForm.find("#value" + nextNO).addClass('date');
  539. }
  540. }
  541. });
  542. $searchForm.find('.btn-save-form').modalTrigger({width:650, type:'iframe', title: setQueryTitle});
  543. if($('#formType').val() == 'more') expandForm(true);
  544. $searchForm.on('click', '.user-query .icon-close', function(e)
  545. {
  546. e.preventDefault(); // Fix bug #21572.
  547. var $query = $(this).closest('.user-query');
  548. var queryId = $query.data('queryId');
  549. var deleteQueryLink = $.createLink('search', 'deleteQuery', 'queryID=' + queryId);
  550. $.get(deleteQueryLink, function(data)
  551. {
  552. if(data == 'success') $query.remove();
  553. });
  554. e.stopPropagation();
  555. });
  556. /* Init datepicker for search. */
  557. $searchForm.find('.table-condensed input.date').each(function()
  558. {
  559. setDateField($(this), $(this).attr('id').substr(5));
  560. });
  561. });
  562. </script>