preferences.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. namespace OC;
  37. use \OC\DB\Connection;
  38. /**
  39. * This class provides an easy way for storing user preferences.
  40. */
  41. class Preferences {
  42. protected $conn;
  43. public function __construct(Connection $conn) {
  44. $this->conn = $conn;
  45. }
  46. /**
  47. * @brief Get all users using the preferences
  48. * @return array with user ids
  49. *
  50. * This function returns a list of all users that have at least one entry
  51. * in the preferences table.
  52. */
  53. public function getUsers() {
  54. $query = 'SELECT DISTINCT `userid` FROM `*PREFIX*preferences`';
  55. $result = $this->conn->executeQuery( $query );
  56. $users = array();
  57. while( $userid = $result->fetchColumn()) {
  58. $users[] = $userid;
  59. }
  60. return $users;
  61. }
  62. /**
  63. * @brief Get all apps of an user
  64. * @param string $user user
  65. * @return array with app ids
  66. *
  67. * This function returns a list of all apps of the user that have at least
  68. * one entry in the preferences table.
  69. */
  70. public function getApps( $user ) {
  71. $query = 'SELECT DISTINCT `appid` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  72. $result = $this->conn->executeQuery( $query, array( $user ) );
  73. $apps = array();
  74. while( $appid = $result->fetchColumn()) {
  75. $apps[] = $appid;
  76. }
  77. return $apps;
  78. }
  79. /**
  80. * @brief Get the available keys for an app
  81. * @param string $user user
  82. * @param string $app the app we are looking for
  83. * @return array with key names
  84. *
  85. * This function gets all keys of an app of an user. Please note that the
  86. * values are not returned.
  87. */
  88. public function getKeys( $user, $app ) {
  89. $query = 'SELECT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?';
  90. $result = $this->conn->executeQuery( $query, array( $user, $app ));
  91. $keys = array();
  92. while( $key = $result->fetchColumn()) {
  93. $keys[] = $key;
  94. }
  95. return $keys;
  96. }
  97. /**
  98. * @brief Gets the preference
  99. * @param string $user user
  100. * @param string $app app
  101. * @param string $key key
  102. * @param string $default = null, default value if the key does not exist
  103. * @return string the value or $default
  104. *
  105. * This function gets a value from the preferences table. If the key does
  106. * not exist the default value will be returned
  107. */
  108. public function getValue( $user, $app, $key, $default = null ) {
  109. // Try to fetch the value, return default if not exists.
  110. $query = 'SELECT `configvalue` FROM `*PREFIX*preferences`'
  111. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  112. $row = $this->conn->fetchAssoc( $query, array( $user, $app, $key ));
  113. if($row) {
  114. return $row["configvalue"];
  115. } else {
  116. return $default;
  117. }
  118. }
  119. /**
  120. * @brief sets a value in the preferences
  121. * @param string $user user
  122. * @param string $app app
  123. * @param string $key key
  124. * @param string $value value
  125. *
  126. * Adds a value to the preferences. If the key did not exist before, it
  127. * will be added automagically.
  128. */
  129. public function setValue( $user, $app, $key, $value ) {
  130. // Check if the key does exist
  131. $query = 'SELECT COUNT(*) FROM `*PREFIX*preferences`'
  132. .' WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?';
  133. $count = $this->conn->fetchColumn( $query, array( $user, $app, $key ));
  134. $exists = $count > 0;
  135. if( !$exists ) {
  136. $data = array(
  137. 'userid' => $user,
  138. 'appid' => $app,
  139. 'configkey' => $key,
  140. 'configvalue' => $value,
  141. );
  142. $this->conn->insert('*PREFIX*preferences', $data);
  143. } else {
  144. $data = array(
  145. 'configvalue' => $value,
  146. );
  147. $where = array(
  148. 'userid' => $user,
  149. 'appid' => $app,
  150. 'configkey' => $key,
  151. );
  152. $this->conn->update('*PREFIX*preferences', $data, $where);
  153. }
  154. }
  155. /**
  156. * @brief Deletes a key
  157. * @param string $user user
  158. * @param string $app app
  159. * @param string $key key
  160. *
  161. * Deletes a key.
  162. */
  163. public function deleteKey( $user, $app, $key ) {
  164. $where = array(
  165. 'userid' => $user,
  166. 'appid' => $app,
  167. 'configkey' => $key,
  168. );
  169. $this->conn->delete('*PREFIX*preferences', $where);
  170. }
  171. /**
  172. * @brief Remove app of user from preferences
  173. * @param string $user user
  174. * @param string $app app
  175. *
  176. * Removes all keys in preferences belonging to the app and the user.
  177. */
  178. public function deleteApp( $user, $app ) {
  179. $where = array(
  180. 'userid' => $user,
  181. 'appid' => $app,
  182. );
  183. $this->conn->delete('*PREFIX*preferences', $where);
  184. }
  185. /**
  186. * @brief Remove user from preferences
  187. * @param string $user user
  188. *
  189. * Removes all keys in preferences belonging to the user.
  190. */
  191. public function deleteUser( $user ) {
  192. $where = array(
  193. 'userid' => $user,
  194. );
  195. $this->conn->delete('*PREFIX*preferences', $where);
  196. }
  197. /**
  198. * @brief Remove app from all users
  199. * @param string $app app
  200. *
  201. * Removes all keys in preferences belonging to the app.
  202. */
  203. public function deleteAppFromAllUsers( $app ) {
  204. $where = array(
  205. 'appid' => $app,
  206. );
  207. $this->conn->delete('*PREFIX*preferences', $where);
  208. }
  209. }
  210. require_once __DIR__.'/legacy/'.basename(__FILE__);