Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
SiteSetupForm.inc.php
1 <?php
2 
18 import('lib.pkp.classes.admin.form.PKPSiteSettingsForm');
19 
20 class SiteSetupForm extends PKPSiteSettingsForm {
24  function SiteSetupForm() {
25  parent::Form('controllers/tab/settings/siteSetup/form/siteSetupForm.tpl');
26  $this->siteSettingsDao = DAORegistry::getDAO('SiteSettingsDAO');
27 
28  // Validation checks for this form
29  $this->addCheck(new FormValidatorLocale($this, 'title', 'required', 'admin.settings.form.titleRequired'));
30  $this->addCheck(new FormValidatorLocale($this, 'contactName', 'required', 'admin.settings.form.contactNameRequired'));
31  $this->addCheck(new FormValidatorLocaleEmail($this, 'contactEmail', 'required', 'admin.settings.form.contactEmailRequired'));
32  $this->addCheck(new FormValidatorCustom($this, 'minPasswordLength', 'required', 'admin.settings.form.minPasswordLengthRequired', create_function('$l', sprintf('return $l >= %d;', SITE_MIN_PASSWORD_LENGTH))));
33  $this->addCheck(new FormValidatorPost($this));
34 
35  AppLocale::requireComponents(LOCALE_COMPONENT_APP_COMMON);
36  }
37 
38  //
39  // Extended methods from Form.
40  //
46  function fetch($request, $params = null) {
47  $site = $request->getSite();
48  $publicFileManager = new PublicFileManager();
49  $contextDao = Application::getContextDAO();
50  $contexts = $contextDao->getNames();
51  $siteStyleFilename = $publicFileManager->getSiteFilesPath() . '/' . $site->getSiteStyleFilename();
52 
53  $cssSettingName = 'siteStyleSheet';
54  $imageSettingName = 'pageHeaderTitleImage';
55 
56  // Get link actions.
57  $uploadCssLinkAction = $this->_getFileUploadLinkAction($cssSettingName, 'css', $request);
58  $uploadImageLinkAction = $this->_getFileUploadLinkAction($imageSettingName, 'image', $request);
59 
60  // Get the files view.
61  $cssView = $this->renderFileView($cssSettingName, $request);
62  $imageView = $this->renderFileView($imageSettingName, $request);
63 
64  $templateMgr = TemplateManager::getManager($request);
65  $templateMgr->assign('locale', AppLocale::getLocale());
66  $templateMgr->assign('siteStyleFileExists', file_exists($siteStyleFilename));
67  $templateMgr->assign('uploadCssLinkAction', $uploadCssLinkAction);
68  $templateMgr->assign('uploadImageLinkAction', $uploadImageLinkAction);
69  $templateMgr->assign('cssView', $cssView);
70  $templateMgr->assign('imageView', $imageView);
71  $templateMgr->assign('redirectOptions', $contexts);
72  $templateMgr->assign('pageHeaderTitleImage', $site->getSetting($imageSettingName));
73 
74  $application = Application::getApplication();
75  $templateMgr->assign('availableMetricTypes', $application->getMetricTypes(true));
76 
77  $themePlugins = PluginRegistry::loadCategory('themes');
78  $themePluginOptions = array();
79  foreach ($themePlugins as $themePlugin) {
80  $themePluginOptions[basename($themePlugin->getPluginPath())] = $themePlugin->getDisplayName();
81  }
82  $templateMgr->assign('themePluginOptions', $themePluginOptions);
83 
84  return parent::fetch($request);
85  }
86 
87 
88  //
89  // Extend method from PKPSiteSettingsForm
90  //
95  function initData($request) {
96  $site = $request->getSite();
97  $publicFileManager = $publicFileManager = new PublicFileManager();
98  $siteStyleFilename = $publicFileManager->getSiteFilesPath() . '/' . $site->getSiteStyleFilename();
99 
100  // Get the files settings that can be uploaded within this form.
101 
102  // FIXME Change the way we get the style sheet setting when
103  // it's implemented in site settings table, like pageHeaderTitleImage.
104  $siteStyleSheet = null;
105  if (file_exists($siteStyleFilename)) {
106  $siteStyleSheet = array(
107  'name' => $site->getOriginalStyleFilename(),
108  'uploadName' => $site->getSiteStyleFilename(),
109  'dateUploaded' => filemtime($siteStyleFilename),
110  );
111  }
112 
113  $pageHeaderTitleImage = $site->getSetting('pageHeaderTitleImage');
114 
115  $this->setData('siteStyleSheet', $siteStyleSheet);
116  $this->setData('pageHeaderTitleImage', $pageHeaderTitleImage);
117  $this->setData('themePluginPath', $site->getSetting('themePluginPath'));
118  $this->setData('defaultMetricType', $site->getSetting('defaultMetricType'));
119 
120  parent::initData();
121  }
122 
126  function readInputData() {
127  $this->readUserVars(
128  array('pageHeaderTitleType', 'title', 'intro', 'about', 'redirect', 'contactName',
129  'contactEmail', 'minPasswordLength', 'themePluginPath', 'defaultMetricType',)
130  );
131  }
132 
136  function execute() {
137  $siteDao = DAORegistry::getDAO('SiteDAO');
138  $site = $siteDao->getSite();
139 
140  $site->setRedirect($this->getData('redirect'));
141  $site->setMinPasswordLength($this->getData('minPasswordLength'));
142 
143  $siteSettingsDao = $this->siteSettingsDao;
144  foreach ($this->getLocaleFieldNames() as $setting) {
145  $siteSettingsDao->updateSetting($setting, $this->getData($setting), null, true);
146  }
147 
148  $siteSettingsDao->updateSetting('defaultMetricType', $this->getData('defaultMetricType'));
149 
150  // Activate the selected theme plugin
151  $selectedThemePluginPath = $this->getData('themePluginPath');
152  $site->updateSetting('themePluginPath', $selectedThemePluginPath);
153  $themePlugins = PluginRegistry::loadCategory('themes');
154  $selectedThemePlugin = null;
155  foreach ($themePlugins as $themePlugin) {
156  if (basename($themePlugin->getPluginPath()) != $selectedThemePluginPath) {
157  // Flag other themes for deactivation to ensure
158  // they won't be included in a CSS recompile.
159  $themePlugin->setEnabled(false);
160  } else {
161  $selectedThemePlugin = $themePlugin;
162  }
163  }
164  if ($selectedThemePlugin) {
165  $themePlugin->setEnabled(true);
166  } else {
167  assert(false); // Couldn't identify the selected theme plugin
168  }
169 
170  $siteDao->updateObject($site);
171  return true;
172  }
173 
174  //
175  // Public methods.
176  //
184  function renderFileView($fileSettingName, $request) {
185  $file = $this->getData($fileSettingName);
186  $locale = AppLocale::getLocale();
187 
188  // Check if the file is localized.
189  if (!is_null($file) && key_exists($locale, $file)) {
190  // We use the current localized file value.
191  $file = $file[$locale];
192  }
193 
194  // Only render the file view if we have a file.
195  if (is_array($file)) {
196  $templateMgr = TemplateManager::getManager($request);
197  $deleteLinkAction = $this->_getDeleteFileLinkAction($fileSettingName, $request);
198 
199  // Get the right template to render the view.
200  if ($fileSettingName == 'pageHeaderTitleImage') {
201  $template = 'controllers/tab/settings/formImageView.tpl';
202 
203  // Get the common alternate text for the image.
204  $localeKey = 'admin.settings.homeHeaderImage.altText';
205  $commonAltText = __($localeKey);
206  $templateMgr->assign('commonAltText', $commonAltText);
207  } else {
208  $template = 'controllers/tab/settings/formFileView.tpl';
209  }
210 
211  $publicFileManager = $publicFileManager = new PublicFileManager();
212 
213  $templateMgr->assign('publicFilesDir', $request->getBasePath() . '/' . $publicFileManager->getSiteFilesPath());
214  $templateMgr->assign('file', $file);
215  $templateMgr->assign('deleteLinkAction', $deleteLinkAction);
216  $templateMgr->assign('fileSettingName', $fileSettingName);
217 
218  return $templateMgr->fetch($template);
219  } else {
220  return null;
221  }
222  }
223 
229  function deleteFile($fileSettingName, $request) {
230  $locale = AppLocale::getLocale();
231 
232  // Get the file.
233  $file = $this->getData($fileSettingName);
234 
235  // Check if the file is localized.
236  if (key_exists($locale, $file)) {
237  // We use the current localized file value.
238  $file = $file[$locale];
239  } else {
240  $locale = null;
241  }
242 
243  // Deletes the file and its settings.
244  import('classes.file.PublicFileManager');
245  $publicFileManager = new PublicFileManager();
246  if ($publicFileManager->removeSiteFile($file['uploadName'])) {
247  $settingsDao = DAORegistry::getDAO('SiteSettingsDAO');
248  $settingsDao->deleteSetting($fileSettingName, $locale);
249  return true;
250  } else {
251  return false;
252  }
253  }
254 
255 
256  //
257  // Private helper methods.
258  //
266  function _getFileUploadLinkAction($settingName, $fileType, $request) {
267  $router = $request->getRouter();
268  import('lib.pkp.classes.linkAction.request.AjaxModal');
269 
270  $ajaxModal = new AjaxModal(
271  $router->url(
272  $request, null, null, 'showFileUploadForm', null, array(
273  'fileSettingName' => $settingName,
274  'fileType' => $fileType
275  )
276  )
277  );
278  import('lib.pkp.classes.linkAction.LinkAction');
279  $linkAction = new LinkAction(
280  'uploadFile-' . $settingName,
281  $ajaxModal,
282  __('common.upload'),
283  null
284  );
285 
286  return $linkAction;
287  }
288 
295  function _getDeleteFileLinkAction($settingName, $request) {
296  $router = $request->getRouter();
297  import('lib.pkp.classes.linkAction.request.RemoteActionConfirmationModal');
298 
299  $confirmationModal = new RemoteActionConfirmationModal(
300  __('common.confirmDelete'), null,
301  $router->url(
302  $request, null, null, 'deleteFile', null, array(
303  'fileSettingName' => $settingName,
304  'tab' => 'siteSetup'
305  )
306  )
307  );
308  $linkAction = new LinkAction(
309  'deleteFile-' . $settingName,
310  $confirmationModal,
311  __('common.delete'),
312  null
313  );
314 
315  return $linkAction;
316  }
317 }
318 
319 ?>
static & getDAO($name, $dbconn=null)
addCheck($formValidator)
Definition: Form.inc.php:396
setData($key, $value)
Definition: Form.inc.php:219
Wrapper class for uploading files to a site/press&#39; public directory.
fetch($request, $params=null)
Form validation check with a custom user function performing the validation check.
Form validation check to make sure the form is POSTed.
Form validation check for email addresses.
static requireComponents()
readUserVars($vars)
Definition: Form.inc.php:364
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
getData($key)
Definition: Form.inc.php:210
getLocaleFieldNames()
Definition: Form.inc.php:317
A modal that retrieves its content from via AJAX.
static getLocale()
_getDeleteFileLinkAction($settingName, $request)
Class to represent a form validation check for localized fields.
Form to edit site settings.
Class defining a simple confirmation modal with a remote action and ok/cancel buttons.
_getFileUploadLinkAction($settingName, $fileType, $request)
renderFileView($fileSettingName, $request)
static getContextDAO()
Base class defining an action that can be performed by the user in the user interface.
deleteFile($fileSettingName, $request)