-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvopros_editorial.module
282 lines (263 loc) · 8.85 KB
/
vopros_editorial.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* @file
* Main module file for vopros_editorial.
*
* The vopros_editorial module allows to seperate questions into editorial
* groups, with their own type of macross etc.
*/
/**
* Implements hook_init().
*/
function vopros_editorial_init() {
drupal_add_css(drupal_get_path('module', 'vopros_editorial') . '/theme/vopros_editorial.base.css');
}
/**
* Implements hook_module_implements_alter().
*
* Ensure that vopros_service gets first shot at altering the
* vopros_question_client form.
*/
function vopros_editorial_module_implements_alter(&$implementations, $hook) {
// Fixing form_vopros_question_client_form_alter doesn't work, but this
// does.
if ($hook == 'form_alter') {
$group = $implementations['vopros_editorial'];
unset($implementations['vopros_editorial']);
$implementations['vopros_editorial'] = $group;
}
}
/**
* Configure editorial fields on vopros_question.
*/
function vopros_editorial_configure_question() {
$type = 'vopros_question';
$bundle = 'vopros_question';
// Create a taxonomy term reference field.
$field_name = 'vopros_editorial';
$field = field_info_field($field_name);
$instance = field_info_instance($type, $field_name, $bundle);
if (empty($field)) {
$field = array(
'cardinality' => 1,
'entity_types' => array($type),
'translatable' => FALSE,
'assigned' => TRUE,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'vopros_editorial',
'parent' => 0,
),
),
),
'field_name' => $field_name,
'type' => 'taxonomy_term_reference',
);
field_create_field($field);
}
if (empty($instance)) {
$instance = array(
'entity_type' => $type,
'bundle' => $bundle,
'required' => FALSE,
'settings' => array(),
'field_name' => $field_name,
'label' => t('Editorial'),
'widget' => array(
'type' => 'options_select',
),
'settings' => array(),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($instance);
}
}
/**
* Implements hook_vopros_question_activity_feed().
*/
function vopros_editorial_vopros_question_activity_feed($question, &$feed) {
$field_info = field_info_field('vopros_editorial');
// If we can get info on the field SQL we try to fetch the revisions.
if (isset($field_info['storage']['details']['sql']['FIELD_LOAD_REVISION'])) {
$sql_info = $field_info['storage']['details']['sql']['FIELD_LOAD_REVISION'];
$table = array_shift(array_keys($sql_info));
$column = array_shift($sql_info[$table]);
// Perform the actual query.
try {
$query = db_select($table, 'r');
$query->join('vopros_question_revision', 'v', 'r.revision_id = v.revision_id');
$query->addField('r', $column, 'tid');
$editorial_revisions = $query
->fields('r', array('revision_id'))
->fields('v', array('revision_timestamp', 'revision_uid'))
->condition('v.question_id', $question->question_id, '=')
->execute()
->fetchAll();
}
catch (PDOException $e) {
// Log error, and silently fail.
watchdog(
'vopros_editorial',
'SQL error trying to get activity stream for question: @question_id with error message: @message',
array('@question_id' => $question->question_id, '@message' => $e->errorInfo[2]),
WATCHDOG_ERROR
);
}
if (isset($editorial_revisions) && !empty($editorial_revisions)) {
$tid = $editorial_revisions[0]->tid;
foreach ($editorial_revisions as $editorial_revision) {
// Detect any change:
if ($editorial_revision->tid != $tid) {
$feed['vopros_editorial_' . $editorial_revision->revision_timestamp] = array(
'#theme' => 'vopros_editorial_field_feed',
'#revision' => $editorial_revision,
'#weight' => -$editorial_revision->revision_timestamp,
);
// Update the tid we're matching for.
$tid = $editorial_revision->tid;
}
}
}
}
}
/**
* Implements hook_views_default_views_alter().
*
* Add a filter to allow filtering on editorial on questions, public
* questions and service forwarded questions views.
*/
function vopros_editorial_views_default_views_alter(&$views) {
$vocab = taxonomy_vocabulary_machine_name_load('vopros_editorial');
$terms = taxonomy_term_load_multiple(FALSE, array('vid' => $vocab->vid));
if (!empty($terms)) {
$filter_views = array(
'vopros_question_list',
'vopros_question_published_list',
'vopros_service_forwarded_question_list',
);
foreach ($filter_views as $view_name) {
if (isset($views[$view_name])) {
$filters = $views[$view_name]->display['default']->display_options['filters'];
$filters['vopros_editorial_tid'] = array(
'id' => 'vopros_editorial_tid',
'table' => 'field_data_vopros_editorial',
'field' => 'vopros_editorial_tid',
'group' => 0,
'exposed' => TRUE,
'expose' => array(
'operator_id' => 'vopros_editorial_tid_op',
'label' => 'with the editorial',
'operator' => 'vopros_editorial_tid_op',
'use_operator' => FALSE,
'identifier' => 'vopros_editorial_tid',
'remember' => 1,
'multiple' => FALSE,
),
);
$views[$view_name]->display['default']->display_options['filters'] = $filters;
}
}
}
}
/**
* Implements hook_theme().
*/
function vopros_editorial_theme() {
$path = drupal_get_path('module', 'vopros_editorial') . '/theme';
return array(
'vopros_editorial_field_feed' => array(
'render element' => 'question',
'file' => 'vopros_editorial.theme.inc',
'path' => $path,
),
);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds default editorial selector to the client admin form if service is
* defined..
*/
function vopros_editorial_form_vopros_question_client_form_alter(&$form, &$form_state) {
if (isset($form['vopros_service'])) {
$client_id = $form['client']['#value']->client_id;
$vocab = taxonomy_vocabulary_machine_name_load('vopros_editorial');
$terms = taxonomy_term_load_multiple(FALSE, array('vid' => $vocab->vid));
if ($terms) {
$options = array(
0 => t('- None -'),
);
foreach ($terms as $term) {
$options[$term->tid] = $term->name;
}
$form['vopros_service']['default_editorial'] = array(
'#type' => 'select',
'#title' => t('Default editorial'),
'#description' => t('New incoming questions will get this editorial.'),
'#options' => $options,
'#default_value' => variable_get('vopros_editorial_default_service_' . $client_id, 0),
);
}
$form['#submit'][] = 'vopros_editorial_form_vopros_question_client_form_submit';
}
}
/**
* Submit handler for the vopros_question_client_form.
*
* Saves service default editorial.
*/
function vopros_editorial_form_vopros_question_client_form_submit($form, &$form_state) {
$variable_name = 'vopros_editorial_default_service_' .
$form['client']['#value']->client_id;
if ($form_state['values']['vopros_service']['enabled'] &&
!empty($form_state['values']['vopros_service']['default_editorial'])) {
variable_set($variable_name, $form_state['values']['vopros_service']['default_editorial']);
}
else {
variable_del($variable_name);
}
}
/**
* Implements hook_entity_delete().
*
* Delete the default editorial variable when deleting service entity.
*/
function vopros_editorial_entity_delete($entity, $entity_type) {
if ($entity_type == 'vopros_question_client') {
variable_del('vopros_editorial_default_service_' . $entity->client_id);
}
}
/**
* Implements hook_entity_presave().
*
* Set editorial for service created questions.
* Reactivate questions when changing editorial.
*/
function vopros_editorial_entity_presave($entity, $entity_type) {
if ($entity_type == 'vopros_question') {
if ($entity->is_new) {
$variable_name = 'vopros_editorial_default_service_' .
$entity->vopros_service->client_id;
if (!empty($entity->vopros_service) &&
($tid = variable_get($variable_name, 0))) {
$wrapper = entity_metadata_wrapper('vopros_question', $entity);
$wrapper->vopros_editorial->set($tid);
}
}
else {
// If the editorial changed, reactivate the question.
$previous = entity_load_unchanged('vopros_question', $entity->question_id);
$wrapper = entity_metadata_wrapper('vopros_question', $entity);
$wrapper2 = entity_metadata_wrapper('vopros_question', $previous);
if ($wrapper->vopros_editorial->value() != $wrapper2->vopros_editorial->value()) {
$entity->question_status = 'active';
}
}
}
}