API-CHANGE: additional DMS implementation code

This commit is contained in:
Julian Seidenberg 2012-07-17 17:58:33 +12:00
parent 27d666a08d
commit 5e4c295853
7 changed files with 208 additions and 4 deletions

View File

@ -0,0 +1,2 @@
<?php
Object::add_extension('SiteTree','DMSSiteTreeExtension');

View File

@ -6,6 +6,14 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
"Folder" => "Text" "Folder" => "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. * 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 * 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 * @return null
*/ */
function addPage($pageObject) { function addPage($pageObject) {
// TODO: Implement addPage() method. $this->Pages()->add($pageObject);
} }
/** /**
@ -22,7 +30,7 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
* @return mixed * @return mixed
*/ */
function removePage($pageObject) { function removePage($pageObject) {
// TODO: Implement removePage() method. $this->Pages()->remove($pageObject);
} }
/** /**
@ -30,7 +38,15 @@ class DMSDocument extends DataObject implements DMSDocumentInterface {
* @return DataList * @return DataList
*/ */
function getPages() { 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 * @return null
*/ */
function addTag($category, $value, $multiValue = true) { 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; 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();
}
} }

View File

@ -0,0 +1,7 @@
<?php
class DMSSiteTreeExtension extends DataExtension {
static $belongs_many_many = array(
'Documents' => 'DMSDocument'
);
}

15
code/DMSTag.php Normal file
View File

@ -0,0 +1,15 @@
<?php
/**
* Hold a set of metadata category/value tags associated with a DMSDocument
*/
class DMSTag extends DataObject {
static $db = array(
'Category' => 'varchar(1024)',
'Value' => 'varchar(1024)'
);
static $has_one = array(
'Document' => 'DMSDocument'
);
}

View File

@ -40,6 +40,13 @@ interface DMSDocumentInterface {
*/ */
function getPages(); 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. * 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. * Each category can have multiple values by default. So: addTag("fruit","banana") addTag("fruit", "apple") will add two items.

64
tests/DMSDocumentTest.php Normal file
View File

@ -0,0 +1,64 @@
<?php
class DMSDocumentTest extends SapphireTest {
static $fixture_file = "dms/tests/dmstest.yml";
function testPageRelations() {
$s1 = $this->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");
}
}

49
tests/dmstest.yml Normal file
View File

@ -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