Joomla Component - Site Model Issue - joomla

I've been developing a Real Estate component for several of my clients. I have most everything working, except getting images from a separate table.
One example of a site model is City. Within the city model, the primary get (query) is a list of properties, based on the city. Example, if from either the cities view, or menu item, a city such as Dallas is selected, then in the single city view only property listings where property city = Dallas will show.
That part is working great, as it is supposed to. The problem is that every listing has multiple images, stored in an images table. On the property list view, only a single image (out of potentially many) needs to be pulled an displayed.
Adding as a "linked" sub-query within the main properties query doesn't work, as it will create multiple duplicate property listings for each image. So, I created as a custom method, get image.
The compiler that I am using to aid in the building doesn't support a signature method (method that takes values), so I needed to add as a class value. In the getItems function for properties, I created the class value of:
$this->a_property_id = $item->id;
Then, in the getImage custom function I added
$query->where('a.propid = ' . $db->quote($this->a_property_id));
as part of the query. In theory, this should pull a single item where a.propid equals the property id, such as property id=3, then pull an item from images where propid=3.
Lastly, I added the image code in the site view default.php as part of the foreach:
<?php foreach ($this->items as $item): ?>
<li>
<div class="uk-grid uk-panel uk-panel-box">
<div class="uk-width-medium-1-3">
<?php if(empty($this->image->path)){ ?>
<div> <img class="uk-thumbnail uk-thumbnail-medium" src="<?php echo JURI::root().'media/com_mostwantedrealestate/images/No_image_available.png'; ?>"> </div>
<?php } else { ?>
<div> <img class="uk-thumbnail uk-thumbnail-medium" src="<?php echo JURI::root() . $this->image->path . $this->image->filename; ?>"></div>
<?php } ?>
</div>
<div class="uk-width-medium-1-3 uk-float-left">
<a href="<?php echo 'index.php?option=com_mostwantedrealestate&view=property&id='.$item->id;?>" title="<?php echo $item->name;?>" rel="" >
<h3><?php echo $item->name; ?></h3>
</a>
</div>
This is kind of working, but not completely, so I'm unsure what I may be missing. With the way I have this coded, it's displaying an image. If there is only a single property listing, then it shows the proper image, however if there a, lets say, two properties: id=1 and id=3, then it shows propid=3 from the image table for both listings, rather than showing propid=1 for id=1 and propid=3 for id=3.
Any thoughts on what I might be missing?

You're looping over the $this->items but you use the same $this->image object for all of them. So you see the same image over and over again.
You can fix this by adding the image information to the item. Since you want to show just one image you can easily do this with single query joining your item table and your image table.
Another way is to combine the necessary data from different queries in your model so you can use something like $item->image->path.

Related

Joomla : render custom fields in search results

Is it possible to display core custom fields in search results ? I can add custom fields in template override for article view like this :
<?php echo $this->item->jcfields[1]->value; ?>
It works fine for com_content but it doesn't display anything in search results (com_search).
Is there a way to render my custom fields in the search results ?
Thank you.
You can create a template overwrite for the com search component like other overwrites by creating a 'html/com_content/com_search/search' folder in your current template.
In this you copy the content of /components/com_search/views/search/tmpl
You can find a description with examples here: https://docs.joomla.org/Customising_the_Smart_Search_results_page
This is targeted at Smart Search (com_finder), however you can follow this by replacing com_finder for com_search.
Then you customize and add the custom fields in the file: default_results.php in your overwrite file.
Please note that the custom fields are a string in the $result object
["jcfields"]=>
string(26) "Custom Field,Search123,1,2"
echo "My fields: ".$result->jcfields;
If you want to do this for Smart Search in Joomla 4, here is the solution.
Create a template override of the file: components/com_finder/tmpl/search/default_result.php
At the start of the file (after line 20), add this code:
// Creates an array with all the CF of the current article.
$articleCustomFields = Joomla\Component\Fields\Administrator\Helper\FieldsHelper::getFields('com_content.article', $this->result, true);
// Creates an associative array using the custom field id as a key.
$articleCustomFields = \Joomla\Utilities\ArrayHelper::pivot($articleCustomFields, 'id');
Then print the custom fields in the page:
<ul class="fields-container">
<?php
// We are using the custom fields with ids 10 and 3
if (!empty($articleCustomFields[10]->value)) : ?>
<li class="field-entry">
<span class="field-label"><?= $articleCustomFields[10]->label ?>:</span>
<span class="field-value"><?= $articleCustomFields[10]->value; ?></span>
</li>
<?php
endif; ?>
<?php
if (!empty($articleCustomFields[3]->value)) : ?>
<li class="field-entry">
<span class="field-label"><?= $articleCustomFields[3]->label ?>:</span>
<span class="field-value"><?= $articleCustomFields[3]->value; ?></span>
</li>
<?php
endif; ?>
</ul>
If you are looking for a complete tutorial, please have a look at:
https://blue-coder.com/help/blog/adding-custom-fields-in-the-search-results

How to display two currency in magento cart

I want to display two currencies in Magento's cart and invoice. I have make it in product page by this code
<div class="currency">
<?php if( Mage::app()->getStore()->getCurrentCurrencyCode() == 'USD'): ?>
<span class="price">(<?php echo round(Mage::helper('directory')->currencyConvert( $_product->getFinalPrice(), 'USD', 'AUD'), 2 ); ?>) AUD</span>
<?php else: ?>
<span class="price">(<?php echo round(Mage::helper('di`enter code here`rectory')->currencyConvert( $_product->getFinalPrice(), 'AUD', 'USD'), 2 ); ?>) USD</span>
<?php endif; ?>
</div>
and now I want to display this in the email invoice.
in the email template ti has this line
{{layout handle="sales_email_order_items" order=$order}}
looking up sales layouts we find app\design\frontend[package][theme]\layout\sales.xml
from there you can see the .phtml files used to render the items section of the email, edit the ones you need, keep in mind that these are also used on the front end.
if you want to separate the .phtml files so that the frontend and email template can use different files, copy/paste/rename sales_email_order_items in the .xml file and change the template= parts to the new templates, be sure that in your email template you change the handle stated above to what you renamed the layout to otherwise it'll use the original one.
your code should work fine, you may just need to change $_product->getFinalPrice() if it doesn't work and $_product may not be in the template or be a different class since your dealing with the the collection that deals with sales_flat_order rather than catalog_product

Magento - All categories have same image

So there's some code floating around that gets an individual subcategory's image:
foreach ($collection as $cat){
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$_img = $this->getCurrentCategory()->getImageUrl();
}
Where $cat is the subcategory I'm trying to get the image of. The problem I'm running in to - the images for the subcategories are all the same! The first image from the subcategories is the image that shows up on the page. The links and names are all correct though: (this is also in the for loop)
<div>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<img src="<?php echo $_img; ?>" title="<?php $cat->getName() ?>"/>
<cite><?php echo $cat->getName();?></cite>
</a>
</div>
I'm making this modification in catalog/category/view.phtml. There's a bit more to it than shown, but this is all the relevant information.
Edit
All categories have their own unique images, which are properly uploaded. They show in the admin correctly. Also, $cat->getId() is returning the correct id's for the individual subcategories.
#RandyHall, are your sure that $this->getCurrentCategory() will get the category from the same place as $layer->setCurrentCategory($cur_category); will set it to?
Watch the source code here and here.
As you can see category is set to the layer model and get returns category from registry(via call to block).
So I would suggest you to do something like this:
$_img = $layer->getCurrentCategory()->getImageUrl();
Perhaps you are doing wrong when getting image url.
instead of using
$_img = $this->getCurrentCategory()->getImageUrl();
try below code
$_img = $cur_category->getImageUrl();
foreach ($collection as $cat){
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getImageUrl();
}
Looks to me like this is what you are going for?

joomla category list override to add id to each list item

I am trying to add a custom style to each list item within Joomla!s Category List output which gives me the following html
<div class="blog">
<div class="cat-children">
<ul>
<li class="first">
<span class="item-title">HYT
</span>
</li>
<li></li>
</ul>
</div>
</div>
I think what I need to do is add something like:
<li id="myID<?php echo $this->item->catid; ?> ">
The trouble is I can't find which file to override. I have looked in /templates/mytemplate/html/com_content/category/ as well as /components/com_content/views/category/tmpl yet none of the files seem to have a an unordered list within them that relates to cat-chidren.
So my first question is which file should I edit? And my second is what is the best syntax of this->item->[correct'Method'?] (is method the correct term or variable, I'm a little shaky on this!) to use so that each list item will have an id="myID[nameofarticle/subcatagory]"
You'll see cat-children in /components/com_content/views/category/tmpl/default.php
The ul is in another loaded subtemplate, loadTemplate('children'); ?> , i.e.
/components/com_content/views/category/tmpl/default_children.php
If you want to modify the li class you could stick something like this at line 26 (of your override not core file - but fine to just test on a core file)
<?php $class = ' class="cmyId' . $this->escape($child->title) . '"';?>
That would make each li appear as
So this
<li<?php echo $class; ?>>
<?php $class = ''; ?>
becomes this
<?php $class = ' class="cmyId' . $this->escape($child->title) . '"';?>
<li<?php echo $class; ?>>
<?php //$class = ''; ?>
Have tested it out on a 2.5 installation.
You should override several files stored in components/com_content/views/
Depending on the list you want to edit, you should then look in the folders:
- article
- category
- categories
- featured
In each of these folders you'll see a subfolder called 'tmpl', inside which there is a 'default.php' file. That's what you're looking for.
If you want to override the files remember that the best practice is to place the alternative files in your template's folder, building a similar path as the one in which the original file is (e.g. for the article folder: templates/YOURTEMPLATEFOLDER/html/com_content/article/default.php - NO tmpl folder needed, nor views folder).
An alternative and, in my opinion, easier way could be setting different templates for each category, and then assigning to each the list styles you prefer.
Or, even easier, you could simply edit your index.php file in the template's folder so that it echoes a specific css stylesheet depending on the $catId.
As your HTML code shows you are using Category Blog view, each article instance in the category is being rendered by blog_children.php file, as it shows /components/com_content/views/category/tmpl/blog.php
<?php echo $this->loadTemplate('children'); ?>
So /components/com_content/views/category/tmpl/blog_children.php is the file you need to edit or override in template html directory.
Then you can apply custom styling adding an id or class for each article with $child->id.

How to implement a sidebar in Zend Framework

How do I implement a sidebar in Zend Framework?
I know that I can use a placeholder or something similar in Zend_layout, but how do I automatically generate the code for the sidebar in my controllers without having to call a sidebar class within every controller?
My setup is as follows
Application
- modules
- blog
- other modules
I only want the sidebar for my blog module.
I have found this http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-layout-sidebar-etc-2677.html but I do not understand the last part "just inject your layout, register it with the front controller ..."
You could just have a action and view in one of your controllers which renders the sidebar.
from the layout for the blog module you just call:
<? echo $this->action('action','controller','module',array('optionalparams'=>1); ?>
on the position where you want to have it. So one call to one action.
Rather than use the action stack and the action() view helper, you could render a "partial view script" that includes your sidebar elements.
# in your layout.phtml
<div id="sidebar">
<?php echo $this->render('blog/_sidebar.phtml'); /*relative to your view scripts directory*/ ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
The render() view helper is used to render the content of another view script. It has the same scope as all your other view scripts, so if there are any variable assigned to the view, they will be available to your partial. So in the example above, the categories variable was set in the controller.
There is another view helper called the partial() view helper. This function is a little more expensive since it creates its own variable scope. In other words, none of your current view variables will be available. You will have a clean slate to work with, which means you must pass in any variables you need:
# in your layout.phtml
<div id="sidebar">
<?php echo $this->partial('blog/_sidebar.phtml', array('categories2'=>$this->categories)); ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories2 as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
I don't find myself using partial() very often since it is more expensive, and I rarely need to create a separate context.
As far as setting up the variables for use in the sidebar partial ($this->categories in this example), I have used a number of different methods depending on the particular problem. If it's specific to a controller action, I will write the code and assign it in the view script:
# controller
public function somethingAction()
{
$this->view->categories = $this->_getCategoriesForThisParticularAction();
// other controller code
}
If my code is more generic to all the actions of the controller, I will utilize the controller's preDispatch() function. If it's more generic to multiple controllers, I will put the code in the init() of my base controller (a controller the most of my controllers extend).
Sometimes I do not even put the code in my controller. If it's simple enough, I just stick the code in the partial. If it's a little more complex, I will move it to a view helper. This may break the MVC pattern, but I think it really depends on the particular case in order to determine the best placement.
If you are using Zend_Layout, just add the sidebar with the Action viewhelper as Rufinus said.
in your layout script:
<div id="sidebar">
<?php echo $this->action('action', 'controller', 'module', array('optionalparams'=>1)); ?>
</div>
<div id="content">
<?php echo $this->layout()->content; ?>
</div>
This should meet the requirements posted in your question.

Resources