How to create a function available in all opencart controllers? - opencart

I always use:
echo '<pre>';
var_dump($results);
echo '</pre>';
I know I can create the following function
function v($results){
echo '<pre>';
var_dump($results);
echo '</pre>';
}
But I don't know how can I make it available in all controllers.

Cleanest way, imo would be to include something in /system/startup.php in the helper block:
// Helper
require_once(DIR_SYSTEM . 'helper/my_functions.php');
Then make a new php file /system/helper/my_functions.php and add your custom functions there and they will be available sitewide in both admin and front end. It would also be a good idea to check and make sure the function doesn't already exist to be on the safe side:
if (!function_exists('v')) {
function v($results){
echo '<pre>';
var_dump($results);
echo '</pre>';
}
}
This should work on any version of Opencart.

Related

getGraphEdge getGraphNode - What's the difference and when to use it?

I'm confused by those both.
In my case, I try to get data from a FB page and got this code:
try {
$response = $fb->get('/' . $sPageID . '?fields=posts', $_SESSION['facebook_access_token']);
$responseFeed = $fb->get('/' . $sPageID . '/feed', $_SESSION['facebook_access_token']);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "catch";
dd($e->getMessage());
}
$graphEdge = $responseFeed->getGraphEdge();
$tester = $response->getGraphNode();
echo "<pre>";
print_r ( $tester );
echo "</pre>";
$response and $responseFeed contain data. But only $tester contains data later on. $graphEdge is empty.
$tester offers a method: getParentGraphEdge() but the return value is empty as well. That happens on different URLs like /pageID or /pageID?field=posts or /pageID/feed
When to use getGraphEdge? And why doesn't it work here?
How you use thoses functions is the right way. Use getGraphNode() when you request a node, and getGraphEdge() for an edge.
I don't know why the edge is empty, but try to execute your request (/{page-id}/feed) on the Graph API Explorer with the access token in $_SESSION['facebook_access_token'], maybe its result is just empty.

SoapClient does not recognize a function I am seeing in WSDL

I have a simple webservice in symfony2 that is working perfectly. I have added a new method, however, strangely, that method is not recognized, even when I see it in the WSDL definition.
Please load: WSDL definition
Method is called GetHoliday
The controller that executes that method is the following:
public function getHolidayAction() {
date_default_timezone_set('America/Santiago');
$request = $this->getRequest();
$client = new \SoapClient('http://' . $request->getHttpHost() . $request->getScriptName() . '/feriados?wsdl');
$year = $request->get('year');
$month = $request->get('month');
$day = $request->get('day');
$types = $client->__getFunctions();
var_dump($types);
die();
$result = $client->GetHoliday('8cd4c502f69b5606a8bef291deaac1ba83bb7727', 'cl', $year, $month, $day);
echo $result;
die();
}
After the call to __getFunctions call, GetHoliday method is missing.
If you want to see the __getFunctions response, please load online site
Enter any date in the input field. The response will appear in red.
The most curious thing, is that this works in my development machine which also has RedHat operating system (my hosting is HostGator).
Any help will be appreciated,
Finally, the problem was that the WSDL was being cached.
To make the first test, I used
$client = new \SoapClient('http://' . $request->getHttpHost() . $request->getScriptName() . '/feriados?wsdl', array('cache_wsdl' => WSDL_CACHE_NONE) );
To instantiate SoapClient. That way, it worked. so to get rid of WSDL_CACHE_NONE parameter, I deleted all files that start with wsdl in /tmp folder.
Regards,
Jaime

Joomla 2.5 Virtuemart hook method in a custom plugin wont fire

I managed to create this simple plugin that I need it to fire a method when ever an item is added to the cart and a method to fire when a checkout has happend. But those methods wont fire in anyway. I also seen another plugin named stockable in virtuemart that uses plgVmOnAddToCart and it fires there correctly. But in my plugins class it wont fire at all.
Here is my code in my plugin, what can I do to make it work? thank you
<?php
defined('_JEXEC') or die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' ) ;
if (!class_exists('vmCustomPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
class plgVmAftercheckout extends vmCustomPlugin {
private $stockhandle = 0;
function __construct(& $subject, $config) {
parent::__construct($subject, $config);
$varsToPush = array(
'selectname1'=>array('','char'),'selectname2'=>array('','char'),'selectname3'=>array('','char'),'selectname4'=>array('','char'),
'selectoptions1'=>array('','char'),'selectoptions2'=>array('','char'),'selectoptions3'=>array('','char'),'selectoptions4'=>array('','char')
);
$this->setConfigParameterable('custom_params',$varsToPush);
}
public function plgVmOnAddToCart(&$product){
echo "plgVmOnAddToCart fired";
die();
}
public function plgVmOnUserInvoice(){
echo "plgVmOnUserInvoice fired";
die();
}
}
?>
Ok I found the solution. The problem was the class name. In the joomla 1.5 documentation
http://docs.joomla.org/Creating_a_Plugin_for_Joomla_1.5
it mentions that the class name must follow this rule:
class plg extends JPlugin
But this is not mentiond in any joomla 2.5 documentation, since it is probably considered " an already known rule ".
So my solution was to change the class name from
class plgVmAftercheckout extends vmCustomPlugin {
to
class plgVmCustomAftercheckout extends vmCustomPlugin {
"Custom" because the plugin belongs to a specific group called Custom. So we need to mention the group name in order to make those hook methods observe the events.

Symfony2: global variables in php templating engine

There is a cookbook for adding globals to the twig templating engine, but it doesn't get into doing the same thing for the php engine. How would I do this?
So I might have something like:
# config.yml
someSortOfReferenceToThePHPEngineInstance:
calls:
- [ addGlobals, ["foo", "bar"] ]
- [ addGlobals, ["myService", "#myService"] ]
And then access those like:
// templateName.contentType.php
<?
echo $foo; // echos "bar"
echo $myService->myMethod($foo); // echos the result of modifying "bar" with "myMethod" method of "myService" service
I could not find any documention on this for the PHP engine...
What does work however is:
Config:
//config.yml
parameters:
hello: "YO!"
PHP Template:
// index.html.php
<?php
print $view->container->parameters['hello'];
This does not fit as nicely as the twig convention... Maybe there is better way - I have not debugged any further...
Here are a couple of options:
If you create a base controller that all others inherit from, you can override symfony's render function and add keys to the parameters argument, like:
public function render($view, array $parameters = array(), Response $response = null){
if(!array_key_exists("bar", $parameters){
$parameters["foo"] = $this->get("foo");
}
if(!array_key_exists("bar", $parameters){
$parameters["bar"] = $this->get("bar");
}
return parent::render($view, $parameters, $response);
}
This is the only way I see to modify the "global" variables "globally", though they'll not be available in any views rendered by controllers you don't create (of course, those'll likely be done in Twig anyway and you can use the normal twig means of adding functionality).
The PHP rendering engine has what're called "helpers", which you can access via array keys of $view, like:
$view["foo"]->doSomething();
We created a class for easily making services into helpers:
use Symfony\Component\Templating\Helper\Helper as BaseHelper;
class Helper extends BaseHelper{
protected $name;
public $service;
public function __construct($name, $service){
$this->name = $name;
$this->service = $service;
}
public function __get($name){
if(isset($this->service->$name)){
return $this->service->$name;
}
}
public function __call($name, $arguments){
if(method_exists($this->service, $name)){
return call_user_func_array(array($this->service,$name), $arguments);
}
}
public function getName(){
return $this->name;
}
}
Then in our configuration under the services we'd add:
helper.foo:
class: %helper.class%
arguments:
name: "foo"
helper: "#foo"
tags:
- { name: templating.helper, alias: foo }
This would theoretically be available then to any view files, even those with controllers you don't have control of.
I had a very same problem. For some reason this feature is only available for Twig templating with TwigBundle. Both Twig and PHP templating engines provide possibility to define global variables, but only Twig engine has configuration for that. For me the only real way to achieve that is something you proposed in the question post - to define method calls (and this is the way Twig globals are registered).
Problem is, that with DI extensions you can't access service definition from outside your extension, so you can't add these calls from your DI extension. The way for me was to do that with DI compiler pass.
But I'm also developer of ChillDevViewHelpersBundle and since I was facing this problem in most of my projects I decided to implement it there for common use and you can use 0.1.8 release for this feature.

Why is php_template_preprocess_page function not called in Drupal 6x?

From another forum I found the following example:
"I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.
in the template.php file for your theme:
function phptemplate_preprocess_page(&$vars) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$vars['template_file'] = 'page-ajax';
}
}
then create page-ajax.tpl.php in your theme directory with this content:
<?php print $content; ?>
"
This seems like the logical way to do it and I did this, but the phptemplate_preprocess_page function is never called ... any suggestions?
I figured it out for myself from a Drupal Support Theme Development page:
"Maybe this helps
leahcim.2707 - May 29, 2008 - 05:40
I was trying to get the same thing done and for me this works, but I'm not sure if it is the correct way as I'm still new to Drupal:
in "template.php" I added the following function:
function phptemplate_preprocess_page(&$vars)
{
$css = $vars['css'];
unset($css['all']['module']['modules/system/system.css']);
unset($css['all']['module']['modules/system/defaults.css']);
$vars['styles'] = drupal_get_css($css);
}
I think after adding the function you need to go to /admin/build/themes so that Drupal recognises the function."
The part in bold is what did the trick ... you have to re-save the configuration so it recognizes that you've added a new function to the template.