preferences.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.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. return true;
  141. }
  142. /**
  143. * @brief Deletes a key
  144. * @param $user user
  145. * @param $app app
  146. * @param $key key
  147. * @returns true/false
  148. *
  149. * Deletes a key.
  150. */
  151. public static function deleteKey( $user, $app, $key ){
  152. // No need for more comments
  153. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?' );
  154. $result = $query->execute( array( $user, $app, $key ));
  155. return true;
  156. }
  157. /**
  158. * @brief Remove app of user from preferences
  159. * @param $user user
  160. * @param $app app
  161. * @returns true/false
  162. *
  163. * Removes all keys in appconfig belonging to the app and the user.
  164. */
  165. public static function deleteApp( $user, $app ){
  166. // No need for more comments
  167. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?' );
  168. $result = $query->execute( array( $user, $app ));
  169. return true;
  170. }
  171. /**
  172. * @brief Remove user from preferences
  173. * @param $user user
  174. * @returns true/false
  175. *
  176. * Removes all keys in appconfig belonging to the user.
  177. */
  178. public static function deleteUser( $user ){
  179. // No need for more comments
  180. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?' );
  181. $result = $query->execute( array( $user ));
  182. return true;
  183. }
  184. /**
  185. * @brief Remove app from all users
  186. * @param $app app
  187. * @returns true/false
  188. *
  189. * Removes all keys in preferences belonging to the app.
  190. */
  191. public static function deleteAppFromAllUsers( $app ){
  192. // No need for more comments
  193. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*preferences` WHERE `appid` = ?' );
  194. $result = $query->execute( array( $app ));
  195. return true;
  196. }
  197. }