mirror of
https://github.com/nextcloud/server.git
synced 2026-04-20 22:00:39 -04:00
Switch from using separate db table to use OC_Preferences.
There is a limitation in that the the configvalue field in the preferences table is a varchar(255).
This commit is contained in:
parent
adec09b075
commit
8a1b671fdd
1 changed files with 29 additions and 67 deletions
|
|
@ -28,47 +28,26 @@
|
|||
* Category names are not case-sensitive, but will be saved with the case they are
|
||||
* entered in. If a user already has a category 'family' for an app, and tries to add
|
||||
* a category named 'Family' it will be silently ignored.
|
||||
* NOTE: There is a limitation in that the the configvalue field in the preferences table is a varchar(255).
|
||||
*/
|
||||
OC_HOOK::connect('OC_User', 'post_deleteUser', 'OC_VCategories', 'deleteUser');
|
||||
class OC_VCategories {
|
||||
/**
|
||||
* cache
|
||||
*/
|
||||
protected static $cache = array();
|
||||
|
||||
/**
|
||||
* Categories
|
||||
*/
|
||||
private $categories = array();
|
||||
|
||||
private $app = '';
|
||||
|
||||
/**
|
||||
* @brief Constructor.
|
||||
* @param $app The application identifier e.g. 'contacts' or 'calendar'.
|
||||
*/
|
||||
public function __construct($app, $user=null) {
|
||||
$this->app = $app;
|
||||
if(is_null($user)) {
|
||||
$user = OC_User::getUser();
|
||||
}
|
||||
// Use cache if possible - I doubt this is ever the case. Copy/paste from OC_L10N.
|
||||
if(array_key_exists($app.'::'.$user, self::$cache)){
|
||||
OC_Log::write('core','OC_Categories::ctor, using cache', OC_Log::DEBUG);
|
||||
$this->categories = self::$cache[$app.'::'.$user];
|
||||
} else {
|
||||
$result = null;
|
||||
try {
|
||||
$stmt = OC_DB::prepare('SELECT DISTINCT name FROM *PREFIX*categories WHERE userid = ? AND appid = ? ORDER BY name');
|
||||
$result = $stmt->execute(array($user, $app));
|
||||
} catch(Exception $e) {
|
||||
OC_Log::write('core','OC_VCategories::ctor, exception: '.$e->getMessage(), OC_Log::ERROR);
|
||||
OC_Log::write('core','OC_VCategories::ctor, app: '.$app.', user: '.$user, OC_Log::ERROR);
|
||||
}
|
||||
if(!is_null($result)) {
|
||||
while( $row = $result->fetchRow()){
|
||||
$this->categories[] = $row['name'];
|
||||
}
|
||||
self::$cache[$app.'::'.$user] = $this->categories;
|
||||
}
|
||||
}
|
||||
$this->categories = OC_VObject::unescapeSemicolons(OC_Preferences::getValue($user, $app, 'extra categories', ''));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -93,7 +72,7 @@ class OC_VCategories {
|
|||
* @param $names A string with a name or an array of strings containing the name(s) of the categor(y|ies) to add.
|
||||
* @returns bool Returns false on error.
|
||||
*/
|
||||
public function add($app, $names) {
|
||||
public function add($names) {
|
||||
$user = OC_User::getUser();
|
||||
$newones = array();
|
||||
if(!is_array($names)) {
|
||||
|
|
@ -106,17 +85,8 @@ class OC_VCategories {
|
|||
}
|
||||
}
|
||||
if(count($newones) > 0) {
|
||||
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*categories (userid,appid,name) VALUES(?,?,?)' );
|
||||
foreach($newones as $name) {
|
||||
$this->categories[] = $name;
|
||||
try {
|
||||
$result = $stmt->execute(array($user, $app, $name));
|
||||
} catch(Exception $e) {
|
||||
OC_Log::write('core','OC_VCategories::add, exception: '.$e->getMessage(), OC_Log::ERROR);
|
||||
OC_Log::write('core','OC_VCategories::add, app: '.$app.', user: '.$user.', name: '.$name, OC_Log::ERROR);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$this->categories = $this->cleanArray(array_merge($this->categories, $newones));
|
||||
OC_Preferences::setValue(OC_User::getUser(), $this->app, 'extra categories', OC_VObject::escapeSemicolons($this->categories));
|
||||
natcasesort($this->categories); // Dunno if this is necessary
|
||||
}
|
||||
return true;
|
||||
|
|
@ -127,29 +97,23 @@ class OC_VCategories {
|
|||
* @param $vobject The instance of OC_VObject to load the categories from.
|
||||
* @returns bool Returns false if the name already exist (case insensitive) or on error.
|
||||
*/
|
||||
public function loadFromVObject($app, $vobject) {
|
||||
public function loadFromVObject($vobject) {
|
||||
$this->add($vobject->getAsArray('CATEGORIES'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Delete a category from the db and from all the vobject supplied
|
||||
* @param $app
|
||||
* @param $name
|
||||
* @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table.
|
||||
*/
|
||||
public function delete($app, $name, array &$objects) {
|
||||
public function delete($name, array &$objects) {
|
||||
$user = OC_User::getUser();
|
||||
if(!$this->hasCategory($name)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$stmt = OC_DB::prepare('DELETE FROM *PREFIX*categories WHERE UPPER(name) = ?');
|
||||
$result = $stmt->execute(array(strtoupper($name),));
|
||||
} catch(Exception $e) {
|
||||
OC_Log::write('core','OC_VCategories::delete, exception: '.$e->getMessage(), OC_Log::ERROR);
|
||||
OC_Log::write('core','OC_VCategories::delete, name: '.$name, OC_Log::ERROR);
|
||||
return false;
|
||||
}
|
||||
unset($this->categories[$this->array_searchi($name, $this->categories)]);
|
||||
$this->categories = $this->cleanArray($this->categories);
|
||||
OC_Preferences::setValue($user, $this->app, 'extra categories', OC_VObject::escapeSemicolons($this->categories));
|
||||
foreach($objects as $key=>&$value) {
|
||||
$vobject = OC_VObject::parse($value[1]);
|
||||
if(!is_null($vobject)){
|
||||
|
|
@ -167,24 +131,6 @@ class OC_VCategories {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Delete all categories for a specific user. Connected to OC_User::post_deleteUser
|
||||
* @param $parameters The id of the user.
|
||||
* @returns bool Returns false on error.
|
||||
*/
|
||||
public function deleteUser($parameters) {
|
||||
$user = $parameters['uid'];
|
||||
try {
|
||||
$stmt = OC_DB::prepare('DELETE FROM *PREFIX*categories WHERE user = ?');
|
||||
$result = $stmt->execute(array($user,));
|
||||
} catch(Exception $e) {
|
||||
OC_Log::write('core','OC_VCategories::deleteFromUser, exception: '.$e->getMessage(), OC_Log::ERROR);
|
||||
OC_Log::write('core','OC_VCategories::deleteFromUser, user: '.$user, OC_Log::ERROR);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// case-insensitive in_array
|
||||
private function in_arrayi($needle, $haystack) {
|
||||
return in_array(strtolower($needle), array_map('strtolower', $haystack));
|
||||
|
|
@ -194,5 +140,21 @@ class OC_VCategories {
|
|||
private function array_searchi($needle, $haystack) {
|
||||
return array_search(strtolower($needle),array_map('strtolower',$haystack));
|
||||
}
|
||||
|
||||
private function cleanArray($array, $remove_null_number = true){
|
||||
$new_array = array();
|
||||
$null_exceptions = array();
|
||||
|
||||
foreach ($array as $key => $value){
|
||||
$value = trim($value);
|
||||
if($remove_null_number){
|
||||
$null_exceptions[] = '0';
|
||||
}
|
||||
if(!in_array($value, $null_exceptions) && $value != "") {
|
||||
$new_array[] = $value;
|
||||
}
|
||||
}
|
||||
return $new_array;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Loading…
Reference in a new issue