{"id":1095,"date":"2014-07-02T00:59:20","date_gmt":"2014-07-02T04:59:20","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=1095"},"modified":"2016-01-04T22:07:07","modified_gmt":"2016-01-05T03:07:07","slug":"transform-classes","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=1095","title":{"rendered":"Transform Classes"},"content":{"rendered":"<p>Available in version 2.8. This functionality is an ALPHA version.<br \/>\n<a href=\"http:\/\/cfdbplugin.com\/?page_id=1118\">Transform introduction<\/a><\/p>\n<h3>Transforms Classes: PHP Classes that operate on the entire data set, not just each row<\/h3>\n<p>Good for operations across the entire data set like generating statistics and sorting.<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"AddField\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Enter code via <a href=\"http:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\">Shortcodes Actions and Filters<\/a>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/BaseTransform.php');\r\n\r\nclass AddField extends BaseTransform {\r\n\r\n public function getTransformedData() {\r\n   $idx = 0;\r\n   foreach ($this-&gt;data as &amp;$entry) {\r\n     $entry['index'] = $idx++;\r\n   }\r\n   return $this-&gt;data;\r\n }\r\n}<\/pre>\n<p>In this example we extend\u00a0<strong>BaseTransform<\/strong>\u00a0which captures all the data in\u00a0<strong>$this-&gt;data<\/strong>\u00a0which is an array of associative-arrays of each entry. We implement the\u00a0<strong>getTranformedData()<\/strong>\u00a0function and return the entire data set.<\/p>\n<p>This simplistic example merely adds an &#8216;index&#8217; 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.<\/p>\n<h3>Without using BaseTransform<\/h3>\n<p>Let&#8217;s look at the above example without subclassing BaseTransform:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"AddField\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Enter code via <a href=\"http:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\">Shortcodes Actions and Filters<\/a>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">class AddField {\r\n  var $data = array();\r\n\r\n  public function addEntry(&amp;$entry) {\r\n    $this-&gt;data[] = $entry;\r\n }\r\n public function getTransformedData() {\r\n   $idx = 0;\r\n   foreach ($this-&gt;data as &amp;$entry) {\r\n     $entry['index'] = $idx++;\r\n   }\r\n   return $this-&gt;data;\r\n }\r\n}<\/pre>\n<p>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-&gt;data so that it is available for getTransformedData. This is what BaseTransform does.<\/p>\n<h3>Statistic Example<\/h3>\n<p>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 <strong>unbuffered=&#8221;true&#8221;<\/strong> to your query and don&#8217;t hold the data in addEntry. If you are computing maximums for example you could do this:<\/p>\n<p>&nbsp;<\/p>\n<p>Enter code via <a href=\"http:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\">Shortcodes Actions and Filters<\/a>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">class ComputeMax {\r\n\r\n  \/**\r\n   * @var array of name =&gt; max score\r\n   *\/\r\n  var $maxArray = array();\r\n\r\n  public function addEntry(&amp;$entry) {\r\n    \/\/ Get the max score associated with each name\r\n    if (!isset($this-&gt;maxArray[$entry['name']]) &amp;&amp;\r\n      $this-&gt;maxArray[$entry['name']] &gt; $entry['score']) {\r\n      $this-&gt;maxArray[$entry['name']] = $entry['score'];\r\n    }\r\n  }\r\n\r\n  public function getTransformedData() {\r\n    \/\/ return array of associate array[name=&gt;score]\r\n    $data = array();\r\n    foreach ($this-&gt;maxArray as $name =&gt; $maxScore) {\r\n      $data[] = array($name =&gt; $maxScore);\r\n    }\r\n    return $data;\r\n  }\r\n}<\/pre>\n<h3>Passing Parameters to Constructor<\/h3>\n<p>Your class can have a constructor that takes a parameter. Pass the parameter to the class like this:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"ConstExample(value)\"<span style=\"color: #000000;\">]<\/span><\/span><\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Enter code via <a href=\"http:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\">Shortcodes Actions and Filters<\/a>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/BaseTransform.php');\r\n\r\nclass ConstExample\u00a0extends BaseTransform {\r\n\r\n  var $param;\r\n\r\n  function __construct($param) {\r\n    $this-&gt;param = $param;\r\n  }\r\n\r\n  public function getTransformedData() {\r\n    \/\/ use $this-&gt;param\r\n    return $this-&gt;data;\r\n  }\r\n}\r\n\r\n<\/pre>\n<h3>Sorting with Transforms<\/h3>\n<p>See <a href=\"http:\/\/cfdbplugin.com\/?page_id=1135\">Sorting with Transforms<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. &nbsp; Enter code via Shortcodes Actions and Filters: require_once(ABSPATH . &#8216;wp-content\/plugins\/contact-form-7-to-database-extension\/BaseTransform.php&#8217;); class AddField extends BaseTransform [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1118,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-1095","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-hF","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1095"}],"version-history":[{"count":20,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1095\/revisions"}],"predecessor-version":[{"id":1370,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1095\/revisions\/1370"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1118"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}