1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * Create Record action
39: *
40: * @package application.components.x2flow.actions
41: */
42: class X2FlowEmail extends BaseX2FlowEmail {
43: public $title = 'Email';
44: public $info = 'Send a template or custom email to the specified address.';
45:
46:
47: public function paramRules() {
48: $parentRules = parent::paramRules ();
49: $parentRules['options'] = array_merge (
50: $parentRules['options'],
51: array (
52: array(
53: 'name'=>'to',
54: 'label'=>Yii::t( 'studio','To:'),
55: 'type'=>'email'
56: ),
57: array(
58: 'name' => 'template',
59: 'label' => Yii::t('studio', 'Template'),
60: 'type' => 'dropdown',
61: 'defaultVal' => '',
62: 'options' => array('' => Yii::t('studio', 'Custom')) +
63: Docs::getEmailTemplates('email', 'Contacts')
64: ),
65: array(
66: 'name' => 'subject',
67: 'label' => Yii::t('studio', 'Subject'),
68: 'optional' => 1
69: ),
70: array(
71: 'name' => 'cc',
72: 'label' => Yii::t('studio', 'CC:'),
73: 'optional' => 1,
74: 'type' => 'email'
75: ),
76: array(
77: 'name' => 'bcc',
78: 'label' => Yii::t('studio', 'BCC:'),
79: 'optional' => 1,
80: 'type' => 'email'
81: ),
82: array(
83: 'name' => 'logEmail',
84: 'label' =>
85: Yii::t('studio', 'Log email?').' '.
86: X2Html::hint2 (
87: Yii::t('studio', 'Checking this box will cause the email to be attached '.
88: 'to the record associated with this flow, if it exists.')),
89: 'optional' => 1,
90: 'defaultVal' => 1,
91: 'type' => 'boolean',
92: ),
93: array(
94: 'name' => 'body',
95: 'label' => Yii::t('studio', 'Message'),
96: 'optional' => 1,
97: 'type' => 'richtext'
98: ),
99:
100: )
101: );
102: return $parentRules;
103: }
104:
105: public function execute(&$params) {
106: $eml = new InlineEmail;
107:
108: // make subject optional in order to support legacy flows
109: $eml->requireSubjectOnCustom = false;
110: $id = $this->config['id'];
111: $options = &$this->config['options'];
112: $eml->to = $this->parseOption('to', $params);
113:
114: $historyFlag = false;
115: if (isset($params['model'])) {
116: $historyFlag = $options['logEmail']['value'];
117: $eml->targetModel = $params['model'];
118: }
119: if (isset($options['cc']['value']))
120: $eml->cc = $this->parseOption('cc', $params);
121: if (isset($options['bcc']['value'])) {
122: $eml->bcc = $this->parseOption('bcc', $params);
123: }
124:
125: //$eml->from = array('address'=>$this->parseOption('from',$params),'name'=>'');
126: $eml->credId = $this->parseOption('from', $params);
127: if ($eml->credentials && $eml->credentials->user)
128: $eml->setUserProfile($eml->credentials->user->profile);
129:
130: //printR ($eml->from, true);
131: $eml->subject = $this->parseOption('subject', $params);
132:
133: // "body" option (deliberately-entered content) takes precedence over template
134: if (isset($options['body']['value']) && !empty($options['body']['value'])) {
135: $eml->scenario = 'custom';
136: $eml->message = InlineEmail::emptyBody($this->parseOption('body',
137: $params));
138: $prepared = $eml->prepareBody();
139: // $eml->insertSignature(array('<br /><br /><span style="font-family:Arial,Helvetica,sans-serif; font-size:0.8em">','</span>'));
140: } elseif (!empty($options['template']['value'])) {
141: $eml->scenario = 'template';
142: $eml->template = $this->parseOption('template', $params);
143: $prepared = $eml->prepareBody();
144: } else {
145: $prepared = true; // no email body
146: }
147:
148: if (!$prepared) { // InlineEmail failed validation
149: $errors = $eml->getErrors();
150: return array(false, array_shift($errors));
151: }
152:
153: list ($success, $message) = $this->checkDoNotEmailFields($eml);
154: if (!$success) {
155: return array($success, $message);
156: }
157:
158: $result = $eml->send($historyFlag);
159: if (isset($result['code']) && $result['code'] == 200) {
160: if (!isset($params['sentEmails'])) {
161: $params['sentEmails'] = array();
162: }
163: $params['sentEmails'][$id] = $eml->uniqueId;
164: if (YII_UNIT_TESTING) {
165: return array(true, $eml->message);
166: } else {
167: return array(true, "");
168: }
169: } else {
170: return array(false, Yii::t('app', "Email could not be sent"));
171: }
172: }
173:
174: }
175: