/**
  * Clear all messages
  *
  * @return boolean True if messages were cleared, false if none existed
  */
 public function clearMessages()
 {
     if ($this->hasMessages()) {
         self::$_messages = array();
         return true;
     }
     return false;
 }
 public function init()
 {
     $options = $this->getOptions();
     $messenger = WarningsMessenger::getInstance();
     if (isset($options['avoidModule'])) {
         $modules = $options['avoidModule'];
         $module = Zend_Controller_Front::getInstance()->getRequest()->getModuleName();
         $data = explode(',', $modules);
         foreach ($data as $candidate) {
             if ($candidate == $module) {
                 $messenger->avoidModule();
                 break;
             }
         }
     }
     return $messenger;
 }
 public function setUp()
 {
     $this->_messenger = WarningsMessenger::getInstance();
     $this->_messenger->clearMessages();
 }
 /**
  * Update a servicePack identified by its Id
  */
 public function putAction()
 {
     $data = $this->_helper->requestData(true);
     $data = $this->_helper->filter($data)->blacklist(array('lifeCycleId', 'restrictionsId', 'tariffPlanServicesId', 'tariffPlanServices.zonePlanId', 'published'));
     $spId = $this->_getParam('id');
     $servicePack = $this->_spSrv->load($spId);
     if (empty($servicePack)) {
         throw new NotFoundException("ServicePack {$spId} not found", 404);
     }
     // If servicepack is published, only allow modify tariffs, discounts, name or maxSubscription
     if ($servicePack->isPublished()) {
         if (isset($data['tariffPlanLifeCycle']) && isset($data['tariffPlanLifeCycle']['modified']) && isset($data['tariffPlanLifeCycle']['id']) && $data['tariffPlanLifeCycle']['modified'] === true) {
             $id = $data['tariffPlanLifeCycle']['id'];
             $tariffPlan = $this->_tariffSrv->load(TariffPlanService::TYPE_LIFECYCLE, $id);
             if (empty($tariffPlan)) {
                 throw new NotFoundException("Tariff plan {$id} not found", 404);
             }
             $this->_helper->allowed('update', $tariffPlan);
             $tariffPlan->importData($data['tariffPlanLifeCycle']);
             $result['tariffPlanLifeCycle'] = $this->_tariffSrv->update($tariffPlan);
         }
         if (isset($data['lifeCycle']) && isset($data['lifeCycle']['modified']) && isset($data['lifeCycle']['id']) && $data['lifeCycle']['modified'] === true) {
             $id = $data['lifeCycle']['id'];
             $lifeCycle = $this->_lifeCycleSrv->load($id);
             if (empty($lifeCycle)) {
                 throw new NotFoundException("Life Cycle {$id} not found", 404);
             }
             $lifeCycle->importData($data['lifeCycle']);
             $lifeCycle->published = $servicePack->published;
             $result['lifeCycle'] = $this->_lifeCycleSrv->update($lifeCycle);
         }
         if (isset($data['tariffPlanServices']) && isset($data['tariffPlanServices']['modified']) && isset($data['tariffPlanServices']['id']) && $data['tariffPlanServices']['modified'] === true) {
             $id = $data['tariffPlanServices']['id'];
             $tariffPlan = $this->_tariffSrv->load(TariffPlanService::TYPE_SERVICES, $id);
             if (empty($tariffPlan)) {
                 throw new NotFoundException("Tariff plan {$id} not found", 404);
             }
             $this->_helper->allowed('update', $tariffPlan);
             $tariffPlan->importData($data['tariffPlanServices']);
             $tariffPlan->published = $servicePack->published;
             $result['tariffPlanServices'] = $this->_tariffSrv->update($tariffPlan);
         }
         if (isset($data['restrictions']) && isset($data['restrictions']['modified']) && isset($data['restrictions']['id']) && $data['restrictions']['modified'] === true) {
             $id = $data['restrictions']['id'];
             $restrictions = $this->_restrictionsSrv->load($id);
             if (empty($restrictions)) {
                 throw new NotFoundException("Restrictions {$id} not found", 404);
             }
             $restrictions->importData($data['restrictions']);
             if ($restrictions->restrictionsModified) {
                 unset($restrictions->restrictionsModified);
             }
             $restrictions->published = $servicePack->published;
             $steeringListService = SteeringListService::getInstance();
             try {
                 $result['restrictions'] = $this->_restrictionsSrv->update($restrictions);
             } catch (\Application\Model\Mapper\Exception\EricssonException $e) {
                 $stOrig = $steeringListService->loadByServicePack($servicePack->id);
                 if ($e->getHttpCode() != 304 || $stOrig->steeringType == $restrictions->steering->steeringType && $stOrig->useCustomOrder == $restrictions->steering->useCustomOrder && $stOrig->steeringList == $restrictions->operator) {
                     throw $e;
                 }
                 $warn_mess = WarningsMessenger::getInstance();
                 $warn_mess->avoidModule();
             }
             $steeringListService->applySteering($restrictions, $servicePack->id);
         }
         if (isset($data['discounts'])) {
             if (isset($data['discounts']['discounts']) && $data['discounts']['modified'] === true) {
                 $this->_helper->allowed('update_discounts', $servicePack);
                 $servicePack->setDiscounts($data['discounts']['discounts']);
             } else {
                 $servicePack->setDiscounts($data['discounts']);
             }
             $result['discounts'] = $this->_spSrv->setDiscounts($servicePack);
         }
         if (isset($data['voucherOnlyDiscounts'])) {
             if (isset($data['voucherOnlyDiscounts']['discounts']) && $data['voucherOnlyDiscounts']['modified'] === true) {
                 $this->_helper->allowed('update_voucher_only_discounts', $servicePack);
                 $servicePack->setVoucherOnlyDiscounts($data['voucherOnlyDiscounts']['discounts']);
             } else {
                 $servicePack->setVoucherOnlyDiscounts($data['voucherOnlyDiscounts']);
             }
             $result['voucherOnlyDiscounts'] = $this->_spSrv->setVoucherOnlyDiscounts($servicePack);
         }
         $update = array();
         if (isset($data['name']) && $data['name'] != $servicePack->getName()) {
             $update['name'] = $data['name'];
         }
         if (isset($data['maxSubscriptions'])) {
             $update['maxSubscriptions'] = $data['maxSubscriptions'];
         }
         if (!empty($update)) {
             $servicePack->importData($update);
             $result['servicePack'] = $this->_spSrv->update($servicePack);
         }
         $this->view->data = isset($result) ? $result : array();
     } else {
         if (isset($data['tariffPlanServices']['currency'])) {
             $data['currency'] = $data['tariffPlanServices']['currency'];
         } else {
             if (isset($data['tariffPlanLifeCycle']['currency'])) {
                 $data['currency'] = $data['tariffPlanLifeCycle']['currency'];
             }
         }
         // Make sure that operator is not an empty array
         if (isset($data['restrictions']['operator']) && empty($data['restrictions']['operator'])) {
             unset($data['restrictions']['operator']);
         }
         if (isset($data['restrictions']) && isset($data['restrictions']['modified']) && isset($data['restrictions']['id']) && $data['restrictions']['modified'] === true) {
             $id = $data['restrictions']['id'];
             $restrictions = $this->_restrictionsSrv->load($id);
             if (empty($restrictions)) {
                 throw new NotFoundException("Restrictions {$id} not found", 404);
             }
             $restrictions->importData($data['restrictions']);
             $steeringListService = SteeringListService::getInstance();
             $steeringListService->applySteering($restrictions, $servicePack->id);
         }
         $servicePack->importData($data);
         // Check if it's allowed
         $this->_helper->allowed('update', $servicePack);
         // Update the service pack via its service
         $result = $this->_spSrv->update($servicePack);
         $this->view->data = $result;
     }
 }