| 1 |
<?php
|
| 2 |
// $Id: simpleviews.inc,v 1.1 2008/09/25 22:08:09 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* SimpleViews utility library.
|
| 7 |
*
|
| 8 |
* Provides helper functions to generate standard Views based on a small number
|
| 9 |
* of parameters.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Build a view object.
|
| 14 |
*
|
| 15 |
* This function converts a simple set of parameters into a full View definition.
|
| 16 |
*
|
| 17 |
* @param $simpleview
|
| 18 |
* An array of votes, each with the following structure:
|
| 19 |
* $simpleview['path'] (Required)
|
| 20 |
* $simpleview['title'] (Required)
|
| 21 |
* $simpleview['header'] (Optional)
|
| 22 |
* $simpleview['filter'] (Optional, defaults to 'all')
|
| 23 |
* $simpleview['style'] (Optional, defaults to 'teasers')
|
| 24 |
* $simpleview['sort'] (Optional, defaults to 'newest')
|
| 25 |
* $simpleview['argument'] (Optional)
|
| 26 |
* $simpleview['rss'] (Optional, defaults to TRUE)
|
| 27 |
* $simpleview['block'] (Optional, defaults to TRUE)
|
| 28 |
* $simpleview['module'] (Optional, defaults to 'simpleviews')
|
| 29 |
*
|
| 30 |
* For individual properties inside $simplview, accepted values include:
|
| 31 |
* $simpleview['filter'] 'all', or 'node:$nodetype'
|
| 32 |
* $simpleview['style'] 'teasers', 'full', 'titles', or 'gallery'
|
| 33 |
* $simpleview['sort'] 'newest', 'oldest', 'a-z', 'rating', or 'hits'
|
| 34 |
* $simpleview['argument'] 'author', 'date', 'term:$vid', or 'field:$fieldname'
|
| 35 |
|
| 36 |
* @return
|
| 37 |
* A newly constructed View object. To be properly handled by the Views module,
|
| 38 |
* the $view->name property must be set.
|
| 39 |
*/
|
| 40 |
function _simpleviews_build_view($simpleview = array()) {
|
| 41 |
$simpleview += simpleviews_default_data();
|
| 42 |
|
| 43 |
// Now we'll set up the basic starting structure.
|
| 44 |
$view = new view;
|
| 45 |
$view->name = empty($simpleview['name']) ? $simpleview['module'] . '_' . $simpleview['svid'] : $simpleview['name'];
|
| 46 |
$view->description = "Generated automatically by the {$simpleview['module']} module.";
|
| 47 |
$view->base_table = 'node';
|
| 48 |
$view->is_cacheable = FALSE;
|
| 49 |
$view->api_version = 2;
|
| 50 |
$view->disabled = FALSE;
|
| 51 |
|
| 52 |
// Bootstrap the default display for the view. Here's where the action happens.
|
| 53 |
$handler = $view->new_display('default', 'Defaults', 'default');
|
| 54 |
$handler->override_option('title', $simpleview['title']);
|
| 55 |
$handler->override_option('row_plugin', 'node');
|
| 56 |
$handler->override_option('row_options', array(
|
| 57 |
'teaser' => ($simpleview['style'] == 'full') ? 0 : 1,
|
| 58 |
'links' => 1,
|
| 59 |
'comments' => 0,
|
| 60 |
));
|
| 61 |
$handler->override_option('use_pager', '1');
|
| 62 |
|
| 63 |
_simpleviews_add_filter($handler, $simpleview);
|
| 64 |
_simpleviews_add_sort($handler, $simpleview);
|
| 65 |
_simpleviews_add_argument($handler, $simpleview);
|
| 66 |
|
| 67 |
// This next step might be tricky if we want to allow just-block or just-feed
|
| 68 |
// views in the future, but for now we will always add a page.
|
| 69 |
_simpleviews_add_page($view, $simpleview);
|
| 70 |
|
| 71 |
_simpleviews_add_extras($view, $simpleview);
|
| 72 |
|
| 73 |
drupal_alter('simpleview', $view, $simpleview);
|
| 74 |
|
| 75 |
// We will NOT save the view. Instead we're going to return it so others can
|
| 76 |
// expose it as a default, save it to the DB, or whatever they'd like to do.
|
| 77 |
return $view;
|
| 78 |
}
|
| 79 |
|
| 80 |
function _simpleviews_add_filter(&$handler, $simpleview) {
|
| 81 |
// We always want to add the status filter.
|
| 82 |
$filters['status'] = array(
|
| 83 |
'operator' => '=',
|
| 84 |
'value' => 1,
|
| 85 |
'group' => '0',
|
| 86 |
'exposed' => FALSE,
|
| 87 |
'expose' => array(
|
| 88 |
'operator' => FALSE,
|
| 89 |
'label' => '',
|
| 90 |
),
|
| 91 |
'id' => 'status',
|
| 92 |
'table' => 'node',
|
| 93 |
'field' => 'status',
|
| 94 |
'relationship' => 'none',
|
| 95 |
);
|
| 96 |
|
| 97 |
// Currently, the only supported filters are per-node-type.
|
| 98 |
// 'all' is actually just a passthrough option that doesn't apply
|
| 99 |
// filtering beyond the standard 'published' check.
|
| 100 |
$filter = $simpleview['filter'];
|
| 101 |
if (strstr($filter, 'node') !== FALSE && $type = end(explode(':', $filter))) {
|
| 102 |
$filters['type'] = array(
|
| 103 |
'operator' => 'in',
|
| 104 |
'value' => array($type => $type),
|
| 105 |
'group' => '0',
|
| 106 |
'exposed' => FALSE,
|
| 107 |
'expose' => array('operator' => FALSE, 'label' => ''),
|
| 108 |
'id' => 'type',
|
| 109 |
'table' => 'node',
|
| 110 |
'field' => 'type',
|
| 111 |
'relationship' => 'none',
|
| 112 |
);
|
| 113 |
}
|
| 114 |
|
| 115 |
$handler->override_option('filters', $filters);
|
| 116 |
}
|
| 117 |
|
| 118 |
function _simpleviews_add_sort(&$handler, $simpleview) {
|
| 119 |
switch ($simpleview['sort']) {
|
| 120 |
case 'newest':
|
| 121 |
$sorts['created'] = array(
|
| 122 |
'order' => 'DESC',
|
| 123 |
'granularity' => 'second',
|
| 124 |
'id' => 'created',
|
| 125 |
'table' => 'node',
|
| 126 |
'field' => 'created',
|
| 127 |
'relationship' => 'none',
|
| 128 |
);
|
| 129 |
break;
|
| 130 |
|
| 131 |
case 'oldest':
|
| 132 |
$sorts['created'] = array(
|
| 133 |
'order' => 'ASC',
|
| 134 |
'granularity' => 'second',
|
| 135 |
'id' => 'created',
|
| 136 |
'table' => 'node',
|
| 137 |
'field' => 'created',
|
| 138 |
'relationship' => 'none',
|
| 139 |
);
|
| 140 |
break;
|
| 141 |
|
| 142 |
case 'a-z':
|
| 143 |
$sorts['title'] = array(
|
| 144 |
'order' => 'ASC',
|
| 145 |
'id' => 'title',
|
| 146 |
'table' => 'node',
|
| 147 |
'field' => 'title',
|
| 148 |
'relationship' => 'none',
|
| 149 |
);
|
| 150 |
break;
|
| 151 |
|
| 152 |
case 'top-rated':
|
| 153 |
if (module_exists('fivestar')) {
|
| 154 |
// Use percentages
|
| 155 |
$handler->override_option('relationships', array(
|
| 156 |
'votingapi_cache' => array(
|
| 157 |
'label' => 'Average',
|
| 158 |
'required' => 0,
|
| 159 |
'votingapi' => array(
|
| 160 |
'value_type' => 'percent',
|
| 161 |
'tag' => 'vote',
|
| 162 |
'function' => 'average',
|
| 163 |
),
|
| 164 |
'id' => 'votingapi_cache',
|
| 165 |
'table' => 'node',
|
| 166 |
'field' => 'votingapi_cache',
|
| 167 |
'relationship' => 'none',
|
| 168 |
),
|
| 169 |
));
|
| 170 |
}
|
| 171 |
else if (module_exists('plus1') || module_exists('drigg')) {
|
| 172 |
// Use points
|
| 173 |
$handler->override_option('relationships', array(
|
| 174 |
'votingapi_cache' => array(
|
| 175 |
'label' => 'Average',
|
| 176 |
'required' => 0,
|
| 177 |
'votingapi' => array(
|
| 178 |
'value_type' => 'points',
|
| 179 |
'tag' => 'vote',
|
| 180 |
'function' => 'sum',
|
| 181 |
),
|
| 182 |
'id' => 'votingapi_cache',
|
| 183 |
'table' => 'node',
|
| 184 |
'field' => 'votingapi_cache',
|
| 185 |
'relationship' => 'none',
|
| 186 |
),
|
| 187 |
));
|
| 188 |
}
|
| 189 |
|
| 190 |
$sorts['value'] = array(
|
| 191 |
'order' => 'DESC',
|
| 192 |
'id' => 'value',
|
| 193 |
'table' => 'votingapi_cache',
|
| 194 |
'field' => 'value',
|
| 195 |
'relationship' => 'votingapi_cache',
|
| 196 |
);
|
| 197 |
break;
|
| 198 |
|
| 199 |
case 'popular':
|
| 200 |
$sorts['totalcount'] = array(
|
| 201 |
'order' => 'DESC',
|
| 202 |
'id' => 'totalcount',
|
| 203 |
'table' => 'node_counter',
|
| 204 |
'field' => 'totalcount',
|
| 205 |
'relationship' => 'none',
|
| 206 |
);
|
| 207 |
break;
|
| 208 |
}
|
| 209 |
|
| 210 |
if (!empty($sorts)) {
|
| 211 |
$handler->override_option('sorts', $sorts);
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
function _simpleviews_add_argument(&$handler, $simpleview) {
|
| 216 |
$argument = $simpleview['argument'];
|
| 217 |
|
| 218 |
if ($argument == 'author') {
|
| 219 |
$handler->override_option('arguments', array(
|
| 220 |
'uid' => array(
|
| 221 |
'default_action' => 'ignore',
|
| 222 |
'style_plugin' => 'default_summary',
|
| 223 |
'style_options' => array(),
|
| 224 |
'wildcard' => 'all',
|
| 225 |
'wildcard_substitution' => 'All',
|
| 226 |
'title' => '',
|
| 227 |
'default_argument_type' => 'fixed',
|
| 228 |
'default_argument' => '',
|
| 229 |
'validate_type' => 'none',
|
| 230 |
'validate_fail' => 'not found',
|
| 231 |
'break_phrase' => 0,
|
| 232 |
'not' => 0,
|
| 233 |
'id' => 'uid',
|
| 234 |
'table' => 'users',
|
| 235 |
'field' => 'uid',
|
| 236 |
'relationship' => 'none',
|
| 237 |
'default_options_div_prefix' => '',
|
| 238 |
),
|
| 239 |
));
|
| 240 |
}
|
| 241 |
else if ($argument = 'date') {
|
| 242 |
$handler->override_option('arguments', array(
|
| 243 |
'created_year_month' => array(
|
| 244 |
'id' => 'created_year_month',
|
| 245 |
'table' => 'node',
|
| 246 |
'field' => 'created_year_month',
|
| 247 |
'validate_type' => 'none',
|
| 248 |
'validate_fail' => 'ignore',
|
| 249 |
'default_argument_type' => 'fixed',
|
| 250 |
'relationship' => 'none',
|
| 251 |
'default_action' => 'ignore',
|
| 252 |
'validate_argument_php' => '',
|
| 253 |
),
|
| 254 |
));
|
| 255 |
}
|
| 256 |
else if (strstr($argument, 'term:') !== FALSE && $vid = end(explode(':', $argument))) {
|
| 257 |
$handler->override_option('arguments', array(
|
| 258 |
'tid' => array(
|
| 259 |
'default_action' => 'ignore',
|
| 260 |
'style_plugin' => 'default_summary',
|
| 261 |
'style_options' => array(),
|
| 262 |
'wildcard' => 'all',
|
| 263 |
'wildcard_substitution' => 'All',
|
| 264 |
'title' => '',
|
| 265 |
'default_argument_type' => 'fixed',
|
| 266 |
'default_argument' => '',
|
| 267 |
'validate_type' => 'taxonomy_term',
|
| 268 |
'validate_fail' => 'ignore',
|
| 269 |
'break_phrase' => 0,
|
| 270 |
'add_table' => 0,
|
| 271 |
'require_value' => 0,
|
| 272 |
'reduce_duplicates' => 1,
|
| 273 |
'set_breadcrumb' => 1,
|
| 274 |
'id' => 'tid',
|
| 275 |
'table' => 'term_node',
|
| 276 |
'field' => 'tid',
|
| 277 |
'relationship' => 'none',
|
| 278 |
'validate_argument_vocabulary' => array((string)$vid => $vid),
|
| 279 |
'validate_argument_type' => 'convert',
|
| 280 |
),
|
| 281 |
));
|
| 282 |
}
|
| 283 |
else if (strstr($argument, 'field:')) {
|
| 284 |
// No handling for this at the moment. In the future, we want
|
| 285 |
// to have some custom handling for CCK field based arguments.
|
| 286 |
// For now? No love.
|
| 287 |
}
|
| 288 |
}
|
| 289 |
|
| 290 |
|
| 291 |
function _simpleviews_add_page(&$view, $simpleview) {
|
| 292 |
// Here we add the default page view. Good stuff.
|
| 293 |
$handler = $view->new_display('page', 'Page', 'page');
|
| 294 |
$handler->override_option('path', $simpleview['path']);
|
| 295 |
$handler->override_option('menu', array(
|
| 296 |
'type' => 'normal',
|
| 297 |
'title' => $simpleview['title'],
|
| 298 |
'weight' => '0',
|
| 299 |
'default' => 'navigation' // Relies on drupal.org/node/284893 and drupal.org/node/285309
|
| 300 |
));
|
| 301 |
$handler->override_option('tab_options', array(
|
| 302 |
'type' => 'none',
|
| 303 |
'title' => '',
|
| 304 |
'weight' => 0,
|
| 305 |
));
|
| 306 |
|
| 307 |
if ($simpleview['style'] == 'titles') {
|
| 308 |
_simpleviews_set_display_to_title_list($handler, $simpleview);
|
| 309 |
$handler->override_option('items_per_page', 20);
|
| 310 |
}
|
| 311 |
}
|
| 312 |
|
| 313 |
|
| 314 |
function _simpleviews_set_display_to_title_list(&$handler, $simpleview) {
|
| 315 |
// This lets us override the defaults for blocks and pages set to
|
| 316 |
// list view.
|
| 317 |
$handler->override_option('fields', array(
|
| 318 |
'title' => array(
|
| 319 |
'label' => '',
|
| 320 |
'link_to_node' => 1,
|
| 321 |
'exclude' => 0,
|
| 322 |
'id' => 'title',
|
| 323 |
'table' => 'node',
|
| 324 |
'field' => 'title',
|
| 325 |
'relationship' => 'none',
|
| 326 |
'override' => array('button' => 'Use default'),
|
| 327 |
),
|
| 328 |
));
|
| 329 |
$handler->override_option('row_plugin', 'fields');
|
| 330 |
$handler->override_option('style_plugin', 'list');
|
| 331 |
$handler->override_option('style_options', array(
|
| 332 |
'grouping' => '',
|
| 333 |
'type' => 'ul',
|
| 334 |
));
|
| 335 |
}
|
| 336 |
|
| 337 |
|
| 338 |
function _simpleviews_add_extras(&$view, $simpleview) {
|
| 339 |
// Here we generate a block with a [more] link.
|
| 340 |
if (!empty($simpleview['block'])) {
|
| 341 |
$handler = $view->new_display('block', 'Block', 'block');
|
| 342 |
$handler->override_option('items_per_page', 5);
|
| 343 |
$handler->override_option('use_pager', '0');
|
| 344 |
$handler->override_option('use_more', 1);
|
| 345 |
$handler->override_option('block_description', $simpleview['title']);
|
| 346 |
$handler->override_option('block_caching', -1);
|
| 347 |
|
| 348 |
_simpleviews_set_display_to_title_list($handler, $simpleview);
|
| 349 |
}
|
| 350 |
|
| 351 |
// Here we generate an RSS feed at [path]/rss.xml
|
| 352 |
if (!empty($simpleview['rss'])) {
|
| 353 |
$handler = $view->new_display('feed', 'Feed', 'feed');
|
| 354 |
$handler->override_option('row_plugin', 'node_rss');
|
| 355 |
$handler->override_option('row_options', array(
|
| 356 |
'item_length' => 'default',
|
| 357 |
));
|
| 358 |
$handler->override_option('style_plugin', 'rss');
|
| 359 |
$handler->override_option('style_options', array(
|
| 360 |
'mission_description' => FALSE,
|
| 361 |
'description' => '',
|
| 362 |
));
|
| 363 |
|
| 364 |
$path = $simpleview['path'];
|
| 365 |
$path .= empty($simpleview['argument']) ? '/rss.xml' : '/%/rss.xml';
|
| 366 |
$handler->override_option('path', $path);
|
| 367 |
$handler->override_option('displays', array('page' => 'page'));
|
| 368 |
}
|
| 369 |
}
|