-
Notifications
You must be signed in to change notification settings - Fork 988
/
FailedEmail.php
260 lines (244 loc) · 9.17 KB
/
FailedEmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* This is the model class for table "{{failed_emails}}".
*
* The following are the available columns in table '{{failed_emails}}':
* @property integer $id primary key
* @property integer $surveyid the surveyid this one belongs to
* @property integer $responseid the id of the participants response
* @property string $email_type the email type
* @property string $recipient the recipients email address
* @property string $language the email language
* @property string $error_message the error message
* @property string $created datetime when this entry is created
* @property string $status status in which this entry is default 'SEND FAILED'
* @property string $updated datetime when it was last updated
* @property Survey $survey the surveyobject related to the entry
* @property string $resend_vars json encoded values needed to resend the email [message_type,Subject,uniqueid,boundary[1],boundary[2],boundary[3],MIMEBody]
*
* @psalm-suppress InvalidScalarArgument
*/
class FailedEmail extends LSActiveRecord
{
/** @var string The success status */
public const STATE_SUCCESS = 'SEND SUCCESS';
/** @var string The failed status */
public const STATE_FAILED = 'SEND FAILED';
/**
* @inheritdoc
* @return string the associated database table name
*/
public function tableName(): string
{
return '{{failed_emails}}';
}
/**
* @inheritdoc
* @return array validation rules for model attributes.
*/
public function rules(): array
{
return [
['id, surveyid, responseid, email_type, recipient, error_message, created, resend_vars', 'required'],
['email_type', 'length', 'max' => 200],
['recipient', 'length', 'max' => 320],
['status', 'length', 'max' => 20],
['language', 'length', 'max' => 20],
['created, updated', 'safe'],
// The following rule is used by search().
['id, responseid, email_type, recipient, language, created, error_message, status, updated', 'safe', 'on' => 'search'],
];
}
/**
* @inheritdoc
* @return array relational rules.
*/
public function relations(): array
{
return [
'survey' => array(self::HAS_ONE, 'Survey', ['sid' => 'surveyid']),
];
}
/**
* @inheritdoc
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels(): array
{
return [
'id' => 'ID',
'recipient' => gt('Recipient'),
'email_type' => gt('Email type'),
'language' => gt('Email language'),
'created' => gt('Date'),
'status' => gt('Status'),
'updated' => gt('Updated'),
'error_message' => gt('Error message')
];
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search(): CActiveDataProvider
{
$pageSize = App()->user->getState('pageSize', App()->params['defaultPageSize']);
$criteria = new CDbCriteria();
$criteria->compare('id', $this->id);
$criteria->compare('surveyid', $this->surveyid);
$criteria->compare('responseid', $this->responseid);
$criteria->compare('email_type', $this->email_type, true);
$criteria->compare('recipient', $this->recipient, true);
$criteria->compare('language', $this->language, true);
$criteria->compare('error_message', $this->error_message, true);
$criteria->compare('created', $this->created, true);
$criteria->compare('status', $this->status, true);
$criteria->compare('updated', $this->updated, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => $pageSize
)
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return static the static model class
*/
public static function model($className = __CLASS__): FailedEmail
{
return parent::model($className);
}
public function getColumns(): array
{
return [
[
'id' => 'id',
'class' => 'CCheckBoxColumn',
'selectableRows' => '100',
],
[
'name' => 'buttons',
'type' => 'raw',
'filter' => false,
'header' => gT('Action'),
],
[
'header' => gT('Status'),
'name' => 'status',
'value' => '$data->status',
'htmlOptions' => ['class' => 'nowrap']
],
[
'header' => gT('Response ID'),
'name' => 'responseUrl',
'type' => 'raw',
'filter' => false,
],
[
'header' => gT('Created'),
'name' => 'created',
'value' => '$data->created',
'filter' => false,
'htmlOptions' => ['class' => 'nowrap']
],
[
'header' => gT('Updated'),
'name' => 'updated',
'value' => '$data->updated',
'filter' => false,
'htmlOptions' => ['class' => 'nowrap']
],
[
'header' => gT('Email type'),
'name' => 'email_type',
'value' => '$data->email_type',
'htmlOptions' => ['class' => 'nowrap']
],
[
'header' => gT("Recipient"),
'name' => 'recipient',
'value' => '$data->recipient',
'htmlOptions' => ['class' => 'nowrap']
],
[
'header' => gT('Language'),
'name' => 'language',
'value' => '$data->language',
],
];
}
public function getButtons(): string
{
$permissions = [
'update' => Permission::model()->hasSurveyPermission($this->surveyid, 'responses', 'update'),
'delete' => Permission::model()->hasSurveyPermission($this->surveyid, 'responses', 'delete'),
'read' => Permission::model()->hasSurveyPermission($this->surveyid, 'responses', 'read')
];
$buttons = App()->getController()->renderPartial('/failedEmail/partials/buttons', [
'id' => $this->id,
'permissions' => $permissions
], true);
return $buttons;
}
/**
* create a link to the response if it exists, else just return the id
* @return string
*/
public function getResponseUrl(): string
{
$survey = Survey::model()->findByPk($this->surveyid);
if ( $survey !== null && $survey->hasResponsesTable) {
$response = Response::model($this->surveyid)->findByPk($this->responseid);
if (!empty($response)) {
$responseUrl = App()->createUrl("responses/view/", ['surveyId' => $this->surveyid, 'id' => $this->responseid]);
$responseLink = '<a href="' . $responseUrl . '" role="button" data-toggle="tooltip" title="' . gT('View response details') . '">' . $this->responseid . '</a>';
} else {
$responseLink = (string)$this->responseid;
}
} else {
$responseLink = (string)$this->responseid;
}
return $responseLink;
}
/**
* @return string
* @throws CException
* @throws CHttpException
*/
public function getRawMailBody(): string
{
$mailer = \LimeMailer::getInstance();
$mailer->setSurvey($this->surveyid);
$mailer->setTypeWithRaw($this->email_type, $this->language);
$rawMail = $mailer->rawBody;
return $rawMail;
}
/**
* Returns array having surveyid as key and the localized survey title as value of all failed email entries
* @return array
*/
public function getFailedEmailSurveyTitles()
{
$allFailedEmails = $this->with('survey')->findAllByAttributes(
[],
"owner_id = :owner AND status != :status",
[':owner' => App()->user->id, ':status' => self::STATE_SUCCESS]
);
$groupedFailedEmails = [];
foreach ($allFailedEmails as $failedEmail) {
$groupedFailedEmails[$failedEmail->surveyid] = $failedEmail->survey->getLocalizedTitle();
}
return $groupedFailedEmails;
}
}