Formula.php 1.84 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\Core\Annotation\Plugin;
11
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
12

merlinofchaos's avatar
merlinofchaos committed
13
14
15
16
17
18
19
20
21
/**
 * 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
22
 *
23
 * @Plugin(
aspilicious's avatar
aspilicious committed
24
 *   id = "formula"
25
26
 * )
 */
27
class Formula extends ArgumentPluginBase {
28

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

merlinofchaos's avatar
merlinofchaos committed
31
  /**
32
   * Constructs a Formula object.
merlinofchaos's avatar
merlinofchaos committed
33
   */
34
35
  public function __construct(array $configuration, $plugin_id, DiscoveryInterface $discovery) {
    parent::__construct($configuration, $plugin_id, $discovery);
merlinofchaos's avatar
merlinofchaos committed
36
37
38
39
40
41
42

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

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

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

    // 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);

    return $this->summary_basics(FALSE);
  }

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

merlinofchaos's avatar
merlinofchaos committed
75
}