2013-05-19 00:53:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - VObject Group Property
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
2014-01-26 00:40:22 +01:00
|
|
|
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
|
2013-05-19 00:53:33 +02: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\VObject;
|
|
|
|
|
|
|
|
use OC\VObject\CompoundProperty;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class adds convenience methods for the CATEGORIES property.
|
|
|
|
*
|
|
|
|
* NOTE: Group names are case-insensitive.
|
|
|
|
*/
|
|
|
|
class GroupProperty extends CompoundProperty {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a group.
|
|
|
|
*
|
|
|
|
* NOTE: We cannot just use add() as that method name is used in
|
|
|
|
* \Sabre\VObject\Property
|
|
|
|
*
|
|
|
|
* @param string $name
|
2014-01-25 17:08:01 +01:00
|
|
|
* @return bool
|
2013-05-19 00:53:33 +02:00
|
|
|
*/
|
|
|
|
public function addGroup($name) {
|
2013-06-27 15:52:18 +02:00
|
|
|
$name = trim($name);
|
2013-05-19 00:53:33 +02:00
|
|
|
if($this->hasGroup($name)) {
|
2014-01-25 17:08:01 +01:00
|
|
|
return false;
|
2013-05-19 00:53:33 +02:00
|
|
|
}
|
|
|
|
$groups = $this->getParts();
|
2014-01-25 17:08:01 +01:00
|
|
|
// Remove empty elements
|
|
|
|
$groups = array_filter($groups, 'strlen');
|
2013-05-19 00:53:33 +02:00
|
|
|
$groups[] = $name;
|
|
|
|
$this->setParts($groups);
|
2014-01-25 17:08:01 +01:00
|
|
|
return true;
|
2013-05-19 00:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing group.
|
|
|
|
*
|
|
|
|
* @param string $name
|
2013-06-27 15:52:18 +02:00
|
|
|
* @return bool
|
2013-05-19 00:53:33 +02:00
|
|
|
*/
|
|
|
|
public function removeGroup($name) {
|
2013-06-27 15:52:18 +02:00
|
|
|
$name = trim($name);
|
2013-05-19 00:53:33 +02:00
|
|
|
if(!$this->hasGroup($name)) {
|
2013-06-27 15:52:18 +02:00
|
|
|
return false;
|
2013-05-19 00:53:33 +02:00
|
|
|
}
|
|
|
|
$groups = $this->getParts();
|
2013-06-27 15:52:18 +02:00
|
|
|
$groups = array_map('trim', $groups);
|
2013-05-19 00:53:33 +02:00
|
|
|
array_splice($groups, $this->array_searchi($name, $groups), 1);
|
|
|
|
$this->setParts($groups);
|
2013-06-27 15:52:18 +02:00
|
|
|
return true;
|
2013-05-19 00:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test it a group by that name exists.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasGroup($name) {
|
2013-06-27 15:52:18 +02:00
|
|
|
$name = trim($name);
|
|
|
|
$groups = $this->getParts();
|
|
|
|
$groups = array_map('trim', $groups);
|
|
|
|
return $this->in_arrayi($name, $groups);
|
2013-05-19 00:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename an existing group.
|
|
|
|
*
|
|
|
|
* @param string $from
|
|
|
|
* @param string $to
|
|
|
|
*/
|
|
|
|
public function renameGroup($from, $to) {
|
2013-06-27 15:52:18 +02:00
|
|
|
$from = trim($from);
|
|
|
|
$to = trim($to);
|
2013-05-19 00:53:33 +02:00
|
|
|
if(!$this->hasGroup($from)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$groups = $this->getParts();
|
2013-06-27 15:52:18 +02:00
|
|
|
$groups = array_map('trim', $groups);
|
2013-05-19 00:53:33 +02:00
|
|
|
$groups[$this->array_searchi($from, $groups)] = $to;
|
|
|
|
$this->setParts($groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
// case-insensitive in_array
|
|
|
|
protected function in_arrayi($needle, $haystack) {
|
|
|
|
if(!is_array($haystack)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return in_array(strtolower($needle), array_map('strtolower', $haystack));
|
|
|
|
}
|
|
|
|
|
|
|
|
// case-insensitive array_search
|
|
|
|
protected function array_searchi($needle, $haystack) {
|
|
|
|
if(!is_array($haystack)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return array_search(strtolower($needle), array_map('strtolower', $haystack));
|
|
|
|
}
|
|
|
|
}
|