From 1fc507d8d27160132cfa4db1bea4d8fa227becaa Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Mon, 2 Sep 2013 20:18:29 +0200 Subject: [PATCH] // Added configuration checks to the Google Analytics modules // Implemented the first real data in the dashboard --- modules/dashtrends/dashtrends.php | 52 ++++++++++++++++++++++--------- modules/gapi/gapi.php | 34 ++++++++++++++------ 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/modules/dashtrends/dashtrends.php b/modules/dashtrends/dashtrends.php index 124281d73..f7ad51266 100644 --- a/modules/dashtrends/dashtrends.php +++ b/modules/dashtrends/dashtrends.php @@ -54,23 +54,45 @@ class Dashtrends extends Module public function hookDashboardDatas($params) { + $gapi = Module::isInstalled('gapi') ? Module::getInstanceByName('gapi') : false; + if (Validate::isLoadedObject($gapi)) + { + $visits_score = 0; + if ($result = $gapi->requestReportData('', 'ga:visits', $this->context->employee->stats_date_from, $this->context->employee->stats_date_to, null, null, 1, 1)) + $visits_score = $result[0]['metrics']['visits']; + } + else + { + $visits_score = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(c.`id_connections`) + FROM `'._DB_PREFIX_.'connections` c + WHERE c.`date_add` BETWEEN '.ModuleGraph::getDateBetween().' + '.Shop::addSqlRestriction(false, 'c')); + } + $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + SELECT COUNT(o.`id_order`) as orders_score, SUM(o.`total_paid_tax_excl` / o.conversion_rate) as sales_score + FROM `'._DB_PREFIX_.'orders` o + WHERE o.`invoice_date` BETWEEN '.ModuleGraph::getDateBetween().' + '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o')); + extract($row); + return array( 'data_value' => array( - 'sales_score' => Tools::displayPrice(151.365), - 'orders_score' => 120, - 'cart_value_score' => Tools::displayPrice(35), - 'visits_score' => 12, - 'convertion_rate_score' => 4, - 'net_profits_score' => Tools::displayPrice(42), - ), + 'sales_score' => Tools::displayPrice((float)$sales_score), + 'orders_score' => $orders_score, + 'cart_value_score' => Tools::displayPrice($orders_score ? $sales_score / $orders_score : 0), + 'visits_score' => $visits_score, + 'convertion_rate_score' => $visits_score ? 100 * $orders_score / $visits_score : 0, + 'net_profits_score' => Tools::displayPrice(0), + ), 'data_trends' => array( - 'sales_score_trends' => array('way' => 'up', 'value' => 0.66), - 'orders_score_trends' => array('way' => 'down', 'value' => 0.66), - 'cart_value_score_trends' => array('way' => 'up', 'value' => 0.66), - 'visits_score_trends' => array('way' => 'down', 'value' => 0.66), - 'convertion_rate_score_trends' => array('way' => 'up', 'value' => 0.66), - 'net_profits_score_trends' => array('way' => 'up', 'value' => 0.66) - ) - ); + 'sales_score_trends' => array('way' => 'up', 'value' => 0.42), + 'orders_score_trends' => array('way' => 'down', 'value' => 0.42), + 'cart_value_score_trends' => array('way' => 'up', 'value' => 0.42), + 'visits_score_trends' => array('way' => 'down', 'value' => 0.42), + 'convertion_rate_score_trends' => array('way' => 'up', 'value' => 0.42), + 'net_profits_score_trends' => array('way' => 'up', 'value' => 0.42) + ) + ); } } \ No newline at end of file diff --git a/modules/gapi/gapi.php b/modules/gapi/gapi.php index 75dfceab6..50c07e5ec 100644 --- a/modules/gapi/gapi.php +++ b/modules/gapi/gapi.php @@ -44,8 +44,28 @@ class Gapi extends Module public function getContent() { + $html = ''; + + // Check configuration + $allow_url_fopen = ini_get('allow_url_fopen'); + $openssl = extension_loaded('openssl'); + $curl = extension_loaded('curl'); + $ping = (($allow_url_fopen || $curl) && $openssl && Tools::file_get_contents('https://www.google.com/')); + $online = (in_array(Tools::getRemoteAddr(), array('127.0.0.1', '::1')) ? false : true); + + if (!$ping || !$online) + { + $html .= $this->displayError(''); + } + // You can switch to the 1.3 API by replacing the following function call by $this->api_1_3_getContent() - return $this->api_3_0_getContent(); + return $html.$this->api_3_0_getContent(); } public function requestReportData($dimensions, $metrics, $date_from, $date_to, $sort = null, $filters = null, $start = 1, $limit = 30) @@ -207,7 +227,6 @@ class Gapi extends Module $fields_options = array( 'general' => array( 'title' => $this->l('Google Analytics API v3.0'), - 'icon' => $this->_path.'logo.png', 'fields' => $fields = array( 'PS_GAPI30_CLIENT_ID' => array( 'title' => $this->l('Client ID'), @@ -219,8 +238,7 @@ class Gapi extends Module ), 'PS_GAPI30_PROFILE' => array( 'title' => $this->l('Profile'), - 'type' => 'text', - 'desc' => sprintf($this->l('You can find your profile ID in the address bar of your browser while accessing Analytics report: %s.'), ''.$this->l('see screenshot').'') + 'type' => 'text' ) ), 'submit' => array('title' => $this->l('Save and Authenticate')), @@ -345,26 +363,22 @@ class Gapi extends Module $fields_options = array( 'general' => array( 'title' => $this->l('Google Analytics API v1.3'), - 'icon' => $this->_path.'logo.png', 'fields' => $fields = array( 'PS_GAPI13_EMAIL' => array( 'title' => $this->l('Email'), - 'validation' => 'isEmail', 'type' => 'text' ), 'PS_GAPI13_PASSWORD' => array( 'title' => $this->l('Password'), - 'validation' => 'isNothing', 'type' => 'password' ), 'PS_GAPI13_PROFILE' => array( 'title' => $this->l('Profile'), - 'validation' => 'isUnsignedInt', 'type' => 'text', - 'desc' => sprintf($this->l('You can find your profile ID in the address bar of your browser while accessing Analytics report: %s.'), ''.$this->l('see screenshot').'') + 'desc' => $this->l('You can find your profile ID in the address bar of your browser while accessing Analytics report.') ) ), - 'submit' => array('title' => $this->l('Authenticate'), 'class' => 'button'), + 'submit' => array('title' => $this->l('Save and Authenticate')), ) );