Skip to content

Commit

Permalink
UPD: pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Itskiprotich committed Aug 28, 2024
1 parent 5ce5f27 commit cb2e33a
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 136 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dereuromark/cakephp-captcha": "^1.4",
"dereuromark/cakephp-queue": "^6.9",
"friendsofcake/cakepdf": "^4.1",
"friendsofcake/search": "^6.2",
"josegonzalez/cakephp-upload": "^7.0",
"mobiledetect/mobiledetectlib": "^3.74"
},
Expand Down
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function bootstrap(): void
// Load more plugins here
$this->addPlugin('Josegonzalez/Upload');
$this->addPlugin('CakePdf');
$this->addPlugin('Search');
}

/**
Expand Down
24 changes: 11 additions & 13 deletions src/Controller/Admin/MessagesController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace App\Controller\Admin;
Expand All @@ -25,21 +26,18 @@ class MessagesController extends AppController
*/
public function index()
{
// $page_options = array('25' => '25', '20' => '20');
// $messages = $this->paginate($this->Messages);
// $this->set('page_options', $page_options);
// $this->set(compact('messages'));
$limit = $this->request->getQuery('pages', 1000); // Default to 10 if 'pages' is not set


$filters = [
'name' => $this->request->getQuery('name'),
// 'email' => $this->request->getQuery('email'),
// 'status' => $this->request->getQuery('status'),
];
$messages = $this->paginate(
$this->Messages->find(
'search',
['search' => $this->request->getQuery()]
),
['limit' => $limit]
);

$query = $this->Messages->find();
$query = $this->Messages->search($query, $filters);
$page_options = array('25' => '25', '20' => '20');
$messages = $this->paginate($query);
$page_options = array('5' => '5', '10' => '10', '25' => '25', '20' => '20');
$this->set(compact('messages'));
$this->set('page_options', $page_options);
}
Expand Down
17 changes: 12 additions & 5 deletions src/Controller/Admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
*/
class UsersController extends AppController
{

public function initialize(): void
{
parent::initialize();

}

public function dashboard()
{
Expand All @@ -29,12 +33,15 @@ public function dashboard()
*/
public function index()
{
$limit = $this->request->getQuery('pages', 1000); // Default to 10 if 'pages' is not set

$this->paginate = [
'contain' => ['Designations', 'Counties', 'Roles'],
];
$users = $this->paginate($this->Users);
// dd($users);

'limit'=>$limit

];
$users = $this->paginate($this->Users->find('search', ['search' => $this->request->getQuery()]));

$this->set(compact('users'));
}

Expand Down
1 change: 1 addition & 0 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function initialize(): void
*/
//$this->loadComponent('FormProtection');
$this->loadComponent('Paginator');
$this->loadComponent('Search.Search');

$this->loadComponent('Acl', [
'className' => 'Acl.Acl'
Expand Down
12 changes: 11 additions & 1 deletion src/Model/Table/MessagesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ public function initialize(array $config): void
$this->setPrimaryKey('id');

$this->addBehavior('Timestamp');
// $this->addBehavior('Search.Search');

$this->addBehavior('Search.Search');

$this->searchManager()
->value('subject')
->value('content')
->like('name', ['fields' => ['name']])
->value('sms');
// ->compare('start_date', ['operator' => '>=', 'fields' => ['created']])
// ->compare('end_date', ['operator' => '<=', 'fields' => ['created']])
// ->value('designation_id');
}

public function search(Query $query, array $filters): Query
Expand Down
31 changes: 26 additions & 5 deletions src/Model/Table/UsersTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UsersTable extends Table
array('name' => 'username', 'type' => 'like'),
array('name' => 'name', 'type' => 'like'),
array('name' => 'email', 'type' => 'like'),
array('name' => 'range', 'type' => 'expression', 'method' => 'makeRangeCondition', 'field' => 'Users.created BETWEEN ? AND ?'),
array('name' => 'range', 'type' => 'expression', 'method' => 'makeRangeCondition', 'field' => 'Users.created BETWEEN ? AND ?'),
);

/**
Expand All @@ -79,6 +79,27 @@ public function initialize(array $config): void
$this->addBehavior('Timestamp');
$this->addBehavior('Acl.Acl', ['type' => 'requester']);

$this->addBehavior('Search.Search');

$this->searchManager()
->value('username')
->value('role_id')
->value('email')
->compare('start_date', ['operator' => '>=', 'fields' => ['created']])
->compare('end_date', ['operator' => '<=', 'fields' => ['created']])
->value('designation_id');
// ->like('name', ['fields' => ['name', 'username', 'email', 'phone_no']])

// ->add('search', 'Search.Like', [
// 'before' => true,
// 'after' => true,
// 'fieldMode' => 'OR',
// 'comparison' => 'LIKE',
// 'wildcardAny' => '*',
// 'wildcardOne' => '?',
// 'fields' => ['name', 'email'],
// ]);


$this->belongsTo('Designations', [
'foreignKey' => 'designation_id',
Expand Down Expand Up @@ -163,7 +184,7 @@ public function validationDefault(Validator $validator): Validator
->scalar('username')
->maxLength('username', 255)
->requirePresence('username', 'create')
->notEmptyString('username','Please provide username')
->notEmptyString('username', 'Please provide username')
->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

$validator
Expand All @@ -179,7 +200,7 @@ public function validationDefault(Validator $validator): Validator
'message' => 'Passwords do not match'
])

->notEmptyString('password','Please provide password');
->notEmptyString('password', 'Please provide password');

$validator
->scalar('confirm_password')
Expand All @@ -193,12 +214,12 @@ public function validationDefault(Validator $validator): Validator

$validator
->email('email')
->notEmptyString('email','Please provide email address')
->notEmptyString('email', 'Please provide email address')
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

$validator
->integer('role_id')
->notEmptyString('role_id','Please select a role');
->notEmptyString('role_id', 'Please select a role');

$validator
->scalar('name_of_institution')
Expand Down
6 changes: 4 additions & 2 deletions templates/Admin/Messages/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
<div class="span-12">


<?php
echo $this->Form->create(); ?>
<?php

echo $this->Form->create(null, ['valueSources' => 'query']);
?>
<table class="table table-condensed table-bordered" style="margin-bottom: 2px;">
<thead>
<tr>
Expand Down
Loading

0 comments on commit cb2e33a

Please sign in to comment.