00001 <?php
00002
00015 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
00016 import('lib.pkp.classes.controllers.grid.DataObjectGridCellProvider');
00017
00018 class PluginGridHandler extends CategoryGridHandler {
00022 function PluginGridHandler($roles) {
00023 if (is_null($roles)) {
00024 fatalError('Direct access not allowed!');
00025 }
00026
00027 $this->addRoleAssignment($roles,
00028 array('fetchGrid, fetchCategory', 'fetchRow'));
00029
00030 $this->addRoleAssignment(ROLE_ID_SITE_ADMIN,
00031 array('installPlugin', 'upgradePlugin', 'deletePlugin'));
00032
00033 parent::CategoryGridHandler();
00034 }
00035
00036
00037
00038
00039
00043 function authorize($request, $args, $roleAssignments) {
00044 $category = $request->getUserVar('category');
00045 $pluginName = $request->getUserVar('plugin');
00046 $verb = $request->getUserVar('verb');
00047
00048 if ($category && $pluginName) {
00049 import('classes.security.authorization.OmpPluginAccessPolicy');
00050 if ($verb) {
00051 $accessMode = ACCESS_MODE_MANAGE;
00052 } else {
00053 $accessMode = ACCESS_MODE_ADMIN;
00054 }
00055
00056 $this->addPolicy(new OmpPluginAccessPolicy($request, $args, $roleAssignments, $accessMode));
00057 }
00058
00059 return parent::authorize($request, $args, $roleAssignments);
00060 }
00061
00065 function initialize(&$request) {
00066 parent::initialize($request);
00067
00068
00069 AppLocale::requireComponents(LOCALE_COMPONENT_PKP_MANAGER, LOCALE_COMPONENT_PKP_COMMON);
00070 AppLocale::requireComponents(LOCALE_COMPONENT_OMP_MANAGER);
00071
00072
00073 $this->setTitle('common.plugins');
00074
00075
00076 $this->setEmptyRowText('grid.noItems');
00077
00078 $press =& $request->getPress();
00079
00080
00081 import('controllers.grid.plugins.PluginGridCellProvider');
00082 $pluginCellProvider = new PluginGridCellProvider();
00083 $this->addColumn(
00084 new GridColumn('name',
00085 'common.name',
00086 null,
00087 'controllers/grid/gridCell.tpl',
00088 $pluginCellProvider,
00089 array('multiline' => true)
00090 )
00091 );
00092
00093 $descriptionColumn = new GridColumn(
00094 'description',
00095 'common.description',
00096 null,
00097 'controllers/grid/gridCell.tpl',
00098 $pluginCellProvider
00099 );
00100 $descriptionColumn->addFlag('html', true);
00101 $this->addColumn($descriptionColumn);
00102
00103 $this->addColumn(
00104 new GridColumn('enabled',
00105 'common.enabled',
00106 null,
00107 'controllers/grid/common/cell/selectStatusCell.tpl',
00108 $pluginCellProvider
00109 )
00110 );
00111
00112 $router =& $request->getRouter();
00113
00114
00115 $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
00116 if (in_array(ROLE_ID_SITE_ADMIN, $userRoles)) {
00117 import('lib.pkp.classes.linkAction.request.AjaxModal');
00118
00119
00120 $this->addAction(
00121 new LinkAction(
00122 'install',
00123 new AjaxModal(
00124 $router->url($request, null, null, 'installPlugin'),
00125 __('manager.plugins.install'), 'modal_add_file'),
00126 __('manager.plugins.install'),
00127 'add'));
00128 }
00129 }
00130
00134 function getFilterForm() {
00135 return 'controllers/grid/plugins/pluginGridFilter.tpl';
00136 }
00137
00141 function getFilterSelectionData(&$request) {
00142 $category = $request->getUserVar('category');
00143 $pluginName = $request->getUserVar('pluginName');
00144
00145 if (is_null($category)) {
00146 $category = 'all';
00147 }
00148
00149 return array('category' => $category, 'pluginName' => $pluginName);
00150 }
00151
00155 function renderFilter($request) {
00156 $categoriesSymbolic = $this->loadData($request, null);
00157 $categories = array('all' => __('grid.plugin.allCategories'));
00158 foreach ($categoriesSymbolic as $category) {
00159 $categories[$category] = __("plugins.categories.$category");
00160 }
00161 $filterData = array('categories' => $categories);
00162
00163 return parent::renderFilter($request, $filterData);
00164 }
00165
00169 function getCategoryRowInstance() {
00170 import('controllers.grid.plugins.PluginCategoryGridRow');
00171 return new PluginCategoryGridRow();
00172 }
00173
00177 function getCategoryData($categoryDataElement, $filter) {
00178 $plugins =& PluginRegistry::loadCategory($categoryDataElement);
00179
00180 if (!is_null($filter) && isset($filter['pluginName']) && $filter['pluginName'] != "") {
00181
00182 $filteredPlugins = array();
00183 foreach ($plugins as $plugin) {
00184 $pluginName = $plugin->getDisplayName();
00185 if (stristr($pluginName, $filter['pluginName']) !== false) {
00186 $filteredPlugins[$plugin->getName()] = $plugin;
00187 }
00188 unset($plugin);
00189 }
00190 return $filteredPlugins;
00191 }
00192
00193 return $plugins;
00194 }
00195
00199 function getCategoryRowIdParameterName() {
00200 return 'category';
00201 }
00202
00207 function getRowInstance($contextLevel) {
00208 $userRoles = $this->getAuthorizedContextObject(ASSOC_TYPE_USER_ROLES);
00209
00210 import('controllers.grid.plugins.PluginGridRow');
00211 return new PluginGridRow($userRoles, $contextLevel);
00212 }
00213
00217 function loadData($request, $filter) {
00218 $categories = PluginRegistry::getCategories();
00219 if (is_array($filter) && isset($filter['category']) && ($i = array_search($filter['category'], $categories)) !== false) {
00220 return array($filter['category'] => $filter['category']);
00221 } else {
00222 return array_combine($categories, $categories);
00223 }
00224 }
00225
00226
00227
00228
00229
00235 function plugin($args, &$request) {
00236 $verb = (string) $request->getUserVar('verb');
00237
00238 $this->setupTemplate(true);
00239
00240 $plugin =& $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN);
00241 $message = null;
00242 $pluginModalContent = null;
00243 if (!is_a($plugin, 'Plugin') || !$plugin->manage($verb, $args, $message, &$messageParams, &$pluginModalContent)) {
00244 if ($message) {
00245 $notificationManager = new NotificationManager();
00246 $user =& $request->getUser();
00247 $notificationManager->createTrivialNotification($user->getId(), $message, $messageParams);
00248
00249 return DAO::getDataChangedEvent($request->getUserVar('plugin'), $request->getUserVar($this->getCategoryRowIdParameterName()));
00250 }
00251 }
00252 if ($pluginModalContent) {
00253 $json = new JSONMessage(true, $pluginModalContent);
00254 return $json->getString();
00255 }
00256 }
00257
00264 function installPlugin($args, &$request) {
00265 return $this->_showUploadPluginForm('install');
00266 }
00267
00274 function upgradePlugin($args, &$request) {
00275 return $this->_showUploadPluginForm('upgrade');
00276 }
00277
00283 function uploadPlugin($args, &$request) {
00284 $errorMsg = '';
00285
00286 import('classes.file.TemporaryFileManager');
00287 $temporaryFileManager = new TemporaryFileManager();
00288 $user =& $request->getUser();
00289
00290
00291 if ($temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId())) {
00292 $json = new JSONMessage(true);
00293 $json->setAdditionalAttributes(array(
00294 'temporaryFileId' => $temporaryFile->getId()
00295 ));
00296 } else {
00297 $json = new JSONMessage(false, __('manager.plugins.uploadError'));
00298 }
00299
00300 return $json->getString();
00301 }
00302
00309 function saveUploadPlugin($args, &$request) {
00310 $function = $request->getUserVar('function');
00311
00312 import('controllers.grid.plugins.form.UploadPluginForm');
00313 $uploadPluginForm = new UploadPluginForm($function);
00314 $uploadPluginForm->readInputData();
00315
00316 if($uploadPluginForm->validate()) {
00317 if($uploadPluginForm->execute($request)) {
00318 return DAO::getDataChangedEvent();
00319 }
00320 }
00321
00322 $json = new JSONMessage(false);
00323 return $json->getString();
00324 }
00325
00331 function deletePlugin($args, &$request) {
00332 $this->setupTemplate();
00333 $plugin =& $this->getAuthorizedContextObject(ASSOC_TYPE_PLUGIN);
00334 $category = $plugin->getCategory();
00335 $productName = basename($plugin->getPluginPath());
00336
00337 $versionDao =& DAORegistry::getDAO('VersionDAO');
00338 $installedPlugin = $versionDao->getCurrentVersion('plugins.'.$category, $productName, true);
00339
00340 $notificationMgr = new NotificationManager();
00341 $user =& $request->getUser();
00342
00343 if ($installedPlugin) {
00344 $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
00345 $pluginLibDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'pkp' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $productName;
00346
00347
00348 if (in_array($category, PluginRegistry::getCategories())) {
00349
00350 $fileManager = new FileManager();
00351 $fileManager->rmtree($pluginDest);
00352 $fileManager->rmtree($pluginLibDest);
00353 }
00354
00355 if(is_dir($pluginDest) || is_dir($pluginLibDest)) {
00356 $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.deleteError', array('pluginName' => $plugin->getDisplayName()))));
00357 } else {
00358 $versionDao->disableVersion('plugins.'.$category, $productName);
00359 $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('manager.plugins.deleteSuccess', array('pluginName' => $plugin->getDisplayName()))));
00360 }
00361 } else {
00362 $notificationMgr->createTrivialNotification($user->getId(), NOTIFICATION_TYPE_ERROR, array('contents' => __('manager.plugins.doesNotExist', array('pluginName' => $plugin->getDisplayName()))));
00363 }
00364
00365 return DAO::getDataChangedEvent($plugin->getName());
00366 }
00367
00373 function _showUploadPluginForm($function) {
00374 $this->setupTemplate(true);
00375
00376 import('controllers.grid.plugins.form.UploadPluginForm');
00377 $uploadPluginForm = new UploadPluginForm($function);
00378 $uploadPluginForm->initData();
00379
00380 $json = new JSONMessage(true, $uploadPluginForm->fetch($request));
00381 return $json->getString();
00382 }
00383 }
00384 ?>