From 15e8c0a4aca94475ef48daea82e726b78c3be22b Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Fri, 25 Apr 2014 09:41:17 +0200 Subject: [PATCH] fixed ratelimit bug --- Controller/DashboardController.php | 5 ++ Controller/GoogleAnalyticsController.php | 47 +++++++++++++++++-- Helper/GoogleAnalyticsHelper.php | 41 +++++++++++----- Helper/GoogleClientHelper.php | 11 +++++ Resources/translations/messages.en.yml | 2 + .../accountSelection.html.twig | 19 ++++++++ .../propertySelection.html.twig | 2 +- 7 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 Resources/views/GoogleAnalytics/accountSelection.html.twig diff --git a/Controller/DashboardController.php b/Controller/DashboardController.php index 5025d515..e967a96b 100755 --- a/Controller/DashboardController.php +++ b/Controller/DashboardController.php @@ -48,6 +48,11 @@ public function indexAction(Request $request) return $this->render('KunstmaanDashboardBundle:GoogleAnalytics:connect.html.twig', $params); } + // if propertyId not set + if (!$googleClientHelper->accountIsSet()) { + return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_AccountSelection')); + } + // if propertyId not set if (!$googleClientHelper->propertyIsSet()) { return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection')); diff --git a/Controller/GoogleAnalyticsController.php b/Controller/GoogleAnalyticsController.php index ec6b1210..c29091fb 100755 --- a/Controller/GoogleAnalyticsController.php +++ b/Controller/GoogleAnalyticsController.php @@ -72,12 +72,51 @@ public function setTokenAction(Request $request) $googleClientHelper->getClient()->authenticate($code); $googleClientHelper->saveToken($googleClientHelper->getClient()->getAccessToken()); - return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection')); + return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_AccountSelection')); } return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_widget_googleanalytics')); } + + + /** + * @Route("/selectAccount", name="KunstmaanDashboardBundle_AccountSelection") + * + * @param Request $request + * + * @return array + */ + public function accountSelectionAction(Request $request) + { + // get API client + try { + $googleClientHelper = $this->container->get('kunstmaan_dashboard.googleclienthelper'); + } catch (\Exception $e) { + // catch exception thrown by the googleClientHelper if one or more parameters in parameters.yml is not set + $currentRoute = $request->attributes->get('_route'); + $currentUrl = $this->get('router')->generate($currentRoute, array(), true); + $params['url'] = $currentUrl . 'analytics/setToken/'; + return $this->render('KunstmaanDashboardBundle:GoogleAnalytics:connect.html.twig', $params); + } + + if (null !== $request->request->get('accounts')) { + $googleClientHelper->saveAccountId($request->request->get('accounts')); + return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection')); + } + + /** @var GoogleClientHelper $googleClient */ + $googleClient = $googleClientHelper->getClient(); + $analyticsHelper = $this->container->get('kunstmaan_dashboard.googleanalyticshelper'); + $analyticsHelper->init($googleClientHelper); + $accounts = $analyticsHelper->getAccounts(); + + return $this->render( + 'KunstmaanDashboardBundle:GoogleAnalytics:accountSelection.html.twig', + array('accounts' => $accounts) + ); + } + /** * @Route("/selectWebsite", name="KunstmaanDashboardBundle_PropertySelection") * @@ -99,9 +138,7 @@ public function propertySelectionAction(Request $request) } if (null !== $request->request->get('properties')) { - $parts = explode("::", $request->request->get('properties')); - $googleClientHelper->saveAccountId($parts[1]); - $googleClientHelper->savePropertyId($parts[0]); + $googleClientHelper->savePropertyId($request->request->get('properties')); return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection')); } @@ -227,7 +264,7 @@ public function resetPropertyAction() { $em = $this->getDoctrine()->getManager(); $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->resetPropertyId(); - return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection')); + return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_AccountSelection')); } /** diff --git a/Helper/GoogleAnalyticsHelper.php b/Helper/GoogleAnalyticsHelper.php index 074424d9..35453674 100755 --- a/Helper/GoogleAnalyticsHelper.php +++ b/Helper/GoogleAnalyticsHelper.php @@ -35,31 +35,48 @@ public function getAnalytics() { } /** - * Get a list of all available properties for a Google Account + * Get a list of all available accounts for a Google Analytics Account * * @return array $data A list of all properties */ - public function getProperties() - { - $data = array(); + public function getAccounts() { $accounts = $this->analytics->management_accounts->listManagementAccounts()->getItems(); + $data = array(); foreach ($accounts as $account) { - $webproperties = $this->analytics->management_webproperties->listManagementWebproperties($account->getId()); - foreach ($webproperties->getItems() as $property) { - $data[] = array( - 'propertyId' => $property->getId(), - 'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')', - 'accountId' => $account->getId() - ); - } + $data[] = array( + 'accountId' => $account->getId(), + 'accountName' => $account->getName() + ); } + return $data; + } + /** + * Get a list of all available properties for an account + * + * @return array $data A list of all properties + */ + public function getProperties() + { + if (!$this->clientHelper->getAccountId()) {return false;} + + $webproperties = $this->analytics->management_webproperties->listManagementWebproperties($this->clientHelper->getAccountId()); + $data = array(); + + foreach ($webproperties->getItems() as $property) { + $data[] = array( + 'propertyId' => $property->getId(), + 'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')', + ); + } return $data; } public function getProfiles() { + if (!$this->clientHelper->getAccountId() || !$this->clientHelper->getPropertyId()) {return false;} + // get views $profiles = $this->analytics->management_profiles->listManagementProfiles( $this->clientHelper->getAccountId(), diff --git a/Helper/GoogleClientHelper.php b/Helper/GoogleClientHelper.php index a4ec7698..1d5378bd 100755 --- a/Helper/GoogleClientHelper.php +++ b/Helper/GoogleClientHelper.php @@ -189,6 +189,17 @@ public function tokenIsSet() return $this->getToken() && '' !== $this->getToken(); } + + /** + * Check if token is set + * + * @return boolean $result + */ + public function accountIsSet() + { + return $this->getAccountId() && '' !== $this->getAccountId(); + } + /** * Check if propertyId is set * diff --git a/Resources/translations/messages.en.yml b/Resources/translations/messages.en.yml index eb4d1df8..c78e96a6 100755 --- a/Resources/translations/messages.en.yml +++ b/Resources/translations/messages.en.yml @@ -29,6 +29,8 @@ dashboard: property: title: Select a website to track content: Select a domain registered in your Google Analytics account. + account: + title: Select an account reset: profile: Choose a new Analytics Profile (view) property: Track a new Analytics Website (property) diff --git a/Resources/views/GoogleAnalytics/accountSelection.html.twig b/Resources/views/GoogleAnalytics/accountSelection.html.twig new file mode 100644 index 00000000..4823df3b --- /dev/null +++ b/Resources/views/GoogleAnalytics/accountSelection.html.twig @@ -0,0 +1,19 @@ +{% extends 'KunstmaanAdminBundle:Default:layout.html.twig' %} + +{% block header %}{% endblock %} + +{% block content %} +
+

{{ 'dashboard.ga.setup.title' | trans }}

+

{{ 'dashboard.ga.setup.account.title' | trans }}

+ + +
+ +
+ +{% endblock %} diff --git a/Resources/views/GoogleAnalytics/propertySelection.html.twig b/Resources/views/GoogleAnalytics/propertySelection.html.twig index 78a4de76..2c6ba266 100755 --- a/Resources/views/GoogleAnalytics/propertySelection.html.twig +++ b/Resources/views/GoogleAnalytics/propertySelection.html.twig @@ -10,7 +10,7 @@