summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2012-11-13 15:40:46 -0800
committerThomas Tanghus <thomas@tanghus.net>2012-11-13 15:40:46 -0800
commitc353568ea09ef89e82c7b0f35b8d646b9c35005d (patch)
tree0d5247b00f465e5d85ead47fd1532cc20bf5a68e
parent9c36326e4776350b64b9a657feb5fca8a50cefcc (diff)
parentbfb6faa85077ef8f7e56a7e706f57587460a2125 (diff)
downloadnextcloud-server-c353568ea09ef89e82c7b0f35b8d646b9c35005d.tar.gz
nextcloud-server-c353568ea09ef89e82c7b0f35b8d646b9c35005d.zip
Merge pull request #41 from owncloud/vcategories_db
OC_VCategories - this time actually usable ;)
-rw-r--r--core/ajax/vcategories/add.php20
-rw-r--r--core/ajax/vcategories/addToFavorites.php38
-rw-r--r--core/ajax/vcategories/delete.php25
-rw-r--r--core/ajax/vcategories/edit.php14
-rw-r--r--core/ajax/vcategories/favorites.php30
-rw-r--r--core/ajax/vcategories/removeFromFavorites.php38
-rw-r--r--core/js/oc-vcategories.js180
-rw-r--r--core/routes.php6
-rw-r--r--core/templates/edit_categories_dialog.php9
-rw-r--r--db_structure.xml121
-rw-r--r--lib/db.php72
-rw-r--r--lib/public/db.php21
-rwxr-xr-xlib/util.php2
-rw-r--r--lib/vcategories.php632
-rw-r--r--tests/data/db_structure.xml43
-rw-r--r--tests/lib/db.php63
-rw-r--r--tests/lib/vcategories.php117
17 files changed, 1316 insertions, 115 deletions
diff --git a/core/ajax/vcategories/add.php b/core/ajax/vcategories/add.php
index 8d31275dbfb..23d00af70ab 100644
--- a/core/ajax/vcategories/add.php
+++ b/core/ajax/vcategories/add.php
@@ -14,23 +14,25 @@ function debug($msg) {
OC_Log::write('core', 'ajax/vcategories/add.php: '.$msg, OC_Log::DEBUG);
}
-OC_JSON::checkLoggedIn();
-$category = isset($_GET['category'])?strip_tags($_GET['category']):null;
-$app = isset($_GET['app'])?$_GET['app']:null;
+OCP\JSON::checkLoggedIn();
+OCP\JSON::callCheck();
-if(is_null($app)) {
- bailOut(OC_Contacts_App::$l10n->t('Application name not provided.'));
-}
+$l = OC_L10N::get('core');
+
+$category = isset($_POST['category']) ? strip_tags($_POST['category']) : null;
+$type = isset($_POST['type']) ? $_POST['type'] : null;
-OC_JSON::checkAppEnabled($app);
+if(is_null($type)) {
+ bailOut($l->t('Category type not provided.'));
+}
if(is_null($category)) {
- bailOut(OC_Contacts_App::$l10n->t('No category to add?'));
+ bailOut($l->t('No category to add?'));
}
debug(print_r($category, true));
-$categories = new OC_VCategories($app);
+$categories = new OC_VCategories($type);
if($categories->hasCategory($category)) {
bailOut(OC_Contacts_App::$l10n->t('This category already exists: '.$category));
} else {
diff --git a/core/ajax/vcategories/addToFavorites.php b/core/ajax/vcategories/addToFavorites.php
new file mode 100644
index 00000000000..52f62d5fc6b
--- /dev/null
+++ b/core/ajax/vcategories/addToFavorites.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+function bailOut($msg) {
+ OC_JSON::error(array('data' => array('message' => $msg)));
+ OC_Log::write('core', 'ajax/vcategories/addToFavorites.php: '.$msg, OC_Log::DEBUG);
+ exit();
+}
+function debug($msg) {
+ OC_Log::write('core', 'ajax/vcategories/addToFavorites.php: '.$msg, OC_Log::DEBUG);
+}
+
+OCP\JSON::checkLoggedIn();
+OCP\JSON::callCheck();
+
+$l = OC_L10N::get('core');
+
+$id = isset($_POST['id']) ? strip_tags($_POST['id']) : null;
+$type = isset($_POST['type']) ? $_POST['type'] : null;
+
+if(is_null($type)) {
+ bailOut($l->t('Object type not provided.'));
+}
+
+if(is_null($id)) {
+ bailOut($l->t('%s ID not provided.', $type));
+}
+
+$categories = new OC_VCategories($type);
+if(!$categories->addToFavorites($id, $type)) {
+ bailOut($l->t('Error adding %s to favorites.', $id));
+}
+
+OC_JSON::success();
diff --git a/core/ajax/vcategories/delete.php b/core/ajax/vcategories/delete.php
index 74b0220870c..dfec3785743 100644
--- a/core/ajax/vcategories/delete.php
+++ b/core/ajax/vcategories/delete.php
@@ -15,21 +15,26 @@ function debug($msg) {
OC_Log::write('core', 'ajax/vcategories/delete.php: '.$msg, OC_Log::DEBUG);
}
-OC_JSON::checkLoggedIn();
-$app = isset($_POST['app'])?$_POST['app']:null;
-$categories = isset($_POST['categories'])?$_POST['categories']:null;
-if(is_null($app)) {
- bailOut(OC_Contacts_App::$l10n->t('Application name not provided.'));
-}
+OCP\JSON::checkLoggedIn();
+OCP\JSON::callCheck();
+
+$l = OC_L10N::get('core');
-OC_JSON::checkAppEnabled($app);
+$type = isset($_POST['type']) ? $_POST['type'] : null;
+$categories = isset($_POST['categories']) ? $_POST['categories'] : null;
+
+if(is_null($type)) {
+ bailOut($l->t('Object type not provided.'));
+}
-debug('The application "'.$app.'" uses the default file. OC_VObjects will not be updated.');
+debug('The application using category type "'
+ . $type
+ . '" uses the default file for deletion. OC_VObjects will not be updated.');
if(is_null($categories)) {
- bailOut('No categories selected for deletion.');
+ bailOut($l->t('No categories selected for deletion.'));
}
-$vcategories = new OC_VCategories($app);
+$vcategories = new OC_VCategories($type);
$vcategories->delete($categories);
OC_JSON::success(array('data' => array('categories'=>$vcategories->categories())));
diff --git a/core/ajax/vcategories/edit.php b/core/ajax/vcategories/edit.php
index caeebcaa940..0387b17576c 100644
--- a/core/ajax/vcategories/edit.php
+++ b/core/ajax/vcategories/edit.php
@@ -16,16 +16,18 @@ function debug($msg) {
}
OC_JSON::checkLoggedIn();
-$app = isset($_GET['app'])?$_GET['app']:null;
-if(is_null($app)) {
- bailOut('Application name not provided.');
+$l = OC_L10N::get('core');
+
+$type = isset($_GET['type']) ? $_GET['type'] : null;
+
+if(is_null($type)) {
+ bailOut($l->t('Category type not provided.'));
}
-OC_JSON::checkAppEnabled($app);
-$tmpl = new OC_TEMPLATE("core", "edit_categories_dialog");
+$tmpl = new OCP\Template("core", "edit_categories_dialog");
-$vcategories = new OC_VCategories($app);
+$vcategories = new OC_VCategories($type);
$categories = $vcategories->categories();
debug(print_r($categories, true));
$tmpl->assign('categories', $categories);
diff --git a/core/ajax/vcategories/favorites.php b/core/ajax/vcategories/favorites.php
new file mode 100644
index 00000000000..20accea61cd
--- /dev/null
+++ b/core/ajax/vcategories/favorites.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+function bailOut($msg) {
+ OC_JSON::error(array('data' => array('message' => $msg)));
+ OC_Log::write('core', 'ajax/vcategories/addToFavorites.php: '.$msg, OC_Log::DEBUG);
+ exit();
+}
+function debug($msg) {
+ OC_Log::write('core', 'ajax/vcategories/addToFavorites.php: '.$msg, OC_Log::DEBUG);
+}
+
+OCP\JSON::checkLoggedIn();
+OCP\JSON::callCheck();
+
+$type = isset($_GET['type']) ? $_GET['type'] : null;
+
+if(is_null($type)) {
+ $l = OC_L10N::get('core');
+ bailOut($l->t('Object type not provided.'));
+}
+
+$categories = new OC_VCategories($type);
+$ids = $categories->getFavorites($type)) {
+
+OC_JSON::success(array('ids' => $ids));
diff --git a/core/ajax/vcategories/removeFromFavorites.php b/core/ajax/vcategories/removeFromFavorites.php
new file mode 100644
index 00000000000..ba6e95c2497
--- /dev/null
+++ b/core/ajax/vcategories/removeFromFavorites.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+function bailOut($msg) {
+ OC_JSON::error(array('data' => array('message' => $msg)));
+ OC_Log::write('core', 'ajax/vcategories/removeFromFavorites.php: '.$msg, OC_Log::DEBUG);
+ exit();
+}
+function debug($msg) {
+ OC_Log::write('core', 'ajax/vcategories/removeFromFavorites.php: '.$msg, OC_Log::DEBUG);
+}
+
+OCP\JSON::checkLoggedIn();
+OCP\JSON::callCheck();
+
+$l = OC_L10N::get('core');
+
+$id = isset($_POST['id']) ? strip_tags($_POST['id']) : null;
+$type = isset($_POST['type']) ? $_POST['type'] : null;
+
+if(is_null($type)) {
+ bailOut($l->t('Object type not provided.'));
+}
+
+if(is_null($id)) {
+ bailOut($l->t('%s ID not provided.', $type));
+}
+
+$categories = new OC_VCategories($type);
+if(!$categories->removeFromFavorites($id, $type)) {
+ bailOut($l->t('Error removing %s from favorites.', $id));
+}
+
+OC_JSON::success();
diff --git a/core/js/oc-vcategories.js b/core/js/oc-vcategories.js
index c99dd51f53a..609703f2cc9 100644
--- a/core/js/oc-vcategories.js
+++ b/core/js/oc-vcategories.js
@@ -1,30 +1,48 @@
-var OCCategories={
- edit:function(){
- if(OCCategories.app == undefined) {
- OC.dialogs.alert('OCCategories.app is not set!');
- return;
+var OCCategories= {
+ category_favorites:'_$!<Favorite>!$_',
+ edit:function(type, cb) {
+ if(!type && !this.type) {
+ throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
}
+ type = type ? type : this.type;
$('body').append('<div id="category_dialog"></div>');
- $('#category_dialog').load(OC.filePath('core', 'ajax', 'vcategories/edit.php')+'?app='+OCCategories.app, function(response){
+ $('#category_dialog').load(
+ OC.filePath('core', 'ajax', 'vcategories/edit.php') + '?type=' + type, function(response) {
try {
var jsondata = jQuery.parseJSON(response);
- if(response.status == 'error'){
+ if(response.status == 'error') {
OC.dialogs.alert(response.data.message, 'Error');
return;
}
} catch(e) {
- $('#edit_categories_dialog').dialog({
+ var setEnabled = function(d, enable) {
+ if(enable) {
+ d.css('cursor', 'default').find('input,button:not(#category_addbutton)')
+ .prop('disabled', false).css('cursor', 'default');
+ } else {
+ d.css('cursor', 'wait').find('input,button:not(#category_addbutton)')
+ .prop('disabled', true).css('cursor', 'wait');
+ }
+ }
+ var dlg = $('#edit_categories_dialog').dialog({
modal: true,
height: 350, minHeight:200, width: 250, minWidth: 200,
buttons: {
- 'Close': function() {
- $(this).dialog("close");
+ 'Close': function() {
+ $(this).dialog('close');
},
'Delete':function() {
- OCCategories.doDelete();
+ var categories = $('#categorylist').find('input:checkbox').serialize();
+ setEnabled(dlg, false);
+ OCCategories.doDelete(categories, function() {
+ setEnabled(dlg, true);
+ });
},
'Rescan':function() {
- OCCategories.rescan();
+ setEnabled(dlg, false);
+ OCCategories.rescan(function() {
+ setEnabled(dlg, true);
+ });
}
},
close : function(event, ui) {
@@ -32,7 +50,7 @@ var OCCategories={
$('#category_dialog').remove();
},
open : function(event, ui) {
- $('#category_addinput').live('input',function(){
+ $('#category_addinput').live('input',function() {
if($(this).val().length > 0) {
$('#category_addbutton').removeAttr('disabled');
}
@@ -43,7 +61,7 @@ var OCCategories={
$('#category_addbutton').attr('disabled', 'disabled');
return false;
});
- $('#category_addbutton').live('click',function(e){
+ $('#category_addbutton').live('click',function(e) {
e.preventDefault();
if($('#category_addinput').val().length > 0) {
OCCategories.add($('#category_addinput').val());
@@ -55,58 +73,142 @@ var OCCategories={
}
});
},
- _processDeleteResult:function(jsondata, status, xhr){
- if(jsondata.status == 'success'){
+ _processDeleteResult:function(jsondata) {
+ if(jsondata.status == 'success') {
OCCategories._update(jsondata.data.categories);
} else {
OC.dialogs.alert(jsondata.data.message, 'Error');
}
},
- doDelete:function(){
- var categories = $('#categorylist').find('input:checkbox').serialize();
+ favorites:function(type, cb) {
+ if(!type && !this.type) {
+ throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
+ }
+ type = type ? type : this.type;
+ $.getJSON(OC.filePath('core', 'ajax', 'categories/favorites.php'), {type: type},function(jsondata) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
+ } else {
+ if(jsondata.status === 'success') {
+ OCCategories._update(jsondata.data.categories);
+ } else {
+ OC.dialogs.alert(jsondata.data.message, t('core', 'Error'));
+ }
+ }
+ });
+ },
+ addToFavorites:function(id, type, cb) {
+ if(!type && !this.type) {
+ throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
+ }
+ type = type ? type : this.type;
+ $.post(OC.filePath('core', 'ajax', 'vcategories/addToFavorites.php'), {id:id, type:type}, function(jsondata) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
+ } else {
+ if(jsondata.status !== 'success') {
+ OC.dialogs.alert(jsondata.data.message, 'Error');
+ }
+ }
+ });
+ },
+ removeFromFavorites:function(id, type, cb) {
+ if(!type && !this.type) {
+ throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
+ }
+ type = type ? type : this.type;
+ $.post(OC.filePath('core', 'ajax', 'vcategories/removeFromFavorites.php'), {id:id, type:type}, function(jsondata) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
+ } else {
+ if(jsondata.status !== 'success') {
+ OC.dialogs.alert(jsondata.data.message, t('core', 'Error'));
+ }
+ }
+ });
+ },
+ doDelete:function(categories, type, cb) {
+ if(!type && !this.type) {
+ throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
+ }
+ type = type ? type : this.type;
if(categories == '' || categories == undefined) {
OC.dialogs.alert(t('core', 'No categories selected for deletion.'), t('core', 'Error'));
return false;
}
- categories += '&app=' + OCCategories.app;
- $.post(OC.filePath(OCCategories.app, 'ajax', 'categories/delete.php'), categories, OCCategories._processDeleteResult)
- .error(function(xhr){
- if (xhr.status == 404) {
- $.post(OC.filePath('core', 'ajax', 'vcategories/delete.php'), categories, OCCategories._processDeleteResult);
- }
- });
+ var self = this;
+ var q = categories + '&type=' + type;
+ if(this.app) {
+ q += '&app=' + this.app;
+ $.post(OC.filePath(this.app, 'ajax', 'categories/delete.php'), q, function(jsondata) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
+ } else {
+ self._processDeleteResult(jsondata);
+ }
+ });
+ } else {
+ $.post(OC.filePath('core', 'ajax', 'vcategories/delete.php'), q, function(jsondata) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
+ } else {
+ self._processDeleteResult(jsondata);
+ }
+ });
+ }
},
- add:function(category){
- $.getJSON(OC.filePath('core', 'ajax', 'vcategories/add.php'),{'category':category, 'app':OCCategories.app},function(jsondata){
- if(jsondata.status == 'success'){
- OCCategories._update(jsondata.data.categories);
+ add:function(category, type, cb) {
+ if(!type && !this.type) {
+ throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') };
+ }
+ type = type ? type : this.type;
+ $.post(OC.filePath('core', 'ajax', 'vcategories/add.php'),{'category':category, 'type':type},function(jsondata) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
} else {
- OC.dialogs.alert(jsondata.data.message, 'Error');
+ if(jsondata.status === 'success') {
+ OCCategories._update(jsondata.data.categories);
+ } else {
+ OC.dialogs.alert(jsondata.data.message, 'Error');
+ }
}
});
- return false;
},
- rescan:function(){
- $.getJSON(OC.filePath(OCCategories.app, 'ajax', 'categories/rescan.php'),function(jsondata, status, xhr){
- if(jsondata.status == 'success'){
- OCCategories._update(jsondata.data.categories);
+ rescan:function(app, cb) {
+ if(!app && !this.app) {
+ throw { name: 'MissingParameter', message: t('core', 'The app name is not specified.') };
+ }
+ app = app ? app : this.app;
+ $.getJSON(OC.filePath(app, 'ajax', 'categories/rescan.php'),function(jsondata, status, xhr) {
+ if(typeof cb == 'function') {
+ cb(jsondata);
} else {
- OC.dialogs.alert(jsondata.data.message, 'Error');
+ if(jsondata.status === 'success') {
+ OCCategories._update(jsondata.data.categories);
+ } else {
+ OC.dialogs.alert(jsondata.data.message, 'Error');
+ }
}
}).error(function(xhr){
if (xhr.status == 404) {
- OC.dialogs.alert('The required file ' + OC.filePath(OCCategories.app, 'ajax', 'categories/rescan.php') + ' is not installed!', 'Error');
+ var errormessage = t('core', 'The required file {file} is not installed!',
+ {file: OC.filePath(app, 'ajax', 'categories/rescan.php')}, t('core', 'Error'));
+ if(typeof cb == 'function') {
+ cb({status:'error', data:{message:errormessage}});
+ } else {
+ OC.dialogs.alert(errormessage);
+ }
}
});
},
- _update:function(categories){
+ _update:function(categories) {
var categorylist = $('#categorylist');
categorylist.find('li').remove();
for(var category in categories) {
var item = '<li><input type="checkbox" name="categories" value="' + categories[category] + '" />' + categories[category] + '</li>';
$(item).appendTo(categorylist);
}
- if(OCCategories.changed != undefined) {
+ if(typeof OCCategories.changed === 'function') {
OCCategories.changed(categories);
}
}
diff --git a/core/routes.php b/core/routes.php
index 6f999356689..fc511d403d8 100644
--- a/core/routes.php
+++ b/core/routes.php
@@ -24,6 +24,12 @@ $this->create('core_ajax_vcategories_add', '/core/ajax/vcategories/add.php')
->actionInclude('core/ajax/vcategories/add.php');
$this->create('core_ajax_vcategories_delete', '/core/ajax/vcategories/delete.php')
->actionInclude('core/ajax/vcategories/delete.php');
+$this->create('core_ajax_vcategories_addtofavorites', '/core/ajax/vcategories/addToFavorites.php')
+ ->actionInclude('core/ajax/vcategories/addToFavorites.php');
+$this->create('core_ajax_vcategories_removefromfavorites', '/core/ajax/vcategories/removeFromFavorites.php')
+ ->actionInclude('core/ajax/vcategories/removeFromFavorites.php');
+$this->create('core_ajax_vcategories_favorites', '/core/ajax/vcategories/favorites.php')
+ ->actionInclude('core/ajax/vcategories/favorites.php');
$this->create('core_ajax_vcategories_edit', '/core/ajax/vcategories/edit.php')
->actionInclude('core/ajax/vcategories/edit.php');
// Routing
diff --git a/core/templates/edit_categories_dialog.php b/core/templates/edit_categories_dialog.php
index 8997fa586bd..d0b7b5ee62a 100644
--- a/core/templates/edit_categories_dialog.php
+++ b/core/templates/edit_categories_dialog.php
@@ -6,11 +6,14 @@ $categories = isset($_['categories'])?$_['categories']:array();
<form method="post" id="categoryform">
<div class="scrollarea">
<ul id="categorylist">
- <?php foreach($categories as $category) { ?>
+ <?php foreach($categories as $category): ?>
<li><input type="checkbox" name="categories[]" value="<?php echo $category; ?>" /><?php echo $category; ?></li>
- <?php } ?>
+ <?php endforeach; ?>
</ul>
</div>
- <div class="bottombuttons"><input type="text" id="category_addinput" name="category" /><button id="category_addbutton" disabled="disabled"><?php echo $l->t('Add'); ?></button></div>
+ <div class="bottombuttons">
+ <input type="text" id="category_addinput" name="category" />
+ <button id="category_addbutton" disabled="disabled"><?php echo $l->t('Add'); ?></button>
+ </div>
</form>
</div>
diff --git a/db_structure.xml b/db_structure.xml
index 99a30cb6137..851c8aa998d 100644
--- a/db_structure.xml
+++ b/db_structure.xml
@@ -671,4 +671,125 @@
</table>
+ <table>
+
+ <name>*dbprefix*vcategory</name>
+
+ <declaration>
+
+ <field>
+ <name>id</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <autoincrement>1</autoincrement>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>uid</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>64</length>
+ </field>
+
+ <field>
+ <name>type</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>64</length>
+ </field>
+
+ <field>
+ <name>category</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>255</length>
+ </field>
+
+ <index>
+ <name>uid_index</name>
+ <field>
+ <name>uid</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+
+ <index>
+ <name>type_index</name>
+ <field>
+ <name>type</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+
+ <index>
+ <name>category_index</name>
+ <field>
+ <name>category</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+
+ </declaration>
+ </table>
+
+ <table>
+
+ <name>*dbprefix*vcategory_to_object</name>
+
+ <declaration>
+
+ <field>
+ <name>objid</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>categoryid</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>type</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>64</length>
+ </field>
+
+ <index>
+ <primary>true</primary>
+ <unique>true</unique>
+ <name>category_object_index</name>
+ <field>
+ <name>categoryid</name>
+ <sorting>ascending</sorting>
+ </field>
+ <field>
+ <name>objid</name>
+ <sorting>ascending</sorting>
+ </field>
+ <field>
+ <name>type</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+
+ </declaration>
+
+ </table>
+
</database>
diff --git a/lib/db.php b/lib/db.php
index fba2687967f..de42626563d 100644
--- a/lib/db.php
+++ b/lib/db.php
@@ -542,6 +542,78 @@ class OC_DB {
}
/**
+ * @brief Insert a row if a matching row doesn't exists.
+ * @param string $table. The table to insert into in the form '*PREFIX*tableName'
+ * @param array $input. An array of fieldname/value pairs
+ * @returns The return value from PDOStatementWrapper->execute()
+ */
+ public static function insertIfNotExist($table, $input) {
+ self::connect();
+ $prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
+ $table = str_replace( '*PREFIX*', $prefix, $table );
+
+ if(is_null(self::$type)) {
+ self::$type=OC_Config::getValue( "dbtype", "sqlite" );
+ }
+ $type = self::$type;
+
+ $query = '';
+ // differences in escaping of table names ('`' for mysql) and getting the current timestamp
+ if( $type == 'sqlite' || $type == 'sqlite3' ) {
+ // NOTE: For SQLite we have to use this clumsy approach
+ // otherwise all fieldnames used must have a unique key.
+ $query = 'SELECT * FROM "' . $table . '" WHERE ';
+ foreach($input as $key => $value) {
+ $query .= $key . " = '" . $value . '\' AND ';
+ }
+ $query = substr($query, 0, strlen($query) - 5);
+ try {
+ $stmt = self::prepare($query);
+ $result = $stmt->execute();
+ } catch(PDOException $e) {
+ $entry = 'DB Error: "'.$e->getMessage() . '"<br />';
+ $entry .= 'Offending command was: ' . $query . '<br />';
+ OC_Log::write('core', $entry, OC_Log::FATAL);
+ error_log('DB error: '.$entry);
+ die( $entry );
+ }
+
+ if($result->numRows() == 0) {
+ $query = 'INSERT INTO "' . $table . '" ("'
+ . implode('","', array_keys($input)) . '") VALUES("'
+ . implode('","', array_values($input)) . '")';
+ } else {
+ return true;
+ }
+ } elseif( $type == 'pgsql' || $type == 'oci' || $type == 'mysql') {
+ $query = 'INSERT INTO `' .$table . '` ('
+ . implode(',', array_keys($input)) . ') SELECT \''
+ . implode('\',\'', array_values($input)) . '\' FROM ' . $table . ' WHERE ';
+
+ foreach($input as $key => $value) {
+ $query .= $key . " = '" . $value . '\' AND ';
+ }
+ $query = substr($query, 0, strlen($query) - 5);
+ $query .= ' HAVING COUNT(*) = 0';
+ }
+
+ // TODO: oci should be use " (quote) instead of ` (backtick).
+ //OC_Log::write('core', __METHOD__ . ', type: ' . $type . ', query: ' . $query, OC_Log::DEBUG);
+
+ try {
+ $result = self::prepare($query);
+ } catch(PDOException $e) {
+ $entry = 'DB Error: "'.$e->getMessage() . '"<br />';
+ $entry .= 'Offending command was: ' . $query.'<br />';
+ OC_Log::write('core', $entry, OC_Log::FATAL);
+ error_log('DB error: ' . $entry);
+ die( $entry );
+ }
+
+ return $result->execute();
+ }
+
+ /**
* @brief does minor changes to query
* @param string $query Query string
* @return string corrected query string
diff --git a/lib/public/db.php b/lib/public/db.php
index d2484b6eb83..92ff8f93a22 100644
--- a/lib/public/db.php
+++ b/lib/public/db.php
@@ -46,6 +46,27 @@ class DB {
}
/**
+ * @brief Insert a row if a matching row doesn't exists.
+ * @param $table string The table name (will replace *PREFIX*) to perform the replace on.
+ * @param $input array
+ *
+ * The input array if in the form:
+ *
+ * array ( 'id' => array ( 'value' => 6,
+ * 'key' => true
+ * ),
+ * 'name' => array ('value' => 'Stoyan'),
+ * 'family' => array ('value' => 'Stefanov'),
+ * 'birth_date' => array ('value' => '1975-06-20')
+ * );
+ * @returns true/false
+ *
+ */
+ public static function insertIfNotExist($table, $input) {
+ return(\OC_DB::insertIfNotExist($table, $input));
+ }
+
+ /**
* @brief gets last value of autoincrement
* @param $table string The optional table name (will replace *PREFIX*) and add sequence suffix
* @returns id
diff --git a/lib/util.php b/lib/util.php
index 73b72bad1a5..da57b226ba0 100755
--- a/lib/util.php
+++ b/lib/util.php
@@ -95,7 +95,7 @@ class OC_Util {
*/
public static function getVersion() {
// hint: We only can count up. So the internal version number of ownCloud 4.5 will be 4.90.0. This is not visible to the user
- return array(4, 91, 00);
+ return array(4, 91, 01);
}
/**
diff --git a/lib/vcategories.php b/lib/vcategories.php
index 46256def9c4..406a4eb1074 100644
--- a/lib/vcategories.php
+++ b/lib/vcategories.php
@@ -21,6 +21,7 @@
*
*/
+OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_VCategories', 'post_deleteUser');
/**
* Class for easy access to categories in VCARD, VEVENT, VTODO and VJOURNAL.
@@ -28,50 +29,261 @@
* anything else that is either parsed from a vobject or that the user chooses
* to add.
* 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
+ * are entered in. If a user already has a category 'family' for a type, 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).
*/
class OC_VCategories {
- const PREF_CATEGORIES_LABEL = 'extra_categories';
+
/**
* Categories
*/
private $categories = array();
- private $app = null;
+ /**
+ * Used for storing objectid/categoryname pairs while rescanning.
+ */
+ private static $relations = array();
+
+ private $type = null;
private $user = null;
+ const CATEGORY_TABLE = '*PREFIX*vcategory';
+ const RELATION_TABLE = '*PREFIX*vcategory_to_object';
+
+ const CATEGORY_FAVORITE = '_$!<Favorite>!$_';
+
+ const FORMAT_LIST = 0;
+ const FORMAT_MAP = 1;
+
/**
* @brief Constructor.
- * @param $app The application identifier e.g. 'contacts' or 'calendar'.
+ * @param $type The type identifier e.g. 'contact' or 'event'.
* @param $user The user whos data the object will operate on. This
* parameter should normally be omitted but to make an app able to
* update categories for all users it is made possible to provide it.
* @param $defcategories An array of default categories to be used if none is stored.
*/
- public function __construct($app, $user=null, $defcategories=array()) {
- $this->app = $app;
+ public function __construct($type, $user=null, $defcategories=array()) {
+ $this->type = $type;
$this->user = is_null($user) ? OC_User::getUser() : $user;
- $categories = trim(OC_Preferences::getValue($this->user, $app, self::PREF_CATEGORIES_LABEL, ''));
- if ($categories) {
- $categories = @unserialize($categories);
+
+ $this->loadCategories();
+ OCP\Util::writeLog('core', __METHOD__ . ', categories: '
+ . print_r($this->categories, true),
+ OCP\Util::DEBUG
+ );
+
+ if($defcategories && count($this->categories) === 0) {
+ $this->addMulti($defcategories, true);
+ }
+ }
+
+ /**
+ * @brief Load categories from db.
+ */
+ private function loadCategories() {
+ $this->categories = array();
+ $result = null;
+ $sql = 'SELECT `id`, `category` FROM `' . self::CATEGORY_TABLE . '` '
+ . 'WHERE `uid` = ? AND `type` = ? ORDER BY `category`';
+ try {
+ $stmt = OCP\DB::prepare($sql);
+ $result = $stmt->execute(array($this->user, $this->type));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ }
+
+ if(!is_null($result)) {
+ while( $row = $result->fetchRow()) {
+ // The keys are prefixed because array_search wouldn't work otherwise :-/
+ $this->categories[$row['id']] = $row['category'];
+ }
+ }
+ OCP\Util::writeLog('core', __METHOD__.', categories: ' . print_r($this->categories, true),
+ OCP\Util::DEBUG);
+ }
+
+
+ /**
+ * @brief Check if any categories are saved for this type and user.
+ * @returns boolean.
+ * @param $type The type identifier e.g. 'contact' or 'event'.
+ * @param $user The user whos categories will be checked. If not set current user will be used.
+ */
+ public static function isEmpty($type, $user = null) {
+ $user = is_null($user) ? OC_User::getUser() : $user;
+ $sql = 'SELECT COUNT(*) FROM `' . self::CATEGORY_TABLE . '` '
+ . 'WHERE `uid` = ? AND `type` = ?';
+ try {
+ $stmt = OCP\DB::prepare($sql);
+ $result = $stmt->execute(array($user, $type));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+ return ($result->numRows() == 0);
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
}
- $this->categories = is_array($categories) ? $categories : $defcategories;
}
/**
* @brief Get the categories for a specific user.
+ * @param
* @returns array containing the categories as strings.
*/
- public function categories() {
- //OC_Log::write('core', 'OC_VCategories::categories: '.print_r($this->categories, true), OC_Log::DEBUG);
+ public function categories($format = null) {
if(!$this->categories) {
return array();
}
- usort($this->categories, 'strnatcasecmp'); // usort to also renumber the keys
- return $this->categories;
+ $categories = array_values($this->categories);
+ uasort($categories, 'strnatcasecmp');
+ if($format == self::FORMAT_MAP) {
+ $catmap = array();
+ foreach($categories as $category) {
+ if($category !== self::CATEGORY_FAVORITE) {
+ $catmap[] = array(
+ 'id' => $this->array_searchi($category, $this->categories),
+ 'name' => $category
+ );
+ }
+ }
+ return $catmap;
+ }
+
+ // Don't add favorites to normal categories.
+ $favpos = array_search(self::CATEGORY_FAVORITE, $categories);
+ if($favpos !== false) {
+ return array_splice($categories, $favpos);
+ } else {
+ return $categories;
+ }
+ }
+
+ /**
+ * Get the a list if items belonging to $category.
+ *
+ * Throws an exception if the category could not be found.
+ *
+ * @param string|integer $category Category id or name.
+ * @returns array An array of object ids or false on error.
+ */
+ public function idsForCategory($category) {
+ $result = null;
+ if(is_numeric($category)) {
+ $catid = $category;
+ } elseif(is_string($category)) {
+ $catid = $this->array_searchi($category, $this->categories);
+ }
+ OCP\Util::writeLog('core', __METHOD__.', category: '.$catid.' '.$category, OCP\Util::DEBUG);
+ if($catid === false) {
+ $l10n = OC_L10N::get('core');
+ throw new Exception(
+ $l10n->t('Could not find category "%s"', $category)
+ );
+ }
+
+ $ids = array();
+ $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
+ . '` WHERE `categoryid` = ?';
+
+ try {
+ $stmt = OCP\DB::prepare($sql);
+ $result = $stmt->execute(array($catid));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
+ }
+
+ if(!is_null($result)) {
+ while( $row = $result->fetchRow()) {
+ $ids[] = (int)$row['objid'];
+ }
+ }
+
+ return $ids;
+ }
+
+ /**
+ * Get the a list if items belonging to $category.
+ *
+ * Throws an exception if the category could not be found.
+ *
+ * @param string|integer $category Category id or name.
+ * @param array $tableinfo Array in the form {'tablename' => table, 'fields' => ['field1', 'field2']}
+ * @param int $limit
+ * @param int $offset
+ *
+ * This generic method queries a table assuming that the id
+ * field is called 'id' and the table name provided is in
+ * the form '*PREFIX*table_name'.
+ *
+ * If the category name cannot be resolved an exception is thrown.
+ *
+ * TODO: Maybe add the getting permissions for objects?
+ *
+ * @returns array containing the resulting items or false on error.
+ */
+ public function itemsForCategory($category, $tableinfo, $limit = null, $offset = null) {
+ $result = null;
+ if(is_numeric($category)) {
+ $catid = $category;
+ } elseif(is_string($category)) {
+ $catid = $this->array_searchi($category, $this->categories);
+ }
+ OCP\Util::writeLog('core', __METHOD__.', category: '.$catid.' '.$category, OCP\Util::DEBUG);
+ if($catid === false) {
+ $l10n = OC_L10N::get('core');
+ throw new Exception(
+ $l10n->t('Could not find category "%s"', $category)
+ );
+ }
+ $fields = '';
+ foreach($tableinfo['fields'] as $field) {
+ $fields .= '`' . $tableinfo['tablename'] . '`.`' . $field . '`,';
+ }
+ $fields = substr($fields, 0, -1);
+
+ $items = array();
+ $sql = 'SELECT `' . self::RELATION_TABLE . '`.`categoryid`, ' . $fields
+ . ' FROM `' . $tableinfo['tablename'] . '` JOIN `'
+ . self::RELATION_TABLE . '` ON `' . $tableinfo['tablename']
+ . '`.`id` = `' . self::RELATION_TABLE . '`.`objid` WHERE `'
+ . self::RELATION_TABLE . '`.`categoryid` = ?';
+
+ try {
+ $stmt = OCP\DB::prepare($sql, $limit, $offset);
+ $result = $stmt->execute(array($catid));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
+ }
+
+ if(!is_null($result)) {
+ while( $row = $result->fetchRow()) {
+ $items[] = $row;
+ }
+ }
+ //OCP\Util::writeLog('core', __METHOD__.', count: ' . count($items), OCP\Util::DEBUG);
+ //OCP\Util::writeLog('core', __METHOD__.', sql: ' . $sql, OCP\Util::DEBUG);
+
+ return $items;
}
/**
@@ -84,22 +296,51 @@ class OC_VCategories {
}
/**
- * @brief Add a new category name.
+ * @brief Add a new category.
+ * @param $name A string with a name of the category
+ * @returns int the id of the added category or false if it already exists.
+ */
+ public function add($name) {
+ OCP\Util::writeLog('core', __METHOD__.', name: ' . $name, OCP\Util::DEBUG);
+ if($this->hasCategory($name)) {
+ OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', OCP\Util::DEBUG);
+ return false;
+ }
+ OCP\DB::insertIfNotExist(self::CATEGORY_TABLE,
+ array(
+ 'uid' => $this->user,
+ 'type' => $this->type,
+ 'category' => $name,
+ ));
+ $id = OCP\DB::insertid(self::CATEGORY_TABLE);
+ OCP\Util::writeLog('core', __METHOD__.', id: ' . $id, OCP\Util::DEBUG);
+ $this->categories[$id] = $name;
+ return $id;
+ }
+
+ /**
+ * @brief Add a new category.
* @param $names A string with a name or an array of strings containing
* the name(s) of the categor(y|ies) to add.
* @param $sync bool When true, save the categories
+ * @param $id int Optional object id to add to this|these categor(y|ies)
* @returns bool Returns false on error.
*/
- public function add($names, $sync=false) {
+ public function addMulti($names, $sync=false, $id = null) {
if(!is_array($names)) {
$names = array($names);
}
$names = array_map('trim', $names);
$newones = array();
foreach($names as $name) {
- if(($this->in_arrayi($name, $this->categories) == false) && $name != '') {
+ if(($this->in_arrayi(
+ $name, $this->categories) == false) && $name != '') {
$newones[] = $name;
}
+ if(!is_null($id) ) {
+ // Insert $objectid, $categoryid pairs if not exist.
+ self::$relations[] = array('objid' => $id, 'category' => $name);
+ }
}
if(count($newones) > 0) {
$this->categories = array_merge($this->categories, $newones);
@@ -114,8 +355,8 @@ class OC_VCategories {
* @brief Extracts categories from a vobject and add the ones not already present.
* @param $vobject The instance of OC_VObject to load the categories from.
*/
- public function loadFromVObject($vobject, $sync=false) {
- $this->add($vobject->getAsArray('CATEGORIES'), $sync);
+ public function loadFromVObject($id, $vobject, $sync=false) {
+ $this->addMulti($vobject->getAsArray('CATEGORIES'), $sync, $id);
}
/**
@@ -128,23 +369,62 @@ class OC_VCategories {
* $result = $stmt->execute();
* $objects = array();
* if(!is_null($result)) {
- * while( $row = $result->fetchRow()) {
- * $objects[] = $row['carddata'];
+ * while( $row = $result->fetchRow()){
+ * $objects[] = array($row['id'], $row['carddata']);
* }
* }
* $categories->rescan($objects);
*/
public function rescan($objects, $sync=true, $reset=true) {
+
if($reset === true) {
+ $result = null;
+ // Find all objectid/categoryid pairs.
+ try {
+ $stmt = OCP\DB::prepare('SELECT `id` FROM `' . self::CATEGORY_TABLE . '` '
+ . 'WHERE `uid` = ? AND `type` = ?');
+ $result = $stmt->execute(array($this->user, $this->type));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ }
+
+ // And delete them.
+ if(!is_null($result)) {
+ $stmt = OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
+ . 'WHERE `categoryid` = ? AND `type`= ?');
+ while( $row = $result->fetchRow()) {
+ $stmt->execute(array($row['id'], $this->type));
+ }
+ }
+ try {
+ $stmt = OCP\DB::prepare('DELETE FROM `' . self::CATEGORY_TABLE . '` '
+ . 'WHERE `uid` = ? AND `type` = ?');
+ $result = $stmt->execute(array($this->user, $this->type));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return;
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__ . ', exception: '
+ . $e->getMessage(), OCP\Util::ERROR);
+ return;
+ }
$this->categories = array();
}
+ // Parse all the VObjects
foreach($objects as $object) {
- //OC_Log::write('core', 'OC_VCategories::rescan: '.substr($object, 0, 100).'(...)', OC_Log::DEBUG);
- $vobject = OC_VObject::parse($object);
+ $vobject = OC_VObject::parse($object[1]);
if(!is_null($vobject)) {
- $this->loadFromVObject($vobject, $sync);
+ // Load the categories
+ $this->loadFromVObject($object[0], $vobject, $sync);
} else {
- OC_Log::write('core', 'OC_VCategories::rescan, unable to parse. ID: '.', '.substr($object, 0, 100).'(...)', OC_Log::DEBUG);
+ OC_Log::write('core', __METHOD__ . ', unable to parse. ID: ' . ', '
+ . substr($object, 0, 100) . '(...)', OC_Log::DEBUG);
}
}
$this->save();
@@ -155,16 +435,224 @@ class OC_VCategories {
*/
private function save() {
if(is_array($this->categories)) {
- usort($this->categories, 'strnatcasecmp'); // usort to also renumber the keys
- $escaped_categories = serialize($this->categories);
- OC_Preferences::setValue($this->user, $this->app, self::PREF_CATEGORIES_LABEL, $escaped_categories);
- OC_Log::write('core', 'OC_VCategories::save: '.print_r($this->categories, true), OC_Log::DEBUG);
+ foreach($this->categories as $category) {
+ OCP\DB::insertIfNotExist(self::CATEGORY_TABLE,
+ array(
+ 'uid' => $this->user,
+ 'type' => $this->type,
+ 'category' => $category,
+ ));
+ }
+ // reload categories to get the proper ids.
+ $this->loadCategories();
+ // Loop through temporarily cached objectid/categoryname pairs
+ // and save relations.
+ $categories = $this->categories;
+ // For some reason this is needed or array_search(i) will return 0..?
+ ksort($categories);
+ foreach(self::$relations as $relation) {
+ $catid = $this->array_searchi($relation['category'], $categories);
+ OC_Log::write('core', __METHOD__ . 'catid, ' . $relation['category'] . ' ' . $catid, OC_Log::DEBUG);
+ if($catid) {
+ OCP\DB::insertIfNotExist(self::RELATION_TABLE,
+ array(
+ 'objid' => $relation['objid'],
+ 'categoryid' => $catid,
+ 'type' => $this->type,
+ ));
+ }
+ }
+ self::$relations = array(); // reset
} else {
- OC_Log::write('core', 'OC_VCategories::save: $this->categories is not an array! '.print_r($this->categories, true), OC_Log::ERROR);
+ OC_Log::write('core', __METHOD__.', $this->categories is not an array! '
+ . print_r($this->categories, true), OC_Log::ERROR);
}
}
/**
+ * @brief Delete categories and category/object relations for a user.
+ * For hooking up on post_deleteUser
+ * @param string $uid The user id for which entries should be purged.
+ */
+ public static function post_deleteUser($arguments) {
+ // Find all objectid/categoryid pairs.
+ $result = null;
+ try {
+ $stmt = OCP\DB::prepare('SELECT `id` FROM `' . self::CATEGORY_TABLE . '` '
+ . 'WHERE `uid` = ?');
+ $result = $stmt->execute(array($arguments['uid']));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ }
+
+ if(!is_null($result)) {
+ try {
+ $stmt = OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
+ . 'WHERE `categoryid` = ?');
+ while( $row = $result->fetchRow()) {
+ try {
+ $stmt->execute(array($row['id']));
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ }
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ }
+ }
+ try {
+ $stmt = OCP\DB::prepare('DELETE FROM `' . self::CATEGORY_TABLE . '` '
+ . 'WHERE `uid` = ? AND');
+ $result = $stmt->execute(array($arguments['uid']));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__ . ', exception: '
+ . $e->getMessage(), OCP\Util::ERROR);
+ }
+ }
+
+ /**
+ * @brief Delete category/object relations from the db
+ * @param int $id The id of the object
+ * @param string $type The type of object (event/contact/task/journal).
+ * Defaults to the type set in the instance
+ * @returns boolean Returns false on error.
+ */
+ public function purgeObject($id, $type = null) {
+ $type = is_null($type) ? $this->type : $type;
+ try {
+ $stmt = OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
+ . 'WHERE `objid` = ? AND `type`= ?');
+ $result = $stmt->execute(array($id, $type));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ return false;
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Get favorites for an object type
+ *
+ * @param string $type The type of object (event/contact/task/journal).
+ * Defaults to the type set in the instance
+ * @returns array An array of object ids.
+ */
+ public function getFavorites($type = null) {
+ $type = is_null($type) ? $this->type : $type;
+
+ try {
+ return $this->idsForCategory(self::CATEGORY_FAVORITE);
+ } catch(Exception $e) {
+ // No favorites
+ return array();
+ }
+ }
+
+ /**
+ * Add an object to favorites
+ *
+ * @param int $objid The id of the object
+ * @param string $type The type of object (event/contact/task/journal).
+ * Defaults to the type set in the instance
+ * @returns boolean
+ */
+ public function addToFavorites($objid, $type = null) {
+ $type = is_null($type) ? $this->type : $type;
+ if(!$this->hasCategory(self::CATEGORY_FAVORITE)) {
+ $this->add(self::CATEGORY_FAVORITE, true);
+ }
+ return $this->addToCategory($objid, self::CATEGORY_FAVORITE, $type);
+ }
+
+ /**
+ * Remove an object from favorites
+ *
+ * @param int $objid The id of the object
+ * @param string $type The type of object (event/contact/task/journal).
+ * Defaults to the type set in the instance
+ * @returns boolean
+ */
+ public function removeFromFavorites($objid, $type = null) {
+ $type = is_null($type) ? $this->type : $type;
+ return $this->removeFromCategory($objid, self::CATEGORY_FAVORITE, $type);
+ }
+
+ /**
+ * @brief Creates a category/object relation.
+ * @param int $objid The id of the object
+ * @param int|string $category The id or name of the category
+ * @param string $type The type of object (event/contact/task/journal).
+ * Defaults to the type set in the instance
+ * @returns boolean Returns false on database error.
+ */
+ public function addToCategory($objid, $category, $type = null) {
+ $type = is_null($type) ? $this->type : $type;
+ if(is_string($category) && !is_numeric($category)) {
+ if(!$this->hasCategory($category)) {
+ $this->add($category, true);
+ }
+ $categoryid = $this->array_searchi($category, $this->categories);
+ } else {
+ $categoryid = $category;
+ }
+ try {
+ OCP\DB::insertIfNotExist(self::RELATION_TABLE,
+ array(
+ 'objid' => $objid,
+ 'categoryid' => $categoryid,
+ 'type' => $type,
+ ));
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * @brief Delete single category/object relation from the db
+ * @param int $objid The id of the object
+ * @param int|string $category The id or name of the category
+ * @param string $type The type of object (event/contact/task/journal).
+ * Defaults to the type set in the instance
+ * @returns boolean
+ */
+ public function removeFromCategory($objid, $category, $type = null) {
+ $type = is_null($type) ? $this->type : $type;
+ $categoryid = (is_string($category) && !is_numeric($category))
+ ? $this->array_searchi($category, $this->categories)
+ : $category;
+ try {
+ $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
+ . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
+ OCP\Util::writeLog('core', __METHOD__.', sql: ' . $objid . ' ' . $categoryid . ' ' . $type,
+ OCP\Util::DEBUG);
+ $stmt = OCP\DB::prepare($sql);
+ $stmt->execute(array($objid, $categoryid, $type));
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
+ }
+ return true;
+ }
+
+ /**
* @brief Delete categories from the db and from all the vobject supplied
* @param $names An array of categories to delete
* @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table.
@@ -173,37 +661,87 @@ class OC_VCategories {
if(!is_array($names)) {
$names = array($names);
}
- OC_Log::write('core', 'OC_VCategories::delete, before: '.print_r($this->categories, true), OC_Log::DEBUG);
+
+ OC_Log::write('core', __METHOD__ . ', before: '
+ . print_r($this->categories, true), OC_Log::DEBUG);
foreach($names as $name) {
- OC_Log::write('core', 'OC_VCategories::delete: '.$name, OC_Log::DEBUG);
+ $id = null;
+ OC_Log::write('core', __METHOD__.', '.$name, OC_Log::DEBUG);
if($this->hasCategory($name)) {
- //OC_Log::write('core', 'OC_VCategories::delete: '.$name.' got it', OC_Log::DEBUG);
- unset($this->categories[$this->array_searchi($name, $this->categories)]);
+ $id = $this->array_searchi($name, $this->categories);
+ unset($this->categories[$id]);
+ }
+ try {
+ $stmt = OCP\DB::prepare('DELETE FROM `' . self::CATEGORY_TABLE . '` WHERE '
+ . '`uid` = ? AND `type` = ? AND `category` = ?');
+ $result = $stmt->execute(array($this->user, $this->type, $name));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__ . ', exception: '
+ . $e->getMessage(), OCP\Util::ERROR);
+ }
+ if(!is_null($id) && $id !== false) {
+ try {
+ $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
+ . 'WHERE `categoryid` = ?';
+ $stmt = OCP\DB::prepare($sql);
+ $result = $stmt->execute(array($id));
+ if (OC_DB::isError($result)) {
+ OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
+ }
+ } catch(Exception $e) {
+ OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
+ OCP\Util::ERROR);
+ return false;
+ }
}
}
- $this->save();
- OC_Log::write('core', 'OC_VCategories::delete, after: '.print_r($this->categories, true), OC_Log::DEBUG);
+ OC_Log::write('core', __METHOD__.', after: '
+ . print_r($this->categories, true), OC_Log::DEBUG);
if(!is_null($objects)) {
foreach($objects as $key=>&$value) {
$vobject = OC_VObject::parse($value[1]);
if(!is_null($vobject)) {
- $categories = $vobject->getAsArray('CATEGORIES');
- //OC_Log::write('core', 'OC_VCategories::delete, before: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
+ $object = null;
+ $componentname = '';
+ if (isset($vobject->VEVENT)) {
+ $object = $vobject->VEVENT;
+ $componentname = 'VEVENT';
+ } else
+ if (isset($vobject->VTODO)) {
+ $object = $vobject->VTODO;
+ $componentname = 'VTODO';
+ } else
+ if (isset($vobject->VJOURNAL)) {
+ $object = $vobject->VJOURNAL;
+ $componentname = 'VJOURNAL';
+ } else {
+ $object = $vobject;
+ }
+ $categories = $object->getAsArray('CATEGORIES');
foreach($names as $name) {
$idx = $this->array_searchi($name, $categories);
- //OC_Log::write('core', 'OC_VCategories::delete, loop: '.$name.', '.print_r($idx, true), OC_Log::DEBUG);
if($idx !== false) {
- OC_Log::write('core', 'OC_VCategories::delete, unsetting: '.$categories[$this->array_searchi($name, $categories)], OC_Log::DEBUG);
+ OC_Log::write('core', __METHOD__
+ .', unsetting: '
+ . $categories[$this->array_searchi($name, $categories)],
+ OC_Log::DEBUG);
unset($categories[$this->array_searchi($name, $categories)]);
- //unset($categories[$idx]);
}
}
- //OC_Log::write('core', 'OC_VCategories::delete, after: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
- $vobject->setString('CATEGORIES', implode(',', $categories));
+
+ $object->setString('CATEGORIES', implode(',', $categories));
+ if($vobject !== $object) {
+ $vobject[$componentname] = $object;
+ }
$value[1] = $vobject->serialize();
$objects[$key] = $value;
} else {
- OC_Log::write('core', 'OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG);
+ OC_Log::write('core', __METHOD__
+ .', unable to parse. ID: ' . $value[0] . ', '
+ . substr($value[1], 0, 50) . '(...)', OC_Log::DEBUG);
}
}
}
@@ -224,5 +762,5 @@ class OC_VCategories {
}
return array_search(strtolower($needle), array_map('strtolower', $haystack));
}
-
}
+
diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml
index 03d7502c441..af2e5ce3439 100644
--- a/tests/data/db_structure.xml
+++ b/tests/data/db_structure.xml
@@ -135,4 +135,47 @@
</table>
+ <table>
+
+ <name>*dbprefix*vcategory</name>
+
+ <declaration>
+
+ <field>
+ <name>id</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <autoincrement>1</autoincrement>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>uid</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>64</length>
+ </field>
+
+ <field>
+ <name>type</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>64</length>
+ </field>
+
+ <field>
+ <name>category</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>255</length>
+ </field>
+
+ </declaration>
+ </table>
+
</database>
diff --git a/tests/lib/db.php b/tests/lib/db.php
index 2344f7d8ec4..c2eb38dae83 100644
--- a/tests/lib/db.php
+++ b/tests/lib/db.php
@@ -24,6 +24,7 @@ class Test_DB extends UnitTestCase {
$this->test_prefix = $r;
$this->table1 = $this->test_prefix.'contacts_addressbooks';
$this->table2 = $this->test_prefix.'contacts_cards';
+ $this->table3 = $this->test_prefix.'vcategory';
}
public function tearDown() {
@@ -67,4 +68,66 @@ class Test_DB extends UnitTestCase {
$result = $query->execute(array('uri_3'));
$this->assertTrue($result);
}
+
+ public function testinsertIfNotExist() {
+ $categoryentries = array(
+ array('user' => 'test', 'type' => 'contact', 'category' => 'Family'),
+ array('user' => 'test', 'type' => 'contact', 'category' => 'Friends'),
+ array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'),
+ array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'),
+ array('user' => 'test', 'type' => 'contact', 'category' => 'School'),
+ );
+
+ foreach($categoryentries as $entry) {
+ $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3,
+ array(
+ 'uid' => $entry['user'],
+ 'type' => $entry['type'],
+ 'category' => $entry['category'],
+ ));
+ $this->assertTrue($result);
+ }
+
+ $query = OC_DB::prepare('SELECT * FROM *PREFIX*'.$this->table3);
+ $result = $query->execute();
+ $this->assertTrue($result);
+ $this->assertEqual('4', $result->numRows());
+ }
+
+ public function testinsertIfNotExistDontOverwrite() {
+ $fullname = 'fullname test';
+ $uri = 'uri_1';
+ $carddata = 'This is a vCard';
+
+ // Normal test to have same known data inserted.
+ $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)');
+ $result = $query->execute(array($fullname, $uri, $carddata));
+ $this->assertTrue($result);
+ $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
+ $result = $query->execute(array($uri));
+ $this->assertTrue($result);
+ $row = $result->fetchRow();
+ $this->assertArrayHasKey('carddata', $row);
+ $this->assertEqual($carddata, $row['carddata']);
+ $this->assertEqual('1', $result->numRows());
+
+ // Try to insert a new row
+ $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
+ array(
+ 'fullname' => $fullname,
+ 'uri' => $uri,
+ ));
+ $this->assertTrue($result);
+
+ $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
+ $result = $query->execute(array($uri));
+ $this->assertTrue($result);
+ $row = $result->fetchRow();
+ $this->assertArrayHasKey('carddata', $row);
+ // Test that previously inserted data isn't overwritten
+ $this->assertEqual($carddata, $row['carddata']);
+ // And that a new row hasn't been inserted.
+ $this->assertEqual('1', $result->numRows());
+
+ }
}
diff --git a/tests/lib/vcategories.php b/tests/lib/vcategories.php
new file mode 100644
index 00000000000..63516a063da
--- /dev/null
+++ b/tests/lib/vcategories.php
@@ -0,0 +1,117 @@
+<?php
+/**
+* ownCloud
+*
+* @author Thomas Tanghus
+* @copyright 2012 Thomas Tanghus (thomas@tanghus.net)
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+//require_once("../lib/template.php");
+
+class Test_VCategories extends UnitTestCase {
+
+ protected $objectType;
+ protected $user;
+ protected $backupGlobals = FALSE;
+
+ public function setUp() {
+
+ OC_User::clearBackends();
+ OC_User::useBackend('dummy');
+ $this->user = uniqid('user_');
+ $this->objectType = uniqid('type_');
+ OC_User::createUser($this->user, 'pass');
+ OC_User::setUserId($this->user);
+
+ }
+
+ public function tearDown() {
+ //$query = OC_DB::prepare('DELETE FROM `*PREFIX*vcategories` WHERE `item_type` = ?');
+ //$query->execute(array('test'));
+ }
+
+ public function testInstantiateWithDefaults() {
+ $defcategories = array('Friends', 'Family', 'Work', 'Other');
+
+ $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories);
+
+ $this->assertEqual(4, count($catmgr->categories()));
+ }
+
+ public function testAddCategories() {
+ $categories = array('Friends', 'Family', 'Work', 'Other');
+
+ $catmgr = new OC_VCategories($this->objectType, $this->user);
+
+ foreach($categories as $category) {
+ $result = $catmgr->add($category);
+ $this->assertTrue($result);
+ }
+
+ $this->assertFalse($catmgr->add('Family'));
+ $this->assertFalse($catmgr->add('fAMILY'));
+
+ $this->assertEqual(4, count($catmgr->categories()));
+ }
+
+ public function testdeleteCategories() {
+ $defcategories = array('Friends', 'Family', 'Work', 'Other');
+ $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories);
+ $this->assertEqual(4, count($catmgr->categories()));
+
+ $catmgr->delete('family');
+ $this->assertEqual(3, count($catmgr->categories()));
+
+ $catmgr->delete(array('Friends', 'Work', 'Other'));
+ $this->assertEqual(0, count($catmgr->categories()));
+
+ }
+
+ public function testAddToCategory() {
+ $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
+
+ $catmgr = new OC_VCategories($this->objectType, $this->user);
+
+ foreach($objids as $id) {
+ $catmgr->addToCategory($id, 'Family');
+ }
+
+ $this->assertEqual(1, count($catmgr->categories()));
+ $this->assertEqual(9, count($catmgr->idsForCategory('Family')));
+ }
+
+ /**
+ * @depends testAddToCategory
+ */
+ public function testRemoveFromCategory() {
+ $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
+
+ // Is this "legal"?
+ $this->testAddToCategory();
+ $catmgr = new OC_VCategories($this->objectType, $this->user);
+
+ foreach($objids as $id) {
+ $this->assertTrue(in_array($id, $catmgr->idsForCategory('Family')));
+ $catmgr->removeFromCategory($id, 'Family');
+ $this->assertFalse(in_array($id, $catmgr->idsForCategory('Family')));
+ }
+
+ $this->assertEqual(1, count($catmgr->categories()));
+ $this->assertEqual(0, count($catmgr->idsForCategory('Family')));
+ }
+
+}