info@ektanjali.com +91 99276-99286

CakePHP 2.x User Management Basic Plugin Version 1.7.x

Ajax Search

I have developed a search element for filtering the results in this plugin. This search option works with or with out Ajax.
Auto suggestions on search fields works with this search form.

How to use Search Options for filtering the results in Cake Php
1. Include Search helper in controller's Helper variable for e.g.

public $helpers = array('Usermgmt.Search');
2. Include Search component in controller's Component variable for e.g.

public $components = array('Usermgmt.Search');
3. set search fields variable in your controller for e.g. in UsersController.php

var $searchFields = array
        (
            'index' => array(
                'User' => array(
                    'User'=> array(
                        'type' => 'text',
                        'label' => 'Search',
                        'tagline' => 'Search by name, username, email',
                        'condition' => 'multiple',
                        'searchBreak' => false,
                        'searchFields'=>array('User.first_name', 'User.last_name', 'User.username', 'User.email'),
                        'searchFunc'=>array('plugin'=>'usermgmt', 'controller'=>'Users', 'function'=>'indexSearch'),
                        'inputOptions'=>array('style'=>'width:200px;')
                    ),
                    'User.id'=> array(
                        'type' => 'text',
                        'condition' => '=',
                        'label' => 'User Id',
                        'inputOptions'=>array('style'=>'width:50px;')
                    ),
                    'User.user_group_id' => array(
                        'type' => 'select',
                        'condition' => 'comma',
                        'label' => 'Group',
                        'model' => 'UserGroup',
                        'selector' => 'getGroups'
                    ),
                    'User.email_verified' => array(
                        'type' => 'select',
                        'label' => 'Email Verified',
                        'options' => array(''=>'Select', '0'=>'No', '1'=>'Yes')
                    ),
                    'User.active' => array(
                        'type' => 'select',
                        'label' => 'Status',
                        'options' => array(''=>'Select', '1'=>'Active', '0'=>'Inactive')
                    )
                )
            )
        );

where
'index' is a function on which you want to set Search FIlter.
'User' is a model name for which you are going to display results.
'User.id' is a field name of User model for displaying a option to search on id. here you can use other model fox ex belongsTo, HasOne etc. For more information see var $searchFields in UsersController.php, look code for online function here I used other model 'UserActivity.status'
'type' is search field type for e.g. 'text', 'select', 'checkbox'.
'label' is search form 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'.
'searchBreak' (optional, used only with 'condition' => 'multiple') is true or false.
'searchFields' (used only with 'condition' => 'multiple') search fields name with model name.

'multiple' condition is a bit complex, let me explain it in detail-
It is used for searching same query text in multiple columns. for e.g. in above code we will search query text in 'first_name', 'last_name', 'username', 'email' columns. Please note- We can take advantage of 'searchBreak' option for e.g. if we are searching text 'chetan varshney' then by default it will search whole text in all given columns. and if we set 'searchBreak' true then it breaks (space break) text to multiple parts 'chetan', 'varshney' then will search all parts in all given columns.

'searchFunc' (optional, used only with 'condition' => 'multiple') This is used for search suggestion on search input field. For use this option you have to pass plugin, controller, and function name. Plugin name can be null. Don't forget to define function in given controller.
'selector (used only with 'type' => 'select')' is function name in given model for pop up the select box values.
'model' (used only with 'type' => 'select') is model name in which selector function is defined.
'options' (used only with 'type' => 'select') is options array for pop up the select box values. This can be used in the place of 'selector' and 'model'.
'inputOptions' additional information for input field.


4. Now your View file code is conditional code for reducing the duplicate code. I mean View code is divided in two parts one for ajax updation and other for normal request. Confused ?? I know it is little bit complex don't worry I am going to explain in easy way.
Just open the index file, location is yourapp/app/Plugin/Usermgmt/View/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'); ?>

5. Now find the element file all_users.ctp in Elements directory

Now find the div with id "updateUserIndex" got it?? 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('User', array('legend' => 'Search'"updateDivId" => "updateUserIndex")); ?>

where
Search is a helper for creating search form, location is yourapp/app/Plugin/Usermgmt/View/Helper/SearchHelper.php.
'User' is a model name. The same you pass in form creation.
'updateDivId' is id of main div. The contents of this div will update on every ajax call. I have explained this in step 5 above. In index.ctp I have set this id "updateUserIndex" you can change this according your wish.

For more information see
Users Controller yourapp/app/Plugin/Usermgmt/Controller/UsersController.php
Search Component yourapp/app/Plugin/Usermgmt/Controller/Component/SearchComponent.php
Search Behavior yourapp/app/Plugin/Usermgmt/Model/Behavior/SearchingBehavior.php
Index View yourapp/app/Plugin/Usermgmt/View/Users/index.ctp
Index View yourapp/app/Plugin/Usermgmt/View/Elements/all_users.ctp
Search Helper yourapp/app/Plugin/Usermgmt/View/Helper/SearchHelper.ctp
Search Form Element yourapp/app/Plugin/Usermgmt/View/Elements/search_form.ctp