mirror of
https://github.com/owncloudarchive/contacts.git
synced 2024-11-29 11:24:11 +01:00
Added custom GroupProperty. Refs #96
This commit is contained in:
parent
4e70507601
commit
591a48aa67
@ -5,6 +5,7 @@ use \OCA\AppFramework\Core\API;
|
||||
|
||||
//require_once __DIR__ . '/../controller/groupcontroller.php';
|
||||
\Sabre\VObject\Component::$classMap['VCARD'] = '\OCA\Contacts\VObject\VCard';
|
||||
\Sabre\VObject\Property::$classMap['CATEGORIES'] = 'OCA\Contacts\VObject\GroupProperty';
|
||||
\Sabre\VObject\Property::$classMap['FN'] = '\OC\VObject\StringProperty';
|
||||
\Sabre\VObject\Property::$classMap['TITLE'] = '\OC\VObject\StringProperty';
|
||||
\Sabre\VObject\Property::$classMap['ROLE'] = '\OC\VObject\StringProperty';
|
||||
@ -15,7 +16,6 @@ use \OCA\AppFramework\Core\API;
|
||||
\Sabre\VObject\Property::$classMap['IMPP'] = '\OC\VObject\StringProperty';
|
||||
\Sabre\VObject\Property::$classMap['URL'] = '\OC\VObject\StringProperty';
|
||||
\Sabre\VObject\Property::$classMap['N'] = '\OC\VObject\CompoundProperty';
|
||||
\Sabre\VObject\Property::$classMap['CATEGORIES'] = '\OC\VObject\CompoundProperty';
|
||||
\Sabre\VObject\Property::$classMap['ADR'] = '\OC\VObject\CompoundProperty';
|
||||
\Sabre\VObject\Property::$classMap['GEO'] = '\OC\VObject\CompoundProperty';
|
||||
|
||||
|
104
lib/vobject/groupproperty.php
Normal file
104
lib/vobject/groupproperty.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* ownCloud - VObject Group Property
|
||||
*
|
||||
* @author Thomas Tanghus
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
public function addGroup($name) {
|
||||
if($this->hasGroup($name)) {
|
||||
return;
|
||||
}
|
||||
$groups = $this->getParts();
|
||||
$groups[] = $name;
|
||||
$this->setParts($groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an existing group.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function removeGroup($name) {
|
||||
if(!$this->hasGroup($name)) {
|
||||
return;
|
||||
}
|
||||
$groups = $this->getParts();
|
||||
array_splice($groups, $this->array_searchi($name, $groups), 1);
|
||||
$this->setParts($groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it a group by that name exists.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasGroup($name) {
|
||||
return $this->in_arrayi($name, $this->getParts());
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename an existing group.
|
||||
*
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
*/
|
||||
public function renameGroup($from, $to) {
|
||||
if(!$this->hasGroup($from)) {
|
||||
return;
|
||||
}
|
||||
$groups = $this->getParts();
|
||||
$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));
|
||||
}
|
||||
}
|
@ -37,6 +37,9 @@ class Test_Contacts_Backend_Datebase extends PHPUnit_Framework_TestCase {
|
||||
self::$addressBooksTableName,
|
||||
self::$cardsTableName
|
||||
);
|
||||
|
||||
\Sabre\VObject\Property::$classMap['CATEGORIES'] = 'OCA\Contacts\VObject\GroupProperty';
|
||||
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass() {
|
||||
@ -127,10 +130,43 @@ class Test_Contacts_Backend_Datebase extends PHPUnit_Framework_TestCase {
|
||||
\Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES
|
||||
);
|
||||
$obj->validate($obj::REPAIR|$obj::UPGRADE);
|
||||
echo "\n" . $obj->serialize();
|
||||
|
||||
$this->assertEquals('3.0', (string)$obj->VERSION);
|
||||
$this->assertEquals('Adèle Fermée', (string)$obj->FN);
|
||||
$this->assertEquals('Fermée;Adèle;;;', (string)$obj->N);
|
||||
}
|
||||
|
||||
public function testGroupProperty() {
|
||||
$arr = array(
|
||||
'Home',
|
||||
'work',
|
||||
'Friends, Family',
|
||||
);
|
||||
|
||||
$property = \Sabre\VObject\Property::create('CATEGORIES');
|
||||
$property->setParts($arr);
|
||||
|
||||
// Test parsing and serializing
|
||||
$this->assertEquals('Home,work,Friends\, Family', $property->value);
|
||||
$this->assertEquals('CATEGORIES:Home,work,Friends\, Family' . "\r\n", $property->serialize());
|
||||
$this->assertEquals(3, count($property->getParts()));
|
||||
|
||||
// Test add
|
||||
$property->addGroup('Coworkers');
|
||||
$this->assertTrue($property->hasGroup('coworkers'));
|
||||
$this->assertEquals(4, count($property->getParts()));
|
||||
$this->assertEquals('Home,work,Friends\, Family,Coworkers', $property->value);
|
||||
|
||||
// Test remove
|
||||
$this->assertTrue($property->hasGroup('Friends, fAmIlY'));
|
||||
$property->removeGroup('Friends, fAmIlY');
|
||||
$this->assertEquals(3, count($property->getParts()));
|
||||
$parts = $property->getParts();
|
||||
$this->assertEquals('Coworkers', $parts[2]);
|
||||
|
||||
// Test rename
|
||||
$property->renameGroup('work', 'Work');
|
||||
$parts = $property->getParts();
|
||||
$this->assertEquals('Work', $parts[1]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user