views_handler_sort_formula.inc

File

handlers/views_handler_sort_formula.inc
View source
  1. <?php
  2. /**
  3. * Base sort handler that has no options and performs a simple sort
  4. *
  5. * Definition items:
  6. * - formula: The formula to use to sort on, such as with a random sort.
  7. * The formula should be an array, with keys for database
  8. * types, and 'default' for non-specified. 'default' is
  9. * required, all others ('mysql', 'mysqli' and 'pgsql' are
  10. * optional). It is recommended you use 'default' for mysql
  11. * and create specific overrides for pgsql when the formulae
  12. * differ.
  13. *
  14. * @ingroup views_sort_handlers
  15. */
  16. class views_handler_sort_formula extends views_handler_sort {
  17. /**
  18. * Constructor to take the formula this sorts on.
  19. */
  20. function construct() {
  21. $this->formula = $this->definition['formula'];
  22. if (is_array($this->formula) && !isset($this->formula['default'])) {
  23. $this->error = t('views_handler_sort_formula missing default: @formula', array('@formula' => var_export($this->formula, TRUE)));
  24. }
  25. parent::construct();
  26. }
  27. /**
  28. * Called to add the sort to a query.
  29. */
  30. function query() {
  31. if (is_array($this->formula)) {
  32. global $db_type;
  33. if (isset($this->formula[$db_type])) {
  34. $formula = $this->formula[$db_type];
  35. }
  36. else {
  37. $formula = $this->formula['default'];
  38. }
  39. }
  40. else {
  41. $formula = $this->formula;
  42. }
  43. $this->ensure_my_table();
  44. // Add the field.
  45. $this->query->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $this->field);
  46. }
  47. }