| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file
|
| 4 |
* provide a menu for navigating a taxonomy inside panels
|
| 5 |
* set breadcrumbs (done inside)
|
| 6 |
* set title (done inside)
|
| 7 |
* validate arg? can't be done inside
|
| 8 |
* hook_taxonomy (to direct links
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_term_path
|
| 13 |
*
|
| 14 |
* activated by setting module=panels_taxonomy in vocabulary table
|
| 15 |
*/
|
| 16 |
|
| 17 |
function panels_taxonomy_term_path($term) {
|
| 18 |
static $handlers;
|
| 19 |
if (!is_array($handlers)) {
|
| 20 |
// TODO: store this unserialized in a seperate table to make this quicker?
|
| 21 |
$handlers = array();
|
| 22 |
$result = db_query('SELECT p.path, b.configuration FROM {panels_pane} b INNER JOIN {panels_page} p ON b.did = p.did WHERE b.type="taxonomy"');
|
| 23 |
while ($pane = db_fetch_object($result)) {
|
| 24 |
$pane->configuration = unserialize($pane->configuration);
|
| 25 |
if ($pane->configuration['primary']) {
|
| 26 |
$handlers[$pane->configuration['vid']] = array('path' => $pane->path, 'position' =>$pane->configuration['arg position']);
|
| 27 |
}
|
| 28 |
}
|
| 29 |
}
|
| 30 |
$handler = $handlers[$term->vid];
|
| 31 |
if (isset($handler)) {
|
| 32 |
return $handler['path'] . '/' . str_repeat('/', $handler['position'] - 1) . $term->tid;
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Output function for the 'taxonomy' content type.
|
| 38 |
*/
|
| 39 |
function panels_taxonomy_content($conf, $panel_args) {
|
| 40 |
$args = explode('/', $_GET['q']);
|
| 41 |
$argidx = count($args) - count($panel_args) + ($conf['arg position'] - 1); //absolute argument position
|
| 42 |
$args = array_pad($args, $argidx+1, '');
|
| 43 |
|
| 44 |
// Term display
|
| 45 |
if ($args[$argidx]) {
|
| 46 |
$term = taxonomy_get_term($args[$argidx]);
|
| 47 |
if ($conf['display']['page title']) {
|
| 48 |
panels_taxonomy_set_title($term->name);
|
| 49 |
$block->subject = filter_xss_admin($conf['title']);
|
| 50 |
}
|
| 51 |
else {
|
| 52 |
$block->subject = filter_xss_admin($conf['title']?$conf['title']: $term->name);
|
| 53 |
}
|
| 54 |
if ($term->vid != $conf['vid']) {
|
| 55 |
$block->subject = 'Error!';
|
| 56 |
$block->content = "Invalid term.";
|
| 57 |
return $block;
|
| 58 |
}
|
| 59 |
if ($conf['display']['description']) {
|
| 60 |
$block->content .= '<div class="description">' . $term->description . '</div>';
|
| 61 |
}
|
| 62 |
if ($conf['display']['top level terms']) {
|
| 63 |
$block->content .= panels_taxonomy_child_terms_list(0, $conf['vid'], $args, $argidx);
|
| 64 |
}
|
| 65 |
if ($conf['display']['children']) {
|
| 66 |
$block->content .= panels_taxonomy_child_terms_list($term->tid, $conf['vid'], $args, $argidx);
|
| 67 |
}
|
| 68 |
if ($conf['display']['breadcrumbs']) {
|
| 69 |
// Build breadcrumb based on first hierarchy of first term
|
| 70 |
// Borrowed from taxonomy_term_page
|
| 71 |
$current = $term;
|
| 72 |
while ($current = array_shift(taxonomy_get_parents($current->tid))) {
|
| 73 |
$args[$argidx] = $current->tid;
|
| 74 |
$breadcrumbs[] = l($current->name, implode('/', $args));
|
| 75 |
}
|
| 76 |
if ($conf['display']['vocabulary']) {
|
| 77 |
$vocabulary = taxonomy_get_vocabulary($conf['vid']);
|
| 78 |
$breadcrumbs[] = l($vocabulary->name, implode('/', array_slice($args, 0, $argidx)));
|
| 79 |
}
|
| 80 |
if (is_array($breadcrumbs)) {
|
| 81 |
$breadcrumbs = array_reverse($breadcrumbs);
|
| 82 |
drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(), $breadcrumbs));
|
| 83 |
}
|
| 84 |
}
|
| 85 |
} elseif ($conf['display']['vocabulary']) {
|
| 86 |
// Vocabulary display
|
| 87 |
$vocabulary = taxonomy_get_vocabulary($conf['vid']);
|
| 88 |
$block->subject = filter_xss_admin($conf['title']?$conf['title']: $vocabulary->name);
|
| 89 |
if ($conf['display']['description']) {
|
| 90 |
$block->content .= '<div class="description">' . ($vocabulary->description) . '</div>';
|
| 91 |
}
|
| 92 |
if ($conf['display']['top level terms'] OR $conf['display']['children']) {
|
| 93 |
$block->content .= panels_taxonomy_child_terms_list(0, $conf['vid'], $args, $argidx);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
return $block;
|
| 97 |
}
|
| 98 |
|
| 99 |
function panels_taxonomy_set_title($set_title=NULL) {
|
| 100 |
static $title;
|
| 101 |
if (is_string($set_title)) {
|
| 102 |
$title = $set_title;
|
| 103 |
}
|
| 104 |
if (is_string($title) AND is_null($set_title)) {
|
| 105 |
drupal_set_title($title);
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
| 109 |
function panels_taxonomy_footer() {
|
| 110 |
panels_taxonomy_set_title();
|
| 111 |
}
|
| 112 |
|
| 113 |
function panels_taxonomy_child_terms_list($tid, $vid, $args, $argidx){
|
| 114 |
$links = array();
|
| 115 |
foreach (taxonomy_get_children($tid, $vid) as $child_tid => $child_term) {
|
| 116 |
$args[$argidx] = $child_tid;
|
| 117 |
$links[] = l($child_term->name, implode('/', $args));
|
| 118 |
}
|
| 119 |
return theme('item_list', $links);
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* Callback function to supply a list of content types.
|
| 124 |
*/
|
| 125 |
function panels_taxonomy_panels_content_types() {
|
| 126 |
$items['taxonomy'] = array(
|
| 127 |
'title' => t('Taxonomy browser'),
|
| 128 |
'render callback' => 'panels_taxonomy_content',
|
| 129 |
'content_types' => 'panels_taxonomy_content_types',
|
| 130 |
'add callback' => 'panels_taxonomy_add',
|
| 131 |
'edit callback' => 'panels_taxonomy_edit',
|
| 132 |
'title callback' => 'panels_taxonomy_title',
|
| 133 |
);
|
| 134 |
return $items;
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Returns the form for a new taxonomy browser.
|
| 139 |
*/
|
| 140 |
function panels_taxonomy_add($id) {
|
| 141 |
$conf = array('vid' => $id, 'arg position' => 1, 'validate arg' => 1);
|
| 142 |
return panels_taxonomy_edit($conf);
|
| 143 |
}
|
| 144 |
|
| 145 |
function panels_taxonomy_edit($conf = array()) {
|
| 146 |
$form['#taxonomy'] = TRUE;
|
| 147 |
$form['help'] = array('#weight' => -1, '#value'=>'<div>Provides navigation for a vocabulary. In order to actually display the nodes associated with the current term, you should add one or more views to your panel with a "vocabulary" argument. <br/><em>Note: </em> <strong>The views should be set to "embedded".</strong></div>');
|
| 148 |
$form['vid'] = array(
|
| 149 |
'#type' => 'hidden',
|
| 150 |
'#default_value' => $conf['vid'],
|
| 151 |
);
|
| 152 |
$form['title'] = array(
|
| 153 |
'#type' => 'textfield',
|
| 154 |
'#default_value' => $conf['title'],
|
| 155 |
'#title' => t('Title'),
|
| 156 |
'#description' => t('Title for navigation block. Leave blank to use term name. Enter a single space to <em>really</em> leave blank.'),
|
| 157 |
'#size' => 20,
|
| 158 |
);
|
| 159 |
$form['display'] = array(
|
| 160 |
'#type' => 'checkboxes',
|
| 161 |
'#title' => t('Display'),
|
| 162 |
'#options' => array(
|
| 163 |
'page title' => t('Term title as page title'),
|
| 164 |
'description' => t('Term description'),
|
| 165 |
'vocabulary' => t('Display vocabulary if no term is specified'),
|
| 166 |
'breadcrumbs' => t('Ancestor terms as breadcrumbs'),
|
| 167 |
'children' => t('Child terms'),
|
| 168 |
'top level terms' => t('Top-level terms'),
|
| 169 |
),
|
| 170 |
'#default_value' => $conf['display'],
|
| 171 |
);
|
| 172 |
$form['arg position'] = array(
|
| 173 |
'#type' => 'textfield',
|
| 174 |
'#default_value' => $conf['arg position'],
|
| 175 |
'#title' => t('Argument position'),
|
| 176 |
'#description' => t('Which argument contains the taxonomy term ID? For example, if the first argument to your "colors" panel is the term ID, so that a typical URL is colors/3, the argument position should be 1.'),
|
| 177 |
'#required' => TRUE,
|
| 178 |
'#size' => 2,
|
| 179 |
);
|
| 180 |
$form['primary'] = array(
|
| 181 |
'#type' => 'checkbox',
|
| 182 |
'#default_value' => $conf['primary'],
|
| 183 |
'#title' => t('Override term links'),
|
| 184 |
'#description' => t('Make this panel the primary handler for terms in this vocabulary, so that term links will point here etc.'),
|
| 185 |
);
|
| 186 |
|
| 187 |
return $form;
|
| 188 |
}
|
| 189 |
|
| 190 |
function panels_taxonomy_validate($form, $form_values) {
|
| 191 |
if ((!is_numeric($form_values['arg position'])) OR $form_values['arg position'] < 1) {
|
| 192 |
return form_set_error('arg position', 'Argument position must be a positive integer.');
|
| 193 |
}
|
| 194 |
if ($form_values['primary'] AND ($form_values['arg position'] != 1)) {
|
| 195 |
return form_set_error('primary', t('You can only user the "Override term links" option if the argument position is 1.'));
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
function panels_taxonomy_form_alter($form_id, &$form) {
|
| 200 |
// ATTENTION YOU: is this up to date with the current panels?
|
| 201 |
if ($form_id == 'panels_add_content_config_form' OR $form_id == 'panels_edit_pane_config_form') {
|
| 202 |
if ($form['configuration']['#taxonomy']) {
|
| 203 |
$form['#submit']['panels_taxonomy_submit'] = array();
|
| 204 |
}
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
function panels_taxonomy_submit($form_id, $form_values) {
|
| 209 |
//set vocabulary link handler
|
| 210 |
$module = ($form_values['configuration']['primary'] ? 'panels_taxonomy': 'taxonomy');
|
| 211 |
|
| 212 |
// Make sure no other pane is using this vocab
|
| 213 |
$result = db_query('SELECT configuration FROM {panels_pane} WHERE type = "taxonomy" AND pid <> %d', $form_values['pid']);
|
| 214 |
while ($pane = db_fetch_array($result)) {
|
| 215 |
$conf = unserialize($pane['configuration']);
|
| 216 |
if ($conf['primary'] AND $conf['vid'] == $form_values['configuration']['vid']) {
|
| 217 |
$module = 'taxonomy';
|
| 218 |
break;
|
| 219 |
}
|
| 220 |
}
|
| 221 |
db_query('UPDATE {vocabulary} SET module="%s" WHERE vid=%d', $module, $form_values['configuration']['vid']);
|
| 222 |
}
|
| 223 |
|
| 224 |
/**
|
| 225 |
* Return all content types available.
|
| 226 |
*/
|
| 227 |
function panels_taxonomy_content_types() {
|
| 228 |
$types = array();
|
| 229 |
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
|
| 230 |
$types[$vid] = array(
|
| 231 |
'title' => $vocabulary->name,
|
| 232 |
'icon' => 'icon_taxonomy_browser.png',
|
| 233 |
'description' => $vocabulary->description,
|
| 234 |
'category' => array(t('Taxonomy browsers'), -1),
|
| 235 |
'path' => drupal_get_path('module', 'panels_taxonomy'),
|
| 236 |
);
|
| 237 |
}
|
| 238 |
return $types;
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
* Returns the administrative title for a type.
|
| 243 |
*/
|
| 244 |
function panels_taxonomy_title($conf) {
|
| 245 |
$vocabulary = taxonomy_get_vocabulary($form['vid']);
|
| 246 |
return 'Taxonomy browser: '.$vocabulary->name;
|
| 247 |
}
|
| 248 |
|