1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Craft Contact Form Extensions plugin for Craft CMS 3.x. |
4
|
|
|
* |
5
|
|
|
* Adds extensions to the Craft CMS contact form plugin. |
6
|
|
|
* |
7
|
|
|
* @link https://rias.be |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Rias |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace rias\contactformextensions\elements; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\base\Element; |
16
|
|
|
use craft\elements\actions\Delete; |
17
|
|
|
use craft\elements\db\ElementQueryInterface; |
18
|
|
|
use rias\contactformextensions\elements\db\ContactFormSubmissionQuery; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Element. |
22
|
|
|
* |
23
|
|
|
* Element is the base class for classes representing elements in terms of objects. |
24
|
|
|
* |
25
|
|
|
* @property FieldLayout|null $fieldLayout The field layout used by this element |
26
|
|
|
* @property array $htmlAttributes Any attributes that should be included in the element’s DOM representation in the Control Panel |
27
|
|
|
* @property int[] $supportedSiteIds The site IDs this element is available in |
28
|
|
|
* @property string|null $uriFormat The URI format used to generate this element’s URL |
29
|
|
|
* @property string|null $url The element’s full URL |
30
|
|
|
* @property \Twig_Markup|null $link An anchor pre-filled with this element’s URL and title |
31
|
|
|
* @property string|null $ref The reference string to this element |
32
|
|
|
* @property string $indexHtml The element index HTML |
33
|
|
|
* @property bool $isEditable Whether the current user can edit the element |
34
|
|
|
* @property string|null $cpEditUrl The element’s CP edit URL |
35
|
|
|
* @property string|null $thumbUrl The URL to the element’s thumbnail, if there is one |
36
|
|
|
* @property string|null $iconUrl The URL to the element’s icon image, if there is one |
37
|
|
|
* @property string|null $status The element’s status |
38
|
|
|
* @property Element $next The next element relative to this one, from a given set of criteria |
39
|
|
|
* @property Element $prev The previous element relative to this one, from a given set of criteria |
40
|
|
|
* @property Element $parent The element’s parent |
41
|
|
|
* @property mixed $route The route that should be used when the element’s URI is requested |
42
|
|
|
* @property int|null $structureId The ID of the structure that the element is associated with, if any |
43
|
|
|
* @property ElementQueryInterface $ancestors The element’s ancestors |
44
|
|
|
* @property ElementQueryInterface $descendants The element’s descendants |
45
|
|
|
* @property ElementQueryInterface $children The element’s children |
46
|
|
|
* @property ElementQueryInterface $siblings All of the element’s siblings |
47
|
|
|
* @property Element $prevSibling The element’s previous sibling |
48
|
|
|
* @property Element $nextSibling The element’s next sibling |
49
|
|
|
* @property bool $hasDescendants Whether the element has descendants |
50
|
|
|
* @property int $totalDescendants The total number of descendants that the element has |
51
|
|
|
* @property string $title The element’s title |
52
|
|
|
* @property string|null $serializedFieldValues Array of the element’s serialized custom field values, indexed by their handles |
53
|
|
|
* @property array $fieldParamNamespace The namespace used by custom field params on the request |
54
|
|
|
* @property string $contentTable The name of the table this element’s content is stored in |
55
|
|
|
* @property string $fieldColumnPrefix The field column prefix this element’s content uses |
56
|
|
|
* @property string $fieldContext The field context this element’s content uses |
57
|
|
|
* |
58
|
|
|
* http://pixelandtonic.com/blog/craft-element-types |
59
|
|
|
* |
60
|
|
|
* @author Rias |
61
|
|
|
* |
62
|
|
|
* @since 1.0.0 |
63
|
|
|
*/ |
64
|
|
|
class ContactFormSubmission extends Element |
65
|
|
|
{ |
66
|
|
|
// Public Properties |
67
|
|
|
// ========================================================================= |
68
|
|
|
|
69
|
|
|
public $form; |
70
|
|
|
public $fromName; |
71
|
|
|
public $fromEmail; |
72
|
|
|
public $subject; |
73
|
|
|
public $message; |
74
|
|
|
|
75
|
|
|
public static function hasContent(): bool |
76
|
|
|
{ |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function isLocalized(): bool |
81
|
|
|
{ |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function find(): ElementQueryInterface |
86
|
|
|
{ |
87
|
|
|
return new ContactFormSubmissionQuery(static::class); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected static function defineSearchableAttributes(): array |
91
|
|
|
{ |
92
|
|
|
return ['form', 'subject', 'fromName', 'fromEmail']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getIsEditable(): bool |
96
|
|
|
{ |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getCpEditUrl() |
101
|
|
|
{ |
102
|
|
|
return 'contact-form-extensions/submissions/'.$this->id; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected static function defineSources(string $context = null): array |
106
|
|
|
{ |
107
|
|
|
$forms = array_unique(array_map(function (ContactFormSubmission $submission) { |
108
|
|
|
return $submission->form; |
109
|
|
|
}, self::find()->all())); |
110
|
|
|
|
111
|
|
|
$sources = [ |
112
|
|
|
[ |
113
|
|
|
'key' => '*', |
114
|
|
|
'label' => Craft::t('contact-form-extensions', 'All submissions'), |
115
|
|
|
'criteria' => [], |
116
|
|
|
], |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
foreach ($forms as $formHandle) { |
120
|
|
|
$sources[] = [ |
121
|
|
|
'key' => $formHandle, |
122
|
|
|
'label' => ucfirst($formHandle), |
123
|
|
|
'criteria' => ['form' => $formHandle], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $sources; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected static function defineActions(string $source = null): array |
131
|
|
|
{ |
132
|
|
|
$actions = []; |
133
|
|
|
|
134
|
|
|
$actions[] = Craft::$app->getElements()->createAction([ |
135
|
|
|
'type' => Delete::class, |
136
|
|
|
'confirmationMessage' => Craft::t('app', 'Are you sure you want to delete the selected entries?'), |
137
|
|
|
'successMessage' => Craft::t('app', 'Entries deleted.'), |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
return $actions; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected static function defineTableAttributes(): array |
144
|
|
|
{ |
145
|
|
|
$attributes = [ |
146
|
|
|
'id' => Craft::t('contact-form-extensions', 'ID'), |
147
|
|
|
'form' => Craft::t('contact-form-extensions', 'Form'), |
148
|
|
|
'subject' => Craft::t('contact-form-extensions', 'Subject'), |
149
|
|
|
'fromName' => Craft::t('contact-form-extensions', 'From Name'), |
150
|
|
|
'fromEmail' => Craft::t('contact-form-extensions', 'From Email'), |
151
|
|
|
'message' => Craft::t('contact-form-extensions', 'Message'), |
152
|
|
|
'dateCreated' => Craft::t('contact-form-extensions', 'Date Created'), |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
return $attributes; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected static function defineDefaultTableAttributes(string $source): array |
159
|
|
|
{ |
160
|
|
|
return [ |
161
|
|
|
'id', |
162
|
|
|
'form', |
163
|
|
|
'subject', |
164
|
|
|
'fromName', |
165
|
|
|
'fromEmail', |
166
|
|
|
'message', |
167
|
|
|
'dateCreated', |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getTableAttributeHtml(string $attribute): string |
172
|
|
|
{ |
173
|
|
|
if ($attribute == 'message') { |
174
|
|
|
$message = array_map('utf8_decode', (array) json_decode($this->message)); |
175
|
|
|
$html = '<ul>'; |
176
|
|
|
foreach ($message as $key => $value) { |
177
|
|
|
if (is_string($value)) { |
178
|
|
|
$shortened = trim(substr($value, 0, 30)); |
179
|
|
|
$html .= "<li><em>{$key}</em>: {$shortened}...</li>"; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
$html .= '</ul>'; |
183
|
|
|
|
184
|
|
|
return $html; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return parent::getTableAttributeHtml($attribute); // TODO: Change the autogenerated stub |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
protected static function defineSortOptions(): array |
191
|
|
|
{ |
192
|
|
|
$sortOptions = parent::defineSortOptions(); |
193
|
|
|
|
194
|
|
|
unset($sortOptions['dateCreated']); |
195
|
|
|
|
196
|
|
|
return $sortOptions; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param bool $isNew |
201
|
|
|
* |
202
|
|
|
* @throws \yii\db\Exception |
203
|
|
|
*/ |
204
|
|
|
public function afterSave(bool $isNew) |
205
|
|
|
{ |
206
|
|
|
if ($isNew) { |
207
|
|
|
Craft::$app->db->createCommand() |
208
|
|
|
->insert('{{%contactform_submissions}}', [ |
209
|
|
|
'id' => $this->id, |
210
|
|
|
'form' => $this->form, |
211
|
|
|
'subject' => $this->subject, |
212
|
|
|
'fromName' => $this->fromName, |
213
|
|
|
'fromEmail' => $this->fromEmail, |
214
|
|
|
'message' => $this->message, |
215
|
|
|
]) |
216
|
|
|
->execute(); |
217
|
|
|
} else { |
218
|
|
|
Craft::$app->db->createCommand() |
219
|
|
|
->update('{{%contactform_submissions}}', [ |
220
|
|
|
'form' => $this->form, |
221
|
|
|
'subject' => $this->subject, |
222
|
|
|
'fromName' => $this->fromName, |
223
|
|
|
'fromEmail' => $this->fromEmail, |
224
|
|
|
'message' => $this->message, |
225
|
|
|
], ['id' => $this->id]) |
226
|
|
|
->execute(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
parent::afterSave($isNew); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|