Opencart: How to include a script on certain pages? - php

I'd like to include a script to detect $_GET values on certain pages of the site. If I use the script in header.tpl, it will add the script on all pages. If I use it as a template file and display it as Layout in the Extensions > Modules, it gets added to all the pages within that Layout. Is there a way to include this only on certain selected pages? I'm sure this will have to done programatically. Just wanted to find out if there is an elegant way to do this.

You can access the $_GET files using the correct method of $this->request->get in any of the controller, model or view files. As for an elegant solution, that really depends on which pages you want it to show on and also what region of the page. For example, if you only want to use code on the product pages, but want to have the code placed in the header, you would need to check for the route to be product/product in your header.php controller in the index() method, then pass the data to the template using $this->data['your-variable'] = $some_data;. Similarly, this would be almost the same on a product page using the product.php controller and skipping the route check since that controller will only be called on product pages

Related

How does CS-Cart handle the storefront index page on the back-end side?

I am trying to add custom sections to the homepage in between product rows, which include products filtered with custom queries not provided by default from CS-Cart.
The usual way of doing this via code (the project is customized to the point that CMS functionality in this case doesn't help) is to do some logic in the proper controller and assign the data to the view.
By researching I know that the index page is handled differently from the other pages meaning that it doesn't follow the controller->view pattern.
How is the index view (homepage) handled on the back-end? Where is the logic for the index page and how do I edit it to customize my homepage?
On backend > homepage CS-Cart follow the same pattern and the files are:
php
app/controllers/backend/index.php
To add your own php code you can add it by adding into file
app/addons/[ADDON_NAME]/controllers/backend/index.post.php
tpl
design/backend/templates/views/index/index.tpl
Please check tpl file because there are already available some hooks
you have to edit config.local file with this code
$config['customer_index'] = '<Your_new_index_path>';

ZF2 Navigation: How to show some pages ONLY in breadcrumb navigation

In Zend Framework 2 I use ZendNavigation for the main navigation and the breadcrumb-navigation. I want the Breadcrumb-Navigation to show on every page, but I do not want every page to appear in the main navigation.
So in other words I want some pages to show ONLY in breadcrumb navigation.
Is there any way to achieve this? Maybe an option that can be set in the navigation-array? Or do I need to create two different navigations and use one as main navigation and the other as breadcrumb?
Edit
It doesn't really make sense to duplicate the 'pages' configuration as you can instruct the view helper to render what you need. So you can use the same navigation container to render different menus (e.g. breadcrumb vs main navigation).
You can do this by either using the provided breadcrumb methods such as setMaxDepth() or setMinDepth(). Alternatively, for more flexibility, use a view partial as I explained bellow.
In each case you are simply filtering down the complete container into the seletion you want to render.
You can use a view partial to render the breadcrumbs.
This method will find the deepest active page and pass an array of pages that leads to the active page to the partial view script.
The example in the attached link uses array_map to render the contents of $this->pages, however you can achieve the same result with a loop.
foreach($this->pages as $page) {
printf('%s', $page->getHref(), $page->getLabel());
}
Keep in mind however you will now need to check the accessibility of the page manually for the current user if you are using the ACL - This can be done using the methods available for MVC or URL pages

Add new page to Magento user account

I've been stuck on the proper way to do this for a while. I want to add a new page to the Magento user account on the front end after log in/creation. There is some information we would like to gather from our customers separate from account creation. The page needs to be dynamically generated, as it will pull information from the database, so CMS is not an option.
I know there's a template .phtml file I'll need to create. I also know where to override customer.xml to have the link show up.
But, I have NO idea how to make the page show up (I can add the link, but it goes to 404). I assume there's some sort of xml reference to associate url to file, and the template to use. If someone can point me in the right direction, I can probably reverse-engineer the format.
I also don't know how to properly use Magento's existing functions to grab the information on submit. I want to show the same page after submission, with info in fields ready to change. Can I/should I just use the same .phtml template file to process the form submit?
Thanks!
Create a custom phtml page with your desired functionality.
Create a cms page and call the .phtml page from within it as
follows,
{{block type="core/template" template=".../.../yourcustom.phtml"}}
3.Your problem solved.
I think you have a fair idea on how to handle the cms page to get your desired functionality.
You can customize the phtml file of account information page ../customer/account/edit/
but to force filling this form you need to make module and add observer controller_action_predispatch and add method that check for the data you want and if not redirect them to the account information page

Need a light understanding MVC pattern

You have the URL:
http:///www.site.com/controller/method/values/1/2/3
Do I have to always have the controller being called or can I have the view being called and instantiate the controller inside the view or in a bootstrap file referring to this view?
What I don't get it is if I need more than 1 controller on the view, how to archive that?
For example:
On my index page I want run a simple CMS, where the admin can change the text blocks and images of the site. That would be on the content management controller.
On my index page I also got the latest added products vitrine, what would be controlled by the controller products.
If I define www.site.com/contentmanagement or www.site.com to run the contentmanagement controller, how the product controller would be called?
Also, another example. On my menu I got a link to a page called aboutus, that would be a simple page and the only feature needed would be the content management controller to manage the texts blocks.
If I follow the pattern Im reading all over the place I will end with a link like:
http://www.site.com/contentmanagement/method/aboutus
?
Kinda lost here cause surely this URL will look weird. Would be much easier to have the URL calling the view http://www.site.com/aboutus and a boot file where I can tell the controller that should be loaded when the surfer is there ...
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
I appreciate any help or a light here, thanks.
by the way, Im coding in PHP.
Hm, if you want to have text blocks and images of the site (one controller), and products vitrine (second controller), then call the methods you need in one controller..
I would do it this way: when you request index page with all the elements you mentioned above, actually one controller is called, and it decides which view to show.. so in that controller, you call the all methods that you need, get the data, and pass it to the view.. if the methods are called from other controllers, just call that methods and get the data.. i often make some static methods in controllers, which I can call anywhere, so I don't have to instantiate whole object..
E.g. you call www.site.com/contentmanagement, controller is called, which will display index view, and in that controller, you call all methods you need, prepare the data, and pass that data to the final view which will be displayed..
Do I have to always have the controller being called or can I have (..blah blah..)?
It kinda depends on what you understand by "call".
Controller needs an ability to change state of both View and Model(s).
And each View need ability to request data (in Model2 MVC) from Model(s).
Thought Controller should not cause the rendering of View (which is common in all the RoR sycophants) much like View has not business in executing actions on the Controller.
What I don't get it is if I need more than 1 controller on the view, how to archive that?
Views and Controllers should have 1:1 relationship. If your view need more then one controller, you might want to look into HMVC architecture.
For example: On my index page I want run a simple CMS, (.. more blah .. ) how the product controller would be called?
CMS should be a separate application ( or at least module ) with multiple controllers.
If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?
Why not just http://site.com/cms/content/2/edit (where 2 is the ID for "about us" page). There is no law which states that URL structure for site administration must mirror the publicly available page .. hell .. it can actually cause increased vulnerability.
I am going to try to walk
What I don't get it is if I need more than 1 controller on the view,
how to archive that?
For example: On my index page I want run a simple CMS, where the admin
can change the text blocks and images of the site. That would be on
the content management controller. On my index page I also got the
latest added products vitrine, what would be controlled by the
controller products. If I define www.site.com/contentmanagement or
www.site.com to run the contentmanagement controller, how the product
controller would be called?
Having the URL to contains the controller's name is definitely nice, but it is not the requirement for MVC. So you don't have to necessary map your controller to the URL itself. A classic MVC does not tie itself to a particular naming convention, so you could call your product controller via different URL which then process the product and show the view for the product.The important for MVC is to have one controller that deals with sets of model and resulting in one view as the presentation layer. In your particular example, your entry point seems to be a single controller that interacts with both ContentManagement and Product Class/Module and the resulting interaction produce a single view.
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
Thus your original interaction above is not completely off, except that you are not really calling two controllers, but rather doing the following upon hitting index:
Load a controller, let's call this one IndexController
Load ContentManagement module to get the related content, you might want to put the info as part of your Model for the Index page
Load Product module to get related products, again put this into your Model
Pass the Model containing the info for rendering the page into the View
In your View, you render the Model that contains both Content from ContentManagement module and Product list from the Product module thereby producing a single view
If I follow the pattern Im reading all over the place I will end with
a link like: http://www.site.com/contentmanagement/method/aboutus ?
This is again not a requirement, you should follow what makes sense and easier for the users. Also, if you are required to follow the conventions, you could always make your URL pretty by using URL mapping.
What you are looking for is called HMVC and there are a couple of frameworks for that out there, like Kohana.
Also see this question:
What is the HMVC pattern?

Drupal *_preprocess with a module

I'm new to Drupal dev, and was trying to add an existing region variable to my module's preprocessor function.
Basically, I've created a new region for my site (here's the definition from within side my theme's .info file):
regions[feeds] = Feeds
From Administer->Blocks, I've added the blocks I want to the new "Feeds" region.
Then, from another module, the "Advanced Front Page" module, I'm trying to add some PHP to my "front page" inside this module. The Advanced Front Page module just allows the site to have a landing page, instead of immediately viewing a list of other site content, etc. I've enabled PHP for the content area, and then added the following:
<div>
<?php print $feeds; ?>
</div>
It does not print the "Feeds" region, and I believe it's because that region variable is not accessible from outside of the page.tpl.php file. So after looking around, I came upon these links:
http://drupal.org/node/223430
http://drupal.org/node/237391
From there, I tried to add a preprocessor function for the module "Advanced Front Page", which has a module name of "front_page" (or possibly just "front", I'm not 100% sure). Here's my preprocessor function, that I tried to add to both my template.php file, and the /modules/front/front_page.module file (not at the same time, mind you):
function front_preprocess(&$vars)
{
$vars['feeds'] = theme('blocks', 'feeds');
}
Regardless of where I've placed this file (template.php or front_page.module) it doesn't seem to do anything. Any idea where I might be going wrong?
There are several points to address in your question:
I'd second googletorps answer in that you should approach this in a different way.
The *_preprocess functions can only be used to manipulate/inject variables for templates or theme functions, e.g. page.tpl.php, node.tpl.php, theme_links(), etc. As the front_page module does not use a theme function or (special) template to render its output, you can not make the $feed variable there by means of a *_preprocess function.
Sidenote: With *_preprocess functions, naming is crucial. You need to prefix the function name either with the exact module name or the theme name, depending on where you declare it. So in your example, if you want to add a preprocess function to the module, you'd prefix it with 'front_page_', if you add it to your themes template.php, you'd add 'yourThemeName_'.
You could achieve what you want by creating the blocks directly from code in your frontpage content area. Instead of trying to output the (not available) $feed variable, you could call:
theme('blocks', 'feeds')
This will cause Drupal to return the themed blocks for the given region ('feeds' in this case). Note that this is still not a good way to do it, as even if you don't use the region in your page.tpl.php, it still gets created for every page request made to your site!
So I would go with googletorps suggestion, adding the new region only if there are other uses for it also. If I'd just wanted to add some blocks to the frontpage, I would not create a new region, but configure the blocks to show in the content region and simply restrict them to show only for <front> in their visibility settings.
I haven't tried the advanced front page module, but when dealing with regions, you shouldn't do what you have done. It's a bit hacky and actually not needed. I don't know how the module hook into the templating system, but your problem is probably getting region variables into it's scope. But instead of trying to get the region into the frontpage using the module, you should instead get it into your page.tpl.php. You can actually do what you first tried, but I would suggest that you alter it a bit like this:
<?php if ($feeds): ?>
<div id="feeds">
<?php print $feeds; ?>
</div>
<?php endif; ?>
I have improved in two ways.
By adding the if statement, you don't add empty markup. So you wont get an empty div if $feeds doesn't contain anything.
Adding id's to regions is a good idea. It makes styling them or their content a lot easier, also it adds semantic to your page which html is all about.
Now if you only want your blocks to be shown in the front page you can set that up in each of the blocks settings. So you could possibly just use a region that already exist, unless you want your blocks display a outside an existing region. When adding regions it's not a good practice to only add a region to a single page, instead it's much better to control when it's content should be shown. It might be that you don't need to create a new region, but simply can use one that's already made. Also if you want to make some template changes to your front page, you can also create the front-page.tpl.php where you can create a different template layout for your front page if you so desire.

Categories