Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
EmailTemplate.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified 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 Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  const TYPE_WORKFLOW = 1;
40 
41  const TYPE_CONTACT = 2;
42 
43  const BUILT_TYPE_PLAIN_TEXT_ONLY = 1;
44 
45  const BUILT_TYPE_PASTED_HTML = 2;
46 
47  const BUILT_TYPE_BUILDER_TEMPLATE = 3;
48 
53  private static $cachedDataAndLabelsByType = array();
54 
55  public static function getByName($name)
56  {
57  return self::getByNameOrEquivalent('name', $name);
58  }
59 
60  public static function getModuleClassName()
61  {
62  return 'EmailTemplatesModule';
63  }
64 
65  protected static function getLabel($language = null)
66  {
67  return Zurmo::t('EmailTemplatesModule', 'Email Template', array(), null, $language);
68  }
69 
75  protected static function getPluralLabel($language = null)
76  {
77  return Zurmo::t('EmailTemplatesModule', 'Email Templates', array(), null, $language);
78  }
79 
80  public static function getTypeDropDownArray()
81  {
82  return array(
83  static::TYPE_WORKFLOW => Zurmo::t('WorkflowsModule', 'Workflow'),
84  static::TYPE_CONTACT => Zurmo::t('ContactsModule', 'Contact'),
85  );
86  }
87 
88  public static function getBuiltTypeDropDownArray()
89  {
90  return array(
91  static::BUILT_TYPE_BUILDER_TEMPLATE => Zurmo::t('EmailTemplatesModule', 'Template Builder'),
92  static::BUILT_TYPE_PLAIN_TEXT_ONLY => Zurmo::t('EmailTemplatesModule', 'Plain Text'),
93  static::BUILT_TYPE_PASTED_HTML => Zurmo::t('EmailTemplatesModule', 'HTML'),
94  );
95  }
96 
97  public static function renderNonEditableTypeStringContent($type)
98  {
99  assert('is_int($type) || $type == null');
100  $dropDownArray = self::getTypeDropDownArray();
101  if (!empty($dropDownArray[$type]))
102  {
103  return Yii::app()->format->text($dropDownArray[$type]);
104  }
105  }
106 
107  public static function getNonEditableBuiltTypeStringContent($builtType)
108  {
109  assert('is_int($builtType) || $builtType == null');
110  $dropDownArray = self::getBuiltTypeDropDownArray();
111  if (!empty($dropDownArray[$builtType]))
112  {
113  return Yii::app()->format->text($dropDownArray[$builtType]);
114  }
115  }
116 
117  public function __toString()
118  {
119  try
120  {
121  if (trim($this->name) == '')
122  {
123  return Zurmo::t('Core', '(Unnamed)');
124  }
125  return $this->name;
126  }
128  {
129  return '';
130  }
131  }
132 
133  public static function canSaveMetadata()
134  {
135  return true;
136  }
137 
138  public static function isTypeDeletable()
139  {
140  return true;
141  }
142 
143  public static function getDefaultMetadata()
144  {
145  $metadata = parent::getDefaultMetadata();
146  $metadata[__CLASS__] = array(
147  'members' => array(
148  'type',
149  'builtType',
150  'isDraft',
151  'modelClassName',
152  'name',
153  'subject',
154  'language',
155  'htmlContent',
156  'textContent',
157  'serializedData',
158  'isFeatured',
159  ),
160  'rules' => array(
161  array('type', 'required'),
162  array('type', 'type', 'type' => 'integer'),
163  array('type', 'numerical'),
164  array('isDraft', 'type', 'type' => 'boolean'),
165  array('isDraft', 'SetToTrueForBuilderTemplateElseFalseValidator'),
166  array('builtType', 'required'),
167  array('builtType', 'type', 'type' => 'integer'),
168  array('builtType', 'numerical'),
169  array('modelClassName', 'required'),
170  array('modelClassName', 'type', 'type' => 'string'),
171  array('modelClassName', 'length', 'max' => 64),
172  array('modelClassName', 'ModelExistsAndIsReadableValidator'),
173  array('name', 'required'),
174  array('name', 'type', 'type' => 'string'),
175  array('name', 'length', 'min' => 1, 'max' => 64),
176  array('subject', 'required'),
177  array('subject', 'type', 'type' => 'string'),
178  array('subject', 'length', 'min' => 1, 'max' => 255),
179  array('language', 'type', 'type' => 'string'),
180  array('language', 'length', 'min' => 2, 'max' => 2),
181  array('language', 'SetToUserDefaultLanguageValidator'),
182  array('htmlContent', 'type', 'type' => 'string'),
183  array('textContent', 'type', 'type' => 'string'),
184  array('htmlContent', 'StripDummyHtmlContentFromOtherwiseEmptyFieldValidator'),
185  array('textContent', 'EmailTemplateAtLeastOneContentAreaRequiredValidator'),
186  array('htmlContent', 'EmailTemplateMergeTagsValidator'),
187  array('textContent', 'EmailTemplateMergeTagsValidator'),
188  array('serializedData', 'type', 'type' => 'string'),
189  array('serializedData', 'EmailTemplateSerializedDataValidator'),
190  array('isFeatured', 'type', 'type' => 'boolean'),
191  ),
192  'elements' => array(
193  'htmlContent' => 'TextArea',
194  'textContent' => 'TextArea',
195  ),
196  'relations' => array(
197  'files' => array(static::HAS_MANY, 'FileModel', static::OWNED,
198  static::LINK_TYPE_POLYMORPHIC, 'relatedModel'),
199  ),
200  );
201  return $metadata;
202  }
203 
209  public static function getByType($type, $includeDrafts = false)
210  {
211  assert('is_int($type)');
212  $searchAttributeData = array();
213  $searchAttributeData['clauses'] = array(
214  1 => array(
215  'attributeName' => 'type',
216  'operatorType' => 'equals',
217  'value' => $type,
218  ),
219  2 => array(
220  'attributeName' => 'modelClassName',
221  'operatorType' => 'isNotNull',
222  'value' => null,
223  ),
224  );
225  $searchAttributeData['structure'] = '1 and 2';
226  if (!$includeDrafts)
227  {
228  $searchAttributeData['clauses'][3] = array(
229  'attributeName' => 'isDraft',
230  'operatorType' => 'equals',
231  'value' => intval($includeDrafts),
232  );
233  $searchAttributeData['structure'] .= ' and 3';
234  }
235  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
236  $where = RedBeanModelDataProvider::makeWhere(get_called_class(), $searchAttributeData, $joinTablesAdapter);
237  return self::getSubset($joinTablesAdapter, null, null, $where, 'name');
238  }
239 
245  {
246  $searchAttributeData = array();
247  $searchAttributeData['clauses'] = array(
248  1 => array(
249  'attributeName' => 'builtType',
250  'operatorType' => 'equals',
251  'value' => static::BUILT_TYPE_BUILDER_TEMPLATE,
252  ),
253  2 => array(
254  'attributeName' => 'modelClassName',
255  'operatorType' => 'isNull',
256  'value' => null,
257  ),
258  );
259  $searchAttributeData['structure'] = '1 and 2';
260  return $searchAttributeData;
261  }
262 
267  public static function getPredefinedBuilderTemplates()
268  {
269  $searchAttributeData = static::getPredefinedBuilderTemplatesSearchAttributeData();
270  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
271  $where = RedBeanModelDataProvider::makeWhere(get_called_class(), $searchAttributeData, $joinTablesAdapter);
272  return self::getSubset($joinTablesAdapter, null, null, $where, 'name');
273  }
274 
275  protected static function bypassReadPermissionsOptimizationToSqlQueryBasedOnWhere($where)
276  {
278  $builtTemplateType = static::BUILT_TYPE_BUILDER_TEMPLATE;
279  $isNull = SQLOperatorUtil::resolveOperatorAndValueForNullOrEmpty('isNull');
280  $expectedWhere = "({$q}emailtemplate{$q}.{$q}builttype{$q} = {$builtTemplateType}) and " .
281  "({$q}emailtemplate{$q}.{$q}modelclassname{$q} {$isNull})";
282  if ($where == $expectedWhere)
283  {
284  return true;
285  }
286  return parent::bypassReadPermissionsOptimizationToSqlQueryBasedOnWhere($where);
287  }
288 
289  public function checkPermissionsHasAnyOf($requiredPermissions, User $user = null)
290  {
291  if ($user == null)
292  {
293  $user = Yii::app()->user->userModel;
294  }
295  $effectivePermissions = $this->getEffectivePermissions($user);
296  if (($effectivePermissions & $requiredPermissions) == 0)
297  {
299  if (!$this->isPredefinedBuilderTemplate())
300  {
301  throw new AccessDeniedSecurityException($user, $requiredPermissions, $effectivePermissions);
302  }
303  else
304  {
305  //Do nothing
306  }
307  }
308  }
309 
316  public static function getPreviouslyCreatedBuilderTemplateSearchAttributeData($modelClassName = null, $includeDrafts = false)
317  {
318  $searchAttributeData = array();
319  $searchAttributeData['clauses'] = array(
320  1 => array(
321  'attributeName' => 'builtType',
322  'operatorType' => 'equals',
323  'value' => static::BUILT_TYPE_BUILDER_TEMPLATE,
324  ));
325  $searchAttributeData['structure'] = '1';
326  if (!$includeDrafts)
327  {
328  $searchAttributeData['clauses'][2] = array(
329  'attributeName' => 'isDraft',
330  'operatorType' => 'equals',
331  'value' => intval($includeDrafts),
332  );
333  $searchAttributeData['structure'] .= ' and 2';
334  }
335  if (isset($modelClassName))
336  {
337  $searchAttributeData['clauses'][3] = array(
338  'attributeName' => 'modelClassName',
339  'operatorType' => 'equals',
340  'value' => $modelClassName,
341  );
342  }
343  else
344  {
345  // if moduleClassName isn't give then at least exclude the pre-defined ones.
346  $searchAttributeData['clauses'][3] = array(
347  'attributeName' => 'modelClassName',
348  'operatorType' => 'isNotNull',
349  'value' => null,
350  );
351  }
352  $searchAttributeData['structure'] .= ' and 3';
353  return $searchAttributeData;
354  }
355 
362  public static function getPreviouslyCreatedBuilderTemplates($modelClassName = null, $includeDrafts = false, $limit = null)
363  {
364  $searchAttributeData = static::getPreviouslyCreatedBuilderTemplateSearchAttributeData($modelClassName, $includeDrafts);
365  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
366  $where = RedBeanModelDataProvider::makeWhere(get_called_class(), $searchAttributeData, $joinTablesAdapter);
367  return self::getSubset($joinTablesAdapter, null, $limit, $where, 'name');
368  }
369 
374  public static function getDataAndLabelsByType($type)
375  {
376  assert('is_int($type)');
377  if (isset(self::$cachedDataAndLabelsByType[$type]))
378  {
379  return self::$cachedDataAndLabelsByType[$type];
380  }
381  $dataAndLabels = array();
382  $emailTemplates = static::getByType($type);
383  foreach ($emailTemplates as $emailTemplate)
384  {
385  $dataAndLabels[$emailTemplate->id] = strval($emailTemplate);
386  }
387  self::$cachedDataAndLabelsByType[$type] = $dataAndLabels;
388  return self::$cachedDataAndLabelsByType[$type];
389  }
390 
391  public static function getGamificationRulesType()
392  {
393  return 'EmailTemplateGamification';
394  }
395 
396  public static function hasReadPermissionsOptimization()
397  {
398  return true;
399  }
400 
401  protected static function translatedAttributeLabels($language)
402  {
403  return array_merge(parent::translatedAttributeLabels($language),
404  array(
405  'modelClassName' => Zurmo::t('Core', 'Module', null, null, $language),
406  'language' => Zurmo::t('Core', 'Language', null, null, $language),
407  'htmlContent' => Zurmo::t('EmailMessagesModule', 'Html Content', null, null, $language),
408  'name' => Zurmo::t('Core', 'Name', null, null, $language),
409  'subject' => Zurmo::t('Core', 'Subject', null, null, $language),
410  'type' => Zurmo::t('Core', 'Type', null, null, $language),
411  'textContent' => Zurmo::t('EmailMessagesModule', 'Text Content', null, null, $language),
412  )
413  );
414  }
415 
416  public function isContactTemplate()
417  {
418  return ($this->type == static::TYPE_CONTACT);
419  }
420 
421  public function isWorkflowTemplate()
422  {
423  return ($this->type == static::TYPE_WORKFLOW);
424  }
425 
426  public function isPlainTextTemplate()
427  {
428  return ($this->builtType == static::BUILT_TYPE_PLAIN_TEXT_ONLY);
429  }
430 
431  public function isPastedHtmlTemplate()
432  {
433  return ($this->builtType == static::BUILT_TYPE_PASTED_HTML);
434  }
435 
436  public function isBuilderTemplate()
437  {
438  return ($this->builtType == static::BUILT_TYPE_BUILDER_TEMPLATE);
439  }
440 
441  public function isPredefinedBuilderTemplate()
442  {
443  return ($this->isBuilderTemplate() && empty($this->modelClassName));
444  }
445 
446  public function __set($attributeName, $value)
447  {
448  parent::__set($attributeName, $value);
449  // we exclude predefined because:
450  // a- we don't want htmlContent compiled for those. It wont be used anywhere anyway.
451  // b- Using console installer we get errors due to getAssetManager(used in BuilderCanvasElement)
452  // not being available in CConsoleApplication
453  if ($attributeName == 'serializedData' &&
454  $this->isBuilderTemplate() &&
455  !$this->isPredefinedBuilderTemplate() &&
456  ArrayUtil::getArrayValue(CJSON::decode($this->serializedData), 'dom'))
457  {
458  $this->htmlContent = EmailTemplateSerializedDataToHtmlUtil::resolveHtmlBySerializedData($this->serializedData, false);
459  }
460  }
461 
462  public function validate(array $attributeNames = null, $ignoreRequiredValidator = false, $validateAll = false)
463  {
464  if ($validateAll == false)
465  {
466  $metadata = static::getMetadata();
467  $excludedMembers = array('textContent');
468  $members = $metadata['EmailTemplate']['members'];
469  // ignore content fields, we validate them inside the wizard form anyway.
470  $attributeNames = array_diff($members, $excludedMembers);
471  }
472  return parent::validate($attributeNames, $ignoreRequiredValidator);
473  }
474  }
475 ?>
static getByType($type, $includeDrafts=false)
static getPredefinedBuilderTemplatesSearchAttributeData()
setTreatCurrentUserAsOwnerForPermissions($value)
Definition: User.php:37
static getPreviouslyCreatedBuilderTemplateSearchAttributeData($modelClassName=null, $includeDrafts=false)
static getDataAndLabelsByType($type)
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static getPreviouslyCreatedBuilderTemplates($modelClassName=null, $includeDrafts=false, $limit=null)
static getPredefinedBuilderTemplates()
static resolveHtmlBySerializedData($serializedData, $renderForCanvas=false)
static getPluralLabel($language=null)
Generated on Thu Jul 9 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.