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) { function addTag($category, $value, $multiValue = true) {
if ($multiValue) { if ($multiValue) {
//check for a duplicate tag, don't add the duplicate //check for a duplicate tag, don't add the duplicate
$currentTag = $this->Tags()->filter("Category = '$category' AND Value = '$value'"); $currentTag = $this->Tags()->filter(array('Category' => $category, 'Value' => $value));
if (!$currentTag) { if ($currentTag->Count() == 0) {
//multi value tag //multi value tag
$tag = new DMSTag(); $tag = new DMSTag();
$tag->Category = $category; $tag->Category = $category;
$tag->Value = $value; $tag->Value = $value;
$tag->DocumentID = $this->ID;
$tag->write(); $tag->write();
$tag->Documents()->add($this);
} }
} else { } else {
//single value tag //single value tag
$currentTag = $this->Tags()->filter("Category = '$category'"); $currentTag = $this->Tags()->filter(array('Category' => $category));
if (!$currentTag) { if (!$currentTag) {
//create the single-value tag //create the single-value tag
$tag = new DMSTag(); $tag = new DMSTag();
$tag->Category = $category; $tag->Category = $category;
$tag->Value = $value; $tag->Value = $value;
$tag->DocumentID = $this->ID;
$tag->write(); $tag->write();
$tag->Documents()->add($this);
} else { } else {
//update the single value tag //update the single value tag
$tag = $currentTag->first(); $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. * Quick way to add multiple tags to a Document. This takes a multidimensional array of category/value pairs.
* The array should look like this: * The array should look like this:

View File

@ -62,6 +62,16 @@ interface DMSDocumentInterface {
*/ */
function addTag($category, $value, $multiValue = true); 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. * Quick way to add multiple tags to a DMSDocument. This takes a multidimensional array of category/value pairs.
* The array should look like this: * 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");
}
}