first commit
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/library/LibraryFileAdminGridDataProvider.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class LibraryFileAdminGridDataProvider
|
||||
*
|
||||
* @ingroup controllers_grid_settings_library
|
||||
*
|
||||
* @brief The data provider for the admin library files grid.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\library;
|
||||
|
||||
use PKP\context\Context;
|
||||
use PKP\context\LibraryFileDAO;
|
||||
use PKP\controllers\grid\CategoryGridDataProvider;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\security\authorization\ContextAccessPolicy;
|
||||
|
||||
class LibraryFileAdminGridDataProvider extends CategoryGridDataProvider
|
||||
{
|
||||
/** @var Context the context for this library */
|
||||
public $_context;
|
||||
|
||||
/** @var bool Whether or not this grid is editable */
|
||||
public $_canEdit;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct($canEdit)
|
||||
{
|
||||
$this->_canEdit = $canEdit;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Getters and Setters
|
||||
//
|
||||
|
||||
/**
|
||||
* @copydoc GridDataProvider::getAuthorizationPolicy()
|
||||
*/
|
||||
public function getAuthorizationPolicy($request, $args, $roleAssignments)
|
||||
{
|
||||
$this->_context = $request->getContext();
|
||||
return new ContextAccessPolicy($request, $roleAssignments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc GridDataProvider::getRequestArgs()
|
||||
*/
|
||||
public function getRequestArgs()
|
||||
{
|
||||
return ['canEdit' => $this->canEdit()];
|
||||
}
|
||||
|
||||
/**
|
||||
* get the current context
|
||||
*
|
||||
* @return $context Context
|
||||
*/
|
||||
public function &getContext()
|
||||
{
|
||||
return $this->_context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get whether or not this grid is editable (has actions).
|
||||
*
|
||||
* @return bool $canEdit
|
||||
*/
|
||||
public function canEdit()
|
||||
{
|
||||
return $this->_canEdit;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @copydoc CategoryGridHandler::loadCategoryData()
|
||||
*
|
||||
* @param null|mixed $filter
|
||||
*/
|
||||
public function loadCategoryData($request, $fileType, $filter = null)
|
||||
{
|
||||
// Elements to be displayed in the grid
|
||||
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /** @var LibraryFileDAO $libraryFileDao */
|
||||
$context = $this->getContext();
|
||||
$libraryFiles = $libraryFileDao->getByContextId($context->getId(), $fileType);
|
||||
|
||||
return $libraryFiles->toAssociativeArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/library/LibraryFileAdminGridHandler.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class LibraryFileAdminGridHandler
|
||||
*
|
||||
* @ingroup controllers_grid_settings_library
|
||||
*
|
||||
* @brief Handle library file grid requests.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\library;
|
||||
|
||||
use PKP\context\Context;
|
||||
use PKP\controllers\grid\files\LibraryFileGridHandler;
|
||||
use PKP\controllers\grid\settings\library\form\EditLibraryFileForm;
|
||||
use PKP\controllers\grid\settings\library\form\NewLibraryFileForm;
|
||||
use PKP\security\Role;
|
||||
|
||||
class LibraryFileAdminGridHandler extends LibraryFileGridHandler
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new LibraryFileAdminGridDataProvider(true));
|
||||
$this->addRoleAssignment(
|
||||
[Role::ROLE_ID_MANAGER, Role::ROLE_ID_SITE_ADMIN],
|
||||
[
|
||||
'fetchGrid', 'addFile', 'uploadFile', 'saveFile', // Adding new library files
|
||||
'editFile', 'updateFile', // Editing existing library files
|
||||
'deleteFile'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Overridden template methods
|
||||
//
|
||||
|
||||
/**
|
||||
* Configure the grid
|
||||
*
|
||||
* @see LibraryGridHandler::initialize
|
||||
*
|
||||
* @param null|mixed $args
|
||||
*/
|
||||
public function initialize($request, $args = null)
|
||||
{
|
||||
// determine if this grid is read only.
|
||||
$this->setCanEdit((bool) $request->getUserVar('canEdit'));
|
||||
|
||||
parent::initialize($request, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific instance of the new form for this grid.
|
||||
*
|
||||
* @param Context $context
|
||||
*
|
||||
* @return NewLibraryFileForm
|
||||
*/
|
||||
public function _getNewFileForm($context)
|
||||
{
|
||||
return new NewLibraryFileForm($context->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific instance of the edit form for this grid.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param int $fileId
|
||||
*
|
||||
* @return EditLibraryFileForm
|
||||
*/
|
||||
public function _getEditFileForm($context, $fileId)
|
||||
{
|
||||
return new EditLibraryFileForm($context->getId(), $fileId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/library/form/EditLibraryFileForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class EditLibraryFileForm
|
||||
*
|
||||
* @ingroup controllers_grid_file_form
|
||||
*
|
||||
* @brief Form for editing a library file
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\library\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\file\LibraryFileManager;
|
||||
use PKP\context\LibraryFile;
|
||||
use PKP\context\LibraryFileDAO;
|
||||
use PKP\controllers\grid\files\form\LibraryFileForm;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\file\TemporaryFileManager;
|
||||
|
||||
class EditLibraryFileForm extends LibraryFileForm
|
||||
{
|
||||
/** @var LibraryFile the file being edited, or null for new */
|
||||
public $libraryFile;
|
||||
|
||||
/** @var int the id of the context this library file is attached to */
|
||||
public $contextId;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $contextId
|
||||
* @param int $fileId optional
|
||||
*/
|
||||
public function __construct($contextId, $fileId)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/library/form/editFileForm.tpl', $contextId);
|
||||
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /** @var LibraryFileDAO $libraryFileDao */
|
||||
$this->libraryFile = $libraryFileDao->getById($fileId);
|
||||
|
||||
if (!$this->libraryFile || $this->libraryFile->getContextId() != $this->contextId) {
|
||||
fatalError('Invalid library file!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
* @see Form::readInputData()
|
||||
*/
|
||||
function readInputData() {
|
||||
$this->readUserVars(array('temporaryFileId'));
|
||||
return parent::readInputData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current settings.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$this->_data = [
|
||||
'libraryFileName' => $this->libraryFile->getName(null), // Localized
|
||||
'libraryFile' => $this->libraryFile, // For read-only info
|
||||
'publicAccess' => $this->libraryFile->getPublicAccess() ? true : false,
|
||||
'temporaryFileId' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$userId = Application::get()->getRequest()->getUser()->getId();
|
||||
|
||||
// Fetch the temporary file storing the uploaded library file
|
||||
$temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO'); /* @var $temporaryFileDao TemporaryFileDAO */
|
||||
$temporaryFile = $temporaryFileDao->getTemporaryFile(
|
||||
$this->getData('temporaryFileId'),
|
||||
$userId
|
||||
);
|
||||
if ($temporaryFile) {
|
||||
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /* @var $libraryFileDao LibraryFileDAO */
|
||||
$libraryFileManager = new LibraryFileManager($this->contextId);
|
||||
|
||||
// Convert the temporary file to a library file and store
|
||||
$this->libraryFile = $libraryFileManager->replaceFromTemporaryFile($temporaryFile, $this->getData('fileType'), $this->libraryFile);
|
||||
// Clean up the temporary file
|
||||
$temporaryFileManager = new TemporaryFileManager();
|
||||
$temporaryFileManager->deleteById($this->getData('temporaryFileId'), $userId);
|
||||
}
|
||||
$this->libraryFile->setName($this->getData('libraryFileName'), null); // Localized
|
||||
$this->libraryFile->setType($this->getData('fileType'));
|
||||
$this->libraryFile->setPublicAccess($this->getData('publicAccess'));
|
||||
|
||||
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /** @var LibraryFileDAO $libraryFileDao */
|
||||
$libraryFileDao->updateObject($this->libraryFile);
|
||||
parent::execute(...$functionArgs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/library/form/NewLibraryFileForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class NewLibraryFileForm
|
||||
*
|
||||
* @ingroup controllers_grid_file_form
|
||||
*
|
||||
* @brief Form for adding/editing a file
|
||||
* stores/retrieves from an associative array
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\library\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\file\LibraryFileManager;
|
||||
use PKP\context\LibraryFileDAO;
|
||||
use PKP\controllers\grid\files\form\LibraryFileForm;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\file\TemporaryFileDAO;
|
||||
use PKP\file\TemporaryFileManager;
|
||||
|
||||
class NewLibraryFileForm extends LibraryFileForm
|
||||
{
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $contextId
|
||||
*/
|
||||
public function __construct($contextId)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/library/form/newFileForm.tpl', $contextId);
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'temporaryFileId', 'required', 'settings.libraryFiles.fileRequired'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*
|
||||
* @see Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['temporaryFileId']);
|
||||
return parent::readInputData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*
|
||||
* @return $fileId int The new library file id.
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$userId = Application::get()->getRequest()->getUser()->getId();
|
||||
|
||||
// Fetch the temporary file storing the uploaded library file
|
||||
$temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO'); /** @var TemporaryFileDAO $temporaryFileDao */
|
||||
$temporaryFile = $temporaryFileDao->getTemporaryFile(
|
||||
$this->getData('temporaryFileId'),
|
||||
$userId
|
||||
);
|
||||
$libraryFileDao = DAORegistry::getDAO('LibraryFileDAO'); /** @var LibraryFileDAO $libraryFileDao */
|
||||
$libraryFileManager = new LibraryFileManager($this->contextId);
|
||||
|
||||
// Convert the temporary file to a library file and store
|
||||
$libraryFile = $libraryFileManager->copyFromTemporaryFile($temporaryFile, $this->getData('fileType'));
|
||||
assert(isset($libraryFile));
|
||||
$libraryFile->setContextId($this->contextId);
|
||||
$libraryFile->setName($this->getData('libraryFileName'), null); // Localized
|
||||
$libraryFile->setType($this->getData('fileType'));
|
||||
$libraryFile->setPublicAccess($this->getData('publicAccess'));
|
||||
|
||||
$fileId = $libraryFileDao->insertObject($libraryFile);
|
||||
|
||||
// Clean up the temporary file
|
||||
$temporaryFileManager = new TemporaryFileManager();
|
||||
$temporaryFileManager->deleteById($this->getData('temporaryFileId'), $userId);
|
||||
parent::execute(...$functionArgs);
|
||||
return $fileId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user