-
Notifications
You must be signed in to change notification settings - Fork 0
/
os2web_citizen_proposals.module
216 lines (180 loc) · 6.01 KB
/
os2web_citizen_proposals.module
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
<?php
/**
* @file
* OS2Web Citizen Proposals module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;
use Drupal\os2web_citizen_proposals\Form\SettingsForm;
use Drupal\comment\Entity\Comment;
/**
* Implements hook_theme().
*/
function os2web_citizen_proposals_theme($existing, $type, $theme, $path) {
return [
'paragraph__os2web_citizen_proposals__default' => [
'template' => 'paragraph--os2web-citizen-proposals',
'base hook' => 'paragraph',
'variables' => [
'new_proposal_form' => NULL,
'active_proposals' => NULL,
],
],
];
}
/**
* Implements hook_preprocess_paragraph().
*
* Adding proposal page into into paragraph.
*/
function os2web_citizen_proposals_preprocess_paragraph__os2web_citizen_proposals(&$variables) {
if ($variables['view_mode'] == 'preview') {
return;
}
$form = \Drupal::formBuilder()->getForm('Drupal\os2web_citizen_proposals\Form\CreateProposalForm');
$variables['new_proposal_form'] = $form;
$query = \Drupal::entityQuery('node');
$query->accessCheck(false);
$query->condition('status', 1);
$query->condition('type', 'os2web_citizen_proposals');
$entity_ids = $query->execute();
if (!empty($entity_ids)) {
$proposals = Node::loadMultiple($entity_ids);
foreach ($proposals as $proposal) {
$variables['published_proposals'][$proposal->id()] = [
'proposal_node' => $proposal,
'vote_form' => \Drupal::formBuilder()->getForm('Drupal\os2web_citizen_proposals\Form\ProposalVoteForm', $proposal->id()),
'votes_count' => $proposal->get('field_os2web_cit_props_votes')->comment_count,
];
}
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function os2web_citizen_proposals_node_presave(EntityInterface $entity) {
if ($entity->bundle() != 'os2web_citizen_proposals') {
return;
}
// Skip for new nodes.
if ($entity->isNew()) {
return;
}
// Publish node if proposal is accepted.
// Unpublish node if proposal is rejected.
$status = $entity->field_os2web_cit_props_status->value;
if ($status == 'accept') {
$entity->setPublished();
$config = \Drupal::config('os2web_citizen_proposals.settings');
$writePublishDate = ($config->get('proposal_publish_date_on_author') == 1);
if ($writePublishDate && $entity->original->field_os2web_cit_props_status->value != $status) {
$entity->setCreatedTime(time());
}
} elseif ($status == 'reject') {
$entity->setUnpublished();
}
// Send emails only if status updated.
if ($entity->original->field_os2web_cit_props_status->value != $status) {
/** @var \Drupal\os2web_citizen_proposals\Service\ProposalEmailService $emailService */
$emailService = \Drupal::service('os2web_citizen_proposals.email_service');
if ($status == 'accept') {
$emailService->sendUserProposalAcceptedEmail($entity);
}
elseif ($status == 'reject') {
$emailService->sendUserProposalRejectedEmail($entity);
}
}
}
/**
* Implements hook_mail().
*/
function os2web_citizen_proposals_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
if (!isset($params['from'])) {
$params['from'] = \Drupal::config('system.site')->get('mail');
}
if (!isset($params['sender'])) {
$params['sender'] = \Drupal::config('system.site')->get('name');
}
$params['sender'] = '=?UTF-8?B?' . base64_encode($params['sender']) . '?=';
$message['headers'] = array_merge($message['headers'], [
'Content-Type' => 'text/html; charset=UTF-8;',
'Content-Transfer-Encoding' => '8Bit',
'MIME-Version' => '1.0',
'Reply-To' => $params['from'],
]);
if (isset($params['Cc'])) {
$message['headers']['Cc'] = $params['Cc'];
}
if (isset($params['Bcc'])) {
$message['headers']['Bcc'] = $params['Bcc'];
}
}
/**
* Implements hook_cron().
*/
function os2web_citizen_proposals_cron() {
os2web_citizen_proposals_expire_nodes();
os2web_citizen_proposals_delete_nodes();
}
/**
* Unpublish proposals when they are older than specified period.
*/
function os2web_citizen_proposals_expire_nodes() {
$config = \Drupal::config(SettingsForm::$configName);
$publishPeriodMonths = $config->get('proposal_publish_period_months');
if (!$publishPeriodMonths) {
$publishPeriodMonths = 2;
}
$publishPeriodTs = strtotime("-$publishPeriodMonths months");
$query = \Drupal::entityQuery('node');
$query->accessCheck(false);
$query->condition('status', 1);
$query->condition('type', 'os2web_citizen_proposals');
$query->condition('created', $publishPeriodTs, '<');
$entity_ids = $query->execute();
if (empty($entity_ids)) {
return;
}
$max = $config->get('proposal_max_votes');
$nodes = Node::loadMultiple($entity_ids);
$emailService = \Drupal::service('os2web_citizen_proposals.email_service');
$nodes = Node::loadMultiple($entity_ids);
/** @var \Drupal\node\NodeInterface $node */
foreach ($nodes as $node) {
$votes = $node->get('field_os2web_cit_props_votes')->comment_count;
if ($votes < $max ) {
$emailService->sendUserProposalNotMaxedEmail($node);
}
if ($node->hasField('field_os2web_cit_props_status')) {
$node->field_os2web_cit_props_status = "expired";
}
$node->setUnpublished();
$node->save();
}
}
/**
* Deletes proposals when they are older than specified period.
*/
function os2web_citizen_proposals_delete_nodes() {
$config = \Drupal::config(SettingsForm::$configName);
$lifePeriodMonths = $config->get('proposal_life_period_months');
if (!$lifePeriodMonths) {
$lifePeriodMonths = 24;
}
$lifePeriodTs = strtotime("-$lifePeriodMonths months");
$query = \Drupal::entityQuery('node');
$query->accessCheck(false);
$query->condition('type', 'os2web_citizen_proposals');
$query->condition('created', $lifePeriodTs, '<');
$entity_ids = $query->execute();
if (empty($entity_ids)) {
return;
}
$nodes = Node::loadMultiple($entity_ids);
/** @var \Drupal\node\NodeInterface $node */
foreach ($nodes as $node) {
$node->delete();
}
}