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.
Related
What is the recommended way to get the access level for the current content (article) being rendered for the active request, using the Joomla API.
Currently I've got it hacked using a manual query but this doesn't feel right in that I assume that there should be a way to get the access level using the object model, and not by having to write a custom query.
$db = JFactory::getDBO();
$cid = JRequest::getInt('id', 0); // get the content id from the request
$query = ' SELECT #__content.access FROM #__content WHERE #__content.id="'.$cid.'"';
$db->setQuery($query);
$objList = $db->loadObjectList();
foreach($objList as $obj)
{
return $obj->access; // gets the access level for the first object
}
You can call getItem function defined in model of article in below file:
components\com_content\models\article.php
You need to load the model first and call the getItem function by passing article id.
jimport('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models');
$articleModel = JModelLegacy::getInstance( 'Article', 'ContentModel' );
Call the function:
$result = $articleModel->getItem($articleId);
And use the access level:
$result->access;
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.
Basically as the question describes, I need to get "POST" data in Joomla 2.5/3.xx and I want it through the JInput (the new talk of the town).
Now everything is fine and dandy, until my further requirements needs those fields/data to be dynamic, ie. It(the fields) is designed to change depending on circumstances,there's no way for me to know what the fields are gonna be, I know how to do it in core php, but that's not the case with JInput, so thats it, how do I do it...
Well I know this has been some time since this was asked, but I came across the issue today and found a Joomla solution for POST forms.
$input = JFactory::getApplication()->input;
$fieldname = $input->post->get('fieldname');
This is essentially the same as using $fieldname = $_POST['fieldname']; except you get the added benefit of staying within Joomla's API.
JInput doesn't offer such feature; so you might have to use $_POST.
You could get around it if you can have the input be in the form of array (and use JInput::getArray() ) or a json-encoded object (you use json_decode(JInput::getString()))
The latter is very effective I have used it with success on many projects.
Try this
$post = JFactory::getApplication()->input->post;
Joomla3 offers two functions:
JInputJSON (extends Jinput with the getRaw() method)
JResponseJson (convert and return data as JSON)
The request data:
var jsonString = '{"test":"1"}';
var data = { ajaxrequest : jsonString }
Joomla:
$jinput = JFactory::getApplication()->input;
$json = $jinput->getRaw('ajaxrequest'); // returns {\"test\":\"1\"}
$data = json_decode($json); // json decode, returns data object
// do stuff..
echo new JResponseJson($response);
You can use Jinput for this
$jinput = JFactory::getApplication()->input;
Getting Values from a Specific Super Global
$foo = $jinput->get->get('varname', 'default_value', 'filter');
$foo = $jinput->post->get('varname', 'default_value', 'filter');
$foo = $jinput->server->get('varname', 'default_value', 'filter');
Please refer this document for more details:
https://docs.joomla.org/Retrieving_request_data_using_JInput
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).
Normally I would just use URL GET parameters but CodeIgniter doesn't seem to like them and none of the URL helper functions are designed for them, so I'm trying to do this the 'CodeIgniter way'.
I would like to build a page where the model can accept a number of different URI paramters, none necessarily present, and none having to be in any particular order, much like a regular URL query string with get parameters.
Let's say I have the following url:
http://example.com/site/data/name/joe/
Here not including the controller or the method there would be one parameter:
$params = $this->uri->uri_to_assoc(1);
print_r($params);
// output
array( [name] => [joe] )
If I wanted 'joe' to change to 'ray' I could do this:
echo anchor('name/ray');
Simple enough but what if there are more parameters and the position of the parameters are changing? Like:
http://example.com/site/data/town/losangeles/name/joe/
http://example.com/site/data/age/21/name/joe/town/seattle
Is there a way to just grab the URL and output it with just the 'name' parameter changed?
Edit: As per landons advice I took his script and set it up as a url helper function by creating the file:
application/helpers/MY_url_helper.php
Basically I rewrote the function current_url() to optionally accept an array of parameters that will be substituted into the current URI. If you don't pass the array the function acts as originally designed:
function current_url($vars = NULL)
{
$CI =& get_instance();
if ( ! is_array($vars))
{
return $CI->config->site_url($CI->uri->uri_string());
}
else
{
$start_index = 1;
$params = $CI->uri->uri_to_assoc($start_index);
foreach ($vars as $key => $value)
{
$params[$key] = $value;
}
$new_uri = $CI->uri->assoc_to_uri($params);
return $CI->config->site_url($new_uri);
}
}
It works OK. I think the bottom line is I do not like the 'CodeIgniter Way' and I will be looking at mixing segment based URL's with querystrings or another framework altogether.
You can use the assoc_to_uri() method to get it back to URI format:
<?php
// The segment offset to use for associative data (change me!)
$start_index = 1;
// Parse URI path into associative array
$params = $this->uri->uri_to_assoc($start_index);
// Change the value you want (change me!)
$params['name'] = 'ray';
// Convert back to path format
$new_uri = $this->uri->assoc_to_uri($params);
// Prepend the leading segments back to the URI
for ($i=1; $i<$start_index; $i++)
{
$new_uri = $this->uri->segment($i).'/'.$new_uri;
}
// Output anchor
echo anchor($new_uri);
I'd recommend wrapping this in a helper function of some sort. Happy coding!
Why not use CodeIgniter's built in URI Class? It allows you to select the relevant segments from the URL which you could use to create the anchor. However, unless you created custom routes, it would mean that your methods would need to accept more parameters.
To use the URI Class, you would have the following in your method:
echo anchor($this->uri->segment(3).'/ray');
Assuming /site/data/name are all CodeIgniter specific (/controller/method/parameter)
Now, I think this could be made a lot easier if you were using routes. Your route would look like this:
$route['site/data/name/(:any)'] = 'site/data/$1';
Effictively, your URL can be as detailed and specific as you want it to be, but in your code the function is a lot cleaner and the parameters are quite descriptive. You method would defined like this:
function data($name) { }
To extend your route to accept more parameters, your route for the the example URL "http://example.com/site/data/age/21/name/joe/town/seattle" you supplied would look like this:
$route['site/data/age/(:num)/name/(:any)/town/(:any)'] = 'controller/data/$1/$2/$3';
And your function would look like this:
function data($age, $name, $town) { }