Dynamic menu/create controller for menu? - codeigniter

I'm building a small app in CI. I'm using a pre-made template with a separate menu file. That menu file gets included in the pageview:
<?php include('include/sidebar.php'); ?>
Now I want to make the items in the menu dynamic based on the user permissions. In my sidebar.php I define the menu items like this:
<?php
$classname = "logs";
if (check_class($classname) == true){
?>
<li id="<? echo $classname;?>" class="<?php if($this->uri->segment(1)==$classname){echo "active";}?>">
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">youtube_searched_for</i>
<span><?php echo $this->lang->line('menu_logs') ?></span>
</a>
<ul class="ml-menu">
<?php
$methodname = "viewlogs";
if (check_method($classname,$methodname) == true){
?>
<li id="<? echo $classname;?>" class="<?php if($this->uri->segment(1)==$classname AND $this->uri->segment(2)==$methodname){echo "active";}?>">
<?php echo $this->lang->line('menu_logs') ?>
</li>
<?php }?>
</ul>
</li>
<?php }?>
check_class and check_method are currently included in the sidebar.php file as well:
<?php
// This should not be here...
global $thisglobal;
$thisglobal = $this;
global $auth_roleglobal;
$auth_roleglobal = $auth_role;
function check_class($class) {
global $thisglobal;
//Override if admin
if ($thisglobal->auth_role == "admin") {
return true;
}
// Get current roles permissions
$role_arr_flipped = array_flip(array($thisglobal->auth_role)); // Avoid Error # Only variables should be passed by reference
$role_arr_intersected = array_intersect_key($thisglobal->config->item('user_role_permissions'), $role_arr_flipped);
$role_perms = array_shift($role_arr_intersected);
if (array_key_exists($class, $role_perms)) {
return true;
} else {
return false;
}
}
function check_method($class,$method) {
global $thisglobal;
//Override if admin
if ($thisglobal->auth_role == "admin") {
return true;
}
// Get current roles permissions
$role_arr_flipped = array_flip(array($thisglobal->auth_role)); // Avoid Error # Only variables should be passed by reference
$role_arr_intersected = array_intersect_key($thisglobal->config->item('user_role_permissions'), $role_arr_flipped);
$role_perms = array_shift($role_arr_intersected);
// Flip arrary
$role_perms["$class"] = array_flip($role_perms["$class"]);
if (array_key_exists($method, $role_perms["$class"])) {
return true;
} else {
return false;
}
}
?>
This works, but obviously including those functions into the view file is against the MVC approach, and I might want to reuse check_class and check_method in other views as well. I have moved those functions to my_controller, but again I should not call those functions from my view.
I am kinda lost in how to continue...
The sidebar does not have its own controller. Should I create a separate one? But then how do I load it because I can't (should not) call the menu controller from the page view.
Or should I call the check_class and check_method before loading the view, but I don't know yet which menu items I should check at that point.
Thanks!

I would create a library called Menu.php in there I would create functions that would check the user permissions and stuff, and also have a render method that will just output the menu.
This way your controller would load that library. Send some data into it and get the menu as a string. Then you just send that string to the view and echo it.
Other option would be looking into a presenter pattern and try to implement that in codeigniter.
Presenter pattern
Presenter library for ci

Related

I want change page heading position

In Joomla Page heading showing inside of an article I want to change the position of page heading, is it possible to customize page heading position?
I had included following code in template/protostar/index.php
<?php if ($this->params->get('show_page_heading', 1)) : ?>
<div class="page-header">
<h1> <?php echo $this->escape($this->params->get('page_heading')); ?> </h1>
</div>
<?php endif;
if (!empty($this->item->pagination) && $this->item->pagination && !$this->item->paginationposition && $this->item->paginationrelative)
{
echo $this->item->pagination;
}
?>
What you can do:
Just update one of the css files in the correct template to display the header correctly. If the header should only be reformatted on some pages and not all then you should be using different templates.
What you should do:
Otherwise (if you want to change the php instead) you can override the components/com_content/views/article/default.php using the standard joomla override method.
You can do both the above if necessary.
You should not need to override the index.php of your template in order to do this. However if you really want to i would use the code
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
$ids = explode(':',JRequest::getString('id'));
$article_id = $ids[0];
$article =& JTable::getInstance("content");
$article->load($article_id);
echo $article->get("title");
}
Sorry if you want more you need to give more :)
PS. I am on joomla 2.5 but i know that for joomla 3 it is more or less the same thing.
Sources: http://forum.joomla.org/viewtopic.php?t=525350
http://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core

Magento Info Block in Payment Method

does anyone know how do I include a block information when selecting a payment method in my checkout page? within that block I want to insert an html(image) from the payment gateway that I'm using.
I've tried to do this using jquery inserting the html needed when the page loads, the image appears quickly but some update in the layout of the checkout page removes the image, ie, it appears and disappears soon after.
I am very new to magento and any help would be welcome, thanks
I´m using magento 1.8.1
I assume that you are referring to the right blocks which output the checkout progress ( see below if you are referring to the actual payment method selection step ).
You don't need to do any client side scripting. Magento supports that by default.
Checkout progress ( right blocks on checkout page ):
You need to follow these steps:
Open your Payment Method model.
Add the following code:
protected $_infoBlockType = 'mymodule/info';
The block code should look like:
<?php
class Company_MyModule_Block_Info extends Mage_Payment_Block_Info_Cc
{
protected function _prepareSpecificInformation($transport = null)
{
$info = $this->getInfo();
$transport = new Varien_Object(array('My Var' => "My Value"));
return $transport;
}
}
Payment method selection step:
If you are referring to the actual payment method selection step, when you click on a payment method and a small form appears under it you need to:
Add the following code in your payment model:
protected $_formBlockType = "mymodule/form";
You also need to create the mymodule/form:
<?php
class Company_MyModule_Block_Form extends Mage_Payment_Block_Form_Cc
{
public function __construct()
{
parent::__construct();
$this->setTemplate('path/to/template.phtml');
}
}
The template file should look something like:
<?php $_code=$this->getMethodCode(); ?>
<fieldset class="form-list">
<ul style="display: none;" id="payment_form_<?php echo $_code ?>">
<li>
<p>here is my custom content!:)</p>
</li>
</ul>
</fieldset>

Magento, show image on custom frontend block

I've a module up and running. On the backend side I can upload images with several attributes and I'm saving image path and other information on a custom table. On frontend I already managed to detect when I need to show these images. I've used an Observer that adds a new layout handle when the displayed product is found on the custom created table. Then on that layout handle I call a phtml template file and from here I call a function inside a block that will be in charge of doing all the checks to be sure wich image to show.
My problem is that I can not find how to show these images. Everything I found references how to add image tag on a phtml file doing some extra verifications on the inserted php code. Buy on my case everything is on php code outside any phtml file.
My phtml file code:
<div>
<h3><?php $this->showCampaign(); ?></h3>
</div>
My block code for now:
<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
/**
* Shows a campaign banner according to the current selected product
* Seeks on main banners table to see if it is an image/html/product alone campaign
* Once knows the campaign type search on needed table the needed fields
*/
public function showCampaign() {
//Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
$product = Mage::registry('current_product');
$currentProdID = $product->getId();
if (Mage::registry('campaign_data') && Mage::registry('campaign_data')->getId()) {
$currentCampaign = Mage::registry('campaign_data');
} else {
return;
}
// get campaign type and show needed information
switch ($currentCampaign->getbanner_type()) {
case 1: // image
$myImgCollection = Mage::getModel('banners/bannersimg')->getCollection();
$myImgCollection->addfieldtofilter('bannerid',$currentCampaign->getID());
$currentImg = $myImgCollection->getFirstItem();
$currentImgPath = $currentImg->getimg_path();
//{{block type="catalog/product_new" product_id="16" template="catalog/product/view/your_new_page.phtml"}}
break;
case 2: // html
echo "html";
break;
case 3: // plain product url
echo "plain product url";
break;
}
}
}
As usual, couple of hours searching for an answer and when I post the question, found the answer. Just in case someone needs it: is as easy as creating an image html tag inside the code and return it to the template file. Sample code:
<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
public function showCampaign() {
...
$currentImgPath = $currentImg->getimg_path();
//build html to output to the template
$html .= "<div class=\"visual\">";
$html .= "<img src=\"" . $currentImgPath . "\" alt=\"\" />";
$html .= "</div>";
....
return $html;
}
And the phtml file code:
<div class="visual">
<?php echo $this->showCampaign(); ?>
</div>

Retrieve product custom media image label in magento

I have a custom block loading products on my front page that loads the four newest products that have a custom product picture attribute set via:
$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(4);
What I am trying to do is grab the image_feature_front_right label as set in the back-end, but have been unable to do so. Here is my code for displaying the products on the front end:
<?php foreach($_productCollection as $_product) : ?>
<div class="fll frontSale">
<div class="productImageWrap">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4) ?>" />
</div>
<div class="salesItemInfo">
<p class="caps"><?php echo $this->htmlEscape($_product->getName());?></p>
<p class="nocaps"><?php echo $this->getImageLabel($_product, 'image_feature_front_right') ?></p>
</div>
</div>
I read that $this->getImageLabel($_product, 'image_feature_front_right') was the way to do it, but produces nothing. What am I doing wrong?
Thanks!
Tre
It seems you asked this same question in another thread, so to help others who might be searching for an answer, I'll anser it here as well:
I imagine this is some sort of magento bug. The issue seems to be that the Magento core is not setting the custom_image_label attribute. Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes - so you could do something like:
$_product->getData('small_image_label');
If you look at Mage_Catalog_Block_Product_Abstract::getImageLabel() it just appends '_label' to the $mediaAttributeCode that you pass in as the 2nd param and calls $_product->getData().
If you call $_product->getData('media_gallery'); you'll see the custom image label is available. It's just nested in an array. So use this function:
function getImageLabel($_product, $key) {
$gallery = $_product->getData('media_gallery');
$file = $_product->getData($key);
if ($file && $gallery && array_key_exists('images', $gallery)) {
foreach ($gallery['images'] as $image) {
if ($image['file'] == $file)
return $image['label'];
}
}
return '';
}
It'd be prudent to extend the Magento core code (Ideally Mage_Catalog_Block_Product_Abstract, but I don't think Magento lets you override Abstract classes), but if you need a quick hack - just stick this function in your phtml file then call:
<?php echo getImageLabel($_product, 'image_feature_front_right')?>
Your custom block would need to inherit from Mage_Catalog_Block_Product_Abstract to give access to that method.
You could also use the code directly from the method in the template:
$label = $_product->getData('image_feature_front_right');
if (empty($label)) {
$label = $_product->getName();
}

magento accesing "SendFriend" module from category page

I'm trying to add the "send to a friend" action to a category page.
In the product view i can see this code:
"canEmailToFriend()): ?>
<p class="email-friend"><?php echo $this->__('Email to a Friend') ?></p>
<?php endif; ?>
If I try to add this code to my "list.phtml" (where products grid is displayed) I receive this error:
Invalid method Mage_Catalog_Block_Product_List::canEmailToFriend(Array
saying that this methos is not available in this context...
Who can I add the funcionality of "sendtofriend" module to any page I need?
Thanks in advance
This is because the $this->canEmailToFriend() call is a block method belonging to the product page, a class called Mage_Catalog_Block_Product_View. The product listing page uses a block class called Mage_Catalog_Block_Product_List which does not include this code.
The method canEmailToFriend() contains (as defined in app/code/core/Mage/Catalog/Block/Product/View.php) the logic:
$sendToFriendModel = Mage::registry('send_to_friend_model');
return $sendToFriendModel && $sendToFriendModel->canEmailToFriend();
You could embed that directly in your template and then call the helper to output the link if $sendToFriendModel->canEmailToFriend(), but the best way to achieve this would be to move the canEmailToFriend functionality out into a new helper and call it from there.
I founded an alternative solution, just load sendfriend model
$sendToFriendModel = Mage::getModel('sendfriend/sendfriend');
Then use
<?php if ( $sendToFriendModel->canEmailToFriend() ) : ?>
<p class="email-friend"><span><span><?php echo $this->__('Email to a Friend') ?></span></span></p>
<?php endif;?>
Resource from C:/xampp/htdocs/magento/app/code/core/Mage/Sendfriend/controllers/ProductController.php
I think Mage::registry('send_to_friend_model') returns an object of the class Mage_Sendfriend_Model_Sendfriend. The canEmailToFriend() method in Mage_Sendfriend_Model_Sendfriend checks whether the "email to a friend" functionality is enabled:
You can find these two methods in app/code/core/Mage/Sendfriend/Model/Sendfriend.php:
/**
* Check if user is allowed to email product to a friend
*
* #return boolean
*/
public function canEmailToFriend()
{
return $this->_getHelper()->isEnabled();
}
/**
* Retrieve Data Helper
*
* #return Mage_Sendfriend_Helper_Data
*/
protected function _getHelper()
{
return Mage::helper('sendfriend');
}
So you could check that yourself in your template like so:
<?php if (Mage::helper('sendfriend')->isEnabled()) : ?>
<?php // Do stuff ?>
<?php endif ?>

Resources