Commit de4625695eee410ffa3d0206c4057444f156c06d

Authored by Jarrett Jordaan
1 parent 86e9dd1a

PT:2378610 : Added user preferences plugin..

Committed by: Jarrett Jordaan

Reviewed by: Megan Watson
plugins/Xpa1Rntal/user-preferences/KTUserPreferences.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Electronic Signatures
  4 + *
  5 + * $Id: $
  6 + *
  7 + * The contents of this file are subject to the KnowledgeTree
  8 + * Commercial Editions On-Premise License ("License");
  9 + * You may not use this file except in compliance with the License.
  10 + * You may obtain a copy of the License at
  11 + * http://www.knowledgetree.com/about/legal/
  12 + * The terms of this license may change from time to time and the latest
  13 + * license will be published from time to time at the above Internet address.
  14 + *
  15 + * This edition of the KnowledgeTree software
  16 + * is NOT licensed to you under Open Source terms.
  17 + * You may not redistribute this source code.
  18 + * For more information please see the License above.
  19 + *
  20 + * (c) 2008, 2009, 2010 KnowledgeTree Inc.
  21 + * All Rights Reserved.
  22 + *
  23 + */
  24 +
  25 +$start = strpos(dirname(__FILE__), 'plugins');
  26 +$filePath = substr(dirname(__FILE__), 0, $start);
  27 +
  28 +require_once($filePath.'config/dmsDefaults.php');
  29 +require_once('UserPreferences.inc.php');
  30 +
  31 +/**
  32 + * Class handles the document type alerts
  33 + *
  34 + * @author KnowledgeTree Team
  35 + * @package Alerts Plugin
  36 + * @version 1.0
  37 + */
  38 +class KTUserPreferences
  39 +{
  40 +
  41 + /**
  42 + * Constructor function for the class
  43 + *
  44 + * @author KnowledgeTree Team
  45 + * @access public
  46 + * @return KTDocTypeAlerts
  47 + */
  48 + public function __construct()
  49 + {
  50 + }
  51 +
  52 + /**
  53 + * Check if user is logged in
  54 + *
  55 + * @author KnowledgeTree Team
  56 + * @access public
  57 + * @return html
  58 + */
  59 + function isLoggedIn() {
  60 + $session = new Session();
  61 + $sessionStatus = $session->verify();
  62 + if ($sessionStatus !== true) {
  63 + return false;
  64 + }
  65 + return true;
  66 + }
  67 +}
  68 +
  69 +$oKTUserPreferences = new KTUserPreferences();
  70 +
  71 +if (!$oKTUserPreferences->isLoggedIn()) {
  72 + echo _kt('Session has expired. Refresh page and login.');
  73 + exit;
  74 +}
  75 +
  76 +switch($_GET['action']){
  77 + case 'getUserPreferences':
  78 +
  79 + break;
  80 + case 'addUserPreferences':
  81 +
  82 + break;
  83 + default:
  84 + echo "No action defined";
  85 + break;
  86 +}
  87 +
  88 +exit;
  89 +?>
0 90 \ No newline at end of file
... ...
plugins/Xpa1Rntal/user-preferences/UserPreferences.inc.php 0 → 100644
  1 +<?php
  2 +/*
  3 + * $Id: $
  4 + *
  5 + * The contents of this file are subject to the KnowledgeTree
  6 + * Commercial Editions On-Premise License ("License");
  7 + * You may not use this file except in compliance with the License.
  8 + * You may obtain a copy of the License at
  9 + * http://www.knowledgetree.com/about/legal/
  10 + * The terms of this license may change from time to time and the latest
  11 + * license will be published from time to time at the above Internet address.
  12 + *
  13 + * This edition of the KnowledgeTree software
  14 + * is NOT licensed to you under Open Source terms.
  15 + * You may not redistribute this source code.
  16 + * For more information please see the License above.
  17 + *
  18 + * (c) 2008, 2009, 2010 KnowledgeTree Inc.
  19 + * All Rights Reserved.
  20 + *
  21 + */
  22 +
  23 +$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..';
  24 +$dir = realpath($dir).DIRECTORY_SEPARATOR;
  25 +require_once(KT_LIB_DIR . "/ktentity.inc");
  26 +
  27 +class UserPreferences extends KTEntity {
  28 + public $iId; // primary key of current object
  29 + public $iUserId; // id of user
  30 + public $sKey; // description of user preference
  31 + public $sValue; // value of user preference
  32 +
  33 + public $_aFieldToSelect = array(
  34 + "iId" => "id",
  35 + "iUserId" => "user_id",
  36 + "sKey" => "prefkey",
  37 + "sValue" => "prefvalue",
  38 + );
  39 +
  40 + public $_bUsePearError = true;
  41 +
  42 + function UserPreferences($iUserId, $sKey, $sValue) {
  43 + $this->iId = -1;
  44 + $this->iUserId = $iUserId;
  45 + $this->sKey = $sKey;
  46 + $this->sValue = $sValue;
  47 + }
  48 +
  49 + /**
  50 + * Retrieve UserPreferences objects database table name
  51 + *
  52 + * @author KnowledgeTree Team
  53 + * @access public
  54 + * @param none
  55 + * @return string
  56 + */
  57 + function _table () { return KTUtil::getTableName('user_preferences'); }
  58 +
  59 + // ---------------
  60 + // Getters/setters
  61 + // ---------------
  62 + /**
  63 + * Retrieve a list of UserPreferences objects
  64 + *
  65 + * @author KnowledgeTree Team
  66 + * @access public
  67 + * @param $sWhereClause - string
  68 + * @param $aOptions - array
  69 + * @return UserPreferences objects - array
  70 + */
  71 + public function getList($sWhereClause = null, $aOptions = null) {
  72 + if (is_null($aOptions)) { $aOptions = array(); }
  73 + $aOptions['orderby'] = KTUtil::arrayGet($aOptions, 'orderby','name');
  74 +
  75 + return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions);
  76 + }
  77 +
  78 + public function getUserPreferences($iUserId, $sKey, $aOptions = null) {
  79 + $sWhereClause = "WHERE user_id = '$iUserId' AND prefkey = '$sKey'";
  80 +
  81 + return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions);
  82 + }
  83 +
  84 + /**
  85 + * Retrieve a UserPreferences object
  86 + *
  87 + * @author KnowledgeTree Team
  88 + * @access public
  89 + * @param $iId - int - Id of template
  90 + * @return UserPreferences object
  91 + */
  92 + public function get($iId) { return KTEntityUtil::get('UserPreferences', $iId); }
  93 +
  94 + /**
  95 + * Retrieve UserPreferences user id
  96 + *
  97 + * @author KnowledgeTree Team
  98 + * @access public
  99 + * @param none
  100 + * @return string
  101 + */
  102 + public function getUserId() { return $this->iUserId; }
  103 +
  104 + /**
  105 + * Set the user id
  106 + *
  107 + * @author KnowledgeTree Team
  108 + * @access public
  109 + * @param $iUserId - string - the user id
  110 + * @return none
  111 + */
  112 + public function setUserId($iUserId) { $this->iUserId = $iUserId; }
  113 +
  114 + /**
  115 + *
  116 + *
  117 + * @author KnowledgeTree Team
  118 + * @access public
  119 + * @param
  120 + * @return none
  121 + */
  122 + public function setKey($sKey) { $this->sKey = $sKey; }
  123 +
  124 + /**
  125 + *
  126 + *
  127 + * @author KnowledgeTree Team
  128 + * @access public
  129 + * @param none
  130 + * @return string
  131 + */
  132 + public function getKey() { return $this->sKey; }
  133 +
  134 + /**
  135 + *
  136 + *
  137 + * @author KnowledgeTree Team
  138 + * @access public
  139 + * @param
  140 + * @return none
  141 + */
  142 + public function setValue($sValue) { $this->sValue = $sValue; }
  143 +
  144 + /**
  145 + *
  146 + *
  147 + * @author KnowledgeTree Team
  148 + * @access public
  149 + * @param none
  150 + * @return string
  151 + */
  152 + public function getValue() { return $this->sValue; }
  153 +
  154 + // Utility
  155 +
  156 + /**
  157 + * Set the template name
  158 + *
  159 + * @author KnowledgeTree Team
  160 + * @access public
  161 + * @param $sName - string - the template node name
  162 + * @param $iParentId - int - the template id
  163 + * @return boolean
  164 + */
  165 + public function exists($iUserId, $sKey, $sValue) {
  166 + return UserPreferencesUtil::userPreferenceExists($iUserId, $sKey, $sValue);
  167 + }
  168 +
  169 + /**
  170 + *
  171 + *
  172 + * @author KnowledgeTree Team
  173 + * @access public
  174 + * @param $aOptions - array
  175 + * @return
  176 + */
  177 + public function getAllUserPreferences($userId, $aOptions = null) {
  178 + if (is_null($aOptions)) { $aOptions = array(); }
  179 + $aOptions['orderby'] = KTUtil::arrayGet($aOptions, 'orderby','name');
  180 + $sWhereClause = "WHERE user_id = '$userId'";
  181 + return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions);
  182 + }
  183 +}
  184 +
  185 +class UserPreferencesUtil {
  186 +
  187 + /**
  188 + *
  189 + *
  190 + * @author KnowledgeTree Team
  191 + *
  192 + * @return
  193 + */
  194 + function userPreferenceExists($iUserId, $sKey, $sValue) {
  195 + $sQuery = "SELECT id, name FROM " . KTUtil::getTableName('user_preferences') . " WHERE user_id = ? AND prefkey = ? AND prefvalue = ?";/*ok*/
  196 + $aParams = array($iUserId, $sKey, $sValue);
  197 + $res = DBUtil::getResultArray(array($sQuery, $aParams));
  198 + if (count($res) != 0) {
  199 + foreach ($res as $user_pref){
  200 + $userid = isset($user_pref['user_id']) ? $user_pref['user_id'] : '';
  201 + $key = isset($user_pref['prefkey']) ? $user_pref['prefkey'] : '';
  202 + if($sKey == $key && $iUserId == $userid) {
  203 + return true;
  204 + }
  205 + }
  206 + return false;
  207 + }
  208 + return false;
  209 + }
  210 +}
  211 +
  212 +?>
... ...
plugins/Xpa1Rntal/user-preferences/UserPreferencesPlugin.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * $Id: $
  4 + *
  5 + * The contents of this file are subject to the KnowledgeTree
  6 + * Commercial Editions On-Premise License ("License");
  7 + * You may not use this file except in compliance with the License.
  8 + * You may obtain a copy of the License at
  9 + * http://www.knowledgetree.com/about/legal/
  10 + * The terms of this license may change from time to time and the latest
  11 + * license will be published from time to time at the above Internet address.
  12 + *
  13 + * This edition of the KnowledgeTree software
  14 + * is NOT licensed to you under Open Source terms.
  15 + * You may not redistribute this source code.
  16 + * For more information please see the License above.
  17 + *
  18 + * (c) 2008 KnowledgeTree Inc.
  19 + * All Rights Reserved.
  20 + *
  21 + */
  22 +
  23 +define('PLUGINDIR_UserPreferencesPlugin', dirname(__FILE__));
  24 +/* */
  25 +require_once("UserPreferences.inc.php");
  26 +/* Plugin Base */
  27 +require_once(KT_LIB_DIR . '/plugins/plugin.inc.php');
  28 +require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php');
  29 +require_once(KT_LIB_DIR . '/database/sqlfile.inc.php');
  30 +require_once(KT_LIB_DIR . '/database/dbutil.inc');
  31 +
  32 +/* Folder Actions */
  33 +require_once(KT_LIB_DIR . '/actions/folderaction.inc.php');
  34 +require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
  35 +require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
  36 +require_once(KT_LIB_DIR . '/browse/browseutil.inc.php');
  37 +
  38 +class UserPreferencesPlugin extends KTPlugin {
  39 + public $sNamespace = 'up.UserPreferencesPlugin.plugin';
  40 + public $iVersion = 0;
  41 + public $autoRegister = true;
  42 + //public $showInAdmin = false;
  43 +
  44 + /**
  45 + * User Preferences constructor
  46 + *
  47 + * @param string $sFilename
  48 + * @return UserPreferencesPlugin
  49 + */
  50 + function UserPreferencesPlugin($sFilename = null) {
  51 + parent::KTPlugin($sFilename);
  52 + $this->sFriendlyName = _kt('User Preferences Plugin');
  53 + $this->sSQLDir = PLUGINDIR_UserPreferencesPlugin . DIRECTORY_SEPARATOR. 'sql' . DIRECTORY_SEPARATOR;
  54 + $this->dir = dirname(__FILE__);
  55 + }
  56 +
  57 + /**
  58 + * Basic plugin setup
  59 + *
  60 + * @param none
  61 + * @return none
  62 + */
  63 + function setup() {
  64 + $this->registerAdminPage("adminuserpreferencesmanagement",
  65 + 'adminManageUserPreferencesDispatcher',
  66 + 'misc',
  67 + _kt('User Preferences'),
  68 + _kt('User Preferences'),
  69 + 'manageUserPreferences.php',
  70 + null);
  71 + $this->registerPage('userpreferencesmanagement', 'ManageUserPreferencesDispatcher', 'manageUserPreferences.php');
  72 + $plugin_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  73 + require_once(KT_LIB_DIR . '/templating/templating.inc.php');
  74 + $oTemplating =& KTTemplating::getSingleton();
  75 + $oTemplating->addLocation('UserPreferencesPlugin', $plugin_dir.'templates', 'fs.UserPreferencesPlugin.plugin');
  76 + $this->applySQL(); // Create Table
  77 + }
  78 +
  79 + function applySQL()
  80 + {
  81 + $sql = "select * from user_preferences";
  82 + $result = DBUtil::getResultArray($sql);
  83 +
  84 + if (!PEAR::isError($result))
  85 + {
  86 + return; // if we find the table, we assume it has been applied
  87 + }
  88 + $filename = $this->sSQLDir . 'user_preferences.sql';
  89 + $content = file_get_contents($filename);
  90 +
  91 + global $default;
  92 + DBUtil::setupAdminDatabase();
  93 + $db = $default->_admindb;
  94 + $aQueries = SQLFile::splitSQL($content);
  95 + DBUtil::startTransaction();
  96 + $res = DBUtil::runQueries($aQueries, $db);
  97 + if (PEAR::isError($res)) {
  98 + DBUtil::rollback();
  99 + return $res;
  100 + }
  101 + DBUtil::commit();
  102 + }
  103 +
  104 + /**
  105 + * Method to setup the plugin on rendering it
  106 + *
  107 + * @param none
  108 + * @return boolean
  109 + */
  110 + function run_setup() {
  111 +
  112 + return true;
  113 + }
  114 +
  115 + /**
  116 + * Register the plugin
  117 + *
  118 + * @return unknown
  119 + */
  120 + function register() {
  121 + $oEnt = parent::register();
  122 +
  123 + return $oEnt;
  124 + }
  125 +
  126 + public function getUserPreferences($iUserId, $sKey) {
  127 + $aPref = UserPreferences::getUserPreferences($iUserId, $sKey);
  128 + if(PEAR::isError($aPref)) {
  129 + return false;
  130 + }
  131 + if(count($aPref) > 1) {
  132 + return false;
  133 + }
  134 +
  135 + foreach ($aPref as $oPref) {
  136 + return $oPref->getValue();
  137 + }
  138 + }
  139 +}
  140 +
  141 +$oPluginRegistry =& KTPluginRegistry::getSingleton();
  142 +$oPluginRegistry->registerPlugin('UserPreferencesPlugin', 'up.UserPreferencesPlugin.plugin', __FILE__);
  143 +?>
0 144 \ No newline at end of file
... ...
plugins/Xpa1Rntal/user-preferences/manageUserPreferences.php 0 → 100644
  1 +<?php
  2 +/*
  3 + * $Id: $
  4 + *
  5 + * The contents of this file are subject to the KnowledgeTree
  6 + * Commercial Editions On-Premise License ("License");
  7 + * You may not use this file except in compliance with the License.
  8 + * You may obtain a copy of the License at
  9 + * http://www.knowledgetree.com/about/legal/
  10 + * The terms of this license may change from time to time and the latest
  11 + * license will be published from time to time at the above Internet address.
  12 + *
  13 + * This edition of the KnowledgeTree software
  14 + * is NOT licensed to you under Open Source terms.
  15 + * You may not redistribute this source code.
  16 + * For more information please see the License above.
  17 + *
  18 + * (c) 2008, 2009, 2010 KnowledgeTree Inc.
  19 + * All Rights Reserved.
  20 + *
  21 + */
  22 +
  23 +require_once('UserPreferences.inc.php'); //
  24 +
  25 +require_once(KT_LIB_DIR . '/templating/templating.inc.php'); //
  26 +require_once(KT_LIB_DIR . '/util/ktutil.inc');
  27 +require_once(KT_LIB_DIR . '/dispatcher.inc.php');
  28 +require_once(KT_LIB_DIR . '/templating/kt3template.inc.php');
  29 +
  30 +class ManageUserPreferencesDispatcher extends KTStandardDispatcher {
  31 + //--------------------------------
  32 + // Forms
  33 + //--------------------------------
  34 +
  35 + /**
  36 + *
  37 + *
  38 + * @author KnowledgeTree Team
  39 + * @access public
  40 + * @param none
  41 + * @return html
  42 + */
  43 + public function do_main() {
  44 + return "Coming Soon";
  45 + }
  46 +
  47 + /**
  48 + *
  49 + *
  50 + * @author KnowledgeTree Team
  51 + * @access public
  52 + * @param none
  53 + * @return html
  54 + */
  55 + // TODO : Write generic, this only for zoho warning
  56 + public function do_saveUserPreference() {
  57 + $sValue = KTUtil::arrayGet($_GET, 'zohoWarning', '');
  58 + if($sValue != '') {
  59 + $aUserPreference = UserPreferences::getUserPreferences($this->oUser->getId(), 'zohoWarning'); // Get user preference
  60 + if(empty($aUserPreference) || is_null($aUserPreference)) { // Create the prefernce
  61 + $oUserPreference = new UserPreferences( $this->oUser->getId(), 'zohoWarning', $sValue);
  62 + DBUtil::startTransaction();
  63 + $oUserPreference->create();
  64 + DBUtil::commit();
  65 + } else {
  66 + foreach ($aUserPreference as $oUserPreference) { // Access object
  67 + if($oUserPreference->getValue() != $sValue) { // Update preference
  68 + $oUserPreference->setValue($sValue);
  69 + DBUtil::startTransaction();
  70 + $oUserPreference->update();
  71 + DBUtil::commit();
  72 + }
  73 + }
  74 + }
  75 + }
  76 + exit();
  77 + }
  78 +
  79 + public function do_setPreference($sKey, $sValue) {
  80 +
  81 + }
  82 +}
  83 +
  84 +class adminManageUserPreferencesDispatcher extends ManageUserPreferencesDispatcher {
  85 + var $bAdminRequired = true;
  86 + var $sSection = 'administration';
  87 +
  88 + function adminManageUserPreferencesDispatcher() {
  89 + $this->aBreadcrumbs = array(array('action' => 'administration', 'name' => _kt('Administration')),);
  90 +
  91 + return parent::KTStandardDispatcher();
  92 + }
  93 +}
  94 +
  95 +class manageUserPreferencesNavigationPortlet extends KTPortlet {
  96 + var $bActive = true;
  97 +
  98 + function manageUserPreferencesNavigationPortlet($sTitle) {
  99 + parent::KTPortlet($sTitle);
  100 + }
  101 +}
  102 +
  103 +?>
... ...
plugins/Xpa1Rntal/user-preferences/sql/user_preferences.sql 0 → 100644
  1 +CREATE TABLE IF NOT EXISTS `user_preferences` (
  2 + `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  3 + `user_id` int(11) unsigned NOT NULL,
  4 + `prefkey` varchar(255) NOT NULL,
  5 + `prefvalue` varchar(255) NOT NULL,
  6 + PRIMARY KEY (`id`),
  7 + KEY `user_id` (`user_id`)
  8 +) ENGINE=MyISAM DEFAULT CHARSET=latin1;
0 9 \ No newline at end of file
... ...
plugins/Xpa1Rntal/user-preferences/templates/manage.smarty 0 → 100644
  1 +Coming Soon
0 2 \ No newline at end of file
... ...