/** * Creates a new version of a document by moving the current file and * renaming it to the versioned filename. * * This method assumes that the method calling this is just about to upload * a new file to replace the old file. * * @static * @param DMSDocument $doc * * @return bool Success or failure */ public static function create_version(DMSDocument $doc) { $success = false; $existingPath = $doc->getFullPath(); if (is_file($existingPath)) { $docData = $doc->toMap(); unset($docData['ID']); $version = new DMSDocument_versions($docData); //create a copy of the current DMSDocument as a version $previousVersionCounter = 0; $newestExistingVersion = self::get_versions($doc)->sort(array('Created' => 'DESC', 'ID' => 'DESC'))->limit(1); if ($newestExistingVersion && $newestExistingVersion->Count() > 0) { $previousVersionCounter = $newestExistingVersion->first()->VersionCounter; } //change the filename field to a field containing the new soon-to-be versioned file $version->VersionCounter = $previousVersionCounter + 1; //start versions at 1 $newFilename = $version->generateVersionedFilename($doc, $version->VersionCounter); $version->Filename = $newFilename; //add a relation back to the origin ID; $version->DocumentID = $doc->ID; $id = $version->write(); if (!empty($id)) { rename($existingPath, $version->getFullPath()); $success = true; } } return $success; }
/** * Takes a File object or a String (path to a file) and copies it into the DMS. The original file remains unchanged. * When storing a document, sets the fields on the File has "tag" metadata. * @param $file File object, or String that is path to a file to store, e.g. "assets/documents/industry/supplied-v1-0.pdf" */ function storeDocument($file) { $filePath = self::transform_file_to_file_path($file); //create a new document and get its ID $doc = new DMSDocument(); $doc->write(); $doc->storeDocument($filePath); return $doc; }
public static function handle($arguments, $content, ShortcodeParser $parser, $tag, array $extra = array()) { if (!empty($arguments['id'])) { $document = DMSDocument::get()->byID($arguments['id']); if ($document && !$document->isHidden()) { if ($content) { return sprintf('<a href="%s">%s</a>', $document->Link(), $parser->parse($content)); } else { if (isset($extra['element'])) { $extra['element']->setAttribute('data-ext', $document->getExtension()); $extra['element']->setAttribute('data-size', $document->getFileSizeFormatted()); } return $document->Link(); } } } $error = ErrorPage::get()->filter('ErrorCode', '404')->First(); if ($error) { return $error->Link(); } return ''; }
public function documentautocomplete() { $term = isset($_GET['term']) ? $_GET['term'] : ''; $term_sql = Convert::raw2sql($term); $data = DMSDocument::get()->where("(\"ID\" LIKE '%" . $term_sql . "%' OR \"Filename\" LIKE '%" . $term_sql . "%' OR \"Title\" LIKE '%" . $term_sql . "%')")->sort('ID ASC')->limit(20); $return = array(); foreach ($data as $doc) { $return[] = array('label' => $doc->ID . ' - ' . $doc->Title, 'value' => $doc->ID); } return json_encode($return); }
function testEmbargoUntilPublished() { $s1 = $this->objFromFixture('SiteTree', 's1'); $doc = new DMSDocument(); $doc->Filename = "test file"; $doc->Folder = "0"; $dID = $doc->write(); $doc->addPage($s1); $s1->publish('Stage', 'Live'); $s1->doPublish(); $this->assertFalse($doc->isHidden(), "Document is not hidden"); $this->assertFalse($doc->isEmbargoed(), "Document is not embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); $doc->embargoUntilPublished(); $this->assertTrue($doc->isHidden(), "Document is hidden"); $this->assertTrue($doc->isEmbargoed(), "Document is embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); $s1->publish('Stage', 'Live'); $s1->doPublish(); $doc = DataObject::get_by_id("DMSDocument", $dID); $this->assertFalse($doc->isHidden(), "Document is not hidden"); $this->assertFalse($doc->isEmbargoed(), "Document is not embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); $doc->embargoUntilPublished(); $doc = DataObject::get_by_id("DMSDocument", $dID); $this->assertTrue($doc->isHidden(), "Document is hidden"); $this->assertTrue($doc->isEmbargoed(), "Document is embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); $doc->embargoIndefinitely(); $doc = DataObject::get_by_id("DMSDocument", $dID); $this->assertTrue($doc->isHidden(), "Document is hidden"); $this->assertTrue($doc->isEmbargoed(), "Document is embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); $s1->publish('Stage', 'Live'); $s1->doPublish(); $doc = DataObject::get_by_id("DMSDocument", $dID); $this->assertTrue($doc->isHidden(), "Document is still hidden because although the untilPublish flag is cleared, the indefinitely flag is still there"); $this->assertTrue($doc->isEmbargoed(), "Document is embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); $doc->clearEmbargo(); $doc = DataObject::get_by_id("DMSDocument", $dID); $this->assertFalse($doc->isHidden(), "Document is not hidden"); $this->assertFalse($doc->isEmbargoed(), "Document is not embargoed"); $this->assertFalse($doc->isExpired(), "Document is not expired"); }
function testRemovingTags() { $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"); $companies = $doc->getTagsList("company"); $this->assertNotNull($companies, "Companies returned before deletion"); $this->assertEquals(count($companies), 2, "Two companies returned before deletion"); //delete an entire category $doc->removeTag("company"); $companies = $doc->getTagsList("company"); $this->assertNull($companies, "All companies deleted"); $fruit = $doc->getTagsList("fruit"); $this->assertEquals(count($fruit), 3, "Three fruits returned before deletion"); //delete a single tag $doc->removeTag("fruit", "apple"); $fruit = $doc->getTagsList("fruit"); $this->assertEquals(count($fruit), 2, "Two fruits returned after deleting one"); //delete a single tag $doc->removeTag("fruit", "orange"); $fruit = $doc->getTagsList("fruit"); $this->assertEquals(count($fruit), 1, "One fruits returned after deleting two"); //nothing happens when deleting tag that doesn't exist $doc->removeTag("fruit", "jellybean"); $fruit = $doc->getTagsList("fruit"); $this->assertEquals(count($fruit), 1, "One fruits returned after attempting to delete non-existent fruit"); //delete the last fruit $doc->removeTag("fruit", "banana"); $fruit = $doc->getTagsList("fruit"); $this->assertNull($fruit, "All fruits deleted"); $tags = DataObject::get("DMSTag"); $this->assertEquals($tags->Count(), 0, "No DMS tag objects remain after deletion"); }
function testUnpublishPageWithAssociatedDocuments() { $s2 = $this->objFromFixture('SiteTree', 's2'); $s2->publish('Stage', 'Live'); $s2ID = $s2->ID; $doc = new DMSDocument(); $doc->Filename = "delete test file"; $doc->Folder = "0"; $doc->write(); $doc->addPage($s2); $s2->doDeleteFromLive(); $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'"); $this->assertEquals($documents->Count(), '1', "Deleting a page from live stage doesn't delete the associated docs," . "even if it's the last page they're associated with"); $s2 = Versioned::get_one_by_stage('SiteTree', 'Stage', sprintf('"SiteTree"."ID" = %d', $s2ID)); $s2->delete(); $documents = DataObject::get("DMSDocument", "\"Filename\" = 'delete test file'"); $this->assertEquals($documents->Count(), '0', "However, deleting the draft version of the last page that a document is " . "associated with causes that document to be deleted as well"); }