Cannot retrive column value from jos_users in Joomla 1.5 - joomla

I have a question. In jos_users table there are columns referring to user details and such.
I can retrieve all other columns by using
$user = JFactory::getUser();
$userId = $user->get('columnname');
echo $userId;
But why can't I retrieve column user_group_id?
It returns nothing.

Seeing as you're using Joomla 1.5, you can use the following to get the user's usergroup:
$user = JFactory::getUser();
$user_type = $user->get('usertype');
echo $user_type;
Please bare in mind that 'usertype' was deprecated as of Joomla 1.6 so if you ever decide to upgrade to the latest Joomla version, this code will need to be replaced.

Related

Programmatically modify related products in magento

I'm trying to programmatically manipulate the product relations in a Magento store.
From what I've read, setRelatedLinkData should be the way to go.
As I simple test, I'm just trying to replace a products related products with nothing (i.e. an empty array), however it's not working - the product in question is still showing the related product in the backend.
The test code I'm working with is:
$product = Mage::getModel('catalog/product')->load($product->getId());
$linkData = array();
print_r($linkData);
$product->setRelatedLinkData($linkData);
echo "Save\n";
$r = $product->save();
As mentioned above however the product still has a related product when I reload it in the backend.
NOTE: I don't only want to remove related products, eventually I want to be able to add new ones as well, so a DELTE FROM... SQL query isn't what I am looking for. However if I can't get it to work to remove products, then it's certainly not going to work to add them, so one step at a time :-)
The quickest way I can think of is to use the Link Resource:
app/code/core/Mage/Catalog/Model/Resource/Product/Link.php saveProductLinks
// sample code
$product = Mage::getModel('catalog/product')->load(147);
$linkData = array();
Mage::getResourceModel('catalog/product_link')->saveProductLinks(
$product, $linkData, Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED
);
and if you want to assign products use the same code but provide this as $linkData:
$linkData = array(
'145' => array('position' => 1),
'146' => array('position' => 2)
);

Insert a k2 article into joomla database with php

I want to add an K2 article to a joomla DB with php. Can I just add a record to the article-table? Or do I I need to update any other table as well?
I have some fundamental knowledge of PHP and mySQL, but I would appreciate a nudge in the right direction for the correct syntax to do this.
Many thanks,
HÃ¥kan
I am doing the same but on K2 item save (not for the already saved items in K2). So that I can duplicate the items in K2 into articles in joomla, in one click on save on the K2 item creation panel.
An "item" in K2 is an "article" in Joomla.
I am overriding core K2 using the process I have mentioned in this post-> Joomla - Overriding getItem method
Then I have found out where the K2 saves the article, as in this post-> Joomla - Where is the code in K2 which saves a new item's Title and alias
Now, my next step will be to add a piece code to the override file's (K2's models\item.php) save() function, so as to save the same K2 item into joomla com_content table as well. You will find all the info in the posts I have mentioned. If something is not clear, leave a comment so I can revert to the query.
Good Luck!
For an article in Joomla 1.6 or later you must have an entry in the asset table as well and it has to be correctly created using the methods from JTableContent.
I've done this several times in Joomla 2.5 and later. Simply insert properly structured rows into the k2_items table in your Joomla database. You'll want to make sure each new row that gets added has a properly incremented id, though. Here's an example:
$data =new stdClass();
$data->id = null;
$data->title = $title;
$temp = strtolower($data->title);
$data->alias = str_replace(' ', '-', $temp);
$data->catid = $catid;
$data->published = 1;
$data->introtext = $introtext;
$data->fulltext = $fulltext;
Do the same with all the other K2 fields (there's a bunch of them) until you finally can write the following code:
$db = JFactory::getDBO();
$db->insertObject( '#__k2_items', $data, id );

Joomla: How to check if another user (not current user) is online

this is probably really simple but I've gone round in circles trying to get it to work. My forte is HTML/CSS rather than PHP.
I have a 3rd party component that I am customising to show when the user who posted the ad is online. So far I have not been able to get it to work.
I have been passing the user ID I get from the component, to the Joomla user object.
$user =& JFactory::getUser($var_from_component);
I have then tried to check whether the guest flag is set, as I understand that it will be zero if the user is logged in but null if the user is not.
if($user->guest==0) {
echo "User is online";
} else {
echo "User is offline";
}
I have also tried with three equals signs as I want check the value is actually set at zero rather than it being null, so basically I have been messing around with variations of this method for too long. If anyone knows a better way to check then please say. I also tried to check the session table for the userid, but I am not that savvy with mySQL queries.
Thanks in advance!
You can query session table to check if user is logged in or not-
$db =& JFactory::getDBO();
$query = 'SELECT COUNT(userid) FROM #__session AS s WHERE s.userid = '.(int)$userid;
$db->setQuery($query);
$loggedin = $db->loadResult();
if($loggedin){
//echo 'User is logged in';
}
try this,
You can check like
$user = &JFactory::getUser($user_id);
//print_r($user);
if(isset($user->guest))
echo "Online";
else
echo "Offline";
Hope this may help you..

Sanitize input in joomla

I have code like this in my Joomla plugin:
$some_id = $_GET["someid"];
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table WHERE id = '$some_id'");
$result = $db->loadRow();
Does Joomla sanitize this automatically, or i need to do something (and what) to sanitize this query ? Using Joomla 2.5.
There is no need to sanitize database queries when using Joomla. The information you are pulling down is the information that has put put there or already there, and thus you don't want to change. I would also recommend using Joomla 2.5 coding standards to make database queries, like so:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('*'))
->from($db->quoteName('#__table'))
->where($db->quoteName('id') . ' = ' . $db->quote($some_id));
$db->setQuery($query);
$rows = $db->loadRow(); //or loadResult()
The only time I have ever needed to sanitize (so to speak) something was when handling files, in which case I used JFile::makeSafe();.
Please take a look at you will get your answer
Secure coding guidelines
http://docs.joomla.org/Secure_coding_guidelines
Take a look at JInput and this corresponding documentation
Filter example:
$jinput = JFactory::getApplication()->input;
$some_id = $jinput->get('someid', '', 'string');

Magento getProductUrl() is not returning the right url (random?)

I am using Magento 1.5.0.1 and the getProductUrl() function used in the cross sell and up sell blocks on the product page is throwing up different URL formats.
Either the correct url like:
/laptop-bag.html
Or the wrong one (well it works, but of course its not the rewrite URL):
/catalog/product/view/id/825/s/laptop-bag/category/16/
Sometimes both cross sell and up sell blocks return the correct URL, sometimes both use the longer version, and in some cases, one uses the correct and the other uses the long version??
Any ideas why this is happening?
I have already run a magento database repair, reindexed, and refreshes / flushed all caches.
Try $product->getUrlPath() instead of $product->getProductUrl()
UPDATE: As per below comment by #jordan314, Magento recommends to EE customers:
The url_path attribute is no longer used as of 1.13 but is still available for backward-compatibility, and Magento will not assign a value to it for new products, so it's not recommended to continue using it. Perhaps you could try using $product->getProductUrl() instead.
The incorrect url is generated because it can't find the rewritten url.
Maybe it is caused because incorrect store_id.
eg:
$id = 290;
Mage::app()->setCurrentStore('default');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";
//change store id
Mage::app()->setCurrentStore('admin');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";
result:
store_id: 1
http://local.com/surestep-pro-diabetic-test-strips-50-strips-professional-care.html
store_id: 0
https://local.com/index.php/catalog/product/view/id/290/s/surestep-pro-diabetic-test-strips-50-strips-professional-care/
The correct url rewrite can be found in table named core_url_rewrite (including the information about the store_id)
If it found match value in core_url_rewrite, it will generate 'the correct url' else it will concat the product_id + url key + category_id
$routePath = 'catalog/product/view';
$routeParams['id'] = $product->getId();
$routeParams['s'] = $product->getUrlKey();
if ($categoryId) {
$routeParams['category'] = $categoryId;
}
Try add this when you're getting your collection
$collection->addUrlRewrite();
It has helped me.
$id = 10;
Mage::app()->setCurrentStore('admin');
$url = Mage::helper('catalog/product')->getProductUrl($id);

Resources