mirror of
https://github.com/silverstripe/silverstripe-tagfield
synced 2024-06-29 07:59:26 +02:00
ENHANCEMENT Added saving unit tests
BUGFIX Fixed separator handling MINOR Added README
This commit is contained in:
parent
fd15deeee8
commit
dddedf48a1
19
README
19
README
|
@ -0,0 +1,19 @@
|
|||
####################################################
|
||||
TagField Moudle
|
||||
####################################################
|
||||
|
||||
# Maintainer Contact
|
||||
Ingo Schommer (Nickname: ischommer)
|
||||
<ingo (at) silverstripe (dot) com>
|
||||
|
||||
# Requirements
|
||||
SilverStripe 2.3
|
||||
|
||||
# Documentation
|
||||
http://doc.silverstripe.com/doku.php?id=modules:tagfield
|
||||
|
||||
# Installation Instructions
|
||||
CAUTION: Work in progress, the module trunk is most likely not useable
|
||||
|
||||
# Usage Overview
|
||||
...
|
|
@ -1,8 +1,13 @@
|
|||
<?php
|
||||
/**
|
||||
* Provides a Formfield for saving a string of tags into either
|
||||
* a many_many relationship or a text property.
|
||||
* By default, tags are separated by whitespace.
|
||||
*
|
||||
* Features:
|
||||
* - Bundled with jQuery-based autocomplete library which is applied to a textfield
|
||||
* - Autosuggest functionality (currently JSON only)
|
||||
*
|
||||
* @author Ingo Schommer, SilverStripe Ltd. (<firstname>@silverstripe.com)
|
||||
* @package formfields
|
||||
* @subpackage tagfield
|
||||
|
@ -33,10 +38,14 @@ class TagField extends TextField {
|
|||
protected $tagSort;
|
||||
|
||||
/**
|
||||
* @var $separator
|
||||
* @var $separator Determines on which character to split tags in a string.
|
||||
*/
|
||||
protected $separator = ' ';
|
||||
|
||||
protected static $separator_to_regex = array(
|
||||
' ' => '\s',
|
||||
);
|
||||
|
||||
/**
|
||||
* @var array $customTags Override the tagging behaviour with a custom set
|
||||
* used by {@link suggest()}.
|
||||
|
@ -76,7 +85,7 @@ class TagField extends TextField {
|
|||
} else if($tagTopicClassObj->hasField($this->Name())) {
|
||||
$tags = $this->getTextbasedTags($searchString);
|
||||
} else {
|
||||
user_error('TagField::suggest(): Cant find valid relation or text property with name "' . $this->Name . '"', E_USER_ERROR);
|
||||
user_error('TagField::suggest(): Cant find valid relation or text property with name "' . $this->Name() . '"', E_USER_ERROR);
|
||||
}
|
||||
|
||||
return Convert::raw2json($tags);
|
||||
|
@ -85,7 +94,7 @@ class TagField extends TextField {
|
|||
function saveInto($record) {
|
||||
if($this->value) {
|
||||
// $record should match the $tagTopicClass
|
||||
if($record->many_many($this->Name()) {
|
||||
if($record->many_many($this->Name())) {
|
||||
$this->saveIntoObjectTags($record);
|
||||
} elseif($record->hasField($this->Name())) {
|
||||
$this->saveIntoTextbasedTags($record);
|
||||
|
@ -97,7 +106,6 @@ class TagField extends TextField {
|
|||
|
||||
protected function saveIntoObjectTags($record) {
|
||||
$tagsArr = $this->splitTagsToArray($this->value);
|
||||
|
||||
$relationName = $this->Name();
|
||||
$existingTagsComponentSet = $record->$relationName();
|
||||
$tagClass = $this->getTagClass();
|
||||
|
@ -113,12 +121,12 @@ class TagField extends TextField {
|
|||
$tagObj = DataObject::get_one($tagClass, $SQL_filter);
|
||||
if(!$tagObj) {
|
||||
$tagObj = new $tagClass();
|
||||
$tagObj->{$this->tagObjectField} = $this->value;
|
||||
$tabObj->write();
|
||||
$tagObj->{$this->tagObjectField} = $tagString;
|
||||
$tagObj->write();
|
||||
}
|
||||
$tagsToAdd[] = $tagObj;
|
||||
}
|
||||
|
||||
|
||||
// remove all before readding
|
||||
$existingTagsComponentSet->removeAll();
|
||||
$existingTagsComponentSet->addMany($tagsToAdd);
|
||||
|
@ -126,11 +134,19 @@ class TagField extends TextField {
|
|||
|
||||
protected function saveIntoTextbasedTags($record) {
|
||||
$tagFieldName = $this->Name();
|
||||
$record->$tagFieldName = $this->value;
|
||||
|
||||
// necessary step to filter whitespace etc.
|
||||
$RAW_tagsArr = $this->splitTagsToArray($this->value);
|
||||
$record->$tagFieldName = $this->combineTagsFromArray($RAW_tagsArr);
|
||||
}
|
||||
|
||||
protected function splitTagsToArray($tagsString) {
|
||||
return array_unique(split("*" . escape($this->separator) . "*", trim($tagsString)));
|
||||
$separator = (isset(self::$separator_to_regex[$this->separator])) ? self::$separator_to_regex[$this->separator] : $this->separator;
|
||||
return array_unique(preg_split('/\s*' . $separator . '\s*/', trim($tagsString)));
|
||||
}
|
||||
|
||||
protected function combineTagsFromArray($tagsArr) {
|
||||
return implode($this->separator, $tagsArr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,19 +4,50 @@
|
|||
* @package testing
|
||||
*
|
||||
* @todo Test filtering and sorting
|
||||
* @todo Test custom tags
|
||||
* @todo Test custom separators
|
||||
*/
|
||||
class TagFieldTest extends FunctionalTest {
|
||||
|
||||
static $fixture_file = 'tagfield/tests/TagFieldTest.yml';
|
||||
|
||||
function testExistingObjectSaving() {
|
||||
|
||||
// should contain "tag1" and "tag2"
|
||||
$existingEntry = $this->objFromFixture('TagFieldTest_BlogEntry', 'blogentry1');
|
||||
$field = new TagField('Tags', null, null, 'TagFieldTest_BlogEntry');
|
||||
$field->setValue('tag1 tag3 ');
|
||||
$field->saveInto($existingEntry);
|
||||
$existingEntry->write();
|
||||
$this->assertEquals(
|
||||
array_values($existingEntry->Tags()->map('ID', 'Title')),
|
||||
array('tag1','tag3')
|
||||
);
|
||||
}
|
||||
|
||||
function testNewObjectSaving() {
|
||||
|
||||
$newEntry = new TagFieldTest_BlogEntry();
|
||||
$newEntry->write();
|
||||
$field = new TagField('Tags', null, null, 'TagFieldTest_BlogEntry');
|
||||
$field->setValue('tag1 tag2'); // test separator handling as well
|
||||
$field->saveInto($newEntry);
|
||||
|
||||
$this->assertEquals(
|
||||
array_values($newEntry->Tags()->map('ID', 'Title')),
|
||||
array('tag1','tag2')
|
||||
);
|
||||
}
|
||||
|
||||
function testTextbasedSaving() {
|
||||
|
||||
// should contain "tag1" and "tag2"
|
||||
$existingEntry = $this->objFromFixture('TagFieldTest_BlogEntry', 'blogentry1');
|
||||
$field = new TagField('TextbasedTags', null, null, 'TagFieldTest_BlogEntry');
|
||||
$field->setValue('tag1 tag3 '); // test separator handling as well
|
||||
$field->saveInto($existingEntry);
|
||||
$existingEntry->write();
|
||||
$this->assertEquals(
|
||||
$existingEntry->TextbasedTags,
|
||||
'tag1 tag3'
|
||||
);
|
||||
}
|
||||
|
||||
function testObjectSuggest() {
|
||||
|
|
Loading…
Reference in New Issue
Block a user