Skip to content

Commit

Permalink
added: pivot / cross tab, list plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pollen8 committed Jan 16, 2014
1 parent 8190371 commit 1535ef5
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 0 deletions.
46 changes: 46 additions & 0 deletions plugins/fabrik_list/pivot/forms/fields.xml
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<form>
<fields addfieldpath="/administrator/components/com_fabrik/models/fields" name="params">
<fieldset name="plg-list-php">

<field type="helplink"
url="PLG_LIST_PIVOT_HELP_SERVER" />

<field name="pivot_sum"
type="listfields"
valueformat="tableelement"
usestep="true"
mode="gui"
repeat="true"
description="PLG_LIST_PIVOT_SUM_FIELDS_DESC"
label="PLG_LIST_PIVOT_SUM_FIELDS_LABEL" />

<field name="pivot_group"
type="listfields"
valueformat="tableelement"
usestep="true"
mode="gui"
repeat="true"
description="PLG_LIST_PIVOT_GROUP_FIELDS_DESC"
label="PLG_LIST_PIVOT_GROUP_FIELDS_LABEL" />

<field name="pivot_xcol"
type="listfields"
valueformat="tableelement"
usestep="true"
repeat="true"
description="PLG_LIST_PIVOT_X_COL_DESC"
label="PLG_LIST_PIVOT_X_COL_LABEL" />

<field name="pivot_ycol"
type="listfields"
valueformat="tableelement"
usestep="true"
repeat="true"
description="PLG_LIST_PIVOT_Y_COL_DESC"
label="PLG_LIST_PIVOT_Y_COL_LABEL" />


</fieldset>
</fields>
</form>
Empty file.
@@ -0,0 +1,14 @@
; Fabrik 3.1
; Copyright (C) 2005-2013 fabrikar.com - All rights reserved.. All rights reserved.
; License GNU/GPL http://www.gnu.org/copyleft/gpl.html
; Note : All ini files need to be saved as UTF-8 - No BOM

PLG_LIST_PIVOT_ERROR_X_AND_Y_COL_MUST_BE_SELECTED="Error: pivot plugin - An X and Y column must be selected"
PLG_LIST_PIVOT_GROUP_FIELDS_DESC="A comma separated list of element you wish to group the data by, often this is a date element"
PLG_LIST_PIVOT_GROUP_FIELDS_LABEL="Group by"
PLG_LIST_PIVOT_SUM_FIELDS_DESC="A element you wish to sum() on"
PLG_LIST_PIVOT_SUM_FIELDS_LABEL="Sum"
PLG_LIST_PIVOT_X_COL_DESC="Should be one of the selected group by elements. This element can NOT be ordered"
PLG_LIST_PIVOT_X_COL_LABEL="X axis element"
PLG_LIST_PIVOT_Y_COL_DESC="Should be one of the selected group by elements. This element can be ordered"
PLG_LIST_PIVOT_Y_COL_LABEL="Y axis element"
@@ -0,0 +1,8 @@
; Fabrik 3.1
; Copyright (C) 2005-2014 fabrikar.com - All rights reserved.. All rights reserved.
; License GNU/GPL http://www.gnu.org/copyleft/gpl.html
; Note : All ini files need to be saved as UTF-8 - No BOM

PLG_FABRIK_LIST_PIVOT="Fabrik List - Pivot table"
PLG_LIST_PIVOT_HELP_SERVER="http://fabrikar.com/forums/index.php?wiki/pivot-list-plugin/"
PLG_LIST_PIVOT_DESCRIPTION="Mutates your list into a pivot / cross tab table"
256 changes: 256 additions & 0 deletions plugins/fabrik_list/pivot/pivot.php
@@ -0,0 +1,256 @@
<?php
/**
* Mutate the list data into a pivot table
*
* @package Joomla.Plugin
* @subpackage Fabrik.list.pivot
* @copyright Copyright (C) 2005-2013 fabrikar.com - All rights reserved.
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/

// No direct access
defined('_JEXEC') or die('Restricted access');

// Require the abstract plugin class
require_once COM_FABRIK_FRONTEND . '/models/plugin-list.php';

/**
* Mutate the list data into a pivot table
*
* @package Joomla.Plugin
* @subpackage Fabrik.list.pivot
* @since 3.1
*/

class PlgFabrik_ListPivot extends plgFabrik_List
{
/**
* Inject the select sum() fields into the list query JDatabaseQuery object
*
* @param array $args Plugin call arguements
*
* @return void
*/
public function onBuildQuerySelect($args)
{
if (!$query = $this->hasQuery($args))
{
return;
}

$sum = $this->sums();
$query->select($sum);
}

/**
* Do the plugin arguements have a JDatabaseQuery among them
*
* @param array $args Plugin call arguements
*
* @return mixed false if no JDatabaseQuery found otherwise returns JDatabaseQuery object
*/
private function hasQuery($args)
{
foreach ($args as $arg)
{
if (is_object($arg) && is_a($arg, 'JDatabaseQuery'))
{
return $arg;
}
}

return false;
}

/**
* Inject the group by statement into the query object
*
* @param array $args Plugin arguements
*
* @return void
*/
public function onBuildQueryGroupBy($args)
{
if (!$query = $this->hasQuery($args))
{
return;
}

$query->clear('group');
$query->group($this->group());
}

/**
* Build the group by sql statement
*
* @return string
*/
private function group()
{
$params = $this->getParams();
$groups = explode(',', $params->get('pivot_group'));

foreach ($groups as &$group)
{
$group = trim($group);
$group = FabrikString::safeColName($group);
}

$group = implode(', ', $groups);

return $group;

}

/**
* Build the sums() sql statement
*
* @return string
*/
private function sums()
{
$params = $this->getParams();
$sums = explode(',', $params->get('pivot_sum'));
$db = $this->model->getDb();

foreach ($sums as &$sum)
{
$sum = trim($sum);
$as = FabrikString::safeColNameToArrayKey($sum);

$statement = 'SUM(' . FabrikString::safeColName($sum) . ')';
$statement .= ' AS ' . $db->quoteName($as);
$statement .= ', SUM(' . FabrikString::safeColName($sum) . ')';
$statement .= ' AS ' . $db->quoteName($as . '_raw');

$sum = $statement;
}

$sum = implode(', ', $sums);

return $sum;
}

private function getCols()
{
$params = $this->getParams();
$xCol = $params->get('pivot_xcol', '');
$yCol = $params->get('pivot_ycol', '');

if ($xCol === '' || $yCol === '')
{
throw new UnexpectedValueException(JText::_('PLG_LIST_PIVOT_ERROR_X_AND_Y_COL_MUST_BE_SELECTED'));
}
//pivot___date

return array($xCol, $yCol);
}

public function onGetPluginRowHeadings(&$args)
{
list($xCol, $yCol) = $this->getCols();
$args =& $args[0];
$yColLabel = $args['tableHeadings'][$yCol];
$yColHeadingClass = $args['headingClass'][$yCol];
$yColCellClas = $args['cellClass'][$yCol];

$headings = array();

$headings[$yCol] = $yColLabel;

$data = $args['data'];
$headingClass = $args['headingClass'][$xCol];
$cellClass = $args['cellClass'][$xCol];
$args['headingClass'] = array();
$args['cellClass'] = array();

$args['headingClass'][$yCol] = $yColHeadingClass;
$args['cellClass'][$yCol] = $yColCellClas;

$group = array_shift($data);
$row = array_shift($group);

foreach ($row as $k => $v)
{
if ($k !== $yCol)
{
$headings[$k] = $k;
$args['headingClass'][$k] = $headingClass;
$args['cellClass'][$k] = $cellClass;
}
}

$args['tableHeadings'] = $headings;
}

public function onLoadData(&$args)
{
$data =& $args[0]->data;
$params = $this->getParams();
$sums = $params->get('pivot_sum');
list($xCol, $yCol) = $this->getCols();

// Get distinct areas?
$xCols = array();

foreach ($data as $group)
{
foreach ($group as $row)
{
if (!in_array($row->$xCol, $xCols))
{
$xCols[] = $row->$xCol;
}
}
}

// Order headings
asort($xCols);

// Get distinct dates
$yCols = array();

foreach ($data as $group)
{
foreach ($group as $row)
{
if (!in_array($row->$yCol, $yCols))
{
$yCols[] = $row->$yCol;
}
}
}

$new = array();

foreach ($yCols as $yColData)
{
$newRow = new stdClass();
$newRow->$yCol = $yColData;

// Set default values
foreach ($xCols as $xColData)
{
$newRow->$xColData = '';
}

foreach ($data as $group)
{
foreach ($group as $row)
{
foreach ($xCols as $xColData)
{
if ($row->$xCol === $xColData && $row->$yCol === $yColData)
{
$newRow->$xColData = $row->$sums;
}
}
}
}

$new[] = $newRow;
}

$data[0] = $new;
}
}
19 changes: 19 additions & 0 deletions plugins/fabrik_list/pivot/pivot.xml
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<extension group="fabrik_list" method="upgrade" type="plugin" version="30">
<name>plg_fabrik_list_pivot</name>
<author>Rob Clayburn</author>
<creationDate>November 2013</creationDate>
<copyright>Copyright (C) 2005-2013 fabrikar.com - All rights reserved.</copyright>
<license>GNU/GPL http://www.gnu.org/copyleft/gpl.html</license>
<authorEmail>rob@pollen-8.co.uk</authorEmail>
<authorUrl>www.fabrikar.com</authorUrl>
<version>3.1rc2</version>
<help url="PLG_LIST_PIVOT_HELP_SERVER"/>
<description>PLG_LIST_PIVOT_DESCRIPTION</description>
<files>
<filename plugin="pivot">pivot.php</filename>
<filename>index.html</filename>
<folder>forms</folder>
<folder>language</folder>
</files>
</extension>

0 comments on commit 1535ef5

Please sign in to comment.