Display Joomla component within a module - joomla

I'm trying to display a Joomla 2.5 component in a module.
In my module entrypoint I have:
JLoader::import('joomla.application.component.model');
JLoader::import( 'quizzes', JPATH_SITE . '/components/com_questions/models' );
JLoader::import('joomla.application.component.controller');
if (!class_exists('QuestionsControllerQuizzes')) {
require_once (JPATH_SITE . DS . 'components' . DS . 'com_questions/controllers' . DS . 'quizzes.php');
}
$quiz_controller = new QuestionsControllerQuizzes(false, $params->get('poll'));
$quiz_controller->execute( JRequest::getVar('task','load') );
I tried to debug it and it seems that can't load the correct view. Seems to looking for a generic com_content in $this->basePath.
The strange thing is that if I load the module in a page where the component is loaded, it display correctly.
Any idea how to succesfully display the component output in a module?

Try this: How to display Joomla component in a module
and remember your controller should include a function for the task!

Related

Laravel 5.2: Trying to use ID from database to display image with corresponding name?

For each entry in the database, there is a corresponding image whose file name is the same as the unique ID of that database entry, eg an entry with ID '4' has the header image title '4.png'. I want to display the relevant image to the entry currently displayed, but I can't quite get the code below to work:
$img = asset('images/header/' . $img_id . '.png');
I've verified that $img_id works, and the images are displayed when if I just use the direct path to them - ie 'images/header/4.png'.
Your code snippet seems to be ok. You should be using it in the view like:
<img src="<?= asset('images/header/' . $img_id . '.png') ?>">
Ideally you could opt for an accessor to deal with this. In your model, add this:
public function getImgAttribute()
{
return '<img src="'. asset('images/header/' . $this->attributes['id'] . '.png') .'">';
}
Now you can use it in your views like $model->img.

Joomla 3 MVC platform 12.1 custom component template override

I'm working with Joomla 3's MVC 12.1 platform and running into some issues with getting template overrides to work.
My Component has 3 controllers: stores, coupons, sales.
For each of these controllers I call my view similiar to this:
$paths = new SplPriorityQueue;
$paths->insert(JPATH_SITE.'/templates/'.$app->getTemplate().'/html/com_stores/stores', 'normal');
$paths->insert(JPATH_COMPONENT . '/views/stores/tmpl', 'normal');
$view = new StoresViewsStores(new StoresModelsStore, $paths);
$view->setLayout('default');
// Render our view.
echo $view->render();
Only deference between them is switching the view/model/directories out respectively.
As long as I don't include any overrides in my template everything works as expected. However as soon as I include overrides things get wonky.
If I add a com_stores/stores/default.php into my template, my stores get overridden correctly however the coupons and sales controllers start pointing to the stores override instead of their own folders.
Is their something that i'm missing that is making each of the controllers point to the same override?
Just for reference here is what is in for the paths in each controller.
Stores:
$paths->insert(JPATH_SITE.'/templates/'.$app->getTemplate().'/html/com_stores/stores', 'normal');
$paths->insert(JPATH_COMPONENT . '/views/' . $viewName . '/tmpl', 'normal');
Sales:
$paths->insert(JPATH_SITE.'/templates/'.$app->getTemplate().'/html/com_stores/sales', 'normal');
$paths->insert(JPATH_COMPONENT . '/views/' . $viewName . '/tmpl', 'normal');
Coupons:
$paths->insert(JPATH_SITE.'/templates/'.$app->getTemplate().'/html/com_stores/coupons', 'normal');
$paths->insert(JPATH_COMPONENT . '/views/' . $viewName . '/tmpl', 'normal');
Thanks for the help
Finally found the solutions. I started by tracing through The component to see what being loaded. Turns out everything was being forced through my stores controller. So I made a slight change in how my component checks to see which controller to use.
$controller = $app->input->get('controller','stores');
to
$controller = $app->input->get('view','stores');
One thing to note is that all of my views match match my controller name perfectly so i didn't have to do anything special to know this will work.

joomla controllers how they work?

I am struggling to understand how to call sub controllers from a joomla component. What are to be placed in the controllers folder?
I have the entry point of my component like -
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// require helper file
JLoader::register('TieraerzteHelper', dirname(__FILE__) . DS . 'helpers' . DS . 'my_helper.php');
// import joomla controller library
jimport('joomla.application.component.controller');
$controller = JController::getInstance('MyController');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
Then if I want to call a controller, which is placed in the controllers folder, how do I do that?
You do a task=controller.function
As an example: You want to call the MycomponentControllerFoo in /controllers/foo.php and execute the function bar(). You use the following URL to call this:
index.php?option=com_mycomponent&task=foo.bar
Or you can use a form where there is a hidden task field.

How to resize the categories images in Magento?

How to resize the categories images in Magento? I used the following code to resize product images, but I'm not able to use it for showing categories images:
$this->helper('catalog/image')->init($_product, 'small_image')->resize(170);
If I am not mistaken, it should be :
init($_product, 'small_image')->resize(100,100);
// single parameter work with 'image'
init($_product, 'image')->resize(100);
// How about this
$this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $image->getFile())->resize(100,100);
Here is the new code. If you tell me before which extension used, we were solve quickly.
If I am not mistaken, you used Template Monster Catalog Image Extension. So, there is a function inside of the extension, like below.
// app/design/frontend/default/default/template/easycatalogimg/homepage.phtml
<?php echo Mage::helper('easycatalogimg/image')->resize($imageUrl, $width , $height) ?>
<?php
$_file_name = $cat->getThumbnail(); // Here $cat is category data array
$_media_dir = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
$cache_dir = $_media_dir . 'resize' . DS; // Here i create a resize folder. for upload new category image
if (file_exists($cache_dir . $_file_name)) {
$catImg =Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . 'resize' . DS . $_file_name;
} elseif (file_exists($_media_dir . $_file_name)) {
if (!is_dir($cache_dir)) {
mkdir($cache_dir);
}
$_image = new Varien_Image($_media_dir . $_file_name);
$_image->constrainOnly(true);
$_image->keepAspectRatio(false);
$_image->keepFrame(false);
$_image->keepTransparency(true);
$_image->resize(224, 174); // change image height, width
$_image->save($cache_dir . $_file_name);
$catImg = Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . 'resize' . DS . $_file_name;
}
echo $catImg ; // display resize category thumbnail imagename
?>
" />
For more clarification see here
If you want to resize category images, here is a free module based on core image resize. As I was struggling myself so, ended up creating this extension to resize category images with ease same as product image resizing.
I realize that I'm late to the party but I had a chance to look at this recently and using init() to resize product images is perfectly fine as described in the question but you shouldn't have to use the same function to resize a single image, through a function where the image itself (third parameter) is an optional value.
Here's what I've done to resize the image, this is for any image:
$model = Mage::getModel('catalog/product_image');
$model->setBaseFile('test.png');
$model->setWidth(100);
$model->resize();
$model->saveFile();
echo $model->getUrl();
// generates the following url: http://example.com/media/catalog/product/cache/1//9df78eab33525d08d6e5fb8d27136e95/test.png
If you don't want to save your image in the media directory then you can use Varien_Image object to achieve that:
$varienImage = new Varien_Image('test.png');
$varienImage->resize(100);
$varienImage->save('var', 'test2.png');
save() takes the first parameter as the directory where you'd like the new file to be saved and second parameters is the name of the new file.
The steps are similar as to what the init function does aside from dealing with a bunch of logic with the product itself.
If you need to resize the category image as it keeps shrinking to 475px wide, you can do so by deleting the 'width="475"' from the following template:
app/design/frontend/default/default/template/catalog/category/view.phtml
You might also need to turn off or clear the cache in the backend at: System --> Cache Management --> Image Cache --> Clear.
From: http://www.pridedesign.ie/content/magento-resize-category-image

Cakephp : Check if view element exists

Is there a way to check if an element exists for a view?
I want to load a different element according to a category it belongs to but not all categories have an element for it...
As of CakePHP version 2.3 you can use the View's elementExists method:
if($this->elementExists($name)) { ... }
In older versions of 2.x you can do:
if($this->_getElementFilename($name)) { ... }
But sadly in version 1.3 it looks like the only way is to know the full path and do something like:
if(file_exists($path . 'elements' . DS . $name . $ext)) { ... }
That is what they do in the 1.3 source code, but there is some complexity around getting $path from various plugins and checking each of those paths. (See link below.)
Sources:
http://api.cakephp.org/2.3/class-View.html#_elementExists
http://api.cakephp.org/2.0/source-class-View.html#722
http://api.cakephp.org/1.3/source-class-View.html#380
set element name in controller:
$default_element = 'my_element';
$element = 'my_cat_element';
if ($this->theme) {
$element_path = APP . 'views' . DS . 'themed' . DS . $this->theme . 'elements' . DS . $element . DS . $this-ext;
} else {
$element_path = APP . 'views' . DS . 'elements' . DS . $element . $this-ext;
}
if (!file_exists($element_path)) {
$element = $default_element;
}
You can always load an element specific to a category 'on demand' by telling it so from the controller. For example:
Within Controller Action:
$this->set('elementPath', "directory_name/$categoryName");
Within the View (this can also be tried exactly within a Layout):
<?php
if (!empty($elementPath)) { // you can also set a default $elementPath somewhere else, just in case
echo $this->element($elementPath);
}
?>
In fact, there is even other ways to achieve this. If the element is going to be loaded within a layout, then the set() method shown above can be specified from the view itself. Or, it can even be fetched from url parameters, like:
Within the View or Layout:
<?php
$elementPath = $this->params['url']['category']; // note that the param array can vary according how you set the url; see http://book.cakephp.org/#!/view/963/The-Parameters-Attribute-params
echo $this->element($elementPath);
?>
Of course, you will always have to specify, but the same would go for checking if the file exists.

Resources