diff --git a/plugins/user-preferences/KTUserPreferences.php b/plugins/user-preferences/KTUserPreferences.php index 35c9cf8..91f8359 100644 --- a/plugins/user-preferences/KTUserPreferences.php +++ b/plugins/user-preferences/KTUserPreferences.php @@ -37,6 +37,7 @@ require_once('UserPreferences.inc.php'); */ class KTUserPreferences { + private $oUserPreference; /** * Constructor function for the class @@ -47,14 +48,54 @@ class KTUserPreferences */ public function __construct() { + $this->oUserPreference = new UserPreferences(); } /** - * Check if user is logged in + * * * @author KnowledgeTree Team * @access public - * @return html + * @return + */ + function saveUserPreferences() { + $iUserId = KTUtil::arrayGet($_GET, 'user_id', $_SESSION['userID']); + $sKey = KTUtil::arrayGet($_GET, 'key', null); + $sValue = KTUtil::arrayGet($_GET, 'value', null); + if(is_null($iUserId) || is_null($sKey) || is_null($sValue)) { + exit("Missing Required options : user_id = $iUserId key = $sKey value = $sValue"); + } + $this->oUserPreference->saveUserPreferences($iUserId, $sKey, $sValue); + } + + /** + * + * + * @author KnowledgeTree Team + * @access public + * @return + */ + function getUserPreferences() { + + } + + /** + * + * + * @author KnowledgeTree Team + * @access public + * @return + */ + function deleteUserPreferences() { + + } + + /** + * + * + * @author KnowledgeTree Team + * @access public + * @return boolean */ function isLoggedIn() { $session = new Session(); @@ -73,16 +114,9 @@ if (!$oKTUserPreferences->isLoggedIn()) { exit; } -switch($_GET['action']){ - case 'getUserPreferences': - - break; - case 'addUserPreferences': - - break; - default: - echo "No action defined"; - break; +if(isset($_GET['action'])) { + $action = $_GET['action']; + $oKTUserPreferences->$action(); } exit; diff --git a/plugins/user-preferences/UserPreferences.inc.php b/plugins/user-preferences/UserPreferences.inc.php index 98df0d3..86b2d52 100644 --- a/plugins/user-preferences/UserPreferences.inc.php +++ b/plugins/user-preferences/UserPreferences.inc.php @@ -75,7 +75,16 @@ class UserPreferences extends KTEntity { return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions); } - public function getUserPreferences($iUserId, $sKey, $aOptions = null) { + /** + * + * + * @author KnowledgeTree Team + * @access public + * @param + * @param + * @return + */ + public function getPreferences($iUserId, $sKey, $aOptions = null) { $sWhereClause = "WHERE user_id = '$iUserId' AND prefkey = '$sKey'"; return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions); @@ -154,16 +163,30 @@ class UserPreferences extends KTEntity { // Utility /** - * Set the template name + * * * @author KnowledgeTree Team * @access public - * @param $sName - string - the template node name - * @param $iParentId - int - the template id - * @return boolean + * @param + * @param + * @param + * @return */ public function exists($iUserId, $sKey, $sValue) { - return UserPreferencesUtil::userPreferenceExists($iUserId, $sKey, $sValue); + $sQuery = "SELECT id, name FROM " . KTUtil::getTableName('user_preferences') . " WHERE user_id = ? AND prefkey = ? AND prefvalue = ?";/*ok*/ + $aParams = array($iUserId, $sKey, $sValue); + $res = DBUtil::getResultArray(array($sQuery, $aParams)); + if (count($res) != 0) { + foreach ($res as $user_pref){ + $userid = isset($user_pref['user_id']) ? $user_pref['user_id'] : ''; + $key = isset($user_pref['prefkey']) ? $user_pref['prefkey'] : ''; + if($sKey == $key && $iUserId == $userid) { + return true; + } + } + return false; + } + return false; } /** @@ -180,33 +203,54 @@ class UserPreferences extends KTEntity { $sWhereClause = "WHERE user_id = '$userId'"; return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions); } -} - -class UserPreferencesUtil { - - /** - * - * - * @author KnowledgeTree Team - * - * @return - */ - function userPreferenceExists($iUserId, $sKey, $sValue) { - $sQuery = "SELECT id, name FROM " . KTUtil::getTableName('user_preferences') . " WHERE user_id = ? AND prefkey = ? AND prefvalue = ?";/*ok*/ - $aParams = array($iUserId, $sKey, $sValue); - $res = DBUtil::getResultArray(array($sQuery, $aParams)); - if (count($res) != 0) { - foreach ($res as $user_pref){ - $userid = isset($user_pref['user_id']) ? $user_pref['user_id'] : ''; - $key = isset($user_pref['prefkey']) ? $user_pref['prefkey'] : ''; - if($sKey == $key && $iUserId == $userid) { - return true; - } - } - return false; + + /** + * + * + * @author KnowledgeTree Team + * @access public + * @param $aOptions - array + * @return + */ + public function getUserPreferenceValue($iUserId, $sKey) { + $aPref = UserPreferences::getPreferences($iUserId, $sKey); + if(PEAR::isError($aPref)) { + return false; + } + if(count($aPref) > 1) { + return false; + } + + foreach ($aPref as $oPref) { + return $oPref->getValue(); + } + } + + /** + * + * + * @author KnowledgeTree Team + * @access public + * @param $aOptions - array + * @return + */ + public function saveUserPreferences($iUserId, $sKey, $sValue) { + $oUser = User::get($iUserId); // Get the user + if (PEAR::isError($oUser)) { + return false; + } + $aUserPreference = UserPreferences::getPreferences($iUserId, 'zohoWarning'); // Get user preference + if(empty($aUserPreference) || is_null($aUserPreference)) { // Create the preference + $oUserPreference = new UserPreferences($iUserId, 'zohoWarning', $sValue); + $oUserPreference->create(); + } else { + foreach ($aUserPreference as $oUserPreference) { // Access object + if($oUserPreference->getValue() != $sValue) { // Check if value needs to be updated + $oUserPreference->setValue($sValue); // Set the new value + $oUserPreference->update(); // Update preference + } + } } - return false; - } + } } - ?> diff --git a/plugins/user-preferences/UserPreferencesPlugin.php b/plugins/user-preferences/UserPreferencesPlugin.php index 95e9650..56fd31e 100644 --- a/plugins/user-preferences/UserPreferencesPlugin.php +++ b/plugins/user-preferences/UserPreferencesPlugin.php @@ -20,26 +20,24 @@ * */ -define('PLUGINDIR_UserPreferencesPlugin', dirname(__FILE__)); +define('UserPreferences_PluginDir', dirname(__FILE__)); +$start = strpos(dirname(__FILE__), 'plugins'); +$path = substr(dirname(__FILE__), $start); +$path = str_replace("\\", "/", $path); +$file = $path.'/KTUserPreferences.php'; +define('UserPreferencesPlugin_KTFile', $file); + /* */ require_once("UserPreferences.inc.php"); /* Plugin Base */ require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); -require_once(KT_LIB_DIR . '/database/sqlfile.inc.php'); -require_once(KT_LIB_DIR . '/database/dbutil.inc'); - -/* Folder Actions */ -require_once(KT_LIB_DIR . '/actions/folderaction.inc.php'); -require_once(KT_LIB_DIR . '/permissions/permission.inc.php'); -require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php'); -require_once(KT_LIB_DIR . '/browse/browseutil.inc.php'); class UserPreferencesPlugin extends KTPlugin { - public $sNamespace = 'up.UserPreferencesPlugin.plugin'; + public $sNamespace = 'user.preferences.plugin'; public $iVersion = 0; public $autoRegister = true; - //public $showInAdmin = false; + public $showInAdmin = false; /** * User Preferences constructor @@ -50,94 +48,13 @@ class UserPreferencesPlugin extends KTPlugin { function UserPreferencesPlugin($sFilename = null) { parent::KTPlugin($sFilename); $this->sFriendlyName = _kt('User Preferences Plugin'); - $this->sSQLDir = PLUGINDIR_UserPreferencesPlugin . DIRECTORY_SEPARATOR. 'sql' . DIRECTORY_SEPARATOR; + $this->sSQLDir = UserPreferences_PluginDir . DIRECTORY_SEPARATOR. 'sql' . DIRECTORY_SEPARATOR; $this->dir = dirname(__FILE__); } - /** - * Basic plugin setup - * - * @param none - * @return none - */ - function setup() { - $this->registerAdminPage("adminuserpreferencesmanagement", - 'adminManageUserPreferencesDispatcher', - 'misc', - _kt('User Preferences'), - _kt('User Preferences'), - 'manageUserPreferences.php', - null); - $this->registerPage('userpreferencesmanagement', 'ManageUserPreferencesDispatcher', 'manageUserPreferences.php'); - $plugin_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR; - require_once(KT_LIB_DIR . '/templating/templating.inc.php'); - $oTemplating =& KTTemplating::getSingleton(); - $oTemplating->addLocation('UserPreferencesPlugin', $plugin_dir.'templates', 'fs.UserPreferencesPlugin.plugin'); - $this->applySQL(); // Create Table - } - - function applySQL() - { - $sql = "select * from user_preferences"; - $result = DBUtil::getResultArray($sql); - - if (!PEAR::isError($result)) - { - return; // if we find the table, we assume it has been applied - } - $filename = $this->sSQLDir . 'user_preferences.sql'; - $content = file_get_contents($filename); - - global $default; - DBUtil::setupAdminDatabase(); - $db = $default->_admindb; - $aQueries = SQLFile::splitSQL($content); - DBUtil::startTransaction(); - $res = DBUtil::runQueries($aQueries, $db); - if (PEAR::isError($res)) { - DBUtil::rollback(); - return $res; - } - DBUtil::commit(); - } - - /** - * Method to setup the plugin on rendering it - * - * @param none - * @return boolean - */ - function run_setup() { - - return true; - } - - /** - * Register the plugin - * - * @return unknown - */ - function register() { - $oEnt = parent::register(); - return $oEnt; - } - - public function getUserPreferences($iUserId, $sKey) { - $aPref = UserPreferences::getUserPreferences($iUserId, $sKey); - if(PEAR::isError($aPref)) { - return false; - } - if(count($aPref) > 1) { - return false; - } - - foreach ($aPref as $oPref) { - return $oPref->getValue(); - } - } } $oPluginRegistry =& KTPluginRegistry::getSingleton(); -$oPluginRegistry->registerPlugin('UserPreferencesPlugin', 'up.UserPreferencesPlugin.plugin', __FILE__); +$oPluginRegistry->registerPlugin('UserPreferencesPlugin', 'user.preferences.plugin', __FILE__); ?> \ No newline at end of file diff --git a/plugins/user-preferences/sql/upgradeto0.sql b/plugins/user-preferences/sql/upgradeto0.sql new file mode 100644 index 0000000..d3036c8 --- /dev/null +++ b/plugins/user-preferences/sql/upgradeto0.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS `user_preferences` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(11) unsigned NOT NULL, + `prefkey` varchar(255) NOT NULL, + `prefvalue` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; \ No newline at end of file