Kamis, 20 Agustus 2020

Joomla Component - Important Scripts

Some important scripts to simplify joomla component development.

1. Refer function from other models.

Calling from a model, referring to function in other model. For example, referring to "Home" model, in component called "CCauth", function called "get_things" with the rest function parameters.

$results = $this->getInstance('Home', 'CCauthModel')-> get_things(array('alias', 'name'), 'ccempl', 'status', 1);

2. Important function:  The function compares a value against array of values. Return the compared ("$data") value if match or return default value if not found. In below sample, default value is empty.
  
public function validateCompare($data="", $validData = array())
{
//This function to compare a string against valid data in arrays.
$data = $this->sanitizeInput($data);
//set default first
$result = "";// set default result to first valid data if not found
//compare, if found override result
foreach ($validData as $testdata){
if($testdata == $data){
$result = $data; //override result
}
}
return $result;
}

3. To get column/array with optional where and sort:
public function getColumn($select, $table, $where="", $ins="", $ordering="", $by="") {
// This function is to get eligible items as arrays.
// Apply restrictions accordingly.
// No need to apply ordering in the query
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName($select)); //change field
$query->from($db->quoteName($table)); // change when change table
// here is the matching criteria:  
if($where!=""){
$query->where($db->quoteName($where) . ' = '. $db->quote($ins));
}
if($ordering!=""){
$query->order($db->quote($ordering.$by));
}
$db->setQuery($query);
$results = $db->loadColumn();
return $results;
}