-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedcontent.module
More file actions
104 lines (92 loc) · 3.48 KB
/
sharedcontent.module
File metadata and controls
104 lines (92 loc) · 3.48 KB
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
<?php
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function sharedcontent_form_node_type_form_alter(&$form, &$form_state) {
$type = content_types($form['#node_type']->type);
$fields = $type['fields'];
$userreference_fields = array();
foreach ($fields as $field => $info) {
if ($info['type'] == 'userreference') {
$userreference_fields[$field] = $info;
}
}
$form['sharedcontent'] = array(
'#type' => 'fieldset',
'#title' => t('Shared Content'),
'#collapsible' => TRUE,
'#collapsed' => TRUE
);
if (!$userreference_fields) {
$form['sharedcontent']['sharedcontent_na_message'] = array(
'#type' => 'markup',
'#value' => t('<p>Shared Content is not available for this node type yet. You first should add a User Reference field to this node type.</p>')
);
} else {
$field_options = array('' => '- Select a field -');
foreach ($userreference_fields as $field) {
$field_options[$field['field_name']] = t('@label (@field)', array('@label' => $field['widget']['label'], '@field' => $field['field_name']));
}
$form['sharedcontent']['sharedcontent_field'] = array(
'#type' => 'select',
'#title' => t('Field'),
'#options' => $field_options,
'#default_value' => variable_get('sharedcontent_field_' . $form['#node_type']->type, ''),
);
$form['sharedcontent']['sharedcontent_operations'] = array(
'#type' => 'checkboxes',
'#title' => t('Operations'),
'#options' => array('view' => t('View'), 'update' => t('Update'), 'delete' => t('Delete')),
'#default_value' => variable_get('sharedcontent_operations_' . $form['#node_type']->type, array('view')),
);
}
}
/**
* Implementation of hook_node_access_records().
*/
function sharedcontent_node_access_records($node) {
$records = array();
if ($field = variable_get('sharedcontent_field_' . $node->type, '')) {
if (isset($node->$field) && is_array($node->$field) && $node->$field) {
$operations = variable_get('sharedcontent_operations_' . $node->type, array('view'));
foreach ($node->$field as $delta => $item) {
if ($item['uid'] && intval($item['uid']) && intval($item['uid']) != $node->uid) {
$records[] = array(
'realm' => 'sharedcontent',
'gid' => intval($item['uid']),
'grant_view' => in_array('view', $operations), 'grant_update' => in_array('update', $operations), 'grant_delete' => in_array('delete', $operations)
);
}
}
if ($node->uid) {
$account = user_load(array('uid' => $node->uid));
$name = check_plain($type->type);
$records[] = array(
'realm' => 'sharedcontent',
'gid' => $node->uid,
'grant_view' => 1,
'grant_update' => user_access('edit any ' . $name . 'content', $account) || user_access('edit own ' . $name . 'content', $account),
'grant_delete' => user_access('delete any ' . $name . 'content', $account) || user_access('delete own ' . $name . 'content', $account),
);
}
}
if (!$records) {
$records[] = array(
'realm' => 'sharedcontent',
'gid' => 0,
'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0
);
}
}
return $records;
}
/**
* Implementation of hook_node_grants().
*/
function sharedcontent_node_grants($account, $op) {
$grants = array();
if ($account->uid > 0) {
$grants['sharedcontent'] = array($account->uid);
}
return $grants;
}