I want to modify the current template params via database outside joomla framework. I managed to set the 'preset' param, but I can't write it back to the database. Here is my code.
Thanks a lot
// Initialize The Joomla Framework
// -----------------------------------------------------------------------------------
define('_JEXEC', 1);
// this is relative to the current script, so change this according to your environment
define('JPATH_BASE', '/home/kristof/public_html/joomla1');
define('DS', DIRECTORY_SEPARATOR);
// Require Joomla libraries
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_CONFIGURATION . DS . 'configuration.php');
require_once(JPATH_LIBRARIES . DS . 'joomla' . DS . 'database' . DS . 'database.php');
require_once(JPATH_LIBRARIES . DS . 'import.php');
// -----------------------------------------------------------------------------------
$app = JFactory::getApplication('site');
$template = $app->getTemplate(true);
$param=$template->params->set('preset','preset3');
$template = JFactory::getApplication('site')->getTemplate();
$db = JFactory::getDBO();
$sql = "select params from #__template_styles where template = ".$db->quote($template);
$db->setQuery($sql);
$params = json_decode($db->loadResult());
$params->PARAM_NAME= PARAM_VALUE;
$sql = "update #__template_styles set params = ".$db->quote(json_encode($params))." where template = ".$db->quote($template);
$db->setQuery($sql);
return $db->query();
Related
Is there a quick fire method of accessing all the data for a product from an external file by using its sku?
I have looked at How can I get prodcut data of a VirtueMart 2 product in an external file?
and tried to replace the Product ID with my desired id but to no avail:
if (!class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'config.php');
VmConfig::loadConfig();
if (!class_exists( 'VmModel' )) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart'.DS.'helpers'.DS.'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct(Product_ID);
In short terms, I'm looking for a way to access product data.
Product sku may not be unique. so I have found a solution that works for me. Hope this helps you. This is the full code that you need to call product by sku externally.
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', 'C:\Server\www\joomla' );//you will have diff location for your site
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();
function getproductBySKU($sku){
$db = JFactory::getDbo();
$db->setQuery(true);
$db->setQuery("SELECT virtuemart_product_id FROM #__virtuemart_products WHERE product_sku= $sku");
$productids = $db->loadAssocList();
return $productids;
}
function getProduct($id)
{
if (!class_exists('VmConfig')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'config.php');
VmConfig::loadConfig();
if (!class_exists('VmModel')) require(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'vmmodel.php');
$productModel = VmModel::getModel('Product');
$product = $productModel->getProduct($id);
return $product;
}
$products = getproductBySKU(1);//In this example SKU is 1 having 2 products
var_dump($products);//Gives the product id's in the SKU
foreach($products as $product){
var_dump(getProduct($product));
}
I just want to know if I can create Product block in local code pool (i.e. \app\code\local\Mage\Catalog\Block\Product.php) without making my custom module just place this single file?
If so, will this local code pool block call or the core one call? If it would be the local one, please let me know why.
If you copy a code/core file to the code/local repository, the core file will be overwritten by the local file.
This is because of the include path order to load system files specified in app/Mage.php:
$paths = array();
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
$paths[] = BP . DS . 'lib';
So in your case the system will search for the Product.php in following order:
app/code/local/Mage/Catalog/Block/Product.php
app/code/community/Mage/Catalog/Block/Product.php
app/code/core/Mage/Catalog/Block/Product.php
lib/Mage/Catalog/Block/Product.php
If the system cannot find any of these files, it will throw an error.
This used to work in 1.5... I could set up the following code:
function startJoomla() {
define('_JEXEC', true);
define( 'DS', DIRECTORY_SEPARATOR );
$dir= dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_LIBRARIES . DS . 'loader.php';
jimport('joomla.base.object');
jimport('joomla.factory');
jimport('joomla.filter.filterinput');
jimport('joomla.error.error');
jimport('joomla.event.dispatcher');
jimport('joomla.event.plugin');
jimport('joomla.plugin.helper');
jimport( 'joomla.utilities.utility' );
jimport('joomla.utilities.arrayhelper');
jimport('joomla.environment.uri');
jimport('joomla.environment.request');
jimport('joomla.user.user');
// JText cannot be loaded with jimport since it's not in a file called text.php but in methods
JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
$mainframe = & JFactory::getApplication('site');
$GLOBALS['mainframe'] = & $mainframe;
return $mainframe;
}
$mainframe = startJoomla();
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
And then, when I wanted to call something up from Joomla, I could do the following:
$custom = new CustomController(); // assuming there's a com_custom somewhere
This worked for 1.5. But in 2.5, the above doesn't work at all. I found a revised version of the load script:
function startJoomla() {
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
$dir = dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE.'/includes/defines.php';
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
return $app->initialise();
}
$mainframe = startJoomla();
This enables things like 'JFactory::getUser();' but will no longer allow me to utilize commands like 'new MycustomcomponentController();' Gives me 'core' Joomla functionality, but skips everything I installed. Is there an appropriate new revised compilation of jimport instructions or perhaps a different function of JComponent that allows me to call up my custom functions along with other components and models for use in external php files? Assume this will be run from a CRON or similar.
I have created external php file inside my module and there i have use some sql queries , first i have tried using php/mysql and it works , then i tried to make it convert to joomla style . but when i use joomla framework to db connection gives errors
Old code: FROM PHP
mysql_connect("localhost","root","");
mysql_select_db("1234");
$searchp=$_GET["term"];
$query=mysql_query("SELECT * FROM sltdb_cddir_content where title like '%".$searchp."%'AND categories_id=82 order by title ASC ");
$json=array();
while($display=mysql_fetch_array($query)){
$json[]=array(
'value'=> $display["title"],
'label'=>$display["title"]
);
}
echo json_encode($json);
New Code : JOOMLA3
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
//create application
$mainframe = &JFactory::getApplication('site');
$db = JFactory::getDBO();
// Create a new query object.
$query = $db -> getQuery(true);
$searchp = $_GET["term"];
$query -> select($db -> quoteName(array('title')));
$query -> from($db -> quoteName('sltdb_cddir_content'));
$query -> where($db -> quoteName('title') . ' LIKE ' . $db -> quote('\'$searchp.%\''));
$query -> order('ordering ASC');
$db -> setQuery($query);
$json = array();
while ($display = mysql_fetch_array($query)) {
$json[] = array('value' => $display["title"], 'label' => $display["title"]);
}
echo json_encode($json);
once after converting to the code in joomla its given a error
*"mysql_fetch_array() expects parameter 1 to be resource, object given in "*
Please advice me where i have done incorrect .
EDIT 01
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
//create application
$mainframe = &JFactory::getApplication('site');
$searchp = $_GET["term"];
$db = JFactory::getDBO();
// Create a new query object.
$query = $db -> getQuery(true);
$query -> select($db -> quoteName(array('title')));
$query -> from($db -> quoteName('sltdb_cddir_content'));
$query -> where($db -> quoteName('title') . ' LIKE ' . $db -> quote('\'$searchp.%\''));
$query->where($db->quoteName('categories_id')." = ".$db->quote(82));
$query -> order('ordering ASC');
$db->setQuery($query);
$results = $db-> loadAssocList();
$json = array();
foreach($results as $json_result) {
$json[] = array('value' => $json_result["title"], 'label' => $json_result["title"]) ;
}
echo json_encode($json);
You are mixing using Joomla's "SDK" with pure PHP methods for data access, so I would recommend stay away from that mix:
1) mysql_fetch_array its going to be depreacated, and indeed it expects the 1st parameter to be a mysql connection created with a mysql_connect call, that's why the error says that the 1st parameter expects to be a resource, you can check the documentation in here http://www.php.net/mysql_fetch_array.
2) Since you are using Data Access classes from joomla to obtain the connection and build the query, which is fine, I would recommend keep using it for obtaining the results and looping through them, like this.
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of associated arrays.
$results = $db-> loadAssocList();
$json = array();
foreach($results as $json_result) {
$json[] = array('value' => $json_result["title"], 'label' => $json_result["title"])
}
More about Joomla's DB access here: http://docs.joomla.org/Selecting_data_using_JDatabase
Made a few tweaks to your database query and replaced $_GET with the correct Joomla method
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . DS . '');
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once (JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
$app = JFactory::getApplication();
$jinput = $app->input;
$db = JFactory::getDbo();
$searchp = $jinput->get('term', null, null);
$id = 82;
$query = $db->getQuery(true);
$query->select($db->quoteName('title'))
->from($db->quoteName('sltdb_cddir_content'))
->where($db->quoteName('title') . ' LIKE ' . $db->quote($searchp))
->where($db->quoteName('categories_id') . ' = ' . $db->quote((int) $id))
->order('ordering ASC');
$db->setQuery($query);
Let me know if it works
Use
echo $query->dump();
To see what the generated query looks like and you can then test the query directly.
You need to connect to the database ... is it the same or different? If it is the same you can do
// Creating the database connection.
$this->db = JDatabase::getInstance(
array(
'driver' => $config->dbtype,
'host' => $config->host,
'user' => $config->user,
'password' => $config->password,
'database' => $config->db,
'prefix' => $config->dbprefix,
)
);
But if it is a different databas you'll need to supply the connection data in some other way.
I have created a rule in Jomsocial to award points for member who invites non users.. But now the problem is that...The points are awarded even
when the member enters "invite friends" page
points awarded when member sent email to a user(never checks whether user is a member in network or not)
How can I restrict this?
I need to award points only when the email is sent to a "non-user" or when the non-user clicks the link in the email body.
Currently this is used in components/com_community/libraries/mailq.php inside the function:
public function send( $total = 100 )
{
$mailqModel = CFactory::getModel( 'mailq' );
$userModel = CFactory::getModel( 'user' );
$mails = $mailqModel->get( $total, true );
$jconfig = JFactory::getConfig();
$mailer = JFactory::getMailer();
$config = CFactory::getConfig();
$senderEmail = $jconfig->getValue('mailfrom');
$senderName = $jconfig->getValue('fromname');
The code below is used to award points. I think some more conditions need to be added to make it validated:
if($senderName)
{
$JomSocialCheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php';
if ( file_exists($JomSocialCheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
CuserPoints::assignPoint('com_user.add.friend');
}
}
My grandpa used to say: "when you're shouting - I hear you, when you're talking - I'm listening..." ;)
One of the two problems, I believe you can solve by modifying your code as follows:
if($senderName)
{
$JomSocialCheck = JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php';
if ( file_exists($JomSocialCheck)) {
include_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'libraries' . DS . 'userpoints.php');
$user =& JFactory::getUser();
if($user->id) {
// he's already a user - do nothing
}
else {
CuserPoints::assignPoint('com_user.add.friend');
}
}
}