• Main Page
  • Modules
  • Classes
  • Files
  • File List

controllers/grid/content/spotlights/ManageSpotlightsGridHandler.inc.php

00001 <?php
00002 
00015 // import grid base classes
00016 import('lib.pkp.classes.controllers.grid.GridHandler');
00017 
00018 
00019 // import format grid specific classes
00020 import('controllers.grid.content.spotlights.SpotlightsGridCellProvider');
00021 import('controllers.grid.content.spotlights.SpotlightsGridRow');
00022 import('controllers.grid.content.spotlights.form.SpotlightForm');
00023 
00024 // import Spotlight class for class constants
00025 import('classes.spotlight.Spotlight');
00026 
00027 // Link action & modal classes
00028 import('lib.pkp.classes.linkAction.request.AjaxModal');
00029 
00030 class ManageSpotlightsGridHandler extends GridHandler {
00031 
00035    var $_press;
00036 
00040    function ManageSpotlightsGridHandler() {
00041       parent::GridHandler();
00042       $this->addRoleAssignment(
00043             array(ROLE_ID_PRESS_MANAGER),
00044             array('fetchGrid', 'fetchRow', 'addSpotlight', 'editSpotlight',
00045             'updateSpotlight', 'deleteSpotlight', 'itemAutocomplete'));
00046    }
00047 
00048    //
00049    // Getters/Setters
00050    //
00055    function &getPress() {
00056       return $this->_press;
00057    }
00058 
00063    function setPress($press) {
00064       $this->_press =& $press;
00065    }
00066 
00067    //
00068    // Overridden methods from PKPHandler
00069    //
00076    function authorize(&$request, $args, $roleAssignments) {
00077       import('classes.security.authorization.OmpPressAccessPolicy');
00078       $this->addPolicy(new OmpPressAccessPolicy($request, $roleAssignments));
00079       $returner = parent::authorize($request, $args, $roleAssignments);
00080 
00081       $spotlightId = $request->getUserVar('spotlightId');
00082       if ($spotlightId) {
00083          $press =& $request->getPress();
00084          $spotlightDao =& DAORegistry::getDAO('SpotlightDAO');
00085          $spotlight =& $spotlightDao->getById($spotlightId);
00086          if ($spotlight == null || $spotlight->getPressId() != $press->getId()) {
00087             return false;
00088          }
00089       }
00090 
00091       return $returner;
00092    }
00093 
00094    /*
00095     * Configure the grid
00096     * @param $request PKPRequest
00097     */
00098    function initialize(&$request) {
00099       parent::initialize($request);
00100 
00101       // Load locale components.
00102       AppLocale::requireComponents(LOCALE_COMPONENT_OMP_SUBMISSION, LOCALE_COMPONENT_OMP_MANAGER);
00103 
00104       // Basic grid configuration
00105       $this->setTitle('spotlight.spotlights');
00106 
00107       // Set the no items row text
00108       $this->setEmptyRowText('spotlight.noneExist');
00109 
00110       $press =& $request->getPress();
00111       $this->setPress($press);
00112 
00113       // Columns
00114       import('controllers.grid.content.spotlights.SpotlightsGridCellProvider');
00115       $spotlightsGridCellProvider = new SpotlightsGridCellProvider();
00116       $this->addColumn(
00117          new GridColumn('title',
00118             'grid.content.spotlights.form.title',
00119             null,
00120             'controllers/grid/gridCell.tpl',
00121             $spotlightsGridCellProvider,
00122             array('width' => 40)
00123          )
00124       );
00125 
00126       $this->addColumn(
00127          new GridColumn('itemTitle',
00128             'grid.content.spotlights.spotlightItemTitle',
00129             null,
00130             'controllers/grid/gridCell.tpl',
00131             $spotlightsGridCellProvider,
00132             array('width' => 40)
00133          )
00134       );
00135 
00136       $this->addColumn(
00137          new GridColumn('type',
00138             'common.type',
00139             null,
00140             'controllers/grid/gridCell.tpl',
00141             $spotlightsGridCellProvider
00142          )
00143       );
00144 
00145       // Add grid action.
00146       $router =& $request->getRouter();
00147       import('lib.pkp.classes.linkAction.request.AjaxModal');
00148       $this->addAction(
00149          new LinkAction(
00150             'addSpotlight',
00151             new AjaxModal(
00152                $router->url($request, null, null, 'addSpotlight', null, null),
00153                __('grid.action.addSpotlight'),
00154                'modal_add_item'
00155             ),
00156             __('grid.action.addSpotlight'),
00157             'add_item')
00158       );
00159    }
00160 
00161 
00162    //
00163    // Overridden methods from GridHandler
00164    //
00169    function &getRowInstance() {
00170       $row = new SpotlightsGridRow($this->getPress());
00171       return $row;
00172    }
00173 
00177    function loadData($request, $filter = null) {
00178 
00179       $spotlightDao =& DAORegistry::getDAO('SpotlightDAO');
00180       $press =& $this->getPress();
00181       return $spotlightDao->getByPressId($press->getId());
00182    }
00183 
00189    function getRequestArgs() {
00190       $press =& $this->getPress();
00191       return array(
00192          'pressId' => $press->getId()
00193       );
00194    }
00195 
00196    //
00197    // Public Spotlights Grid Actions
00198    //
00199 
00200    function addSpotlight($args, &$request) {
00201       return $this->editSpotlight($args, $request);
00202    }
00203 
00210    function editSpotlight($args, &$request) {
00211       $spotlightId = (int)$request->getUserVar('spotlightId');
00212       $press =& $request->getPress();
00213       $pressId = $press->getId();
00214 
00215       $spotlightForm = new SpotlightForm($pressId, $spotlightId);
00216       $spotlightForm->initData($args, $request);
00217 
00218       $json = new JSONMessage(true, $spotlightForm->fetch($request));
00219       return $json->getString();
00220    }
00221 
00228    function updateSpotlight($args, &$request) {
00229       // Identify the spotlight entry to be updated
00230       $spotlightId = $request->getUserVar('spotlightId');
00231 
00232       $press =& $this->getPress();
00233 
00234       $spotlightDao =& DAORegistry::getDAO('SpotlightDAO');
00235       $spotlight = $spotlightDao->getById($spotlightId, $press->getId());
00236 
00237       // Form handling
00238       $spotlightForm = new SpotlightForm($press->getId(), $spotlightId);
00239 
00240       $spotlightForm->readInputData();
00241       if ($spotlightForm->validate()) {
00242          $spotlightId = $spotlightForm->execute($request);
00243 
00244          if(!isset($spotlight)) {
00245             // This is a new entry
00246             $spotlight =& $spotlightDao->getById($spotlightId, $press->getId());
00247             // New added entry action notification content.
00248             $notificationContent = __('notification.addedSpotlight');
00249          } else {
00250             // entry edit action notification content.
00251             $notificationContent = __('notification.editedSpotlight');
00252          }
00253 
00254          // Create trivial notification.
00255          $currentUser =& $request->getUser();
00256          $notificationMgr = new NotificationManager();
00257          $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $notificationContent));
00258 
00259          // Prepare the grid row data
00260          $row =& $this->getRowInstance();
00261          $row->setGridId($this->getId());
00262          $row->setId($spotlightId);
00263          $row->setData($spotlight);
00264          $row->initialize($request);
00265 
00266          // Render the row into a JSON response
00267          return DAO::getDataChangedEvent();
00268 
00269       } else {
00270          $json = new JSONMessage(true, $spotlightForm->fetch($request));
00271          return $json->getString();
00272       }
00273    }
00274 
00281    function deleteSpotlight($args, &$request) {
00282 
00283       // Identify the entry to be deleted
00284       $spotlightId = $request->getUserVar('spotlightId');
00285 
00286       $spotlightDao =& DAORegistry::getDAO('SpotlightDAO');
00287       $press =& $this->getPress();
00288       $spotlight =& $spotlightDao->getById($spotlightId, $press->getId());
00289       if ($spotlight != null) { // authorized
00290 
00291          $result = $spotlightDao->deleteObject($spotlight);
00292 
00293          if ($result) {
00294             $currentUser =& $request->getUser();
00295             $notificationMgr = new NotificationManager();
00296             $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedSpotlight')));
00297             return DAO::getDataChangedEvent();
00298          } else {
00299             $json = new JSONMessage(false, __('manager.setup.errorDeletingItem'));
00300             return $json->getString();
00301          }
00302       }
00303    }
00304 
00312    function itemAutocomplete($args, &$request) {
00313 
00314       $name = $request->getUserVar('name');
00315 
00316       $press =& $this->getPress();
00317 
00318       $itemList = array();
00319 
00320       // get the items that match.
00321 
00322       $matches = array();
00323 
00324       $publishedMonographDao =& DAORegistry::getDAO('PublishedMonographDAO');
00325       $publishedMonographs =& $publishedMonographDao->getByPressId($press->getId());
00326       while ($monograph =& $publishedMonographs->next()) {
00327          if ($name == '' || preg_match('/'. preg_quote($name, '/') . '/i', $monograph->getLocalizedTitle())) {
00328             $matches[] = array('label' => $monograph->getLocalizedTitle(), 'value' => $monograph->getId() . ':' . SPOTLIGHT_TYPE_BOOK);
00329          }
00330       }
00331 
00332       if (!empty($matches)) {
00333          $itemList[] = array('label' => String::strtoupper(__('submission.workType.authoredWork')), 'value' => '');
00334          $itemList = array_merge($itemList, $matches);
00335       }
00336 
00337       $matches = array();
00338 
00339       $seriesDao =& DAORegistry::getDAO('SeriesDAO');
00340       $allSeries =& $seriesDao->getByPressId($press->getId());
00341       while ($series =& $allSeries->next()) {
00342          if ($name == '' || preg_match('/'. preg_quote($name, '/') . '/i', $series->getLocalizedTitle())) {
00343             $matches[] = array('label' => $series->getLocalizedTitle(), 'value' => $series->getId() . ':' . SPOTLIGHT_TYPE_SERIES);
00344          }
00345       }
00346 
00347       if (!empty($matches)) {
00348          $itemList[] = array('label' => String::strtoupper(__('manager.series.book')), 'value' => '');
00349          $itemList = array_merge($itemList, $matches);
00350       }
00351 
00352       $matches = array();
00353 
00354       $authorDao =& DAORegistry::getDAO('AuthorDAO');
00355       $authors =& $authorDao->getAuthorsAlphabetizedByPress($press->getId());
00356       while ($author =& $authors->next()) {
00357          if ($name == '' || preg_match('/'. preg_quote($name, '/') . '/i', $author->getFullName())) {
00358             $publishedMonograph =& $publishedMonographDao->getById($author->getSubmissionId());
00359             if ($publishedMonograph) { // only include Authors if they are authors on published monographs.
00360                $matches[] = array('label' => $author->getFullName() . ' (' . $publishedMonograph->getLocalizedTitle() . ')', 'value' => $author->getId() . ':' . SPOTLIGHT_TYPE_AUTHOR);
00361             }
00362          }
00363       }
00364 
00365       if (!empty($matches)) {
00366          $itemList[] = array('label' => String::strtoupper(__('user.role.author')), 'value' => '');
00367          $itemList = array_merge($itemList, $matches);
00368       }
00369 
00370       if (count($itemList) == 0) {
00371          return $this->noAutocompleteResults();
00372       }
00373 
00374       $json = new JSONMessage(true, $itemList);
00375       return $json->getString();
00376    }
00377 }
00378 
00379 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1