QueryPluginBase.php 5.11 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3
<?php

/**
tim.plunkett's avatar
tim.plunkett committed
4
 * @file
5
 * Definition of Drupal\views\Plugin\views\query\QueryPluginBase.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\query;
merlinofchaos's avatar
merlinofchaos committed
9

10
use Drupal\views\Plugin\views\PluginBase;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\ViewExecutable;
13

14 15 16
/**
 * @todo.
 */
17
abstract class QueryPluginBase extends PluginBase implements QueryInterface {
18

merlinofchaos's avatar
merlinofchaos committed
19 20 21 22 23 24 25
  /**
   * A pager plugin that should be provided by the display.
   *
   * @var views_plugin_pager
   */
  var $pager = NULL;

26 27 28 29 30 31 32
  /**
   * Stores the limit of items that should be requested in the query.
   *
   * @var int
   */
  protected $limit;

merlinofchaos's avatar
merlinofchaos committed
33 34 35 36 37 38 39
  /**
   * Generate a query and a countquery from all of the information supplied
   * to the object.
   *
   * @param $get_count
   *   Provide a countquery if this is true, otherwise provide a normal query.
   */
40
  public function query($get_count = FALSE) { }
merlinofchaos's avatar
merlinofchaos committed
41 42 43 44 45 46 47

  /**
   * Let modules modify the query just prior to finalizing it.
   *
   * @param view $view
   *   The view which is executed.
   */
48
  function alter(ViewExecutable $view) {  }
merlinofchaos's avatar
merlinofchaos committed
49 50 51 52 53 54 55

  /**
   * Builds the necessary info to execute the query.
   *
   * @param view $view
   *   The view which is executed.
   */
56
  function build(ViewExecutable $view) { }
merlinofchaos's avatar
merlinofchaos committed
57 58 59 60 61 62 63 64

  /**
   * Executes the query and fills the associated view object with according
   * values.
   *
   * Values to set: $view->result, $view->total_rows, $view->execute_time,
   * $view->pager['current_page'].
   *
65 66
   * $view->result should contain an array of objects. The array must use a
   * numeric index starting at 0.
merlinofchaos's avatar
merlinofchaos committed
67 68 69 70
   *
   * @param view $view
   *   The view which is executed.
   */
71
  function execute(ViewExecutable $view) {  }
merlinofchaos's avatar
merlinofchaos committed
72 73 74 75 76 77 78 79 80 81

  /**
   * Add a signature to the query, if such a thing is feasible.
   *
   * This signature is something that can be used when perusing query logs to
   * discern where particular queries might be coming from.
   *
   * @param view $view
   *   The view which is executed.
   */
82
  function add_signature(ViewExecutable $view) { }
merlinofchaos's avatar
merlinofchaos committed
83 84 85 86 87 88 89 90

  /**
   * Get aggregation info for group by queries.
   *
   * If NULL, aggregation is not allowed.
   */
  function get_aggregation_info() { }

91
  public function validateOptionsForm(&$form, &$form_state) { }
merlinofchaos's avatar
merlinofchaos committed
92

93
  public function submitOptionsForm(&$form, &$form_state) { }
merlinofchaos's avatar
merlinofchaos committed
94

95
  public function summaryTitle() {
merlinofchaos's avatar
merlinofchaos committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    return t('Settings');
  }

  /**
   * Set a LIMIT on the query, specifying a maximum number of results.
   */
  function set_limit($limit) {
    $this->limit = $limit;
  }

  /**
   * Set an OFFSET on the query, specifying a number of results to skip
   */
  function set_offset($offset) {
    $this->offset = $offset;
  }

113 114 115 116 117 118 119
  /**
   * Returns the limit of the query.
   */
  public function getLimit() {
    return $this->limit;
  }

merlinofchaos's avatar
merlinofchaos committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  /**
   * Create a new grouping for the WHERE or HAVING clause.
   *
   * @param $type
   *   Either 'AND' or 'OR'. All items within this group will be added
   *   to the WHERE clause with this logical operator.
   * @param $group
   *   An ID to use for this group. If unspecified, an ID will be generated.
   * @param $where
   *   'where' or 'having'.
   *
   * @return $group
   *   The group ID generated.
   */
  function set_where_group($type = 'AND', $group = NULL, $where = 'where') {
    // Set an alias.
    $groups = &$this->$where;

    if (!isset($group)) {
      $group = empty($groups) ? 1 : max(array_keys($groups)) + 1;
    }

    // Create an empty group
    if (empty($groups[$group])) {
      $groups[$group] = array('conditions' => array(), 'args' => array());
    }

    $groups[$group]['type'] = strtoupper($type);
    return $group;
  }

  /**
   * Control how all WHERE and HAVING groups are put together.
   *
   * @param $type
   *   Either 'AND' or 'OR'
   */
  function set_group_operator($type = 'AND') {
    $this->group_operator = strtoupper($type);
  }

  /**
162 163 164 165 166 167 168
   * Loads all entities contained in the passed-in $results.
   *.
   * If the entity belongs to the base table, then it gets stored in
   * $result->_entity. Otherwise, it gets stored in
   * $result->_relationship_entities[$relationship_id];
   *
   * Query plugins that don't support entities can leave the method empty.
merlinofchaos's avatar
merlinofchaos committed
169
   */
170
  function load_entities(&$results) {}
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
  /**
   * Returns a Unix timestamp to database native timestamp expression.
   *
   * @param string $field
   *   The query field that will be used in the expression.
   *
   * @return string
   *   An expression representing a timestamp with time zone.
   */
  public function getDateField($field) {
    return $field;
  }

  /**
   * Set the database to the current user timezone,
   *
   * @return string
   *   The current timezone as returned by drupal_get_user_timezone().
   */
  public function setupTimezone() {
    return drupal_get_user_timezone();
  }

  /**
   * Creates cross-database date formatting.
   *
   * @param string $field
   *   An appropriate query expression pointing to the date field.
   * @param string $format
   *   A format string for the result, like 'Y-m-d H:i:s'.
   *
   * @return string
   *   A string representing the field formatted as a date in the format
   *   specified by $format.
   */
  public function getDateFormat($field, $format) {
    return $field;
  }

merlinofchaos's avatar
merlinofchaos committed
211
}