@phoob opened this issue on July 15th 2016

Hello!

I don't know if this might be related to #10292 – but I discovered another issue when searching in a report that I created. ## The issue

In the API method used to show the report (see below), I add two columns and a "logo" for each row using filters.

When I look at the report in the browser, and try to search or click "next" to show the next rows (if there are more than the current display limit), the values in the columns added by using filters are missing (but the logos are still there). When deleting the search query, the values in these columns reappear… I noticed one random exception, where a row retained its filter added values. ## See attached screen shots: ### 1 - All is well:

### 2 - Clicked "next" (results). Values missing:

### 3 - Searched. Values missing:

### 4 - Another search. One row has values(!):

## Here is the API method used for the report:

    public function getSkjemasvar($idSite, $period, $date, $segment = false, $expanded = false, $flat = false, $idSubtable = null, $depth = null)
    {
        Piwik::checkUserHasViewAccess($idSite);

        $dataTable = Archive::createDataTableFromArchive(
            Archiver::SKJEMASVAR_FANT_DU_DET_DU_LETTE_ETTER_RECORD, 
            $idSite, 
            $period, 
            $date, 
            $segment, 
            $expanded, 
            $flat, 
            $idSubtable,
            $depth
        );

        $dataTable->queueFilter('ColumnCallbackAddColumn', array('label', 'nb_visits', function($label){
            $visits = \Piwik\API\Request::processRequest('Actions.getPageUrl', array('pageUrl' => 'http://' . $label));
            return $visits->getColumn('nb_visits')[0];
        }));

        $dataTable->queueFilter('ColumnCallbackAddColumn', array(array('ja', 'nei', 'nb_visits'), 'score', function($ja, $nei, $nb_visits){
            $andel_som_har_svart = 10 * ($ja + $nei) / $nb_visits;
            $normert_antall_ja_nei_svar = 2 * ( ( $ja / ( $ja + $nei ) ) - 0.5 );
            $score = $andel_som_har_svart * $normert_antall_ja_nei_svar ;
            return $score;
        }));

        $dataTable->queueFilter('ColumnCallbackAddMetadata', array(array('ja', 'nei'), 'logo', function($ja, $nei){
            $imagesDir = \Piwik\SettingsPiwik::getPiwikUrl() . '/plugins/Skjemasvar/img/';
            $normert_antall_ja_nei_svar = 2 * ( ( $ja / ( $ja + $nei ) ) - 0.5 );
            if ($normert_antall_ja_nei_svar < -0.5 ) return $imagesDir . 'traffic-lights-red.png';
            elseif ($normert_antall_ja_nei_svar < 0.5 ) return $imagesDir . 'traffic-lights-yellow.png';
            else return $imagesDir . 'traffic-lights-green.png';
        }));

        $dataTable->queueFilter('AddSegmentByLabel', array('pageUrl'));

        $dataTable->applyQueuedFilters();

        return $dataTable;

    }
@mattab commented on July 19th 2016

Hello @phoob

Could you maybe reproduce this issue in any of the core reports? or is the issue maybe only in your custom report?

@phoob commented on July 19th 2016

Thanks, good idea. Will try when I get back to my keyboard in a few days.

@phoob commented on July 20th 2016

Ok, I have an update… I have narrowed the issue down to the fact that I use \Piwik\API\Request::processRequest() in a filter in the API:

        $dataTable->queueFilter('ColumnCallbackAddColumn', array('label', 'nb_visits', function($label){
            $visits = \Piwik\API\Request::processRequest('Actions.getPageUrl', array('pageUrl' => 'http://' . $label));
            return $visits->getColumn('nb_visits')[0];
        }));

I got the same result when using processRequest in a filter in a core plugin report as well (GetEntryPageUrls). I thought this was the intended usage of this function? Am I doing something wrong..?

@phoob commented on August 11th 2016

Ok, another update: I have solved it, by adjusting the abovementioned filter, like this:

        $dataTable->queueFilter('ColumnCallbackAddColumn', array('label', 'nb_visits', function($label) use (&$idSite, &$period, &$date, &$segment){
            // check that $label is a url
            if ( !filter_var($label = 'http://' . $label, FILTER_VALIDATE_URL) ) return;
            // get visitors from $label, and override all params
            $request = \Piwik\API\Request::processRequest(
                $method = 'Actions.getPageUrl', 
                $paramOverride = array(
                    'pageUrl' => $label,
                    'idSite' => $idSite,
                    'period' => $period,
                    'date' => $date,
                    'segment' => $segment
                ),
                $defaultRequest = array()
            );
            $visits = $request->getColumn('nb_visits');
            return isset($visits[0]) ? $visits[0] : 0;
        }));

@mattab commented on November 11th 2016

Great to hear @phoob you found a solution. Maybe you have seen our announcement about the Marketplace: https://piwik.org/blog/2016/11/premium-plugins-now-available-marketplace/ maybe you could be interested in selling paid plugins in the Marketplace.

This issue was closed on November 11th 2016
Powered by GitHub Issue Mirror