From cb2e33a5827efdb9fbac8a30746863329a18f8cb Mon Sep 17 00:00:00 2001 From: itskiprotich Date: Wed, 28 Aug 2024 13:15:06 +0300 Subject: [PATCH] UPD: pagination --- composer.json | 1 + composer.lock | 61 ++++- src/Application.php | 1 + src/Controller/Admin/MessagesController.php | 24 +- src/Controller/Admin/UsersController.php | 17 +- src/Controller/AppController.php | 1 + src/Model/Table/MessagesTable.php | 12 +- src/Model/Table/UsersTable.php | 31 ++- templates/Admin/Messages/index.php | 6 +- templates/Admin/Users/index.php | 242 +++++++++++--------- webroot/js/search/users.js | 28 +++ 11 files changed, 288 insertions(+), 136 deletions(-) create mode 100755 webroot/js/search/users.js diff --git a/composer.json b/composer.json index b8529fa04..f3e593cf1 100755 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/composer.lock b/composer.lock index b9993f485..71bd5a45f 100755 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d9c58a99e14c6edfa78f0e68a9f68c5c", + "content-hash": "75cd46e8540923a78a36c942422e1244", "packages": [ { "name": "brick/math", @@ -1031,6 +1031,65 @@ }, "time": "2022-08-31T13:06:10+00:00" }, + { + "name": "friendsofcake/search", + "version": "6.2.5", + "source": { + "type": "git", + "url": "https://github.com/FriendsOfCake/search.git", + "reference": "d99b5c73948e632b9b0db1a31aa98ae489aa1502" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/FriendsOfCake/search/zipball/d99b5c73948e632b9b0db1a31aa98ae489aa1502", + "reference": "d99b5c73948e632b9b0db1a31aa98ae489aa1502", + "shasum": "" + }, + "require": { + "cakephp/cakephp": "^4.0" + }, + "require-dev": { + "cakephp/bake": "^2.3", + "cakephp/cakephp-codesniffer": "^4.0", + "cakephp/twig-view": "^1.1.1", + "muffin/webservice": "^3.0", + "phpunit/phpunit": "~8.5.0 || ^9.3" + }, + "type": "cakephp-plugin", + "autoload": { + "psr-4": { + "Search\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Christian Winther", + "role": "Author" + }, + { + "name": "ADmad", + "homepage": "https://github.com/admad", + "role": "Contributor" + } + ], + "description": "CakePHP Search plugin using PRG pattern", + "homepage": "https://github.com/FriendsOfCake/search", + "keywords": [ + "cakephp", + "filter", + "prg", + "search" + ], + "support": { + "issues": "https://github.com/FriendsOfCake/search/issues", + "source": "https://github.com/FriendsOfCake/search" + }, + "time": "2023-03-12T12:50:17+00:00" + }, { "name": "josegonzalez/cakephp-upload", "version": "7.0.1", diff --git a/src/Application.php b/src/Application.php index dc19f951d..0045c8832 100755 --- a/src/Application.php +++ b/src/Application.php @@ -76,6 +76,7 @@ public function bootstrap(): void // Load more plugins here $this->addPlugin('Josegonzalez/Upload'); $this->addPlugin('CakePdf'); + $this->addPlugin('Search'); } /** diff --git a/src/Controller/Admin/MessagesController.php b/src/Controller/Admin/MessagesController.php index 840fc2143..dee936cf3 100755 --- a/src/Controller/Admin/MessagesController.php +++ b/src/Controller/Admin/MessagesController.php @@ -1,4 +1,5 @@ '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); } diff --git a/src/Controller/Admin/UsersController.php b/src/Controller/Admin/UsersController.php index 9aa759de8..f343257a3 100755 --- a/src/Controller/Admin/UsersController.php +++ b/src/Controller/Admin/UsersController.php @@ -14,7 +14,11 @@ */ class UsersController extends AppController { - + public function initialize(): void + { + parent::initialize(); + + } public function dashboard() { @@ -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')); } diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index d29eba548..cc0439b1f 100755 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -68,6 +68,7 @@ public function initialize(): void */ //$this->loadComponent('FormProtection'); $this->loadComponent('Paginator'); + $this->loadComponent('Search.Search'); $this->loadComponent('Acl', [ 'className' => 'Acl.Acl' diff --git a/src/Model/Table/MessagesTable.php b/src/Model/Table/MessagesTable.php index 68022ea7d..951e9ab55 100755 --- a/src/Model/Table/MessagesTable.php +++ b/src/Model/Table/MessagesTable.php @@ -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 diff --git a/src/Model/Table/UsersTable.php b/src/Model/Table/UsersTable.php index 519cdb7ac..d997e29b9 100755 --- a/src/Model/Table/UsersTable.php +++ b/src/Model/Table/UsersTable.php @@ -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 ?'), ); /** @@ -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', @@ -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 @@ -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') @@ -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') diff --git a/templates/Admin/Messages/index.php b/templates/Admin/Messages/index.php index 24b249ca3..49ae6c53f 100755 --- a/templates/Admin/Messages/index.php +++ b/templates/Admin/Messages/index.php @@ -6,8 +6,10 @@
- Form->create(); ?> + Form->create(null, ['valueSources' => 'query']); + ?> diff --git a/templates/Admin/Users/index.php b/templates/Admin/Users/index.php index 3b29d2230..88b9e8fb3 100755 --- a/templates/Admin/Users/index.php +++ b/templates/Admin/Users/index.php @@ -1,4 +1,5 @@ Html->script('search/users', array('inline' => false)); $this->assign('Users', 'active'); ?> @@ -36,7 +37,7 @@
Form->create(); + echo $this->Form->create(null, ['valueSources' => 'query']); // 'User', array( // 'url' => array_merge(array('action' => 'admin_index'), $this->params['pass']), // 'class' => 'well', @@ -54,6 +55,7 @@ // 'default' => $this->request->getQuery('pages') ?? $this->request->getQuery('paging')['limit'], 'empty' => true, // Adds an empty option 'options' => [ + 1=>1, 5 => 5, 10 => 10, 20 => 20, @@ -75,8 +77,11 @@ echo $this->Form->control( 'username', array( - 'div' => false, 'id' => 'adminTitleId', 'required' => false, - 'class' => 'span9', 'label' => array('class' => 'required', 'text' => 'Username') + 'div' => false, + 'id' => 'adminTitleId', + 'required' => false, + 'class' => 'span9', + 'label' => array('class' => 'required', 'text' => 'Username') ) ); ?> @@ -86,130 +91,149 @@ echo $this->Form->control( 'email', array( - 'div' => false, 'id' => 'adminSearchId', 'required' => false, - 'type' => 'text', 'class' => 'span9', 'label' => array('class' => 'required', 'text' => 'Email Address') + 'div' => false, + 'id' => 'adminSearchId', + 'required' => false, + 'type' => 'text', + 'class' => 'span9', + 'label' => array('class' => 'required', 'text' => 'Email Address') ) ); ?>
- Form->control( - 'start_date', - array( - 'div' => false, 'type' => 'text', 'class' => 'control-small', 'id' => 'SadrStartDate', 'after' => '-to-', - 'label' => array('class' => 'required', 'text' => 'User Created Dates'), 'placeHolder' => 'Start Date' - ) - ); - echo $this->Form->control( - 'end_date', - array( - 'div' => false, 'type' => 'text', 'class' => 'control-small', 'id' => 'SadrEndDate', - 'after' => ' - clear', - 'label' => false, 'placeHolder' => 'End Date' - ) - ); - ?> +
User Created Dates
+
+ Form->control( + 'start_date', + array( + 'div' => false, + 'type' => 'text', + 'class' => 'control-small unauthorized_index', + 'label' => false, + 'placeHolder' => 'Start Date' + ) + ); + ?> + -to- + Form->control( + 'end_date', + array( + 'div' => false, + 'type' => 'text', + 'class' => 'control-small unauthorized_index', + 'after' => ' + clear!', + 'label' => false, + 'placeHolder' => 'End Date' + ) + ); + ?> +
-
-
-
- Form->button(' Search', array( - 'escapeTitle' => false, - 'class' => 'btn btn-inverse', 'div' => 'control-group', 'div' => false, - )); - ?> -
-
- Html->link(' Add User', array('controller' => 'users', 'action' => 'add'), array( - 'class' => 'btn', 'div' => false, 'escape' => false, - )); - ?> -
-
- +
+
+ Form->button(' Search', array( + 'escapeTitle' => false, + 'class' => 'btn btn-inverse', + 'div' => 'control-group', + 'div' => false, + )); + ?> +
+
+ Html->link(' Add User', array('controller' => 'users', 'action' => 'add'), array( + 'class' => 'btn', + 'div' => false, + 'escape' => false, + )); + ?> +
+
+ + ?> +
-
- Form->end(); ?> + Form->end(); ?> -
+
- 0) { ?> -

- Paginator->counter( - __('Page {{page}} of {{pages}}, + 0) { ?> +

+ Paginator->counter( + __('Page {{page}} of {{pages}}, showing {{current}} Users out of {{count}} total, starting on record {{start}}, ending on {{end}}') - ); + ); - ?> -

- -
- - - - - - - - - - - request->paging['User']['page'] - 1) * $this->request->paging['User']['limit']; - foreach ($users as $user) : ?> +

+ +
#Paginator->sort('Username'); ?>Paginator->sort('email'); ?>Paginator->sort('created', 'Date Created'); ?>
+ - - - - - + + + + + - - -
-

- .

-
' . $user['username'] . ''; ?>    - Html->link( - ' ', - array('controller' => 'users', 'action' => 'edit', $user['id']), - array('escape' => false) - ); ?>  - Form->postLink( - __(' '), - array('action' => 'delete', $user['id']), - array('escape' => false), - __('Are you sure you want to Delete the User %s?', $user['username']) - ); ?>  - #Paginator->sort('Username'); ?>Paginator->sort('email'); ?>Paginator->sort('created', 'Date Created'); ?>
+ + + request->paging['User']['page'] - 1) * $this->request->paging['User']['limit']; + foreach ($users as $user) : + ?> + + +

+ .

+ + ' . $user['username'] . ''; ?>  +   +   + + Html->link( + ' ', + array('controller' => 'users', 'action' => 'edit', $user['id']), + array('escape' => false) + ); ?>  + Form->postLink( + __(' '), + array('action' => 'delete', $user['id']), + array('escape' => false), + __('Are you sure you want to Delete the User %s?', $user['username']) + ); ?>  + + + + + - -

There were no reports that met your search criteria.

- -
- + +

There were no reports that met your search criteria.

+ + + - \ No newline at end of file + \ No newline at end of file diff --git a/webroot/js/search/users.js b/webroot/js/search/users.js new file mode 100755 index 000000000..198a44665 --- /dev/null +++ b/webroot/js/search/users.js @@ -0,0 +1,28 @@ +$(function () { + + $('#start-date').datepicker({ + minDate: "-100Y", + maxDate: "-0D", + dateFormat: 'yy-mm-dd', + showButtonPanel: true, + changeMonth: true, + changeYear: true, + showAnim: 'show', + onSelect: function (selectedDate) { + // Set the minDate of the end date picker based on the selected start date + $('#end-date').datepicker("option", "minDate", selectedDate); + } + }); + + // Initialize end date picker + $('#end-date').datepicker({ + minDate: "-100Y", // This will be dynamically updated + maxDate: "-0D", + dateFormat: 'yy-mm-dd', + showButtonPanel: true, + changeMonth: true, + changeYear: true, + showAnim: 'show' + }); + +}); \ No newline at end of file