00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00040 class SendGridEmailAccount extends Item
00041 {
00042 const DEFAULT_NAME = 'Default';
00043
00044 public function __toString()
00045 {
00046 if (trim($this->name) == '')
00047 {
00048 return Zurmo::t('Core', '(Unnamed)');
00049 }
00050 return $this->name;
00051 }
00052
00056 public static function getModuleClassName()
00057 {
00058 return 'SendGridModule';
00059 }
00060
00065 public static function getByUserAndName(User $user, $name = null)
00066 {
00067 if ($name == null)
00068 {
00069 $name = self::DEFAULT_NAME;
00070 }
00071 else
00072 {
00073
00074 throw new NotSupportedException();
00075 }
00076 assert('is_string($name)');
00077 $bean = ZurmoRedBean::findOne(SendGridEmailAccount::getTableName(),
00078 "_user_id = ? AND name = ?", array($user->id, $name));
00079 assert('$bean === false || $bean instanceof RedBean_OODBBean');
00080 if ($bean === false)
00081 {
00082 throw new NotFoundException();
00083 }
00084 else
00085 {
00086 $emailAccount = self::makeModel($bean);
00087 }
00088 return $emailAccount;
00089 }
00090
00099 public static function resolveAndGetByUserAndName(User $user, $name = null, $decrypt = true)
00100 {
00101 try
00102 {
00103 $emailAccount = static::getByUserAndName($user, $name);
00104 if ($decrypt === true)
00105 {
00106 $emailAccount->apiPassword = ZurmoPasswordSecurityUtil::decrypt($emailAccount->apiPassword);
00107 }
00108 }
00109 catch (NotFoundException $e)
00110 {
00111 $emailAccount = new SendGridEmailAccount();
00112 $emailAccount->user = $user;
00113 $emailAccount->name = self::DEFAULT_NAME;
00114 $emailAccount->fromName = $user->getFullName();
00115 if ($user->primaryEmail->id > 0 && $user->primaryEmail->emailAddress != null)
00116 {
00117 $emailAccount->fromAddress = $user->primaryEmail->emailAddress;
00118 }
00119 }
00120 return $emailAccount;
00121 }
00122
00126 public static function getDefaultMetadata()
00127 {
00128 $metadata = parent::getDefaultMetadata();
00129 $metadata[__CLASS__] = array(
00130 'members' => array(
00131 'name',
00132 'fromAddress',
00133 'fromName',
00134 'replyToAddress',
00135 'apiUsername',
00136 'apiPassword'
00137 ),
00138 'relations' => array(
00139 'messages' => array(static::HAS_MANY, 'EmailMessage', static::NOT_OWNED,
00140 static::LINK_TYPE_SPECIFIC, 'sendgridAccount'),
00141 'user' => array(static::HAS_ONE, 'User'),
00142 ),
00143 'rules' => array(
00144 array('apiUsername', 'required'),
00145 array('apiPassword', 'required'),
00146 array('fromName', 'required'),
00147 array('fromAddress', 'required'),
00148 array('name', 'type', 'type' => 'string'),
00149 array('fromName', 'type', 'type' => 'string'),
00150 array('apiUsername', 'type', 'type' => 'string'),
00151 array('apiPassword', 'type', 'type' => 'string'),
00152 array('fromName', 'length', 'max' => 64),
00153 array('apiUsername', 'length', 'max' => 64),
00154 array('apiPassword', 'length', 'max' => 128),
00155 array('fromAddress', 'email'),
00156 array('replyToAddress', 'email')
00157 )
00158 );
00159 return $metadata;
00160 }
00161
00165 public static function isTypeDeletable()
00166 {
00167 return true;
00168 }
00169
00175 protected static function getLabel($language = null)
00176 {
00177 return Zurmo::t('SendGridModule', 'SendGrid Email Account', array(), null, $language);
00178 }
00179
00185 protected static function getPluralLabel($language = null)
00186 {
00187 return Zurmo::t('SendGridModule', 'SendGrid Email Accounts', array(), null, $language);
00188 }
00189
00193 protected static function translatedAttributeLabels($language)
00194 {
00195 return array_merge(parent::translatedAttributeLabels($language),
00196 array(
00197 'fromAddress' => Zurmo::t('EmailMessagesModule', 'From Address', array(), null, $language),
00198 'fromName' => Zurmo::t('EmailMessagesModule', 'From Name', array(), null, $language),
00199 'messages' => Zurmo::t('Core', 'Messages', array(), null, $language),
00200 'name' => Zurmo::t('Core', 'Name', array(), null, $language),
00201 'apiPassword' => Zurmo::t('SendGridModule', 'Api Password', array(), null, $language),
00202 'apiUsername' => Zurmo::t('SendGridModule', 'Api Username', array(), null, $language),
00203 'replyToAddress' => Zurmo::t('EmailMessagesModule', 'Reply To Address', array(), null, $language),
00204 'user' => Zurmo::t('UsersModule', 'User', array(), null, $language),
00205 )
00206 );
00207 }
00208
00213 public function afterValidate()
00214 {
00215 parent::afterValidate();
00216 if ($this->apiPassword !== null && $this->apiPassword !== '')
00217 {
00218 $this->apiPassword = ZurmoPasswordSecurityUtil::encrypt($this->apiPassword);
00219 }
00220 }
00221 }
00222 ?>