/** * Sets model fields using the provided attributes and values. * * @param CActiveRecord $model the model to set fields on * @param array $attributes an associative array of attributes * @param array $params the params array passed to X2Flow::trigger() * @return boolean whether or not the attributes were valid and set successfully * */ public function setModelAttributes(&$model, &$attributeList, &$params) { $data = array(); foreach ($attributeList as &$attr) { if (!isset($attr['name'], $attr['value'])) { continue; } if (null !== ($field = $model->getField($attr['name']))) { // first do variable/expression evaluation, // then process with X2Fields::parseValue() $type = $field->type; $value = $attr['value']; if (is_string($value)) { if (strpos($value, '=') === 0) { $evald = X2FlowFormatter::parseFormula($value, $params); if (!$evald[0]) { return false; } $value = $evald[1]; } elseif ($params !== null) { if (is_string($value) && isset($params['model'])) { $value = X2FlowFormatter::replaceVariables($value, $params, $type); } } } $data[$attr['name']] = $value; } } if (!isset($model->scenario)) { $model->setScenario('X2Flow'); } $model->setX2Fields($data); if ($model instanceof Actions && isset($data['complete'])) { switch ($data['complete']) { case 'Yes': $model->complete(); break; case 'No': $model->uncomplete(); break; } } return true; }
public static function parseValue($value, $type, &$params = null, $renderFlag = true) { if (is_string($value)) { if (strpos($value, '=') === 0) { // It's a formula. Evaluate it. $evald = X2FlowFormatter::parseFormula($value, $params); // Fail silently because there's not yet a good way of reporting // problems that occur in parseFormula -- $value = ''; if ($evald[0]) { $value = $evald[1]; } } else { // Run token replacement: $value = X2FlowFormatter::replaceVariables($value, $params, $type, $renderFlag, true); } } switch ($type) { case 'boolean': return (bool) $value; case 'time': case 'date': case 'dateTime': if (ctype_digit((string) $value)) { // must already be a timestamp return $value; } if ($type === 'date') { $value = Formatter::parseDate($value); } elseif ($type === 'dateTime') { $value = Formatter::parseDateTime($value); } else { $value = strtotime($value); } return $value === false ? null : $value; case 'link': $pieces = explode('_', $value); if (count($pieces) > 1) { return $pieces[0]; } return $value; case 'tags': return Tags::parseTags($value); default: return $value; } }