Open Journal Systems  3.3.0
CustomBlockGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.GridHandler');
17 import('plugins.generic.customBlockManager.controllers.grid.CustomBlockGridRow');
18 
21  var $plugin;
22 
26  function __construct() {
27  parent::__construct();
28  $this->addRoleAssignment(
29  array(ROLE_ID_MANAGER, ROLE_ID_SITE_ADMIN),
30  array('fetchGrid', 'fetchRow', 'addCustomBlock', 'editCustomBlock', 'updateCustomBlock', 'deleteCustomBlock')
31  );
32  $this->plugin = PluginRegistry::getPlugin('generic', CUSTOMBLOCKMANAGER_PLUGIN_NAME);
33  }
34 
35 
36  //
37  // Overridden template methods
38  //
42  function authorize($request, &$args, $roleAssignments) {
43  if ($request->getContext()) {
44  import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
45  $this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
46  } else {
47  import('lib.pkp.classes.security.authorization.PKPSiteAccessPolicy');
48  $this->addPolicy(new PKPSiteAccessPolicy($request, null, $roleAssignments));
49  }
50  return parent::authorize($request, $args, $roleAssignments);
51  }
52 
56  function initialize($request, $args = null) {
57  parent::initialize($request, $args);
58  $context = $request->getContext();
59  $contextId = $context ? $context->getId() : 0;
60 
61  // Set the grid title.
62  $this->setTitle('plugins.generic.customBlockManager.customBlocks');
63  // Set the grid instructions.
64  $this->setEmptyRowText('plugins.generic.customBlockManager.noneCreated');
65 
66  // Get the blocks and add the data to the grid
67  $customBlockManagerPlugin = $this->plugin;
68  $blocks = $customBlockManagerPlugin->getSetting($contextId, 'blocks');
69  $gridData = array();
70  if (is_array($blocks)) foreach ($blocks as $block) {
71  $gridData[$block] = array(
72  'title' => $block
73  );
74  }
75  $this->setGridDataElements($gridData);
76 
77  // Add grid-level actions
78  $router = $request->getRouter();
79  import('lib.pkp.classes.linkAction.request.AjaxModal');
80  $this->addAction(
81  new LinkAction(
82  'addCustomBlock',
83  new AjaxModal(
84  $router->url($request, null, null, 'addCustomBlock'),
85  __('plugins.generic.customBlockManager.addBlock'),
86  'modal_add_item'
87  ),
88  __('plugins.generic.customBlockManager.addBlock'),
89  'add_item'
90  )
91  );
92 
93  // Columns
94  $this->addColumn(
95  new GridColumn(
96  'title',
97  'plugins.generic.customBlockManager.blockName',
98  null,
99  'controllers/grid/gridCell.tpl'
100  )
101  );
102  }
103 
104  //
105  // Overridden methods from GridHandler
106  //
110  function getRowInstance() {
111  return new CustomBlockGridRow();
112  }
113 
114  //
115  // Public Grid Actions
116  //
122  function addCustomBlock($args, $request) {
123  // Calling editCustomBlock with an empty ID will add
124  // a new custom block.
125  return $this->editCustomBlock($args, $request);
126  }
127 
134  function editCustomBlock($args, $request) {
135  $blockName = $request->getUserVar('blockName');
136  $context = $request->getContext();
137  $contextId = $context ? $context->getId() : 0;
138  $this->setupTemplate($request);
139 
140  $customBlockPlugin = null;
141  // If this is the edit of the existing custom block plugin,
142  if ($blockName) {
143  // Create the custom block plugin
144  import('plugins.generic.customBlockManager.CustomBlockPlugin');
145  $customBlockPlugin = new CustomBlockPlugin($blockName, CUSTOMBLOCKMANAGER_PLUGIN_NAME);
146  }
147 
148  // Create and present the edit form
149  import('plugins.generic.customBlockManager.controllers.grid.form.CustomBlockForm');
150  $customBlockManagerPlugin = $this->plugin;
151  $template = $customBlockManagerPlugin->getTemplateResource('editCustomBlockForm.tpl');
152  $customBlockForm = new CustomBlockForm($template, $contextId, $customBlockPlugin);
153  $customBlockForm->initData();
154  return new JSONMessage(true, $customBlockForm->fetch($request));
155  }
156 
163  function updateCustomBlock($args, $request) {
164  $pluginName = $request->getUserVar('existingBlockName');
165  $context = $request->getContext();
166  $contextId = $context ? $context->getId() : 0;
167  $this->setupTemplate($request);
168 
169  $customBlockPlugin = null;
170  // If this was the edit of the existing custom block plugin
171  if ($pluginName) {
172  // Create the custom block plugin
173  import('plugins.generic.customBlockManager.CustomBlockPlugin');
174  $customBlockPlugin = new CustomBlockPlugin($pluginName, CUSTOMBLOCKMANAGER_PLUGIN_NAME);
175  }
176 
177  // Create and populate the form
178  import('plugins.generic.customBlockManager.controllers.grid.form.CustomBlockForm');
179  $customBlockManagerPlugin = $this->plugin;
180  $template = $customBlockManagerPlugin->getTemplateResource('editCustomBlockForm.tpl');
181  $customBlockForm = new CustomBlockForm($template, $contextId, $customBlockPlugin);
182  $customBlockForm->readInputData();
183 
184  // Check the results
185  if ($customBlockForm->validate()) {
186  // Save the results
187  $customBlockForm->execute();
188  return DAO::getDataChangedEvent();
189  } else {
190  // Present any errors
191  return new JSONMessage(true, $customBlockForm->fetch($request));
192  }
193  }
194 
201  function deleteCustomBlock($args, $request) {
202  $blockName = $request->getUserVar('blockName');
203  $context = $request->getContext();
204  $contextId = $context ? $context->getId() : 0;
205 
206  // Delete all the entries for this block plugin
207  $pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
208  $pluginSettingsDao->deleteSetting($contextId, $blockName, 'enabled');
209  $pluginSettingsDao->deleteSetting($contextId, $blockName, 'context');
210  $pluginSettingsDao->deleteSetting($contextId, $blockName, 'seq');
211  $pluginSettingsDao->deleteSetting($contextId, $blockName, 'blockContent');
212 
213  // Remove this block plugin from the list of the custom block plugins
214  $customBlockManagerPlugin = $this->plugin;
215  $blocks = $customBlockManagerPlugin->getSetting($contextId, 'blocks');
216  $newBlocks = array_diff($blocks, array($blockName));
217  ksort($newBlocks);
218  $customBlockManagerPlugin->updateSetting($contextId, 'blocks', $newBlocks);
219  return DAO::getDataChangedEvent();
220  }
221 }
222 
PKPHandler\addRoleAssignment
addRoleAssignment($roleIds, $operations)
Definition: PKPHandler.inc.php:213
GridHandler\setEmptyRowText
setEmptyRowText($emptyRowText)
Definition: GridHandler.inc.php:231
GridColumn
The GridColumn class represents a column within a grid. It is used to format the data presented in a ...
Definition: GridColumn.inc.php:27
CustomBlockGridHandler\getRowInstance
getRowInstance()
Definition: CustomBlockGridHandler.inc.php:113
ContextAccessPolicy
Class to control access to PKP applications' setup components.
Definition: ContextAccessPolicy.inc.php:17
CustomBlockGridHandler\deleteCustomBlock
deleteCustomBlock($args, $request)
Definition: CustomBlockGridHandler.inc.php:204
CustomBlockGridHandler\addCustomBlock
addCustomBlock($args, $request)
Definition: CustomBlockGridHandler.inc.php:125
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
CustomBlockGridHandler\__construct
__construct()
Definition: CustomBlockGridHandler.inc.php:29
CustomBlockGridHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: CustomBlockGridHandler.inc.php:45
CustomBlockGridHandler\$plugin
$plugin
Definition: CustomBlockGridHandler.inc.php:24
GridHandler\addAction
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
Definition: GridHandler.inc.php:266
GridHandler\addColumn
addColumn($column)
Definition: GridHandler.inc.php:335
DAO\getDataChangedEvent
static getDataChangedEvent($elementId=null, $parentElementId=null, $content='')
Definition: DAO.inc.php:647
CustomBlockGridHandler\initialize
initialize($request, $args=null)
Definition: CustomBlockGridHandler.inc.php:59
GridHandler\setGridDataElements
setGridDataElements($data)
Definition: GridHandler.inc.php:379
JSONMessage
Class to represent a JSON (Javascript Object Notation) message.
Definition: JSONMessage.inc.php:18
AjaxModal
A modal that retrieves its content from via AJAX.
Definition: AjaxModal.inc.php:18
LinkAction
Base class defining an action that can be performed by the user in the user interface.
Definition: LinkAction.inc.php:22
CustomBlockPlugin
Definition: CustomBlockPlugin.inc.php:19
GridHandler\setTitle
setTitle($title)
Definition: GridHandler.inc.php:215
CustomBlockGridHandler\editCustomBlock
editCustomBlock($args, $request)
Definition: CustomBlockGridHandler.inc.php:137
PKPHandler\setupTemplate
setupTemplate($request)
Definition: PKPHandler.inc.php:466
PluginRegistry\getPlugin
static getPlugin($category, $name)
Definition: PluginRegistry.inc.php:85
GridHandler
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
Definition: GridHandler.inc.php:58
CustomBlockGridRow
Handle custom blocks grid row requests.
Definition: CustomBlockGridRow.inc.php:18
CustomBlockGridHandler\updateCustomBlock
updateCustomBlock($args, $request)
Definition: CustomBlockGridHandler.inc.php:166
CustomBlockForm
Definition: CustomBlockForm.inc.php:19
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
PKPSiteAccessPolicy
Class to that makes sure that a user is logged in.
Definition: PKPSiteAccessPolicy.inc.php:20
CustomBlockGridHandler
Handle custom block manager grid requests.
Definition: CustomBlockGridHandler.inc.php:19