Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
PKPManageFileApiHandler.inc.php
1 <?php
2 
16 // Import the base handler.
17 import('classes.handler.Handler');
18 import('lib.pkp.classes.core.JSONMessage');
19 
20 abstract class PKPManageFileApiHandler extends Handler {
21 
25  function PKPManageFileApiHandler() {
26  parent::Handler();
27  $this->addRoleAssignment(
28  array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT, ROLE_ID_REVIEWER, ROLE_ID_AUTHOR),
29  array('deleteFile', 'editMetadata', 'saveMetadata')
30  );
31  }
32 
33  //
34  // Implement methods from PKPHandler
35  //
36  function authorize($request, &$args, $roleAssignments) {
37  import('lib.pkp.classes.security.authorization.SubmissionFileAccessPolicy');
38  $this->addPolicy(new SubmissionFileAccessPolicy($request, $args, $roleAssignments, SUBMISSION_FILE_ACCESS_MODIFY));
39 
40  return parent::authorize($request, $args, $roleAssignments);
41  }
42 
43  //
44  // Public handler methods
45  //
52  function deleteFile($args, $request) {
53  $submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
54  $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
55  $stageId = $request->getUserVar('stageId');
56  if ($stageId) {
57  // validate the stage id.
58  $stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
59  $user = $request->getUser();
60  $stageAssignments = $stageAssignmentDao->getBySubmissionAndStageId($submission->getId(), $stageId, null, $user->getId());
61  }
62 
63  assert($submissionFile && $submission); // Should have been validated already
64 
65  $noteDao = DAORegistry::getDAO('NoteDAO');
66  $noteDao->deleteByAssoc(ASSOC_TYPE_SUBMISSION_FILE, $submissionFile->getFileId());
67 
68  // Delete the submission file.
69  $submissionFileDao = DAORegistry::getDAO('SubmissionFileDAO'); /* @var $submissionFileDao SubmissionFileDAO */
70 
71  // check to see if we need to remove review_round_file associations
72  if (!$stageAssignments->wasEmpty()) {
73  $submissionFileDao->deleteReviewRoundAssignment($submission->getId(), $stageId, $submissionFile->getFileId(), $submissionFile->getRevision());
74  }
75  $success = (boolean)$submissionFileDao->deleteRevisionById($submissionFile->getFileId(), $submissionFile->getRevision(), $submissionFile->getFileStage(), $submission->getId());
76 
77  $notificationMgr = new NotificationManager();
78  if ($success) {
79  if ($submissionFile->getFileStage() == SUBMISSION_FILE_REVIEW_REVISION) {
80  // Get a list of author user IDs
81  $authorUserIds = array();
82  $stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
83  $submitterAssignments = $stageAssignmentDao->getBySubmissionAndRoleId($submission->getId(), ROLE_ID_AUTHOR);
84  while ($assignment = $submitterAssignments->next()) {
85  $authorUserIds[] = $assignment->getUserId();
86  }
87 
88  // Update the notifications
89  $notificationMgr->updateNotification(
90  $request,
91  array(NOTIFICATION_TYPE_PENDING_INTERNAL_REVISIONS, NOTIFICATION_TYPE_PENDING_EXTERNAL_REVISIONS),
92  $authorUserIds,
93  ASSOC_TYPE_SUBMISSION,
94  $submission->getId()
95  );
96 
97  $reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO');
98  $lastReviewRound = $reviewRoundDao->getLastReviewRoundBySubmissionId($submission->getId(), $stageId);
99  $notificationMgr->updateNotification(
100  $request,
101  array(NOTIFICATION_TYPE_ALL_REVISIONS_IN),
102  null,
103  ASSOC_TYPE_REVIEW_ROUND,
104  $lastReviewRound->getId()
105  );
106  }
107 
108  $this->removeFileIndex($submission, $submissionFile);
109  $fileManager = $this->getFileManager($submission->getContextId(), $submission->getId());
110  $fileManager->deleteFile($submissionFile->getFileId(), $submissionFile->getRevision());
111 
112  $this->setupTemplate($request);
113  $user = $request->getUser();
114  if (!$request->getUserVar('suppressNotification')) NotificationManager::createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedFile')));
115 
116  $this->logDeletionEvent($request, $submission, $submissionFile, $user);
117 
118  return DAO::getDataChangedEvent();
119  } else {
120  return new JSONMessage(false);
121  }
122  }
123 
130  function editMetadata($args, $request) {
131  $submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
132  $reviewRound = $this->getAuthorizedContextObject(ASSOC_TYPE_REVIEW_ROUND);
133  $stageId = $request->getUserVar('stageId');
134  $metadataForm = $submissionFile->getMetadataForm($stageId, $reviewRound);
135  $metadataForm->setShowButtons(true);
136  return new JSONMessage(true, $metadataForm->fetch($request));
137  }
138 
146  function saveMetadata($args, $request) {
147  $submission = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION);
148  $submissionFile = $this->getAuthorizedContextObject(ASSOC_TYPE_SUBMISSION_FILE);
149  $reviewRound = $this->getAuthorizedContextObject(ASSOC_TYPE_REVIEW_ROUND);
150  $stageId = $request->getUserVar('stageId');
151  $metadataForm = $submissionFile->getMetadataForm($stageId, $reviewRound);
152  $metadataForm->readInputData();
153  if ($metadataForm->validate()) {
154  $metadataForm->execute($args, $request);
155  $submissionFile = $metadataForm->getSubmissionFile();
156 
157  // Get a list of author user IDs
158  $authorUserIds = array();
159  $stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
160  $submitterAssignments = $stageAssignmentDao->getBySubmissionAndRoleId($submission->getId(), ROLE_ID_AUTHOR);
161  while ($assignment = $submitterAssignments->next()) {
162  $authorUserIds[] = $assignment->getUserId();
163  }
164 
165  // Update the notifications
166  $notificationMgr = new NotificationManager(); /* @var $notificationMgr NotificationManager */
167  $notificationMgr->updateNotification(
168  $request,
169  $this->getUpdateNotifications(),
170  $authorUserIds,
171  ASSOC_TYPE_SUBMISSION,
172  $submission->getId()
173  );
174 
175  if ($reviewRound) {
176  $notificationMgr->updateNotification(
177  $request,
178  array(NOTIFICATION_TYPE_ALL_REVISIONS_IN),
179  null,
180  ASSOC_TYPE_REVIEW_ROUND,
181  $reviewRound->getId()
182  );
183 
184  // Delete any 'revision requested' notifications since all revisions are now in.
185  $context = $request->getContext();
186  $notificationDao = DAORegistry::getDAO('NotificationDAO');
187  $stageAssignmentDao = DAORegistry::getDAO('StageAssignmentDAO');
188  $submitterAssignments = $stageAssignmentDao->getBySubmissionAndRoleId($submission->getId(), ROLE_ID_AUTHOR);
189  while ($assignment = $submitterAssignments->next()) {
190  $notificationDao->deleteByAssoc(ASSOC_TYPE_SUBMISSION, $submission->getId(), $assignment->getUserId(), NOTIFICATION_TYPE_EDITOR_DECISION_PENDING_REVISIONS, $context->getId());
191  }
192  }
193 
194  // Log the upload event
195  import('lib.pkp.classes.log.SubmissionLog');
196  import('classes.log.SubmissionEventLogEntry');
197  import('lib.pkp.classes.log.SubmissionFileEventLogEntry'); // constants
198  $user = $request->getUser();
200  $request, $submission,
201  $submissionFile->getRevision()>1?SUBMISSION_LOG_FILE_REVISION_UPLOAD:SUBMISSION_LOG_FILE_UPLOAD,
202  $submissionFile->getRevision()>1?'submission.event.fileRevised':'submission.event.fileUploaded',
203  array('fileStage' => $submissionFile->getFileStage(), 'fileId' => $submissionFile->getFileId(), 'fileRevision' => $submissionFile->getRevision(), 'originalFileName' => $submissionFile->getOriginalFileName(), 'submissionId' => $submissionFile->getSubmissionId(), 'username' => $user->getUsername())
204  );
205 
206  return DAO::getDataChangedEvent();
207  } else {
208  return new JSONMessage(true, $metadataForm->fetch($request));
209  }
210  }
211 
217  abstract function removeFileIndex($submission, $submissionFile);
218 
225  function getFileManager($contextId, $submissionId) {
226  import('lib.pkp.classes.file.SubmissionFileManager');
227  return new SubmissionFileManager($contextId, $submissionId);
228  }
229 
238  abstract function logDeletionEvent($request, $submission, $submissionFile, $user);
239 
244  protected function getUpdateNotifications() {
245  return array(NOTIFICATION_TYPE_PENDING_EXTERNAL_REVISIONS);
246  }
247 }
248 
249 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
Base class to control (write) access to submissions and (read) access to submission files...
setupTemplate($request)
addRoleAssignment($roleIds, $operations)
Class to represent a JSON (Javascript Object Notation) message.
Base request handler application class.
Definition: Handler.inc.php:18
static logEvent($request, $submission, $eventType, $messageKey, $params=array())
authorize($request, &$args, $roleAssignments, $enforceRestrictedSite=true)
static getDataChangedEvent($elementId=null, $parentElementId=null, $content= '')
Definition: DAO.inc.php:633
& getAuthorizedContextObject($assocType)