/[drupal]/contributions/modules/term_message/term_message.module
ViewVC logotype

Contents of /contributions/modules/term_message/term_message.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sat Mar 7 20:57:01 2009 UTC (8 months, 2 weeks ago) by binarybill
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +149 -61 lines
File MIME type: text/x-php
#317853 by add1sun.  Changed storage structure of term message to database table.  No update path offered.
1 <?php
2 // $Id: term_message.module,v 1.2 2009/01/06 23:38:42 add1sun Exp $
3
4 /**
5 * @file
6 * Module that provides custom messages per taxonomy term and displays the
7 * appropriate message on nodes that have been assigned the term(s).
8 *
9 * @todo
10 * Skip functionality
11 * Move to DB table
12 * Vocabulary settings
13 * Implement correctly or remove
14 * Change storage to separate table instead of variables
15 */
16
17 /**
18 * Implementation of hook_help().
19 */
20 function term_message_help($path, $arg) {
21 switch ($path) {
22 case 'admin/help#term_message':
23 $output = '<p>'. t('Term message allows users with administer taxonomy permission to provide messages that will:') .'</p>';
24 $output .= '<ul>';
25 $output .= '<li>'. t('<a href="!term-message-settings" title="Term message settings">Configure site-wide settings for term messages</a>.', array('!term-message-settings' => url('admin/content/term-message'))) .'</li>';
26 $output .= '<li>'. t('Edit per-vocabulary settings on the <a href="!vocabulary-listing" title="list vocabularies">vocabularies\' edit pages</a>.', array('!vocabulary-listing' => url('admin/content/taxonomy'))) .'</li>';
27 $output .= '<li>'. t('Set messages for terms on individual term add and edit forms.') .'</li>';
28 $output .= '</ul>';
29 return $output;
30
31 case 'admin/content/term-message':
32 $output = '<p>'. t('Configure site-wide settings for term messages below.&nbsp; Edit per-vocabulary settings on the <a href="!vocabulary-listing" title="list vocabularies">vocabularies\' edit pages</a>.&nbsp; Set messages for terms on individual term add and edit forms.', array('!vocabulary-listing' => url('admin/content/taxonomy'))) .'</p>';
33 return $output;
34 }
35 }
36
37 /**
38 * Implementation of hook_perm().
39 */
40 function term_message_perm() {
41 return array('administer term messages');
42 }
43
44 /**
45 * Implementation of hook_menu().
46 */
47 function term_message_menu() {
48 $items['admin/content/term-message'] = array(
49 'title' => 'Term messages',
50 'page callback' => 'drupal_get_form',
51 'page arguments' => array('term_message_admin_settings'),
52 'description' => 'Set up how your site handles messages added to terms. Change sitewide settings for term message functionality.',
53 'access arguments' => array('administer taxonomy')
54 );
55
56 return $items;
57 }
58
59 /**
60 * Implementation of hook_nodeapi().
61 */
62 function term_message_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
63 if ($page || variable_get('term_message_teaser_lists', FALSE)) {
64 switch ($op) {
65 case 'view':
66 // $node->taxonomy empty array, even if content type cannot have taxonomy
67 // but we don't want to assign $term_messages repeatedly
68 if (empty($node->taxonomy)) {
69 return;
70 }
71 // Look at other terms and deterimine if we should skip/suppress message
72 foreach ($node->taxonomy as $tid => $term) {
73 $message = _term_message_load($tid);
74 if ($message) {
75 $suppress = FALSE;
76 // Get skip variable (see TODO at top)
77 $skip = variable_get('term_message_skip_'. $tid, array());
78 $present = $node->taxonomy;
79 // Determine suppression
80 foreach (array_keys($skip) as $skip_tid) {
81 if (isset($present[$skip_tid])) {
82 $suppress = TRUE;
83 }
84 }
85 // If not suppressed, display message
86 if (!$suppress) {
87 drupal_set_message($message);
88 }
89 }
90 }
91 break;
92 }
93 }
94 }
95
96 /**
97 * Implementation of hook_form_alter().
98 */
99 function term_message_form_alter(&$form, $form_state, $form_id) {
100 switch ($form_id) {
101 case 'taxonomy_form_term':
102 // $form_state is empty, should figure out why and try to reimplement using $form_state
103 // $tid = $form_state['values']['tid']['#value'];
104 $tid = $form['tid']['#value'];
105 $vid = $form['vid']['#value'];
106
107 // ?? Looks for permissions
108 $show_form = variable_get('term_message_show_form', array($vid => 'all'));
109 if ($show_form[$tid] == 'none') {
110 return;
111 }
112 elseif ($show_form[$tid] == 'admin' && !user_access('administer term message')) {
113 return;
114 }
115
116 // Get current message
117 $term_message = _term_message_load($tid);
118
119 // Form Add
120 $form_add = array();
121 $form_add['term_message'] = array(
122 '#type' => 'fieldset',
123 '#title' => t('Term message settings'),
124 '#collapsible' => TRUE,
125 '#collapsed' => variable_get('term_message_collapse_form', FALSE),
126 );
127 $form_add['term_message']['term_message_data'] = array(
128 '#type' => 'value',
129 '#value' => $term_message,
130 );
131 $form_add['term_message']['term_message'] = array(
132 '#type' => 'textarea',
133 '#title' => t('Term message'),
134 '#default_value' => (isset($term_message)) ? $term_message : '',
135 '#description' => t('Provide a message to be displayed on pages of content tagged with this taxonomy term.'),
136 '#rows' => 3,
137 );
138
139 // Get Vocabulary
140 $vocabulary = taxonomy_vocabulary_load($vid);
141 // Check for multiple type, so that we can provide some overrides.
142 if ($vocabulary->multiple) {
143 $options = _term_message_terms($vid, $tid);
144 $form_add['term_message']['term_message_skip'] = array(
145 '#type' => 'select',
146 '#title' => t('Suppress message for content with selected terms'),
147 '#options' => $options,
148 '#default_value' => variable_get('term_message_skip_' . $tid, array()),
149 '#multiple' => TRUE,
150 '#size' => min(12, count($options)),
151 '#description' => t('If one of these terms is also present, do not show the message.'),
152 );
153 }
154
155 // Handled by hook_taxonomy, no custom submit handler needed
156 // put the form additions before the buttons
157 $pos = array_search('submit', array_keys($form));
158 $form = array_merge(array_slice($form, 0, $pos), $form_add, array_slice($form, $pos));
159 break;
160
161 case 'taxonomy_form_vocabulary':
162 // Check for vid
163 $vid = isset($form_state['values']['vid']['#value']) ? $form_state['values']['vid']['#value'] : NULL;
164
165 // Add form elements
166 $form_add = array();
167 $form_add['term_message'] = array(
168 '#type' => 'fieldset',
169 '#title' => t('Term message options'),
170 '#collapsible' => TRUE,
171 '#collapsed' => FALSE,
172 );
173 // TODO ?? This functionality needs review
174 $show_form = variable_get('term_message_show_form', array($vid => 'all'));
175 $form_state['term_message']['term_message_admin_access'] = array(
176 '#type' => 'radios',
177 '#title' => t('Display term message'),
178 '#default_value' => $show_form[$vid],
179 '#options' => array(
180 'all' => t('For all users (with <em>administer taxonomy</em> permission)'),
181 'admin' => t('Only for users with <em>administer term message</em> permission'),
182 'none' => t('For no users'),
183 ),
184 '#description' => t('Please note that this only removes the term message textfield from taxonomy term add and edit forms. Existing term messages are not removed. This functionality needs review'),
185 );
186
187 // Handled by hook_taxonomy, no custom submit handler needed
188 // put the form additions before the buttons
189 $pos = array_search('submit', array_keys($form));
190 $form = array_merge(array_slice($form, 0, $pos), $form_add, array_slice($form, $pos));
191 break;
192 }
193 }
194
195
196 /**
197 * Implementation of hook_taxonomy().
198 */
199 function term_message_taxonomy($op, $type, $array) {
200 switch ($type) {
201 case 'term':
202 switch ($op) {
203 case 'insert':
204 case 'update':
205 // If not set, it's taxonomy manager, and don't overwrite
206 if (isset($array['term_message'])) {
207 _term_message_save($array);
208 }
209 // Save skip values
210 _term_message_skip_save($array);
211 break;
212
213 case 'delete':
214 _term_message_delete($array['tid']);
215 break;
216
217 }
218 break;
219
220 case 'vocabulary':
221 switch ($op) {
222 case 'insert':
223 case 'update':
224 _term_message_vocab_save($array);
225 break;
226
227 case 'delete':
228 _term_message_vocab_delete($array);
229 break;
230
231 }
232 break;
233
234 }
235 }
236
237 /**
238 * Configuration settings for Term message.
239 */
240 function term_message_admin_settings() {
241 $form['term_message_collapse_form'] = array(
242 '#type' => 'checkbox',
243 '#title' => t('Collapse term message fields on edit term form'),
244 '#default_value' => variable_get('term_message_collapse_form', FALSE),
245 );
246 $form['term_message_teaser_lists'] = array(
247 '#type' => 'checkbox',
248 '#title' => t('Show term messages for listings (not recommended)'),
249 '#default_value' => variable_get('term_message_teaser_lists', FALSE),
250 '#description' => t('Show term messages on listing pages and whenever the content with the term is processed. <strong>Likely to result in duplicate and out-of-context messages.</strong>'),
251 );
252 return system_settings_form($form);
253 }
254
255 /**
256 * Retrieve terms
257 *
258 * @param $vid
259 * vocabulary ID
260 * @param $tid
261 * term ID
262 * @return
263 * array of term ID and names
264 */
265 function _term_message_terms($vid, $tid) {
266 $options = array();
267 $terms = _term_message_get_terms_by_vid($vid);
268 foreach ($terms as $term) {
269 if ($term->tid != $tid) {
270 $options[$term->tid] = $term->name;
271 // if we do use depth for taxonomy tree result:
272 // $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
273 }
274 }
275 return $options;
276 }
277
278 /**
279 * Save term message and terms to suppress, if any.
280 *
281 * @param $td
282 * array of term data
283 */
284 function _term_message_save($td) {
285 // Get the ID and Message
286 $tid = $td['tid'];
287 $term_message = $td['term_message'];
288
289 // Check if tid exists, insert if it doesn't exist, update if it does exist
290 $query_check = "SELECT t.tid FROM {term_message} t WHERE t.tid = %s";
291 $exists = db_result(db_query($query_check, $tid));
292 if (!$exists) {
293 $query_write = "INSERT INTO {term_message} (tid, message) VALUES (%d, '%s')";
294 db_query($query_write, $tid, $term_message);
295 }
296 else {
297 $query_write = "UPDATE {term_message} SET tid = %d, message = '%s' WHERE tid = %d";
298 db_query($query_write, $tid, $term_message, $tid);
299 }
300
301 return;
302 }
303
304 /**
305 * Load message for term by term id (tid)
306 *
307 * @param $tid
308 * string term id
309 * @return
310 * string message
311 */
312 function _term_message_load($tid) {
313 $query_write = "SELECT message FROM {term_message} WHERE tid = %d";
314 $result = db_result(db_query($query_write, $tid));
315 return $result;
316 }
317
318 /**
319 * Delete message for term by term id (tid)
320 *
321 * @param $td
322 * array of term data
323 */
324 function _term_message_delete($tid) {
325 // Delete row from table
326 $query = "DELETE FROM {term_message} WHERE tid = %d";
327 db_query($query, $tid);
328
329 return;
330 }
331
332 /**
333 * Save term message and terms to suppress, if any.
334 *
335 * @param $td
336 * array of term data
337 */
338 function _term_message_skip_save($td) {
339 // Get the ID and Message
340 $tid = $td['tid'];
341 $skips = $td['term_message_skip'];
342
343 // Store in variable (TODO change)
344 // if there is a tid
345 if ($tid) {
346 variable_set('term_message_skip_' . $tid, $skips);
347 }
348
349 return;
350 }
351
352 /**
353 * Save vocab settings (See top TODO)
354 *
355 * @param $vd
356 * Vocabulary data.
357 */
358 function _term_message_vocab_save($vd) {
359 $vid = $vd['vid'];
360 $show_form = variable_get('term_message_show_form', array());
361 $show_form[$vid] = isset($vd['term_message_show_form']) ? $vd['term_message_show_form'] : NULL;
362 variable_set('term_message_show_form', $show_form);
363 return;
364 }
365
366 /**
367 * UNDOC (See top TODO)
368 */
369 function _term_message_vocab_delete($vd) {
370 $vid = $vd['vid'];
371 $show_form = variable_get('term_message_show_form', array());
372 unset($show_form[$vid]);
373 variable_set('term_message_show_form', $show_form);
374 return;
375 }
376
377
378 /**
379 * Returns an array of all the terms in a vocabulary.
380 *
381 * @param $vid
382 * Vocabulary ID of the terms that should be returned.
383 *
384 * @param $page_increment
385 * Optional. Only used if $depth == FALSE. Provide pager for large vocabs.
386 *
387 * Note: This is modeled on the code used in taxonomy_overview_terms()
388 * taxonomy_overview_terms($vid) could be refactored to use this:
389 * $terms = term_message/taxonomy_get_terms_by_vid($vid, 25, $vocabulary->tags);
390 * if ($vocabulary->tags) {
391 * foreach ($terms as $term) {
392 * $rows[] = array(
393 * l($term->name, "taxonomy/term/$term->tid"),
394 * l(t('edit'), "admin/content/taxonomy/edit/term/$term->tid", array(), $destination),
395 * );
396 * }
397 * }
398 * else {
399 * foreach ($terms as $term) {
400 * $total_entries++; // we're counting all-totals, not displayed
401 * ...
402 * and it continues on from there the same.
403 * (Personally I would also factor out the admin link building.)
404 */
405 function _term_message_get_terms_by_vid($vid, $depth = TRUE, $page_increment = 25) {
406 if (!$depth) {
407 // We are not calling taxonomy_get_tree because that might fail with a big
408 // number of tags in the freetagging vocabulary.
409 $results = pager_query(db_rewrite_sql("
410 SELECT t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), " . $page_increment . ", 0, NULL, " . $vid . "
411 "));
412
413 // Below feels efficient, but only way to put result objects in array?
414 $terms = array();
415 while ($term = db_fetch_object($results)) {
416 $terms[] = $term;
417 }
418 }
419 else {
420 $terms = taxonomy_get_tree($vid);
421 }
422 return $terms;
423 }

  ViewVC Help
Powered by ViewVC 1.1.2