-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwo.module
124 lines (102 loc) · 3.92 KB
/
wo.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
<?php
/**
* @file
* Code for the Wikiocracy feature.
*/
require_once('wo.features.inc');
require_once('wo.vote.inc');
require_once('wo.batch.inc');
// Add css
function wo_init() {
drupal_add_css(drupal_get_path('module', 'wo') . '/wo.css');
}
/**
* Implementation of hook_ctools_plugin_dierctory() to let the system know
* we implement plugins.
*/
function wo_ctools_plugin_directory($module, $plugin) {
return 'plugins/' . $plugin;
}
function wo_node_insert($node) {
if ($node->type == 'wo_vote') wo_vote_updated_created($node);
}
function wo_node_update($node) {
if ($node->type == 'wo_vote') wo_vote_updated_created($node);
}
function wo_form_alter(&$form, &$form_state, $form_id) {
wo_form_alter_diable_fields($form, $form_state, $form_id);
// Proposal node create and edit form
if ($form_id == 'wo_proposal_node_form') {
// If no issue is assigned, assign it from the URL
if (!$form['field_wo_proposal_issue']['und'][0]['target_id']['#default_value']) {
// Trigger an error if we are missing the issue nid
if (!arg(3)) {
trigger_error('Bad Form State - missing issue nid', E_USER_ERROR);
drupal_not_found();
}
// Load the issue and make sure it's valid
$issue = node_load(intval(arg(3)));
if ($issue->type != 'wo_issue') {
trigger_error('Bad Form State - missing issue nid', E_USER_ERROR);
drupal_not_found();
}
$form['field_wo_proposal_issue']['und'][0]['target_id']['#default_value'] = $issue->title.' ('.$issue->nid.')';
}
}
// Vote node create and edit form
if ($form_id == 'wo_vote_node_form') {
// If no issue is assigned, assign it from the URL
if (!$form['field_wo_vote_issue']['und'][0]['target_id']['#default_value']) {
// Trigger an error if we are missing the issue nid
if (!arg(3)) {
trigger_error('Bad Form State - missing issue nid', E_USER_ERROR);
drupal_not_found();
}
// Load the issue and make sure it's valid
$issue = node_load(intval(arg(3)));
if ($issue->type != 'wo_issue') {
trigger_error('Bad Form State - missing issue nid', E_USER_ERROR);
drupal_not_found();
}
$form['field_wo_vote_issue']['und'][0]['target_id']['#default_value'] = $issue->title.' ('.$issue->nid.')';
}
}
// Essay node create and edit form
if ($form_id == 'wo_essay_node_form') {
// If no issue is assigned, assign it from the URL
if (!$form['field_wo_essay_proposal']['und'][0]['target_id']['#default_value']) {
// Trigger an error if we are missing the issue nid
if (!arg(3)) {
trigger_error('Bad Form State - missing proposal nid', E_USER_ERROR);
drupal_not_found();
}
// Load the issue and make sure it's valid
$proposal = node_load(intval(arg(4)));
if ($proposal->type != 'wo_proposal') {
trigger_error('Bad Form State - missing proposal nid', E_USER_ERROR);
drupal_not_found();
}
$form['field_wo_essay_proposal']['und'][0]['target_id']['#default_value'] = $proposal->title.' ('.$proposal->nid.')';
}
}
}
function wo_form_alter_diable_fields(&$form, &$form_state, $form_id) {
$disable = array (
'wo_proposal' => array('field_wo_proposal_essay_agt','field_wo_proposal_essay_for','field_wo_proposal_issue'),
'wo_issue' => array('field_wo_issue_consensus','field_wo_issue_proposals'),
'wo_vote' => array('field_wo_vote_issue'),
'wo_essay' => array('field_wo_essay_proposal'),
);
foreach ($disable as $content_type => $fields) {
if ($form_id == $content_type.'_node_form') {
foreach ($fields as $field) {
$form[$field]['#access'] = FALSE;
}
}
}
}
function wo_vote_updated_created($vote) {
// Recalculate the issue -- eventually this will be done on cron
$issue = node_load($vote->field_wo_vote_issue['und'][0]['target_id']);
wo_batch_update_issue($issue);
}