-
Notifications
You must be signed in to change notification settings - Fork 21
/
oa_sections.module
313 lines (289 loc) · 10.2 KB
/
oa_sections.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
<?php
/**
* @file
* Code for the OpenAtrium2 Section Pages feature.
*/
include_once 'oa_sections.features.inc';
/**
* Name of default OpenAtrium Group field in Section.
*/
define('OA_GROUP_FIELD', 'field_oa_group_ref');
/**
* Name of default OpenAtrium Team field in Section.
*/
define('OA_TEAM_FIELD', 'field_oa_team_ref');
/**
* Name of default OpenAtrium User field in Section.
*/
define('OA_USER_FIELD', 'field_oa_user_ref');
// Default sections to enabled but hidden.
define('OA_SECTION_TYPE_MENU_DEFAULT', 0);
// Default sections to enabled and visible.
define('OA_SECTION_TYPE_MENU_VISIBLE', 1);
// Default sections to not use menus.
define('OA_SECTION_TYPE_MENU_NONE', 2);
/**
* Implements hook_menu().
*/
function oa_sections_menu() {
$items = array();
$items['node/add/oa-section/%'] = array(
'title' => 'Create Section',
'page callback' => 'oa_core_create_space_page_callback',
'page arguments' => array('oa_section', 3),
'access callback' => 'node_access',
'access arguments' => array('create', 'oa_section'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_node_update().
*/
function oa_sections_node_update($node) {
// If the Section has been moved to a new Space, then we need to update all
// the content inside the Section to also be in the new Space.
if (!empty($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'])) {
$original = $node->original;
$space_nid = $node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'];
if ($original->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'] != $space_nid) {
$child_nids = oa_core_get_section_content($node->nid);
// For 10 nodes or less, we update them inline (per Mike Potter).
if (count($child_nids) <= 10) {
foreach ($child_nids as $nid) {
_oa_sections_batch_change_space_operation($nid, $space_nid);
}
} else {
// Create a batch operation to actually update the child nodes.
$batch = array(
'title' => t('Moving content to new Space'),
'operations' => array(),
);
foreach ($child_nids as $nid) {
$batch['operations'][] = array('_oa_sections_batch_change_space_operation', array($nid, $space_nid));
}
batch_set($batch);
}
}
}
}
/**
* Batch operation that changes a node's Space.
*
* @param int $nid
* The NID of the node to update.
* @param int $space_nid
* The NID of the Space the node will belong to after changed.
*/
function _oa_sections_batch_change_space_operation($nid, $space_nid) {
$node = node_load($nid);
$node->og_group_ref[LANGUAGE_NONE][0]['target_id'] = $space_nid;
node_save($node);
}
/**
* Implements hook_og_menu_single_menu_parent().
*/
function oa_sections_og_menu_single_menu_parent($conf) {
if ($plid = og_menu_single_get_active_plid()) {
$node = menu_get_object();
if ($node && $node->type == 'oa_section') {
// Menu will be cached via core, so just get it.and look for the parent
$section_id = $node->nid;
}
elseif ($node && ($values = field_get_items('node', $node, 'oa_section_ref'))) {
$value = reset($values);
$section_id = $value['target_id'];
}
else {
$section_id = oa_core_get_section_context();
}
// If in a section, find the the secondary link that section is in.
if ($section_id) {
if ($mlid = og_menu_single_get_link_mlid('node', $section_id)) {
foreach (og_menu_single_children_items($plid) as $item) {
if (_oa_sections_is_parent($item, $mlid)) {
return $item['link']['mlid'];
}
}
}
}
}
}
/**
* Helpder function to determine if an item or it's children have a mlid.
*/
function _oa_sections_is_parent($item, $mlid) {
if ($item['link']['mlid'] == $mlid) {
return TRUE;
}
foreach ($item['below'] as $child_item) {
if (_oa_sections_is_parent($child_item, $mlid)) {
return TRUE;
}
}
}
/**
* Implements hook_query_TAG_alter().
*/
function oa_sections_query_entityreference_alter($query) {
$field = $query->alterMetaData['entityreference_selection_handler']->field;
$current_query_conditions = $query->alterMetaData['entity_field_query']->entityConditions;
if ($field && $field['field_name'] == 'oa_section_ref' && empty($current_query_conditions['entity_id'])) {
$entity = $query->alterMetaData['entityreference_selection_handler']->entity;
// A space is active, limit to sections in that space.
if ($value = _oa_sections_get_current_selected_space($entity)) {
$query->join('og_membership', 'omrol', 'node.nid = omrol.etid and omrol.entity_type = :entity_type AND omrol.field_name = :field_name AND omrol.gid = :target_id', array(':target_id' => $value, ':entity_type' => 'node', ':field_name' => 'og_group_ref'));
}
// Don't want to show all sections ever, so zero it out.
else {
$query->condition('1', '0', '=');
}
}
}
/**
* Get the currently selected group.
*/
function _oa_sections_get_current_selected_space($entity, $set_value = NULL) {
static $found_value;
if (isset($set_value)) {
$found_value = $set_value;
}
if (isset($found_value)) {
return $found_value ? $found_value : NULL;
}
$value = NULL;
if (isset($_POST['og_group_ref'])) {
$process = $_POST['og_group_ref'];
}
elseif ($entity && isset($entity->og_group_ref)) {
$process = $entity->og_group_ref;
}
// First check active view filter
if (!empty($_GET['og_group_ref_target_id'])) {
$value = $_GET['og_group_ref_target_id'];
}
// For panelizer settings.
elseif (!empty($_POST['exposed']['og_group_ref_target_id'])) {
$value = $_POST['exposed']['og_group_ref_target_id'];
}
// Check post or entity values.
elseif (isset($process) && is_array($process)) {
if (!empty($process[LANGUAGE_NONE][0]['target_id'])) {
$value = $process[LANGUAGE_NONE][0]['target_id'];
}
elseif (!empty($process[LANGUAGE_NONE][0]['default'])) {
$value = $process[LANGUAGE_NONE][0]['default'];
}
}
else {
$value = oa_core_get_space_context();
}
$found_value = $value;
// Make sure the current user has access.
if (!is_numeric($value) || !($node = node_load($value)) || !node_access('view', $node)) {
$found_value = FALSE;
return NULL;
}
return $value;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function oa_sections_form_oa_section_node_form_alter(&$form, &$form_state, $form_id) {
$default_hidden = TRUE;
$default_enabled = TRUE;
// If we are on the node add form that is specific to this Section type, we
// take the 'Section type' from the URL and hide the field.
if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'oa-section' && ($section_tid = arg(3)) && !empty($section_tid)) {
$form['field_oa_section']['und']['#default_value'] = array($section_tid);
$form['field_oa_section']['#access'] = FALSE;
$section = taxonomy_term_load($section_tid);
$values = field_get_items('taxonomy_term', $section, 'field_oa_section_type_menu');
if (!empty($values[0]['value'])) {
switch ($values[0]['value']) {
case OA_SECTION_TYPE_MENU_VISIBLE:
$default_hidden = FALSE;
break;
case OA_SECTION_TYPE_MENU_NONE:
$default_enabled = FALSE;
break;
}
}
}
_oa_core_hide_comment_settings($form);
_oa_core_setup_node_space_type($form, 'section_type', 'field_oa_section');
if (!empty($form['menu'])) {
$form['menu']['link']['hidden'] = array(
'#type' => 'checkbox',
'#title' => t('Disabled'),
// Default to hidden so in menu for organization but not showing.
'#default_value' => !empty($form['#node']->nid) ? $form['#node']->menu['hidden'] : $default_hidden,
'#description' => t('Allow items to be placed under this section, but prevent display in section menu.'),
'#weight' => 100,
);
}
// Enable menu link and group menu by default
if (empty($form['#node']->nid) && !empty($form['#node']->group_plid)) {
$form['menu']['enabled']['#default_value'] = $default_enabled;
$form['menu']['link']['parent']['#default_value'] = OG_MENU_SINGLE_MENU_NAME . ':' . $form['#node']->group_plid;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function oa_sections_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
if ($form['#vocabulary']->machine_name == 'section_type') {
_oa_core_setup_taxonomy_space_type($form, 'oa_section');
}
}
/**
* Implements hook_views_pre_render().
*/
function oa_sections_views_pre_render(&$view) {
if ($view->name == 'oa_sections_section_types') {
if (count($view->result) == 1) {
// If there is only one Section type, then we jump to it automatically.
$args = $_GET;
unset($args['q']);
drupal_goto('node/add/oa-section/' . $view->result[0]->tid, array('query' => $args));
}
elseif (user_access('administer taxonomy')) {
$view->attachment_before = '<p>' . t('Manage existing <em>Section types</em> or add new ones by <a href="!url">administrating the taxonomy</a>.', array('!url' => url('admin/structure/taxonomy/section_type'))) . '</p>';
}
}
}
/**
* Implements hook_views_pre_view().
*/
function oa_sections_views_pre_view(&$view) {
if ($view->name == 'oa_sections_section_types') {
oa_core_views_copy_get_arguments_to_link($view);
}
}
/**
* Return array of icon/images keyed by section type tid
*/
function oa_sections_get_icons() {
$vid = taxonomy_vocabulary_machine_name_load('section_type')->vid;
$terms = taxonomy_get_tree($vid);
$query = db_select('taxonomy_term_data', 't');
$query->leftJoin('field_data_field_oa_icon_class', 'ic', "ic.entity_id = t.tid");
$query->leftJoin('field_data_field_oa_icon_image', 'im', "im.entity_id = t.tid");
$query
->fields('t', array('tid'))
->fields('ic', array('field_oa_icon_class_value'))
->fields('im', array('field_oa_icon_image_fid'))
->condition('t.vid',$vid);
$result = $query->execute()->fetchAllAssoc('tid');
return $result;
}
/**
* Allowed values callback for field_oa_section_type_menu.
*/
function oa_sections_field_oa_section_type_menu_allowed_values() {
return array(
OA_SECTION_TYPE_MENU_DEFAULT => t('Added to OG Menu, hidden'),
OA_SECTION_TYPE_MENU_VISIBLE => t('Added to OG Menu, visible'),
OA_SECTION_TYPE_MENU_NONE => t('Not added to OG Menu'),
);
}