Request Variables in Joomla - joomla

I am new to Joomla. I am trying to write a module which would fetch and display the contents from database.
Now for fetching the contents from database, I need to get the current article id and category id of the current article.
I am able to get the id with article title append using JRequest::getVar('id');
ex : 16:abc-def-article-title.
I can sub-string the id out of it but I think there would be a better place of achieving it ?
And also I need to get the category id as well.
Also please let me know how can I see what all variables does JRequest::getVar(') holds.
Many Thanks !!

First, try extract article id by method getInt():
JRequest::getInt('id', 0);
It's more safely, then getVar().
Then, to get category id, you can use this code in module file:
jimport('joomla.application.component.model');
$articlesModel = JModel::getInstance('ContentModelArticle');
$articleId = JRequest::getInt('id', 0);
$article = $articlesModel->getItem($articleId);
$categoryId = $article->catid;
Using Joomla! built-in models instead direct SQL queries is best way to understanding Joomla architecture and writing safety applications that will be easily maintained in the future.
Note: code samples are actually only for Joomla 2.5 (probably and for 1.6, 1.7, but i don't shure).

Related

How do I call the core content component in my custom component in joomla 2.5?

My custom component has to call the core content component and create an entry in the #__content table with proper values. When I add a new article in the article manger I have to give only a title as mandatory field value and other values will be created automatically. I want to pretend this in my custom component.
Instead of updating the database table directly load the ContentModelArticle from com_content and use the model to create new articles. This will generate the alias for you, do all of ACL etc based on the View Access and let any system or content plugins have a look at the article and do whatever they do.
ContentModelArticle extends from JModelAdmin so you can use it's ->save($data) method. Where $data is the array of content to bind to the new ContentModelArticle in the save() method.
So, a basic example would be :
// Create our data with some sample code
$data['title'] = 'My Title';
$data['catid'] = 10; /* NB. this is just an example category Id obviously you'll need to provide a suitable catid for your site */
$data['articletext'] = 'Starting text...'; /* From memory you can't have an empty article.*/
// Get the Article model
$articleModel = $this->getModel('Article', 'ContentModel', array());
// Then save it:
$articleModel->save($data);
Obviously you can populate as many fields as you want, including state, publish_up, publish_down, create_by and lots of others — just look at the structure of the content table in the database #__content to see the various columns and their type.
NB: This is untested code just typed into the browser. It's also been a while since I did any 2.x stuff.
Finally, as this question is about Joomla specific implementation details, you may get a better result if you, try asking on the Joomla Q&A StackExhange site

Extending Joomla 2.5 Banner Component

I really hope someone can help me.
I need to be able to serve banners in categories which are dependant on a session variable - and can't find a component which does that. So I'd like to extend the Joomla Banner component in order to select banners based on a session variable which contains the category path.
The correct session variable is being stored correctly.
In order to do this I added an option in the banners module .xml to allow for a session variable and the name of the session variable. This is being stored correctly in the module table within the params field along with the other module parameters.
Then I started on the
components > banners > com_banners > models > banners.php
by adding two lines of code in getListQuery where the SQL is assembled. They are:
$sess_vars = $this->getState('filter.sess_vars');
$sess_vars_name = $this->getState('filter.sess_vars_name');
But both variables contain nothing even though the ones the component already has can be retrieved fine. Without a doubt I need to change something somewhere else as well - but just can't figure out what to do.
Any help would be greatly appreciated.
The first thing to do is not hack the core files, hacking the core prevents you from using the built-in update feature to apply the regular bug fixes and security patches released by Joomla! (e.g. the recently released 2.5.9 version).
Rather make a copy of them and modify it so it's called something else like com_mybanners. Apart from the folder name and the entry point file (i.e. banners.php becomes mybanners.php) you will also want to update the components banners.xml to mybanners.php.(You will need to duplicate and modify both the front end /components/com_banners/ and /administrator/components/mybanners.php.)
Because of the way Banners work (i.e. banners are displayed in a module) you will also need to duplicate and modify /modules/mod_banners/,/modules/mod_banners/mod_banners.php and /modules/mod_banners/mod_banners.xml. Changing mod_banners to mod_mybanners in each location.
In Joomla! components the state is usually populated when JModel is instantiated, however, in this case the component is really about managing banners and recording clicks the display is handled by mod_banners. So, you will want to add some code to mod_mybanners.php to use the session variables you want to act on. Normally when a models state is queried you will collect the variables via JInput and add them to your object's state e.g.
protected function populateState()
{
$jApp = JFactory::getApplication('site');
// Load state from the request.
$pk = $jApp->input->get('id',0,'INT');
$this->setState('myItem.id', $pk);
$offset = $jApp->input->get('limitstart',0,'INT');
$this->setState('list.offset', $offset);
// Load the parameters.
$params = $app->getParams();
$this->setState('params', $params);
// Get the user permissions
$user = JFactory::getUser();
if ((!$user->authorise('core.edit.state', 'com_mycomponent')) && (!$user->authorise('core.edit', 'com_mycomponent')))
{
$this->setState('filter.published', 1);
$this->setState('filter.archived', 2);
}
}
The populateState() method is called when a state is read by the getState method.
This means you will have to change your copy of /components/com_banners/models/banner.php to capture your variables into the objects state similar to my example above.
From there it's all your own code.
You can find all of this information in the Developing a Model-View-Controller tutorial on the Joomla Doc's site

Getting virtuemart price at the template

I'm new in joomla and I'm getting crazy because as I wanna get the price (and another metadata) of a product at the template just using the ID of the product, does anyboy could tell me how getting it, perhaps with a helper o static method or instanciating a class? (I have googled it). It's Virtuemart 1.1.3 and joomla 1.5.10.
Thanks in advance.
Still not clear on where you're wanting to use it, but if it's from within VM that you want to get the product information, there are a number of places you can look at and see if it suits what you're trying to accomplish:
Virtuemart Custom Theme.php
Found at components\com_virtuemart\themes\default\theme.php. Allows you to write custom functions to access anything in VM for your own use.
/**
* This is the theme's function file.
* It allows you to declare additional functions and classes
* that may be used in your templates
*
Browse Results on Flypage
The search/browse saves many product fields - they're saved into an array near the end of html\show.browse.php (look for $products[$i]['product_price'] = $product_price;) , then loaded into the VM templates with $tpl->set( 'products', $products ) , and finally, used when rendering the product fly page

Prestashop: Get id of parent category

I've faced the problem while coding my very first prestashop template. So what I want to do is to get the list of all sub categories which are under the same parent category. So running foreach loop I need to send parent category id.
In the internet there's not so much information about prestas' coding, modules or stuff, but I found two possible solutions.
One was: {$product->id_category_default} but that works only inside a product page (?) Maybe I'm wrong and it's possible to get a solution with this?
Another way sounds bit desperate: {$cookie->last_visited_category}
problem with this one, that it works only if you come from parent category directly, but fails when followed by direct link or any other way.
Also I was thinking of writing php function, which sends MySQL query and returns parent category id, but is this optimal way to do? Also I haven't tried writing my own php functions in smarty, seems it woks in some different way than just calling function.
So to conclude, do anyone knows how to get parent category id without separate php function and if it's so desperate, where and how to define your own php functions for smarty and how to call them.
I'm working with prestashop 1.4.
Thank you for your attention.
I've used this code in a module, you might need to adapt it but basically, the id of the parent category is stored in the database.
$parentCategoryList = array(2, 3, 4, 5);
if(!in_array($id_category, $parentCategoryList)){
$parentCategory = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT DISTINCT c.id_parent
FROM '._DB_PREFIX_.'category c
WHERE c.id_category = '.(int)($id_category)
);
$id_category_parent = $parentCategory[0]['id_parent'];
}
else{
$id_category_parent = $id_category;
}
In my code, the $parentCategoryList stores the id of my main categories (change it to your values). Then it checks if the category you're viewing ($id_category) is a main category. If not, it looks for the parent category in the database.
I am not really happy about the use of an array to store manually the main categories, but it works.
Hope this helps!

Auto fill information for movie products in Magento

I'm working with Magento CE 1.6 in a project where we need an easy way to fill the movie info in DVDs and Blu-Rays products for a reseller ecommerce. I'm set to use the Rotten Tomatoes API wich seems very adequate for our purposes, but here's the thing: We don't want to have to input every single detail of the movie in the New Product dialog, oppositely, we want to fetch the info automatically using the movie name as hint (the API perfectly supports this). I though that we could achieve this by two means:
Having the administrator to enter only the names of the movies and
create and run periodically a script that fetches the rest of the
info with the API and updates the data directly in the DB. I've been
watching the DB changes when a product is saved and would'nt like to
do that.
Editing the Magento code to make the new product form auto fillable,
maybe with ajax, once a movie name is entered. Zend framework isn't
my strong and seems kind of hard too.
Am I seeing this problem from the rigth angle? Is there maybe another API? Or a Magento extension? Or another ecommerce?!
I would suggest a little different approach. Enhancing the Admin interface is difficult, but possible. Here is an easier way.
Method #1 - Quick and Easy
Create yourself a script that would go through a list of products. You can select them by attribute type, category, or even just select them all! Then, loop through that collection, and for each product, grab the title, query the movie API, and set the product's attributes. Then, save the product, and move to the next one. Something like this:
Note: Be sure to create your custom attributes in the admin and assign them to the attribute set.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
function getVideoDataFromAPI($title)
{
// get your data from the API here...
return $data;
}
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('attribute_set_id', $yourAttributeSetId)
->addAttributeToFilter('year', ''); // <-- Set a field here that will be empty by default, and filled by the API. This is '' because it won't be null.
foreach ( $collection->getAllIds() as $id ) {
$product = Mage::getModel('catalog/product')->load($id);
$videoData = getVideoDataFromAPI($product->getName());
if ( empty($videoData) ) { continue; }
$product->setYear($videoData['year'])
->setRating($videoData['rating'])
->save();
}
?>
Method #2 - Do the above, but in a custom extension
I always like extensions over scripts. They are more secure and more powerful. With an extension you could have an admin list of the products, can filter them how ever you would like, and have a mass action to pull the video data manually. You could also set it up on a cron job to pull regularly.

Resources