/**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * Currently only works with creating new and creating new related models. Not designed to support updating
  * existing models.
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param $attribute
  * @throws FailedToResolveExplicitReadWriteModelPermissionsException
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($adapter->getModel()->id < 0) {
         throw new NotSupportedException();
     }
     if ($this->type == self::TYPE_DYNAMIC_SAME_AS_TRIGGERED_MODEL) {
         $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($adapter->getTriggeredModel());
     } elseif ($this->type == self::TYPE_DYNAMIC_OWNER) {
         //Do nothing, by default this will take.
         return;
     } elseif ($this->type == self::TYPE_DYNAMIC_EVERYONE_GROUP) {
         $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($adapter->getModel());
         $explicitReadWriteModelPermissions->addReadWritePermitable(Group::getByName(Group::EVERYONE_GROUP_NAME));
     } else {
         $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::makeBySecurableItem($adapter->getModel());
         try {
             $group = Group::getById((int) $this->type);
             $explicitReadWriteModelPermissions->addReadWritePermitable($group);
         } catch (NotFoundException $e) {
             //todo: handle exception better
             return;
         }
     }
     $success = ExplicitReadWriteModelPermissionsUtil::resolveExplicitReadWriteModelPermissions($adapter->getModel(), $explicitReadWriteModelPermissions);
     if (!$success) {
         throw new FailedToResolveExplicitReadWriteModelPermissionsException();
     }
 }
 /**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param string $attribute
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($this->type == WorkflowActionAttributeForm::TYPE_STATIC) {
         $adapter->getModel()->{$attribute} = ContactState::getById((int) $this->value);
         //todo: what if it doesn't exist anymore?
     } else {
         throw new NotSupportedException();
     }
 }
 /**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param $attribute
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($this->type == static::TYPE_STATIC) {
         $adapter->getModel()->{$attribute}->values->removeAll();
         $value = new CustomFieldValue();
         $value->value = $this->value;
         $adapter->getModel()->{$attribute}->values->add($value);
     } else {
         throw new NotSupportedException();
     }
 }
 /**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * If the current value is null and the type is TYPE_DYNAMIC_STEP_FORWARD_OR_BACKWARDS then something is wrong
  * so this should not be processed. the step forward/backwards should only be processed on existing models
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param $attribute
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($this->type == static::TYPE_STATIC) {
         $adapter->getModel()->{$attribute}->value = $this->value;
     } elseif ($this->type == static::TYPE_DYNAMIC_STEP_FORWARD_OR_BACKWARDS) {
         if ($adapter->getModel()->{$attribute}->value != null) {
             $data = unserialize($adapter->getModel()->{$attribute}->data->serializedData);
             if (false !== ($key = array_search($adapter->getModel()->{$attribute}->value, $data))) {
                 $newKey = $key + $this->value;
                 if (isset($data[$newKey])) {
                     $adapter->getModel()->{$attribute}->value = $data[$newKey];
                 }
             }
         }
     } else {
         throw new NotSupportedException();
     }
 }
 /**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param $attribute
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($this->type == static::TYPE_STATIC) {
         $adapter->getModel()->{$attribute} = $this->value;
     } elseif ($this->type == self::TYPE_DYNAMIC_FROM_TRIGGERED_DATETIME) {
         $newTimeStamp = $this->resolveNewTimeStampForDuration(time());
         $adapter->getModel()->{$attribute} = DateTimeUtil::convertTimestampToDbFormatDateTime($newTimeStamp);
     } elseif ($this->type == self::TYPE_DYNAMIC_FROM_EXISTING_DATETIME) {
         if (!DateTimeUtil::isDateTimeStringNull($adapter->getModel()->{$attribute})) {
             $existingTimeStamp = DateTimeUtil::convertDbFormatDateTimeToTimestamp($adapter->getModel()->{$attribute});
             $newTimeStamp = $this->resolveNewTimeStampForDuration($existingTimeStamp);
             $newDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($newTimeStamp);
             $adapter->getModel()->{$attribute} = $newDateTime;
         }
     } else {
         throw new NotSupportedException();
     }
 }
 /**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param $attribute
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($this->type == static::TYPE_STATIC) {
         $adapter->getModel()->{$attribute}->value = $this->value;
         $adapter->getModel()->{$attribute}->currency = Currency::getById((int) $this->currencyId);
     } else {
         throw new NotSupportedException();
     }
 }
 /**
  * Utilized to create or update model attribute values after a workflow's triggers are fired as true.
  * @param WorkflowActionProcessingModelAdapter $adapter
  * @param $attribute
  * @throws NotSupportedException
  */
 public function resolveValueAndSetToModel(WorkflowActionProcessingModelAdapter $adapter, $attribute)
 {
     assert('is_string($attribute)');
     if ($this->type == WorkflowActionAttributeForm::TYPE_STATIC) {
         $adapter->getModel()->{$attribute} = User::getById((int) $this->value);
     } elseif ($this->type == self::TYPE_DYNAMIC_OWNER_OF_TRIGGERED_MODEL) {
         if ($adapter->getTriggeredModel() instanceof OwnedSecurableItem) {
             $adapter->getModel()->{$attribute} = $adapter->getTriggeredModel()->owner;
         }
     } elseif ($this->type == self::TYPE_DYNAMIC_CREATED_BY_USER) {
         $adapter->getModel()->{$attribute} = $adapter->getTriggeredModel()->createdByUser;
     } elseif ($this->type == self::TYPE_DYNAMIC_MODIFIED_BY_USER) {
         $adapter->getModel()->{$attribute} = $adapter->getTriggeredModel()->modifiedByUser;
     } elseif ($this->type == self::TYPE_DYNAMIC_TRIGGERED_BY_USER) {
         $adapter->getModel()->{$attribute} = $adapter->getTriggeredByUser();
     } else {
         throw new NotSupportedException();
     }
 }
 protected function resolveValueForMergeTags($value, WorkflowActionProcessingModelAdapter $adapter)
 {
     $mergeTagsUtil = MergeTagsUtilFactory::make(EmailTemplate::TYPE_WORKFLOW, Yii::app()->languageHelper->getByUser($adapter->getTriggeredByUser()), $value);
     if (false === ($resolvedValue = $mergeTagsUtil->resolveMergeTags($adapter->getTriggeredModel()))) {
         return $value;
     }
     return $resolvedValue;
 }