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: * Provides an action for sending email from a view page with an inline form.
39: *
40: * Accepts post requests with form-urlencoded data, responds with JSON.
41: *
42: * @property InlineEmail $model
43: * @package application.components
44: */
45: class InlineEmailAction extends CAction {
46:
47: public $model = null;
48:
49: public function getBehaviors(){
50: return array(
51: 'responds' => array(
52: 'class' => 'application.components.ResponseBehavior',
53: 'errorCode' => 200
54: ),
55: );
56: }
57:
58: public function run(){
59: if (Yii::app()->user->isGuest) {
60: Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/site/login'));
61: }
62:
63: $this->attachBehaviors($this->behaviors);
64: // Safety net of handlers - they ensure that errors can be caught and seen easily:
65:
66: $scenario = 'custom';
67: if(empty($this->model))
68: $model = new InlineEmail();
69: else
70: $model = $this->model;
71: if(isset($_POST['contactFlag'])){
72: $model->contactFlag=$_POST['contactFlag'];
73: }
74: $makeEvent = isset($_GET['skipEvent']) ? !((bool) $_GET['skipEvent']) : 1;
75: // Check to see if the user is requesting a new template
76: if(isset($_GET['template'])){
77: $scenario = 'template';
78: }
79: $model->setScenario($scenario);
80: $attachments = array();
81:
82: if(isset($_POST['InlineEmail'])){
83: // This could indicate either a template change or a form submission.
84: $model->attributes = $_POST['InlineEmail'];
85:
86: // Prepare attachments that may have been uploaded on-the-fly
87: if(isset(
88: $_POST['AttachmentFiles'],
89: $_POST['AttachmentFiles']['id'],
90: $_POST['AttachmentFiles']['types'])){
91:
92: $ids = $_POST['AttachmentFiles']['id'];
93: $types = $_POST['AttachmentFiles']['types'];
94: $attachments = array();
95: for($i = 0; $i < count($ids); $i++){
96: $type = $types[$i];
97: switch ($type) {
98: case 'temp': // attachment is a temp file
99: $file = TempFile::model()->findByPk($ids[$i]);
100: $attachments[] = array(
101: 'filename' => $file->name,
102: 'folder' => $file->folder,
103: 'type' => $type,
104: 'id' => $file->id,
105: 'model' => $file,
106: );
107: break;
108: case 'media': // attachment is from media library
109: $file = Media::model()->findByPk($ids[$i]);
110: $attachments[] = array(
111: 'filename' => $file->fileName,
112: 'folder' => $file->uploadedBy,
113: 'type' => $type,
114: 'id' => $file->id,
115: 'model' => $file,
116: );
117: break;
118:
119: default:
120: throw new CException ('Invalid attachment type: '.$type);
121: }
122: }
123: }
124: $model->attachments = $attachments;
125:
126: // Validate/prepare the body, and send if no problems occur:
127: $sendStatus = array_fill_keys(array('code','message'),'');
128: $failed = false;
129: $message = '';
130: $postReplace = isset($_GET['postReplace']) ? $_GET['postReplace'] : 0;
131: if(isset($_GET['loadTemplate'])) {
132: // A special override for when it's not possible to include the template in $_POST
133: $model->template = $_GET['loadTemplate'];
134: }
135:
136: if($model->prepareBody($postReplace)){
137: if($scenario != 'template'){
138: // Sending the email, not merely requesting a template change
139: //
140: // First check that the user has permission to use the
141: // specified credentials:
142: if($model->credId != Credentials::LEGACY_ID)
143: if(!Yii::app()->user->checkAccess(
144: 'CredentialsSelect',array('model'=>$model->credentials))) {
145: $this->respond(
146: Yii::t('app','Did not send email because you do not have '.
147: 'permission to use the specified credentials.'),1);
148: }
149: $sendStatus = $model->send($makeEvent);
150: // $sendStatus = array('code'=>'200','message'=>'sent (testing)');
151:
152: $failed = $sendStatus['code'] != '200';
153: $message = $sendStatus['message'];
154: } else if($model->modelName == 'Quote' && empty($model->template)) {
155: // Fill in the gap with the default / "semi-legacy" quotes view
156: $model->message = $this->controller->renderPartial(
157: 'application.modules.quotes.views.quotes.print',
158: array('model' => $model->targetModel,'email' => true), true);
159:
160: // Add a linebreak at the beginning for user-entered notes in the email:
161: $model->insertInBody('<br />',1);
162: }
163: }
164:
165: // Populate response data:
166: $modelHasErrors = $model->hasErrors();
167: $failed = $failed || $modelHasErrors;
168: $model->attachments = array (); // prevent response json encoding failures
169: $response = array(
170: 'scenario' => $scenario,
171: 'sendStatus' => $sendStatus,
172: 'attributes' => $model->attributes,
173: 'modelErrors' => $model->errors,
174: 'modelHasErrors' => $modelHasErrors,
175: 'modelErrorHtml' => CHtml::errorSummary(
176: $model,Yii::t('app', "Please fix the following errors:"),
177: null,
178: array(
179: 'style'=>'margin-bottom: 5px;',
180: 'class'=>''
181: )),
182: );
183: if($scenario == 'template') {
184: // There's a chance the inline email form is switching gears into
185: // quote mode, in which case we need to include templates and
186: // insertable attributes for setting it all up properly:
187: $response['insertableAttributes'] = $model->insertableAttributes;
188: $templates = array(0=>Yii::t('docs','Custom Message')) +
189: Docs::getEmailTemplates($model->modelName=='Quote'?'quote':'email',
190: $_POST['associationType']);
191: $response['templateList'] = array();
192: foreach($templates as $id=>$templateName) {
193: $response['templateList'][] = array('id'=>$id,'name'=>$templateName);
194: }
195: }
196: $this->mergeResponse($response);
197:
198: $this->respond($message,$failed);
199: }else{
200: $this->respond(
201: Yii::t('app', 'Inline email model missing from the request to the server.'), true);
202: }
203: }
204:
205: }
206:
207: ?>
208: