Skip to content

Commit

Permalink
improv. fix ban controller
Browse files Browse the repository at this point in the history
  • Loading branch information
nivcoo committed Aug 28, 2021
1 parent 92ce8b7 commit 9b553bd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
40 changes: 34 additions & 6 deletions app/Controller/BanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ function admin_index()

$this->set('title_for_layout', $this->Lang->get("BAN__HOME"));
$this->layout = 'admin';

$this->loadModel("user");
$this->loadModel("Ban");
$this->loadModel("User");
$banned_users = $this->Ban->find("all");

$users = $this->User->find("all");
Expand All @@ -34,6 +33,7 @@ function admin_add()

$this->set('title_for_layout', $this->Lang->get("BAN__HOME"));
$this->layout = 'admin';
$this->set('type', $this->Configuration->getKey('member_page_type'));

if ($this->request->is("post")) {
$this->autoRender = false;
Expand Down Expand Up @@ -79,12 +79,11 @@ public function admin_get_users_not_ban()
0 => ['label' => 'success', 'name' => $this->Lang->get('USER__RANK_MEMBER')],
2 => ['label' => 'warning', 'name' => $this->Lang->get('USER__RANK_MODERATOR')],
3 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')],
4 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')],
5 => ['label' => 'primary', 'name' => $this->Lang->get('USER__RANK_BANNED')]
4 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')]
];
$this->loadModel('Rank');
$custom_ranks = $this->Rank->find('all');
foreach ($custom_ranks as $value) {
foreach ($custom_ranks as $key => $value) {
$available_ranks[$value['Rank']['rank_id']] = [
'label' => 'info',
'name' => $value['Rank']['name']
Expand All @@ -100,7 +99,7 @@ public function admin_get_users_not_ban()
$response = $this->DataTable->getResponse();
$users = $response['aaData'];
$data = [];
foreach ($users as $value) {
foreach ($users as $key => $value) {
$checkIsBan = $this->Ban->find('first', ["conditions" => ['user_id' => $value['User']['id']]]);

if ($checkIsBan != null)
Expand All @@ -127,4 +126,33 @@ public function admin_get_users_not_ban()
}
}
}

function admin_liveSearch($query = false)
{
$this->autoRender = false;
$this->response->type('json');
if ($this->isConnected and $this->Permissions->can('MANAGE_USERS')) {
$this->loadModel("User");
if ($query != false) {
$result = $this->User->find('all', ['conditions' => ['pseudo LIKE' => $query . '%']]);
foreach ($result as $key => $value) {
$checkIsBan = $this->Ban->find('first', ["conditions" => ['user_id' => $value['User']['id']]]);

if ($checkIsBan != null)
continue;

if ($this->Permissions->have($value['User']['rank'], "CAN_BE_BAN"))
continue;

$users[] = ['pseudo' => $value['User']['pseudo'], 'id' => $value['User']['id']];
}
$response = (empty($result)) ? ['status' => false] : ['status' => true, 'data' => $users];
$this->response->body($response);
} else {
$this->response->body(json_encode(['status' => false]));
}
} else {
$this->response->body(json_encode(['status' => false]));
}
}
}
55 changes: 53 additions & 2 deletions app/View/Ban/admin_add.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<div class="form-group">
<label><?= $Lang->get('BAN__REASON') ?></label>
<input type="text" class="form-control"
value="<?= $page['title'] ?>"
name="reason">
</div>
</div>
Expand All @@ -40,6 +39,7 @@
</div>
</section>
<script type="text/javascript">
<?php if($type == '0') { ?>
$(document).ready(function () {
$('#users').DataTable({
"paging": true,
Expand All @@ -53,10 +53,61 @@
"bServerSide": true,
"sAjaxSource": "<?= $this->Html->url(['action' => 'get_users_not_ban']) ?>",
"aoColumns": [
{mData: "User.ban", "bSearchable": true},
{mData: "User.ban", "bSearchable": false},
{mData: "User.pseudo", "bSearchable": true},
{mData: "User.rank", "bSearchable": false}
]
});
});
<?php } else { ?>
$('form[method="search"]').each(function (e) {

$(this).on('submit', function (e) {
e.preventDefault();
var val = $(this).find('input[name="search"]').val();
window.location = '<?= $this->Html->url(['action' => 'edit']) ?>/' + val;
});

var url = $(this).attr('action');
var form = $(this);

$(this).find('input[name="search"]').keyup(function (e) {

var value = $(this).val();

$.ajax({
url: url + '/' + encodeURI(value),
method: 'GET',
dataType: 'JSON',
success: function (data) {

form.find('.list-group').empty();

if (data.status) {

var users = data.data;

for (var i = 0; i < users.length; i++) {

console.log(users[i]);

form.find('.list-group').prepend('<a href="<?= $this->Html->url(['action' => 'edit']) ?>/' + users[i]['id'] + '" class="list-group-item">' + users[i]['pseudo'] + '</a>')

}

form.find('.list-group').slideDown(250);

} else {
form.find('.list-group').slideUp(250);
}

},
error: function (data) {
form.find('.list-group').slideUp(250);
}
})

});
});
<?php } ?>
</script>

0 comments on commit 9b553bd

Please sign in to comment.