public function testValidateAttributeWithUniqueValidator()
 {
     $trigger = new TriggerForWorkflowForm('AccountsModule', 'Account', Workflow::TYPE_ON_SAVE);
     $trigger->attributeIndexOrDerivedType = 'owner___username';
     $trigger->setOperator(OperatorRules::TYPE_CHANGES);
     $trigger->value = 'jason';
     $validated = $trigger->validate();
     $this->assertTrue($validated);
     $this->assertCount(0, $trigger->getErrors());
 }
 /**
  * For a time trigger, the value must first 'change'.  If the operator is TYPE_DOES_NOT_CHANGE, then we can
  * assume true since any 'change' pushes out the time expiration.  If the value does 'change', then the
  * operator can be evaluated normally.
  * @param RedBeanModel $model
  * @param $attribute
  * @param $changeRequiredToProcess - if a change in value is required to confirm the time trigger is true
  * @return bool
  */
 public function evaluateTimeTriggerBeforeSave(RedBeanModel $model, $attribute, $changeRequiredToProcess = true)
 {
     assert('is_string($attribute)');
     assert('is_bool($changeRequiredToProcess)');
     if (array_key_exists($attribute, $model->originalAttributeValues) || !$changeRequiredToProcess) {
         if ($this->trigger->getOperator() == OperatorRules::TYPE_DOES_NOT_CHANGE) {
             return true;
         }
         return $this->evaluateBeforeSave($model, $attribute);
     }
     return false;
 }
 /**
  * @param TriggerForWorkflowForm $trigger
  * @return TriggerRules
  * @throws NotSupportedException
  */
 public static function createTriggerRulesByTrigger(TriggerForWorkflowForm $trigger)
 {
     $type = $trigger->getValueEvaluationType();
     $rulesClassName = $type . 'TriggerRules';
     if (in_array($type, array('Email', 'Phone', 'Text', 'TextArea', 'Url'))) {
         return new TextTriggerRules($trigger);
     } elseif ($type == 'DropDown' || $type == 'RadioDropDown') {
         return new DropDownTriggerRules($trigger);
     } elseif ($type == 'MultiSelectDropDown' || $type == 'TagCloud') {
         return new MultiSelectDropDownTriggerRules($trigger);
     } elseif (@class_exists($rulesClassName)) {
         return new $rulesClassName($trigger);
     } else {
         throw new NotSupportedException($type);
     }
 }
 /**
  * @return array
  */
 public function rules()
 {
     return array_merge(parent::rules(), array(array('durationInterval', 'type', 'type' => 'integer'), array('durationInterval', 'numerical', 'min' => 0), array('durationInterval', 'required'), array('durationSign', 'type', 'type' => 'string'), array('durationSign', 'required'), array('durationType', 'type', 'type' => 'string'), array('durationType', 'required')));
 }
 /**
  * @return string
  */
 public static function getTreeType()
 {
     return TriggerForWorkflowForm::getType();
 }
 /**
  * @param Workflow $workflow
  * @param TriggerForWorkflowForm $trigger
  * @param RedBeanModel $model
  * @return bool
  * @throws NotSupportedException
  */
 protected static function isTriggerTrueByModel(Workflow $workflow, TriggerForWorkflowForm $trigger, RedBeanModel $model)
 {
     if ($trigger->getAttribute() == null) {
         $attributeAndRelationData = $trigger->getAttributeAndRelationData();
         if (count($attributeAndRelationData) == 2) {
             $penultimateRelation = $trigger->getResolvedRealAttributeNameForPenultimateRelation();
             $resolvedAttribute = $trigger->getResolvedAttributeRealAttributeName();
             if ($model->{$penultimateRelation} instanceof RedBeanMutableRelatedModels) {
                 //ManyMany or HasMany
                 $penultimateModelClassName = $trigger->getResolvedAttributeModelClassName();
                 $resolvedModels = static::resolveRelatedModelsForInferredRelations($attributeAndRelationData[0], $model, $penultimateRelation, $penultimateModelClassName);
                 foreach ($resolvedModels as $resolvedModel) {
                     if (get_class($resolvedModel) == $penultimateModelClassName) {
                         if (self::resolveIsTrueByEvaluationRules($workflow, $trigger, $resolvedModel, $resolvedAttribute) && $trigger->relationFilter == TriggerForWorkflowForm::RELATION_FILTER_ANY) {
                             return true;
                         }
                     }
                 }
                 return false;
             } else {
                 $resolvedModel = $model->{$penultimateRelation};
                 return self::resolveIsTrueByEvaluationRules($workflow, $trigger, $resolvedModel, $resolvedAttribute);
             }
         } elseif (count($attributeAndRelationData) == 3) {
             $firstRelation = $trigger->getResolvedRealAttributeNameForFirstRelation();
             $resolvedAttribute = $trigger->getResolvedAttributeRealAttributeName();
             $penultimateRelation = $trigger->getResolvedRealAttributeNameForPenultimateRelation();
             if ($model->{$firstRelation} instanceof RedBeanMutableRelatedModels) {
                 //ManyMany or HasMany
                 $resolvedAttributeModelClassName = $trigger->getPenultimateModelClassName();
                 $resolvedModels = static::resolveRelatedModelsForInferredRelations($attributeAndRelationData[0], $model, $firstRelation, $resolvedAttributeModelClassName);
                 foreach ($resolvedModels as $relatedModel) {
                     if (get_class($relatedModel) == $trigger->getPenultimateModelClassName()) {
                         $resolvedModel = $relatedModel->{$penultimateRelation};
                         if (self::resolveIsTrueByEvaluationRules($workflow, $trigger, $resolvedModel, $resolvedAttribute) && $trigger->relationFilter == TriggerForWorkflowForm::RELATION_FILTER_ANY) {
                             return true;
                         }
                     }
                 }
                 return false;
             } else {
                 $relatedModel = $model->{$firstRelation};
                 $resolvedModel = $relatedModel->{$penultimateRelation};
                 return self::resolveIsTrueByEvaluationRules($workflow, $trigger, $resolvedModel, $resolvedAttribute);
             }
         } else {
             throw new NotSupportedException();
         }
     } else {
         $attribute = $trigger->getResolvedAttributeRealAttributeName();
         $resolvedModel = $model;
         return self::resolveIsTrueByEvaluationRules($workflow, $trigger, $resolvedModel, $attribute);
     }
 }
示例#7
0
 /**
  * @param string $moduleClassName
  * @param string $workflowType
  * @param array $triggerData
  * @return mixed
  */
 protected static function sanitizeTriggerData($moduleClassName, $workflowType, $triggerData)
 {
     assert('is_string($moduleClassName)');
     assert('is_string($workflowType)');
     assert('is_array($triggerData)');
     $triggerForSanitizing = new TriggerForWorkflowForm($moduleClassName, $moduleClassName::getPrimaryModelName(), $workflowType);
     $triggerForSanitizing->setAttributes($triggerData);
     $valueElementType = null;
     $valueElementType = $triggerForSanitizing->getValueElementType();
     if ($valueElementType == 'MixedDateTypesForWorkflow') {
         if (isset($triggerData['value']) && $triggerData['value'] !== null) {
             $triggerData['value'] = DateTimeUtil::resolveValueForDateDBFormatted($triggerData['value']);
         }
         if (isset($triggerData['secondValue']) && $triggerData['secondValue'] !== null) {
             $triggerData['secondValue'] = DateTimeUtil::resolveValueForDateDBFormatted($triggerData['secondValue']);
         }
     }
     return $triggerData;
 }
 public function testValidateThirdValues()
 {
     $trigger = new TriggerForWorkflowForm('AccountsModule', 'Account', Workflow::TYPE_ON_SAVE);
     $trigger->attributeIndexOrDerivedType = 'createdDateTime';
     $trigger->value = null;
     $trigger->secondValue = null;
     $trigger->thirdValueDurationInterval = 5;
     $trigger->thirdValueDurationType = TimeDurationUtil::DURATION_TYPE_DAY;
     $trigger->operator = null;
     $trigger->currencyIdForValue = null;
     $trigger->valueType = 'At Least X After Triggered Date';
     $validated = $trigger->validate();
     $this->assertTrue($validated);
     $this->assertCount(0, $trigger->getErrors());
     $trigger = new TriggerForWorkflowForm('AccountsModule', 'Account', Workflow::TYPE_ON_SAVE);
     $trigger->attributeIndexOrDerivedType = 'createdDateTime';
     $trigger->value = null;
     $trigger->secondValue = null;
     $trigger->thirdValueDurationInterval = 5;
     $trigger->thirdValueDurationType = null;
     $trigger->operator = null;
     $trigger->currencyIdForValue = null;
     $trigger->valueType = 'At Least X After Triggered Date';
     $validated = $trigger->validate();
     $this->assertFalse($validated);
     $this->assertCount(1, $trigger->getErrors());
     $trigger = new TriggerForWorkflowForm('AccountsModule', 'Account', Workflow::TYPE_ON_SAVE);
     $trigger->attributeIndexOrDerivedType = 'createdDateTime';
     $trigger->value = null;
     $trigger->secondValue = null;
     $trigger->thirdValueDurationInterval = 'asd';
     //should be integer
     $trigger->thirdValueDurationType = null;
     $trigger->operator = null;
     $trigger->currencyIdForValue = null;
     $trigger->valueType = 'At Least X After Triggered Date';
     $validated = $trigger->validate();
     $this->assertFalse($validated);
     $this->assertCount(2, $trigger->getErrors());
 }
 /**
  * @return array
  */
 public function rules()
 {
     return array_merge(parent::rules(), array(array('durationSeconds', 'type', 'type' => 'integer')));
 }