From e4bc5535215dae647f16a1c5e6f7d9b4f683af0f Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 2 May 2017 14:49:41 +1200 Subject: [PATCH] API Add document sets, remove *Page methods from DMSDocument * Add 2.0.0 changelog * Update DMSInterface and DMSDocumentInterface removing *page and adding getDocumentSetsByPage to DMSInterface * Update use documentation and update unit tests This commit changes the relationship from Pages has_many Documents to Pages has_many DocumentSets which are many_many to Documents. The upload field has been upated to attach documents to a set instead of a page, the tests updated and the DMSInterface and DMSDocumentInterface updated to be less relevant to pages and more relevant to document sets. --- CONTRIBUTING.md | 5 + _config.php | 8 +- _config/dmsdocument.yml | 5 + code/DMS.php | 26 ++- code/cms/DMSDocumentAddController.php | 165 ++++++++++++-------- code/cms/DMSGridFieldAddNewButton.php | 20 +-- code/cms/DMSGridFieldDeleteAction.php | 2 +- code/cms/DMSGridFieldDetailForm.php | 2 +- code/cms/DMSUploadField.php | 71 +++++---- code/cms/DMSUploadField_ItemHandler.php | 7 +- code/extensions/DMSSiteTreeExtension.php | 156 +++++------------- code/interface/DMSDocumentInterface.php | 66 -------- code/interface/DMSInterface.php | 20 ++- code/model/DMSDocument.php | 140 ++++------------- code/model/DMSDocumentSet.php | 123 +++++++++++++++ docs/en/changelogs/2.0.0.md | 30 ++++ docs/en/configuration.md | 27 ++++ docs/en/creating-documents.md | 25 ++- docs/en/download-documents.md | 2 +- docs/en/index.md | 4 + docs/en/manage-page-relations.md | 35 ++++- docs/en/use-in-templates.md | 2 +- lang/en.yml | 25 ++- templates/DMSDocumentAddExistingField.ss | 101 +++++------- templates/DMSUploadField.ss | 12 +- templates/Includes/DocumentSet.ss | 11 ++ templates/Includes/DocumentSets.ss | 7 + templates/Includes/Documents.ss | 8 - tests/DMSDocumentSetTest.php | 35 +++++ tests/DMSDocumentTest.php | 155 ++---------------- tests/DMSEmbargoTest.php | 2 +- tests/DMSGridFieldAddNewButtonTest.php | 10 +- tests/DMSTest.php | 56 +++++-- tests/Stub/StubRelatedDocumentExtension.php | 30 +++- tests/dmsembargotest.yml | 4 + tests/dmstest.yml | 34 +++- 36 files changed, 750 insertions(+), 681 deletions(-) create mode 100644 CONTRIBUTING.md create mode 100644 code/model/DMSDocumentSet.php create mode 100644 docs/en/changelogs/2.0.0.md create mode 100644 templates/Includes/DocumentSet.ss create mode 100644 templates/Includes/DocumentSets.ss delete mode 100644 templates/Includes/Documents.ss create mode 100644 tests/DMSDocumentSetTest.php diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c61dd4a --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,5 @@ +# Contributing + +Any open source product is only as good as the community behind it. You can participate by sharing code, ideas, or simply helping others. No matter what your skill level is, every contribution counts. + +See our [high level overview](http://silverstripe.org/contributing-to-silverstripe) on silverstripe.org on how you can help out. diff --git a/_config.php b/_config.php index b288829..5d1c4b9 100644 --- a/_config.php +++ b/_config.php @@ -2,14 +2,10 @@ $config = Config::inst(); -DMSSiteTreeExtension::show_documents_tab(); //show the Documents tab on all pages -DMSSiteTreeExtension::no_documents_tab(); //and don't exclude it from any pages -DMSDocumentAddController::add_allowed_extensions(); //add an array of additional allowed extensions - -define('DMS_DIR', 'dms'); +define('DMS_DIR', basename(__DIR__)); if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . DMS_DIR)) { - user_error("DMS directory named incorrectly. Please install the DMS module into a folder named: ".DMS_DIR); + user_error('DMS directory named incorrectly. Please install the DMS module into a folder named: ' . DMS_DIR); } CMSMenu::remove_menu_item('DMSDocumentAddController'); diff --git a/_config/dmsdocument.yml b/_config/dmsdocument.yml index 9aa977f..9d796ee 100644 --- a/_config/dmsdocument.yml +++ b/_config/dmsdocument.yml @@ -5,11 +5,16 @@ After: framework/routes#coreroutes Director: rules: 'dmsdocument/$ID' : 'DMSDocument_Controller' + SiteTree: extensions: - DMSSiteTreeExtension + # Whether to show the document sets tab in the CMS for the page type this extension is applied to + documents_enabled: true + HtmlEditorField_Toolbar: extensions: - DocumentHtmlEditorFieldToolbar + DMSDocument_versions: enable_versions: true diff --git a/code/DMS.php b/code/DMS.php index 6746716..10b9348 100644 --- a/code/DMS.php +++ b/code/DMS.php @@ -126,15 +126,25 @@ class DMS implements DMSInterface // TODO: Implement getByFullTextSearch() method. } - /** - * Returns a list of Document objects associated with a Page - * @param $page SiteTree to fetch the associated Documents from - * @param bool $showEmbargoed Boolean that specifies if embargoed documents should be included in results - * @return DataList Document list associated with the Page - */ - public function getByPage($page, $showEmbargoed = false) + public function getByPage(SiteTree $page, $showEmbargoed = false) { - // TODO: Implement getByPage() method. + /** @var ArrayList $documents */ + $documents = $page->getAllDocuments(); + + if (!$showEmbargoed) { + foreach ($documents as $document) { + if ($document->isEmbargoed()) { + $documents->remove($document); + } + } + } + + return $documents; + } + + public function getDocumentSetsByPage(SiteTree $page) + { + return $page->getDocumentSets(); } /** diff --git a/code/cms/DMSDocumentAddController.php b/code/cms/DMSDocumentAddController.php index 899886c..77c4374 100644 --- a/code/cms/DMSDocumentAddController.php +++ b/code/cms/DMSDocumentAddController.php @@ -5,7 +5,6 @@ */ class DMSDocumentAddController extends LeftAndMain { - private static $url_segment = 'pages/adddocument'; private static $url_priority = 60; private static $required_permission_codes = 'CMS_ACCESS_AssetAdmin'; @@ -13,7 +12,13 @@ class DMSDocumentAddController extends LeftAndMain private static $tree_class = 'SiteTree'; private static $session_namespace = 'CMSMain'; - public static $allowed_extensions = array(); + /** + * Allowed file upload extensions, will be merged with `$allowed_extensions` from {@link File} + * + * @config + * @var array + */ + private static $allowed_extensions = array(); private static $allowed_actions = array( 'getEditForm', @@ -22,27 +27,10 @@ class DMSDocumentAddController extends LeftAndMain 'documentlist' ); - /** - * Add an array of additional allowed extensions - * @static - * @param $array - */ - public static function add_allowed_extensions($array = null) - { - if (empty($array)) { - return; - } - if (is_array($array)) { - self::$allowed_extensions = $array; - } else { - self::$allowed_extensions = array($array); - } - } - /** * Custom currentPage() method to handle opening the 'root' folder * - * @return + * @return SiteTree */ public function currentPage() { @@ -61,12 +49,27 @@ class DMSDocumentAddController extends LeftAndMain /** * Return fake-ID "root" if no ID is found (needed to upload files into the root-folder) + * + * @return int */ public function currentPageID() { return ($result = parent::currentPageID()) === null ? 0 : $result; } + /** + * Get the current document set, if a document set ID was provided + * + * @return DMSDocumentSet + */ + public function getCurrentDocumentSet() + { + if ($id = $this->getRequest()->getVar('dsid')) { + return DMSDocumentSet::get()->byid($id); + } + return singleton('DMSDocumentSet'); + } + /** * @return Form * @todo what template is used here? AssetAdmin_UploadContent.ss doesn't seem to be used anymore @@ -75,9 +78,13 @@ class DMSDocumentAddController extends LeftAndMain { Requirements::javascript(FRAMEWORK_DIR . '/javascript/AssetUploadField.js'); Requirements::css(FRAMEWORK_DIR . '/css/AssetUploadField.css'); - Requirements::css(DMS_DIR.'/css/DMSMainCMS.css'); + Requirements::css(DMS_DIR . '/css/DMSMainCMS.css'); + /** @var SiteTree $page */ $page = $this->currentPage(); + /** @var DMSDocumentSet $documentSet */ + $documentSet = $this->getCurrentDocumentSet(); + $uploadField = DMSUploadField::create('AssetUploadField', ''); $uploadField->setConfig('previewMaxWidth', 40); $uploadField->setConfig('previewMaxHeight', 30); @@ -87,31 +94,34 @@ class DMSDocumentAddController extends LeftAndMain $uploadField->addExtraClass('ss-assetuploadfield'); $uploadField->removeExtraClass('ss-uploadfield'); $uploadField->setTemplate('AssetUploadField'); - $uploadField->setRecord($page); + $uploadField->setRecord($documentSet); - $uploadField->getValidator()->setAllowedExtensions( - array_filter(array_merge(Config::inst()->get('File', 'allowed_extensions'), self::$allowed_extensions)) - ); + $uploadField->getValidator()->setAllowedExtensions($this->getAllowedExtensions()); $exts = $uploadField->getValidator()->getAllowedExtensions(); asort($exts); $backlink = $this->Backlink(); $done = " - - Done! + + " . _t('UploadField.DONE', 'DONE') . " "; - $addExistingField = new DMSDocumentAddExistingField('AddExisting', 'Add Existing'); - $addExistingField->setRecord($page); + $addExistingField = new DMSDocumentAddExistingField( + 'AddExisting', + _t('DMSDocumentAddExistingField.ADDEXISTING', 'Add Existing') + ); + $addExistingField->setRecord($documentSet); + $form = new Form( $this, 'getEditForm', new FieldList( new TabSet( - 'Main', + _t('DMSDocumentAddController.MAINTAB', 'Main'), new Tab( - 'From your computer', + _t('UploadField.FROMCOMPUTER', 'From your computer'), new HiddenField('ID', false, $page->ID), + new HiddenField('DSID', false, $documentSet->ID), $uploadField, new LiteralField( 'AllowedExtensions', @@ -123,7 +133,7 @@ class DMSDocumentAddController extends LeftAndMain ) ), new Tab( - 'From the CMS', + _t('UploadField.FROMCMS', 'From the CMS'), $addExistingField ) ) @@ -136,17 +146,6 @@ class DMSDocumentAddController extends LeftAndMain $form->Backlink = $backlink; // Don't use AssetAdmin_EditForm, as it assumes a different panel structure $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); - /*$form->Fields()->push( - new LiteralField( - 'BackLink', - sprintf( - '%s', - Controller::join_links(singleton('AssetAdmin')->Link('show'), $folder ? $folder->ID : 0), - _t('AssetAdmin.BackToFolder', 'Back to folder') - ) - ) - );*/ - //$form->loadDataFrom($folder); return $form; } @@ -170,29 +169,43 @@ class DMSDocumentAddController extends LeftAndMain } $items->push(new ArrayData(array( - 'Title' => 'Add Document', + 'Title' => _t('DMSDocumentSet.ADDDOCUMENTBUTTON', 'Add Document'), 'Link' => $this->Link() ))); return $items; } + /** + * Returns the link to be used to return the user after uploading a document. If a document set ID (dsid) is present + * then it will be redirected back to the page that owns the document set. @todo redirect back to the document set + * + * If no document set ID is present then we assume that it has been added from the model admin, so redirect back to + * that instead. + * + * @return string + */ public function Backlink() { - $pageID = $this->currentPageID(); - return Controller::join_links(singleton('CMSPagesController')->Link(), 'edit/show', $pageID); + if (!$this->getRequest()->getVar('dsid')) { + $modelAdmin = new DMSDocumentAdmin; + $modelAdmin->init(); + return $modelAdmin->Link(); + } + return Controller::join_links(singleton('CMSPagesController')->Link(), 'edit/show', $this->currentPageID()); } public function documentautocomplete() { - $term = (isset($_GET['term'])) ? $_GET['term'] : ''; - $term_sql = Convert::raw2sql($term); + $term = (string) $this->getRequest()->getVar('term'); + $termSql = 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); + ->where( + '("ID" LIKE \'%' . $termSql . '%\' OR "Filename" LIKE \'%' . $termSql . '%\'' + . ' OR "Title" LIKE \'%' . $termSql . '%\')' + ) + ->sort('ID ASC') + ->limit(20); $return = array(); foreach ($data as $doc) { @@ -202,22 +215,24 @@ class DMSDocumentAddController extends LeftAndMain ); } - - return json_encode($return); + return Convert::raw2json($return); } public function linkdocument() { $return = array('error' => _t('UploadField.FIELDNOTSET', 'Could not add document to page')); - +$return = array('error' => 'testing'); +return Convert::raw2json($return); $page = $this->currentPage(); if (!empty($page)) { - $document = DataObject::get_by_id('DMSDocument', (int) $_GET['documentID']); + $document = DMSDocument::get()->byId($this->getRequest()->getVar('documentID')); + // @todo add sets $document->addPage($page); $buttonText = ''; + . ' title="' . _t('DMSDocument.EDITDOCUMENT', 'Edit this document') . '" data-icon="pencil">' + . _t('DMSDocument.EDIT', 'Edit') . '' + . ''; // Collect all output data. $return = array( @@ -232,21 +247,27 @@ class DMSDocumentAddController extends LeftAndMain ); } - return json_encode($return); + return Convert::raw2json($return); } + /** + * Returns HTML representing a list of documents that are associated with the given page ID, across all document + * sets. + * + * @return string HTML + */ public function documentlist() { - if (!isset($_GET['pageID'])) { + if (!$this->getRequest()->getVar('pageID')) { return $this->httpError(400); } - $page = SiteTree::get()->byId($_GET['pageID']); + $page = SiteTree::get()->byId($this->getRequest()->getVar('pageID')); - if ($page && $page->Documents()->count() > 0) { + if ($page && $page->getAllDocuments()->count() > 0) { $list = ' -<% if isDisabled || isReadonly %> - <% if isSaveable %> +<% if $isDisabled || $isReadonly %> + <% if $isSaveable %> <% else %>
- <% _t('FileIFrameField.ATTACHONCESAVED2', 'Files can be attached once you have saved the record for the first time.') %> + <%t FileIFrameField.ATTACHONCESAVED2 "Files can be attached once you have saved the record for the first time." %>
<% end_if %> <% else %> @@ -41,8 +41,8 @@ -