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 inline form for sending email from a view page.
39: *
40: * @property X2Model $targetModel The model of the form.
41: * @property integer $template The default template to use when opening the form.
42: * @property string $templateType The class of template. Different templates are meant for different models and scenarios.
43: * @property InlineEmail $model The form model for email handling and delivery
44: * @property array $insertableAttributes Can be manually set to specify insertable attributes for this scenario. Otherwise, {@link InlineEmail::getInsertableAtributes()} will be used.
45: * @property string $type Type; null is default (plain email). Specifies which list of templates to fetch.
46: * @property bool $startHidden If true, it will not be visible on page load
47: * @property string $specialFields Extra HTML to render inside the form.
48: * @property bool $postReplace If true, variable replacement will be run on non-template user input.
49: * @property bool $skipEvent If true, no event record will be created.
50: * @package application.components
51: */
52: class InlineEmailForm extends X2Widget {
53:
54: /**
55: * @var string $type
56: */
57: public $type = 'inlineEmail';
58:
59: public $attributes;
60:
61: public $instantiateJSClassOnInit = true;
62:
63: /**
64: * @var string|null $action inline email form action
65: */
66: public $action;
67:
68: /**
69: * @var bool $enableResizability
70: */
71: public $enableResizability = true;
72:
73: public $template = null;
74:
75: public $templateType = 'email';
76:
77: public $model;
78:
79: public $targetModel;
80:
81: public $contactFlag = 1;
82:
83: public $insertableAttributes;
84:
85: public $errors = array();
86:
87: public $startHidden = false;
88:
89: public $specialFields = '';
90:
91: public $postReplace = 0;
92:
93: public $skipEvent = 0;
94:
95: /**
96: * @var bool $hideFromField
97: */
98: public $hideFromField = false;
99:
100: /**
101: * @var bool $disableTemplates
102: */
103: public $disableTemplates = false;
104:
105: /**
106: * @var string the association type of the email templates
107: */
108: public $associationType = null;
109:
110: /**
111: * @var string $JSClass
112: */
113: public $JSClass = 'InlineEmailEditorManager';
114:
115: public function getPackages () {
116: if (!isset ($this->_packages)) {
117: $this->_packages = array_merge (parent::getPackages (), array (
118: 'InlineEmailEditorManager' => array(
119: 'baseUrl' => Yii::app()->request->baseUrl,
120: 'js' => array(
121: 'js/InlineEmailResizable.js',
122: 'js/inlineEmailForm.js',
123: ),
124: 'depends' => array ('jquery.ui'),
125: ),
126: ));
127: }
128: return $this->_packages;
129: }
130:
131: private $_moduleName;
132: public function getModuleName () {
133: if (!isset ($this->_moduleName)) {
134: $this->_moduleName = Yii::app()->controller->module->name;
135: }
136: return $this->_moduleName;
137: }
138:
139: public function setModuleName ($moduleName) {
140: $this->_moduleName = $moduleName;
141: }
142:
143: public function init(){
144: $this->disableTemplates = $this->disableTemplates ||
145: in_array ($this->associationType,
146: array_keys (Docs::modelsWhichSupportEmailTemplates ()));
147:
148: // Prepare the model for initially displayed input:
149: $this->model = new InlineEmail();
150: if(isset($this->targetModel)) {
151: $this->model->targetModel = $this->targetModel;
152: }
153:
154: if (!$this->associationType) {
155: $this->associationType = X2Model::getModelName ($this->getModuleName ());
156: }
157:
158: // Bring in attributes set in the configuration:
159: $this->model->attributes = $this->attributes;
160:
161: if (empty ($this->template)) {
162: // check for a default template
163: $defaultTemplateId = Yii::app()->params->profile->getDefaultEmailTemplate (
164: $this->getModuleName ());
165:
166: // if there's a default set for this module
167: if ($defaultTemplateId !== null) {
168: $defaultTemplateDoc = Docs::model()->findByPk ($defaultTemplateId);
169:
170: // ensure that template is still a valid default
171: if ($defaultTemplateDoc &&
172: ($defaultTemplateDoc->associationType === $this->associationType ||
173: $defaultTemplateDoc->type === 'quote' &&
174: $this->model->targetModel instanceof Quote)) {
175:
176: $this->template = $defaultTemplateId;
177: }
178: }
179: }
180:
181: if(empty($this->template)){
182: if(empty($this->model->message))
183: $this->model->message = InlineEmail::emptyBody();
184: $this->model->insertSignature();
185: }else{
186: // Fill in the body with a template:
187: $this->model->scenario = 'template';
188: if (!empty ($this->template))
189: $this->model->template = $this->template;
190: $this->model->prepareBody();
191: }
192:
193: // If insertable attributes aren't set, use the inline email model's
194: // getInsertableAttributes() method to generate them.
195: if((bool) $this->model->targetModel && !isset($this->insertableAttributes)){
196: $this->insertableAttributes = $this->model->insertableAttributes;
197: }
198:
199: // Load resources:
200: Yii::app()->clientScript->registerPackage ('emailEditor');
201:
202: if(!empty($this->insertableAttributes)){
203: Yii::app()->clientScript->registerScript('setInsertableAttributes',
204: 'x2.insertableAttributes = '.CJSON::encode($this->insertableAttributes).';',
205: CClientScript::POS_HEAD);
206: }
207: Yii::app()->clientScript->registerScript('storeOriginalInlineEmailMessage',
208: 'x2.inlineEmailOriginalBody = $("#email-message").val();',
209: CClientScript::POS_READY);
210: //'.CJSON::encode($this->model->message).';',CClientScript::POS_READY);
211:
212: Yii::app()->clientScript->registerScript('toggleEmailForm',
213: ($this->startHidden ?
214: "window.hideInlineEmail = true;\n" :
215: "window.hideInlineEmail = false;\n"
216: ), CClientScript::POS_HEAD);
217:
218: parent::init();
219: }
220:
221:
222: public function getJSClassParams () {
223: return array_merge (parent::getJSClassParams (), array (
224: 'translations' => array (
225: 'defaultTemplateDialogTitle' =>
226: Yii::t('app', 'Set a Default Email Template'),
227: 'Cancel' => Yii::t('app', 'Cancel'),
228: 'Save' => Yii::t('app', 'Save'),
229: 'New Message' => Yii::t('app', 'New Message'),
230: ),
231: 'disableTemplates' => $this->disableTemplates,
232: 'saveDefaultTemplateUrl' =>
233: Yii::app()->controller->createUrl (
234: '/profile/profile/ajaxSaveDefaultEmailTemplate'),
235: 'tmpUploadUrl' => Yii::app()->createUrl('/site/tmpUpload'),
236: 'rmTmpUploadUrl' => Yii::app()->createUrl('/site/removeTmpUpload'),
237: 'type' => $this->type,
238: 'enableResizability' => $this->enableResizability
239: ));
240: }
241:
242: public function run(){
243: $this->render('application.components.views.inlineEmailForm', array(
244: 'type' => $this->templateType,
245: 'associationType' => $this->associationType,
246: 'specialFields' => $this->specialFields,
247: ));
248: }
249:
250: }
251: