2012-02-18 14:45:36 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
|
2012-02-21 01:32:57 +04:00
|
|
|
* @copyright 2012 Bart Visscher bartv@thisnet.nl
|
2012-02-18 14:45:36 +04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-10-03 00:46:37 +04:00
|
|
|
OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_VCategories', 'post_deleteUser');
|
2012-02-18 14:45:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for easy access to categories in VCARD, VEVENT, VTODO and VJOURNAL.
|
2012-02-21 01:32:57 +04:00
|
|
|
* A Category can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
|
|
|
|
* 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
|
2012-09-17 18:03:15 +04:00
|
|
|
* are entered in. If a user already has a category 'family' for a type, and
|
2012-02-21 01:32:57 +04:00
|
|
|
* tries to add a category named 'Family' it will be silently ignored.
|
2012-02-18 14:45:36 +04:00
|
|
|
*/
|
|
|
|
class OC_VCategories {
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-02-18 14:45:36 +04:00
|
|
|
/**
|
|
|
|
* Categories
|
|
|
|
*/
|
2012-11-04 15:24:49 +04:00
|
|
|
private $categories = array();
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
/**
|
|
|
|
* Used for storing objectid/categoryname pairs while rescanning.
|
|
|
|
*/
|
|
|
|
private static $relations = array();
|
2012-02-19 20:00:07 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
private $type = null;
|
2012-02-20 16:16:51 +04:00
|
|
|
private $user = null;
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-19 02:13:34 +04:00
|
|
|
const CATEGORY_TABLE = '*PREFIX*vcategory';
|
|
|
|
const RELATION_TABLE = '*PREFIX*vcategory_to_object';
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-19 02:13:34 +04:00
|
|
|
const CATEGORY_FAVORITE = '_$!<Favorite>!$_';
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
const FORMAT_LIST = 0;
|
|
|
|
const FORMAT_MAP = 1;
|
2012-02-21 01:32:57 +04:00
|
|
|
|
2012-02-18 14:45:36 +04:00
|
|
|
/**
|
|
|
|
* @brief Constructor.
|
2012-09-17 18:03:15 +04:00
|
|
|
* @param $type The type identifier e.g. 'contact' or 'event'.
|
2012-02-21 01:32:57 +04:00
|
|
|
* @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.
|
2012-03-07 19:39:56 +04:00
|
|
|
* @param $defcategories An array of default categories to be used if none is stored.
|
2012-02-18 14:45:36 +04:00
|
|
|
*/
|
2012-09-17 18:03:15 +04:00
|
|
|
public function __construct($type, $user=null, $defcategories=array()) {
|
|
|
|
$this->type = $type;
|
2012-02-21 03:02:27 +04:00
|
|
|
$this->user = is_null($user) ? OC_User::getUser() : $user;
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
$this->loadCategories();
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__ . ', categories: '
|
2012-11-04 15:24:49 +04:00
|
|
|
. print_r($this->categories, true),
|
2012-09-17 18:03:15 +04:00
|
|
|
OCP\Util::DEBUG
|
|
|
|
);
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-11-04 15:24:49 +04:00
|
|
|
if($defcategories && count($this->categories) === 0) {
|
2012-10-19 15:21:05 +04:00
|
|
|
$this->addMulti($defcategories, true);
|
2012-09-13 19:30:26 +04:00
|
|
|
}
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
/**
|
|
|
|
* @brief Load categories from db.
|
|
|
|
*/
|
|
|
|
private function loadCategories() {
|
2012-11-04 15:24:49 +04:00
|
|
|
$this->categories = array();
|
2012-09-17 18:03:15 +04:00
|
|
|
$result = null;
|
2012-10-19 02:13:34 +04:00
|
|
|
$sql = 'SELECT `id`, `category` FROM `' . self::CATEGORY_TABLE . '` '
|
2012-09-17 18:03:15 +04:00
|
|
|
. 'WHERE `uid` = ? AND `type` = ? ORDER BY `category`';
|
|
|
|
try {
|
|
|
|
$stmt = OCP\DB::prepare($sql);
|
|
|
|
$result = $stmt->execute(array($this->user, $this->type));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-09-17 18:03:15 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($result)) {
|
|
|
|
while( $row = $result->fetchRow()) {
|
|
|
|
// The keys are prefixed because array_search wouldn't work otherwise :-/
|
2012-11-04 15:24:49 +04:00
|
|
|
$this->categories[$row['id']] = $row['category'];
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-04 15:24:49 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', categories: ' . print_r($this->categories, true),
|
2012-10-19 04:26:36 +04:00
|
|
|
OCP\Util::DEBUG);
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
/**
|
|
|
|
* @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;
|
2012-10-19 02:13:34 +04:00
|
|
|
$sql = 'SELECT COUNT(*) FROM `' . self::CATEGORY_TABLE . '` '
|
2012-10-19 18:15:47 +04:00
|
|
|
. 'WHERE `uid` = ? AND `type` = ?';
|
2012-09-17 18:03:15 +04:00
|
|
|
try {
|
|
|
|
$stmt = OCP\DB::prepare($sql);
|
|
|
|
$result = $stmt->execute(array($user, $type));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
return ($result->numRows() == 0);
|
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-09-17 18:03:15 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-02-18 14:45:36 +04:00
|
|
|
/**
|
2012-02-21 01:32:57 +04:00
|
|
|
* @brief Get the categories for a specific user.
|
2012-09-17 18:03:15 +04:00
|
|
|
* @param
|
2012-02-18 14:45:36 +04:00
|
|
|
* @returns array containing the categories as strings.
|
|
|
|
*/
|
2012-09-17 18:03:15 +04:00
|
|
|
public function categories($format = null) {
|
2012-11-04 15:24:49 +04:00
|
|
|
if(!$this->categories) {
|
2012-06-27 03:03:18 +04:00
|
|
|
return array();
|
|
|
|
}
|
2012-11-04 15:24:49 +04:00
|
|
|
$categories = array_values($this->categories);
|
2012-09-17 18:03:15 +04:00
|
|
|
uasort($categories, 'strnatcasecmp');
|
|
|
|
if($format == self::FORMAT_MAP) {
|
|
|
|
$catmap = array();
|
|
|
|
foreach($categories as $category) {
|
2012-10-20 15:42:57 +04:00
|
|
|
if($category !== self::CATEGORY_FAVORITE) {
|
|
|
|
$catmap[] = array(
|
2012-11-04 15:24:49 +04:00
|
|
|
'id' => $this->array_searchi($category, $this->categories),
|
2012-10-20 15:42:57 +04:00
|
|
|
'name' => $category
|
|
|
|
);
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
|
|
|
return $catmap;
|
|
|
|
}
|
2012-10-20 15:45:18 +04:00
|
|
|
|
2012-10-20 15:42:57 +04:00
|
|
|
// 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;
|
|
|
|
}
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
/**
|
2012-10-19 02:15:58 +04:00
|
|
|
* Get the a list if items belonging to $category.
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-10-19 02:15:58 +04:00
|
|
|
* Throws an exception if the category could not be found.
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-10-19 02:15:58 +04:00
|
|
|
* @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)) {
|
2012-11-04 15:24:49 +04:00
|
|
|
$catid = $this->array_searchi($category, $this->categories);
|
2012-10-19 02:15:58 +04:00
|
|
|
}
|
|
|
|
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();
|
2012-10-19 02:20:27 +04:00
|
|
|
$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
|
2012-10-22 17:41:00 +04:00
|
|
|
. '` WHERE `categoryid` = ?';
|
2012-10-19 02:15:58 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
$stmt = OCP\DB::prepare($sql);
|
|
|
|
$result = $stmt->execute(array($catid));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-19 02:15:58 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-10-19 02:15:58 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_null($result)) {
|
|
|
|
while( $row = $result->fetchRow()) {
|
2012-10-31 23:06:39 +04:00
|
|
|
$ids[] = (int)$row['objid'];
|
2012-10-19 02:15:58 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-19 02:15:58 +04:00
|
|
|
return $ids;
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-19 02:15:58 +04:00
|
|
|
/**
|
|
|
|
* Get the a list if items belonging to $category.
|
|
|
|
*
|
|
|
|
* Throws an exception if the category could not be found.
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-09-17 18:03:15 +04:00
|
|
|
* @param string|integer $category Category id or name.
|
2012-10-03 00:46:37 +04:00
|
|
|
* @param array $tableinfo Array in the form {'tablename' => table, 'fields' => ['field1', 'field2']}
|
2012-09-17 18:03:15 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-09-17 18:03:15 +04:00
|
|
|
* 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'.
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-09-17 18:03:15 +04:00
|
|
|
* If the category name cannot be resolved an exception is thrown.
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-09-17 18:03:15 +04:00
|
|
|
* TODO: Maybe add the getting permissions for objects?
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-10-31 23:47:04 +04:00
|
|
|
* @returns array containing the resulting items or false on error.
|
2012-09-17 18:03:15 +04:00
|
|
|
*/
|
|
|
|
public function itemsForCategory($category, $tableinfo, $limit = null, $offset = null) {
|
|
|
|
$result = null;
|
|
|
|
if(is_numeric($category)) {
|
|
|
|
$catid = $category;
|
|
|
|
} elseif(is_string($category)) {
|
2012-11-04 15:24:49 +04:00
|
|
|
$catid = $this->array_searchi($category, $this->categories);
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', category: '.$catid.' '.$category, OCP\Util::DEBUG);
|
|
|
|
if($catid === false) {
|
|
|
|
$l10n = OC_L10N::get('core');
|
|
|
|
throw new Exception(
|
2012-10-19 02:16:53 +04:00
|
|
|
$l10n->t('Could not find category "%s"', $category)
|
2012-09-17 18:03:15 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$fields = '';
|
|
|
|
foreach($tableinfo['fields'] as $field) {
|
|
|
|
$fields .= '`' . $tableinfo['tablename'] . '`.`' . $field . '`,';
|
|
|
|
}
|
|
|
|
$fields = substr($fields, 0, -1);
|
|
|
|
|
|
|
|
$items = array();
|
2012-10-19 02:20:27 +04:00
|
|
|
$sql = 'SELECT `' . self::RELATION_TABLE . '`.`categoryid`, ' . $fields
|
|
|
|
. ' FROM `' . $tableinfo['tablename'] . '` JOIN `'
|
|
|
|
. self::RELATION_TABLE . '` ON `' . $tableinfo['tablename']
|
|
|
|
. '`.`id` = `' . self::RELATION_TABLE . '`.`objid` WHERE `'
|
2012-10-19 02:13:34 +04:00
|
|
|
. self::RELATION_TABLE . '`.`categoryid` = ?';
|
2012-09-17 18:03:15 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
$stmt = OCP\DB::prepare($sql, $limit, $offset);
|
|
|
|
$result = $stmt->execute(array($catid));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-09-17 18:03:15 +04:00
|
|
|
OCP\Util::ERROR);
|
2012-10-31 23:47:04 +04:00
|
|
|
return false;
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
return $items;
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-02-18 14:45:36 +04:00
|
|
|
/**
|
|
|
|
* @brief Checks whether a category is already saved.
|
|
|
|
* @param $name The name to check for.
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
public function hasCategory($name) {
|
2012-11-04 15:24:49 +04:00
|
|
|
return $this->in_arrayi($name, $this->categories);
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-19 15:21:05 +04:00
|
|
|
* @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;
|
|
|
|
}
|
2013-02-26 18:01:42 +04:00
|
|
|
try {
|
|
|
|
OCP\DB::insertIfNotExist(self::CATEGORY_TABLE,
|
|
|
|
array(
|
|
|
|
'uid' => $this->user,
|
|
|
|
'type' => $this->type,
|
|
|
|
'category' => $name,
|
|
|
|
));
|
|
|
|
} catch(Exception $e) {
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
|
|
|
OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-19 15:21:05 +04:00
|
|
|
$id = OCP\DB::insertid(self::CATEGORY_TABLE);
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', id: ' . $id, OCP\Util::DEBUG);
|
2012-11-04 15:24:49 +04:00
|
|
|
$this->categories[$id] = $name;
|
2012-10-19 15:21:05 +04:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2013-05-20 12:16:07 +04:00
|
|
|
/**
|
|
|
|
* @brief Rename category.
|
|
|
|
* @param string $from The name of the existing category
|
|
|
|
* @param string $to The new name of the category.
|
|
|
|
* @returns bool
|
|
|
|
*/
|
|
|
|
public function rename($from, $to) {
|
|
|
|
$id = $this->array_searchi($from, $this->categories);
|
|
|
|
if($id === false) {
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', category: ' . $from. ' does not exist', OCP\Util::DEBUG);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = 'UPDATE `' . self::CATEGORY_TABLE . '` SET `category` = ? '
|
|
|
|
. 'WHERE `uid` = ? AND `type` = ? AND `id` = ?';
|
|
|
|
try {
|
|
|
|
$stmt = OCP\DB::prepare($sql);
|
|
|
|
$result = $stmt->execute(array($to, $this->user, $this->type, $id));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
$this->categories[$id] = $to;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-19 15:21:05 +04:00
|
|
|
/**
|
|
|
|
* @brief Add a new category.
|
2012-02-21 01:32:57 +04:00
|
|
|
* @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
|
2012-10-19 15:21:05 +04:00
|
|
|
* @param $id int Optional object id to add to this|these categor(y|ies)
|
2012-02-18 14:45:36 +04:00
|
|
|
* @returns bool Returns false on error.
|
|
|
|
*/
|
2012-10-19 15:21:05 +04:00
|
|
|
public function addMulti($names, $sync=false, $id = null) {
|
2012-02-18 14:45:36 +04:00
|
|
|
if(!is_array($names)) {
|
|
|
|
$names = array($names);
|
|
|
|
}
|
|
|
|
$names = array_map('trim', $names);
|
2012-02-21 01:32:57 +04:00
|
|
|
$newones = array();
|
2012-02-18 14:45:36 +04:00
|
|
|
foreach($names as $name) {
|
2012-09-17 18:03:15 +04:00
|
|
|
if(($this->in_arrayi(
|
2012-11-04 15:24:49 +04:00
|
|
|
$name, $this->categories) == false) && $name != '') {
|
2012-02-18 14:45:36 +04:00
|
|
|
$newones[] = $name;
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
if(!is_null($id) ) {
|
|
|
|
// Insert $objectid, $categoryid pairs if not exist.
|
|
|
|
self::$relations[] = array('objid' => $id, 'category' => $name);
|
|
|
|
}
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
2013-03-04 20:05:08 +04:00
|
|
|
$this->categories = array_merge($this->categories, $newones);
|
|
|
|
if($sync === true) {
|
|
|
|
$this->save();
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
2013-03-04 20:05:08 +04:00
|
|
|
|
2012-02-18 14:45:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2012-09-17 18:03:15 +04:00
|
|
|
public function loadFromVObject($id, $vobject, $sync=false) {
|
2012-10-19 15:21:05 +04:00
|
|
|
$this->addMulti($vobject->getAsArray('CATEGORIES'), $sync, $id);
|
2012-02-20 16:16:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reset saved categories and rescan supplied vobjects for categories.
|
|
|
|
* @param $objects An array of vobjects (as text).
|
|
|
|
* To get the object array, do something like:
|
|
|
|
* // For Addressbook:
|
|
|
|
* $categories = new OC_VCategories('contacts');
|
2012-07-30 22:46:14 +04:00
|
|
|
* $stmt = OC_DB::prepare( 'SELECT `carddata` FROM `*PREFIX*contacts_cards`' );
|
2012-02-20 16:16:51 +04:00
|
|
|
* $result = $stmt->execute();
|
|
|
|
* $objects = array();
|
|
|
|
* if(!is_null($result)) {
|
2012-09-17 18:03:15 +04:00
|
|
|
* while( $row = $result->fetchRow()){
|
|
|
|
* $objects[] = array($row['id'], $row['carddata']);
|
2012-02-20 16:16:51 +04:00
|
|
|
* }
|
|
|
|
* }
|
2012-08-29 22:34:44 +04:00
|
|
|
* $categories->rescan($objects);
|
2012-02-20 16:16:51 +04:00
|
|
|
*/
|
2012-07-09 02:14:42 +04:00
|
|
|
public function rescan($objects, $sync=true, $reset=true) {
|
2012-10-19 02:20:27 +04:00
|
|
|
|
|
|
|
if($reset === true) {
|
2012-09-17 18:03:15 +04:00
|
|
|
$result = null;
|
|
|
|
// Find all objectid/categoryid pairs.
|
|
|
|
try {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('SELECT `id` FROM `' . self::CATEGORY_TABLE . '` '
|
2012-09-17 18:03:15 +04:00
|
|
|
. 'WHERE `uid` = ? AND `type` = ?');
|
|
|
|
$result = $stmt->execute(array($this->user, $this->type));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-09-17 18:03:15 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And delete them.
|
|
|
|
if(!is_null($result)) {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
|
2012-09-17 18:03:15 +04:00
|
|
|
. 'WHERE `categoryid` = ? AND `type`= ?');
|
|
|
|
while( $row = $result->fetchRow()) {
|
|
|
|
$stmt->execute(array($row['id'], $this->type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM `' . self::CATEGORY_TABLE . '` '
|
2012-09-17 18:03:15 +04:00
|
|
|
. 'WHERE `uid` = ? AND `type` = ?');
|
|
|
|
$result = $stmt->execute(array($this->user, $this->type));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__ . ', exception: '
|
2012-09-17 18:03:15 +04:00
|
|
|
. $e->getMessage(), OCP\Util::ERROR);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-04 15:24:49 +04:00
|
|
|
$this->categories = array();
|
2012-07-09 02:14:42 +04:00
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
// Parse all the VObjects
|
2012-02-20 16:16:51 +04:00
|
|
|
foreach($objects as $object) {
|
2012-09-17 18:03:15 +04:00
|
|
|
$vobject = OC_VObject::parse($object[1]);
|
2012-02-21 01:32:57 +04:00
|
|
|
if(!is_null($vobject)) {
|
2012-09-17 18:03:15 +04:00
|
|
|
// Load the categories
|
|
|
|
$this->loadFromVObject($object[0], $vobject, $sync);
|
2012-02-20 16:16:51 +04:00
|
|
|
} else {
|
2012-10-19 02:20:27 +04:00
|
|
|
OC_Log::write('core', __METHOD__ . ', unable to parse. ID: ' . ', '
|
2012-09-17 18:03:15 +04:00
|
|
|
. substr($object, 0, 100) . '(...)', OC_Log::DEBUG);
|
2012-02-20 16:16:51 +04:00
|
|
|
}
|
|
|
|
}
|
2012-02-21 01:32:57 +04:00
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Save the list with categories
|
|
|
|
*/
|
2012-03-07 19:39:56 +04:00
|
|
|
private function save() {
|
2012-11-04 15:24:49 +04:00
|
|
|
if(is_array($this->categories)) {
|
|
|
|
foreach($this->categories as $category) {
|
2013-02-26 18:01:42 +04:00
|
|
|
try {
|
|
|
|
OCP\DB::insertIfNotExist(self::CATEGORY_TABLE,
|
|
|
|
array(
|
|
|
|
'uid' => $this->user,
|
|
|
|
'type' => $this->type,
|
|
|
|
'category' => $category,
|
|
|
|
));
|
|
|
|
} catch(Exception $e) {
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
|
|
|
// reload categories to get the proper ids.
|
|
|
|
$this->loadCategories();
|
|
|
|
// Loop through temporarily cached objectid/categoryname pairs
|
|
|
|
// and save relations.
|
2012-11-04 15:24:49 +04:00
|
|
|
$categories = $this->categories;
|
2012-09-17 18:03:15 +04:00
|
|
|
// For some reason this is needed or array_search(i) will return 0..?
|
2012-10-19 02:20:27 +04:00
|
|
|
ksort($categories);
|
2012-09-17 18:03:15 +04:00
|
|
|
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) {
|
2013-02-26 18:01:42 +04:00
|
|
|
try {
|
|
|
|
OCP\DB::insertIfNotExist(self::RELATION_TABLE,
|
|
|
|
array(
|
|
|
|
'objid' => $relation['objid'],
|
|
|
|
'categoryid' => $catid,
|
|
|
|
'type' => $this->type,
|
|
|
|
));
|
|
|
|
} catch(Exception $e) {
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self::$relations = array(); // reset
|
2012-05-13 17:07:07 +04:00
|
|
|
} else {
|
2012-11-04 15:24:49 +04:00
|
|
|
OC_Log::write('core', __METHOD__.', $this->categories is not an array! '
|
|
|
|
. print_r($this->categories, true), OC_Log::ERROR);
|
2012-05-13 17:07:07 +04:00
|
|
|
}
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-03 00:46:37 +04:00
|
|
|
/**
|
|
|
|
* @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 {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('SELECT `id` FROM `' . self::CATEGORY_TABLE . '` '
|
2012-10-03 00:46:37 +04:00
|
|
|
. 'WHERE `uid` = ?');
|
|
|
|
$result = $stmt->execute(array($arguments['uid']));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
}
|
2012-10-03 00:46:37 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-10-03 00:46:37 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-03 00:46:37 +04:00
|
|
|
if(!is_null($result)) {
|
|
|
|
try {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
|
2012-10-03 00:46:37 +04:00
|
|
|
. 'WHERE `categoryid` = ?');
|
|
|
|
while( $row = $result->fetchRow()) {
|
|
|
|
try {
|
|
|
|
$stmt->execute(array($row['id']));
|
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-10-03 00:46:37 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-10-03 00:46:37 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM `' . self::CATEGORY_TABLE . '` '
|
2013-06-18 18:18:19 +04:00
|
|
|
. 'WHERE `uid` = ?');
|
2012-10-03 00:46:37 +04:00
|
|
|
$result = $stmt->execute(array($arguments['uid']));
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
}
|
2012-10-03 00:46:37 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__ . ', exception: '
|
2012-10-03 00:46:37 +04:00
|
|
|
. $e->getMessage(), OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
/**
|
|
|
|
* @brief Delete category/object relations from the db
|
2013-03-29 08:51:41 +04:00
|
|
|
* @param array $ids The ids of the objects
|
2012-10-03 00:46:37 +04:00
|
|
|
* @param string $type The type of object (event/contact/task/journal).
|
2012-09-17 18:03:15 +04:00
|
|
|
* Defaults to the type set in the instance
|
2012-10-31 23:47:04 +04:00
|
|
|
* @returns boolean Returns false on error.
|
2012-09-17 18:03:15 +04:00
|
|
|
*/
|
2013-03-29 17:54:08 +04:00
|
|
|
public function purgeObjects(array $ids, $type = null) {
|
2012-09-17 18:03:15 +04:00
|
|
|
$type = is_null($type) ? $this->type : $type;
|
2013-03-29 17:54:08 +04:00
|
|
|
if(count($ids) === 0) {
|
|
|
|
// job done ;)
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-30 19:35:13 +04:00
|
|
|
$updates = $ids;
|
2012-09-17 18:03:15 +04:00
|
|
|
try {
|
2013-03-29 08:51:41 +04:00
|
|
|
$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
|
|
|
|
$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
|
|
|
|
$query .= 'AND `type`= ?';
|
|
|
|
$updates[] = $type;
|
|
|
|
$stmt = OCP\DB::prepare($query);
|
|
|
|
$result = $stmt->execute($updates);
|
2012-10-31 23:47:04 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
} catch(Exception $e) {
|
2013-03-29 17:54:08 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
|
2012-09-17 18:03:15 +04:00
|
|
|
OCP\Util::ERROR);
|
2012-10-03 00:46:37 +04:00
|
|
|
return false;
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
2012-10-03 00:46:37 +04:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-19 02:15:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get favorites for an object type
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-10-19 02:15:58 +04:00
|
|
|
* @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;
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-19 02:15:58 +04:00
|
|
|
try {
|
|
|
|
return $this->idsForCategory(self::CATEGORY_FAVORITE);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
// No favorites
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an object to favorites
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-10-19 02:15:58 +04:00
|
|
|
* @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);
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-19 02:15:58 +04:00
|
|
|
/**
|
|
|
|
* Remove an object from favorites
|
2012-10-19 02:20:27 +04:00
|
|
|
*
|
2012-10-19 02:15:58 +04:00
|
|
|
* @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);
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-03 00:46:37 +04:00
|
|
|
/**
|
|
|
|
* @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
|
2012-10-31 23:06:39 +04:00
|
|
|
* @returns boolean Returns false on database error.
|
2012-10-03 00:46:37 +04:00
|
|
|
*/
|
2012-10-19 02:13:34 +04:00
|
|
|
public function addToCategory($objid, $category, $type = null) {
|
2012-10-03 00:46:37 +04:00
|
|
|
$type = is_null($type) ? $this->type : $type;
|
2012-10-20 15:45:18 +04:00
|
|
|
if(is_string($category) && !is_numeric($category)) {
|
|
|
|
if(!$this->hasCategory($category)) {
|
|
|
|
$this->add($category, true);
|
|
|
|
}
|
2012-11-04 15:24:49 +04:00
|
|
|
$categoryid = $this->array_searchi($category, $this->categories);
|
2012-10-20 15:45:18 +04:00
|
|
|
} else {
|
|
|
|
$categoryid = $category;
|
2012-10-19 02:13:34 +04:00
|
|
|
}
|
2012-10-03 00:46:37 +04:00
|
|
|
try {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\DB::insertIfNotExist(self::RELATION_TABLE,
|
2012-10-03 00:46:37 +04:00
|
|
|
array(
|
|
|
|
'objid' => $objid,
|
|
|
|
'categoryid' => $categoryid,
|
|
|
|
'type' => $type,
|
|
|
|
));
|
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-10-03 00:46:37 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-10-03 00:46:37 +04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2012-10-19 02:13:34 +04:00
|
|
|
public function removeFromCategory($objid, $category, $type = null) {
|
2012-10-03 00:46:37 +04:00
|
|
|
$type = is_null($type) ? $this->type : $type;
|
|
|
|
$categoryid = (is_string($category) && !is_numeric($category))
|
2012-11-04 15:24:49 +04:00
|
|
|
? $this->array_searchi($category, $this->categories)
|
2012-10-03 00:46:37 +04:00
|
|
|
: $category;
|
|
|
|
try {
|
2012-10-19 02:13:34 +04:00
|
|
|
$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
|
2012-10-03 00:46:37 +04:00
|
|
|
. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', sql: ' . $objid . ' ' . $categoryid . ' ' . $type,
|
2012-10-03 00:46:37 +04:00
|
|
|
OCP\Util::DEBUG);
|
|
|
|
$stmt = OCP\DB::prepare($sql);
|
|
|
|
$stmt->execute(array($objid, $categoryid, $type));
|
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
2012-10-03 00:46:37 +04:00
|
|
|
OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-09-17 18:03:15 +04:00
|
|
|
}
|
2012-10-19 02:20:27 +04:00
|
|
|
|
2012-02-18 14:45:36 +04:00
|
|
|
/**
|
2012-03-07 19:39:56 +04:00
|
|
|
* @brief Delete categories from the db and from all the vobject supplied
|
|
|
|
* @param $names An array of categories to delete
|
2012-02-18 14:45:36 +04:00
|
|
|
* @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table.
|
|
|
|
*/
|
2012-03-12 17:12:27 +04:00
|
|
|
public function delete($names, array &$objects=null) {
|
2012-03-07 19:39:56 +04:00
|
|
|
if(!is_array($names)) {
|
|
|
|
$names = array($names);
|
|
|
|
}
|
2012-11-05 15:06:59 +04:00
|
|
|
|
2012-10-19 04:26:36 +04:00
|
|
|
OC_Log::write('core', __METHOD__ . ', before: '
|
2012-11-04 15:24:49 +04:00
|
|
|
. print_r($this->categories, true), OC_Log::DEBUG);
|
2012-03-07 19:39:56 +04:00
|
|
|
foreach($names as $name) {
|
2012-10-25 00:39:34 +04:00
|
|
|
$id = null;
|
2012-10-19 04:26:36 +04:00
|
|
|
OC_Log::write('core', __METHOD__.', '.$name, OC_Log::DEBUG);
|
2012-03-07 19:39:56 +04:00
|
|
|
if($this->hasCategory($name)) {
|
2012-11-04 15:24:49 +04:00
|
|
|
$id = $this->array_searchi($name, $this->categories);
|
|
|
|
unset($this->categories[$id]);
|
2012-03-07 19:39:56 +04:00
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
try {
|
2012-10-19 02:13:34 +04:00
|
|
|
$stmt = OCP\DB::prepare('DELETE FROM `' . self::CATEGORY_TABLE . '` WHERE '
|
2012-09-17 18:03:15 +04:00
|
|
|
. '`uid` = ? AND `type` = ? AND `category` = ?');
|
|
|
|
$result = $stmt->execute(array($this->user, $this->type, $name));
|
2012-11-05 19:24:16 +04:00
|
|
|
if (OC_DB::isError($result)) {
|
|
|
|
OC_Log::write('core', __METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result), OC_Log::ERROR);
|
|
|
|
}
|
2012-09-17 18:03:15 +04:00
|
|
|
} catch(Exception $e) {
|
2012-10-19 02:20:27 +04:00
|
|
|
OCP\Util::writeLog('core', __METHOD__ . ', exception: '
|
2012-09-17 18:03:15 +04:00
|
|
|
. $e->getMessage(), OCP\Util::ERROR);
|
|
|
|
}
|
2012-10-25 00:39:34 +04:00
|
|
|
if(!is_null($id) && $id !== false) {
|
|
|
|
try {
|
|
|
|
$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
|
|
|
|
. 'WHERE `categoryid` = ?';
|
|
|
|
$stmt = OCP\DB::prepare($sql);
|
2012-11-05 19:24:16 +04:00
|
|
|
$result = $stmt->execute(array($id));
|
|
|
|
if (OC_DB::isError($result)) {
|
2013-02-11 20:44:02 +04:00
|
|
|
OC_Log::write('core',
|
|
|
|
__METHOD__. 'DB error: ' . OC_DB::getErrorMessage($result),
|
|
|
|
OC_Log::ERROR);
|
2012-11-05 19:24:16 +04:00
|
|
|
}
|
2012-10-25 00:39:34 +04:00
|
|
|
} catch(Exception $e) {
|
|
|
|
OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
|
|
|
|
OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
2012-10-19 04:26:36 +04:00
|
|
|
OC_Log::write('core', __METHOD__.', after: '
|
2012-11-04 15:24:49 +04:00
|
|
|
. print_r($this->categories, true), OC_Log::DEBUG);
|
2012-03-12 17:12:27 +04:00
|
|
|
if(!is_null($objects)) {
|
|
|
|
foreach($objects as $key=>&$value) {
|
|
|
|
$vobject = OC_VObject::parse($value[1]);
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!is_null($vobject)) {
|
2012-09-17 18:03:15 +04:00
|
|
|
$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');
|
2012-03-12 17:12:27 +04:00
|
|
|
foreach($names as $name) {
|
|
|
|
$idx = $this->array_searchi($name, $categories);
|
|
|
|
if($idx !== false) {
|
2012-09-17 18:03:15 +04:00
|
|
|
OC_Log::write('core', __METHOD__
|
2012-10-19 02:20:27 +04:00
|
|
|
.', unsetting: '
|
2012-09-17 18:03:15 +04:00
|
|
|
. $categories[$this->array_searchi($name, $categories)],
|
|
|
|
OC_Log::DEBUG);
|
2012-03-12 17:12:27 +04:00
|
|
|
unset($categories[$this->array_searchi($name, $categories)]);
|
|
|
|
}
|
2012-03-07 19:39:56 +04:00
|
|
|
}
|
2012-11-05 15:06:59 +04:00
|
|
|
|
2012-09-17 18:03:15 +04:00
|
|
|
$object->setString('CATEGORIES', implode(',', $categories));
|
|
|
|
if($vobject !== $object) {
|
|
|
|
$vobject[$componentname] = $object;
|
|
|
|
}
|
2012-03-12 17:12:27 +04:00
|
|
|
$value[1] = $vobject->serialize();
|
|
|
|
$objects[$key] = $value;
|
|
|
|
} else {
|
2012-09-17 18:03:15 +04:00
|
|
|
OC_Log::write('core', __METHOD__
|
2012-10-19 02:20:27 +04:00
|
|
|
.', unable to parse. ID: ' . $value[0] . ', '
|
2012-09-17 18:03:15 +04:00
|
|
|
. substr($value[1], 0, 50) . '(...)', OC_Log::DEBUG);
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// case-insensitive in_array
|
|
|
|
private function in_arrayi($needle, $haystack) {
|
2012-06-27 03:03:18 +04:00
|
|
|
if(!is_array($haystack)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-13 17:07:07 +04:00
|
|
|
return in_array(strtolower($needle), array_map('strtolower', $haystack));
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// case-insensitive array_search
|
2012-02-21 01:32:57 +04:00
|
|
|
private function array_searchi($needle, $haystack) {
|
2012-06-27 03:03:18 +04:00
|
|
|
if(!is_array($haystack)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-02 22:53:02 +04:00
|
|
|
return array_search(strtolower($needle), array_map('strtolower', $haystack));
|
2012-02-18 14:45:36 +04:00
|
|
|
}
|
|
|
|
}
|