CakePHP 3.x User Management Premium Plugin Version 3.0.x
How to use normal search or ajax search in plugin and outside plugin
Features
- Ajax Search is optional we can perform normal search
- Search by field values with like, startswith, endswith, =, <, >, <=, >= conditions
- Ajax suggestions on search inputs
- search inputs types are text, select box, date picker input, checkbox
- Search clear button
1. Include Search component in controller's Component variable for e.g.
public $components = ['Usermgmt.Search'];
2. set search fields variable in your controller for e.g. in UsersController.php
public $searchFields = [
'index'=>[
'Usermgmt.Users'=>[
'Users'=>[
'type'=>'text',
'label'=>'Search',
'tagline'=>'Search by name, username, email',
'condition'=>'multiple',
'searchBreak'=>true,
'searchFields'=>['Users.first_name', 'Users.last_name', 'Users.username', 'Users.email'],
'searchFunc'=>['plugin'=>'Usermgmt', 'controller'=>'Users', 'function'=>'indexSearch'],
'inputOptions'=>['style'=>'width:200px;']
],
'Users.id'=>[
'type'=>'text',
'condition'=>'=',
'label'=>'User Id',
'inputOptions'=>['style'=>'width:50px;']
],
'Users.user_group_id'=>[
'type'=>'select',
'condition'=>'comma',
'label'=>'Group',
'model'=>'Usermgmt.UserGroups',
'selector'=>'getUserGroups'
],
'Users.email_verified'=>[
'type'=>'select',
'label'=>'Email Verified',
'options'=>[''=>'Select', '0'=>'No', '1'=>'Yes']
],
'Users.active'=>[
'type'=>'select',
'label'=>'Status',
'default'=>1,
'options'=>[''=>'Select', '1'=>'Active', '0'=>'Inactive']
],
'Users.created1'=>[
'type'=>'text',
'condition'=>'>=',
'label'=>'From',
'searchField'=>'created',
'inputOptions'=>['style'=>'width:100px;', 'class'=>'datepicker']
],
'Users.created2'=>[
'type'=>'text',
'condition'=>'<=',
'label'=>'To',
'searchField'=>'created',
'inputOptions'=>['style'=>'width:100px;', 'class'=>'datepicker']
]
]
]
];
options
'index' is a function on which you want to set Search FIlter.
'Usermgmt.Users' is model table name, Usermgmt means this model table belongs to usermgmt plugin
'Users.id', 'Users.user_group_id' etc are database table field to make them searchable. You can use associate model fields as well. Please note: we cannot search in hasMany relations.
'searchField' (optional) You can pass this option if you want to search in other fields rather than above fields. for more information look 'Users.created1', 'Users.created2' fields.
'default' (optional) you can set default value of any search filed.
'type' is search field type for e.g. 'text', 'select', 'checkbox'.
'label' is search field label.
'tagline' (optional) is search tag line to be displayed below search input.
'condition' is search field condition which will use in database query for e.g. '=', '<', '>', '<=', '>=', 'like', 'startswith', 'endswith', 'comma', 'multiple'. default is 'like'.
'searchFields' (used only with 'condition'=>'multiple') search fields name with model name.
'searchBreak' (optional, default is false, used only with 'condition'=>'multiple') is true or false. For e.g. you search for "Chetan Varshney" If searchBreak is true then "Chetan" and "Varshney" will be searched in DB with OR conditions in specified searchFields otherwise "Chetan Varshney" whole word will be searched in DB in specified searchFields
'labelImage' (optional) you can add image in search form before any search input, image path for e.g. 'img/icon.png'
'labelImageTitle' (optional) you can add title on label image in search form
'adminOnly' (optional) any search input can be set only for admin.
'options' (optional, used only with 'type'=>'select') you can pass select box options here. for more information look 'Users.active'.
'model' (optional, used only with 'type'=>'select') you can populate select search fields with the result from any model. Required 'selector' option to use it.
'selector' (optional, used only with 'type'=>'select') you can populate select search fields with the result from any model. Required 'model' option to use it.
'selectorArguments' (optional, used only with 'type'=>'select') you can pass arguments in callable model function.
'inputOptions' (optional) you can set search input css style options for any field.
'searchFunc' (optional only with 'type'=>'text') you can pass custom search suggestions url options. generally we use it with 'condition'=>'multiple'.
3. Now in any function you should get results from database in following way e.g. in index function$this->paginate = ['limit'=>10, 'order'=>['Users.id'=>'DESC']];
$this->Search->applySearch();
$users = $this->paginate($this->Users)->toArray();
$this->set(compact('users'));
if($this->request->is('ajax')) {
$this->layout = 'ajax';
$this->render('/Element/all_users');
}
Please Note: $this->Search->applySearch(); is required to add before fetching result from database. If you add conditions in your query then same conditions should be passed in this applySearch function. for e.g.
$cond = [];
$cond['UserEmailSignatures.user_id'] = $this->UserAuth->getUserId();
$this->paginate = ['limit'=>10, 'conditions'=>$cond, 'order'=>['UserEmailSignatures.id'=>'DESC']];
$this->Search->applySearch($cond);
$userEmailSignatures = $this->paginate($this->UserEmailSignatures)->toArray();
for more information check yourapp/plugins/Usermgmt/src/Controller/UserEmailSignaturesController.php
4. Now in template ctp file code is conditional code for reducing the duplicate code. i.e. View code is divided in two parts one for ajax updation and other for normal request.
Just open the index file, location is yourapp/plugins/Usermgmt/src/Template/Users/index.ctp
This code will never execute on ajax calls because there is no need.
I have added element call in this file
<?php echo $this->element('Usermgmt.all_users'); ?>
4. Now find the element file all_users.ctp in yourapp/plugins/Usermgmt/src/Template/Element/
Now find the div with id "updateUsersIndex" The section inside this div will update on every ajax call.
Now in element file you should add search form using Search Helper in following way for e.g. in all_users.ctp before the results table
<?php echo $this->Search->searchForm('Users', ['legend'=>false, 'updateDivId'=>'updateUsersIndex']); ?>
where
'Usermgmt.paginator' is a paginator element location is yourapp/plugins/Usermgmt/src/Template/Element/paginator.ctp.
'ajax' is true/false, set true for ajax search filter and set false for normal search filter
'updateDivId' is reference id to tell the Jquery code that which section will be updated on ajax call.
For more information see
Users Controller yourapp/plugins/Usermgmt/src/Controller/UsersController.php
Search Component yourapp/plugins/Usermgmt/src/Controller/Component/SearchComponent.php
Search Behavior yourapp/plugins/Usermgmt/src/Model/Behavior/SearchingBehavior.php
Index View yourapp/plugins/Usermgmt/src/Template/Users/index.ctp
Index View yourapp/plugins/Usermgmt/src/Template/Element/all_users.ctp
Search Helper yourapp/plugins/Usermgmt/src/View/Helper/SearchHelper.ctp
Search Form Element yourapp/plugins/Usermgmt/src/Template/Elements/search_form.ctp