ENHANCEMENT: implementing adding tags

This commit is contained in:
Julian Seidenberg 2012-07-25 18:56:43 +12:00
parent 5c6f57a6c1
commit e50f776a0d
3 changed files with 64 additions and 5 deletions

View File

@ -64,25 +64,25 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
function addTag($category, $value, $multiValue = true) {
if ($multiValue) {
//check for a duplicate tag, don't add the duplicate
$currentTag = $this->Tags()->filter("Category = '$category' AND Value = '$value'");
if (!$currentTag) {
$currentTag = $this->Tags()->filter(array('Category' => $category, 'Value' => $value));
if ($currentTag->Count() == 0) {
//multi value tag
$tag = new DMSTag();
$tag->Category = $category;
$tag->Value = $value;
$tag->DocumentID = $this->ID;
$tag->write();
$tag->Documents()->add($this);
}
} else {
//single value tag
$currentTag = $this->Tags()->filter("Category = '$category'");
$currentTag = $this->Tags()->filter(array('Category' => $category));
if (!$currentTag) {
//create the single-value tag
$tag = new DMSTag();
$tag->Category = $category;
$tag->Value = $value;
$tag->DocumentID = $this->ID;
$tag->write();
$tag->Documents()->add($this);
} else {
//update the single value tag
$tag = $currentTag->first();
@ -92,6 +92,31 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
}
}
/**
* Fetches all tags associated with this DMSDocument within a given category. If a value is specified this method
* tries to fetch that specific tag.
* @abstract
* @param $category String of the metadata category to get
* @param null $value String of the value of the tag to get
* @return array of Strings of all the tags or null if there is no match found
*/
function getTags($category, $value = null) {
$valueFilter = array("Category" => $category);
if (!empty($value)) $valueFilter['Value'] = $value;
$tag = $this->Tags()->filter($valueFilter);
//convert DataList into array of Values
$returnArray = null;
if ($tag->Count() > 0) {
$returnArray = array();
foreach($tag as $t) {
$returnArray[] = $t->Value;
}
}
return $returnArray;
}
/**
* Quick way to add multiple tags to a Document. This takes a multidimensional array of category/value pairs.
* The array should look like this:

View File

@ -62,6 +62,16 @@ interface DMSDocumentInterface {
*/
function addTag($category, $value, $multiValue = true);
/**
* Fetches all tags associated with this DMSDocument within a given category. If a value is specified this method
* tries to fetch that specific tag.
* @abstract
* @param $category String of the metadata category to get
* @param null $value String of the value of the tag to get
* @return array of Strings of all the tags or null if there is no match found
*/
function getTags($category, $value = null);
/**
* Quick way to add multiple tags to a DMSDocument. This takes a multidimensional array of category/value pairs.
* The array should look like this:

24
tests/DMSTagTest.php Normal file
View File

@ -0,0 +1,24 @@
<?php
class DMSTagTest extends SapphireTest {
//static $fixture_file = "dms/tests/dmstest.yml";
function testAddingTags() {
$doc = new DMSDocument();
$doc->Filename = "test file";
$doc->Folder = "0";
$doc->write();
$doc->addTag("fruit","banana");
$doc->addTag("fruit","orange");
$doc->addTag("fruit","apple");
$doc->addTag("company","apple");
$doc->addTag("company","SilverStripe");
$fruits = $doc->getTags("fruit");
$this->assertNotNull($fruits,"Something returned for fruit tags");
$this->assertEquals(count($fruits),3,"3 fruit tags returned");
$this->assertArrayHasKey("banana",$fruits,"correct fruit tags returned");
}
}