示例#1
0
 public function execute(&$params)
 {
     $eml = new InlineEmail();
     // make subject optional in order to support legacy flows
     $eml->requireSubjectOnCustom = false;
     $options =& $this->config['options'];
     $eml->to = $this->parseOption('to', $params);
     $historyFlag = false;
     if (isset($params['model'])) {
         $historyFlag = true;
         $eml->targetModel = $params['model'];
     }
     if (isset($options['cc']['value'])) {
         $eml->cc = $this->parseOption('cc', $params);
     }
     if (isset($options['bcc']['value'])) {
         $eml->bcc = $this->parseOption('bcc', $params);
     }
     //$eml->from = array('address'=>$this->parseOption('from',$params),'name'=>'');
     $eml->credId = $this->parseOption('from', $params);
     if ($eml->credentials && $eml->credentials->user) {
         $eml->setUserProfile($eml->credentials->user->profile);
     }
     //printR ($eml->from, true);
     $eml->subject = $this->parseOption('subject', $params);
     // "body" option (deliberately-entered content) takes precedence over template
     if (isset($options['body']['value']) && !empty($options['body']['value'])) {
         $eml->scenario = 'custom';
         $eml->message = InlineEmail::emptyBody($this->parseOption('body', $params));
         $prepared = $eml->prepareBody();
         // $eml->insertSignature(array('<br /><br /><span style="font-family:Arial,Helvetica,sans-serif; font-size:0.8em">','</span>'));
     } elseif (!empty($options['template']['value'])) {
         $eml->scenario = 'template';
         $eml->template = $this->parseOption('template', $params);
         $prepared = $eml->prepareBody();
     } else {
         $prepared = true;
         // no email body
     }
     if (!$prepared) {
         // InlineEmail failed validation
         $errors = $eml->getErrors();
         return array(false, array_shift($errors));
     }
     list($success, $message) = $this->checkDoNotEmailFields($eml);
     if (!$success) {
         return array($success, $message);
     }
     $result = $eml->send($historyFlag);
     if (isset($result['code']) && $result['code'] == 200) {
         if (YII_UNIT_TESTING) {
             return array(true, $eml->message);
         } else {
             return array(true, "");
         }
     } else {
         return array(false, Yii::t('app', "Email could not be sent"));
     }
 }
示例#2
0
 public function init()
 {
     $this->disableTemplates = $this->disableTemplates || in_array($this->associationType, array_keys(Docs::modelsWhichSupportEmailTemplates()));
     // Prepare the model for initially displayed input:
     $this->model = new InlineEmail();
     if (isset($this->targetModel)) {
         $this->model->targetModel = $this->targetModel;
     }
     if (!$this->associationType) {
         $this->associationType = X2Model::getModelName(Yii::app()->controller->module->name);
     }
     // Bring in attributes set in the configuration:
     $this->model->attributes = $this->attributes;
     if (empty($this->template)) {
         // check for a default template
         $defaultTemplateId = Yii::app()->params->profile->getDefaultEmailTemplate(Yii::app()->controller->module->name);
         // if there's a default set for this module
         if ($defaultTemplateId !== null) {
             $defaultTemplateDoc = Docs::model()->findByPk($defaultTemplateId);
             // ensure that template is still a valid default
             if ($defaultTemplateDoc && ($defaultTemplateDoc->associationType === $this->associationType || $defaultTemplateDoc->type === 'quote' && $this->model->targetModel instanceof Quote)) {
                 $this->template = $defaultTemplateId;
             }
         }
     }
     if (empty($this->template)) {
         if (empty($this->model->message)) {
             $this->model->message = InlineEmail::emptyBody();
         }
         $this->model->insertSignature();
     } else {
         // Fill in the body with a template:
         $this->model->scenario = 'template';
         if (!empty($this->template)) {
             $this->model->template = $this->template;
         }
         $this->model->prepareBody();
     }
     // If insertable attributes aren't set, use the inline email model's
     // getInsertableAttributes() method to generate them.
     if ((bool) $this->model->targetModel && !isset($this->insertableAttributes)) {
         $this->insertableAttributes = $this->model->insertableAttributes;
     }
     $this->registerJSClassInstantiation();
     // Load resources:
     Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl() . '/js/ckeditor/ckeditor.js');
     Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl() . '/js/ckeditor/adapters/jquery.js');
     Yii::app()->clientScript->registerScriptFile(Yii::app()->getBaseUrl() . '/js/emailEditor.js');
     if (!empty($this->insertableAttributes)) {
         Yii::app()->clientScript->registerScript('setInsertableAttributes', 'x2.insertableAttributes = ' . CJSON::encode($this->insertableAttributes) . ';', CClientScript::POS_HEAD);
     }
     Yii::app()->clientScript->registerScript('storeOriginalInlineEmailMessage', 'x2.inlineEmailOriginalBody = $("#email-message").val();', CClientScript::POS_READY);
     //'.CJSON::encode($this->model->message).';',CClientScript::POS_READY);
     Yii::app()->clientScript->registerScript('toggleEmailForm', $this->startHidden ? "window.hideInlineEmail = true;\n" : "window.hideInlineEmail = false;\n", CClientScript::POS_HEAD);
     $this->registerPackages();
     parent::init();
 }