File: /home/centralexf/www/plugins/system/plg_jausersetting/helper.php
<?php
/*
# ------------------------------------------------------------------------
# JA Usersetting plugin for Joomla 1.5
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license - PHP files are GNU/GPL V2. CSS / JS are Copyrighted Commercial,
# bound by Proprietary License of JoomlArt. For details on licensing,
# Please Read Terms of Use at http://www.joomlart.com/terms_of_use.html.
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com
# Redistribution, Modification or Re-licensing of this file in part of full,
# is bound by the License applied.
# ------------------------------------------------------------------------
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
/**
* JAUserSettingHelper class
*/
class JAUserSettingHelper {
/**
* get store name which contain prefix and module's name
*
* @param string $moduleName
* @return string.
*/
function getStoreName( $moduleName ){
return 'ja_usersetting_' . $moduleName;
}
function resetSetting () {
$user = & JFactory::getUser();
// if user not setting.
$modules = JModuleHelper::_load();
foreach ($modules as $module) {
$storeName = JAUserSettingHelper::getStoreName( $module->module );
if(!$user->id){
setcookie( $storeName, '', time(), '/' );
$_COOKIE [$storeName] = '';
} else {
$user->setParam($storeName, '');
}
}
if ($user->id) $user->save();
}
/**
* Save user setting in the datasource: cookie or database
*
* @param string $moduleName.
*/
function saveSetting( $moduleName ){
// require group setting
$groupName = JRequest::getVar('group', 'default', 'post', 'string');
if( $groupName == '' ) return ;
$user = & JFactory::getUser();
$storeName = JAUserSettingHelper::getStoreName( $moduleName );
unset($_POST['group']);
$data = array( $groupName => $_POST );
//if user do not login then store data in the cookie
if(!$user->id){
// if exist last data setting.
if( !isset($_COOKIE[$storeName]) || empty($_COOKIE[$storeName]) ){
$string = base64_encode(serialize($data));
setcookie( $storeName, $string, time()+3600*24*1000, '/' );
$_COOKIE [$storeName] = $string;
} else {
$tmp = unserialize( base64_decode($_COOKIE[$storeName]) );
// update new data setting.
$tmp = JAUserSettingHelper::arrayMerge( $tmp, $data, $groupName );
setcookie( $storeName, base64_encode(serialize($tmp)), time()+3600*24*1000, '/' );
$_COOKIE [$storeName] = base64_encode(serialize($tmp));
}
} else {
// get last data setting.
$uParams = $user->getParameters();
$string = $uParams->get( $storeName );
$tmp = unserialize( base64_decode($string) );
if( is_array($tmp) ) {
$data = JAUserSettingHelper::arrayMerge( $tmp, $data, $groupName );
}
// store data into user's params.
$string = base64_encode( serialize($data) );
//$userModel = JAUserSettingHelper::getUserModel();
//$data = array( 'params'=> $storeName . "=" . $string );
//$userModel->store( $data );
$user->setParam ($storeName, $string);
$user->save();
}
}
/**
* Get user setting data from datasource.
*
* @param $string $moduleName name of module.
* @return array.
*/
function getSetting( $moduleName ){
$user = & JFactory::getUser();
$storeName = JAUserSettingHelper::getStoreName( $moduleName );
// if user not setting.
if(!$user->id){
$string = isset($_COOKIE[$storeName]) ? $_COOKIE[$storeName] : '';
} else {
$string = $user->getParam( $storeName );
}
return (trim($string) != '') ? unserialize( base64_decode($string) ) : array();
}
/**
* get data setting from data source.
*
* @param string $group
* @params string $moduleName.
* @return array;
*/
function getData($group, $moduleName) {
$data = JAUserSettingHelper::getSetting( $moduleName );
if(isset($data[$group])){
return $data[$group];
}
return ;
}
/**
* get user model, it process storing data in the users table.
*
* @return UserModelUser;
*/
function getUserModel(){
if( class_exists('UserModelUser') ){
return new UserModelUser();
} else {
require_once( JPATH_ROOT. DS .'components' . DS. 'com_user' . DS . 'models'. DS . 'user.php' );
return new UserModelUser();
}
}
/**
* simple merge data from tow array by a centain group.
*
* @param array $array1 is array orginal
* @param array $array2 value of this will override value of $array1.
* @param string $groupName.
* @return array return $array1.
*/
function arrayMerge( $array1, $array2, $groupName ){
foreach( $array1 as $key => $out ){
if( isset($data[$key]) ){
$array1[$key] = $array2[$key];
}else {
$array1[$groupName] = $array2[$groupName];
}
}
return $array1;
}
}
?>