views_node.inc 30.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
// $Id$

/**
 * This include file implements views functionality on behalf of node.module
 */

function node_views_tables() {
  $tables['node'] = array(
    'name' => 'node',
    'provider' => 'internal', // won't show up in external list.
    'fields' => array(
      'title' => array(
        'name' => t('Node: Title'),
        'handler' => array(
          'views_handler_field_nodelink'           => t('Normal'),
          'views_handler_field_nodelink_with_mark' => t('With updated mark')
        ),
19 20 21 22 23 24 25
        'option' => array(
           '#type' => 'select',
           '#options' => array(
             'link' => 'As link',
             'nolink' => 'Without link'
            ),
        ),
26 27 28 29
        'sortable' => true,
        'addlfields' => array('changed'),
        'help' => t('Display the title of the node.'),
      ),
30
      'nid' => array(
31
        'name' => t('Node: ID'),
32
        'sortable' => true,
33 34
        'notafield' => true,
        'handler' => 'views_handler_node_nid',
35
        'sort_handler' => 'views_node_query_handler_nid',
36 37
        'help' => t('Display the NID of a node.'),
      ),
38 39 40
      'created' => array(
        'name' => t('Node: Created Time'),
        'sortable' => true,
41 42
        'handler' => views_handler_field_dates(),
        'option' => 'string',
43 44
        'help' => t('Display the post time of the node.').' '.
                  t('The option field may be used to specify the custom date format as it\'s required by the date() function or if "as time ago" has been chosen to customize the granularity of the time interval.'),
45 46 47 48
      ),
      'changed' => array(
        'name' => t('Node: Updated Time'),
        'sortable' => true,
49 50
        'handler' => views_handler_field_dates(),
        'option' => 'string',
51 52
        'help' => t('Display the last time the node was updated.').' '.
                  t('The option field may be used to specify the custom date format as it\'s required by the date() function or if "as time ago" has been chosen to customize the granularity of the time interval.'),
53 54 55 56 57 58 59
      ),
      'type' => array(
        'name' => t('Node: Type'),
        'handler' => 'views_handler_nodetype',
        'sortable' => true,
        'help' => t("The Node Type field will display the type of a node (for example, 'blog entry', 'forum post', 'story', etc)"),
      ),
60 61 62 63 64 65 66 67
      'link' => array(
        'name' => t('Node: Link to node'),
        'handler' => 'views_handler_field_node_link',
        'sortable' => false,
        'option' => 'string',
        'notafield' => 'true',
        'help' => t("This will create a link to the node; fill the option field with the text for the link. If you want titles that link to the node, use Node: Title instead."),
      ),
68 69 70 71 72 73 74 75 76 77
      'body' => array(
        'name' => t('Node: Body'),
        'handler' => array(
          'views_handler_field_body'   => t('Full Text'),
          'views_handler_field_teaser' => t('Teaser')
        ),
        'addlfields' => array('nid'),
        'notafield' => TRUE,
        'help' => t('Display the Main Content.'),
      ),
78 79 80 81 82 83 84 85 86
      'view' => array(
        'name' => t('Node: View link'),
        'handler' => 'views_handler_node_view',
        'notafield' => TRUE,
        'option' => 'string',
        'help' => t('Display a link to view the node. Enter the text of this link into the option field; if blank the default "view" will be used.'),
      ),
      'edit' => array(
        'name' => t('Node: Edit link'),
87 88 89 90
        'handler' => array(
          'views_handler_node_edit_destination'   => t('Return To View'),
          'views_handler_node_edit' => t('Return to Node')
        ),
91 92 93 94 95 96 97
        'notafield' => TRUE,
        'addlfields' => array('type', 'uid'),
        'option' => 'string',
        'help' => t('Display a link to edit the node. Enter the text of this link into the option field; if blank the default "edit" will be used.'),
      ),
      'delete' => array(
        'name' => t('Node: Delete link'),
98 99 100 101
        'handler' => array(
          'views_handler_node_delete_destination'   => t('Return To View'),
          'views_handler_node_delete' => t('Return To The Frontpage'),
        ),
102 103 104 105 106
        'notafield' => TRUE,
        'addlfields' => array('type', 'uid'),
        'option' => 'string',
        'help' => t('Display a link to delete the node. Enter the text of this link into the option field; if blank the default "delete" will be used.'),
      ),
107 108
    ),
    'sorts' => array(
109 110 111
      'nid' => array(
        'name' => t('Node: ID'),
        'help' => t('Sort by the database ID of the node.'),
112
        'handler' => 'views_node_sort_handler_nid',
113
       ),
114 115
      'created' => array(
        'name' => t('Node: Created Time'),
116 117
        'handler' => 'views_handler_sort_date',
        'option' => views_handler_sort_date_options(),
118 119 120
        'help' => t('Sort by the submission date of the node.'),
       ),
      'changed' => array(
121
        'name' => t('Node: Updated Time'),
122 123
        'handler' => 'views_handler_sort_date',
        'option' => views_handler_sort_date_options(),
124 125 126 127 128 129 130 131 132 133
        'help' => t('Sort by the last update date of the node.'),
      ),
      'sticky' => array(
        'name' => t('Node: Sticky'),
        'help' => t('Sort by whether or not the node is sticky. Choose descending to put sticky nodes at the top.'),
      ),
      'title' => array(
        'name' => t('Node: Title'),
        'help' => t('Sort by the node title, alphabetically'),
      ),
134 135 136 137
      'type' => array(
        'name' => t('Node: Type'),
        'help' => t('Sort by the node type, alphabetically'),
      ),
138 139 140 141 142 143 144 145 146
      'random' => array(
        'name' => t('Random'),
        'handler' => 'views_handler_sort_random',
        'help' => t('By choosing random, nodes will be ordered completely randomly. This is a good way to choose X random nodes from a group of nodes.'),
      ),
    ),
    'filters' => array(
      'status' => array(
        'name' => t('Node: Published'),
147
        'operator' => array('=' => t('Equals')),
148 149 150 151 152 153
        'list' => 'views_handler_operator_yesno',
        'list-type' => 'select',
        'help' => t('Filter by whether or not the node is published. This is recommended for most Views!'),
      ),
      'promote' => array(
        'name' => t('Node: Front Page'),
154
        'operator' => array('=' => t('Equals')),
155 156 157 158 159 160
        'list' => 'views_handler_operator_yesno',
        'list-type' => 'select',
        'help' => t('Filter by whether or not the node has been promoted to Front Page.'),
      ),
      'sticky' => array(
        'name' => t('Node: Sticky'),
161
        'operator' => array('=' => t('Equals')),
162 163 164 165 166 167
        'list' => 'views_handler_operator_yesno',
        'list-type' => 'select',
        'help' => t('Filter by whether or not the node is set sticky.'),
      ),
      'moderate' => array(
        'name' => t('Node: Moderated'),
168
        'operator' => array('=' => t('Equals')),
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 205 206 207 208 209 210 211 212 213
        'list' => 'views_handler_operator_yesno',
        'list-type' => 'select',
        'help' => t('Filter by whether or not the node is moderated.'),
      ),
      'type' => array(
        'name' => t('Node: Type'),
        'list' => 'views_handler_filter_nodetype',
        'list-type' => 'list',
        'operator' => 'views_handler_operator_or',
        'value-type' => 'array',
        'help' => t('Include or exclude nodes of the selected types.'),
      ),
      'anon' => array(
        'field' => 'uid',
        'name' => t('Node: Author is Anonymous'),
        'operator' => 'views_handler_operator_eqneq',
        'list' => 'views_handler_filter_useranon',
        'value-type' => 'array',
        'help' => t('This allows you to filter by whether or not the node author is anonymous.'),
      ),
      'currentuid' => array(
        'field' => 'uid',
        'name' => t('Node: Author is Current User'),
        'operator' => 'views_handler_operator_eqneq',
        'list' => 'views_handler_filter_usercurrent',
        'list-type' => 'select',
        'help' => t('This allows you to filter by whether or not the node was authored by the logged in user of the view.'),
      ),
      'currentuidtouched' => array(
        'field' => 'uid',
        'name' => t('Node: Current User Authored or Commented'),
        'operator' => array('=' => 'touched by'),
        'list' => 'views_handler_filter_usercurrent',
        'list-type' => 'select',
        'handler' => 'views_handler_filter_uid_touched',
        'help' => t('This allows you to filter by whether or not the logged in user authored or commented on the node.'),
      ),
      'distinct' => array(
        'name' => t('Node: Distinct'),
        'operator' => array('=' => 'is'),
        'list' => array('distinct' => 'distinct'),
        'handler' => 'views_handler_filter_distinct',
        'value-type' => 'array',
        'help' => t('This filter ensures that each node may only be listed once, even if it matches multiple criteria. Use this if multiple taxonomy matches return duplicated nodes.'),
      ),
214 215 216 217 218 219
      'title' => array(
        'name' => t('Node: Title'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
        'help' => t('This filter allows nodes to be filtered by their title.'),
      ),
220 221 222 223 224 225
      'created' => array(
        'name' => t('Node: Created Time'),
        'operator' => 'views_handler_operator_gtlt',
        'value' => views_handler_filter_date_value_form(),
        'handler' => 'views_handler_filter_timestamp',
        'option' => 'string',
merlinofchaos's avatar
merlinofchaos committed
226 227
        'help' => t('This filter allows nodes to be filtered by their creation date.')
          .' '. views_t_strings('filter date'),
228 229 230 231 232 233 234
      ),
      'changed' => array(
        'name' => t('Node: Updated Time'),
        'operator' => 'views_handler_operator_gtlt',
        'value' => views_handler_filter_date_value_form(),
        'handler' => 'views_handler_filter_timestamp',
        'option' => 'string',
merlinofchaos's avatar
merlinofchaos committed
235 236
        'help' => t('This filter allows nodes to be filtered by the time they were updated.')
          .' '. views_t_strings('filter date'),
237
      ),
238 239 240 241 242 243
      'body' => array(
        'name' => t('Node: Body'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_body',
        'help' => t('This filter allows nodes to be filtered by their body.'),
      ),
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    ),
  );

  // By default history table assumes current user.
  $tables['history'] = array(
    'name' => 'history',
    'provider' => 'internal',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'nid'
      ),
      'right' => array(
        'field' => 'nid'
      ),
      'extra' => array(
        'uid' => '***CURRENT_USER***'
      )
    ),
    'filters' => array(
      'timestamp' => array(
        'name' => t('Node: Has New Content'),
        'operator' => array('Has'),
        'list' => array('New Content'),
        'handler' => 'views_handler_filter_isnew',
        'value-type' => 'array',
        'help' => t('Including this filter will reduce the node set to nodes that have been updated or have new content since the user last read the node, as well as unread nodes.'),
      ),
    ),
  );

  return $tables;
}

function node_views_arguments() {
  $arguments = array(
    'nodetype' => array(
      'name' => t('Node: Type'),
      'handler' => 'views_handler_arg_nodetype',
      'help' => t('The node type argument allows users to filter a view by specifying the type of node.'),
    ),
    'nodeletter' => array(
      'name' => t('Node: Title'),
      'handler' => 'views_handler_arg_nodeletter',
      'option' => 'string',
      'help' => t('The argument will filter by the node title. For this argument, set the option to the number of characters, using 0 for full term; use 1 for an A/B/C style glossary.'),
    ),
    'year' => array(
      'name' => t('Node: Posted Year'),
      'handler' => 'views_handler_arg_year',
      'help' => t('This argument allows users to filter by what year the node was posted, in the form of CCYY.'),
    ),
    'month' => array(
      'name' => t('Node: Posted Month'),
      'handler' => 'views_handler_arg_month',
      'help' => t("Months are specified by the numbers 1-12. Since this argument does not specify a year, it is recommended that it usually follow a 'Year' argument."),
    ),
    'week' => array(
      'name' => t('Node: Posted Week'),
      'handler' => 'views_handler_arg_week',
      'help' => t("This allows the user to filter a view by the week number from 1-52. It is recommended this argument follow a 'Year' argument."),
    ),
    'monthyear' => array(
      'name' => t('Node: Posted Month + Year'),
      'handler' => 'views_handler_arg_monthyear',
      'help' => t('This argument combines Month and Year into a single argument, specified in the form CCYYMM.'),
    ),
    'fulldate' => array(
      'name' => t('Node: Posted Full Date'),
      'handler' => 'views_handler_arg_fulldate',
      'help' => t('This argument is a complete date in the form of CCYYMMDD.'),
    ),
    'nid' => array(
      'name' => t('Node: ID'),
      'handler' => 'views_handler_arg_nid',
319 320 321 322
      'option' => array(
        '#type' => 'select',
        '#options' => array('equal', 'not equal'),
      ),
323 324
      'help' => t('This argument is a single Node ID.'),
    ),
325 326 327 328 329
    // renamed from 'feed' so things don't DIE when you have viewfeed.module still
    // enabled
    'node_feed' => array(
      'name' => t('Node: Feed Selector'),
      'handler' => 'views_handler_arg_node_feed',
330 331
      'option' => 'string',
      'help' => t('This argument allows pluggable "feed" selectors. If using views_rss module, "feed" will turn the view into an RSS feed. Other modules may provide their own feeds. You may enter the title the feed will advertise in the title field here. If the feed type offers a description, enter the description of the feed in the option field here.'),
332
    ),
333 334 335 336 337 338 339 340 341
  );
  return $arguments;
}


function node_views_default_views() {

  $view = new stdClass();
  $view->name = 'frontpage';
342
  $view->description = t('The basic front page view.');
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
  $view->page = true;
  $view->url = 'frontpage';
  $view->page_title = '';
  $view->page_type = 'teaser';
  $view->use_pager = true;
  $view->nodes_per_page = variable_get('default_nodes_main', 10);
  $view->block = false;
  $view->menu = false;
  $view->breadcrumb_no_home = true;
  $view->sort = array (
    array (
      'tablename' => 'node',
      'field' => 'sticky',
      'sortorder' => 'DESC',
      'options' => '',
    ),
    array (
      'tablename' => 'node',
      'field' => 'created',
      'sortorder' => 'DESC',
      'options' => '',
    ),
  );
366

367
  $view->argument = array (
368 369 370
    array (
      'type' => 'node_feed',
      'argdefault' => '2',
371 372
      'title' => variable_get('site_name', 'drupal') .' - '. variable_get('site_slogan', ''),
      'options' => variable_get('site_mission', ''),
373 374 375
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  );
  $view->field = array (
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'promote',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
  );
  $views[$view->name] = $view;
  return $views;
}

/*
 * Format a field as a link to a node.
 */
function views_handler_field_nodelink($fieldinfo, $fielddata, $value, $data) {
403 404 405
  if ($fielddata['options'] == 'nolink') {
    return check_plain($value);
  }
406 407 408
  return l($value, "node/$data->nid");
}

409 410 411 412 413 414 415 416 417
/*
 * Not to be confused with views_handler_field_nodelink above. This formats
 * the option field as a link to the node.
 */
function views_handler_field_node_link($fieldinfo, $fielddata, $value, $data) {
  return l($fielddata['options'], "node/$data->nid");
}


418 419 420 421 422
/*
 * Format a field as a link to a 'mark', stating whether or not the node has
 * updated since it was last viewed by the user.
 */
function views_handler_field_nodelink_with_mark($fieldinfo, $fielddata, $value, $data) {
423 424 425 426 427 428
  if ($fielddata['options'] == 'nolink') {
    $link = check_plain($value);
  }
  else {
    $link = l($value, "node/$data->nid");
  }
429
  return $link .' '. theme('mark', node_mark($data->nid, $data->node_changed));
430 431 432 433 434 435 436 437 438
}

/*
 * Format a field as a node type.
 */
function views_handler_nodetype($fieldinfo, $fielddata, $value, $data) {
  return node_get_name($value);
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/*
 * Format a field as the Body of a node.
 */
function views_handler_field_body($fieldinfo, $fielddata, $value, $data) {
  $node = node_load($data->nid);

  if ($fielddata['handler'] == 'views_handler_field_body') {
    $teaser = FALSE;
  }
  else {
    $teaser = TRUE;
  }

  $node->body = str_replace('<!--break-->', '', $node->body);

  // The 'view' hook can be implemented to overwrite the default function
  // to display nodes.
  if (node_hook($node, 'view')) {
    node_invoke($node, 'view', $teaser, TRUE);
  }
  else {
    $node = node_prepare($node, $teaser);
  }
  // Allow modules to change $node->body before viewing.
  node_invoke_nodeapi($node, 'view', $teaser, TRUE);

  return $teaser ? $node->teaser : $node->body;
}

/*
 * Format a field as the Teaser of a node.
 */
function views_handler_field_teaser($fieldinfo, $fielddata, $value, $data) {
  return views_handler_field_body($fieldinfo, $fielddata, $value, $data);
}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
/*
 * Handle the node type argument.
 */
function views_handler_arg_nodetype($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $query->add_field("type");
      $query->add_groupby("node.type");
      $fieldinfo['field'] = "node.type";
      return $fieldinfo;
      break;
    case 'sort':
      $query->add_orderby('node', 'type', $argtype);
      break;
    case 'filter':
      $where = db_escape_string($arg);
491
      $query->add_where("node.type = '%s'", $where);
492 493 494 495
      break;
    case 'link':
      return l(node_get_name($query->type), "$arg/$query->type");
    case 'title':
496 497
      $title = node_get_name($query);
      return $title ? $title : check_plain($query);
498 499 500 501
  }
}

function views_handler_arg_nodeletter($op, &$query, $argtype, $arg = '') {
502
  static $field;
503 504 505
  switch($op) {
    case 'summary':
      $len = intval($arg);
506
      $field = $fieldinfo['field'] = ($len <= 0 ? "node.title" : $fieldinfo['field'] = "LEFT(node.title, $len)");
507 508 509 510
      $fieldinfo['fieldname'] = 'letter';
      return $fieldinfo;
      break;
    case 'sort':
511
      $query->add_orderby('', $field, $argtype, 'letter');
512 513 514 515
      break;
    case 'filter':
      $len = intval($argtype['options']);
      if ($len <= 0) {
516
        $query->add_where("node.title = '%s'", $arg);
517
      } else {
518
        $query->add_where("LEFT(node.title, $len) = '%s'", $arg);
519 520 521 522 523
      }
      break;
    case 'link':
      return l(strtoupper($query->letter), "$arg/$query->letter");
    case 'title':
524
      return check_plain(strtoupper($query));
525 526 527 528 529 530 531 532 533 534 535 536 537 538
  }
}

function views_handler_arg_year($op, &$query, $argtype, $arg = '') {
  $timezone = _views_get_timezone();

  switch($op) {
    case 'summary':
      $fieldinfo['field'] = "YEAR(FROM_UNIXTIME(node.created+$timezone))";
      $fieldinfo['fieldname'] = 'year';
      $query->add_field('created');
      return $fieldinfo;
      break;
    case 'sort':
539
      $query->add_orderby(NULL, "YEAR(FROM_UNIXTIME(node.created+$timezone))", $argtype, 'year');
540 541 542 543 544 545 546 547
      break;
    case 'filter':
      $year = intval($arg);
      $query->add_where("YEAR(FROM_UNIXTIME(node.created+$timezone)) = $year");
      break;
    case 'link':
      return l($query->year, "$arg/$query->year");
    case 'title':
548
      return check_plain($query);
549 550 551 552 553 554 555 556 557 558 559 560 561
  }
}
function views_handler_arg_month($op, &$query, $argtype, $arg = '') {
  $timezone = _views_get_timezone();

  switch($op) {
    case 'summary':
      $fieldinfo['field'] = "MONTH(FROM_UNIXTIME(node.created+$timezone))";
      $fieldinfo['fieldname'] = 'name';
      $query->add_field('created');
      return $fieldinfo;
      break;
    case 'sort':
562
      $query->add_orderby(NULL, "MONTH(FROM_UNIXTIME(node.created+$timezone))", $argtype, 'name');
563 564 565 566 567 568 569 570
      break;
    case 'filter':
      $month = intval($arg);
      $query->add_where("MONTH(FROM_UNIXTIME(node.created+$timezone)) = $month");
      break;
    case 'link':
      return l(format_date($query->created, 'custom', 'F'), "$arg/$query->name");
    case 'title':
571
      $month = str_pad($query, 2, '0', STR_PAD_LEFT);
572
      return format_date(strtotime("2005{$month}15"), 'custom', 'F', 0);
573 574 575 576 577 578 579 580 581 582 583 584 585 586
  }
}
function views_handler_arg_week($op, &$query, $argtype, $arg = '') {
  $timezone = _views_get_timezone();

  switch($op) {
    case 'summary':
      // The 3 makes the week 1-53, the first week of the year has at least 3 days
      $fieldinfo['field'] = "WEEK(FROM_UNIXTIME(node.created+$timezone), 3)";
      $fieldinfo['fieldname'] = "name";
      $query->add_field('created');
      return $fieldinfo;
      break;
    case 'sort':
587
      $query->add_orderby(NULL, "WEEK(FROM_UNIXTIME(node.created+$timezone), 3)", $argtype, 'name');
588 589 590 591 592 593 594 595 596
      break;
    case 'filter':
      // The 3 makes the week 1-53, the first week of the year has at least 3 days
      $week = intval($arg);
      $query->add_where("WEEK(FROM_UNIXTIME(node.created+$timezone), 3) = $week");
      break;
    case 'link':
      return l("Week $query->name", "$arg/$query->name");
    case 'title':
597
      return check_plain($query);
598 599 600 601 602 603 604 605 606 607 608 609 610
  }
}
function views_handler_arg_monthyear($op, &$query, $argtype, $arg = '') {
  $timezone = _views_get_timezone();

  switch($op) {
    case 'summary':
      $fieldinfo['field'] = "DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m')";
      $fieldinfo['fieldname'] = 'name';
      $query->add_field('created');
      return $fieldinfo;
      break;
    case 'sort':
611
      $query->add_orderby(NULL, "DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m')", $argtype, 'name');
612 613
      break;
    case 'filter':
614
      $query->add_where("DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m') = '%s'", $arg);
615 616 617 618
      break;
    case 'link':
      return l(format_date($query->created, 'custom', 'F, Y'), "$arg/$query->name");
    case 'title':
619
      return format_date(strtotime("${query}15"), 'custom', 'F, Y', 0);
620 621 622 623 624 625 626
  }
}
function views_handler_arg_fulldate($op, &$query, $argtype, $arg = '') {
  $timezone = _views_get_timezone();

  switch($op) {
    case 'summary':
627
      $fieldinfo['field'] = "DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m%%d')";
628 629 630 631 632
      $fieldinfo['fieldname'] = 'name';
      $query->add_field('created');
      return $fieldinfo;
      break;
    case 'sort':
633
      $query->add_orderby(NULL, "DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m%%d')", $argtype, 'name');
634 635
      break;
    case 'filter':
636
      $query->add_where("DATE_FORMAT(FROM_UNIXTIME(node.created+$timezone), '%Y%m%%d') = '%s'", $arg);
637 638 639 640 641 642 643 644 645 646 647 648
      break;
    case 'link':
      return l(format_date($query->created, 'custom', 'F j, Y'), "$arg/$query->name");
    case 'title':
      return format_date(strtotime($query), 'custom', 'F j, Y');
  }
}

function views_handler_arg_nid($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $query->add_field('nid');
649
      $query->add_field("title");
650 651 652 653 654 655
      $fieldinfo['field'] = 'node.nid';
      return $fieldinfo;
    case 'sort':
      // do nothing here.
      break;
    case 'filter':
merlinofchaos's avatar
merlinofchaos committed
656 657
      $args = _views_break_phrase($arg);
      if ($args[0] == 'and') {
658
        $operator = $argtype['options'] ? '!=' : '=';
merlinofchaos's avatar
merlinofchaos committed
659
        foreach ($args[1] as $arg) {
660
          $query->add_where("node.nid $operator %d", $arg);
merlinofchaos's avatar
merlinofchaos committed
661 662 663 664 665
        }
      }
      else {
        $query->add_where("node.nid IN (%s)", implode(',', $args[1]));
      }
666 667 668 669
      break;
    case 'link':
      return l($query->title, "$arg/$query->nid");
    case 'title':
merlinofchaos's avatar
merlinofchaos committed
670 671 672 673 674 675 676 677 678 679
      list($type, $info) = _views_break_phrase($query);
      if (!$info) {
        return t('Untitled');
      }
      $nids = implode(',', $info); // only does numbers so safe

      $result = db_query("SELECT title FROM {node} WHERE nid IN (%s)", $nids);
      while ($node = db_fetch_object($result)) {
        $title .= ($title ? ($type == 'or' ? ' + ' : ', ') : '') . check_plain($node->title);
      }
680
      return $title;
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
  }
}

/*
 * Custom filter for new content.
 */
function views_handler_filter_isnew($op, $filter, $filterinfo, &$query) {
  global $user;
  if (!$user || !$user->uid) {
    return;
  }

  // Hey, Drupal kills old history, so nodes that haven't been updated
  // since NODE_NEW_LIMIT are bzzzzzzzt outta here!

696
  $limit = time() - NODE_NEW_LIMIT;
697 698 699 700

  $query->ensure_table('history');
  if (module_exist('comment')) {
    $query->ensure_table('node_comment_statistics');
701
    $clause = ("OR node_comment_statistics.last_comment_timestamp > (***CURRENT_TIME*** - $limit)");
702 703 704 705
    $clause2 = "OR history.timestamp < node_comment_statistics.last_comment_timestamp";
  }

  // NULL means a history record doesn't exist. That's clearly new content.
706 707
  // Unless it's very very old content. Everything in the query is already
  // type safe cause none of it is coming from outside here.
708
  $query->add_where("(history.timestamp IS NULL AND (node.changed > (***CURRENT_TIME***-$limit) $clause)) OR history.timestamp < node.changed $clause2");
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
}

/*
 * Create a list of node types.
 */
function views_handler_filter_nodetype($op) {
  $nodes = array();
  foreach (node_get_types() as $type => $name) {
    $nodes[$type] = $name;
  }
  return $nodes;
}

/*
 * Set a query to be distinct as a filter handler
 */
function views_handler_filter_distinct($op, $filter, $filterinfo, &$query) {
726 727 728 729
  if (!$query->no_distinct) {
    $query->set_distinct();
    $query->add_groupby('node.nid');
  }
730 731
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
/*
 * Filter by body
 */
function views_handler_filter_body($op, $filter, $filterinfo, &$query) {
  $filterinfo['table'] = 'node_revisions';
  $joininfo = array(
    'left' => array(
      'table' => 'node',
      'field' => 'vid',
      ),
    'right' => array(
      'field' => 'vid',
    ),
  );
  $query->add_table($filterinfo['table'], FALSE, 1, $joininfo);
  views_handler_filter_like($op, $filter, $filterinfo, $query);
}

750 751 752 753
function views_handler_sort_random($action, &$query, $sortinfo, $sort) {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
754
      $query->add_orderby('', "rand()", "ASC", 'random_sort');
755 756
      break;
    case 'pgsql':
757
      $query->add_orderby('', "random()", "ASC", 'random_sort');
758 759 760 761
      break;
  }
}

762
function views_node_sort_handler_nid($action, &$query, $sortinfo, $sort) {
763
  $query->orderby[] = "node.nid $sort[sortorder]";
764 765
}

766 767 768 769
function views_node_query_handler_nid($fielddata, $fieldinfo) {
  return "node.nid";
}

770 771 772 773 774 775 776 777
function views_handler_arg_node_feed($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
    case 'sort':
    case 'link':
    case 'title':
      break;
    case 'filter':
778
      // Can't use node_invoke_all because we're using a reference to
779 780 781
      // $view.
      foreach (module_implements('views_feed_argument') as $name) {
        $function = $name .'_views_feed_argument';
782
        $function('argument', $GLOBALS['current_view'], $arg, $argtype);
783 784 785 786 787 788 789 790 791 792 793
      }
  }
}

function node_views_post_view($view, $items, $output) {
  foreach ($view->argument as $id => $argument) {
    if ($argument['type'] == 'node_feed') {
      $feed = $id;
      break;
    }
  }
794

795
  if ($feed !== NULL) {
796
    $additions = module_invoke_all('views_feed_argument', 'post_view', $view, $argument['type']);
797 798 799
    return (implode(' ', $additions));
  }
}
800

801
/**
802 803 804
 * helper function -- this function builds a URL for a given feed.
 * It defaults to the built in feed selector, but the 3rd arg can
 * be used to set it up for custom selectors too.
805
 */
806
function views_post_view_make_url($view, $feed_id, $arg) {
807 808 809
  // assemble the URL
  $args = array();
  foreach ($view->argument as $id => $argdata) {
810
    if (!empty($view->args[$id])) {
811 812
      $args[] = $view->args[$id];
    } else {
813
      if ($argdata['id'] == $feed_id) {
814 815 816 817 818 819 820 821 822 823
        $args[] = $arg;
      }
      else if ($argdata['argdefault'] != 1) {
        $args[] = '*';
      }
    }
  }
  
  return views_get_url($view, $args);
}
824

825 826 827 828 829 830 831 832 833
/**
 * helper function -- this function builds a URL for a given feed.
 * It defaults to the built in feed selector, but the 3rd arg can
 * be used to set it up for custom selectors too.
 */
function views_post_view_make_args($view, $feed_id, $arg) {
  // assemble the URL
  $args = array();
  foreach ($view->argument as $id => $argdata) {
834
    if (!empty($view->args[$id])) {
835 836 837 838 839 840
      $args[] = $view->args[$id];
    } else {
      if ($argdata['id'] == $feed_id) {
        $args[] = $arg;
      }
      else if ($argdata['argdefault'] != 1) {
841
        $args[] = $argdata['wildcard'] ? check_plain($argdata['wildcard']) : '*';
842 843 844 845 846 847
      }
    }
  }
  
  return $args;
}
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

/**
 * display a link to view a node
 */
function views_handler_node_view($fieldinfo, $fielddata, $value, $data) {
  $link_text = $fielddata['options'] ? $fielddata['options'] : t('view');
  return l($link_text, "node/$data->nid");
}

/**
 * display a link to edit a node
 */
function views_handler_node_edit($fieldinfo, $fielddata, $value, $data) {
  // try to build a fake node object
  $data->type = $data->node_type;
  $data->uid = $data->node_uid;
  if (node_access('update', $data)) {
    $link_text = $fielddata['options'] ? $fielddata['options'] : t('edit');
    return l($link_text, "node/$data->nid/edit");
  }
}

870 871 872 873 874 875 876 877 878 879 880 881 882
/**
 * display a link to edit a node with a destination return
 */
function views_handler_node_edit_destination($fieldinfo, $fielddata, $value, $data) {
  // try to build a fake node object
  $data->type = $data->node_type;
  $data->uid = $data->node_uid;
  if (node_access('update', $data)) {
    $link_text = $fielddata['options'] ? $fielddata['options'] : t('Edit');
    return l($link_text, "node/$data->nid/edit", NULL, drupal_get_destination());
  }
}

883 884 885 886 887 888 889 890 891 892 893 894
/**
 * display a link to delete a node
 */
function views_handler_node_delete($fieldinfo, $fielddata, $value, $data) {
  // try to build a fake node object
  $data->type = $data->node_type;
  $data->uid = $data->node_uid;
  if (node_access('delete', $data)) {
    $link_text = $fielddata['options'] ? $fielddata['options'] : t('delete');
    return l($link_text, "node/$data->nid/delete");
  }
}
895

896 897 898 899 900 901 902 903 904 905 906 907 908
/**
 * display a link to delete a node with a destination return
 */
function views_handler_node_delete_destination($fieldinfo, $fielddata, $value, $data) {
  // try to build a fake node object
  $data->type = $data->node_type;
  $data->uid = $data->node_uid;
  if (node_access('delete', $data)) {
    $link_text = $fielddata['options'] ? $fielddata['options'] : t('Delete');
    return l($link_text, "node/$data->nid/delete", NULL, drupal_get_destination());
  }
}

909 910 911 912 913 914
/**
 * Display a node's nid, which is a little bit special.
 */
function views_handler_node_nid($fieldinfo, $fielddata, $value, $data) {
  return $data->nid;
}