Skip to content

Commit

Permalink
Merge pull request #25 from Kunstmaan/feature/config-setup
Browse files Browse the repository at this point in the history
Feature/config setup
  • Loading branch information
jockri committed May 23, 2014
2 parents 826fcca + d38786b commit d66c4b8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 13 deletions.
49 changes: 49 additions & 0 deletions Command/GoogleAnalyticsConfigFlushCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Kunstmaan\DashboardBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;

class GoogleAnalyticsConfigFlushCommand extends ContainerAwareCommand
{

protected function configure()
{
$this
->setName('kuma:dashboard:widget:googleanalytics:config:flush')
->setDescription('Flush configs')
->addOption(
'config',
null,
InputOption::VALUE_OPTIONAL,
'Specify to only flush one config',
false
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$configRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
$configId = $input->getOption('config');
$configs = array();

try {
if ($configId) {
$configs[] = $configRepository->find($configId);
} else {
$configs = $configRepository->findAll();
}

foreach ($configs as $config) {
$em->remove($config);
}
$em->flush();
$output->writeln('<fg=green>Config flushed</fg=green>');
} catch (\Exception $e) {
$output->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
}
}
}
21 changes: 12 additions & 9 deletions Command/GoogleAnalyticsDataCollectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,19 @@ private function getAllOverviews()
$configRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
$overviewRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
$segmentRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment');
$config = $configRepository->findFirst();
$configs = $configRepository->findAll();

// add overviews if none exist yet
if (sizeof($config->getOverviews()) == 0) {
$overviewRepository->addOverviews($config);
}
foreach ($configs as $config) {
// add overviews if none exist yet
if (sizeof($config->getOverviews()) == 0) {
$overviewRepository->addOverviews($config);
}

// init all the segments for this config
$segments = $config->getSegments();
foreach ($segments as $segment) {
$segmentRepository->initSegment($segment);
// init all the segments for this config
$segments = $config->getSegments();
foreach ($segments as $segment) {
$segmentRepository->initSegment($segment);
}
}

// get all overviews
Expand All @@ -232,6 +234,7 @@ public function updateData($overviews)

// get data per overview
foreach ($overviews as $overview) {
$configHelper->init($overview->getConfig()->getId());
/** @var AnalyticsOverview $overview */
$this->output->writeln('Fetching data for overview "<fg=green>' . $overview->getTitle() . '</fg=green>"');

Expand Down
16 changes: 16 additions & 0 deletions Controller/GoogleAnalyticsAJAXController.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ public function saveConfigAction(Request $request) {
return new JsonResponse();
}

/**
* @Route("/config/remove", name="kunstmaan_dashboard_ajax_config_remove")
*/
public function removeConfigAction(Request $request) {
// get params
$configId = $request->query->get('configId');

// edit the config
$em = $this->getDoctrine()->getManager();
$config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId);
$em->remove($config);
$em->flush();

return new JsonResponse();
}

/**
* @Route("/config/get", name="kunstmaan_dashboard_ajax_config_get")
*/
Expand Down
6 changes: 5 additions & 1 deletion Helper/Google/Analytics/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ private function getToken($configId=false)
if (!$this->token || $configId) {
/** @var AnalyticsConfigRepository $analyticsConfigRepository */
$analyticsConfigRepository = $this->em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
$this->token = $analyticsConfigRepository->findFirst()->getToken();
if ($configId) {
$this->token = $analyticsConfigRepository->find($configId)->getToken();
} else {
$this->token = $analyticsConfigRepository->findFirst()->getToken();
}
}

return $this->token;
Expand Down
8 changes: 5 additions & 3 deletions Repository/AnalyticsConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ class AnalyticsConfigRepository extends EntityRepository
*
* @return AnalyticsConfig $config
*/
public function findFirst() {
public function findFirst($createNew = true) {
// Backwards compatibility: select the first config, still used in the dashboard, specified config ids are set in the dashboard collection bundle
$em = $this->getEntityManager();
$query = $em->createQuery( 'SELECT c FROM KunstmaanDashboardBundle:AnalyticsConfig c' );
$result = $query->getResult();
// if no configs exist, create a new one
if (!$result) {
if (!$result && $createNew) {
return $this->createConfig();
} else {
} else if ($result) {
return $result[0];
} else {
return false;
}
}

Expand Down

0 comments on commit d66c4b8

Please sign in to comment.