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 
23  function PluginGridHandler($roles) {
24  if (is_null($roles)) {
25  fatalError('Direct access not allowed!');
26  }
27 
28  $this->addRoleAssignment($roles,
29  array('fetchGrid, fetchCategory', 'fetchRow'));
30 
31  $this->addRoleAssignment(ROLE_ID_SITE_ADMIN,
32  array('installPlugin', 'upgradePlugin', 'deletePlugin'));
33 
34  parent::CategoryGridHandler();
35  }
36 
37 
38  //
39  // Overridden template methods
40  //
44  function initialize($request) {
45  parent::initialize($request);
46 
47  // Load language components
48  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_PKP_COMMON, LOCALE_COMPONENT_APP_MANAGER);
49 
50  // Basic grid configuration
51  $this->setTitle('common.plugins');
52 
53  // Set the no items row text
54  $this->setEmptyRowText('grid.noItems');
55 
56  // Columns
57  import('lib.pkp.controllers.grid.plugins.PluginGridCellProvider');
58  $pluginCellProvider = new PluginGridCellProvider();
59  $this->addColumn(
60  new GridColumn('name',
61  'common.name',
62  null,
63  'controllers/grid/gridCell.tpl',
64  $pluginCellProvider,
65  array('multiline' => true)
66  )
67  );
68 
69  $descriptionColumn = new GridColumn(
70  'description',
71  'common.description',
72  null,
73  'controllers/grid/gridCell.tpl',
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  'install',
99  new AjaxModal(
100  $router->url($request, null, null, 'installPlugin'),
101  __('manager.plugins.install'), 'modal_add_file'),
102  __('manager.plugins.install'),
103  'add'));
104  }
105  }
106 
110  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 
146  import('lib.pkp.controllers.grid.plugins.PluginCategoryGridRow');
147  return new PluginCategoryGridRow();
148  }
149 
153  function getCategoryData($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  foreach ($plugins as $plugin) {
161  $version = $plugin->getCurrentVersion();
162  if ($version == null) { // this plugin is on the file system, but not installed.
163  $versionFile = $plugin->getPluginPath() . '/version.xml';
164  if ($fileManager->fileExists($versionFile)) {
165  $versionInfo = VersionCheck::parseVersionXML($versionFile);
166  $pluginVersion = $versionInfo['version'];
167  } else {
168  $pluginVersion = new Version(
169  1, 0, 0, 0, // Major, minor, revision, build
170  Core::getCurrentDate(), // Date installed
171  1, // Current
172  'plugins.'.$category, // Type
173  basename($plugin->getPluginPath()), // Product
174  '', // Class name
175  0, // Lazy load
176  $plugin->isSitePlugin() // Site wide
177  );
178  }
179  $versionDao->insertVersion($pluginVersion, true);
180  }
181  }
182 
183  if (!is_null($filter) && isset($filter['pluginName']) && $filter['pluginName'] != "") {
184  // Find all plugins that have the filter name string in their display names.
185  $filteredPlugins = array();
186  foreach ($plugins as $plugin) { /* @var $plugin Plugin */
187  $pluginName = $plugin->getDisplayName();
188  if (stristr($pluginName, $filter['pluginName']) !== false) {
189  $filteredPlugins[$plugin->getName()] = $plugin;
190  }
191  }
192  return $filteredPlugins;
193  }
194 
195  return $plugins;
196  }
197 
202  return 'category';
203  }
204 
208  function loadData($request, $filter) {
209  $categories = PluginRegistry::getCategories();
210  if (is_array($filter) && isset($filter['category']) && array_search($filter['category'], $categories) !== false) {
211  return array($filter['category'] => $filter['category']);
212  } else {
213  return array_combine($categories, $categories);
214  }
215  }
216 
217 
218  //
219  // Public handler methods.
220  //
226  function plugin($args, $request) {
227  $verb = (string) $request->getUserVar('verb');
228 
229  $this->setupTemplate($request, true);
230 
231  $plugin = $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN); /* @var $plugin Plugin */
232  $message = null;
233  $pluginModalContent = null;
234  if (!is_a($plugin, 'Plugin') || !$plugin->manage($verb, $args, $message, $messageParams, $pluginModalContent)) {
235  if ($message) {
236  $notificationManager = new NotificationManager();
237  $user = $request->getUser();
238  $notificationManager->createTrivialNotification($user->getId(), $message, $messageParams);
239 
240  return DAO::getDataChangedEvent($request->getUserVar('plugin'), $request->getUserVar($this->getCategoryRowIdParameterName()));
241  }
242  }
243  if ($pluginModalContent) {
244  $json = new JSONMessage(true, $pluginModalContent);
245  $json->setEvent('refreshForm', $pluginModalContent);
246  return $json->getString();
247  }
248  }
249 
256  function installPlugin($args, $request) {
257  return $this->_showUploadPluginForm('install', $request);
258  }
259 
266  function upgradePlugin($args, $request) {
267  return $this->_showUploadPluginForm('upgrade', $request);
268  }
269 
275  function uploadPlugin($args, $request) {
276  import('lib.pkp.classes.file.TemporaryFileManager');
277  $temporaryFileManager = new TemporaryFileManager();
278  $user = $request->getUser();
279 
280  // Return the temporary file id.
281  if ($temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId())) {
282  $json = new JSONMessage(true);
283  $json->setAdditionalAttributes(array(
284  'temporaryFileId' => $temporaryFile->getId()
285  ));
286  } else {
287  $json = new JSONMessage(false, __('manager.plugins.uploadError'));
288  }
289 
290  return $json->getString();
291  }
292 
299  function saveUploadPlugin($args, $request) {
300  $function = $request->getUserVar('function');
301 
302  import('lib.pkp.controllers.grid.plugins.form.UploadPluginForm');
303  $uploadPluginForm = new UploadPluginForm($function);
304  $uploadPluginForm->readInputData();
305 
306  if($uploadPluginForm->validate()) {
307  if($uploadPluginForm->execute($request)) {
308  return DAO::getDataChangedEvent();
309  }
310  }
311 
312  $json = new JSONMessage(false);
313  return $json->getString();
314  }
315 
321  function deletePlugin($args, $request) {
322  $this->setupTemplate($request);
323  $plugin =& $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN);
324  $category = $plugin->getCategory();
325  $productName = basename($plugin->getPluginPath());
326 
327  $versionDao = DAORegistry::getDAO('VersionDAO'); /* @var $versionDao VersionDAO */
328  $installedPlugin = $versionDao->getCurrentVersion('plugins.'.$category, $productName, true);
329 
330  $notificationMgr = new NotificationManager();
331  $user = $request->getUser();
332 
333  if ($installedPlugin) {
334  $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
335  $pluginLibDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . PKP_LIB_PATH . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
336 
337  // make sure plugin type is valid and then delete the files
338  if (in_array($category, PluginRegistry::getCategories())) {
339  // Delete the plugin from the file system.
340  $fileManager = new FileManager();
341  $fileManager->rmtree($pluginDest);
342  $fileManager->rmtree($pluginLibDest);
343  }
344 
345  if(is_dir($pluginDest) || is_dir($pluginLibDest)) {
346  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.deleteError', array('pluginName' => $plugin->getDisplayName()))));
347  } else {
348  $versionDao->disableVersion('plugins.'.$category, $productName);
349  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('manager.plugins.deleteSuccess', array('pluginName' => $plugin->getDisplayName()))));
350  }
351  } else {
352  $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.doesNotExist', array('pluginName' => $plugin->getDisplayName()))));
353  }
354 
355  return DAO::getDataChangedEvent($plugin->getName());
356  }
357 
363  function _showUploadPluginForm($function, $request) {
364  $this->setupTemplate($request, true);
365 
366  import('lib.pkp.controllers.grid.plugins.form.UploadPluginForm');
367  $uploadPluginForm = new UploadPluginForm($function);
368  $uploadPluginForm->initData();
369 
370  $json = new JSONMessage(true, $uploadPluginForm->fetch($request));
371  return $json->getString();
372  }
373 }
374 
375 ?>
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
static requireComponents()
setEmptyRowText($emptyRowText)
addAction($action, $position=GRID_ACTION_POSITION_ABOVE)
addColumn($column)
setupTemplate($request)
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.
static getDataChangedEvent($elementId=null, $parentElementId=null)
Definition: DAO.inc.php:606
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)
getCategoryData($categoryDataElement, $filter)
installPlugin($args, $request)
_showUploadPluginForm($function, $request)
& parseVersionXML($url)
static getBaseDir()
Definition: Core.inc.php:37
Represents a column within a grid. It is used to configure the way cells within a column are displaye...
& 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