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

controllers/grid/catalogEntry/RepresentativesGridHandler.inc.php

00001 <?php
00002 
00015 // import grid base classes
00016 import('lib.pkp.classes.controllers.grid.CategoryGridHandler');
00017 
00018 
00019 // import format grid specific classes
00020 import('controllers.grid.catalogEntry.RepresentativesGridCellProvider');
00021 import('controllers.grid.catalogEntry.RepresentativesGridCategoryRow');
00022 import('controllers.grid.catalogEntry.RepresentativesGridRow');
00023 
00024 // Link action & modal classes
00025 import('lib.pkp.classes.linkAction.request.AjaxModal');
00026 
00027 class RepresentativesGridHandler extends CategoryGridHandler {
00029    var $_monograph;
00030 
00034    function RepresentativesGridHandler() {
00035       parent::CategoryGridHandler();
00036       $this->addRoleAssignment(
00037             array(ROLE_ID_PRESS_MANAGER),
00038             array('fetchGrid', 'fetchCategory', 'fetchRow', 'addRepresentative', 'editRepresentative',
00039             'updateRepresentative', 'deleteRepresentative'));
00040    }
00041 
00042 
00043    //
00044    // Getters/Setters
00045    //
00050    function &getMonograph() {
00051       return $this->_monograph;
00052    }
00053 
00058    function setMonograph($monograph) {
00059       $this->_monograph =& $monograph;
00060    }
00061 
00062 
00063    //
00064    // Overridden methods from PKPHandler
00065    //
00072    function authorize(&$request, $args, $roleAssignments) {
00073       import('classes.security.authorization.OmpSubmissionAccessPolicy');
00074       $this->addPolicy(new OmpSubmissionAccessPolicy($request, $args, $roleAssignments));
00075       return parent::authorize($request, $args, $roleAssignments);
00076    }
00077 
00078    /*
00079     * Configure the grid
00080     * @param $request PKPRequest
00081     */
00082    function initialize(&$request) {
00083       parent::initialize($request);
00084 
00085       // Retrieve the authorized monograph.
00086       $this->setMonograph($this->getAuthorizedContextObject(ASSOC_TYPE_MONOGRAPH));
00087 
00088       $representativeId = (int) $request->getUserVar('representativeId'); // set if editing or deleting a representative entry
00089 
00090       if ($representativeId != '') {
00091          $representativeDao =& DAORegistry::getDAO('RepresentativeDAO');
00092          $representative =& $representativeDao->getById($representativeId, $this->getMonograph()->getId());
00093          if (!isset($representative)) {
00094             fatalError('Representative referenced outside of authorized monograph context!');
00095          }
00096       }
00097 
00098       // Load submission-specific translations
00099       AppLocale::requireComponents(
00100          LOCALE_COMPONENT_OMP_SUBMISSION,
00101          LOCALE_COMPONENT_PKP_SUBMISSION,
00102          LOCALE_COMPONENT_PKP_USER,
00103          LOCALE_COMPONENT_OMP_DEFAULT_SETTINGS
00104       );
00105 
00106       // Basic grid configuration
00107       $this->setTitle('grid.catalogEntry.representatives');
00108 
00109       // Grid actions
00110       $router =& $request->getRouter();
00111       $actionArgs = $this->getRequestArgs();
00112       $this->addAction(
00113          new LinkAction(
00114             'addRepresentative',
00115             new AjaxModal(
00116                $router->url($request, null, null, 'addRepresentative', null, $actionArgs),
00117                __('grid.action.addRepresentative'),
00118                'modal_add_item'
00119             ),
00120             __('grid.action.addRepresentative'),
00121             'add_item'
00122          )
00123       );
00124 
00125       // Columns
00126       $cellProvider = new RepresentativesGridCellProvider();
00127       $this->addColumn(
00128          new GridColumn(
00129             'name',
00130             'grid.catalogEntry.representativeName',
00131             null,
00132             'controllers/grid/gridCell.tpl',
00133             $cellProvider
00134          )
00135       );
00136       $this->addColumn(
00137          new GridColumn(
00138             'role',
00139             'grid.catalogEntry.representativeRole',
00140             null,
00141             'controllers/grid/gridCell.tpl',
00142             $cellProvider
00143          )
00144       );
00145    }
00146 
00147 
00148    //
00149    // Overridden methods from GridHandler
00150    //
00155    function &getRowInstance() {
00156       $row = new RepresentativesGridRow($this->getMonograph());
00157       return $row;
00158    }
00159 
00164    function &getCategoryRowInstance() {
00165       $row = new RepresentativesGridCategoryRow();
00166       return $row;
00167    }
00168 
00172    function getCategoryData($category) {
00173       $representativeDao =& DAORegistry::getDAO('RepresentativeDAO');
00174       $representatives = null;
00175       if ($category['isSupplier']) {
00176          $representatives =& $representativeDao->getSuppliersByMonographId($this->getMonograph()->getId());
00177       } else {
00178          $representatives =& $representativeDao->getAgentsByMonographId($this->getMonograph()->getId());
00179       }
00180       return $representatives->toAssociativeArray();
00181    }
00182 
00186    function getCategoryRowIdParameterName() {
00187       return 'representativeCategoryId';
00188    }
00189 
00193    function getRequestArgs() {
00194       $monograph = $this->getMonograph();
00195       return array_merge(
00196          parent::getRequestArgs(),
00197          array('monographId' => $monograph->getId())
00198       );
00199    }
00200 
00204    function loadData($request, $filter = null) {
00205       // set our labels for the two Representative categories
00206       $categories = array(
00207             array('name' => 'grid.catalogEntry.agentsCategory', 'isSupplier' => false),
00208             array('name' => 'grid.catalogEntry.suppliersCategory', 'isSupplier' => true)
00209          );
00210 
00211       return $categories;
00212    }
00213 
00214 
00215    //
00216    // Public Representatives Grid Actions
00217    //
00218 
00219    function addRepresentative($args, $request) {
00220       return $this->editRepresentative($args, $request);
00221    }
00222 
00229    function editRepresentative($args, &$request) {
00230       // Identify the representative entry to be updated
00231       $representativeId = (int) $request->getUserVar('representativeId');
00232       $monograph =& $this->getMonograph();
00233 
00234       $representativeDao =& DAORegistry::getDAO('RepresentativeDAO');
00235       $representative = $representativeDao->getById($representativeId, $monograph->getId());
00236 
00237       // Form handling
00238       import('controllers.grid.catalogEntry.form.RepresentativeForm');
00239       $representativeForm = new RepresentativeForm($monograph, $representative);
00240       $representativeForm->initData();
00241 
00242       $json = new JSONMessage(true, $representativeForm->fetch($request));
00243       return $json->getString();
00244    }
00245 
00252    function updateRepresentative($args, &$request) {
00253       // Identify the representative entry to be updated
00254       $representativeId = $request->getUserVar('representativeId');
00255       $monograph =& $this->getMonograph();
00256 
00257       $representativeDao =& DAORegistry::getDAO('RepresentativeDAO');
00258       $representative = $representativeDao->getById($representativeId, $monograph->getId());
00259 
00260       // Form handling
00261       import('controllers.grid.catalogEntry.form.RepresentativeForm');
00262       $representativeForm = new RepresentativeForm($monograph, $representative);
00263       $representativeForm->readInputData();
00264       if ($representativeForm->validate()) {
00265          $representativeId = $representativeForm->execute();
00266 
00267          if(!isset($representative)) {
00268             // This is a new entry
00269             $representative =& $representativeDao->getById($representativeId, $monograph->getId());
00270             // New added entry action notification content.
00271             $notificationContent = __('notification.addedRepresentative');
00272          } else {
00273             // entry edit action notification content.
00274             $notificationContent = __('notification.editedRepresentative');
00275          }
00276 
00277          // Create trivial notification.
00278          $currentUser =& $request->getUser();
00279          $notificationMgr = new NotificationManager();
00280          $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => $notificationContent));
00281 
00282          // Prepare the grid row data
00283          $row =& $this->getRowInstance();
00284          $row->setGridId($this->getId());
00285          $row->setId($representativeId);
00286          $row->setData($representative);
00287          $row->initialize($request);
00288 
00289          // Render the row into a JSON response
00290          return DAO::getDataChangedEvent($representativeId, (int) $representative->getIsSupplier());
00291 
00292       } else {
00293          $json = new JSONMessage(true, $representativeForm->fetch($request));
00294          return $json->getString();
00295       }
00296    }
00297 
00304    function deleteRepresentative($args, &$request) {
00305 
00306       // Identify the representative entry to be deleted
00307       $representativeId = $request->getUserVar('representativeId');
00308 
00309       $representativeDao =& DAORegistry::getDAO('RepresentativeDAO');
00310       $representative =& $representativeDao->getById($representativeId, $this->getMonograph()->getId());
00311       if ($representative != null) { // authorized
00312 
00313          $result = $representativeDao->deleteObject($representative);
00314 
00315          if ($result) {
00316             $currentUser =& $request->getUser();
00317             $notificationMgr = new NotificationManager();
00318             $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array('contents' => __('notification.removedRepresentative')));
00319             return DAO::getDataChangedEvent($representative->getId(), (int) $representative->getIsSupplier());
00320          } else {
00321             $json = new JSONMessage(false, __('manager.setup.errorDeletingItem'));
00322             return $json->getString();
00323          }
00324       }
00325    }
00326 }
00327 
00328 ?>

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