preferences.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /*
  24. *
  25. * The following SQL statement is just a help for developers and will not be
  26. * executed!
  27. *
  28. * CREATE TABLE `preferences` (
  29. * `userid` VARCHAR( 255 ) NOT NULL ,
  30. * `appid` VARCHAR( 255 ) NOT NULL ,
  31. * `configkey` VARCHAR( 255 ) NOT NULL ,
  32. * `configvalue` VARCHAR( 255 ) NOT NULL
  33. * )
  34. *
  35. */
  36. /**
  37. * This class provides an easy way for storing user preferences.
  38. */
  39. class OC_Preferences{
  40. /**
  41. * @brief Get all users using the preferences
  42. * @returns array with user ids
  43. *
  44. * This function returns a list of all users that have at least one entry
  45. * in the preferences table.
  46. */
  47. public static function getUsers(){
  48. // No need for more comments
  49. $query = OC_DB::prepare( 'SELECT DISTINCT( userid ) FROM *PREFIX*preferences' );
  50. $result = $query->execute();
  51. $users = array();
  52. while( $row = $result->fetchRow()){
  53. $users[] = $row["userid"];
  54. }
  55. return $users;
  56. }
  57. /**
  58. * @brief Get all apps of a user
  59. * @param $user user
  60. * @returns array with app ids
  61. *
  62. * This function returns a list of all apps of the user that have at least
  63. * one entry in the preferences table.
  64. */
  65. public static function getApps( $user ){
  66. // No need for more comments
  67. $query = OC_DB::prepare( 'SELECT DISTINCT( appid ) FROM *PREFIX*preferences WHERE userid = ?' );
  68. $result = $query->execute( array( $user ));
  69. $apps = array();
  70. while( $row = $result->fetchRow()){
  71. $apps[] = $row["appid"];
  72. }
  73. return $apps;
  74. }
  75. /**
  76. * @brief Get the available keys for an app
  77. * @param $user user
  78. * @param $app the app we are looking for
  79. * @returns array with key names
  80. *
  81. * This function gets all keys of an app of an user. Please note that the
  82. * values are not returned.
  83. */
  84. public static function getKeys( $user, $app ){
  85. // No need for more comments
  86. $query = OC_DB::prepare( 'SELECT configkey FROM *PREFIX*preferences WHERE userid = ? AND appid = ?' );
  87. $result = $query->execute( array( $user, $app ));
  88. $keys = array();
  89. while( $row = $result->fetchRow()){
  90. $keys[] = $row["configkey"];
  91. }
  92. return $keys;
  93. }
  94. /**
  95. * @brief Gets the preference
  96. * @param $user user
  97. * @param $app app
  98. * @param $key key
  99. * @param $default = null, default value if the key does not exist
  100. * @returns the value or $default
  101. *
  102. * This function gets a value from the prefernces table. If the key does
  103. * not exist the default value will be returnes
  104. */
  105. public static function getValue( $user, $app, $key, $default = null ){
  106. // Try to fetch the value, return default if not exists.
  107. $query = OC_DB::prepare( 'SELECT configvalue FROM *PREFIX*preferences WHERE userid = ? AND appid = ? AND configkey = ?' );
  108. $result = $query->execute( array( $user, $app, $key ));
  109. $row = $result->fetchRow();
  110. if($row){
  111. return $row["configvalue"];
  112. }else{
  113. return $default;
  114. }
  115. }
  116. /**
  117. * @brief sets a value in the preferences
  118. * @param $user user
  119. * @param $app app
  120. * @param $key key
  121. * @param $value value
  122. * @returns true/false
  123. *
  124. * Adds a value to the preferences. If the key did not exist before, it
  125. * will be added automagically.
  126. */
  127. public static function setValue( $user, $app, $key, $value ){
  128. // Check if the key does exist
  129. $query = OC_DB::prepare( 'SELECT configvalue FROM *PREFIX*preferences WHERE userid = ? AND appid = ? AND configkey = ?' );
  130. $values=$query->execute(array($user,$app,$key))->fetchAll();
  131. $exists=(count($values)>0);
  132. if( !$exists ){
  133. $query = OC_DB::prepare( 'INSERT INTO *PREFIX*preferences ( userid, appid, configkey, configvalue ) VALUES( ?, ?, ?, ? )' );
  134. $query->execute( array( $user, $app, $key, $value ));
  135. }
  136. else{
  137. $query = OC_DB::prepare( 'UPDATE *PREFIX*preferences SET configvalue = ? WHERE userid = ? AND appid = ? AND configkey = ?' );
  138. $query->execute( array( $value, $user, $app, $key ));
  139. }
  140. }
  141. /**
  142. * @brief Deletes a key
  143. * @param $user user
  144. * @param $app app
  145. * @param $key key
  146. * @returns true/false
  147. *
  148. * Deletes a key.
  149. */
  150. public static function deleteKey( $user, $app, $key ){
  151. // No need for more comments
  152. $query = OC_DB::prepare( 'DELETE FROM *PREFIX*preferences WHERE userid = ? AND appid = ? AND configkey = ?' );
  153. $result = $query->execute( array( $user, $app, $key ));
  154. return true;
  155. }
  156. /**
  157. * @brief Remove app of user from preferences
  158. * @param $user user
  159. * @param $app app
  160. * @returns true/false
  161. *
  162. * Removes all keys in appconfig belonging to the app and the user.
  163. */
  164. public static function deleteApp( $user, $app ){
  165. // No need for more comments
  166. $query = OC_DB::prepare( 'DELETE FROM *PREFIX*preferences WHERE userid = ? AND appid = ?' );
  167. $result = $query->execute( array( $user, $app ));
  168. return true;
  169. }
  170. /**
  171. * @brief Remove user from preferences
  172. * @param $user user
  173. * @returns true/false
  174. *
  175. * Removes all keys in appconfig belonging to the user.
  176. */
  177. public static function deleteUser( $user ){
  178. // No need for more comments
  179. $query = OC_DB::prepare( 'DELETE FROM *PREFIX*preferences WHERE userid = ?' );
  180. $result = $query->execute( array( $user ));
  181. return true;
  182. }
  183. /**
  184. * @brief Remove app from all users
  185. * @param $app app
  186. * @returns true/false
  187. *
  188. * Removes all keys in preferences belonging to the app.
  189. */
  190. public static function deleteAppFromAllUsers( $app ){
  191. // No need for more comments
  192. $query = OC_DB::prepare( 'DELETE FROM *PREFIX*preferences WHERE appid = ?' );
  193. $result = $query->execute( array( $app ));
  194. return true;
  195. }
  196. }