Open Journal Systems  3.0.0
 All Data Structures 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  //
128  protected function getCategoryRowInstance() {
129  return new LibraryFileGridCategoryRow($this->getContext());
130  }
131 
135  protected 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  protected 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  null,
169  );
170  }
171 
172  //
173  // Public File Grid Actions
174  //
181  function addFile($args, $request) {
182  $this->initialize($request);
183  $router = $request->getRouter();
184  $context = $request->getContext();
185 
186  $fileForm = $this->_getNewFileForm($context);
187  $fileForm->initData();
188 
189  return new JSONMessage(true, $fileForm->fetch($request));
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  return new JSONMessage(false);
214  }
215 
222  function editFile($args, $request) {
223  $this->initialize($request);
224  assert(isset($args['fileId']));
225  $fileId = (int) $args['fileId'];
226 
227  $router = $request->getRouter();
228  $context = $request->getContext();
229 
230  $fileForm = $this->_getEditFileForm($context, $fileId);
231  $fileForm->initData();
232 
233  return new JSONMessage(true, $fileForm->fetch($request));
234  }
235 
242  function updateFile($args, $request) {
243  assert(isset($args['fileId']));
244  $fileId = (int) $args['fileId'];
245 
246  $router = $request->getRouter();
247  $context = $request->getContext();
248 
249  $fileForm = $this->_getEditFileForm($context, $fileId);
250  $fileForm->readInputData();
251 
252  if ($fileForm->validate()) {
253  $fileForm->execute();
254 
255  // Let the calling grid reload itself
256  return DAO::getDataChangedEvent();
257  }
258 
259  return new JSONMessage(false);
260  }
261 
268  function deleteFile($args, $request) {
269  $fileId = isset($args['fileId']) ? $args['fileId'] : null;
270  $router = $request->getRouter();
271  $context = $router->getContext($request);
272 
273  if ($fileId) {
274  $libraryFileManager = new LibraryFileManager($context->getId());
275  $libraryFileManager->deleteFile($fileId);
276 
277  return DAO::getDataChangedEvent();
278  }
279 
280  return new JSONMessage(false);
281  }
282 
289  function uploadFile($args, $request) {
290  $router = $request->getRouter();
291  $context = $request->getContext();
292  $user = $request->getUser();
293 
294  import('lib.pkp.classes.file.TemporaryFileManager');
295  $temporaryFileManager = new TemporaryFileManager();
296  $temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
297  if ($temporaryFile) {
298  $json = new JSONMessage(true);
299  $json->setAdditionalAttributes(array(
300  'temporaryFileId' => $temporaryFile->getId()
301  ));
302  return $json;
303  } else {
304  return new JSONMessage(false, __('common.uploadFailed'));
305  }
306  }
307 
313  function _getNewFileForm($context){
314  assert(false);
315  }
316 
323  function _getEditFileForm($context, $fileId){
324  assert(false);
325  }
326 
331  function getActionArgs() {
332  return array();
333  }
334 }
335 
336 ?>
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:633
Base class defining an action that can be performed by the user in the user interface.