Advertisement
Guest User

Во имя святого отче нашего безумкина одарившего меня 3 банами

a guest
Jan 16th, 2022
11
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Плагин (событие handlerequest)
  2. ##################################################### PHP PLUGIN #####################################################
  3. <?php
  4.  
  5. $sq = 0;
  6. $publishedon = 0;
  7.  
  8. function convertDate($dt, $next){
  9. if(!$dt){
  10. $dt = date('Y', strtotime(date('Y-m-d')));
  11. $dt = "$dt-1-1";
  12. } else {
  13. $dt = "$dt-1-1";
  14. }
  15.  
  16. if($next == '1'){
  17. $dt = date("Y-m-d",strtotime ( '+1 year' , strtotime ( $dt ) )) ;
  18. }
  19.  
  20. return strtotime($dt);
  21. }
  22.  
  23. if(sizeof($_POST) != 0){
  24. switch ($_POST['action']) {
  25. case 'filter':
  26. $Data = $_POST;
  27. if (sizeof($Data['fields']) >= 0) {
  28.  
  29. $hash = (string) $_POST['hash'];
  30. $where = array();
  31. $fields = $Data['fields'];
  32. foreach($fields as $key => $value){
  33. switch ($key){
  34. case 'publishedon':
  35. $where[$sq ? "AND:".$key.":>=" : $key.":>="] = convertDate($fields[$key], false);
  36. $where["AND:".$key.":<="] = convertDate($fields[$key], true);
  37. $sq++;
  38. break;
  39. default:
  40. $where[$key] = $sq ? "AND:".$fields[$key] : $fields[$key];
  41. $sq++;
  42. break;
  43. }
  44. }
  45.  
  46.  
  47.  
  48. $_SESSION['pdoPage'][$hash]['where'] = $where;
  49. $output['message'] = $where;
  50. $output['success'] = true;
  51.  
  52. echo $modx->toJSON($output);
  53. die();
  54. } else {
  55. $output['message'] = 'Error';
  56. echo 'Error';
  57. die();
  58. }
  59.  
  60.  
  61. break;
  62.  
  63. }
  64. }
  65. ##################################################### PHP PLUGIN #####################################################
  66.  
  67. ##################################################### JS #####################################################
  68. NewsBlock = getElement('#news_sort');
  69.  
  70.  
  71. if(NewsBlock){
  72.  
  73.  
  74. let filterContainer = {};
  75.  
  76. function SendAjaxFilterToServer(fields){
  77.  
  78. $.post(document.location.href, {
  79. action: 'filter',
  80. fields: fields,
  81. hash: pdoPage.configs.page.hash
  82.  
  83. }, function(data) {
  84. console.log(data)
  85. var tmp = document.location.href;
  86. pdoPage.keys.page = 0;
  87. pdoPage.loadPage(tmp, pdoPage.configs.page);
  88.  
  89. });
  90.  
  91.  
  92. }
  93.  
  94. addAndSort = el => {
  95.  
  96. sortField = getAttribute(el, 'data-sort_field', true);
  97. sortFieldValue = getAttribute(el, 'data-sort_value', true);
  98. if(sortFieldValue == 'all'){
  99. delete filterContainer[sortField];
  100. } else {
  101. filterContainer[sortField] = sortFieldValue;
  102. }
  103.  
  104. getElement('.addtexthere', getParent(el, 3)).textContent = el.textContent;
  105. SendAjaxFilterToServer(filterContainer);
  106.  
  107. };
  108.  
  109.  
  110. }
  111.  
  112. ##################################################### JS #####################################################
  113.  
  114. ##################################################### HTML #####################################################
  115. <div class="sort-block__wrapper">
  116. <div class="selected-type --sort addtexthere">Все</div>
  117. <ul class="dropdown">
  118. <li><button data-sort_field="newsType" data-sort_value="all" onclick="addAndSort(this);">Все</button></li>
  119. <li><button data-sort_field="newsType" data-sort_value="newsaboutcompany" onclick="addAndSort(this);">Новости компании</button></li>
  120. <li><button data-sort_field="newsType" data-sort_value="industry" onclick="addAndSort(this);">Новости отрасли</button></li>
  121. </ul>
  122. </div>
  123.  
  124. <div class="sort-block">
  125. <span>Период публикации</span>
  126. <div class="sort-block__wrapper">
  127. <div class="selected-year --sort addtexthere">Всё время</div>
  128. <ul class="dropdown">
  129. <li><button data-sort_field="publishedon" data-sort_value="all" onclick="addAndSort(this);">Всё время</a></li>
  130. <li><button data-sort_field="publishedon" data-sort_value="2022" onclick="addAndSort(this);">2022</button></li>
  131. <li><button data-sort_field="publishedon" data-sort_value="2021" onclick="addAndSort(this);">2021</button></li>
  132. <li><button data-sort_field="publishedon" data-sort_value="2020" onclick="addAndSort(this);">2020</button></li>
  133. </ul>
  134. </div>
  135. </div>
  136. ##################################################### HTML #####################################################
  137.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement