Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
SettingsTabHandler.inc.php
1 <?php
2 
16 // Import the base Handler.
17 import('classes.handler.Handler');
18 import('lib.pkp.classes.core.JSONMessage');
19 
20 class SettingsTabHandler extends Handler {
21 
23  var $_currentTab;
24 
26  var $_pageTabs;
27 
28 
33  function SettingsTabHandler($role) {
34  parent::Handler();
35  $this->addRoleAssignment(
36  $role,
37  array('saveFormData', 'showTab')
38  );
39  }
40 
41  //
42  // Getters and Setters
43  //
48  function getCurrentTab() {
49  return $this->_currentTab;
50  }
51 
56  function setCurrentTab($currentTab) {
57  $this->_currentTab = $currentTab;
58  }
59 
64  function getPageTabs() {
65  return $this->_pageTabs;
66  }
67 
72  function setPageTabs($pageTabs) {
73  $this->_pageTabs = $pageTabs;
74  }
75 
76  //
77  // Extended methods from Handler
78  //
82  function initialize($request) {
83  $this->setCurrentTab($request->getUserVar('tab'));
84  }
85 
86  //
87  // Public handler methods
88  //
92  function showTab($args, $request) {
93  $this->setupTemplate($request);
94  if ($this->_isValidTab()) {
95  if ($this->_isTabTemplate()) {
96  $this->setupTemplate($request, true);
97  $templateMgr = TemplateManager::getManager($request);
98  if ($this->_isManagementHandler()) {
99  // Pass to template if we are in wizard mode.
100  $templateMgr->assign('wizardMode', $this->getWizardMode());
101  }
102  $templateMgr->assign('canEdit', true);
103  return $templateMgr->fetchJson($this->_getTabTemplate());
104  } else {
105  $tabForm = $this->getTabForm();
106  $tabForm->initData($request);
107  $json = new JSONMessage(true, $tabForm->fetch($request));
108  return $json->getString();
109  }
110  }
111  }
112 
117  function saveFormData($args, $request) {
118  $json = new JSONMessage();
119 
120  if ($this->_isValidTab()) {
121  $tabForm = $this->getTabForm();
122 
123  // Try to save the form data.
124  $tabForm->readInputData($request);
125  if($tabForm->validate()) {
126  $result = $tabForm->execute($request);
127  if ($result !== false) {
128  $notificationManager = new NotificationManager();
129  $user = $request->getUser();
130  $notificationManager->createTrivialNotification($user->getId());
131  }
132  } else {
133  $json->setStatus(false);
134  }
135  }
136 
137  return $json->getString();
138  }
139 
144  function getTabForm() {
145  $currentTab = $this->getCurrentTab();
146  $pageTabs = $this->getPageTabs();
147 
148  // Search for a form using the tab name.
149  import($pageTabs[$currentTab]);
150  $tabFormClassName = $this->_getFormClassName($pageTabs[$currentTab]);
151 
152  if ($this->_isManagementHandler()) {
153  $tabForm = new $tabFormClassName($this->getWizardMode());
154  } else {
155  $tabForm = new $tabFormClassName();
156  }
157 
158  assert(is_a($tabForm, 'Form'));
159 
160  return $tabForm;
161  }
162 
163 
164  //
165  // Private helper methods.
166  //
171  function _getTabTemplate() {
172  $currentTab = $this->getCurrentTab();
173  $pageTabs = $this->getPageTabs();
174 
175  return $pageTabs[$currentTab];
176  }
182  function _isValidTab() {
183  if (array_key_exists($this->getCurrentTab(), $this->getPageTabs())) {
184  return true;
185  } else {
186  assert(false);
187  return false;
188  }
189  }
190 
195  function _isTabTemplate() {
196  $currentTab = $this->getCurrentTab();
197  $pageTabs = $this->getPageTabs();
198 
199  return (strstr($pageTabs[$currentTab], '.tpl'));
200  }
207  function _getFormClassName($classPath) {
208  $needle = '.form.';
209  $formClassName = strstr($classPath, $needle);
210  $formClassName = trim(str_replace($needle, ' ', $formClassName));
211  return $formClassName;
212  }
218  function _isManagementHandler() {
219  return is_subclass_of($this, 'ManagerSettingsTabHandler');
220  }
221 }
222 
223 ?>
setupTemplate($request)
addRoleAssignment($roleIds, $operations)
Class to represent a JSON (Javascript Object Notation) message.
Handle AJAX operations for tabs on settings pages, under administration or management pages...
Base request handler application class.
Definition: Handler.inc.php:20
saveFormData($args, $request)