All Data Structures Functions Variables Pages
EmailAccount.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 
40  class EmailAccount extends Item
41  {
42  const DEFAULT_NAME = 'Default';
43 
44  public function __toString()
45  {
46  if (trim($this->name) == '')
47  {
48  return Zurmo::t('Core', '(Unnamed)');
49  }
50  return $this->name;
51  }
52 
53  public static function getModuleClassName()
54  {
55  return 'EmailMessagesModule';
56  }
57 
62  public static function getByUserAndName(User $user, $name = null)
63  {
64  if ($name == null)
65  {
66  $name = self::DEFAULT_NAME;
67  }
68  else
69  {
70  //For now Zurmo does not support multiple email accounts
71  throw new NotSupportedException();
72  }
73  assert('is_string($name)');
74  $bean = ZurmoRedBean::findOne(EmailAccount::getTableName(),
75  "_user_id = ? AND name = ?", array($user->id, $name));
76  assert('$bean === false || $bean instanceof RedBean_OODBBean');
77  if ($bean === false)
78  {
79  throw new NotFoundException();
80  }
81  else
82  {
83  $emailAccount = self::makeModel($bean);
84  }
85  return $emailAccount;
86  }
87 
96  public static function resolveAndGetByUserAndName(User $user, $name = null, $decrypt = true)
97  {
98  try
99  {
100  $emailAccount = static::getByUserAndName($user, $name);
101  if ($decrypt === true)
102  {
103  $emailAccount->outboundPassword = ZurmoPasswordSecurityUtil::decrypt($emailAccount->outboundPassword);
104  }
105  }
106  catch (NotFoundException $e)
107  {
108  $emailAccount = new EmailAccount();
109  $emailAccount->user = $user;
110  $emailAccount->name = self::DEFAULT_NAME;
111  $emailAccount->fromName = $user->getFullName();
112  if ($user->primaryEmail->id > 0 && $user->primaryEmail->emailAddress != null)
113  {
114  $emailAccount->fromAddress = $user->primaryEmail->emailAddress;
115  }
116  $emailAccount->useCustomOutboundSettings = EmailMessageUtil::OUTBOUND_GLOBAL_SETTINGS;
117  $emailAccount->outboundPort = '25';
118  $emailAccount->outboundType = EmailHelper::OUTBOUND_TYPE_SMTP;
119  }
120  return $emailAccount;
121  }
122 
123  public static function getDefaultMetadata()
124  {
125  $metadata = parent::getDefaultMetadata();
126  $metadata[__CLASS__] = array(
127  'members' => array(
128  'name',
129  'fromAddress',
130  'fromName',
131  'replyToName',
132  'replyToAddress',
133  'useCustomOutboundSettings',
134  'outboundType',
135  'outboundHost',
136  'outboundPort',
137  'outboundUsername',
138  'outboundPassword',
139  'outboundSecurity',
140  ),
141  'relations' => array(
142  'messages' => array(static::HAS_MANY, 'EmailMessage', static::NOT_OWNED,
143  static::LINK_TYPE_SPECIFIC, 'account'),
144  'user' => array(static::HAS_ONE, 'User'),
145  ),
146  'rules' => array(
147  array('fromName', 'required'),
148  array('fromAddress', 'required'),
149  array('name', 'type', 'type' => 'string'),
150  array('fromName', 'type', 'type' => 'string'),
151  array('replyToName', 'type', 'type' => 'string'),
152  array('outboundHost', 'type', 'type' => 'string'),
153  array('outboundUsername', 'type', 'type' => 'string'),
154  array('outboundPassword', 'type', 'type' => 'string'),
155  array('outboundSecurity', 'type', 'type' => 'string'),
156  array('outboundType', 'type', 'type' => 'string'),
157  array('outboundPort', 'type', 'type' => 'integer'),
158  array('useCustomOutboundSettings', 'type', 'type' => 'integer'),
159  array('fromName', 'length', 'max' => 64),
160  array('replyToName', 'length', 'max' => 64),
161  array('outboundType', 'length', 'max' => 4),
162  array('outboundHost', 'length', 'max' => 64),
163  array('outboundUsername', 'length', 'max' => 64),
164  array('outboundPassword', 'length', 'max' => 128),
165  array('outboundSecurity', 'length', 'max' => 3),
166  array('fromAddress', 'email'),
167  array('replyToAddress', 'email'),
168  array('useCustomOutboundSettings', 'validateCustomOutboundSettings',
169  'requiredAttributes' => array( 'outboundHost',
170  'outboundPort',
171  'outboundUsername',
172  'outboundPassword'))
173  )
174  );
175  return $metadata;
176  }
177 
178  public static function isTypeDeletable()
179  {
180  return true;
181  }
182 
188  protected static function getLabel($language = null)
189  {
190  return Zurmo::t('EmailMessagesModule', 'Email Account', array(), null, $language);
191  }
192 
198  protected static function getPluralLabel($language = null)
199  {
200  return Zurmo::t('EmailMessagesModule', 'Email Accounts', array(), null, $language);
201  }
202 
203  protected static function translatedAttributeLabels($language)
204  {
205  return array_merge(parent::translatedAttributeLabels($language),
206  array(
207  'fromAddress' => Zurmo::t('EmailMessagesModule', 'From Address', array(), null, $language),
208  'fromName' => Zurmo::t('EmailMessagesModule', 'From Name', array(), null, $language),
209  'messages' => Zurmo::t('Core', 'Messages', array(), null, $language),
210  'name' => Zurmo::t('Core', 'Name', array(), null, $language),
211  'outboundHost' => Zurmo::t('EmailMessagesModule', 'Outbound Host', array(), null, $language),
212  'outboundPassword' => Zurmo::t('EmailMessagesModule', 'Outbound Password', array(), null, $language),
213  'outboundPort' => Zurmo::t('EmailMessagesModule', 'Outbound Port', array(), null, $language),
214  'outboundSecurity' => Zurmo::t('EmailMessagesModule', 'Outbound Security', array(), null, $language),
215  'outboundType' => Zurmo::t('EmailMessagesModule', 'Outbound Type', array(), null, $language),
216  'outboundUsername' => Zurmo::t('EmailMessagesModule', 'Outbound Username', array(), null, $language),
217  'replyToAddress' => Zurmo::t('EmailMessagesModule', 'Reply To Address', array(), null, $language),
218  'replyToName' => Zurmo::t('EmailMessagesModule', 'Reply To Name', array(), null, $language),
219  'useCustomOutboundSettings' => Zurmo::t('EmailMessagesModule', 'Use the following outbound settings', array(), null, $language),
220  'user' => Zurmo::t('UsersModule', 'User', array(), null, $language),
221  )
222  );
223  }
224 
230  public function validateCustomOutboundSettings($attribute, $params)
231  {
232  if ($this->$attribute == EmailMessageUtil::OUTBOUND_PERSONAL_SMTP_SETTINGS)
233  {
234  $haveError = false;
235  foreach ($params['requiredAttributes'] as $attribute)
236  {
237  if ($this->$attribute == null)
238  {
239  $this->addError($attribute, Zurmo::t('EmailMessagesModule', 'This field is required'));
240  $haveError = true;
241  }
242  }
243  }
244  }
245 
249  public function afterValidate()
250  {
251  parent::afterValidate();
252  if ($this->outboundPassword !== null && $this->outboundPassword !== '')
253  {
254  $this->outboundPassword = ZurmoPasswordSecurityUtil::encrypt($this->outboundPassword);
255  }
256  }
257  }
258 ?>
static resolveAndGetByUserAndName(User $user, $name=null, $decrypt=true)
Definition: User.php:37
static decrypt($value, $salt=ZURMO_PASSWORD_SALT)
static encrypt($value, $salt=ZURMO_PASSWORD_SALT)
static getPluralLabel($language=null)
static getLabel($language=null)
static getByUserAndName(User $user, $name=null)
validateCustomOutboundSettings($attribute, $params)
Definition: Item.php:37
addError($attributeName, $errorMessage)
static getTableName()
Generated on Tue Feb 25 2020 07:10:32