Commit c8de44489bb1f81f502028d7695579f91fa137be

Authored by Paul Barrett
2 parents 90d3ad60 4f8b3228

Merge branch 'edge' of github.com:ktgit/knowledgetree into edge

plugins/user-preferences/KTUserPreferences.php
@@ -37,6 +37,7 @@ require_once('UserPreferences.inc.php'); @@ -37,6 +37,7 @@ require_once('UserPreferences.inc.php');
37 */ 37 */
38 class KTUserPreferences 38 class KTUserPreferences
39 { 39 {
  40 + private $oUserPreference;
40 41
41 /** 42 /**
42 * Constructor function for the class 43 * Constructor function for the class
@@ -47,14 +48,54 @@ class KTUserPreferences @@ -47,14 +48,54 @@ class KTUserPreferences
47 */ 48 */
48 public function __construct() 49 public function __construct()
49 { 50 {
  51 + $this->oUserPreference = new UserPreferences();
50 } 52 }
51 53
52 /** 54 /**
53 - * Check if user is logged in 55 + *
54 * 56 *
55 * @author KnowledgeTree Team 57 * @author KnowledgeTree Team
56 * @access public 58 * @access public
57 - * @return html 59 + * @return
  60 + */
  61 + function saveUserPreferences() {
  62 + $iUserId = KTUtil::arrayGet($_GET, 'user_id', $_SESSION['userID']);
  63 + $sKey = KTUtil::arrayGet($_GET, 'key', null);
  64 + $sValue = KTUtil::arrayGet($_GET, 'value', null);
  65 + if(is_null($iUserId) || is_null($sKey) || is_null($sValue)) {
  66 + exit("Missing Required options : user_id = $iUserId key = $sKey value = $sValue");
  67 + }
  68 + $this->oUserPreference->saveUserPreferences($iUserId, $sKey, $sValue);
  69 + }
  70 +
  71 + /**
  72 + *
  73 + *
  74 + * @author KnowledgeTree Team
  75 + * @access public
  76 + * @return
  77 + */
  78 + function getUserPreferences() {
  79 +
  80 + }
  81 +
  82 + /**
  83 + *
  84 + *
  85 + * @author KnowledgeTree Team
  86 + * @access public
  87 + * @return
  88 + */
  89 + function deleteUserPreferences() {
  90 +
  91 + }
  92 +
  93 + /**
  94 + *
  95 + *
  96 + * @author KnowledgeTree Team
  97 + * @access public
  98 + * @return boolean
58 */ 99 */
59 function isLoggedIn() { 100 function isLoggedIn() {
60 $session = new Session(); 101 $session = new Session();
@@ -73,16 +114,9 @@ if (!$oKTUserPreferences->isLoggedIn()) { @@ -73,16 +114,9 @@ if (!$oKTUserPreferences->isLoggedIn()) {
73 exit; 114 exit;
74 } 115 }
75 116
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; 117 +if(isset($_GET['action'])) {
  118 + $action = $_GET['action'];
  119 + $oKTUserPreferences->$action();
86 } 120 }
87 121
88 exit; 122 exit;
plugins/user-preferences/UserPreferences.inc.php
@@ -75,7 +75,16 @@ class UserPreferences extends KTEntity { @@ -75,7 +75,16 @@ class UserPreferences extends KTEntity {
75 return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions); 75 return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions);
76 } 76 }
77 77
78 - public function getUserPreferences($iUserId, $sKey, $aOptions = null) { 78 + /**
  79 + *
  80 + *
  81 + * @author KnowledgeTree Team
  82 + * @access public
  83 + * @param
  84 + * @param
  85 + * @return
  86 + */
  87 + public function getPreferences($iUserId, $sKey, $aOptions = null) {
79 $sWhereClause = "WHERE user_id = '$iUserId' AND prefkey = '$sKey'"; 88 $sWhereClause = "WHERE user_id = '$iUserId' AND prefkey = '$sKey'";
80 89
81 return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions); 90 return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions);
@@ -154,16 +163,30 @@ class UserPreferences extends KTEntity { @@ -154,16 +163,30 @@ class UserPreferences extends KTEntity {
154 // Utility 163 // Utility
155 164
156 /** 165 /**
157 - * Set the template name 166 + *
158 * 167 *
159 * @author KnowledgeTree Team 168 * @author KnowledgeTree Team
160 * @access public 169 * @access public
161 - * @param $sName - string - the template node name  
162 - * @param $iParentId - int - the template id  
163 - * @return boolean 170 + * @param
  171 + * @param
  172 + * @param
  173 + * @return
164 */ 174 */
165 public function exists($iUserId, $sKey, $sValue) { 175 public function exists($iUserId, $sKey, $sValue) {
166 - return UserPreferencesUtil::userPreferenceExists($iUserId, $sKey, $sValue); 176 + $sQuery = "SELECT id, name FROM " . KTUtil::getTableName('user_preferences') . " WHERE user_id = ? AND prefkey = ? AND prefvalue = ?";/*ok*/
  177 + $aParams = array($iUserId, $sKey, $sValue);
  178 + $res = DBUtil::getResultArray(array($sQuery, $aParams));
  179 + if (count($res) != 0) {
  180 + foreach ($res as $user_pref){
  181 + $userid = isset($user_pref['user_id']) ? $user_pref['user_id'] : '';
  182 + $key = isset($user_pref['prefkey']) ? $user_pref['prefkey'] : '';
  183 + if($sKey == $key && $iUserId == $userid) {
  184 + return true;
  185 + }
  186 + }
  187 + return false;
  188 + }
  189 + return false;
167 } 190 }
168 191
169 /** 192 /**
@@ -180,33 +203,54 @@ class UserPreferences extends KTEntity { @@ -180,33 +203,54 @@ class UserPreferences extends KTEntity {
180 $sWhereClause = "WHERE user_id = '$userId'"; 203 $sWhereClause = "WHERE user_id = '$userId'";
181 return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions); 204 return KTEntityUtil::getList2('UserPreferences', $sWhereClause, $aOptions);
182 } 205 }
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; 206 +
  207 + /**
  208 + *
  209 + *
  210 + * @author KnowledgeTree Team
  211 + * @access public
  212 + * @param $aOptions - array
  213 + * @return
  214 + */
  215 + public function getUserPreferenceValue($iUserId, $sKey) {
  216 + $aPref = UserPreferences::getPreferences($iUserId, $sKey);
  217 + if(PEAR::isError($aPref)) {
  218 + return false;
  219 + }
  220 + if(count($aPref) > 1) {
  221 + return false;
  222 + }
  223 +
  224 + foreach ($aPref as $oPref) {
  225 + return $oPref->getValue();
  226 + }
  227 + }
  228 +
  229 + /**
  230 + *
  231 + *
  232 + * @author KnowledgeTree Team
  233 + * @access public
  234 + * @param $aOptions - array
  235 + * @return
  236 + */
  237 + public function saveUserPreferences($iUserId, $sKey, $sValue) {
  238 + $oUser = User::get($iUserId); // Get the user
  239 + if (PEAR::isError($oUser)) {
  240 + return false;
  241 + }
  242 + $aUserPreference = UserPreferences::getPreferences($iUserId, 'zohoWarning'); // Get user preference
  243 + if(empty($aUserPreference) || is_null($aUserPreference)) { // Create the preference
  244 + $oUserPreference = new UserPreferences($iUserId, 'zohoWarning', $sValue);
  245 + $oUserPreference->create();
  246 + } else {
  247 + foreach ($aUserPreference as $oUserPreference) { // Access object
  248 + if($oUserPreference->getValue() != $sValue) { // Check if value needs to be updated
  249 + $oUserPreference->setValue($sValue); // Set the new value
  250 + $oUserPreference->update(); // Update preference
  251 + }
  252 + }
207 } 253 }
208 - return false;  
209 - } 254 + }
210 } 255 }
211 -  
212 ?> 256 ?>
plugins/user-preferences/UserPreferencesPlugin.php
@@ -20,26 +20,24 @@ @@ -20,26 +20,24 @@
20 * 20 *
21 */ 21 */
22 22
23 -define('PLUGINDIR_UserPreferencesPlugin', dirname(__FILE__)); 23 +define('UserPreferences_PluginDir', dirname(__FILE__));
  24 +$start = strpos(dirname(__FILE__), 'plugins');
  25 +$path = substr(dirname(__FILE__), $start);
  26 +$path = str_replace("\\", "/", $path);
  27 +$file = $path.'/KTUserPreferences.php';
  28 +define('UserPreferencesPlugin_KTFile', $file);
  29 +
24 /* */ 30 /* */
25 require_once("UserPreferences.inc.php"); 31 require_once("UserPreferences.inc.php");
26 /* Plugin Base */ 32 /* Plugin Base */
27 require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); 33 require_once(KT_LIB_DIR . '/plugins/plugin.inc.php');
28 require_once(KT_LIB_DIR . '/plugins/pluginregistry.inc.php'); 34 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 35
38 class UserPreferencesPlugin extends KTPlugin { 36 class UserPreferencesPlugin extends KTPlugin {
39 - public $sNamespace = 'up.UserPreferencesPlugin.plugin'; 37 + public $sNamespace = 'user.preferences.plugin';
40 public $iVersion = 0; 38 public $iVersion = 0;
41 public $autoRegister = true; 39 public $autoRegister = true;
42 - //public $showInAdmin = false; 40 + public $showInAdmin = false;
43 41
44 /** 42 /**
45 * User Preferences constructor 43 * User Preferences constructor
@@ -50,94 +48,13 @@ class UserPreferencesPlugin extends KTPlugin { @@ -50,94 +48,13 @@ class UserPreferencesPlugin extends KTPlugin {
50 function UserPreferencesPlugin($sFilename = null) { 48 function UserPreferencesPlugin($sFilename = null) {
51 parent::KTPlugin($sFilename); 49 parent::KTPlugin($sFilename);
52 $this->sFriendlyName = _kt('User Preferences Plugin'); 50 $this->sFriendlyName = _kt('User Preferences Plugin');
53 - $this->sSQLDir = PLUGINDIR_UserPreferencesPlugin . DIRECTORY_SEPARATOR. 'sql' . DIRECTORY_SEPARATOR; 51 + $this->sSQLDir = UserPreferences_PluginDir . DIRECTORY_SEPARATOR. 'sql' . DIRECTORY_SEPARATOR;
54 $this->dir = dirname(__FILE__); 52 $this->dir = dirname(__FILE__);
55 } 53 }
56 54
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 55
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 } 56 }
140 57
141 $oPluginRegistry =& KTPluginRegistry::getSingleton(); 58 $oPluginRegistry =& KTPluginRegistry::getSingleton();
142 -$oPluginRegistry->registerPlugin('UserPreferencesPlugin', 'up.UserPreferencesPlugin.plugin', __FILE__); 59 +$oPluginRegistry->registerPlugin('UserPreferencesPlugin', 'user.preferences.plugin', __FILE__);
143 ?> 60 ?>
144 \ No newline at end of file 61 \ No newline at end of file
plugins/user-preferences/sql/upgradeto0.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 \ No newline at end of file 9 \ No newline at end of file