Open Journal Systems  3.0.0
 All Data Structures Namespaces Functions Variables Groups Pages
PreparedEmailsGridHandler.inc.php
1 <?php
2 
16 // Import grid base classes
17 import('lib.pkp.classes.controllers.grid.GridHandler');
18 
19 // Import classes specific to this grid handler
20 import('lib.pkp.controllers.grid.settings.preparedEmails.PreparedEmailsGridRow');
21 
22 class PreparedEmailsGridHandler extends GridHandler {
26  function PreparedEmailsGridHandler() {
27  $this->addRoleAssignment(
28  array(ROLE_ID_MANAGER),
29  array(
30  'fetchRow', 'fetchGrid', 'addPreparedEmail', 'editPreparedEmail',
31  'updatePreparedEmail', 'resetEmail', 'resetAllEmails',
32  'disableEmail', 'enableEmail', 'deleteCustomEmail'
33  )
34  );
35  parent::GridHandler();
36  }
37 
41  function authorize($request, $args, $roleAssignments) {
42  import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
43  $this->addPolicy(new ContextAccessPolicy($request, $roleAssignments));
44  return parent::authorize($request, $args, $roleAssignments);
45  }
46 
50  function initialize($request, $args = null) {
51  parent::initialize($request, $args);
52  // Basic grid configuration
53  $this->setId('preparedEmailsGrid');
54 
55  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_PKP_USER);
56 
57  // Set the grid title.
58  $this->setTitle('grid.preparedEmails.title');
59 
60  // Grid actions
61  import('lib.pkp.controllers.grid.settings.preparedEmails.linkAction.EditEmailLinkAction');
62  $addEmailLinkAction = new EditEmailLinkAction($request);
63  $this->addAction($addEmailLinkAction);
64 
65  import('lib.pkp.classes.linkAction.LinkAction');
66  import('lib.pkp.classes.linkAction.request.RemoteActionConfirmationModal');
67  $router = $request->getRouter();
68  $this->addAction(
69  new LinkAction(
70  'resetAll',
72  __('manager.emails.resetAll.message'), null,
73  $router->url($request, null,
74  'grid.settings.preparedEmails.PreparedEmailsGridHandler', 'resetAllEmails')
75  ),
76  __('manager.emails.resetAll'),
77  'reset_default'
78  )
79  );
80 
81 
82  // Columns
83  import('lib.pkp.controllers.grid.settings.preparedEmails.PreparedEmailsGridCellProvider');
84  $cellProvider = new PreparedEmailsGridCellProvider();
85  $this->addColumn(new GridColumn('name', 'common.name', null, null, $cellProvider, array('width' => 40)));
86  $this->addColumn(new GridColumn('sender', 'email.sender', null, null, $cellProvider, array('width' => 10)));
87  $this->addColumn(new GridColumn('recipient', 'email.recipient', null, null, $cellProvider));
88  $this->addColumn(new GridColumn('subject', 'common.subject', null, null, $cellProvider));
89  $this->addColumn(new GridColumn('enabled', 'common.enabled', null, 'controllers/grid/common/cell/selectStatusCell.tpl', $cellProvider, array('width' => 5)));
90  }
91 
95  function initFeatures($request, $args) {
96  import('lib.pkp.classes.controllers.grid.feature.PagingFeature');
97  return array(new PagingFeature());
98  }
99 
103  protected function loadData($request, $filter) {
104  // Elements to be displayed in the grid
105  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
106  $context = $request->getContext();
107  $emailTemplates = $emailTemplateDao->getEmailTemplates(AppLocale::getLocale(), $context->getId());
108  foreach ($emailTemplates as $emailTemplate) {
109  $rowData[$emailTemplate->getEmailKey()] = $emailTemplate;
110  }
111  return $rowData;
112  }
113 
114 
115  //
116  // Overridden methods from GridHandler
117  //
122  protected function getRowInstance() {
123  return new PreparedEmailsGridRow();
124  }
125 
126 
127  //
128  // Public handler methods
129  //
136  function addPreparedEmail($args, $request) {
137  return $this->editPreparedEmail($args, $request);
138  }
139 
147  function editPreparedEmail($args, $request) {
148  $context = $request->getContext();
149  $emailKey = $request->getUserVar('emailKey');
150 
151  import('lib.pkp.controllers.grid.settings.preparedEmails.form.PreparedEmailForm');
152  $preparedEmailForm = new PreparedEmailForm($emailKey, $context);
153  $preparedEmailForm->initData($request);
154 
155  return new JSONMessage(true, $preparedEmailForm->fetch($request));
156  }
157 
164  function updatePreparedEmail($args, $request) {
165  $context = $request->getContext();
166  $emailKey = $request->getUserVar('emailKey');
167 
168  import('lib.pkp.controllers.grid.settings.preparedEmails.form.PreparedEmailForm');
169  $preparedEmailForm = new PreparedEmailForm($emailKey, $context);
170  $preparedEmailForm->readInputData();
171 
172  if ($preparedEmailForm->validate()) {
173  $preparedEmailForm->execute();
174 
175  // Create notification.
176  $notificationMgr = new NotificationManager();
177  $user = $request->getUser();
178  $notificationMgr->createTrivialNotification($user->getId());
179 
180  // Let the calling grid reload itself
181  return DAO::getDataChangedEvent($emailKey);
182  } else {
183  return new JSONMessage(false);
184  }
185  }
186 
193  function resetEmail($args, $request) {
194  $emailKey = $request->getUserVar('emailKey');
195  assert(is_string($emailKey));
196 
197  $context = $request->getContext();
198 
199  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
200  if ($emailTemplateDao->templateExistsByKey($emailKey, $context->getId())) {
201  $emailTemplateDao->deleteEmailTemplateByKey($emailKey, $context->getId());
202  return DAO::getDataChangedEvent($emailKey);
203  }
204  return new JSONMessage(false);
205  }
206 
212  function resetAllEmails($args, $request) {
213  $context = $request->getContext();
214  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
215  $emailTemplateDao->deleteEmailTemplatesByContext($context->getId());
216  return DAO::getDataChangedEvent();
217  }
218 
225  function disableEmail($args, $request) {
226  $emailKey = $request->getUserVar('emailKey');
227  assert(is_string($emailKey));
228 
229  $context = $request->getContext();
230 
231  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
232  $emailTemplate = $emailTemplateDao->getBaseEmailTemplate($emailKey, $context->getId());
233 
234  if (isset($emailTemplate)) {
235  if ($emailTemplate->getCanDisable()) {
236  $emailTemplate->setEnabled(0);
237 
238  if ($emailTemplate->getAssocId() == null) {
239  $emailTemplate->setAssocId($context->getId());
240  $emailTemplate->setAssocType(ASSOC_TYPE_JOURNAL);
241  }
242 
243  if ($emailTemplate->getEmailId() != null) {
244  $emailTemplateDao->updateBaseEmailTemplate($emailTemplate);
245  } else {
246  $emailTemplateDao->insertBaseEmailTemplate($emailTemplate);
247  }
248 
249  return DAO::getDataChangedEvent($emailKey);
250  }
251  }
252  return new JSONMessage(false);
253  }
254 
255 
262  function enableEmail($args, $request) {
263  $emailKey = $request->getUserVar('emailKey');
264  assert(is_string($emailKey));
265 
266  $context = $request->getContext();
267 
268  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
269  $emailTemplate = $emailTemplateDao->getBaseEmailTemplate($emailKey, $context->getId());
270 
271  if (isset($emailTemplate)) {
272  if ($emailTemplate->getCanDisable()) {
273  $emailTemplate->setEnabled(1);
274 
275  if ($emailTemplate->getEmailId() != null) {
276  $emailTemplateDao->updateBaseEmailTemplate($emailTemplate);
277  } else {
278  $emailTemplateDao->insertBaseEmailTemplate($emailTemplate);
279  }
280 
281  return DAO::getDataChangedEvent($emailKey);
282  }
283  }
284  return new JSONMessage(false);
285  }
286 
293  function deleteCustomEmail($args, $request) {
294  $emailKey = $request->getUserVar('emailKey');
295  $context = $request->getContext();
296 
297  $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); /* @var $emailTemplateDao EmailTemplateDAO */
298  if ($emailTemplateDao->customTemplateExistsByKey($emailKey, $context->getId())) {
299  $emailTemplateDao->deleteEmailTemplateByKey($emailKey, $context->getId());
300  return DAO::getDataChangedEvent($emailKey);
301  }
302  return new JSONMessage(false);
303  }
304 }
305 
306 ?>
static & getDAO($name, $dbconn=null)
addPolicy($authorizationPolicy, $addToTop=false)
static requireComponents()
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
This class defines basic operations for handling HTML grids. Grids are used to implement a standardiz...
authorize($request, &$args, $roleAssignments, $enforceRestrictedSite=true)
loadData($request, $filter)
Form for creating and modifying prepared emails.
addRoleAssignment($roleIds, $operations)
Class to represent a JSON (Javascript Object Notation) message.
static getLocale()
Add/Edit a prepared email.
Handle PreparedEmails grid row requests.
Class to control access to PKP applications&#39; setup components.
initialize($request, $args=null)
Class defining a simple confirmation modal with a remote action and ok/cancel buttons.
initFeatures($request, &$args)
Add paging functionality to grids.
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.