示例#1
0
 public static function convertToJsonResponse($profile, $data = null, $chartType = 'list')
 {
     $dataTable = new GvizDataTable(JRequest::getVar('tqx', ''));
     try {
         if ($chartType != 'list' && !GAnalyticsHelper::isPROMode()) {
             $chartType = 'list';
         }
         if ($data === null) {
             $message = array_shift(JFactory::getApplication()->getMessageQueue());
             if (!empty($message) && key_exists('message', $message)) {
                 $message = $message['message'];
             } else {
                 $message = print_r($message, true);
             }
             $dataTable->addError('invalid_request', JText::_('COM_GANALYTICS_JSONDFEED_VIEW_ERROR'), $message);
         } else {
             $dataTable->addWarning('other', $chartType);
             $headers = $data->getColumnHeaders();
             //print_R($headers);die;
             foreach ($headers as $header) {
                 $type = 'string';
                 if ($header->dataType == 'INTEGER' || $header->dataType == 'FLOAT' || $header->dataType == 'TIME') {
                     $type = 'number';
                 }
                 if (stripos($header->name, 'ga:date') !== false) {
                     $type = 'date';
                 }
                 $dataTable->addColumn($header->name, GAnalyticsHelper::translate($header->name), $type);
             }
             $dateRange = JRequest::getVar('dateRange', 'day');
             //$dateFormat = $params->get('dateFormat', '%d.%m.%Y');
             $counter = -1;
             foreach ($data->getRows() as $item) {
                 $counter++;
                 $rowId = -1;
                 foreach ($item as $index => $value) {
                     $header = $headers[$index];
                     $formatted = $value;
                     $property = '';
                     if (stripos($header->name, 'ga:date') !== false) {
                         $value = mktime(0, 0, 0, substr($value, 4, 2), substr($value, 6, 2), substr($value, 0, 4));
                         $formatted = strftime(GAnalyticsHelper::getComponentParameter('dateFormat', '%d.%m.%Y'), $value);
                         if ($dateRange == 'week' && strftime('%u', $value) > 1 && $counter > 0 && $counter < count($data->getRows()) - 1) {
                             break;
                         }
                         if ($dateRange == 'month' && strftime('%e', $value) > 1 && $counter > 0 && $counter < count($data->getRows()) - 1) {
                             break;
                         }
                     } else {
                         if (stripos($header->name, 'source') !== false && $value != '(direct)' && $chartType == 'list') {
                             $url = $value;
                             foreach ($headers as $index => $tmpHeader) {
                                 if (stripos($tmpHeader->name, 'referralPath') !== false && $item[$index] != '(not set)') {
                                     $url .= $item[$index];
                                 }
                             }
                             $formatted = '<a href="http://' . $url . '" target="_blank">' . GAnalyticsHelper::trim($value) . '</a>';
                         } else {
                             if (stripos($header->name, 'country') !== false && $chartType == 'list') {
                                 $flag = GAnalyticsHelper::convertCountryNameToISO($value);
                                 if (!empty($flag)) {
                                     $property = "style: 'background-image:url(\"" . JURI::root() . 'media/com_ganalytics/images/flags/' . strtolower($flag) . ".gif\"); background-repeat: no-repeat;background-position: 5px 4px;padding-left:30px;'";
                                 }
                             } else {
                                 if (stripos($header->name, 'pagePath') !== false && $chartType == 'list') {
                                     $formatted = '<a href="' . JRoute::_('index.php?option=com_ganalytics&view=page&tmpl=component&gaid=' . $profile->id . '&path=' . base64_encode($value)) . '">' . GAnalyticsHelper::trim($value) . '</a>';
                                 } else {
                                     $value = addslashes($value);
                                     $formatted = GAnalyticsHelper::trim($value);
                                 }
                             }
                         }
                     }
                     if ($rowId == -1) {
                         $rowId = $dataTable->newRow();
                     }
                     $dataTable->addCell($rowId, $header->name, JText::_($value), JText::_($formatted), $property);
                 }
             }
         }
     } catch (Exception $e) {
         $dataTable->addError('invalid_request', JText::_('COM_GANALYTICS_JSONDFEED_VIEW_ERROR'), $e->getMessage());
     }
     return $dataTable->toJsonResponse();
 }