Joomla 2.5 - how to access module params? - joomla

Does anyone know how I can get the value of the module's 'access' parameter (e.g. public, registered..etc) from within the module's helper.php? I can only see $params, which are all the module-type specific parameters, not the generic params.
I need to retrieve the access permissions and feed them to a plugin.
Thanks :)

Do you mean when you are rendering a module on a page or you want to know when it isn't being rendered?
JModuleHelper will only render a module if the user has access.
But if you want to find out for other reasons than rendering you need to query jos_modules.
Module access level actually can't be found by querying the jos_extensions table since that does not refer to specific instances of modules (e.g. main menu versus some other menu both of which are instances of mod_menu and which may have different access levels). (Use your own prefix of course.)
P.S. I think it might be worth it to put that in as a pull request so you could just get it from the helper. Seems somewhat arbitrary to me that it's not returned in the array.

you will need to run an SQL query for this.
For 1 specific module:
$db =& JFactory::getDBO();
$query = 'SELECT access FROM #__modules WHERE element="mod_yourmodule"';
$db->setQuery($query);
$result = $db->loadResult();
print_r($result);
Don't forget to change mod_yourmodule to whatever module you want the data for.
For all modules:
$db =& JFactory::getDBO();
$query = 'SELECT access FROM #__modules';
$db->setQuery($query);
$result = $db->loadResult();
print_r($result);
Hope this helps

Related

Magento : Taking 35 Second to load 4 product's

I have created a extension that filters product collection based on a attribute.
Below is the controllers, block and view template code.
Controller
$url = Mage::getUrl('no-route');
if(Mage::app()->getRequest()->getParam('ajax')){
echo $this->getLayout()->createBlock('catalogextensions/bestsellers_home_list')
->setTemplate('catalog/landings/bestseller.phtml')
->toHtml();
}
else{
$this->loadLayout();
$this->getLayout()->getBlock('head')->setTitle('Besesellers');
$this->renderLayout();
}
Block product collection function
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter(array( array( 'attribute'=>'top_seller', 'eq' => '1' )));
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
$products->getSelect()->limit(4,$this->get_cur_page());
return $products ;
Product collection is iterated on view.phtml template.
Now, it is taking 35 seconds to get the output, below are the statistics of execution that i was able to get by making use of PHP's microtime() function
For
Block(product collection ) it taking around 0.01 second
Template Rendering its taking around 0.12 second
But for controller function it is taking around 35 second
I am not able to find what to check, because controller function just create a Block at runtime.
*Note:I am making use of a Paid full page cache Extension "Mirasvit FPC".
how can i find it what is taking time
Server configuration is
30GB Ram with 4 vCPU.
Application server : Nginx + php5-fpm.
Version: Magento CE 1.8.0.1
Thanks.
You use a bit deprecated way to call the filters. I don't know if that can really influate the time of loading but using addFieldToFilter() would limit the calls to database and can be usefull. You can use :
Magento: Filter products by Status
filtering product collection on "is_salable"
The block is maybe not cached correctly. I mean the products are load each time the page is called.
Make sure your flat product is set to on and all attributes needed for your filters are set as filter.
If a listing comes from EAv tables it might consume lots of resources thus slowing down.
Additionally apply optimization tricks.
First, are you sure you need to add all attributes to select?
You can replace the
addAttributeToSelect('*')
with
addAttributeToSelect(array('attribute_code', 'attribute_code'))
Next, what exactly is that
$this->get_cur_page()
doing? Rest of code shouldn't make disastrous performance issues.
PS
This should have been a comment, but I'm unable to comment yet.

Deleting customer addresses in magento

I need to delete customer addresses programmatically, but I didn't find a function to do that.
$recordedAddresses = array();
foreach ($customer->getAddresses() as $address)
{
$recordedAddresses = $address->toArray();
}
I already took the addresses' collection as showed above, I just wanna delete them by id.
Curiously I didn't find examples but using API.
Could someone gimme a hand with that?
Somehow Magento keeps empty entities after using $address->delete() in my case. There were empty addresses on account preventing admin from saving the customer form when using this method.
Only way I've found to actually remove the address from user account is by changing the protected $_isDeleted flag to true:
$address = Mage::getModel('customer/address')->load($addressId);
$address->isDeleted(true);
Hope it saves some time for anyone who will stumble uppon same Magento behaviour.
Have a look at the Mage_Customer_AddressController controller class and deleteAction() method. Essentially all you need to is load the address by it's id:
$address = Mage::getModel('customer/address')->load($addressId);
and then delete it:
$address->delete();
delete() is a standard method you can run against all models (see Mage_Core_Model_Abstract), you can also set the _isDeleted flag and call save() which will have the same result.

Magento - Limit selected fields without collection when loading a Model

I know how to limit fields when dealing with a collection, but what I'm wondering is if I can limit the selected fields when using getModel()->load($id).
My thought process is if I know the ID I use:
Mage::getModel('model')->load($id)
as opposed to:
Mage::getModel('model')->getCollection()->addFieldToFilter('id', $id)->getFirstItem();
The issue is load($id) returns everything. I know I can use addFieldToSelect on the collection. Is there an equivalent when using load()? When I google this I get the collection method.
Thanks,
GG
EDIT:
I just want to add that if the collection way is how it's done than that's fine. I just want to make sure it can't be done using load().
No, there is no alternative to use the collection except for rewriting the model code altogether which is probably not what you want to do.
The model load call can only be used to load a full entry - what you can specify is the attribute which to use for the load. For example:
Mage::getModel('catalog/product')->load($sku,'sku')
will load a product by its sku.
In fact the model load itself simply defers to it's resource by doing:
$this->_getResource()->load($this, $id, $field);
But the resource load method does also not provide the ability to filter for specific attributes, so you will always load the full pack or use the collection.
You could try loading a collection with only one item (as you are doing now), and selecting what attributes you want using:
addAttributeToSelect('you_attribute_code_here')
This will return a collection (albeit containing one item), with only the attributes you wish to have returned. Example:
$productIds = array(166);
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('description')
->addFieldToFilter('entity_id', array('in' => $productIds));
$product = $collection->getFirstItem();

Magento Tables and how they work with the database

I been trying to understand Magento and I read many things on the wiki, but I couldn't figure out How does Magento works with database tables? because I didn't see any SQL
I would reccomend read over this blog post from Alan Storm:
http://alanstorm.com/magento_models_orm
He explains quite abit about the Magento ORM system, and in my opinion that entire site is a great resource for any Magneto developer.
If you watch your MySQl log, the calls made by magento can sometimes be 500 lines long or longer ... These calls are dynamically constructed using XML files. The best way to manipulate Magento data manually is to use MAGE:: calls or use a direct database connection by using:
$read = $resource->getConnection('core_read');
$sql = "select * from [YOUR_TABLE] where 1 limit 1";
$result = $read->query($sql);
It's either that or calls that look like:
$value = 'some value';
$item->setData('some_key', $value);
$item->save();
Magento is object oriented, so those are the most commonly accepted and used ways to retrieve/set data in Magento. I hope that helps.
Read chapter 5 onwards from the knowledge base.
You are not really asking a question so no one can help on the specifics, I always find that you learn best by doing, I find the best way to mess around with magento is to create a test.php file in shell/ with the following: (for example)
<?php
require('abstract.php');
class Test extends Mage_Shell_Abstract
{
function run(){ //call your functions here
echo 'running ..';
$this->database();
}
function database() { //you can create as many functions as you like
$entityId = '4449'; //product id
$product=Mage::getModel("catalog/product")->load($entityId);
var_dump($product->getAttributeText('size'));
}
}
$test = new Test();
$test -> run();
Then you can run from console:
php test.php
and it returns in my example
running ..string(11) "Extra Large"
Hope this helps you, next time be more specific.

How popular article module is working?

/modules/mod_articles_popular/tmpl/default.php
Can somebody explain me, how this working?
I do not understand how these $item->link and $item->title get correct information?
Where is MySQL query? Is it global variables? If yes, where they are described?
Any advice is much appreciated.
So, like most modules /tmpl/default.php is included on the last line the module entry point file i.e. mod_articles_popular.php
In that file helper.php is included first followed by
$list = modArticlesPopularHelper::getList($params);
As you can see this calls the getList() method of the helper class which performs the task of retrieving the $list of articles.
It (modArticlesPopularHelper) in turn loads the ContentModel and sets the state of the $model based on the default app params and the modules settings.
The it asks the model for the actual items required with the line $items = $model->getItems().
After that it loops through the items returned by the model and creates a link value for each article prior to returning it to the module.
As a result $list is filled with each of the article items which are pulled out individually in the foreach loop in /tmpl/default.php file.

Resources