/** * @param string $name the name of the option * @param array $params the parameters passed to trigger () * @return mixed null if the option was not set by the user, the parsed value otherwise */ public function parseOption($name, &$params) { $options =& $this->config['options']; if (!isset($options[$name]['value'])) { return null; } $type = isset($options[$name]['type']) ? $options[$name]['type'] : ''; return X2Flow::parseValue($options[$name]['value'], $type, $params); }
/** * @param Array $condition * @param Array $params * @return bool true for success, false otherwise */ public static function checkCondition($condition, &$params) { if ($condition['type'] === 'workflow_status') { return self::checkWorkflowStatusCondition($condition, $params); } $model = isset($params['model']) ? $params['model'] : null; $operator = isset($condition['operator']) ? $condition['operator'] : '='; // $type = isset($condition['type'])? $condition['type'] : null; $value = isset($condition['value']) ? $condition['value'] : null; // default to a doing basic value comparison if (isset($condition['name']) && $condition['type'] === '') { if (!isset($params[$condition['name']])) { return false; } return self::evalComparison($params[$condition['name']], $operator, $value); } switch ($condition['type']) { case 'attribute': if (!isset($condition['name'], $model)) { return false; } $attr =& $condition['name']; if (null === ($field = $model->getField($attr))) { return false; } if ($operator === 'changed') { return $model->attributeChanged($attr); } if ($field->type === 'link') { list($attrVal, $id) = Fields::nameAndId($model->getAttribute($attr)); } else { $attrVal = $model->getAttribute($attr); } return self::evalComparison($attrVal, $operator, X2Flow::parseValue($value, $field->type, $params), $field); case 'current_user': return self::evalComparison(Yii::app()->user->getName(), $operator, X2Flow::parseValue($value, 'assignment', $params)); case 'month': return self::evalComparison((int) date('n'), $operator, $value); // jan = 1, dec = 12 // jan = 1, dec = 12 case 'day_of_month': return self::evalComparison((int) date('j'), $operator, $value); // 1 through 31 // 1 through 31 case 'day_of_week': return self::evalComparison((int) date('N'), $operator, $value); // monday = 1, sunday = 7 // monday = 1, sunday = 7 case 'time_of_day': // - mktime(0,0,0) return self::evalComparison(time(), $operator, X2Flow::parseValue($value, 'time', $params)); // seconds since midnight // case 'current_local_time': // seconds since midnight // case 'current_local_time': case 'current_time': return self::evalComparison(time(), $operator, X2Flow::parseValue($value, 'dateTime', $params)); case 'user_active': return CActiveRecord::model('Session')->exists('user=:user AND status=1', array(':user' => X2Flow::parseValue($value, 'assignment', $params))); case 'on_list': if (!isset($model, $value)) { return false; } $value = X2Flow::parseValue($value, 'link'); // look up specified list if (is_numeric($value)) { $list = CActiveRecord::model('X2List')->findByPk($value); } else { $list = CActiveRecord::model('X2List')->findByAttributes(array('name' => $value)); } return $list !== null && $list->hasRecord($model); case 'has_tags': if (!isset($model, $value)) { return false; } $tags = X2Flow::parseValue($value, 'tags'); return $model->hasTags($tags, 'AND'); case 'workflow_status': if (!isset($model, $condition['workflowId'], $condition['stageNumber'])) { return false; } switch ($operator) { case 'started_workflow': return CActiveRecord::model('Actions')->exists('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'])); case 'started_stage': return CActiveRecord::model('Actions')->exists('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow AND stageNumber=:stage AND (completeDate IS NULL OR completeDate=0)', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'], ':stageNumber' => $condition['stageNumber'])); case 'completed_stage': return CActiveRecord::model('Actions')->exists('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow AND stageNumber=:stage AND completeDate > 0', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'], ':stageNumber' => $condition['stageNumber'])); case 'completed_workflow': $stageCount = CActiveRecord::model('WorkflowStage')->count('workflowId=:id', array(':id' => $condition['workflowId'])); $actionCount = CActiveRecord::model('Actions')->count('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'])); return $actionCount >= $stageCount; } return false; case 'email_open': if (isset($params['sentEmails'], $params['sentEmails'][$value])) { $trackEmail = TrackEmail::model()->findByAttributes(array('uniqueId' => $params['sentEmails'][$value])); return $trackEmail && !is_null($trackEmail->opened); } return false; } return false; // foreach($condition as $key = >$value) { // Record attribute (=, <, >, <>, in list, not in list, empty, not empty, contains) // Linked record attribute (eg. a contact's account has > 30 employees) // Current user // Current time (day of week, hours, etc) // Current time in record's timezone // Is user X logged in // Workflow status (in workflow X, started stage Y, completed Y, completed all) // } }
public function execute(&$params) { $url = $this->parseOption('url', $params); if (strpos($url, 'http') === false) { $url = 'http://' . $url; } $method = $this->parseOption('method', $params); if ($this->parseOption('immediate', $params) || true) { $headers = array(); $httpOptions = array('timeout' => 5, 'method' => $method); if (isset($this->config['headerRows'])) { $headers = $this->getHeaders($this->config['headerRows'], $params); } if ($method !== 'GET' && $this->parseOption('jsonPayload', $params)) { $data = $this->parseOption('jsonBlob', $params); } elseif (isset($this->config['attributes']) && !empty($this->config['attributes'])) { $data = array(); foreach ($this->config['attributes'] as $param) { if (isset($param['name'], $param['value'])) { $data[$param['name']] = X2Flow::parseValue($param['value'], '', $params, false); } } } if (isset($data)) { if ($method === 'GET') { $data = http_build_query($data); // make sure the URL is ready for GET params $url .= strpos($url, '?') === false ? '?' : '&'; $url .= $data; } else { if ($this->parseOption('jsonPayload', $params)) { // nested JSON option if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/json'; } $httpOptions['content'] = $data; } else { // set up default header for POST style data if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'application/x-www-form-urlencoded'; } if (preg_match("/application\\/json/", $headers['Content-Type'])) { // legacy flat JSON object support $data = CJSON::encode($data); $httpOptions['content'] = $data; } else { $data = http_build_query($data); $httpOptions['content'] = $data; } } // set up default header for POST style data if (!isset($headers['Content-Length'])) { $headers['Content-Length'] = strlen($data); } } } if (count($headers)) { $formattedHeaders = $this->formatHeaders($headers); $httpOptions['header'] = implode("\r\n", $formattedHeaders); } $context = stream_context_create(array('http' => $httpOptions)); if (!$this->validateUrl($url)) { if (YII_UNIT_TESTING) { return array(false, array('url' => $url)); } else { return array(false, Yii::t('studio', 'Requests cannot be made to X2Engine\'s API from X2Flow.')); } } if (!$this->getMakeRequest()) { return array(true, array_merge(array('url' => $url), $httpOptions)); } else { $response = @file_get_contents($url, false, $context); $params['returnValue'] = $response; if ($response !== false) { if (YII_UNIT_TESTING) { return array(true, $response); } else { return array(true, Yii::t('studio', "Remote API call succeeded")); } } else { if (YII_UNIT_TESTING) { return array(false, print_r($http_response_header, true)); } else { return array(false, Yii::t('studio', "Remote API call failed!")); } } } } }