--- term_display/term_display.module 2008-09-07 16:38:55.000000000 +0200 +++ term_display.module 2009-02-01 23:55:16.000000000 +0100 @@ -3,31 +3,104 @@ define('TERM_DISPLAY_DEFAULT', 'default'); define('TERM_DISPLAY_LIST', 'list'); +define('TERM_DISPLAY_PARENTS', 'parents'); +define('TERM_DISPLAY_NODELINKS', 'nodelinks'); define('TERM_DISPLAY_CUSTOM', 'custom'); +define('TERM_DISPLAY_PLAIN', 'plain'); define('TERM_DISPLAY_NONE', 'none'); +function term_display_theme() { + return array( + 'term_display_list' => array( + 'file' => 'term_display.module', + 'arguments' => array( + 'vocabulary' => NULL, + 'terms' => NULL, + ), + ), + 'term_display_parents' => array( + 'file' => 'term_display.module', + 'arguments' => array( + 'vocabulary' => NULL, + 'terms' => NULL, + ), + ), + 'term_display_nodelinks' => array( + 'file' => 'term_display.module', + 'arguments' => array( + 'vocabulary' => NULL, + 'terms' => NULL, + 'title' => NULL, + ), + ), + 'term_display_custom' => array( + 'file' => 'term_display.module', + 'arguments' => array( + 'vocabulary' => NULL, + 'terms' => NULL, + ), + ), + 'term_display_plain' => array( + 'file' => 'term_display.module', + 'arguments' => array( + 'vocabulary' => NULL, + 'terms' => NULL, + ), + ), + ); +} + /** * Implementation of hook_form_alter(). */ function term_display_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) { $term_display_data = term_display_data($form['vid']['#value']); - + + $types = array(); + + /* $result = db_query("SELECT type FROM {vocabulary_node_types} WHERE vid=%d ORDER by type ASC", $term_display_data); + while($checked_types = db_fetch_object($result)) { + $types[$checked_types->type] = $checked_types->type; + } + */ + + foreach(node_get_types('types') as $type=>$name) { + $types[$type] = $type; + } + $form['term_display'] = array( '#type' => 'fieldset', '#title' => t('Display options'), '#collapsible' => TRUE, '#collapsed' => FALSE, - '#description' => t('Set display options on content view for terms in this vocabulary.'), + '#description' => t('Limit diplaying of terms per roles and content types and set display options on content view for terms in this vocabulary. "parents" display style formats terms inline in parent_term->child_terms order. This setting overrides terms weight and always shows terms in this order no matter of actual weight settings. "nodelinks" retrieves nodes that are related to actual one, both are tagged with same term(s). "plain" style returns only names of terms, not links.'), '#weight' => -0.5, ); - + $form['term_display']['term_display_roles'] = array( + '#type' => 'select', + '#title' => t('Limit to roles'), + '#options' => user_roles(TRUE), + '#multiple' => TRUE, + '#default_value' => unserialize($term_display_data['roles']), + '#description' => t('Limit display of terms per user roles. This setting is different from Display style: none (hidden) from below which hides terms for all users, roles and admin. Here you can control for which role to show/hide it. If none selected all roles and users will be able to see the terms.'), + ); + $form['term_display']['term_display_types'] = array( + '#type' => 'select', + '#title' => t('Limit to content types'), + '#options' => $types, + '#multiple' => TRUE, + '#default_value' => unserialize($term_display_data['types']), + '#description' => t('Select content types to affect overriding its terms display. Only content types checked below for vocabulary are available for this setting and will be affected. If none selected all of these content types will be affected.'), + ); $options = array( TERM_DISPLAY_DEFAULT => t('default'), TERM_DISPLAY_LIST => t('list'), - TERM_DISPLAY_CUSTOM => t('custom (defaults to comma separated)'), + TERM_DISPLAY_PARENTS => t('parents'), + TERM_DISPLAY_NODELINKS => t('nodelinks'), + TERM_DISPLAY_CUSTOM => t('custom (defaults to comma separated)'), + TERM_DISPLAY_PLAIN => t('plain (without link to terms)'), TERM_DISPLAY_NONE => t('none (hidden)'), ); - $form['term_display']['term_display_style'] = array( '#type' => 'select', '#title' => t('Display style'), @@ -35,33 +108,14 @@ function term_display_form_taxonomy_form '#default_value' => $term_display_data['style'], '#description' => t('Select if and how you wish terms to appear on content (node) display.'), ); - $form['term_display']['term_display_weight'] = array( '#type' => 'weight', - '#title' => t('Display style'), - '#default_value' => $term_display_data['weight'], - '#description' => t('Set a weight for term display to control where terms appear in the content body. Set a low weight to move terms to the top of the content or a high one to move them to the bottom. Applies only when display style is set to "list" or "custom". This option is for advanced usage only; usually it\'s best to leave this at 0.'), - ); - -} - -function term_display_theme() { - return array( - 'term_display_list' => array( - 'file' => 'term_display.module', - 'arguments' => array( - 'vocabulary' => NULL, - 'terms' => NULL, - ), - ), - 'term_display_custom' => array( - 'file' => 'term_display.module', - 'arguments' => array( - 'vocabulary' => NULL, - 'terms' => NULL, - ), - ), + '#title' => t('Display weight'), + '#delta' => 20, + '#default_value' => $term_display_data['weight'], + '#description' => t('Set a weight for term display to control where terms appear in the content body. Set a low weight to move terms to the top of the content or a high one to move them to the bottom. Applies to all display styles except "default" and "hidden". This option is for advanced usage only; usually it\'s best to leave this at 0.'), ); + } /** @@ -73,12 +127,14 @@ function term_display_taxonomy($op, $typ case 'update': db_query('DELETE FROM {term_display} WHERE vid = %d', $object['vid']); // Fall through. - case 'insert': - db_query("INSERT INTO {term_display} (vid, style, weight) VALUES (%d, '%s', %d)", $object['vid'], $object['term_display_style'], $object['term_display_weight']); - break; + case 'insert': + $object['term_display_types'] = serialize($object['term_display_types']); + $object['term_display_roles'] = serialize($object['term_display_roles']); + db_query("INSERT INTO {term_display} (vid, style, weight, types, roles) VALUES (%d, '%s', %d, '%s', '%s')", $object['vid'], $object['term_display_style'], $object['term_display_weight'], $object['term_display_types'], $object['term_display_roles']); + break; case 'delete': db_query('DELETE FROM {term_display} WHERE vid = %d', $object['vid']); - break; + break; } } } @@ -90,15 +146,14 @@ function term_display_data($vid) { static $term_display_data; if (empty($term_display_data)) { - $result = db_query('SELECT vid, style, weight FROM {term_display}'); + $result = db_query('SELECT vid, style, weight, types, roles FROM {term_display}'); while ($vocabulary = db_fetch_array($result)) { - $term_display_data[$vocabulary['vid']] = $vocabulary; + $term_display_data[$vocabulary['vid']] = $vocabulary; } } if (isset($term_display_data[$vid])) { return $term_display_data[$vid]; - } - else { + } else { return array( 'style' => TERM_DISPLAY_DEFAULT, 'weight' => 0, @@ -110,39 +165,53 @@ function term_display_data($vid) { * Implementation of hook_nodeapi(). */ function term_display_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { + global $user; switch ($op) { case 'view': if (isset($node->taxonomy)) { - - $vocabularies = taxonomy_get_vocabularies($node->type); + $vocabularies = taxonomy_get_vocabularies($node->type); foreach ($vocabularies as $vocabulary) { $term_display_data = term_display_data($vocabulary->vid); - if ($term_display_data['style'] == TERM_DISPLAY_DEFAULT) { + $term_display_data['roles'] = unserialize($term_display_data['roles']); + $term_display_data['types'] = unserialize($term_display_data['types']); + + if ($term_display_data['style'] == TERM_DISPLAY_DEFAULT) { continue; } - $terms = array(); + + $terms = array(); foreach ($node->taxonomy as $tid => $term) { - if ($term->vid == $vocabulary->vid) { - switch ($term_display_data['style']) { - case TERM_DISPLAY_LIST: - case TERM_DISPLAY_CUSTOM: - $terms[] = $term; - // Fall through. - case TERM_DISPLAY_NONE: - unset($node->taxonomy[$tid]); + if (($term->vid == $vocabulary->vid) && (in_array($node->type, $term_display_data['types']) || empty($term_display_data['types']))) { + $matches = array_intersect(array_keys($user->roles), $term_display_data['roles']); + + if(!empty($matches) || empty($term_display_data['roles'])) { + switch ($term_display_data['style']) { + case TERM_DISPLAY_LIST: + case TERM_DISPLAY_PARENTS: + case TERM_DISPLAY_NODELINKS: + case TERM_DISPLAY_CUSTOM: + case TERM_DISPLAY_PLAIN: + $terms[] = $term; + + case TERM_DISPLAY_NONE: + unset($node->taxonomy[$tid]); break; - } - } + } + }else{ + unset($node->taxonomy[$tid]); + } + } } - if (!empty($terms)) { + + if (!empty($terms)) { $node->content['term_display_'. $vocabulary->vid] = array( '#weight' => $term_display_data['weight'], - '#value' => theme('term_display_'. $term_display_data['style'], $vocabulary, $terms), + '#value' => theme('term_display_'. $term_display_data['style'], $vocabulary, $terms, $node->title), ); } } } - break; + break; } } @@ -158,7 +227,25 @@ function theme_term_display_list($vocabu $output .= "\n"; return $output; } - +/** + * Theme terms as parent->child inline list. This function overrides default weight of terms, they are always showed in this format. + */ +function theme_term_display_parents($vocabulary, $terms) { + $links = array(); + $links = _terms_display_get_parents_child ($vocabulary, $terms); + foreach($links as $link) { + $output[] = l($link->name, taxonomy_term_path($link)); + } + return '