How to Get Global Article Parameters in Joomla? - joomla

I'm programming a module in Joomla! 2.5. The module displays an article at a given position.
I need article attributes (i.e. show_title, link_title ecc.)
With this code I get article's specific attributes:
$db =& JFactory::getDBO();
$query = 'SELECT * FROM #__content WHERE id='.$id.' AND state=1';
$db->setQuery($query);
$item = $db->loadObject();
$attribs = json_decode($item->attribs, true);
If i var_dump the $attribs variable I get:
array(26) {
["show_title"]=>
string(0) ""
["link_titles"]=>
string(0) ""
[...]
}
The variable $attribs represents the article's specific attributes. When an element is set to "" means "use the global configuration".
I can get the global configuration with this query:
SELECT params from #__extensions where extension_id=22;
Where 22 is the id of the com_component extension. Then I can merge the results here with the results for the specific article.
BUT is there an easy way to achieve this? Does Joomla! have a specific class in the framework for this?

I would start with loading the model for articles:
// Get an instance of the generic articles model
$model = JModel::getInstance('Article',
'ContentModel',
array('ignore_request' => true));
The get the specific article...
$model->getItem($id)
To get a components global params I believe you can use:
$params = &JComponentHelper::getParams( 'COMPONENT_NAME' );
In your case you would need something like:
jimport('joomla.application.component.helper'); // load component helper first
$params = JComponentHelper::getParams('com_content');
I would suggest you look at the code of the article modules that ship with Joomla! 2.5.x as they do a lot of similar things to what you're trying to create. You can also have a read of this article, it's a bit dated but I think it still mostly holds true (except for jparams being replaced by jforms).

Related

How can I get Joomla component parameter values?

===
UPDATE:
I think now I am literally just trying to get a database value into my component php files, but again, there seems to be very little documentation that can give an example of a function that will return this info like there is in Wordpress.
So I have a table called membersarea_countries that will have records of differnt countries I want to store values for.
I've read about JTable and other things, but how can I simply just bring back the records from this table?
$row = JTable::getInstance('membersarea_countries', 'Table', array());
But this returns a boolean of 0.
I'd really appreciate some help if anyone can.
===
I've been following what several online guides explain, which are all pretty much the same thing, but I never seem to return the values that I'm expecting.
In Components > Members Area (my component), I have a table set up to allow me to enter a record for each country, and then store a uniqueRef, signature, and URL within that record. (for GeoIP purposes).
I've created the first record, however when I try to use the following code, which the tutorials suggest, I don't see any of my fields within this:
$app = JFactory::getApplication();
$params = $app->getParams();
$uniqueRef = $params->get('uniquereference');
$signature = $params->get('signature');
This is all I see in NetBeans:
There's nothing about $app, and no sign of the fields I've got in the Joomla backend.
I don't understand what's happening, or exactly what I should be doing here. Wordpress uses a simple get_option function. Can anyone try and help me?
Below is the link to the detailed document about JTable -
https://docs.joomla.org/Using_the_JTable_class
Firstly you need to create JTable instance using below code and also change table file name to membersareacountries.php
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_membersarea/tables');
$row = JTable::getInstance('Membersareacountries', 'Table', array());
JTable Class in this file /administrator/components/com_membersarea/tables/membersareacountries.php-
<?php
defined('_JEXEC') or die();
class TableMembersareacountries extends JTable
{
public function __construct($db)
{
parent::__construct( '#__membersarea_countrie', 'id', $db );
}
}
Then you can use load method to get any records. This accepts primary key value of that table -
$id = 1;//change id as per your record
$row->load($id);
//read data
echo $row->id;
echo $row->title;

What are state variables in joomla

What is the usage of state variables in Joomla? and what is the usage of $model->setState in this code (the code is from Joomla's mod_articles_popular module)?
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
// Set application parameters in model
$app = JFactory::getApplication();
$appParams = $app->getParams();
$model->setState('params', $appParams);
// Set the filters based on the module params
$model->setState('list.start', 0);
setState method to a model used to filter data , you can read the details on here. and function syntax and param list can be here.
the main purpose of this method is to filter the result set using parameters. in your case its module param an example post can be found here.
Hope it clear now.

Joomla extension development: How to get global params when retrieiving articles from the database?

In my extension, I've retrieved articles from the content table like this:
.......
.......
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, catid, title, introtext, attribs);
$query->from('#__content');
$query->where('catid="'.$cid.'"');
$query->where('state="1"');
.......
.......
Here I can retrieve attribs data for each article being retrieved.
Is there an easy way to retrieve article params from the global settings (is there some sort of static function in Joomla?) or do I need to manually retrieve the params from the extensions table?
You might want to use the JComponentHelper class to get the params of the component.
<?php
jimport( 'joomla.application.component.helper' );
$com_content_params = JComponentHelper::getParams('com_content');
To get the articles I wouldn't write the query myself as you do, but use the relevant JModel (JModelLegacy in Joomla 3) instance instead.
Something that would look like:
<?php
$model = JModel::getInstance('Articles', 'ContentModel');
$model->setState('filter.category_id', (int)$cid);
$articles = $model->getList();
Don't know if that specific code will work, but you certainly can do some research on Google about that. The spirit is there: use the classes provided by Joomla instead of pulling stuff directly from the DB. You'll gain in maintainability and code quality.

Displaying in back end component the parameters of module(s) and saving them through component

Hi i kinda found the way to display the modules in the component but i am wondering how could i save the parameters through component i mean editing the values in component and saving it.
The modules names and paramaters are known in advance. So the calling will be like this
jimport( 'joomla.application.module.helper' );
$module = &JModuleHelper::getModule( "ModuleName");
$params = new JParameter($module->params);
The purpose of doing so is to ease editing certain values for the customer so it is a pain for a newbie to browse all that joomla stuff(in my case).
All in all cant figure out, how to save the params of a module(s)
Hi this is the code to save the params of a component, module or plugin in Joomla.
It first loads the current params, makes its changes, then saves again; ensure you always load the current params first.
$mparams = JComponentHelper::getParams( 'com_littlehelper' );
$params = $mparams->get('params');
$mparams->set('params.favicons_sourcepath','icons');
$this->saveParams($mparams, $this->componentName);
private function saveParams($params, $extensionName, $type='component') {
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->update('#__extensions AS a');
$query->set('a.params = ' . $db->quote((string)$params));
$query->where(sprintf('a.element = %s AND a.%s = %s',
$db->quote($extensionName),
$db->quoteName('type'),
$db->quote($type)
));
$db->setQuery($query);
return $db->execute();
}
This code comes from my extension LittleHelper published on the JED.

joomla override model for mod_banners

I'm using Joomla 3.1 and I'm using template hacks to override mod_banners -
/mytemplate/html/mod_banners/default.php
Which is working fine.
However, the banners module calls the file:
/components/com_banners/models/banners.php
Which I can't seem to override. I've tried moving the file (and folders) into my /mytemplate/html folder, but that doesn't work.
I've also tried putting the following code into my banners default.php file:
JModelLegacy::addIncludePath(JPATH_ROOT.'/templates/home/com_banners/models/', 'BannersModel');
$model = JModelLegacy::getInstance('Banners', 'BannersModel', array('ignore_request' => true));
$banners = $model->getItems();
But that doesn't work either. Is there any way I can override the query in /com_banners/models/banners.php without changing the core files?
All I'm trying to do is to pull in the descriptions for each banner, without changing the core.
Thanks in advance!
The only way to override a model in Joomla is to make your own version of the original, and load (register) it through a system plugin, before the model is accessed for the first time. For your use case, that is way too complicated.
Even if it is not good practice, since it breaks up the MVC structure, I'd fetch the data from within the template.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id, name, description')
->from('#__banners');
$db->setQuery($query);
$banners = $db->loadObjectList();
Now you can access all banner descriptions, fx. in a loop:
foreach ($banners as $banner) {
echo $banner->id, ': ', $banner->description;
}

Resources