Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column values disappear on search (or when clicking to show "next" results) in datatables #10311

Closed
phoob opened this issue Jul 15, 2016 · 5 comments
Labels
answered For when a question was asked and we referred to forum or answered it.

Comments

@phoob
Copy link
Contributor

phoob commented Jul 15, 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:

1 - all is well

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

2 - clicked next results values missing

3 - Searched. Values missing:

3 - searched values missing

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

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
Copy link
Member

mattab commented Jul 19, 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
Copy link
Contributor Author

phoob commented Jul 19, 2016

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

@phoob
Copy link
Contributor Author

phoob commented Jul 20, 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
Copy link
Contributor Author

phoob commented Aug 11, 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
Copy link
Member

mattab commented Nov 11, 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.

@mattab mattab closed this as completed Nov 11, 2016
@mattab mattab added the answered For when a question was asked and we referred to forum or answered it. label Nov 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered For when a question was asked and we referred to forum or answered it.
Projects
None yet
Development

No branches or pull requests

2 participants