Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
PluginGridHandler.inc.php
1 <?php
2 
16 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
17 import('lib.pkp.controllers.grid.plugins.form.UploadPluginForm');
18 
19 abstract class PluginGridHandler extends CategoryGridHandler {
24  function PluginGridHandler($roles) {
25  $this->addRoleAssignment($roles,
26  array('fetchGrid, fetchCategory', 'fetchRow'));
27 
28  $this->addRoleAssignment(ROLE_ID_SITE_ADMIN,
29  array('uploadPlugin', 'upgradePlugin', 'deletePlugin'));
30 
31  parent::CategoryGridHandler();
32  }
33 
34 
35  //
36  // Overridden template methods
37  //
41  function initialize($request) {
42  parent::initialize($request);
43 
44  // Load language components
45  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_PKP_COMMON, LOCALE_COMPONENT_APP_MANAGER);
46 
47  // Basic grid configuration
48  $this->setTitle('common.plugins');
49 
50  // Set the no items row text
51  $this->setEmptyRowText('grid.noItems');
52 
53  // Columns
54  import('lib.pkp.controllers.grid.plugins.PluginGridCellProvider');
55  $pluginCellProvider = new PluginGridCellProvider();
56  $this->addColumn(
57  new GridColumn('name',
58  'common.name',
59  null,
60  'controllers/grid/gridCell.tpl',
61  $pluginCellProvider,
62  array('multiline' => true)
63  )
64  );
65 
66  $descriptionColumn = new GridColumn(
67  'description',
68  'common.description',
69  null,
70  'controllers/grid/gridCell.tpl',
71  $pluginCellProvider
72  );
73  $descriptionColumn->addFlag('html', true);
74  $this->addColumn($descriptionColumn);
75 
76  $this->addColumn(
77  new GridColumn('enabled',
78  'common.enabled',
79  null,
80  'controllers/grid/common/cell/selectStatusCell.tpl',
81  $pluginCellProvider
82  )
83  );
84 
85  $router = $request->getRouter();
86 
87  // Grid level actions.
88  $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
89  if (in_array(ROLE_ID_SITE_ADMIN, $userRoles)) {
90  import('lib.pkp.classes.linkAction.request.AjaxModal');
91 
92  // Install plugin.
93  $this->addAction(
94  new LinkAction(
95  'upload',
96  new AjaxModal(
97  $router->url($request, null, null, 'uploadPlugin'),
98  __('manager.plugins.upload'), 'modal_add_file'),
99  __('manager.plugins.upload'),
100  'add'));
101  }
102  }
103 
107  function getFilterForm() {
108  return 'controllers/grid/plugins/pluginGridFilter.tpl';
109  }
110 
114  function getFilterSelectionData($request) {
115  $category = $request->getUserVar('category');
116  $pluginName = $request->getUserVar('pluginName');
117 
118  if (is_null($category)) {
119  $category = 'all';
120  }
121 
122  return array('category' => $category, 'pluginName' => $pluginName);
123  }
124 
128  function renderFilter($request) {
129  $categoriesSymbolic = $this->loadData($request, null);
130  $categories = array('all' => __('grid.plugin.allCategories'));
131  foreach ($categoriesSymbolic as $category) {
132  $categories[$category] = __("plugins.categories.$category");
133  }
134  $filterData = array('categories' => $categories);
135 
136  return parent::renderFilter($request, $filterData);
137  }
138 
143  import('lib.pkp.controllers.grid.plugins.PluginCategoryGridRow');
144  return new PluginCategoryGridRow();
145  }
146 
150  function getCategoryData($categoryDataElement, $filter) {
151  $plugins =& PluginRegistry::loadCategory($categoryDataElement);
152 
153  $versionDao = DAORegistry::getDAO('VersionDAO');
154  import('lib.pkp.classes.site.VersionCheck');
155  $fileManager = new FileManager();
156 
157  $notHiddenPlugins = array();
158  foreach ($plugins as $plugin) {
159  if (!$plugin->getHideManagement()) {
160  $notHiddenPlugins[$plugin->getName()] = $plugin;
161  }
162  $version = $plugin->getCurrentVersion();
163  if ($version == null) { // this plugin is on the file system, but not installed.
164  $versionFile = $plugin->getPluginPath() . '/version.xml';
165  if ($fileManager->fileExists($versionFile)) {
166  $versionInfo = VersionCheck::parseVersionXML($versionFile);
167  $pluginVersion = $versionInfo['version'];
168  } else {
169  $pluginVersion = new Version(
170  1, 0, 0, 0, // Major, minor, revision, build
171  Core::getCurrentDate(), // Date installed
172  1, // Current
173  'plugins.'.$plugin->getCategory(), // Type
174  basename($plugin->getPluginPath()), // Product
175  '', // Class name
176  0, // Lazy load
177  $plugin->isSitePlugin() // Site wide
178  );
179  }
180  $versionDao->insertVersion($pluginVersion, true);
181  }
182  }
183 
184  if (!is_null($filter) && isset($filter['pluginName']) && $filter['pluginName'] != "") {
185  // Find all plugins that have the filter name string in their display names.
186  $filteredPlugins = array();
187  foreach ($notHiddenPlugins as $plugin) { /* @var $plugin Plugin */
188  $pluginName = $plugin->getDisplayName();
189  if (stristr($pluginName, $filter['pluginName']) !== false) {
190  $filteredPlugins[$plugin->getName()] = $plugin;
191  }
192  }
193  return $filteredPlugins;
194  }
195 
196  return $notHiddenPlugins;
197  }
198 
203  return 'category';
204  }
205 
209  function loadData($request, $filter) {
210  $categories = PluginRegistry::getCategories();
211  if (is_array($filter) && isset($filter['category']) && array_search($filter['category'], $categories) !== false) {
212  return array($filter['category'] => $filter['category']);
213  } else {
214  return array_combine($categories, $categories);
215  }
216  }
217 
218 
219  //
220  // Public handler methods.
221  //
227  function plugin($args, $request) {
228  $verb = (string) $request->getUserVar('verb');
229 
230  $plugin = $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN); /* @var $plugin Plugin */
231  $message = null;
232  $pluginModalContent = null;
233  if (!is_a($plugin, 'Plugin') || !$plugin->manage($verb, $args, $message, $messageParams, $pluginModalContent)) {
234  if ($message) {
235  $notificationManager = new NotificationManager();
236  $user = $request->getUser();
237  $notificationManager->createTrivialNotification($user->getId(), $message, $messageParams);
238  }
239  }
240  if ($pluginModalContent) {
241  $json = new JSONMessage(true, $pluginModalContent);
242  $json->setEvent('refreshForm', $pluginModalContent);
243  return $json->getString();
244  }
245 
246  return DAO::getDataChangedEvent($request->getUserVar('plugin'), $request->getUserVar($this->getCategoryRowIdParameterName()));
247  }
248 
255  function uploadPlugin($args, $request) {
256  return $this->_showUploadPluginForm(PLUGIN_ACTION_UPLOAD, $request);
257  }
258 
265  function upgradePlugin($args, $request) {
266  return $this->_showUploadPluginForm(PLUGIN_ACTION_UPGRADE, $request);
267  }
268 
274  function uploadPluginFile($args, $request) {
275  import('lib.pkp.classes.file.TemporaryFileManager');
276  $temporaryFileManager = new TemporaryFileManager();
277  $user = $request->getUser();
278 
279  // Return the temporary file id.
280  if ($temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId())) {
281  $json = new JSONMessage(true);
282  $json->setAdditionalAttributes(array(
283  'temporaryFileId' => $temporaryFile->getId()
284  ));
285  } else {
286  $json = new JSONMessage(false, __('manager.plugins.uploadError'));
287  }
288 
289  return $json->getString();
290  }
291 
298  function saveUploadPlugin($args, $request) {
299  $function = $request->getUserVar('function');
300  $uploadPluginForm = new UploadPluginForm($function);
301  $uploadPluginForm->readInputData();
302 
303  if($uploadPluginForm->validate()) {
304  if($uploadPluginForm->execute($request)) {
305  return DAO::getDataChangedEvent();
306  }
307  }
308 
309  $json = new JSONMessage(false);
310  return $json->getString();
311  }
312 
318  function deletePlugin($args, $request) {
319  $plugin =& $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN);
320  $category = $plugin->getCategory();
321  $productName = basename($plugin->getPluginPath());
322 
323  $versionDao = DAORegistry::getDAO('VersionDAO'); /* @var $versionDao VersionDAO */
324  $installedPlugin = $versionDao->getCurrentVersion('plugins.'.$category, $productName, true);
325 
326  $notificationMgr = new NotificationManager();
327  $user = $request->getUser();
328 
329  if ($installedPlugin) {
330  $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
331  $pluginLibDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . PKP_LIB_PATH . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
332 
333  // make sure plugin type is valid and then delete the files
334  if (in_array($category, PluginRegistry::getCategories())) {
335  // Delete the plugin from the file system.
336  $fileManager = new FileManager();
337  $fileManager->rmtree($pluginDest);
338  $fileManager->rmtree($pluginLibDest);
339  }
340 
341  if(is_dir($pluginDest) || is_dir($pluginLibDest)) {
342  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.deleteError', array('pluginName' => $plugin->getDisplayName()))));
343  } else {
344  $versionDao->disableVersion('plugins.'.$category, $productName);
345  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('manager.plugins.deleteSuccess', array('pluginName' => $plugin->getDisplayName()))));
346  }
347  } else {
348  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.doesNotExist', array('pluginName' => $plugin->getDisplayName()))));
349  }
350 
351  return DAO::getDataChangedEvent($plugin->getName());
352  }
353 
360  function _showUploadPluginForm($function, $request) {
361  import('lib.pkp.controllers.grid.plugins.form.UploadPluginForm');
362  $uploadPluginForm = new UploadPluginForm($function);
363  $uploadPluginForm->initData();
364 
365  $json = new JSONMessage(true, $uploadPluginForm->fetch($request));
366  return $json->getString();
367  }
368 }
369 
370 ?>
saveUploadPlugin($args, $request)
Cell provider for columns in a plugin grid.
static & getDAO($name, $dbconn=null)
Plugin category grid row definition.
Describes system version history.
Definition: Version.inc.php:18
uploadPluginFile($args, $request)
static requireComponents()
setEmptyRowText($emptyRowText)
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
static & loadCategory($category, $enabledOnly=false, $mainContextId=null)
uploadPlugin($args, $request)
addRoleAssignment($roleIds, $operations)
Class defining basic operations for file management.
A modal that retrieves its content from via AJAX.
Class to represent a JSON (Javascript Object Notation) message.
Class defining basic operations for handling HTML grids with categories.
Form to upload a plugin file.
static & parseVersionXML($url)
loadData($request, $filter)
deletePlugin($args, $request)
upgradePlugin($args, $request)
getCategoryData($categoryDataElement, $filter)
_showUploadPluginForm($function, $request)
static getBaseDir()
Definition: Core.inc.php:37
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:632
& getAuthorizedContextObject($assocType)
Handle plugins grid requests.
Base class defining an action that can be performed by the user in the user interface.
static getCurrentDate($ts=null)
Definition: Core.inc.php:95