Use Joomla Classes within my php file - joomla-extensions

I have a joomla site and need an xml generating php script to run within it. The file is called by my module. The module works ok but I cannot get to create a php file that outputs the code, primarily because I can't use things like$db =& JFactory::getDBO();
Anyone with an experience on this front?

If you are calling a file that is outside of the joomla framework, then you need to create an instance of the joomla application so the code has access to joomla.
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__) );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
This should give you access to the basics.

Related

Way to access ModX database by including ModX core file?

I have a PHP file (that I cant make a snippet out of). I need to connect it to the database and rather then doing it the normal way was wondering if I could just include a header file of some sort that already has a DB connection.
Does anyone know of such a file in the Modx file structure?
Easy...
if you open the main index.php, you can see some hint:
/* define this as true in another entry file, then include this file to simply access the API
* without executing the MODX request handler */
if (!defined('MODX_API_MODE')) {
define('MODX_API_MODE', false);
}
// ...
/* execute the request handler */
if (!MODX_API_MODE) {
$modx->handleRequest();
}
That said, if you have a raw PHP file, let's say as an example: hello.php
<?php
define('MODX_API_MODE', true); // IMPORTANT!!!
require 'index.php'; // or give directory path, according to your need
// let's test it
$startPage = $modx->getObject('modResource', $modx->getOption('site_start', null, 1));
if (!$startPage) {
die('CRAP!');
}
$startPageArray = $startPage->toArray();
echo '<pre>';
print_r($startPageArray);
echo '</pre>';
die('WOOHOO');
And no, you don't have to define $modx again.
It's using the same object in index.php.
you can run any operations against your modx database if you load the modx module in your external script [in fact you can use any modx functions]
https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/loading-modx-externally
once you instantiate the modx object it will handle all your database connection details. This will work in any page, not just manager pages.
The only place which allows database connections using system database config are manager pages, yet that would require more work to write a plugin to access its classes and functions.
If you want to be able to use MODX functionality to establish connection, I suggest using it's xPDO to perform queries, for security reasons at least.
Such setup would be:
define('MODX_CORE_PATH', '/path/to/revo/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$dbname = 'your_database';
$port = 3306;
$charset = 'utf-8';
$dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
$xpdo = new xPDO($dsn, $username, $password);

How to download MODX resources through database

I have a lot of TV's in my MODX and I want to understand how I can download (create) new resources (documents) through database?
Thank you!
Try to use modx API.
<?php
// put this file in a folder, let's say /raw/something.php
// load MODX without running request handler
define('MODX_API_MODE', true);
include 'path/to/modx/index.php';
$resource = $modx->newObject('modResource');
$resource->set('pagetitle', $pagetitle);
$resource->set('alias', $alias);
$resource->set('content', $content);
$resource->set('template', $template); // IMPORTANT! it's where TVs are attached
// and other $resource->set('fieldname', $variable);
if ($resource->save()) {
$resource->setTVValue('tvName_1', $value_1);
$resource->setTVValue('tvName_2', $value_2);
$resource->setTVValue('tvName_3', $value_3);
$resource->setTVValue('tvName_4', $value_4);
$resource->setTVValue('tvName_5', $value_5);
}

How to do navigation durandal.js?

i am trying to use durandal.js for single page architecture,
i already have application where i am loading all pages in div = old approach for single page architecture,
what i want to do is when i click on page i need to open hotspa pages,
for now i write something like this . www.xyz.com#/details,
where # details is my durandal view page!
when i put <a> herf ....#/details, i got error like this :
http://postimg.org/image/hoeu1wiz5/
but when i refresh with same url, it is working fine, i am able to see view!
i do not know why i got this error
If you are using anything before version 2.0 of Durandal, you are getting this because in your Shell.js you are not defining router, or you have a bad definition of where the router module is, or possibly you are defining scripts in your index instead of 'requiring them' via require.js
1st - Check shell.js, at the top you should have a define function and it should say / do something like this, and should be exposing that to the view like so -
define(['durandal/plugins/router'], function (router) {
var shell = {
router: router
};
return shell;
};
2nd - Check and make sure the 'durandal/plugins/router' is point to the correct location in the solution explorer, in this case it is app > durandal > plugins > router. If it is not or if there is no router you can add it using nuget.
3rd - Make sure you aren't loading scripts up in your index or shell html pages. When using require.js you need to move any scripts you are loading into a require statement for everything to function properly. The 'Mismatched anonymous define() module' error usually occurs when you are loading them elsewhere - http://requirejs.org/docs/errors.html#mismatch

CodeIgniter - htaccess - Redirect

I am currently working with "Code Igniter" and the "i18n Multi-language Library Helper". My website is bilingual.
The simple task I want to do is to force redirect of this path:
domain.com/inscription
to
domain.com/fr/inscription
I tried to do it with CI Route Engine, but it it not working properly because the route engine will redirect to the current language ( ex. domain.com/en/inscription ), which should not work. Only domain.com/fr/inscription should work.
I believe the best way to do it if with the htaccess file, but I can't get it to work.
If you are using Codeigniter 2.x try using this library
http://codeigniter.com/wiki/CodeIgniter_2.1_internationalization_i18n
Read the guide on how to set up the library and you can see there in MY_Lang.php file array like this. The first language in the array is the default one. So it will redirect you automatically to the default language
// languages
private $languages = array(
'en' => 'english',
'de' => 'german',
'fr' => 'french',
'nl' => 'dutch'
);
Hope this helps
Here is a code to redirect
# This allows you to redirect index.html to a specific subfolder
Redirect /inscription http://domain.com/fr/inscription
the line under # comment line is the code you need to paste

Magento custom currency saving?

I'm trying to get some currency exchange rates in a seperate php file in magento and saving them:
<?php
// Initiate application
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::app();
// Code to create my $rates array
/** CODE **/
foreach ($rates as $currencyCode => $currencyRates) {
Mage::getModel('directory/currency')
->setId($currencyCode)
->setRates($currencyRates)
->save();
}
Error:
<br />
<b>Fatal error</b>: Uncaught exception 'Mage_Core_Exception' with message 'Cannot retrieve entity config: directory/currency' in /home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/Mage.php:550
Stack trace:
#0 /home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/code/core/Mage/Core/Model/Resource.php(161): Mage::throwException('Cannot retrieve...')
#1 /home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/code/core/Mage/Core/Model/Mysql4/Abstract.php(265): Mage_Core_Model_Resource->getTableName('directory/curre...')
#2 /home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/code/core/Mage/Core/Model/Mysql4/Abstract.php(247): Mage_Core_Model_Mysql4_Abstract->getTable('currency')
#3 /home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/code/core/Mage/Core/Model/Mysql4/Abstract.php(402): Mage_Core_Model_Mysql4_Abstract->getMainTable()
#4 /home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/code/core/Mage/Core/Model/Abstract.php(306): Mage_Core_Model_Mysql4_Abstract->save( in <b>/home/users/A000456/shoppingonline.be/www.shoppingonline.be/app/Mage.php</b> on line <b>550</b><br />
I turns out to be the save()-method where this happens.
Thoughts? Not sure where to start debugging this. If I knew where the rates were being stored would also be ok so I'd could insert them by hand ...
(I took the code from the Mage_Directory_Model_Currency_Import_Abstract class, normal saving through magento interface works fine)
Seems like you're trying to save the directory/currency when you need to save a directory/currency_rate. Simplest way to do that is to use the saveRates method on the directory/currency model that you create. i.e.
Array
(
[USD] => Array
(
[CAD] => 1.07
[GBP] => .63
[EUR] => .71
)
)
$currency = Mage::getModel('directory/currency')->saveRates($currencies);
My first thought is that starting Magento in the context of another PHP file, but without all the fixins that Magento provides when bootstrapping itself, seems risky.
The path of least resistance is, with very few exceptions, do let Magento be Magento and do its thing, the less hacking the better. With that in mind, is it possible for you to set up a Magento controller/action and invoke this data from there, or even consume it remotely (e.g. using file_get_contents)?

Resources