Building a plugin

I’m building a plugin, and I want to have a Subtable, so that my users can click on the overview data, and display the data from there.

Following the code that I’ve been able to glean:

public function getCompanyList($idSite, $period, $date )
    {
        $dataTable = new Piwik_DataTable();

        $query = Piwik_Query("SELECT cl.id, cl.company_name name, sf.id sf_id FROM sitedb.company_lookup cl INNER JOIN sitedb.storefronts sf ON cl.id = sf.company_id");
        while ($row = $query->fetch()) {
            $piwik_row = new Piwik_DataTable_Row;
            $piwik_row->setSubTable( $this->getProductsForCompany($idSite, $period, $date, $row['id']) );
            $piwik_row->setColumns( array('id' => $row['id'], 'Company Name' => $row['name']) );
            $dataTable->addRow($piwik_row);

        }
        return $dataTable;
    }

    public function getProductsForCompany($idSite, $period, $date, $company_id )
    {
        if (!defined('PIWIK_ENABLE_DISPATCH')) define('PIWIK_ENABLE_DISPATCH', false);
        if (!defined('PIWIK_ENABLE_ERROR_HANDLER')) define('PIWIK_ENABLE_ERROR_HANDLER', false);
        require_once PIWIK_INCLUDE_PATH . "/index.php";
        require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
        Piwik_FrontController::getInstance()->init();
        $request = new Piwik_API_Request('
                method=Actions.getActions
                &idSite=' . $idSite . '
                &date=' . $date . '
                &period=' . $period . '
                &format=PHP
                &filter_column=label
                &filter_pattern=product.php
                &filter_sort_column=nb_visits
                &filter_sort_order=desc
                &token_auth=anonymous
                ');
        $result = $request->process();

        // contains an array of visits to storefront.php
        $result = unserialize($result);

        $query = Piwik_Query("SELECT sp.product_id id, sp.name, sp.storefront_id sf_id, cl.company_name FROM sitedb.storefront_products sp INNER JOIN sitedb.storefronts sf ON sp.storefront_id = sf.id INNER JOIN sitedb.company_lookup cl ON sf.company_id = cl.id WHERE cl.id = {$company_id}");
        $dataTable = new Piwik_DataTable();

        while ($row = $query->fetch()) {
            // piwik returns & escaped to & -- make sure that's what you use to search!
            $this->array_search_in_level("/product.php?id=" . $row['id'] . "&sf_id=" . $row['sf_id'], $result, 'label', $storefront_array, 1);

            if (is_array($storefront_array) && array_key_exists('nb_visits', $storefront_array)) {
                $piwik_row = new Piwik_DataTable_Row;
                $piwik_row->setColumns( array('id' => $row['id'], 'Product Name' => $row['name'], 'Page Views' => $storefront_array['nb_visits']) );
                $dataTable->addRow($piwik_row);
            }
        }
        return $dataTable;
    }

However, the subTable never shows up. Am I doing something wrong?

1 Like

please post your tech questions on piwik-hackers mailing list

I know this post is in the wrong forum but wanted to add a reply because it is one of the few results on google for this topic. It took me many hours to find out how to create a subDataTable. Hopefully this will save others time.

Were you able to get the sub table working?

It seems like the problem is most likely in the controller.php that would be calling the API functions. The functions in the controller should be similar to the below.

function getCompanyList($fetch = false)
{
		$view = Piwik_ViewDataTable::factory();
		$view->setColumnsToDisplay( array('id', 'Company Name') );
		
		$view->init( $this->pluginName,  __FUNCTION__, 
									'YourPluginName.getCompanyList',
									'getProductsForCompany'
								);
		
		$wdh->printMe($view);
		
		return $this->renderView($view, $fetch);
}

function getProductsForCompany( $fetch = false )
{
		$view = Piwik_ViewDataTable::factory();
		$view->init( $this->pluginName, 	__FUNCTION__, 
										'YourPluginName.getProductsForCompany'
								);
		$view->disableSearchBox();
		$view->setColumnsToDisplay( array('Product Name','Page Views') );
		return $this->renderView($view, $fetch);
}

I know this is an old topic, but I still can’t get this to work. My question is how do you get Company (as in example above) into the second function (the one that is searching for products). I don’t see it being passed into. I am running mySQL query for both Company and Product, so my query has a "WHERE comapny = ‘…’ " clause.