Load Joomla K2 module in an external php - php

Hi I'm trying to load a K2 content module in an external php file. I found some info and examples about this but the problem it's that it is only working with some Joomla modules (custom html and others) but not with the k2 modules. This is mi code:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__) .'' ) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS. 'includes' .DS. 'defines.php' );
require_once ( JPATH_BASE .DS. 'includes' .DS. 'framework.php' );
$app = JFactory::getApplication('site');
$app->initialise();
$modules = JModuleHelper::getModules("contenedor-eventos");
$document = JFactory::getDocument();
$attribs = array();
$attribs['style'] = 'xhtml';
foreach ($modules as $mod)
{
echo JModuleHelper::renderModule($mod, $attribs);
}
I have this error message: Fatal error: Class 'K2Model' not found in /home/my_user/my_domain/modules/mod_k2_content/helper.php on line 289
The code in helper.php line 289 is:
$model = K2Model::getInstance('Item', 'K2Model');
Hope someone could help me.
Thanks in advance.

Related

Weird behaviour from joomla and virtuemart regarding cart data

So I wrote up this code to display the cart total outside of joomla framework:
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
$total += $product[quantity];
}
echo $total;
?>
which works fine (by displaying the total items in cart) in a top level directory (/public_html/test.php)
but if I move it to a second-level directory, like (/public_html/includes/test.php), I get this error:
first, the code (notice the /../store because we're in a second-level now):
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
if (!class_exists( 'VmConfig' )) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if(!class_exists('VirtueMartCart')) require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart(false);
$data = $cart->prepareAjaxData();
$total = 0;
foreach ($data->products as $product){
$total += $product[quantity];
}
echo $total;
?>
then the error:
Fatal error: Uncaught exception 'Exception' with message 'XML file did not load' in /home/me/public_html/store/libraries/joomla/form/form.php:2020 Stack trace: #0 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmplugin.php(201): JForm::getInstance('weight_countrie...', '/home/me/publ...', Array, false, '//vmconfig | //...') #1 /home/me/public_html/store/administrator/components/com_virtuemart/plugins/vmpsplugin.php(45): vmPlugin::getVarsToPushByXML('/home/me/publ...', 'weight_countrie...') #2 /home/me/public_html/store/plugins/vmshipment/weight_countries/weight_countries.php(44): vmPSPlugin->getVarsToPush() #3 /home/me/public_html/store/libraries/joomla/plugin/helper.php(194): plgVmShipmentWeight_countries->__construct(Object(JDispatcher), Array) #4 /home/me/public_html/store/libraries/joomla/plugin/helper.php(125): JPluginHelper::_import(Object(stdClass), true, NULL) #5 /home/me/public_html/store/administrator/components/com_virtuemart/helpe in /home/me/public_html/store/libraries/joomla/form/form.php on line 2020
I have no clue why it works fine in the top level but then not in subdirectories. Any ideas?
When you are going to subdirectory the path changes. I got these paths for the defined JPATH_BASE.
For top level
string '/Applications/MAMP/htdocs/store';
And for subdirectory
string '/Applications/MAMP/htdocs/test/../store';
As dirname(realpath(FILE) will give the location of current directory it is in.
So there are two methods to get correct path
JPATH_BASE can be given the exact location like
/var/www/store for linux system
/Applications/MAMP/htdocs/store for MAC
C:/xampp/htdocs/www/store for windows
example
define('JPATH_BASE', '/var/www/store' );
These are just examples.
It can also be achieved this way by defining JPATH_BASE this way
Replace
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
BY
$path=getcwd();
$parts = explode(DIRECTORY_SEPARATOR, $path);
$pop = array_pop($parts);
$path = implode(DIRECTORY_SEPARATOR, $parts);
define('JPATH_BASE', $path.'/store');

Displaying cart total outside of joomla framework

I've been struggling with this.
I've got my joomla/virtuemart running in a directory called "store" and I wish to display the total number of products in the cart outside of the joomla framework. So I've come up with this code, which works fine with older versions of virtuemart ( < v3). However, when trying it with Virtuemart 3.0.16, I'm running into these problems:
The code (which is in a directory called "includes")
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
$array = unserialize($_SESSION['__vm']['vmcart']);
$total = 0;
foreach($array->products as $product){
$total += $product->amount;
}
echo "" . $total;
?>
And the error:
Warning: Invalid argument supplied for foreach() in
/home/me/public_html/includes/header.php on line 20
0
where 0 represents the cart total, which should show 1, since at the time I had an item in cart
I'm not a php expert, but open googling that, it seems to me that my $array isn't an array which is causing the problems?
This is really confusing me since it's working fine in older version of virtuemart.
--
Given the answer below, I am trying to run this:
<?php
// Set flag that this is a parent file
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(realpath(__FILE__)). '/../store' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
jimport('joomla.application.module.helper');
jimport('joomla.application.component.helper');
$mainframe = JFactory::getApplication('site');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
if (!class_exists( 'VmConfig' ))
require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
if(!class_exists('VirtueMartCart'))
require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
echo sizeof($cart->products);
?>
...but without success. It continually says 0
Try this,
after loading Joomla framework just use below codes it should work
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
if (!class_exists( 'VmConfig' ))
require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
if(!class_exists('VirtueMartCart'))
require(VMPATH_SITE.DS.'helpers'.DS.'cart.php');
$cart = VirtueMartCart::getCart();
if(sizeof($cart->products) > 0){
echo "<pre/>";
print_r($cart);
}
else{
echo 'Your cart is empty';
}
The array have all the cart item details just choose what ever you want.
Hope it helps.

Joomla 3 load template in external file

I have created an external file. I want to load joomla default template in that file. I used below code, and it caused an error:
Fatal error: Call to protected method JApplicationSite::render() from
context ' '
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', realpath(dirname(__FILE__) ));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');
JPluginHelper::importPlugin('system');
$mainframe->initialise();
$myContent = "Hello World!!";
$document = JFactory::getDocument();
$document->setBuffer($myContent, 'component');
$mainframe->render();
echo $mainframe;
At the least you need to let the Joomla app know which template you'd like to render.
//normal external script initialisation
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath('/home/somepath/public_html/somedirectory/'));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
require_once( JPATH_LIBRARIES .DS.'joomla'.DS.'factory.php');
$app = JFactory::getApplication('site');
$app->initialise();
//jimport( 'joomla.document.html.html' ); required in certain cases
//initialise the doc to use the Html renderer
$doc = new JDocumentHtml();
//minimal info required
$options['directory'] = "/my/template/directory";
$options["template"] = $app->getTemplate();//get the template name
$options["file"] = "index.php";//usually
//Optional: Manually set params
//$params = new JRegistry();
//$params->colorVariation = "blue";
//$params->widthStyle = "fluid";
//$params->showComponent = "1";
//$options["params"] = $params;
//you may initialise the document object here and set the buffers
//$document = JFactory::getDocument();
//$document->setBuffer($contents, 'module', $name);
// $document->setHeadData($data['head']);etc
echo $doc->render(false, $options);
$app->close();
Of course dont expect it to display the same way esp if you use relative urls. Can be used for other purposes though like examining the scripts in a document template. tested on Joomla 3.3 PHP 5.5 . It will not work on lower versions like 1.5.

Accessing Joomla Enviroment script fails if not in root

I'm a newbie but trying to learn.
I have a script to read the joomla user details. It's a simple script just to display logged in user name. The script works if I put it in the root directory of my website.
What I want to do is run the script from a folder off the root, /ssscart, for example.
When I put the script in the sub-directory it does not execute.
Here is my script:
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
$joomla_name = $user->name;
$joomla_email = $user->email;
$joomla_password = $user->password;
$joomla_username = $user->username;
echo 'hello';
echo " - ".$joomla_username;
?>
I believe my problem is in the JPATH_BASE entry but haven't been able to figure it out yet.
Thanks
G
Try this,
The is issue is due to JPATH_BASE path getting wrongly.
when you are in root it will look like.
define('JPATH_BASE', dirname(__FILE__) );
So in your case you are in sub folder so it will be look like.
define('JPATH_BASE', dirname(dirname(__FILE__)) );
so your final code will be look like.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)) );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
Hope it works..

Loading module external page Joomla

I am trying to load a module on an external page from the Joomla core.
I am using the following:
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__).'/../../..' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('xxxx', 'xxxxxxx');
$memberships = JModuleHelper::renderModule($module);
But get the following error
JPath::check Use of relative paths not permitted
Any ideas

Categories