Open Journal Systems  3.0.0
 All Data Structures 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('enable', 'disable', 'manage', '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  null,
61  $pluginCellProvider,
62  array(
63  'showTotalItemsNumber' => true,
64  'collapseAllColumnsInCategories' => true
65  )
66  )
67  );
68 
69  $descriptionColumn = new GridColumn(
70  'description',
71  'common.description',
72  null,
73  null,
74  $pluginCellProvider
75  );
76  $descriptionColumn->addFlag('html', true);
77  $this->addColumn($descriptionColumn);
78 
79  $this->addColumn(
80  new GridColumn('enabled',
81  'common.enabled',
82  null,
83  'controllers/grid/common/cell/selectStatusCell.tpl',
84  $pluginCellProvider
85  )
86  );
87 
88  $router = $request->getRouter();
89 
90  // Grid level actions.
91  $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
92  if (in_array(ROLE_ID_SITE_ADMIN, $userRoles)) {
93  import('lib.pkp.classes.linkAction.request.AjaxModal');
94 
95  // Install plugin.
96  $this->addAction(
97  new LinkAction(
98  'upload',
99  new AjaxModal(
100  $router->url($request, null, null, 'uploadPlugin'),
101  __('manager.plugins.upload'), 'modal_add_file'),
102  __('manager.plugins.upload'),
103  'add'));
104  }
105  }
106 
110  protected function getFilterForm() {
111  return 'controllers/grid/plugins/pluginGridFilter.tpl';
112  }
113 
117  function getFilterSelectionData($request) {
118  $category = $request->getUserVar('category');
119  $pluginName = $request->getUserVar('pluginName');
120 
121  if (is_null($category)) {
122  $category = 'all';
123  }
124 
125  return array('category' => $category, 'pluginName' => $pluginName);
126  }
127 
131  function renderFilter($request) {
132  $categoriesSymbolic = $this->loadData($request, null);
133  $categories = array('all' => __('grid.plugin.allCategories'));
134  foreach ($categoriesSymbolic as $category) {
135  $categories[$category] = __("plugins.categories.$category");
136  }
137  $filterData = array('categories' => $categories);
138 
139  return parent::renderFilter($request, $filterData);
140  }
141 
145  protected function getCategoryRowInstance() {
146  import('lib.pkp.controllers.grid.plugins.PluginCategoryGridRow');
147  return new PluginCategoryGridRow();
148  }
149 
153  function loadCategoryData($request, $categoryDataElement, $filter) {
154  $plugins =& PluginRegistry::loadCategory($categoryDataElement);
155 
156  $versionDao = DAORegistry::getDAO('VersionDAO');
157  import('lib.pkp.classes.site.VersionCheck');
158  $fileManager = new FileManager();
159 
160  $notHiddenPlugins = array();
161  foreach ((array) $plugins as $plugin) {
162  if (!$plugin->getHideManagement()) {
163  $notHiddenPlugins[$plugin->getName()] = $plugin;
164  }
165  $version = $plugin->getCurrentVersion();
166  if ($version == null) { // this plugin is on the file system, but not installed.
167  $versionFile = $plugin->getPluginPath() . '/version.xml';
168  if ($fileManager->fileExists($versionFile)) {
169  $versionInfo = VersionCheck::parseVersionXML($versionFile);
170  $pluginVersion = $versionInfo['version'];
171  } else {
172  $pluginVersion = new Version(
173  1, 0, 0, 0, // Major, minor, revision, build
174  Core::getCurrentDate(), // Date installed
175  1, // Current
176  'plugins.'.$plugin->getCategory(), // Type
177  basename($plugin->getPluginPath()), // Product
178  '', // Class name
179  0, // Lazy load
180  $plugin->isSitePlugin() // Site wide
181  );
182  }
183  $versionDao->insertVersion($pluginVersion, true);
184  }
185  }
186 
187  if (!is_null($filter) && isset($filter['pluginName']) && $filter['pluginName'] != "") {
188  // Find all plugins that have the filter name string in their display names.
189  $filteredPlugins = array();
190  foreach ($notHiddenPlugins as $plugin) { /* @var $plugin Plugin */
191  $pluginName = $plugin->getDisplayName();
192  if (stristr($pluginName, $filter['pluginName']) !== false) {
193  $filteredPlugins[$plugin->getName()] = $plugin;
194  }
195  }
196  return $filteredPlugins;
197  }
198 
199  return $notHiddenPlugins;
200  }
201 
206  return 'category';
207  }
208 
212  protected function loadData($request, $filter) {
213  $categories = PluginRegistry::getCategories();
214  if (is_array($filter) && isset($filter['category']) && array_search($filter['category'], $categories) !== false) {
215  return array($filter['category'] => $filter['category']);
216  } else {
217  return array_combine($categories, $categories);
218  }
219  }
220 
221 
222  //
223  // Public handler methods.
224  //
231  function manage($args, $request) {
232  $plugin = $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN); /* @var $plugin Plugin */
233  return $plugin->manage($args, $request);
234  }
235 
242  function enable($args, $request) {
243  $plugin = $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN); /* @var $plugin Plugin */
244  if ($plugin->getCanEnable()) {
245  $plugin->setEnabled(true);
246  $user = $request->getUser();
247  $notificationManager = new NotificationManager();
248  $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_PLUGIN_ENABLED, array('pluginName' => $plugin->getDisplayName()));
249  }
250  return DAO::getDataChangedEvent($request->getUserVar('plugin'), $request->getUserVar($this->getCategoryRowIdParameterName()));
251  }
252 
259  function disable($args, $request) {
260  $plugin = $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN); /* @var $plugin Plugin */
261  if ($plugin->getCanDisable()) {
262  $plugin->setEnabled(false);
263  $user = $request->getUser();
264  $notificationManager = new NotificationManager();
265  $notificationManager->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_PLUGIN_DISABLED, array('pluginName' => $plugin->getDisplayName()));
266  }
267  return DAO::getDataChangedEvent($request->getUserVar('plugin'), $request->getUserVar($this->getCategoryRowIdParameterName()));
268  }
269 
276  function uploadPlugin($args, $request) {
277  return $this->_showUploadPluginForm(PLUGIN_ACTION_UPLOAD, $request);
278  }
279 
286  function upgradePlugin($args, $request) {
287  return $this->_showUploadPluginForm(PLUGIN_ACTION_UPGRADE, $request);
288  }
289 
296  function uploadPluginFile($args, $request) {
297  import('lib.pkp.classes.file.TemporaryFileManager');
298  $temporaryFileManager = new TemporaryFileManager();
299  $user = $request->getUser();
300 
301  // Return the temporary file id.
302  if ($temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId())) {
303  $json = new JSONMessage(true);
304  $json->setAdditionalAttributes(array(
305  'temporaryFileId' => $temporaryFile->getId()
306  ));
307  return $json;
308  } else {
309  return new JSONMessage(false, __('manager.plugins.uploadError'));
310  }
311  }
312 
319  function saveUploadPlugin($args, $request) {
320  $function = $request->getUserVar('function');
321  $uploadPluginForm = new UploadPluginForm($function);
322  $uploadPluginForm->readInputData();
323 
324  if($uploadPluginForm->validate()) {
325  if($uploadPluginForm->execute($request)) {
326  return DAO::getDataChangedEvent();
327  }
328  }
329 
330  return new JSONMessage(false);
331  }
332 
339  function deletePlugin($args, $request) {
340  $plugin = $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN);
341  $category = $plugin->getCategory();
342  $productName = basename($plugin->getPluginPath());
343 
344  $versionDao = DAORegistry::getDAO('VersionDAO'); /* @var $versionDao VersionDAO */
345  $installedPlugin = $versionDao->getCurrentVersion('plugins.'.$category, $productName, true);
346 
347  $notificationMgr = new NotificationManager();
348  $user = $request->getUser();
349 
350  if ($installedPlugin) {
351  $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
352  $pluginLibDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . PKP_LIB_PATH . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
353 
354  // make sure plugin type is valid and then delete the files
355  if (in_array($category, PluginRegistry::getCategories())) {
356  // Delete the plugin from the file system.
357  $fileManager = new FileManager();
358  $fileManager->rmtree($pluginDest);
359  $fileManager->rmtree($pluginLibDest);
360  }
361 
362  if(is_dir($pluginDest) || is_dir($pluginLibDest)) {
363  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.deleteError', array('pluginName' => $plugin->getDisplayName()))));
364  } else {
365  $versionDao->disableVersion('plugins.'.$category, $productName);
366  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('manager.plugins.deleteSuccess', array('pluginName' => $plugin->getDisplayName()))));
367  }
368  } else {
369  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.doesNotExist', array('pluginName' => $plugin->getDisplayName()))));
370  }
371 
372  return DAO::getDataChangedEvent($plugin->getName());
373  }
374 
381  function _showUploadPluginForm($function, $request) {
382  import('lib.pkp.controllers.grid.plugins.form.UploadPluginForm');
383  $uploadPluginForm = new UploadPluginForm($function);
384  $uploadPluginForm->initData();
385 
386  return new JSONMessage(true, $uploadPluginForm->fetch($request));
387  }
388 }
389 
390 ?>
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()
static parseVersionXML($url)
setEmptyRowText($emptyRowText)
disable($args, $request)
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.
loadData($request, $filter)
deletePlugin($args, $request)
upgradePlugin($args, $request)
_showUploadPluginForm($function, $request)
loadCategoryData($request, $categoryDataElement, $filter)
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:633
& 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