HOW TO create and mange Search on the website?

Discuss everything related to ApPHP Durecty CMF. All versions. Any questions related to the Directy CMF developing or using should be posted to this forum.

Moderators: alexandrleonenko, alexmst

Site Admin
Site Admin
Posts: 6026
Joined: Jan 7th, '09, 23:18

HOW TO create and mange Search on the website?

Postby administrator » Feb 11th, '19, 15:54

Directy CMF allows an embedded, easy and simple way to use search functionality on the website.
First of all the search functionality is embedded feature, so you don't need to develop it.
Second, it allows to perform search by modules, where it was explicitly defined.

Let say, you want search will work on 2 modules: CMS and NEWS.
So you add to each module main model a method, called "search" with following code:

Code: Select all

public function search($keywords = '', $itemsCount = 10)
   $result = array();
   if($keywords !== ''){
      $limit = !empty($itemsCount) ? '0, '.(int)$itemsCount : '';
      $condition = CConfig::get('db.prefix').$this->_table.'.is_published = 1 AND '.
         CConfig::get('db.prefix').$this->_tableTranslation.'.news_text LIKE :keywords OR '.
         CConfig::get('db.prefix').$this->_tableTranslation.'.news_header LIKE :keywords';
      // Count total items in result
      $total = $this->count(array('condition'=>$condition), array(':keywords'=>'%'.$keywords.'%'));
      // Prepare news result
      $news = $this->findAll(array('condition'=>$condition, 'limit' => $limit), array(':keywords'=>'%'.$keywords.'%'));
      foreach($news as $key => $val){
         $result[0][] = array(
            'date'         => $val['created_at'],
            'title'       => $val['news_header'],
            'intro_image'   => (!empty($val['intro_image']) ? '<img class="intro-thumb news-intro-thumb" src="assets/modules/news/images/intro_images/'.CHtml::encode($val['intro_image']).'" alt="news intro" />' : ''),
            'content'       => $val['news_text'],
            'link'          => Website::prepareLinkByFormat('news', 'news_link_format', $val['id'], $val['news_header'])
      $result[1] = $total;

You may release this method as a logic of your module requires. The important, that this method returns array, that includes following keys: 'date', 'title', 'intro_image', 'content' and 'link'.
The system will know how to display the found data on the Frontend.
cmf-search.png (73.47 KiB) Viewed 122 times

The good news is that following modules already have embedded search functionality and they are: News, Blog and CMS.

Every module, that has implemented search functionality will appear in General / Site Settings / Search Settings, see attached screenshot.

bo-search.png (44.62 KiB) Viewed 118 times

You can change a number of items per page, disable this module and define order priority.

Return to “ApPHP Directy CMF {developers/users}”