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

controllers/grid/catalogEntry/MarketsGridHandler.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.catalogEntry.MarketsGridCellProvider');
00021 import('controllers.grid.catalogEntry.MarketsGridRow');
00022 
00023 // Link action & modal classes
00024 import('lib.pkp.classes.linkAction.request.AjaxModal');
00025 
00026 class MarketsGridHandler extends GridHandler {
00028    var $_monograph;
00029 
00031    var $_publicationFormat;
00032 
00036    function MarketsGridHandler() {
00037       parent::GridHandler();
00038       $this->addRoleAssignment(
00039             array(ROLE_ID_PRESS_MANAGER),
00040             array('fetchGrid', 'fetchRow', 'addMarket', 'editMarket',
00041             'updateMarket', 'deleteMarket'));
00042    }
00043 
00044 
00045    //
00046    // Getters/Setters
00047    //
00052    function &getMonograph() {
00053       return $this->_monograph;
00054    }
00055 
00060    function setMonograph($monograph) {
00061       $this->_monograph =& $monograph;
00062    }
00063 
00068    function &getPublicationFormat() {
00069       return $this->_publicationFormat;
00070    }
00071 
00076    function setPublicationFormat($publicationFormat) {
00077       $this->_publicationFormat =& $publicationFormat;
00078    }
00079 
00080    //
00081    // Overridden methods from PKPHandler
00082    //
00089    function authorize(&$request, $args, $roleAssignments) {
00090       import('classes.security.authorization.OmpSubmissionAccessPolicy');
00091       $this->addPolicy(new OmpSubmissionAccessPolicy($request, $args, $roleAssignments));
00092       return parent::authorize($request, $args, $roleAssignments);
00093    }
00094 
00095    /*
00096     * Configure the grid
00097     * @param $request PKPRequest
00098     */
00099    function initialize(&$request) {
00100       parent::initialize($request);
00101 
00102       // Retrieve the authorized monograph.
00103       $this->setMonograph($this->getAuthorizedContextObject(ASSOC_TYPE_MONOGRAPH));
00104       $publicationFormatDao =& DAORegistry::getDAO('PublicationFormatDAO');
00105       $publicationFormatId = null;
00106 
00107       // Retrieve the associated publication format for this grid.
00108       $marketId = (int) $request->getUserVar('marketId'); // set if editing or deleting a market entry
00109 
00110       if ($marketId != '') {
00111          $marketDao =& DAORegistry::getDAO('MarketDAO');
00112          $market =& $marketDao->getById($marketId, $this->getMonograph()->getId());
00113          if ($market) {
00114             $publicationFormatId = $market->getPublicationFormatId();
00115          }
00116       } else { // empty form for new Market
00117          $publicationFormatId = (int) $request->getUserVar('publicationFormatId');
00118       }
00119 
00120       $publicationFormat =& $publicationFormatDao->getById($publicationFormatId, $this->getMonograph()->getId());
00121 
00122       if ($publicationFormat) {
00123          $this->setPublicationFormat($publicationFormat);
00124       } else {
00125          fatalError('The publication format is not assigned to authorized monograph!');
00126       }
00127 
00128       // Load submission-specific translations
00129       AppLocale::requireComponents(
00130          LOCALE_COMPONENT_OMP_SUBMISSION,
00131          LOCALE_COMPONENT_PKP_SUBMISSION,
00132          LOCALE_COMPONENT_PKP_USER,
00133          LOCALE_COMPONENT_OMP_DEFAULT_SETTINGS
00134       );
00135 
00136       // Basic grid configuration
00137       $this->setTitle('grid.catalogEntry.markets');
00138 
00139       // Grid actions
00140       $router =& $request->getRouter();
00141       $actionArgs = $this->getRequestArgs();
00142       $this->addAction(
00143          new LinkAction(
00144             'addMarket',
00145             new AjaxModal(
00146                $router->url($request, null, null, 'addMarket', null, $actionArgs),
00147                __('grid.action.addMarket'),
00148                'modal_add_item'
00149             ),
00150             __('grid.action.addMarket'),
00151             'add_item'
00152          )
00153       );
00154 
00155       // Columns
00156       $cellProvider = new MarketsGridCellProvider();
00157       $this->addColumn(
00158          new GridColumn(
00159             'territory',
00160             'grid.catalogEntry.marketTerritory',
00161             null,
00162             'controllers/grid/gridCell.tpl',
00163             $cellProvider
00164          )
00165       );
00166       $this->addColumn(
00167          new GridColumn(
00168             'rep',
00169             'grid.catalogEntry.representatives',
00170             null,
00171             'controllers/grid/gridCell.tpl',
00172             $cellProvider
00173          )
00174       );
00175       $this->addColumn(
00176          new GridColumn(
00177             'price',
00178             'monograph.publicationFormat.price',
00179             null,
00180             'controllers/grid/gridCell.tpl',
00181             $cellProvider
00182          )
00183       );
00184    }
00185 
00186 
00187    //
00188    // Overridden methods from GridHandler
00189    //
00194    function &getRowInstance() {
00195       $row = new MarketsGridRow($this->getMonograph());
00196       return $row;
00197    }
00198 
00204    function getRequestArgs() {
00205       $monograph =& $this->getMonograph();
00206       $publicationFormat =& $this->getPublicationFormat();
00207 
00208       return array(
00209          'monographId' => $monograph->getId(),
00210          'publicationFormatId' => $publicationFormat->getId()
00211       );
00212    }
00213 
00217    function &loadData($request, $filter = null) {
00218       $publicationFormat =& $this->getPublicationFormat();
00219       $marketDao =& DAORegistry::getDAO('MarketDAO');
00220       $data =& $marketDao->getByPublicationFormatId($publicationFormat->getId());
00221       return $data->toArray();
00222    }
00223 
00224 
00225    //
00226    // Public  Market Grid Actions
00227    //
00228 
00229    function addMarket($args, $request) {
00230       return $this->editMarket($args, $request);
00231    }
00232 
00239    function editMarket($args, &$request) {
00240       // Identify the market entry to be updated
00241       $marketId = (int) $request->getUserVar('marketId');
00242       $monograph =& $this->getMonograph();
00243 
00244       $marketDao =& DAORegistry::getDAO('MarketDAO');
00245       $market = $marketDao->getById($marketId, $monograph->getId());
00246 
00247       // Form handling
00248       import('controllers.grid.catalogEntry.form.MarketForm');
00249       $marketForm = new MarketForm($monograph, $market);
00250       $marketForm->initData();
00251 
00252       $json = new JSONMessage(true, $marketForm->fetch($request));
00253       return $json->getString();
00254    }
00255 
00262    function updateMarket($args, &$request) {
00263       // Identify the market entry to be updated
00264       $marketId = $request->getUserVar('marketId');
00265       $monograph =& $this->getMonograph();
00266 
00267       $marketDao =& DAORegistry::getDAO('MarketDAO');
00268       $market = $marketDao->getById($marketId, $monograph->getId());
00269 
00270       // Form handling
00271       import('controllers.grid.catalogEntry.form.MarketForm');
00272       $marketForm = new MarketForm($monograph, $market);
00273       $marketForm->readInputData();
00274       if ($marketForm->validate()) {
00275          $marketId = $marketForm->execute();
00276 
00277          if(!isset($market)) {
00278             // This is a new entry
00279             $market =& $marketDao->getById($marketId, $monograph->getId());
00280             // New added entry action notification content.
00281             $notificationContent = __('notification.addedMarket');
00282          } else {
00283             // entry edit action notification content.
00284             $notificationContent = __('notification.editedMarket');
00285          }
00286 
00287          // Create trivial notification.
00288          $currentUser =& $request->getUser();
00289          $notificationMgr = new NotificationManager();
00290          $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $notificationContent));
00291 
00292          // Prepare the grid row data
00293          $row =& $this->getRowInstance();
00294          $row->setGridId($this->getId());
00295          $row->setId($marketId);
00296          $row->setData($market);
00297          $row->initialize($request);
00298 
00299          // Render the row into a JSON response
00300          return DAO::getDataChangedEvent();
00301 
00302       } else {
00303          $json = new JSONMessage(true, $marketForm->fetch($request));
00304          return $json->getString();
00305       }
00306    }
00307 
00314    function deleteMarket($args, &$request) {
00315 
00316       // Identify the markets entry to be deleted
00317       $marketId = $request->getUserVar('marketId');
00318 
00319       $marketDao =& DAORegistry::getDAO('MarketDAO');
00320       $market =& $marketDao->getById($marketId, $this->getMonograph()->getId());
00321       if ($market != null) { // authorized
00322 
00323          $result = $marketDao->deleteObject($market);
00324 
00325          if ($result) {
00326             $currentUser =& $request->getUser();
00327             $notificationMgr = new NotificationManager();
00328             $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedMarket')));
00329             return DAO::getDataChangedEvent();
00330          } else {
00331             $json = new JSONMessage(false, __('manager.setup.errorDeletingItem'));
00332             return $json->getString();
00333          }
00334       }
00335    }
00336 }
00337 
00338 ?>

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