Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Плагин (событие handlerequest)
- ##################################################### PHP PLUGIN #####################################################
- <?php
- $sq = 0;
- $publishedon = 0;
- function convertDate($dt, $next){
- if(!$dt){
- $dt = date('Y', strtotime(date('Y-m-d')));
- $dt = "$dt-1-1";
- } else {
- $dt = "$dt-1-1";
- }
- if($next == '1'){
- $dt = date("Y-m-d",strtotime ( '+1 year' , strtotime ( $dt ) )) ;
- }
- return strtotime($dt);
- }
- if(sizeof($_POST) != 0){
- switch ($_POST['action']) {
- case 'filter':
- $Data = $_POST;
- if (sizeof($Data['fields']) >= 0) {
- $hash = (string) $_POST['hash'];
- $where = array();
- $fields = $Data['fields'];
- foreach($fields as $key => $value){
- switch ($key){
- case 'publishedon':
- $where[$sq ? "AND:".$key.":>=" : $key.":>="] = convertDate($fields[$key], false);
- $where["AND:".$key.":<="] = convertDate($fields[$key], true);
- $sq++;
- break;
- default:
- $where[$key] = $sq ? "AND:".$fields[$key] : $fields[$key];
- $sq++;
- break;
- }
- }
- $_SESSION['pdoPage'][$hash]['where'] = $where;
- $output['message'] = $where;
- $output['success'] = true;
- echo $modx->toJSON($output);
- die();
- } else {
- $output['message'] = 'Error';
- echo 'Error';
- die();
- }
- break;
- }
- }
- ##################################################### PHP PLUGIN #####################################################
- ##################################################### JS #####################################################
- NewsBlock = getElement('#news_sort');
- if(NewsBlock){
- let filterContainer = {};
- function SendAjaxFilterToServer(fields){
- $.post(document.location.href, {
- action: 'filter',
- fields: fields,
- hash: pdoPage.configs.page.hash
- }, function(data) {
- console.log(data)
- var tmp = document.location.href;
- pdoPage.keys.page = 0;
- pdoPage.loadPage(tmp, pdoPage.configs.page);
- });
- }
- addAndSort = el => {
- sortField = getAttribute(el, 'data-sort_field', true);
- sortFieldValue = getAttribute(el, 'data-sort_value', true);
- if(sortFieldValue == 'all'){
- delete filterContainer[sortField];
- } else {
- filterContainer[sortField] = sortFieldValue;
- }
- getElement('.addtexthere', getParent(el, 3)).textContent = el.textContent;
- SendAjaxFilterToServer(filterContainer);
- };
- }
- ##################################################### JS #####################################################
- ##################################################### HTML #####################################################
- <div class="sort-block__wrapper">
- <div class="selected-type --sort addtexthere">Все</div>
- <ul class="dropdown">
- <li><button data-sort_field="newsType" data-sort_value="all" onclick="addAndSort(this);">Все</button></li>
- <li><button data-sort_field="newsType" data-sort_value="newsaboutcompany" onclick="addAndSort(this);">Новости компании</button></li>
- <li><button data-sort_field="newsType" data-sort_value="industry" onclick="addAndSort(this);">Новости отрасли</button></li>
- </ul>
- </div>
- <div class="sort-block">
- <span>Период публикации</span>
- <div class="sort-block__wrapper">
- <div class="selected-year --sort addtexthere">Всё время</div>
- <ul class="dropdown">
- <li><button data-sort_field="publishedon" data-sort_value="all" onclick="addAndSort(this);">Всё время</a></li>
- <li><button data-sort_field="publishedon" data-sort_value="2022" onclick="addAndSort(this);">2022</button></li>
- <li><button data-sort_field="publishedon" data-sort_value="2021" onclick="addAndSort(this);">2021</button></li>
- <li><button data-sort_field="publishedon" data-sort_value="2020" onclick="addAndSort(this);">2020</button></li>
- </ul>
- </div>
- </div>
- ##################################################### HTML #####################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement