Categories > Your tips & tricks >

Example of Joomla application: simple search with more preparations

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Skrol29
Date: 2012-03-02
Time: 02:41

Example of Joomla application: simple search with more preparations

Here is the source code and to install procedure for a small Joomla application that works with the TBS plug-in.

This application present a search area, and the result of the search trough a table in the Joomla database.
But it also:
- checks the TBS version
- allows template which are a full HTML page
- allows one CSS style sheet in the template
- allows additional both GET and POST parameters

Installation:
-------------
1) create a folder "applications/simple_search" under the Joomla folder.
2) put the following files
3) create a new Joomla article with the following contents: {tbs}script=applications/simple_search/index.php{/tbs}
4) in the TBS plug-in parameters (under Joomla plug-ins), add the article id to the hallowed ids.

Files:
------

index.php
<?php

/* Simple Search application
   Skrol29, 2012-03-01
*/

if (!isset($TBS)) exit("This application can work only under Joomla.");

// Check TBS version
if (version_compare($TBS->Version,'3.6.0')<0) {
    $TBS->Source = utf8_encode("This applciation require TinyButStrong Template Engine version 3.6.0 ou higher. The installed version is ".$TBS->Version.".");
    return; // back to Joomla
}

// Retrieve user parameters
global $what;
$what = isset($_REQUEST['what']) ? $_REQUEST['what'] : ''; // text to search

// Build The URL to the current article
global $art_url;
$art_id = $TBS->JoomlaArticle->id;
$art_url = JRoute::_(ContentHelperRoute::getArticleRoute($art_id));
$art_url .= (strpos($art_url,'?')===false) ? '?' : '&'; // ensure compatibility with the Short URL mode of Joomla


// Load the template
$TBS->JoomlaScript = $PrmLst['script'];
f_load_template($TBS, 'tpl_main.html', 'Simple Search');

if ($what=='') {
    // nothing to search
    $TBS->MergeBlock('c', 'clear');
} else {
    $sql = "
    SELECT id, name
    FROM jos_components
    WHERE (name LIKE '%".str_replace("'", "''", $what)."%')
    ORDER BY id
    ";
    $TBS->MergeBlock('c', 'mysql', $sql);
}

/* END : do not use exit() nore TBS->Show() in order to let Joomla continue the process
*/
return; // go back to Joomla

// ------------------------------------------------------
// ------------------------------------------------------

function f_load_template(&$TBS, $modele, $titre='') {

    if ($titre!=='') $TBS->JoomlaArticle->title = utf8_encode($titre);

    $TBS->LoadTemplate(dirname(__FILE__).'/'.$modele);

    // search and delete the <body> element
    $p = strpos($TBS->Source, '<body');
    if ($p!==false) {
        $p = strpos($TBS->Source, '>', $p);
        if ($p!==false) $TBS->Source = substr($TBS->Source, $p+1);
    }
    $p = strpos($TBS->Source, '</body');
    if ($p!==false) $TBS->Source = substr($TBS->Source, 0, $p);
   
    // Add the CSS
    $TBS->Source = '<link rel="stylesheet" href="'.dirname($TBS->JoomlaScript).'/style.css" type="text/css" />'."\r\n".$TBS->Source;
   
}

tpl_main.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="[onshow.art_url;htmlconv=no]" method="post">
  Search for:
    <input type="text" name="what" value="[onshow.what]"/>
    <input type="submit" name="btn_ok" value="OK" />
</form>
<p>Result:</p>
<table border="1" cellspacing="0" cellpadding="4" class="btable">
  <tr>
    <th bgcolor="#999999">Id</th>
    <th bgcolor="#999999">Name</th>
  </tr>
  <tr>
    <td>[c.id;block=tr]</td>
    <td>[c.name]</td>
  </tr>
  <tr class="nodata">
    <td colspan="2">[c;block=tr;nodata]No item found</td>
  </tr>
</table>
</body>
</html>


style.css
.msg-ok {
    color: #060;
}
.msg-err {
    color: #F00;
}
.nodata {
    background-color:#F96;
}

table.btable {
    width:auto;
}
table.btable td, th {
    padding:2px;
}
table.btable a {
    text-decoration:none;
}