From e50f776a0df5072886ed374a5c96f556bde445fe Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Wed, 25 Jul 2012 18:56:43 +1200 Subject: [PATCH] ENHANCEMENT: implementing adding tags --- code/DMSDocument.php | 35 +++++++++++++++++++++---- code/interface/DMSDocumentInterface.php | 10 +++++++ tests/DMSTagTest.php | 24 +++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 tests/DMSTagTest.php diff --git a/code/DMSDocument.php b/code/DMSDocument.php index bbc8046..f980b36 100644 --- a/code/DMSDocument.php +++ b/code/DMSDocument.php @@ -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: diff --git a/code/interface/DMSDocumentInterface.php b/code/interface/DMSDocumentInterface.php index ee6ad7e..702e855 100644 --- a/code/interface/DMSDocumentInterface.php +++ b/code/interface/DMSDocumentInterface.php @@ -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: diff --git a/tests/DMSTagTest.php b/tests/DMSTagTest.php new file mode 100644 index 0000000..9d7a86c --- /dev/null +++ b/tests/DMSTagTest.php @@ -0,0 +1,24 @@ +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"); + } +} \ No newline at end of file