-
Notifications
You must be signed in to change notification settings - Fork 1
/
smartest.feed.inc
163 lines (151 loc) · 5.2 KB
/
smartest.feed.inc
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
<?php
/**
*@file
*/
function smartest_feedback_form($form) {
// @FIXME
// The Assets API has totally changed. CSS, JavaScript, and libraries are now
// attached directly to render arrays using the #attached property.
//
//
// @see https://www.drupal.org/node/2169605
// @see https://www.drupal.org/node/2408597
// drupal_add_css(drupal_get_path('module', 'smartest') . '/styles/feedback.css');
$query_permission = db_select('smartest_cache')
->fields('smartest_cache', array('type'))
->condition('cookie', 'permission_cookie', '=')
->execute()
->fetchAssoc();
$permision_value = 0;
if ($query_permission['type'] == 'default') {
$permision_value = 1;
}
$form['#attached']['css'] = drupal_get_path('module', 'smartest') . '/styles/feedback.css';
$form['feedback'] = array(
'#title' => t('Feedback'),
'#description' => t('Please, help us to improve SmarTest with your opinion.'),
'#type' => 'fieldset',
);
$form['feedback']['data'] = array(
'#title' => t('Identification data'),
'#type' => 'fieldset',
);
$form['feedback']['data']['name-line'] = array(
'#type' => 'fieldset',
);
$form['feedback']['data']['name-line']['name'] = array(
'#title' => t('Name'),
'#type' => t('textfield'),
);
$form['feedback']['data']['organization-line'] = array(
'#type' => 'fieldset',
);
$form['feedback']['data']['organization-line']['organization'] = array(
'#title' => t('Organization'),
'#type' => t('textfield'),
);
$form['feedback']['comment'] = array(
'#title' => t('Comment'),
'#type' => t('textarea'),
);
$form['feedback']['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
$form['feedback-perm']['profile']['permission'] = array(
'#type' => 'fieldset',
);
$form['feedback-perm']['profile']['permission']['auth'] = array(
'#type' => 'checkbox',
'#title' => t('Anonymous permissions'),
'#description' => t("Please, allow the Smartest's developers to know about its use."),
'#default_value' => $permision_value,
);
$form['feedback-perm']['profile']['permission']['update'] = array(
'#type' => 'submit',
'#value' => t('Update'),
'#submit' => array('smartest_update_permissions_submit'),
);
return $form;
}
function smartest_update_permissions_submit($form, &$form_state) {
$permision_value = 'no';
if ($form_state['values']['auth'] == 1) {
$permision_value = 'default';
}
db_update('smartest_cache')
->fields(array('type' => $permision_value))
->condition('cookie', 'permission_cookie', '=')
->execute();
}
function smartest_feedback_form_submit($form, &$form_state) {
if ($form_state['values']['name'] == "" || $form_state['values']['organization'] == "") {
drupal_set_message(t('Fields "name" and "organization" are required.'), 'error');
}
else {
smartest_mail_send($form_state['values']);
}
}
function smartest_mail($key, &$message, $params) {
$user = \Drupal::currentUser();
$options = array(
'langcode' => $message['language']->language,
);
switch ($key) {
case 'feedback':
$message['subject'] = t('Feedback e-mail sent from @site-name', array('@site-name' => \Drupal::config('system.site')->get('name')), $options);
$message['body'][] = t('@name sent you the following message:', array('@name' => $user->name), $options);
$message['body'][] = \Drupal\Component\Utility\SafeMarkup::checkPlain($params['name'] . ', from ' . $params['organization'] . ':\n ' . $params['comment']);
break;
}
}
/**
* Sends an e-mail.
*
* @param array $form_values
* An array of values from the contact form fields that were submitted.
* There are just two relevant items: $form_values['email'] and
* $form_values['message'].
*/
function smartest_mail_send($form_values) {
$module = 'smartest';
$key = 'feedback';
// @todo: This need to be fixed. Cannot be hardcode.
$to = '[email protected]';
// @FIXME
// This looks like another module's variable. You'll need to rewrite this call
// to ensure that it uses the correct configuration object.
// $from = variable_get('site_mail', '[email protected]');
$params = $form_values;
$language = language_default();
$send = TRUE;
$result = drupal_mail($module, $key, $to, $language, $params, \Drupal::config('system.site')->get('mail'), $send);
if ($result['result'] == TRUE) {
drupal_set_message(t('Your message has been sent.'));
}
else {
drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
}
}
/**
* Implements hook_mail_alter().
*
* This function is not required to send an email using Drupal's mail system.
*
* Hook_mail_alter() provides an interface to alter any aspect of email sent by
* Drupal. You can use this hook to add a common site footer to all outgoing
* email, add extra header fields, and/or modify the email in anyway. HTML-izing
* the outgoing email is one possibility.
*/
function smartest_mail_alter(&$message) {
$options = array(
'langcode' => $message['language']->language,
);
$signature = t("\n--\nMail altered.", array(), $options);
if (is_array($message['body'])) {
$message['body'][] = $signature;
}
else {
$message['body'] .= $signature;
}
}