Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
CategoryCategoryGridHandler.inc.php
1 <?php
2 
16 // Import the base GridHandler.
17 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
18 import('lib.pkp.classes.controllers.grid.DataObjectGridCellProvider');
19 
20 // Import user group grid specific classes
21 import('controllers.grid.settings.category.CategoryGridCategoryRow');
22 
23 // Link action & modal classes
24 import('lib.pkp.classes.linkAction.request.AjaxModal');
25 
27  var $_pressId;
28 
33  parent::CategoryGridHandler();
34  $this->addRoleAssignment(
35  array(ROLE_ID_MANAGER),
36  array(
37  'fetchGrid',
38  'fetchCategory',
39  'fetchRow',
40  'addCategory',
41  'editCategory',
42  'updateCategory',
43  'deleteCategory',
44  'uploadImage'
45  )
46  );
47  }
48 
49  //
50  // Overridden methods from PKPHandler.
51  //
58  function authorize($request, &$args, $roleAssignments) {
59  import('lib.pkp.classes.security.authorization.PkpContextAccessPolicy');
60  $this->addPolicy(new PkpContextAccessPolicy($request, $roleAssignments));
61  return parent::authorize($request, $args, $roleAssignments);
62  }
63 
64 
70  function initialize($request) {
71 
72  parent::initialize($request);
73 
74  $press = $request->getPress();
75  $this->_pressId = $press->getId();
76 
77  AppLocale::requireComponents(LOCALE_COMPONENT_APP_MANAGER);
78 
79  // Set the grid title.
80  $this->setTitle('grid.category.categories');
81 
82  $this->setInstructions('manager.setup.categories.description');
83 
84  // Add grid-level actions.
85  $router = $request->getRouter();
86  $this->addAction(
87  new LinkAction(
88  'addCategory',
89  new AjaxModal(
90  $router->url($request, null, null, 'addCategory'),
91  __('grid.category.add'),
92  'modal_manage'
93  ),
94  __('grid.category.add'),
95  'add_category'
96  )
97  );
98 
99  // Add grid columns.
100  $cellProvider = new DataObjectGridCellProvider();
101  $cellProvider->setLocale(AppLocale::getLocale());
102 
103  $this->addColumn(
104  new GridColumn(
105  'title',
106  'grid.category.name',
107  null,
108  'controllers/grid/gridCell.tpl',
109  $cellProvider
110  )
111  );
112  }
113 
117  function loadData($request, $filter) {
118  // For top-level rows, only list categories without parents.
119  $categoryDao = DAORegistry::getDAO('CategoryDAO');
120  $categoriesIterator = $categoryDao->getByParentId(null, $this->_getPressId());
121  return $categoriesIterator->toAssociativeArray();
122  }
123 
128  return 'parentCategoryId';
129  }
130 
135  function getRowInstance() {
136  import('controllers.grid.settings.category.CategoryGridRow');
137  return new CategoryGridRow();
138  }
139 
145  return new CategoryGridCategoryRow();
146  }
147 
151  function getCategoryData(&$category) {
152  $categoryId = $category->getId();
153  $categoryDao = DAORegistry::getDAO('CategoryDAO');
154  $categoriesIterator = $categoryDao->getByParentId($categoryId, $this->_getPressId());
155  $categories = $categoriesIterator->toAssociativeArray();
156  return $categories;
157  }
158 
164  function addCategory($args, $request) {
165  return $this->editCategory($args, $request);
166  }
167 
173  function editCategory($args, $request) {
174  $categoryForm = $this->_getCategoryForm($request);
175 
176  $categoryForm->initData();
177 
178  $json = new JSONMessage(true, $categoryForm->fetch($request));
179  return $json->getString();
180  }
181 
187  function updateCategory($args, $request) {
188  $categoryForm = $this->_getCategoryForm($request);
189 
190  $categoryForm->readInputData();
191  if($categoryForm->validate()) {
192  $category = $categoryForm->execute($request);
193  return DAO::getDataChangedEvent();
194  } else {
195  $json = new JSONMessage(true, $categoryForm->fetch($request));
196  return $json->getString();
197  }
198  }
199 
206  function deleteCategory($args, $request) {
207  // Identify the category to be deleted
208  $categoryDao = DAORegistry::getDAO('CategoryDAO');
209  $press = $request->getPress();
210  $category = $categoryDao->getById(
211  $request->getUserVar('categoryId'),
212  $press->getId()
213  );
214  $categoryId = $category->getId();
215 
216  // FIXME delete dependent objects?
217 
218  // Delete the category
219  $categoryDao->deleteObject($category);
220  return DAO::getDataChangedEvent($category->getId(), $category->getParentId());
221  }
222 
228  function uploadImage($args, $request) {
229  $user = $request->getUser();
230 
231  import('lib.pkp.classes.file.TemporaryFileManager');
232  $temporaryFileManager = new TemporaryFileManager();
233  $temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
234  if ($temporaryFile) {
235  $json = new JSONMessage(true);
236  $json->setAdditionalAttributes(array(
237  'temporaryFileId' => $temporaryFile->getId()
238  ));
239  } else {
240  $json = new JSONMessage(false, __('common.uploadFailed'));
241  }
242 
243  return $json->getString();
244  }
245 
246  //
247  // Private helper methods.
248  //
254  function _getCategoryForm($request) {
255  // Get the category ID.
256  $categoryId = (int) $request->getUserVar('categoryId');
257 
258  // Instantiate the files form.
259  import('controllers.grid.settings.category.form.CategoryForm');
260  $pressId = $this->_getPressId();
261  return new CategoryForm($pressId, $categoryId);
262  }
263 
268  function _getPressId() {
269  return $this->_pressId;
270  }
271 }
272 
273 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
static requireComponents()
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
Class to control access to PKP applications&#39; setup components.
addRoleAssignment($roleIds, $operations)
A modal that retrieves its content from via AJAX.
Class to represent a JSON (Javascript Object Notation) message.
static getLocale()
static getDataChangedEvent($elementId=null, $parentElementId=null)
Definition: DAO.inc.php:606
Class defining basic operations for handling HTML grids with categories.
Handle operations for category management operations.
Category grid row definition.
authorize($request, &$args, $roleAssignments)
Base class for a cell provider that can retrieve simple labels from DataObjects. If you need more com...
Form to add/edit category.
setInstructions($instructions)
Category grid category row definition.
Represents a column within a grid. It is used to configure the way cells within a column are displaye...
Base class defining an action that can be performed by the user in the user interface.