Formula.php 1.9 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\argument\Formula.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\argument;
9

10
use Drupal\Component\Annotation\PluginID;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\ViewExecutable;
13

merlinofchaos's avatar
merlinofchaos committed
14 15 16 17 18 19 20 21 22
/**
 * Abstract argument handler for simple formulae.
 *
 * Child classes of this object should implement summary_argument, at least.
 *
 * Definition terms:
 * - formula: The formula to use for this handler.
 *
 * @ingroup views_argument_handlers
23
 *
24
 * @PluginID("formula")
25
 */
26
class Formula extends ArgumentPluginBase {
27

merlinofchaos's avatar
merlinofchaos committed
28
  var $formula = NULL;
29

merlinofchaos's avatar
merlinofchaos committed
30
  /**
31
   * Overrides Drupal\views\Plugin\views\argument\ArgumentPluginBase::init().
merlinofchaos's avatar
merlinofchaos committed
32
   */
33 34
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
merlinofchaos's avatar
merlinofchaos committed
35 36 37 38 39 40

    if (!empty($this->definition['formula'])) {
      $this->formula = $this->definition['formula'];
    }
  }

41
  public function getFormula() {
42
    return str_replace('***table***', $this->tableAlias, $this->formula);
merlinofchaos's avatar
merlinofchaos committed
43 44 45 46 47 48
  }

  /**
   * Build the summary query based on a formula
   */
  function summary_query() {
49
    $this->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
50
    // Now that our table is secure, get our formula.
51
    $formula = $this->getFormula();
merlinofchaos's avatar
merlinofchaos committed
52 53 54 55 56

    // Add the field.
    $this->base_alias = $this->name_alias = $this->query->add_field(NULL, $formula, $this->field);
    $this->query->set_count_field(NULL, $formula, $this->field);

57
    return $this->summaryBasics(FALSE);
merlinofchaos's avatar
merlinofchaos committed
58 59 60 61 62
  }

  /**
   * Build the query based upon the formula
   */
63
  public function query($group_by = FALSE) {
64
    $this->ensureMyTable();
merlinofchaos's avatar
merlinofchaos committed
65 66
    // Now that our table is secure, get our formula.
    $placeholder = $this->placeholder();
67
    $formula = $this->getFormula() .' = ' . $placeholder;
merlinofchaos's avatar
merlinofchaos committed
68 69 70 71 72
    $placeholders = array(
      $placeholder => $this->argument,
    );
    $this->query->add_where(0, $formula, $placeholders, 'formula');
  }
73

merlinofchaos's avatar
merlinofchaos committed
74
}