Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
LibraryFileGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
17 import('lib.pkp.controllers.grid.files.LibraryFileGridRow');
18 import('lib.pkp.controllers.grid.files.LibraryFileGridCategoryRow');
19 import('classes.file.LibraryFileManager');
20 import('lib.pkp.classes.context.LibraryFile');
21 
22 // Link action & modal classes
23 import('lib.pkp.classes.linkAction.request.AjaxModal');
24 
27  var $_context;
28 
30  var $_canEdit;
31 
35  function LibraryFileGridHandler($dataProvider) {
36  parent::CategoryGridHandler($dataProvider);
37  $this->addRoleAssignment(
38  array(ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR, ROLE_ID_ASSISTANT, ROLE_ID_AUTHOR),
39  array(
40  'fetchGrid', 'fetchCategory', 'fetchRow', // Parent grid-level actions
41  )
42  );
43  }
44 
45 
46  //
47  // Getters/Setters
48  //
53  function getContext() {
54  return $this->_context;
55  }
56 
61  function canEdit() {
62  return $this->_canEdit;
63  }
64 
69  function setCanEdit($canEdit) {
70  $this->_canEdit = $canEdit;
71  }
72 
73  //
74  // Overridden template methods
75  //
76 
77  /*
78  * Configure the grid
79  * @param $request PKPRequest
80  */
81  function initialize($request) {
82  parent::initialize($request);
83 
84  $router = $request->getRouter();
85  $this->_context = $router->getContext($request);
86 
87  // Set name
88  $this->setTitle('manager.publication.library');
89 
91  LOCALE_COMPONENT_PKP_COMMON,
92  LOCALE_COMPONENT_APP_COMMON,
93  LOCALE_COMPONENT_PKP_SUBMISSION,
94  LOCALE_COMPONENT_APP_SUBMISSION,
95  LOCALE_COMPONENT_PKP_MANAGER,
96  LOCALE_COMPONENT_APP_MANAGER
97  );
98 
99  // Columns
100  // Basic grid row configuration
101  $this->addColumn($this->getFileNameColumn());
102 
103  $router = $request->getRouter();
104 
105  // Add grid-level actions
106  if ($this->canEdit()) {
107  $this->addAction(
108  new LinkAction(
109  'addFile',
110  new AjaxModal(
111  $router->url($request, null, null, 'addFile', null, $this->getActionArgs()),
112  __('grid.action.addFile'),
113  'modal_add_file'
114  ),
115  __('grid.action.addFile'),
116  'add'
117  )
118  );
119  }
120  }
121 
122  //
123  // Implement template methods from CategoryGridHandler
124  //
129  return new LibraryFileGridCategoryRow($this->getContext());
130  }
131 
135  function loadData($request, $filter) {
136 
137  $context = $this->getContext();
138  $libraryFileManager = new LibraryFileManager($context->getId());
139  $fileTypeKeys = $libraryFileManager->getTypeSuffixMap();
140  foreach (array_keys($fileTypeKeys) as $key) {
141  $data[$key] = $key;
142  }
143  return $data;
144  }
145 
146  //
147  // Overridden methods from GridHandler
148  //
153  function getRowInstance() {
154  return new LibraryFileGridRow($this->canEdit());
155  }
156 
161  function getFileNameColumn() {
162  import('lib.pkp.controllers.grid.files.LibraryFileGridCellProvider');
163  return new GridColumn(
164  'files',
165  'grid.libraryFiles.column.files',
166  null,
167  'controllers/grid/gridCell.tpl',
169  );
170  }
171 
172  //
173  // Public File Grid Actions
174  //
180  function addFile($args, $request) {
181  $this->initialize($request);
182  $router = $request->getRouter();
183  $context = $request->getContext();
184 
185  $fileForm = $this->_getNewFileForm($context);
186  $fileForm->initData();
187 
188  $json = new JSONMessage(true, $fileForm->fetch($request));
189  return $json->getString();
190  }
191 
198  function saveFile($args, $request) {
199  $router = $request->getRouter();
200  $context = $request->getContext();
201  $user = $request->getUser();
202 
203  $fileForm = $this->_getNewFileForm($context);
204  $fileForm->readInputData();
205 
206  if ($fileForm->validate()) {
207  $fileId = $fileForm->execute($user->getId());
208 
209  // Let the calling grid reload itself
210  return DAO::getDataChangedEvent();
211  }
212 
213  $json = new JSONMessage(false);
214  return $json->getString();
215  }
216 
223  function editFile($args, $request) {
224  $this->initialize($request);
225  assert(isset($args['fileId']));
226  $fileId = (int) $args['fileId'];
227 
228  $router = $request->getRouter();
229  $context = $request->getContext();
230 
231  $fileForm = $this->_getEditFileForm($context, $fileId);
232  $fileForm->initData();
233 
234  $json = new JSONMessage(true, $fileForm->fetch($request));
235  return $json->getString();
236  }
237 
244  function updateFile($args, $request) {
245  assert(isset($args['fileId']));
246  $fileId = (int) $args['fileId'];
247 
248  $router = $request->getRouter();
249  $context = $request->getContext();
250 
251  $fileForm = $this->_getEditFileForm($context, $fileId);
252  $fileForm->readInputData();
253 
254  if ($fileForm->validate()) {
255  $fileForm->execute();
256 
257  // Let the calling grid reload itself
258  return DAO::getDataChangedEvent();
259  }
260 
261  $json = new JSONMessage(false);
262  return $json->getString();
263  }
264 
271  function deleteFile($args, $request) {
272  $fileId = isset($args['fileId']) ? $args['fileId'] : null;
273  $router = $request->getRouter();
274  $context = $router->getContext($request);
275 
276  if ($fileId) {
277  $libraryFileManager = new LibraryFileManager($context->getId());
278  $libraryFileManager->deleteFile($fileId);
279 
280  return DAO::getDataChangedEvent();
281  }
282 
283  $json = new JSONMessage(false);
284  return $json->getString();
285  }
286 
293  function uploadFile($args, $request) {
294  $router = $request->getRouter();
295  $context = $request->getContext();
296  $user = $request->getUser();
297 
298  import('lib.pkp.classes.file.TemporaryFileManager');
299  $temporaryFileManager = new TemporaryFileManager();
300  $temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
301  if ($temporaryFile) {
302  $json = new JSONMessage(true);
303  $json->setAdditionalAttributes(array(
304  'temporaryFileId' => $temporaryFile->getId()
305  ));
306  } else {
307  $json = new JSONMessage(false, __('common.uploadFailed'));
308  }
309 
310  return $json->getString();
311  }
312 
318  function _getNewFileForm($context){
319  assert(false);
320  }
321 
328  function _getEditFileForm($context, $fileId){
329  assert(false);
330  }
331 
336  function getActionArgs() {
337  return array();
338  }
339 }
340 
341 ?>
Base class for handling library file grid requests.
static requireComponents()
Subclass for a LibraryFile grid column&#39;s cell provider.
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
Wrapper class for uploading files to a site/context&#39; library directory.
Handle library file grid row requests.
addRoleAssignment($roleIds, $operations)
A modal that retrieves its content from via AJAX.
Class to represent a JSON (Javascript Object Notation) message.
Class defining basic operations for handling HTML grids with categories.
Library file grid category row definition.
The GridColumn class represents a column within a grid. It is used to format the data presented in a ...
static getDataChangedEvent($elementId=null, $parentElementId=null, $content= '')
Definition: DAO.inc.php:632
Base class defining an action that can be performed by the user in the user interface.