From 5e4c2958531befe87ef642cab071762af7e50fb3 Mon Sep 17 00:00:00 2001 From: Julian Seidenberg Date: Tue, 17 Jul 2012 17:58:33 +1200 Subject: [PATCH] API-CHANGE: additional DMS implementation code --- _config.php | 2 + code/DMSDocument.php | 68 +++++++++++++++++++++++-- code/DMSSiteTreeExtension.php | 7 +++ code/DMSTag.php | 15 ++++++ code/interface/DMSDocumentInterface.php | 7 +++ tests/DMSDocumentTest.php | 64 +++++++++++++++++++++++ tests/dmstest.yml | 49 ++++++++++++++++++ 7 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 code/DMSSiteTreeExtension.php create mode 100644 code/DMSTag.php create mode 100644 tests/DMSDocumentTest.php create mode 100644 tests/dmstest.yml diff --git a/_config.php b/_config.php index e69de29..74053d5 100644 --- a/_config.php +++ b/_config.php @@ -0,0 +1,2 @@ + "Text" ); + static $has_many = array( + 'Tags' => 'DMSTag' + ); + + static $many_many = array( + 'Pages' => 'SiteTree' + ); + /** * Associates this document with a Page. This method does nothing if the association already exists. * This could be a simple wrapper around $myDoc->Pages()->add($myPage) to add a has_many relation @@ -13,7 +21,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { * @return null */ function addPage($pageObject) { - // TODO: Implement addPage() method. + $this->Pages()->add($pageObject); } /** @@ -22,7 +30,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { * @return mixed */ function removePage($pageObject) { - // TODO: Implement removePage() method. + $this->Pages()->remove($pageObject); } /** @@ -30,7 +38,15 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { * @return DataList */ function getPages() { - // TODO: Implement getPages() method. + $this->Pages(); + } + + /** + * Removes all associated Pages from the DMSDocument + * @return null + */ + function removeAllPages() { + $this->Pages()->removeAll(); } /** @@ -46,7 +62,34 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { * @return null */ function addTag($category, $value, $multiValue = true) { - // TODO: Implement addTag() method. + if ($multiValue) { + //check for a duplicate tag, don't add the duplicate + $currentTag = $this->Tags()->filter("Category = '$category' AND Value = '$value'"); + if (!$currentTag) { + //multi value tag + $tag = new DMSTag(); + $tag->Category = $category; + $tag->Value = $value; + $tag->DocumentID = $this->ID; + $tag->write(); + } + } else { + //single value tag + $currentTag = $this->Tags()->filter("Category = '$category'"); + if (!$currentTag) { + //create the single-value tag + $tag = new DMSTag(); + $tag->Category = $category; + $tag->Value = $value; + $tag->DocumentID = $this->ID; + $tag->write(); + } else { + //update the single value tag + $tag = $currentTag->first(); + $tag->Value = $value; + $tag->write(); + } + } } /** @@ -193,4 +236,21 @@ class DMSDocument extends DataObject implements DMSDocumentInterface { return DMS::$dmsPath . DIRECTORY_SEPARATOR . $this->Folder . DIRECTORY_SEPARATOR . $this->Filename; } + /** + * Deletes the DMSDocument, its underlying file, as well as any tags related to this DMSDocument. Also calls the + * parent DataObject's delete method. + */ + function delete() { + //remove tags + $this->removeAllTags(); + + //delete the file + unlink($this->getFullPath()); + + $this->removeAllPages(); + + //delete the dataobject + parent::delete(); + } + } \ No newline at end of file diff --git a/code/DMSSiteTreeExtension.php b/code/DMSSiteTreeExtension.php new file mode 100644 index 0000000..25492b8 --- /dev/null +++ b/code/DMSSiteTreeExtension.php @@ -0,0 +1,7 @@ + 'DMSDocument' + ); +} \ No newline at end of file diff --git a/code/DMSTag.php b/code/DMSTag.php new file mode 100644 index 0000000..c554e67 --- /dev/null +++ b/code/DMSTag.php @@ -0,0 +1,15 @@ + 'varchar(1024)', + 'Value' => 'varchar(1024)' + ); + + static $has_one = array( + 'Document' => 'DMSDocument' + ); +} \ No newline at end of file diff --git a/code/interface/DMSDocumentInterface.php b/code/interface/DMSDocumentInterface.php index df18f21..ee6ad7e 100644 --- a/code/interface/DMSDocumentInterface.php +++ b/code/interface/DMSDocumentInterface.php @@ -40,6 +40,13 @@ interface DMSDocumentInterface { */ function getPages(); + /** + * Removes all associated Pages from the DMSDocument + * @abstract + * @return null + */ + function removeAllPages(); + /** * Adds a metadata tag to the DMSDocument. The tag has a category and a value. * Each category can have multiple values by default. So: addTag("fruit","banana") addTag("fruit", "apple") will add two items. diff --git a/tests/DMSDocumentTest.php b/tests/DMSDocumentTest.php new file mode 100644 index 0000000..be1198b --- /dev/null +++ b/tests/DMSDocumentTest.php @@ -0,0 +1,64 @@ +objFromFixture('SiteTree','s1'); + $s2 = $this->objFromFixture('SiteTree','s2'); + $s3 = $this->objFromFixture('SiteTree','s3'); + $s4 = $this->objFromFixture('SiteTree','s4'); + $s5 = $this->objFromFixture('SiteTree','s5'); + $s6 = $this->objFromFixture('SiteTree','s6'); + + $d1 = $this->objFromFixture('DMSDocument','d1'); + + $pages = $d1->Pages(); + $pagesArray = $pages->toArray(); + $this->assertEquals($pagesArray[0], $s1, "Page 1 associated correctly"); + $this->assertEquals($pagesArray[1], $s2, "Page 2 associated correctly"); + $this->assertEquals($pagesArray[2], $s3, "Page 3 associated correctly"); + $this->assertEquals($pagesArray[3], $s4, "Page 4 associated correctly"); + $this->assertEquals($pagesArray[4], $s5, "Page 5 associated correctly"); + $this->assertEquals($pagesArray[5], $s6, "Page 6 associated correctly"); + } + + function testAddPageRelation() { + $s1 = $this->objFromFixture('SiteTree','s1'); + $s2 = $this->objFromFixture('SiteTree','s2'); + $s3 = $this->objFromFixture('SiteTree','s3'); + + $doc = new DMSDocument(); + $doc->Filename = "test file"; + $doc->Folder = "0"; + $doc->write(); + + $doc->addPage($s1); + $doc->addPage($s2); + $doc->addPage($s3); + + $pages = $doc->Pages(); + $pagesArray = $pages->toArray(); + $this->assertEquals($pagesArray[0], $s1, "Page 1 associated correctly"); + $this->assertEquals($pagesArray[1], $s2, "Page 2 associated correctly"); + $this->assertEquals($pagesArray[2], $s3, "Page 3 associated correctly"); + + $doc->removePage($s1); + $pages = $doc->Pages(); + $pagesArray = $pages->toArray(); //page 1 is missing + $this->assertEquals($pagesArray[0], $s2, "Page 2 still associated correctly"); + $this->assertEquals($pagesArray[1], $s3, "Page 3 still associated correctly"); + + $documents = $s2->Documents(); + $documentsArray = $documents->toArray(); + $this->assertContains($documentsArray, $doc, "Document associated with page"); + + $doc->removeAllPages(); + $pages = $doc->Pages(); + $this->assertEquals($pages->Count(), 0, "All pages removed"); + + $documents = $s2->Documents(); + $documentsArray = $documents->toArray(); + $this->assertNotContains($doc, $documentsArray, "Document no longer associated with page"); + } +} \ No newline at end of file diff --git a/tests/dmstest.yml b/tests/dmstest.yml new file mode 100644 index 0000000..d476c2d --- /dev/null +++ b/tests/dmstest.yml @@ -0,0 +1,49 @@ +SiteTree: + s1: + Title: testPage1 + URLSegment: s1 + s2: + Title: testPage2 + URLSegment: s2 + s3: + Title: testPage3 + URLSegment: s3 + s4: + Title: testPage4 + URLSegment: s4 + s5: + Title: testPage5 + URLSegment: s5 + s6: + Title: testPage6 + URLSegment: s6 +DMSTag: + t1: + Category: tag1 + Value: tag1value + t2: + Category: tag2 + Value: tag2value + t3: + Category: tag3 + Value: tag3value + t4: + Category: tag4 + Value: tag4value + t5: + Category: tag5 + Value: tag5value + t6: + Category: tag6 + Value: tag6value +DMSDocument: + d1: + Filename: test-file-file-doesnt-exist + Folder: 5 + Tags: =>DMSTag.t1, =>DMSTag.t2, =>DMSTag.t3, =>DMSTag.t4 + Pages: =>SiteTree.s1, =>SiteTree.s2, =>SiteTree.s3, =>SiteTree.s4, =>SiteTree.s5, =>SiteTree.s6 + d2: + Filename: test-file-file-doesnt-exist + Folder: 5 + Tags: =>DMSTag.t5, =>DMSTag.t6 + Pages: =>SiteTree.s5, =>SiteTree.s6 \ No newline at end of file