filter.php
См. документацию.
1 <?php
2 
3 /**
4  * @file
5  * @brief окно фильтров моделей
6  */
7 
8 isset($user) or require $_SERVER['DOCUMENT_ROOT'] . '/page/404.php';
9 
10 if (!in_array($_GET['table'], Wrong\Database\Controller::$tables)) {
11  exit('<script>errorToast("Ошибка!");</script>');
12 }
13 
15  'act' => ['0', '1'],
16  'owner_group' => array_map('strval', array_column(Wrong\Rights\Group::$groups_owners, 'id')),
17  'groups' => array_map('strval', array_column(Wrong\Rights\Group::$groups_not_system, 'id'))
18 ];
19 
20 if ($_GET['table'] == 'templates') {
21  $template_types = ['page', 'incode', 'modal', 'select', 'action'];
22  $initial['type'] = array_map('strval', array_keys($template_types));
23 }
24 // ksort($initial);
25 
26 $filter = isset($_SESSION['filter'][$_GET['table']]) ? $_SESSION['filter'][$_GET['table']] : $initial;
27 
28 ?>
29 <div class="modal fade" id="<?= $basename ?>" tabindex="-1" data-backdrop="static">
30  <div class="modal-dialog modal-dialog-centered" role="document">
31  <div class="modal-content">
32  <div class="modal-header">
33  <h5 class="modal-title"><i class="fa fa-filter mr-2"></i>Фильтр "<?= $dbh->query("SHOW TABLE STATUS WHERE Name = '{$_GET['table']}'")->fetch()->Comment ?>"</h5>
34  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
35  <span aria-hidden="true">&times;</span>
36  </button>
37  </div>
38  <div class="modal-body pt-2">
39  <form action="<?= Wrong\Models\Actions::find(41)->request ?>">
40  <input type="hidden" name="table" value="<?= $_GET['table'] ?>">
41  <div class="border px-2 py-1 rounded bg-light-info">
42  <div class="custom-control custom-checkbox small">
43  <input type="checkbox" name="act[0]" value="0" class="custom-control-input" id="check-group-0">
44  <label class="custom-control-label" for="check-group-0">Не активные</label>
45  </div>
46  <div class="custom-control custom-checkbox small">
47  <input type="checkbox" name="act[1]" value="1" class="custom-control-input" id="check-group-1">
48  <label class="custom-control-label" for="check-group-1">Активные</label>
49  </div>
50  </div>
51  <?php if ($_GET['table'] == 'templates') : ?>
52  <div class="border px-2 py-1 rounded bg-light-info masscheck mt-2">
53  <small>Типы шаблонов <a onclick="if(~~this.dataset.checked){$(this).html('отметить все');this.dataset.checked=0;$(this).parents('form').find('[name^=type]').prop('checked', false);}else{$(this).html('снять все');this.dataset.checked=1;$(this).parents('form').find('[name^=type]').prop('checked', true);}$('#<?= $basename ?> form').trigger('change');return false;" href="#"></a></small>
54  <?php
55  foreach ($initial['type'] as $key) {
56  echo '<div class="custom-control custom-checkbox small">
57  <input type="checkbox" name="type[' . $key . ']" value="' . $key . '" class="custom-control-input" id="check-type-' . $key . '">
58  <label class="custom-control-label" for="check-type-' . $key . '">' . $template_types[$key] . '</label>
59  </div>';
60  }
61  ?>
62  </div>
63  <?php endif; ?>
64  <div class="border px-2 py-1 rounded bg-light-info masscheck mt-2">
65  <small>Группа владелец <a onclick="if(~~this.dataset.checked){$(this).html('отметить все');this.dataset.checked=0;$(this).parents('form').find('[name^=owner_group]').prop('checked', false);}else{$(this).html('снять все');this.dataset.checked=1;$(this).parents('form').find('[name^=owner_group]').prop('checked', true);}$('#<?= $basename ?> form').trigger('change');return false;" href="#"></a></small>
66  <?php
67  foreach (Wrong\Rights\Group::$groups_owners as $row) {
68  echo '<div class="custom-control custom-checkbox small">
69  <input type="checkbox" name="owner_group[' . $row->id . ']" value="' . $row->id . '" class="custom-control-input" id="check-owner_group-' . $row->id . '">
70  <label class="custom-control-label" for="check-owner_group-' . $row->id . '">' . Wrong\Rights\Group::text($row->id) . '</label>
71  </div>';
72  }
73  ?>
74  </div>
75  <div class="border px-2 py-1 rounded bg-light-info masscheck mt-2 <?= in_array($_GET['table'], ['crontabs', 'groups']) ? 'd-none' : '' ?>">
76  <small>Группы доступа <a onclick="if(~~this.dataset.checked){$(this).html('отметить все');this.dataset.checked=0;$(this).parents('form').find('[name^=groups]').prop('checked', false);}else{$(this).html('снять все');this.dataset.checked=1;$(this).parents('form').find('[name^=groups]').prop('checked', true);}$('#<?= $basename ?> form').trigger('change');return false;" href="#"></a></small>
77  <?php
78  foreach (Wrong\Rights\Group::$groups_not_system as $row) {
79  echo '<div class="custom-control custom-checkbox small">
80  <input type="checkbox" name="groups[' . $row->id . ']" value="' . $row->id . '" class="custom-control-input" id="check-groups-' . $row->id . '">
81  <label class="custom-control-label" for="check-groups-' . $row->id . '">' . Wrong\Rights\Group::text($row->id) . '</label>
82  </div>';
83  }
84  ?>
85  </div>
86  </form>
87  </div>
88  </div>
89  </div>
90  <script>
91  $(function() {
92  let filter = <?= json_encode($filter) ?>;
93  for (let key in filter) {
94  filter[key].forEach(el => {
95  $('#<?= $basename ?> [name="' + key + '[' + el + ']"]').prop('checked', true);
96  });
97  }
98 
99  $("#<?= $basename ?> .masscheck").each(function() {
100  if ($(this).find('input[type=checkbox]').not(':checked').length) {
101  $(this).find('a').html('отметить все');
102  $(this).find('a')[0].dataset.checked = 0;
103  } else {
104  $(this).find('a').html('снять все');
105  $(this).find('a')[0].dataset.checked = 1;
106  }
107  });
108  });
109 
110  $("#<?= $basename ?> form").change(function() {
111  $(this).trigger('submit');
112  });
113 
114  function isEqual(object1, object2) {
115  const props1 = Object.getOwnPropertyNames(object1);
116  const props2 = Object.getOwnPropertyNames(object2);
117 
118  if (props1.length !== props2.length) {
119  return false;
120  }
121 
122  for (let i = 0; i < props1.length; i += 1) {
123  const prop = props1[i];
124  const bothAreObjects = typeof(object1[prop]) === 'object' && typeof(object2[prop]) === 'object';
125 
126  if ((!bothAreObjects && (object1[prop] !== object2[prop])) ||
127  (bothAreObjects && !isEqual(object1[prop], object2[prop]))) {
128  return false;
129  }
130  }
131 
132  return true;
133  }
134 
135  $("#<?= $basename ?> form").submit(function(e) {
136  e.preventDefault();
137  $.ajax({
138  type: "POST",
139  url: $(this).attr("action"),
140  data: $(this).serialize(),
141  dataType: "json",
142  statusCode: {
143  404: errorToast,
144  403: errorToast
145  }
146  })
147  .done(response => {
148  if (response.error) {
149  errorToast(response.error);
150  return;
151  }
152  if (!isEqual(response.filter, <?= json_encode($initial) ?>)) {
153  $('#reset-filter').show();
154  $('.toast').toast('hide');
155  successToast(response.message);
156  } else {
157  $('#reset-filter').trigger('click');
158  $('#reset-filter').hide();
159  }
160  $('.dataTable').DataTable().ajax.reload(null, false);
161  });
162  });
163  </script>
164 </div>
$_SESSION['filter']
Definition: filter.php:22
static text($id)
Definition: Group.php:233
$user
Definition: from-user.php:38
setcookie('FROM_UID', $uid, [ 'expires'=> time()+31536000, 'path'=> '/', 'domain'=> $_SERVER['HTTP_HOST'], 'secure'=> Wrong\Start\Env::$e->IS_SECURE, 'httponly'=> false, 'samesite'=> Wrong\Start\Env::$e->IS_SECURE ? 'None' :'Lax']) or setcookie('FROM_UID' $_SERVER['HTTP_HOST']
Definition: from-user.php:36
if(($dbh=Connect::getInstance(true) ->dbh) && $dbh->query("SHOW TABLES") ->fetchAll() && $dbh->query("SELECT COUNT(*) FROM `users`") ->fetchColumn()) if(!empty($_POST)) exit
Definition: install.php:198
this parents( 'form').find(' endif[name^ foreach($initial['type'] as $key)
Definition: filter.php:55
if(!in_array($_GET['table'], Wrong\Database\Controller::$tables)) $initial
Definition: filter.php:14
if($_GET['table']=='templates') $filter
Definition: filter.php:26
href
Definition: main.php:21