/**
  * Validate element value
  *
  * If a translation adapter is registered, any error messages will be
  * translated according to the current locale, using the given error code;
  * if no matching translation is found, the original message will be
  * utilized.
  *
  * Note: The *filtered* value is validated.
  *
  * @param  array   $data
  * @param  mixed   $context
  * @return boolean
  */
 public function isValid($value, $context = null)
 {
     $fields = array('__parent.zonePlanId', '__parent.zonePlan');
     if ($value == "-1") {
         return true;
     }
     foreach ($fields as $field) {
         $data = \App_Util_Array::getItem($context, $field);
         if (!empty($data)) {
             if (is_string($data)) {
                 $zonePlan = \Application\Service\ZonePlanService::getInstance()->load($data);
                 if (!$zonePlan) {
                     $this->_error(self::NOT_EXISTS_ZONE_PLAN, $data);
                     return false;
                 }
             } else {
                 if (is_array($data)) {
                     $zonePlan = new ZonePlanModel($data);
                 } else {
                     $this->_error(self::INVALID_DATA_TYPE);
                     return false;
                 }
             }
             //TODO: workaround
             /*
              * For the post verb, they only send the modified zones in the normal object (modified are only readonly for get verb)
              * Now is not mandatory to send current items although they zones has been removed.
              */
             //
             //                if ($zonePlan->zonePlanModified) {
             //                    $zoneGroup = $zonePlan->zonePlanModified->getZoneGroupById($value);
             //                } else {
             //                    $zoneGroup = $zonePlan->getZoneGroupById($value);
             //                }
             //                if (($zoneGroup !== NULL) && (!$zoneGroup->isDeleted)){
             //                    return true;
             //                }
             //
             //                $this->_error(self::NOT_ZONE_GROUP_DEFINED, $value);
             //                return false;
             return true;
         }
     }
     $this->_error(self::NOT_ZONE_PLAN_DEFINED);
     return false;
 }
 public function isValid($value)
 {
     $this->_messages = array();
     if (!is_string($value)) {
         $message = $this->_createMessage(self::ERROR_INVALID_TYPE, $value);
         $this->_messages[self::ERROR_INVALID_TYPE] = $message;
         return false;
     }
     $service = ZonePlanService::getInstance();
     try {
         $item = $service->load($value);
     } catch (Application\Exceptions\GlobalServiceException $e) {
         $message = $this->_createMessage(self::ERROR_ON_CONNECTION, $value);
         $this->_messages[self::ERROR_ON_CONNECTION] = $message;
         return false;
     }
     if (!isset($item)) {
         $message = $this->_createMessage(self::ERROR_ZONEPLAN_NOT_FOUND, $value);
         $this->_messages[self::ERROR_ZONEPLAN_NOT_FOUND] = $message;
         return false;
     }
     return true;
 }
 public function delete($id, $type = null)
 {
     if ($id instanceof TariffPlanLifeCycleModel || $id instanceof TariffPlanServicesModel) {
         $id = $id->getId();
         if ($id instanceof TariffPlanLifeCycleModel) {
             $type = self::TYPE_LIFECYCLE;
         } else {
             $type = self::TYPE_SERVICES;
         }
     }
     if (!is_string($id) || !strlen($id)) {
         throw new AppEx\InvalidArgumentException('Supplied tariff model must have Id');
     }
     switch (strtolower($type)) {
         case self::TYPE_LIFECYCLE:
             $tariff = new TariffPlanLifeCycleModel(array('id' => $id));
             break;
         case self::TYPE_SERVICES:
             $tariff = self::load(self::TYPE_SERVICES, $id);
             $zonePlan = ZonePlanService::getInstance()->load($tariff->zonePlanId);
             $zonePlan->delete();
             break;
         default:
             throw new AppEx\InvalidArgumentException("Invalid Type given");
     }
     $tariff->delete();
     if ($tariff instanceof TariffPlanLifeCycleModel) {
         \App::audit('Deleted life cycle tariff with Id ' . $tariff->getId(), $tariff);
     } else {
         \App::audit('Deleted services tariff with Id ' . $tariff->getId(), $tariff);
     }
     return true;
 }
 /**
  * Validate element value
  *
  * If a translation adapter is registered, any error messages will be
  * translated according to the current locale, using the given error code;
  * if no matching translation is found, the original message will be
  * utilized.
  *
  * Note: The *filtered* value is validated.
  *
  * @param  array   $data
  * @param  mixed   $context
  * @return boolean
  */
 public function isValid($data, $context = null, $removeNotPresentFields = false)
 {
     if (!($data instanceof ZonePlanModel || $this->_acceptArrayAsModel && is_array($data))) {
         $this->_error(self::NOT_ZONE_PLAN);
         return false;
     }
     if (!parent::isValid($data, $context, $removeNotPresentFields)) {
         return false;
     }
     if (!$data instanceof ZonePlanModel) {
         $data = new ZonePlanModel($data);
     }
     $groups = $data->zoneGroups ?: array();
     $groupInfo = array();
     $zones = array();
     if (!$this->getIsTemplateValidation() && $data->id) {
         $orginalData = ZonePlanService::getInstance()->load($data->id);
         $zonePlanModified = $orginalData->zonePlanModified;
         if (in_array($data->published, array(ServicePackModel::STATUS_PUBLISHED, ServicePackModel::STATUS_PUBLISHED_PENDING))) {
             foreach ($orginalData->zoneGroups as $group) {
                 if ($group->isDeleted) {
                     $groupInfo[$group->id] = '';
                 }
             }
             if ($zonePlanModified) {
                 foreach ($zonePlanModified->zoneGroups as $group) {
                     if ($group->isDeleted) {
                         $groupInfo[$group->id] = '';
                     }
                 }
             }
         }
     }
     foreach ($groups as $group) {
         if (in_array($group->name, $groupInfo)) {
             $this->_error(self::REPITED_ZONE_GROUP_NAME, $group->name);
             return false;
         }
         $groupInfo[$group->id] = $group->name;
         $gZones = $group->zones;
         foreach ($gZones as $z) {
             if (in_array($z, $zones)) {
                 $this->_error(self::REPITED_ZONE, $z);
                 return false;
             }
             $zones[] = $z;
         }
     }
     if ($orginalData) {
         foreach ($orginalData->zoneGroups as $group) {
             if (!$group->isDeleted && !isset($groupInfo[$group->id])) {
                 $groupInfo[$group->id] = $group->name;
             }
         }
         if ($zonePlanModified) {
             foreach ($zonePlanModified->zoneGroups as $group) {
                 if (!$group->isDeleted && !isset($groupInfo[$group->id])) {
                     $groupInfo[$group->id] = $group->name;
                 }
             }
         }
         // This has been removed due to a problem in production. To be
         // reviewed with alfred!
         //            if (!empty($groupInfo)) {
         //                if (!isset($groupInfo[1])) {
         //                    $this->_error(self::NOT_ZONE_GROUP_ID_BASED_ON_1);
         //                    return false;
         //                }
         //            }
         if (!$this->getIsTemplateValidation() && in_array($data->published, array(ServicePackModel::STATUS_PUBLISHED, ServicePackModel::STATUS_PUBLISHED_PENDING))) {
             if ($orginalData && count($groupInfo) < count($orginalData->zoneGroups)) {
                 $this->_error(self::NOT_SAME_NUMBER_OF_ZONES);
                 return false;
             }
         }
     }
     return true;
 }