public function testReplaceVariables() { $quote = $this->quotes('docsTest'); $contact = $this->contacts('testAnyone'); $account = $this->accounts('testQuote'); $attrs = $quote->getAttributes(); $contactAttrs = $contact->getAttributes(); $accountAttrs = $account->getAttributes(); // ensure that tokens with references to customized class names get properly replaced $quoteTemplate = array(); foreach ($attrs as $name => $val) { $quoteTemplate[$name] = "{" . $name . "}"; } foreach ($contactAttrs as $name => $val) { $quoteTemplate[$name] = "{" . self::$contactsField . ".{$name}}"; } foreach ($accountAttrs as $name => $val) { $quoteTemplate[$name] = "{" . self::$accountsField . ".{$name}}"; } foreach (array_intersect(array_keys($contactAttrs), array_keys($attrs), array_keys($accountAttrs)) as $name) { unset($quoteTemplate[$name]); unset($attrs[$name]); unset($contactAttrs[$name]); unset($accountAttrs[$name]); } // add quotes template-specific token $quoteTemplate['dateNow'] = '{dateNow}'; $quoteTemplate['lineItems'] = '{lineItems}'; $quoteTemplate['quoteOrInvoice'] = '{quoteOrInvoice}'; $quoteTemplate = CJSON::encode($quoteTemplate); $str = Docs::replaceVariables($quoteTemplate, $quote, array(), false, false); $this->assertEquals(array_merge(array_map(function ($elem) { return (string) $elem; }, $attrs), array_map(function ($elem) { return (string) $elem; }, $contactAttrs), array_map(function ($elem) { return (string) $elem; }, $accountAttrs), array('lineItems' => preg_replace("/\r|\n/", "", $quote->productTable(true)), 'dateNow' => date("F d, Y", time()), 'quoteOrInvoice' => Yii::t('quotes', $quote->type == 'invoice' ? 'Invoice' : Modules::displayName(false, "Quotes")))), CJSON::decode($str)); }
public static function recordEmailSent(Campaign $campaign, Contacts $contact) { $action = new Actions(); // Disable the unsightly notifications for loads of emails: $action->scenario = 'noNotif'; $now = time(); $action->associationType = 'contacts'; $action->associationId = $contact->id; $action->associationName = $contact->firstName . ' ' . $contact->lastName; $action->visibility = $contact->visibility; $action->type = 'email'; $action->assignedTo = $contact->assignedTo; $action->createDate = $now; $action->completeDate = $now; $action->complete = 'Yes'; $action->actionDescription = '<b>' . Yii::t('marketing', 'Campaign') . ': ' . $campaign->name . "</b>\n\n" . Yii::t('marketing', 'Subject') . ": " . Docs::replaceVariables($campaign->subject, $contact) . "\n\n" . Docs::replaceVariables($campaign->content, $contact); if (!$action->save()) { throw new CException('Campaing email action history record failed to save with validation errors: ' . CJSON::encode($action->errors)); } }
public function testReplaceVariables() { $quote = $this->quotes('docsTest'); $contact = $this->contacts('testAnyone'); $attrs = $quote->getAttributes(); $contactAttrs = $contact->getAttributes(); // $quoteTemplate = array (); // foreach ($attrs as $name => &$val) { // $quoteTemplate[$name] = "{Quote.$name}"; // } // foreach ($contactAttrs as $name => &$val) { // $quoteTemplate[$name] = "{Contact.$name}"; // } // foreach (array_intersect (array_keys ($contactAttrs), array_keys ($attrs)) as $name) { // unset ($quoteTemplate[$name]); // unset ($attrs[$name]); // unset ($contactAttrs[$name]); // } // $quoteTemplate = CJSON::encode ($quoteTemplate); // $str = Docs::replaceVariables ($quoteTemplate, $quote, array (), false , false); // $this->assertEquals ( // array_merge ( // array_map (function ($elem) { return (string) $elem; }, $attrs), // array_map (function ($elem) { return (string) $elem; }, $contactAttrs) // ), // CJSON::decode ($str) // ); // ensure that tokens with references to customized class names get properly replaced $quoteTemplate = array(); foreach ($attrs as $name => $val) { $quoteTemplate[$name] = "{" . self::$customQuotesTitle . ".{$name}}"; } foreach ($contactAttrs as $name => $val) { $quoteTemplate[$name] = "{" . self::$customContactsTitle . ".{$name}}"; } foreach (array_intersect(array_keys($contactAttrs), array_keys($attrs)) as $name) { unset($quoteTemplate[$name]); unset($attrs[$name]); unset($contactAttrs[$name]); } // add quotes template-specific token $quoteTemplate['dateNow'] = '{' . self::$customQuotesTitle . '.quoteOrInvoice}'; $quoteTemplate = CJSON::encode($quoteTemplate); $str = Docs::replaceVariables($quoteTemplate, $quote, array(), false, false); $this->assertEquals(array_merge(array_map(function ($elem) { return (string) $elem; }, $attrs), array_map(function ($elem) { return (string) $elem; }, $contactAttrs), array('dateNow' => Yii::t('quotes', $quote->type == 'invoice' ? 'Invoice' : self::$customQuotesTitle))), CJSON::decode($str)); // old test // $this->markTestSkipped('This test has been very badly broken by ' // . 'changes made to X2Model.getAttribute and ' // . 'Formatter.replaceVariables which apparently make it so that ' // . 'no combination of arguments sent to Docs.replaceVariables ' // . 'versus to X2Model.getAttribute produce an equivalent list ' // . 'of values. Thus, for now, abandon hope of fixing it.'); // // // Test replacement in emails: // $contact = $this->contacts('testAnyone'); // $textIn = array(); // $textOutExpected = array(); // $delimiter = "\n@@|@@\n"; // foreach($contact->attributes as $name=>$value) { // $textIn[] = '{'.$name.'}'; // $textOutExpected[] = $contact->renderAttribute($name, false); // } // $textIn = implode($delimiter,$textIn); // $textOutExpected = implode($delimiter,$textOutExpected); // $textOut = Docs::replaceVariables($textIn,$contact); // $this->assertEquals($textOutExpected,$textOut,'Failed asserting that email template replacement succeeded.'); // // // Test replacement in Quote bodies: // $quote = $this->quotes('docsTest'); // $classes = array( // 'Accounts', // 'Contacts', // 'Quote' // ); // In that order // $models = array( // 'Accounts' => $this->accounts('testQuote'), // 'Contacts' => $this->contacts('testAnyone'), // 'Quote' => $this->quotes('docsTest'), // ); // $textIn = array(); // $textOutExpected = array(); // $delimiter = "\n*|*\n"; // // foreach($classes as $class) { // $classNick = rtrim($class,'s'); // $attrs = array_keys($class::model()->attributeLabels()); // foreach($attrs as $attribute) { // $textIn[] = '{'.$classNick.'.'.$attribute.'}'; // $textOutExpected[] = empty($models[$class])?'':$models[$class]->renderAttribute($attribute, false); // } // } // $textIn = implode($delimiter,$textIn); // $textOutExpected = implode($delimiter,$textOutExpected); // $textOut = Docs::replaceVariables($textIn,$quote); // $this->assertEquals($textOutExpected,$textOut, 'Failed asserting that Quote template replacement succeeded.'); // // }
/** * Generate presentation markup for the quote. * * @param integer $id ID of the quote for which to create a presentation * @param bool $email Parameter passed to the item table markup generator * @param string $header Optional header to pass to the print view * @return type */ public function getPrintQuote($id = null, $email = false) { $this->throwOnNullModel = false; $model = $this->getModel($id); if ($model == null) { return Yii::t('quotes', 'Quote {id} does not exist. It may have been deleted.', array('{id}' => $id)); } if (!$model->templateModel instanceof Docs) { // Legacy view (very, very plain!) return $this->renderPartial('print', array('model' => $model, 'email' => $email), true); } else { // User-defined template $template = $model->templateModel; if (!$template instanceof Docs) { // Template not found (it was probably deleted). // Use the default quotes view. $model->template = null; $model->update(array('template')); return $this->getPrintQuote($model->id); } return Docs::replaceVariables($template->text, $model); } }
/** * Prepare the email body for sending or customization by the user. */ public function prepareBody($postReplace = 0) { if (!$this->validate()) { return false; } // Replace the existing body, if any, with a template, i.e. for initial // set-up or an automated email. if ($this->scenario === 'template') { // Get the template and associated model if (!empty($this->templateModel)) { if ($this->templateModel->emailTo !== null) { $this->to = Docs::replaceVariables($this->templateModel->emailTo, $this->targetModel, array(), false, false); } // Replace variables in the subject and body of the email $this->subject = Docs::replaceVariables($this->templateModel->subject, $this->targetModel); // if(!empty($this->targetModel)) { $this->message = Docs::replaceVariables($this->templateModel->text, $this->targetModel, array('{signature}' => self::insertedPattern('signature', $this->signature))); // } else { // $this->insertInBody('<span style="color:red">'.Yii::t('app','Error: attempted using a template, but the referenced model was not found.').'</span>'); // } } else { // No template? $this->message = self::emptyBody(); $this->insertSignature(); } } else { if ($postReplace) { $this->subject = Docs::replaceVariables($this->subject, $this->targetModel); $this->message = Docs::replaceVariables($this->message, $this->targetModel); } } return true; }