Open Monograph Press  3.3.0
ListbuilderHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.GridHandler');
17 import('lib.pkp.classes.controllers.listbuilder.ListbuilderGridRow');
18 import('lib.pkp.classes.controllers.listbuilder.ListbuilderGridColumn');
19 import('lib.pkp.classes.controllers.listbuilder.MultilingualListbuilderGridColumn');
20 
21 /* Listbuilder source types: text-based, pulldown, ... */
22 define('LISTBUILDER_SOURCE_TYPE_TEXT', 0);
23 define('LISTBUILDER_SOURCE_TYPE_SELECT', 1);
24 
25 /* Listbuilder save types */
26 define('LISTBUILDER_SAVE_TYPE_EXTERNAL', 0); // Outside the listbuilder handler
27 define('LISTBUILDER_SAVE_TYPE_INTERNAL', 1); // Using ListbuilderHandler::save
28 
29 /* String to identify optgroup in the returning options data. If you want to use
30  * optgroup in listbuilder select, return the options data in a multidimensional array
31  * array[columnIndex][optgroupId][selectItemId] and also with
32  * array[columnIndex][LISTBUILDER_OPTGROUP_LABEL][optgroupId] */
33 define('LISTBUILDER_OPTGROUP_LABEL', 'optGroupLabel');
34 
37  var $_sourceType;
38 
40  var $_saveType = LISTBUILDER_SAVE_TYPE_INTERNAL;
41 
43  var $_saveFieldName = null;
44 
48  function initialize($request, $args = null) {
49  parent::initialize($request, $args);
50 
51  if ($this->canAddItems()) {
52  import('lib.pkp.classes.linkAction.request.NullAction');
53  $this->addAction($this->getAddItemLinkAction(new NullAction()));
54  }
55  }
56 
57 
58  //
59  // Getters and Setters
60  //
65  function getTemplate() {
66  if (is_null($this->_template)) {
67  $this->setTemplate('controllers/listbuilder/listbuilder.tpl');
68  }
69 
70  return $this->_template;
71  }
72 
77  function setSourceType($sourceType) {
78  $this->_sourceType = $sourceType;
79  }
80 
85  function getSourceType() {
87  }
88 
93  function setSaveType($saveType) {
94  $this->_saveType = $saveType;
95  }
96 
101  function getSaveType() {
103  }
104 
109  function setSaveFieldName($fieldName) {
110  $this->_saveFieldName = $fieldName;
111  }
112 
118  function getSaveFieldName() {
119  assert(isset($this->_saveFieldName));
120  return $this->_saveFieldName;
121  }
122 
128  function getAddItemLinkAction($actionRequest) {
129  return new LinkAction(
130  'addItem',
131  $actionRequest,
132  __('grid.action.addItem'),
133  'add_item'
134  );
135  }
136 
144  function getNewRowId($request) {
145  return $request->getUserVar('newRowId');
146  }
147 
154  function deleteEntry($request, $rowId) {
155  fatalError('ABSTRACT METHOD');
156  }
157 
165  function updateEntry($request, $rowId, $newRowId) {
166  // This may well be overridden by a subclass to modify
167  // an existing entry, e.g. to maintain referential integrity.
168  // If not, we can simply delete and insert.
169  if (!$this->deleteEntry($request, $rowId)) return false;
170  return $this->insertEntry($request, $newRowId);
171  }
172 
179  function insertEntry($request, $newRowId) {
180  fatalError('ABSTRACT METHOD');
181  }
182 
193  function getOptions($request) {
194  return array();
195  }
196 
197  //
198  // Publicly (remotely) available listbuilder functions
199  //
205  function fetch($args, $request) {
206  $templateMgr = TemplateManager::getManager($request);
207  $options = $this->getOptions($request);
208  $availableOptions = false;
209  if (is_array($options) && !empty($options)) {
210  $firstColumnOptions = current($options);
211  $optionsCount = count($firstColumnOptions);
212  if (is_array(current($firstColumnOptions))) { // Options with opt group, count only the selectable options.
213  unset($firstColumnOptions[LISTBUILDER_OPTGROUP_LABEL]);
214  $optionsCount--;
215  $optionsCount = count($firstColumnOptions, COUNT_RECURSIVE) - $optionsCount;
216  }
217 
218  $listElements = $this->getGridDataElements($request);
219  if (count($listElements) < $optionsCount) {
220  $availableOptions = true;
221  }
222  }
223 
224  $templateMgr->assign('availableOptions', $availableOptions);
225 
226  return $this->fetchGrid($args, $request);
227  }
228 
236  static function unpack($request, $data, $deletionCallback, $insertionCallback, $updateCallback) {
237  $data = json_decode($data);
238  $status = true;
239 
240  // Handle deletions
241  if (isset($data->deletions) && $data->deletions !== '') {
242  foreach (explode(' ', trim($data->deletions)) as $rowId) {
243  if (!call_user_func($deletionCallback, $request, $rowId, $data->numberOfRows)) {
244  $status = false;
245  }
246  }
247  }
248 
249  // Handle changes and insertions
250  if (isset($data->changes)) foreach ($data->changes as $entry) {
251  // Get the row ID, if any, from submitted data
252  if (isset($entry->rowId)) {
253  $rowId = $entry->rowId;
254  unset($entry->rowId);
255  } else {
256  $rowId = null;
257  }
258 
259  // $entry should now contain only submitted modified or new rows.
260  // Go through each and unpack the data in prep for application.
261  $changes = array();
262  foreach ($entry as $key => $value) {
263  // Match the column name and localization data, if any.
264  if (!preg_match('/^newRowId\[([a-zA-Z]+)\](\[([a-z][a-z]_[A-Z][A-Z](@([A-Za-z0-9]{5,8}|\d[A-Za-z0-9]{3}))?)\])?$/', $key, $matches)) assert(false);
265 
266  // Get the column name
267  $column = $matches[1];
268 
269  // If this is a multilingual input, fetch $locale; otherwise null
270  $locale = isset($matches[3])?$matches[3]:null;
271 
272  if ($locale) $changes[$column][$locale] = $value;
273  else $changes[$column] = $value;
274  }
275 
276  // $changes should now contain e.g.:
277  // array ('localizedColumnName' => array('en_US' => 'englishValue'),
278  // 'nonLocalizedColumnName' => 'someNonLocalizedValue');
279  if (is_null($rowId)) {
280  if (!call_user_func($insertionCallback, $request, $changes)) $status = false;
281  } else {
282  if (!call_user_func($updateCallback, $request, $rowId, $changes)) $status = false;
283  }
284  }
285  return $status;
286  }
287 
293  function save($args, $request) {
294  // The ListbuilderHandler will post a list of changed
295  // data in the "data" post var. Need to go through it
296  // and reconcile the data against this list, adding/
297  // updating/deleting as needed.
298  $data = $request->getUserVar('data');
299  self::unpack(
300  $request, $data,
301  array($this, 'deleteEntry'),
302  array($this, 'insertEntry'),
303  array($this, 'updateEntry')
304  );
305  }
306 
307 
314  function fetchOptions($args, $request) {
315  $options = $this->getOptions($request);
316  return new JSONMessage(true, $options);
317  }
318 
319 
325  public function canAddItems() {
326  return true;
327  }
328 
329  //
330  // Overridden methods from GridHandler
331  //
336  protected function getRowInstance() {
337  // Return a citation row
338  return new ListbuilderGridRow();
339  }
340 }
341 
342 
ListbuilderHandler\deleteEntry
deleteEntry($request, $rowId)
Definition: ListbuilderHandler.inc.php:163
ListbuilderHandler
Class defining basic operations for handling Listbuilder UI elements.
Definition: ListbuilderHandler.inc.php:35
ListbuilderHandler\getNewRowId
getNewRowId($request)
Definition: ListbuilderHandler.inc.php:153
GridHandler\fetchGrid
fetchGrid($args, $request)
Definition: GridHandler.inc.php:661
ListbuilderGridRow
Handle list builder row requests.
Definition: ListbuilderGridRow.inc.php:18
GridHandler\setTemplate
setTemplate($template)
Definition: GridHandler.inc.php:411
ListbuilderHandler\unpack
static unpack($request, $data, $deletionCallback, $insertionCallback, $updateCallback)
Definition: ListbuilderHandler.inc.php:245
ListbuilderHandler\fetchOptions
fetchOptions($args, $request)
Definition: ListbuilderHandler.inc.php:323
ListbuilderHandler\initialize
initialize($request, $args=null)
Definition: ListbuilderHandler.inc.php:57
ListbuilderHandler\getSaveType
getSaveType()
Definition: ListbuilderHandler.inc.php:110
ListbuilderHandler\getTemplate
getTemplate()
Definition: ListbuilderHandler.inc.php:74
ListbuilderHandler\save
save($args, $request)
Definition: ListbuilderHandler.inc.php:302
GridHandler\getGridDataElements
& getGridDataElements($request)
Definition: GridHandler.inc.php:345
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GuzzleHttp\json_decode
json_decode($json, $assoc=false, $depth=512, $options=0)
Definition: guzzlehttp/guzzle/src/functions.php:301
NullAction
This action does nothing.
Definition: NullAction.inc.php:18
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
GridHandler\$_template
$_template
Definition: GridHandler.inc.php:116
LinkAction
Base class defining an action that can be performed by the user in the user interface.
Definition: LinkAction.inc.php:22
ListbuilderHandler\getAddItemLinkAction
getAddItemLinkAction($actionRequest)
Definition: ListbuilderHandler.inc.php:137
ListbuilderHandler\setSourceType
setSourceType($sourceType)
Definition: ListbuilderHandler.inc.php:86
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
ListbuilderHandler\getOptions
getOptions($request)
Definition: ListbuilderHandler.inc.php:202
ListbuilderHandler\fetch
fetch($args, $request)
Definition: ListbuilderHandler.inc.php:214
ListbuilderHandler\getRowInstance
getRowInstance()
Definition: ListbuilderHandler.inc.php:345
ListbuilderHandler\getSaveFieldName
getSaveFieldName()
Definition: ListbuilderHandler.inc.php:127
ListbuilderHandler\canAddItems
canAddItems()
Definition: ListbuilderHandler.inc.php:334
ListbuilderHandler\$_sourceType
$_sourceType
Definition: ListbuilderHandler.inc.php:40
GridHandler
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
Definition: GridHandler.inc.php:58
ListbuilderHandler\insertEntry
insertEntry($request, $newRowId)
Definition: ListbuilderHandler.inc.php:188
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
ListbuilderHandler\$_saveFieldName
$_saveFieldName
Definition: ListbuilderHandler.inc.php:52
ListbuilderHandler\setSaveType
setSaveType($saveType)
Definition: ListbuilderHandler.inc.php:102
ListbuilderHandler\updateEntry
updateEntry($request, $rowId, $newRowId)
Definition: ListbuilderHandler.inc.php:174
ListbuilderHandler\setSaveFieldName
setSaveFieldName($fieldName)
Definition: ListbuilderHandler.inc.php:118
ListbuilderHandler\$_saveType
$_saveType
Definition: ListbuilderHandler.inc.php:46
ListbuilderHandler\getSourceType
getSourceType()
Definition: ListbuilderHandler.inc.php:94