-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnn_show.module
321 lines (278 loc) · 9.93 KB
/
mnn_show.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* @file
* Code for the MNN Show Import feature.
*/
include_once('mnn_show.features.inc');
define('MNN_PROJECT_OG_FIELD_FOR_SHOW','og_node2');
/**
* Implements hook_form_FORM_ID_alter().
*/
function mnn_show_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'cm_show_node_form') {
$show_node = (isset($form['#node']) && $form['#node']->nid) ?
$form['#node'] : NULL;
$project_id_from_query_string =
filter_input(INPUT_GET, CM_PROJECT_ID_QUERY_ARGUMENT_NAME);
if ($show_node) {
$project_field_on_show = &$show_node->{MNN_PROJECT_OG_FIELD_FOR_SHOW};
$project_id = ($project_field_on_show) ?
$project_field_on_show[LANGUAGE_NONE][0]['target_id'] : NULL;
}
else if ($project_id_from_query_string) {
$project_id = $project_id_from_query_string;
}
if ($project_id) {
$project_node = node_load($project_id);
if ($project_node) {
$default_project_for_show = $project_node->title." (".$project_id.")";
if (!$show_node) {
cm_project_synch_fields_for_child($form, $project_node);
}
}
}
$project_widget = &$form[MNN_PROJECT_OG_FIELD_FOR_SHOW][LANGUAGE_NONE][0];
$default_sub_field = &$project_widget['default'][0]['target_id'];
$admin_sub_field = &$project_widget['admin'][0]['target_id'];
if ($default_project_for_show) {
$form['title']['#type'] = "hidden";
$form['title']['#value'] = NULL;
$default_sub_field['#value'] = NULL;
$admin_sub_field['#value'] = $default_project_for_show;
$default_sub_field['#type'] = "hidden";
$admin_sub_field['#type'] = "hidden";
$form[MNN_PROJECT_OG_FIELD_FOR_SHOW]['cm_project_picker_title'] =
array('#markup' =>
"<div class='cm-box'><h2>Project</h2><big><strong>".
l($project_node->title, "node/".$project_id).
"</big></strong><br/><br/></div>",
'#weight' => -100);
}
else {
$default_sub_field['#value'] = NULL;
$admin_sub_field['#value'] = NULL;
$admin_sub_field['#default_value'] = NULL;
$admin_sub_field['#default_value'] = NULL;
$default_sub_field['#type'] = "hidden";
$default_sub_field['#type'] = "hidden";
$form['title']['#type'] = "hidden";
$form['title']['#value'] = NULL;
drupal_set_message(t("You must use the project picker to add a show."),
'error');
}
if (!$show_node) {
$airings = mnn_show_airings_for_project($project_id);
}
if (isset($airings)) {
$form['mnn_cm_airing'] =
array(
'#type' => 'select',
'#options'=> $airings,
'#prefix' => '<div id="mnn_cm_airing_wrapper" class="cm-box">'.
"<h2>".t('Air Date')."</h2>",
'#suffix' => '</div>',
);
}
if (!$show_node) {
$episode_number = mnn_show_get_next_episode_number($project_id);
$form['field_episode_number'][LANGUAGE_NONE][0]
['value']['#default_value'] = $episode_number;
}
}
}
function mnn_show_node_validate($node, $form, &$form_state) {
if ($node->type=='cm_show') {
$project_id = $node->{MNN_PROJECT_OG_FIELD_FOR_SHOW};
$project_id = $project_id[LANGUAGE_NONE][0]['target_id'];
$show_id = ($node->nid) ? $node->nid : NULL;
$episode_number = $node->field_episode_number[LANGUAGE_NONE][0]['value'];
$existing_show_id =
mnn_show_get_existing_show_for_episode_number($project_id,
$show_id,
$episode_number);
$existing_show = node_load($existing_show_id);
if($existing_show) {
form_set_error('field_episode_number',
t("That episode number is already in use. ").
l($existing_show->title, "node/".$existing_show->nid));
}
//don't allow new shows to be added to inactive projects
if (!$show_id) {
$project = node_load($project_id);
if (!$project->group_group[LANGUAGE_NONE][0]['value']) {
form_set_error(MNN_PROJECT_OG_FIELD_FOR_SHOW,
t("This project is not active. The project must be
active to add new shows"));
}
}
}
}
function mnn_show_get_existing_show_for_episode_number($project_id,
$show_id,
$episode_number){
$sql =
"SELECT s.nid as existing_show_id
FROM {node} s
LEFT JOIN {og_membership} og_membership
ON (s.nid = og_membership.etid AND
og_membership.entity_type = 'node')
LEFT JOIN {node} p
ON og_membership.gid = p.nid
INNER JOIN {field_data_field_episode_number} epnum
ON epnum.revision_id=s.vid
WHERE (p.nid = $project_id )
AND (s.status = '1')
AND (s.type IN ('cm_show'))
AND (epnum.field_episode_number_value = :episode_number)";
$params = array(':episode_number'=>$episode_number);
if ($show_id) {
$sql .= " AND (s.nid != :show_id)";
$params[':show_id'] = $show_id;
}
$result = db_query($sql, $params)->fetchObject();
return $result->existing_show_id;;
}
function mnn_show_get_next_episode_number($project_id) {
$sql =
"SELECT (max(convert(epnum.field_episode_number_value,unsigned)) + 1)
AS episode_number
FROM {node} s
LEFT JOIN {og_membership} og_membership
ON (s.nid = og_membership.etid AND
og_membership.entity_type = 'node')
LEFT JOIN {node} p
ON og_membership.gid = p.nid
INNER JOIN field_data_field_episode_number epnum
ON epnum.revision_id=s.vid
WHERE (p.nid = :project_id )
AND (s.status = '1')
AND (s.type IN ('cm_show'))
ORDER BY epnum.field_episode_number_value desc";
$result = db_query($sql, array(':project_id'=>$project_id))->fetchObject();
$episode_number = $result->episode_number ? $result->episode_number : 1;
return $episode_number;
}
function mnn_show_og_node2_callback($form, $form_state) {
return $form['mnn_cm_airing'];
}
function mnn_show_node_presave($node) {
if ($node->type=='cm_show') {
//SET THE TITLE
$short_description = $node->field_short_description ?
$node->field_short_description[LANGUAGE_NONE][0]['value'] : "";
$episode_number = $node->field_episode_number ?
$node->field_episode_number[LANGUAGE_NONE][0]['value'] : "";
$project_id = $node->{MNN_PROJECT_OG_FIELD_FOR_SHOW} ?
$node->{MNN_PROJECT_OG_FIELD_FOR_SHOW}[LANGUAGE_NONE][0]['target_id']:"";
$project = node_load($project_id);
$project_title = $project->title;
$node->title = ($short_description) ?
$project->title." Episode ".$episode_number.": ".$short_description :
$project->title." Episode ".$episode_number;
//RESAVE ALL RELATED AIRINGS
if ($node->nid) {
$airings = mnn_show_airings_for_show($node->nid);
foreach($airings as $airing_id) {
$airings = entity_load('airing', array($airing_id));
$airing = array_pop($airings);
if ($airing) {
$airing->cm_use_auto_title = TRUE;
$airing->cm_use_auto_endtime = TRUE;
//FIXME SHOULD CHANGE THIS NAME TO FIT BOTH UPDATE AND CREATE
$airing->cm_newly_created_show = $node;
entity_save('airing', $airing);
}
}
}
}
}
function mnn_show_node_insert($node) {
if ($node->type=='cm_show') {
$airing_id = (isset($node->mnn_cm_airing) && $node->mnn_cm_airing) ?
$node->mnn_cm_airing : NULL;
if ($airing_id) {
$airings = entity_load('airing', array($airing_id));
$airing = array_pop($airings);
if ($airing) {
$airing->field_airing_show_ref[LANGUAGE_NONE][0]['target_id'] =
$node->nid;
$airing->cm_use_auto_title = TRUE;
$airing->cm_use_auto_endtime = TRUE;
$airing->cm_newly_created_show = $node;
entity_save('airing', $airing);
}
}
}
}
/**
* This function is called after a Project has been selected, it will
* return a dropdown of airings for a project.
*/
function mnn_show_airings_for_project($project_id = NULL) {
$airdates = array(0=>"- Select a value -");;
$results = db_query(
"SELECT a.airing_id,td.name as channel, at.field_airing_date_value
FROM {cm_airing} a
INNER JOIN {field_data_field_airing_date} at
ON a.airing_id=at.entity_id
INNER JOIN {field_data_field_airing_project_ref} p
ON p.entity_id=at.entity_id
LEFT JOIN {field_data_field_airing_show_ref} s
ON s.entity_id = at.entity_id
INNER JOIN {field_data_field_airing_channel} c
ON a.airing_id = c.entity_id
INNER JOIN {taxonomy_term_data} td
ON td.tid = c.field_airing_channel_tid
WHERE p.field_airing_project_ref_target_id = :pid
AND s.field_airing_show_ref_target_id is NULL
AND at.field_airing_date_value > CURDATE()
ORDER BY at.field_airing_date_value asc
", array(':pid'=>$project_id));
while ($airdate = $results->fetchObject()) {
$txt = date('m-d-y h:ia',
strtotime($airdate->field_airing_date_value))." ".$airdate->channel;
$airdates[$airdate->airing_id] = $txt;
}
return $airdates;
}
/**
* THIS WILL RETURN ALL THE AIRING FOR A SHOW
*/
function mnn_show_airings_for_show($show_id = NULL) {
$ret = array();
$results = db_query(
"SELECT a.airing_id
FROM {cm_airing} a
LEFT JOIN {field_data_field_airing_show_ref} s
ON s.entity_id = a.airing_id
WHERE s.field_airing_show_ref_target_id = :sid
", array(':sid'=>$show_id));
while ($airdate = $results->fetchObject()) {
$ret[$airdate->airing_id] = $airdate->airing_id;
}
return $ret;
}
function mnn_show_node_access($node, $op, $account) {
$type = is_string($node) ? $node : $node->type;
if ($type == 'cm_show' && $op == 'update') {
if (user_access('edit any ' . $type . ' content', $account) ||
(user_access('edit own ' . $type . ' content', $account) &&
($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
else {
return NODE_ACCESS_DENY;
}
}
if ($type == 'cm_show' && $op == 'delete') {
if (user_access('delete any ' . $type . ' content', $account) ||
(user_access('delete own ' . $type . ' content', $account) &&
($account->uid == $node->uid))) {
return NODE_ACCESS_ALLOW;
}
else {
return NODE_ACCESS_DENY;
}
}
return NODE_ACCESS_IGNORE;
}