This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
forked from BlakeLucchesi/camp_schedule
-
Notifications
You must be signed in to change notification settings - Fork 1
/
camp_schedule.admin.inc
153 lines (141 loc) · 5.43 KB
/
camp_schedule.admin.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
<?php
/**
* Schedule settings.
*/
function camp_schedule_settings_form() {
$types = node_get_types();
foreach ($types as $type) {
$options[$type->type] = $type->name;
}
$form['camp_schedule_node_type'] = array(
'#title' => t('Schedule Node Type'),
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('camp_schedule_node_type', NULL),
);
$form['camp_schedule_alert_conflict'] = array(
'#title' => t('Alert Conflicts'),
'#description' => t('When saving schedule assignments, are multiple sessions allowed in the same time slot?'),
'#type' => 'checkbox',
'#default_value' => variable_get('camp_schedule_alert_conflict', FALSE),
);
return system_settings_form($form);
}
/**
* Manage schedule form.
*/
function camp_schedule_manage_form(&$form_state) {
$form = array();
$form['#content_extra_fields'] = $type['extra'];
$form['#pre_render'][] = 'content_alter_extra_weights';
module_load_include('inc', 'content', 'includes/content.node_form');
module_load_include('inc', 'content', 'includes/content.admin');
// Get all of our form options such as day, time, room.
// Define the field names we'd like to render.
$render = array('field_session_day', 'field_session_time', 'field_session_room');
$type = variable_get('camp_schedule_node_type', 'session');
$cck_fields = content_types($type);
drupal_add_js("Drupal.behaviors.camp_schedule = function() {
$('tr', '#camp-schedule-manage-form').hover(function() {
$(this).find('td').css('background-color', '#FFFCCC');
}, function() {
$(this).find('td').css('background-color', 'transparent');
});
}", 'inline');
// Find all of our session nodes.
$form['#tree'] = TRUE;
foreach (camp_schedule_load_nodes() as $node) {
// We need to put the node object in $form['#node'] so that content_field_form can use it to put together the field.
if (isset($node->nid)) {
$form['#node'] = $node;
$form['nodes'][$node->nid]['title'] = array('#value' => l($node->title, 'node/'. $node->nid));
foreach ($cck_fields['fields'] as $field_name => $field) {
if (in_array($field_name, $render)) {
$form['#field_info'][$field['field_name']] = $field;
$form['nodes'][$node->nid] += content_field_form($form, $form_state, $field);
}
}
$form['nodes'][$node->nid]['field_session_day']['#title'] = '';
$form['nodes'][$node->nid]['field_session_room']['#title'] = '';
$form['nodes'][$node->nid]['field_session_time']['#title'] = '';
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
if (!isset($form['#after_build'])) {
$form['#after_build'] = array();
}
$form['#after_build'][] = 'camp_schedule_manage_form_after_build';
return $form;
}
/**
* "After build" callback for node forms. Register the fields you want to alter.
*/
function camp_schedule_manage_form_after_build($form, &$form_state) {
foreach ($form['nodes'] as $nid => $field) {
unset($form['nodes'][$nid]['field_session_room']['value']['#options']['bof1']);
unset($form['nodes'][$nid]['field_session_room']['value']['#options']['bof2']);
unset($form['nodes'][$nid]['field_session_room']['value']['#options']['bof3']);
unset($form['nodes'][$nid]['field_session_room']['value']['#options']['bof4']);
}
return $form;
}
/**
* Form theme function.
*/
function theme_camp_schedule_manage_form($form) {
$header = array('Session Title', 'Day', 'Room', 'Time');
if (is_array($form['nodes'])) {
foreach ($form['nodes'] as $nid => $node) {
if (is_numeric($nid)) {
$rows[] = array(
drupal_render($form['nodes'][$nid]['title']),
drupal_render($form['nodes'][$nid]['field_session_day']),
drupal_render($form['nodes'][$nid]['field_session_room']),
drupal_render($form['nodes'][$nid]['field_session_time'])
);
}
}
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Validation callback.
*/
function camp_schedule_manage_form_validate($form, &$form_state) {
$alert_conflict = variable_get('camp_schedule_alert_conflict', FALSE);
if ($alert_conflict) {
foreach ($form_state['values']['nodes'] as $nid => $value) {
if (is_numeric($nid)) {
$room = $form_state['values']['nodes'][$nid]['field_session_room'][0]['value'];
$day = $form_state['values']['nodes'][$nid]['field_session_day'][0]['value'];
$time = $form_state['values']['nodes'][$nid]['field_session_time'][0]['value'];
if ($room && $day && $time) {
// Check if a session is already in thist spot.
if ($values[$room][$day][$time]) {
form_set_error('nodes]['. $nid .'][field_session_time', 'Scheduling conflict.');
}
$values[$room][$day][$time] = TRUE;
}
}
}
}
}
/**
* Submit callback.
*/
function camp_schedule_manage_form_submit($form, &$form_state) {
foreach ($form_state['values']['nodes'] as $nid => $value) {
if (is_numeric($nid)) {
$node = node_load($nid);
$node->field_session_day[0]['value'] = $form_state['values']['nodes'][$nid]['field_session_day'][0]['value'];
$node->field_session_time[0]['value'] = $form_state['values']['nodes'][$nid]['field_session_time'][0]['value'];
$node->field_session_room[0]['value'] = $form_state['values']['nodes'][$nid]['field_session_room'][0]['value'];
node_save($node);
}
}
}