dioscouri / library

5 stars 3 forks source link

Using Joomlas core com_config is very easy and useful we should move away from our config tables #30

Open ChrisFrench opened 11 years ago

ChrisFrench commented 11 years ago

if add a config.xml to your admin folder and than you edit the defines.php and have the following it works very well.

/* * Retrieves the data, using a cached set if possible * @return array Array of objects containing the data from the database */ public function getData() { $cache = JFactory::getCache('com_locator.defines'); $cache->setCaching(true); $cache->setLifeTime('86400'); $data = $cache->call( array($this, 'loadData') ); return $data; }

/**
 * Loads the data from the database
 */
public function loadData()
{  
   // TODO Loading the component with the helper doesn't work because everything is protected so we can't loop it
   // also this is useful because we now now the component ID, and the option, and enabled status of a component
   // $data = JComponentHelper::getParams('com_locator');
   // 
    $option = 'com_locator';
    $db     = JFactory::getDbo();
    $query  = $db->getQuery(true);
    $query->select('extension_id AS "id", element AS "option", params, enabled');
    $query->from('#__extensions');
    $query->where('`type` = '.$db->quote('component'));
    $query->where('`element` = '.$db->quote($option));
    $db->setQuery($query);

    $result = $db->loadObject();

    $data = json_decode($result->params);
    $data->id = $result->id;
    $data->option = $result->option;
    $data->enabled = $result->enabled;
    return $data;
}

/**
 * Set Variables
 *
 * @acces   public
 * @return  object
 */
public function setVariables() 
{
    $success = false;

    $data = $this->getData();

    if ( !empty($data) )
    {

        foreach($data as $title => $value) {
            if (!empty($title)) {
                $this->$title = $value;
            }
        }

        $success = true;
    }

    return $success;
}