Search plugin для Joomla.


Search plugin расширяет стандартный поиск Joomla, позволяя осуществлять поиск по новым таблицам в базе данных, созданных вашим компонентом.

Структура его проста:
xml файл
php файл

Пример XML-файла

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE install PUBLIC
    "-//Joomla! 1.5//DTD plugin 1.0//EN" "http://www.joomla.org/xml/dtd/1.5/plugin-install.dtd">
        <install version="2.5" type="plugin" group="search">
                <name>Books</name>
                <creationDate>21/06/2012</creationDate>
                <author>Andrew</author>
                <authorEmail>topceo@yandex.ru</authorEmail>
                <authorUrl>My website</authorUrl>
                <copyright>Copyright information</copyright>
                <license>License, for example GNU/GPL</license>
                <version>Version of the plugin 1.0</version>
                <description>This plugin searches books</description>
                <files>
              <filename plugin="books">books.php</filename>
            </files>
        </install>

Пример php-файла. Books нужно заменить на название вашего плагина.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* @version               Booksearch.php 1.0. 21.06.2012
* @copyright            Copyright
* @license              License GNU/GPL
*/
 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
class plgSearchBooks extends JPlugin
{  
 
 
//Define a function to return an array of search areas. 
function onContentSearchAreas()
{
static $areas = array(
        'Books' => 'books'
);
 
return $areas;
}
 
 
function onContentSearch( $text, $phrase='', $ordering='', $areas=null )
{
$db             =& JFactory::getDBO();
$user   =& JFactory::getUser();
//If the array is not correct, return it:
if (is_array( $areas )) {
        if (!array_intersect( $areas, array_keys($this->onContentSearchAreas()))) {
                return array();
        }
}
 
        //Define the parameters. 
        $plugin =& JPluginHelper::getPlugin('search', 'Books');
 
        //Now define the parameters:
        $sContent               = $this->params->get('search_content',            1);
        $sArchived              = $this->params->get('search_archived',           1);
        $limit                  = $this->params->def('search_limit',              50);
 
        //Use the function trim to delete spaces in front of or at the back of the searching terms
        $text = trim( $text );
 
        //Return Array when nothing was filled in.
        if ($text == '') {
                        return array();
                }
 
        $wheres = array();
        switch ($phrase) {
 
   //search exact
                case 'exact':
                        $text           = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
                        $wheres2        = array();
                        $wheres2[]      = 'LOWER(a.title) LIKE '.$text;
                        $where          = '(' . implode( ') OR (', $wheres2 ) . ')';
                        break;
 
   //search all or any
                case 'all':
                case 'any':
 
   //set default
                default:
                        $words  = explode( ' ', $text );
                        $wheres = array();
                        foreach ($words as $word)
                        {
                                $word           = $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
                                $wheres2        = array();
                                $wheres2[]      = 'LOWER(a.title) LIKE '.$word;
                                $wheres2[]      = 'LOWER(a.subtitle) LIKE '.$word;
                                $wheres2[]      = 'LOWER(a.description) LIKE '.$word;
                                $wheres[]       = implode( ' OR ', $wheres2 );
                        }
                        $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
                        break;
        }
 
 //ordering of the results
        switch ( $ordering ) {
 
 //alphabetic, ascending
                case 'alpha':
                        $order = 'a.title ASC';
                        break;
 
 //oldest first
                case 'oldest':
 
 //popular first
                case 'popular':
 
 //newest first
                case 'newest':
 
 //default setting: alphabetic, ascending
                default:
                        $order = 'a.title ASC';
        }
 
        $searchBooksearch = JText::_( 'Books' );
 
//the database query 
        $query = 'SELECT a.title AS title,'
        . ' a.id AS id,'
        . ' a.slug AS slug'
        . ' FROM #__books AS a'
        . ' WHERE ( '. $where .' )'
        . ' ORDER BY '. $order
        ;
 
//Set query
        $db->setQuery( $query, 0, $limit );
        $rows = $db->loadObjectList();
//The 'output' of the displayed link
        foreach($rows as $key => $row) {
                $rows[$key]->href = 'index.php/component/books/view/'.$row->id.'-'.$row->slug;
        }
 
 
//Return the search results in an array
return $rows;
}
}

Другие посты

Категория: Joomla

Оставить комментарий


Яндекс.Метрика