Mambo CMS : search untuk section tertentu

Secara default Mambo akan mencari semua content, category dan section ( yg di publish dan for public tentunya) di dalam fungsi search-nya. Fungsi search sendiri dilakukan oleh mambot yg di invoke oleh komponen search. Sekarang bagaimana kita merubah/memodifikasi fungsi search agar mencari content, category dan section di section-section tertentu saja yang bisa kita define melalui backend. Mengapa kita mau membatasi pencarian hanya di section-section tertentu? alasan-nya bisa macem2, tapi saya gak akan membahas alasan2 tersebut :P

Seperti yg dijelaskan, fungsi search di Mambo sebenernya dijalankan oleh mambot dalam hal ini content.searchbot.php, categories.searchbot.php dan sections.searchbot.php. File2 tersebut berada di lokasi mambots/search. Yg pertama kita lakukan adalah menambahkan parameter di masing2 mambot tersebut. Buka dan edit file2 xml masing2 content.searchbot.xml, categories.searchbot.xml dan sections.searchbot.xml, tambahkan parameter berikut :

XML:
  1. <params>
  2.     <param name="section" type="list" default="0" label="Search section" description="search all sections or selected only">
  3.         <option value="0">All section</option>
  4.         <option value="1">Selected sections only</option>
  5.     </param>
  6.     <param name="sectionid" type="text" size="20" label="Section ID" description="Enter section ID separated by comma i.e: 1,2,3" />
  7. </params>

sehingga keseluruhan file tampak seperti:

XML:
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <mosinstall version="4.5.2" type="mambot" group="search">
  3.     <name>content searchbot</name>
  4.     <author>Mambo Project</author>
  5.     <creationDate>August 2004</creationDate>
  6.     <copyright>(C) 2000 - 2005 Miro International Pty Ltd</copyright>
  7.     <license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license>
  8.     <authorEmail>admin@mamboserver.com</authorEmail>
  9.     <authorUrl>www.mamboserver.com</authorUrl>
  10.     <version>4.5.2</version>
  11.     <description>Allows Searching of all Content items</description>
  12.     <files>
  13.         <filename mambot="content.searchbot">content.searchbot.php</filename>
  14.     </files>
  15.     <params>
  16.         <param name="section" type="list" default="0" label="Search section" description="search all sections or selected only">
  17.             <option value="0">All section</option>
  18.             <option value="1">Selected sections only</option>
  19.         </param>
  20.         <param name="sectionid" type="text" size="20" label="Section ID" description="Enter section ID separated by comma i.e: 1,2,3" />
  21.     </params>
  22. </mosinstall>

lakukan hal serupa pada file categories.searchbot.xml dan sections.searchbot.xml dengan menambahkan parameter tersebut. Parameter tersebut berguna untuk mendapatkan variabel:

  • fitur search in section, apakah semua section atau section tertentu saja
  • section ID, mendapatkan informasi section ID mana saja yg akan di include kan dalam search

Setelah itu, buka file content.searchbot.php. Di dalam fungsi botSearchContent, pas dibawah define global tambahkan script berikut:

PHP:
  1. //modified to search content in these section only.
  2. //you can find the section id from the backend> section manager
  3. $query = "SELECT id FROM #__mambots WHERE element = 'content.searchbot' AND folder = 'search'";
  4. $database->setQuery( $query );
  5. $id = $database->loadResult();
  6. $mambot = new mosMambot( $database );
  7. $mambot->load( $id );
  8. $params =& new mosParameters( $mambot->params );
  9. $section     = $params->def( 'section', 0 );
  10. $sectionid = $params->def( 'sectionid', array() );
  11. $sectionid = explode(',',$sectionid);
  12. if ( $section != 0 AND is_array($sectionid) ) {
  13.     $sectionsql = '';
  14.     foreach ($sectionid as $val) {
  15.         $sectionsql .= $val.',';
  16.     }
  17.     $sectionsql = ' AND a.sectionid IN ('. substr($sectionsql,0,-1) .')';
  18. }

Inti dari kode diatas pertama mendapatkan variabel dari parameter yg diset dari backend, kemudian membuat string query yg akan mencari content hanya di section2 ID yg sudah diset. Kemudian di file tersebut terdapat 3 query yg masing2 kita juga harus menambahkan variabel $sectionsql sehingga masing2 menjadi:

PHP:
  1. $sql = "SELECT a.title AS title,"
  2.     . "\n a.created AS created,"
  3.     . "\n CONCAT(a.introtext, a.fulltext) AS text,"
  4.     . "\n CONCAT_WS( '/', u.title, b.title ) AS section,";
  5.     $sql .= "\n CONCAT( 'index.php?option=com_content&task=view&id=', a.id ) AS href,";
  6.     $sql .= "\n '2' AS browsernav"
  7.     . "\n FROM #__content AS a"
  8.     . "\n INNER JOIN #__categories AS b ON b.id=a.catid AND b.access <= '$my->gid'"
  9.     . "\n LEFT JOIN #__sections AS u ON u.id = a.sectionid"
  10.     . "\n WHERE ( $where )"
  11.     . "\n AND a.state = '1'"
  12.     . "\n AND a.access <= '$my->gid'"
  13.     . "\n AND u.published = '1'"
  14.     . "\n AND b.published = '1'"
  15.     . "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now' )"
  16.     . "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down>= '$now' )"
  17.     . $sectionsql
  18.     . "\n ORDER BY $order";

PHP:
  1. // search typed content
  2.     $database->setQuery( "SELECT a.title AS title, a.created AS created,"
  3.     . "\n a.introtext AS text,"
  4.     . "\n CONCAT( 'index.php?option=com_content&task=view&id=', a.id, '&Itemid=', m.id ) AS href,"
  5.     . "\n '2' as browsernav, 'Menu' AS section"
  6.     . "\n FROM #__content AS a"
  7.     . "\n LEFT JOIN #__menu AS m ON m.componentid = a.id"
  8.     . "\n WHERE ($where)"
  9.     . "\n AND a.state='1' AND a.access<='$my->gid' AND m.type='content_typed'"
  10.     . "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now' )"
  11.     . "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down>= '$now' )"
  12.     . $sectionsql
  13.     . "\n ORDER BY " . ($morder ? $morder : $order)
  14.     );

PHP:
  1. // search archived content
  2.     $database->setQuery( "SELECT a.title AS title,"
  3.     . "\n a.created AS created,"
  4.     . "\n a.introtext AS text,"
  5.     . "\n CONCAT_WS( '/', 'Archived ', u.title, b.title ) AS section,"
  6.     . "\n CONCAT('index.php?option=com_content&task=view&id=',a.id) AS href,"
  7.     . "\n '2' AS browsernav"
  8.     . "\n FROM #__content AS a"
  9.     . "\n INNER JOIN #__categories AS b ON b.id=a.catid AND b.access <='$my->gid'"
  10.     . "\n LEFT JOIN #__sections AS u ON u.id = a.sectionid"
  11.     . "\n WHERE ( $where )"
  12.     . "\n AND a.state = '-1' AND a.access <= '$my->gid'"
  13.     . "\n AND ( publish_up = '0000-00-00 00:00:00' OR publish_up <= '$now' )"
  14.     . "\n AND ( publish_down = '0000-00-00 00:00:00' OR publish_down>= '$now' )"
  15.     . $sectionsql
  16.     . "\n ORDER BY $order"
  17.     );

lakukan juga metoda yg sama utk categories.searchbot.php dan sections.searchbot.php yaitu menambahkan variable $sectionsql di dalam query string. Sedangkan kode utk mengambil parameter berbeda dengan content.searchbot.php.

ini penambahan kode utk file categories.searchbot.php di dalam fungsi botSearchCategories pas dibawah define global

PHP:
  1. //modified  to search categories in these section only.
  2. //you can find the section id from the backend> section manager
  3. $query = "SELECT id FROM #__mambots WHERE element = 'categories.searchbot' AND folder = 'search'";
  4. $database->setQuery( $query );
  5. $id = $database->loadResult();
  6. $mambot = new mosMambot( $database );
  7. $mambot->load( $id );
  8. $params =& new mosParameters( $mambot->params );
  9. $section     = $params->def( 'section', 0 );
  10. $sectionid = $params->def( 'sectionid', array() );
  11. $sectionid = explode(',',$sectionid);
  12. if ( $section != 0 AND is_array($sectionid) ) {
  13.     $sectionsql = '';
  14.     foreach ($sectionid as $val) {
  15.         $sectionsql .= $val.',';
  16.     }
  17.     $sectionsql = ' AND a.section IN ('. substr($sectionsql,0,-1) .')';
  18. }

ini penambahan kode utk file sections.searchbot.php di dalam fungsi botSearchSections pas dibawah define global

PHP:
  1. //modified to search selected section only
  2. //you can find the section id from the backend> section manager
  3. $query = "SELECT id FROM #__mambots WHERE element = 'sections.searchbot' AND folder = 'search'";
  4. $database->setQuery( $query );
  5. $id = $database->loadResult();
  6. $mambot = new mosMambot( $database );
  7. $mambot->load( $id );
  8. $params =& new mosParameters( $mambot->params );
  9. $section     = $params->def( 'section', 0 );
  10. $sectionid = $params->def( 'sectionid', array() );
  11. $sectionid = explode(',',$sectionid);
  12. if ( $section != 0 AND is_array($sectionid) ) {
  13.     $sectioninc = array(2,3,4,5,6,7,8);
  14.     $sectionsql = '';
  15.     foreach ($sectionid as $val) {
  16.         $sectionsql .= $val.',';
  17.     }
  18.     $sectionsql = ' AND a.id IN ('. substr($sectionsql,0,-1) .')';
  19. }

that's all !. kalo gak mau repot2 ini saya attachkan file content.searchbot.php, categories.searchbot.php, sections.searchbot.php beserta xml nya.

happy mambo!

Catatan: Contoh dan file2 diatas2 berdasarkan Mambo 4.5.4, jadi kemungkinan ada sedikit perbedaan utk versi mambo yg lain.

3 Responses to “Mambo CMS : search untuk section tertentu”


  1. 1 dee

    this is cool man! … lain kali bahas dong ttg css hack yang sering dipake di mambo …thanks

  2. 2 Suwahadi

    Wow… greats
    Salam kenal ya mas…
    Aku pengguna setia Mambo nich :)

  3. 3 khuclukz

    lam kenal mas! aku new mamboners nech! jadi gak tau sama sekali hii..

Leave a Reply




It seems you're using an unsafe, out-of-date browser. Click here to upgrade to Firefox for free. X