Transform Classes

January 4th, 2016

Available in version 2.8. This functionality is an ALPHA version.
Transform introduction

Transforms Classes: PHP Classes that operate on the entire data set, not just each row

Good for operations across the entire data set like generating statistics and sorting.

[cfdb-table form="myform" trans="AddField"]

 

Enter code via Shortcodes Actions and Filters:

1
2
3
4
5
6
7
8
9
10
11
12
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/BaseTransform.php');
 
class AddField extends BaseTransform {
 
 public function getTransformedData() {
   $idx = 0;
   foreach ($this->data as &$entry) {
     $entry['index'] = $idx++;
   }
   return $this->data;
 }
}

In this example we extend BaseTransform which captures all the data in $this->data which is an array of associative-arrays of each entry. We implement the getTranformedData() function and return the entire data set.

This simplistic example merely adds an ‘index’ field to each form entry. More complicates examples are sorting the data set or returning an entirely different data set such as a set of statistics about the data.

Without using BaseTransform

Let’s look at the above example without subclassing BaseTransform:

[cfdb-table form="myform" trans="AddField"]

 

Enter code via Shortcodes Actions and Filters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class AddField {
  var $data = array();
 
  public function addEntry(&$entry) {
    $this->data[] = $entry;
 }
 public function getTransformedData() {
   $idx = 0;
   foreach ($this->data as &$entry) {
     $entry['index'] = $idx++;
   }
   return $this->data;
 }
}

When creating a transform class you must give it two functions: addEntry and getTransformedData as shown above. The above class simply collects all the data in $this->data so that it is available for getTransformedData. This is what BaseTransform does.

Statistic Example

But in some cases you may not wish to hold all the data in your object. If you have a very large amount of data then you might run out of memory. In that case, add unbuffered=”true” to your query and don’t hold the data in addEntry. If you are computing maximums for example you could do this:

 

Enter code via Shortcodes Actions and Filters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class ComputeMax {
 
  /**
   * @var array of name => max score
   */
  var $maxArray = array();
 
  public function addEntry(&$entry) {
    // Get the max score associated with each name
    if (!isset($this->maxArray[$entry['name']]) &&
      $this->maxArray[$entry['name']] > $entry['score']) {
      $this->maxArray[$entry['name']] = $entry['score'];
    }
  }
 
  public function getTransformedData() {
    // return array of associate array[name=>score]
    $data = array();
    foreach ($this->maxArray as $name => $maxScore) {
      $data[] = array($name => $maxScore);
    }
    return $data;
  }
}

Passing Parameters to Constructor

Your class can have a constructor that takes a parameter. Pass the parameter to the class like this:

[cfdb-table form="myform" trans="ConstExample(value)"]

 

Enter code via Shortcodes Actions and Filters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/BaseTransform.php');
 
class ConstExample extends BaseTransform {
 
  var $param;
 
  function __construct($param) {
    $this->param = $param;
  }
 
  public function getTransformedData() {
    // use $this->param
    return $this->data;
  }
}

Sorting with Transforms

See Sorting with Transforms.

 

 

 

 

Comments are closed.  Go To Support Forum