-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautotag_post.module
205 lines (166 loc) · 5.98 KB
/
autotag_post.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
<?php
function autotag_post_help($path, $arg) {
$output = '';
switch($path) {
case "admin/help#autotag_post":
$output = '<p>' . t('Autotags posts with taxonomy terms the current node is tagged with') . '</p>';
break;
}
return $output;
}
function autotag_post_perm() {
return array('access autotag_post', 'create autotag_post', 'administer autotag_post');
}
function autotag_post_block($op = 'list', $delta = 0, $edit = array()) {
$block = array();
if ($op == 'list') {
$block[0]['info'] = t('Auto-Tag Post');
$block['content'] = t('Auto-Tagged Posts');
} else if ($op == 'view') {
// don't show this link if the url is not "node/$nid"
if (is_numeric(arg(1)) && arg(0) == "node") {
$content = "<h3>" . variable_get('autotag_post_header', 'Auto-tagged Posts') . "</h3>";
$parent_node = node_load(arg(1));
// display all the posts tagged with the terms this node is tagged with
$content .= '<ul>';
$tids = array();
foreach (taxonomy_node_get_terms($parent_node) as $term) {
$tids[] = $term->tid;
}
$node_select = taxonomy_select_nodes($tids, 'and');
while ($tax_node = db_fetch_object($node_select)) {
if ($tax_node->nid != $parent_node->nid) {
// the query doesn't select the entire node so we need to load it
// to get the teaser, etc
$node = node_load($tax_node->nid);
$content .= "<li>" . l($node->title, 'node/' . $node->nid) . "<br />";
if (variable_get('autotag_post_teaser', 0) == 1) {
$content .= $node->teaser;
}
$content .= "</li>";
}
}
$content .= "</ul>";
if (node_access("update", $parent_node)) {
$linkname = variable_get('autotag_post_linkname', 'Create an auto-tagged post');
$content .= "<br />" . l(t($linkname), 'autotag_post/create/' . arg(1) . "/" . variable_get('autotag_post_content_type', 'page'), array()) . "<br />";
}
}
$block['content'] = $content;
}
return $block;
}
function autotag_post_menu() {
$items = array();
$items['admin/settings/autotag_post'] = array(
'title' => 'Auto-Tag Post Settings',
'description' => 'Manage settings for the Auto-Tag Post Module',
'page callback' => 'drupal_get_form',
'page arguments' => array('autotag_post_admin'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['autotag_post/create/%node/%'] = array(
'title' => 'Create tagged post',
'description' => 'Create your auto-tagged post',
'page callback' => 'autotag_post_create',
'page arguments' => array(2, 3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function autotag_post_admin() {
$form = array();
$vocab_options = array();
$content_options = array();
foreach (taxonomy_get_vocabularies() as $vocab) {
$vocab_options[$vocab->vid] = $vocab->name;
foreach ($vocab->nodes as $key => $value) {
$content_options[$key] = $value;
}
}
$form['autotag_post_vocab'] = array(
'#type' => 'checkboxes',
'#title' => t('Restrict Vocabulary'),
'#options' => $vocab_options,
'#default_value' => variable_get('autotag_post_vocab', array()),
'#description' => t("Select which vocabularies the post will be tagged with. Default is all."),
);
$form['autotag_post_teaser'] = array(
'#type' => 'checkbox',
'#title' => t('Show Teaser'),
'#default_value' => variable_get('autotag_post_teaser', 0),
'#description' => t("Check if you'd like teasers to show."),
);
$form['autotag_post_linkname'] = array(
'#type' => 'textfield',
'#title' => t('Block link text'),
'#default_value' => variable_get('autotag_post_linkname', 'Create an auto-tagged post.'),
'#description' => t('This is the link text that will be displayed in the block.'),
);
$form['autotag_post_header'] = array(
'#type' => 'textfield',
'#title' => t('Block header text'),
'#default_value' => variable_get('autotag_post_header', 'Auto-tagged Posts'),
'#description' => t('This is the header text that will be displayed in the block.'),
);
$form['autotag_post_content_type'] = array(
'#type' => 'select',
'#title' => t('Autotagged Content Type'),
'#options' => $content_options,
'#default_value' => variable_get('autotag_post_content_type', 'page'),
'#description' => t('This is the content type that will be created and tagged.'),
);
return system_settings_form($form);
}
function autotag_post_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, '_node_form') != -1) {
if ($form['#node']->autotag_node) {
$autotag_node = $form['#node']->autotag_node;
//var_dump($autotag_node->taxonomy);
$vocab = variable_get('autotag_post_vocab', array());
if (empty($vocab)) {
foreach (taxonomy_get_vocabularies() as $vocabulary) {
$vocab[] = $vocabulary->vid;
}
}
if (!isset($form['taxonomy'])) {
$form['taxonomy'] = array();
}
foreach ($vocab as $vid) {
unset($form['taxonomy']['tags'][$vid]);
unset($form['taxonomy']['tags'][$vid]);
$form['taxonomy'][$vid]['#required'] = TRUE;
$form['taxonomy'][$vid]['#description'] = t('Taxonomy');
if ($vid != 0) {
$tids = array();
foreach (taxonomy_node_get_terms_by_vocabulary($autotag_node, $vid) as $term) {
$tids[] = $term->tid;
}
$form['taxonomy'][$vid]['#value'] = $tids;
$form['taxonomy'][$vid]['#default_value'] = $tids;
$form['taxonomy'][$vid]['#type'] = 'value';
}
}
}
}
}
function autotag_post_create($node, $node_type) {
$expected_node_type = variable_get('autotag_post_content_type', 'page');
if ($expected_node_type != $node_type) {
drupal_set_message("Incorrect content type.");
$output = "You cannot create a tagged post of this content type.";
} else {
global $user;
module_load_include('inc', 'node', 'node.pages');
$new_node = new stdClass();
$new_node->uid = $user->uid;
$new_node->name = (isset($user->name) ? $user->name : '');
$new_node->type = $node_type;
$new_node->autotag_node = $node;
node_object_prepare($new_node);
$output = drupal_get_form($node_type . '_node_form', $new_node);
}
return $output;
}