/** * Replace tokens with model attribute values. * * @param type $str Input text * @param X2Model $model Model to use for replacement * @param array $vars List of extra variables to replace * @param bool $encode Encode replacement values if true; use renderAttribute otherwise. * @return string */ public static function replaceVariables($str, $model, $vars = array(), $encode = false, $renderFlag = true) { if ($encode) { foreach (array_keys($vars) as $key) { $vars[$key] = CHtml::encode($vars[$key]); } } $str = strtr($str, $vars); // replace any manually set variables if ($model instanceof X2Model) { if (get_class($model) !== 'Quote') { $str = Formatter::replaceVariables($str, $model, '', $renderFlag, false); } else { // Specialized, separate method for quotes that can use details from // either accounts or quotes. // There may still be some stray quotes with 2+ contacts on it, so // explode and pick the first to be on the safe side. The most // common use case by far is to have only one contact on the quote. $accountId = $model->accountName; $staticModels = array('Contact' => Contacts::model(), 'Account' => Accounts::model(), 'Quote' => Quote::model()); $models = array('Contact' => $model->contact, 'Account' => empty($accountId) ? null : $staticModels['Account']->findByAttributes(array('nameId' => $accountId)), 'Quote' => $model); $attributes = array(); foreach ($models as $name => $modelObj) { $moduleRef = Modules::displayName(false, $name . "s"); if (empty($modelObj)) { // Model will be blank foreach ($staticModels[$name]->fields as $field) { $attributes['{' . $moduleRef . '.' . $field->fieldName . '}'] = ''; } } else { // Insert attributes foreach ($modelObj->attributes as $fieldName => $value) { if ($renderFlag) { $attributes['{' . $moduleRef . '.' . $fieldName . '}'] = $modelObj->renderAttribute($fieldName, false, true, $encode); } else { $attributes['{' . $moduleRef . '.' . $fieldName . '}'] = $modelObj->getAttribute($fieldName); } } } } $quoteTitle = Modules::displayName(false, "Quotes"); $quoteParams = array('{' . $quoteTitle . '.lineItems}' => $model->productTable(true), '{' . $quoteTitle . '.dateNow}' => date("F d, Y", time()), '{' . $quoteTitle . '.quoteOrInvoice}' => Yii::t('quotes', $model->type == 'invoice' ? 'Invoice' : $quoteTitle)); // Run the replacement: $str = strtr($str, array_merge($attributes, $quoteParams)); return $str; } } return $str; }
protected function renderCustom($field, $makeLinks, $textOnly, $encode) { $fieldName = $field->fieldName; if ($field->linkType == 'display') { // Interpret as HTML. Restore curly braces in href // attributes that HTMLPurifier has replaced: $fieldText = preg_replace('/%7B([\\w\\.]+)%7D/', '{$1}', $field->data); return Formatter::replaceVariables($fieldText, $this->owner, '', false); } elseif ($field->linkType == 'formula') { $evald = Formatter::parseFormula($field->data, array('model' => $this->owner)); if ($evald[0]) { return $this->render($evald[1], $encode); } else { return Yii::t('app', 'Error parsing formula.') . ' ' . $evald[1]; } } else { return $this->render($this->owner->{$fieldName}, $encode); } }
/** * Replace tokens with model attribute values. * * @param type $str Input text * @param X2Model $model Model to use for replacement * @param array $vars List of extra variables to replace * @param bool $encode Encode replacement values if true; use renderAttribute otherwise. * @return string */ public static function replaceVariables($str, $model, $vars = array(), $encode = false, $renderFlag = true) { if ($encode) { foreach (array_keys($vars) as $key) { $vars[$key] = CHtml::encode($vars[$key]); } } $str = strtr($str, $vars); // replace any manually set variables if ($model instanceof X2Model) { if (get_class($model) === 'Quote') { $quoteTitle = Modules::displayName(false, "Quotes"); $quoteParams = array('{lineItems}' => $model->productTable(true), '{dateNow}' => date("F d, Y", time()), '{quoteOrInvoice}' => Yii::t('quotes', $model->type == 'invoice' ? 'Invoice' : $quoteTitle)); $str = strtr($str, $quoteParams); } $str = Formatter::replaceVariables($str, $model, '', $renderFlag, false); } return $str; }