<commit_before><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CampaignChain\CoreBundle\Entity\Channel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityRepository;

class ChannelController extends Controller
{
    public function indexAction(){
        $repository = $this->getDoctrine()
            ->getRepository('CampaignChainCoreBundle:Channel');

        $query = $repository->createQueryBuilder('c')
            ->select('c, cm, b, l')
            ->join('c.channelModule', 'cm')
            ->join('c.locations', 'l')
            ->join('cm.bundle', 'b')
            ->orderBy('c.name', 'ASC')
            ->getQuery();

        $repository_channels = $query->getResult();

        if(!count($repository_channels)){
            $system = $this->get('campaignchain.core.system')->getActiveSystem();
            $this->get('session')->getFlashBag()->add(
                'warning',
                'No channels defined yet. To learn how to create one, please <a href="#" onclick="popupwindow(\''.
                $system->getDocsURL().'/user/get_started.html#connect-to-a-channel'.
                '\',\'\',900,600)">consult the documentation</a>.'
            );
        }

        return $this->render(
            'CampaignChainCoreBundle:Channel:index.html.twig',
            array(
                'page_title' => 'Channels',
                'repository_channels' => $repository_channels
            ));
    }

    public function newAction(Request $request)
    {
        $form = $this->createFormBuilder()
            ->add('module', 'entity', array(
                'label' => 'Channel',
                'class' => 'CampaignChainCoreBundle:ChannelModule',
                'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('m')
                            ->orderBy('m.displayName', 'ASC');
                    },
                'property' => 'displayName',
                'empty_value' => 'Select a channel',
                'empty_data' => null,
                'attr' => array(
                    'show_image' => true,
                )
            ))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            $channel = new Channel();

            $module = $form->getData()['module'];
            $wizard = $this->get('campaignchain.core.channel.wizard');
            $wizard->start($channel, $module);

            return $this->redirect(
                $this->generateUrl(
                    $module->getRoutes()['new']
                )
            );
        }

        return $this->render(
            'CampaignChainCoreBundle:Base:new.html.twig',
            array(
                'page_title' => 'Connect New Location',
                'form' => $form->createView(),
            ));
    }

    public function apiListActivitiesAction(Request $request, $id){
        $location = $this->getDoctrine()
            ->getRepository('CampaignChainCoreBundle:Location')
            ->find($id);

        if (!$location) {
            throw new \Exception(
                'No channel found for id '.$id
            );
        }

        // Get the modules of type "activity" that are related to the channel.
        $activityModules = $location->getChannel()->getChannelModule()->getActivityModules();

        $response = array();

        // TODO: Check whether there are any activity modules.
//        if($activityModules->count()){
            foreach($activityModules as $activityModule){
                $response[] = array(
                    'id' => $activityModule->getId(),
                    'display_name' => $activityModule->getDisplayName(),
                    'name' => $activityModule->getIdentifier(),
                );
            }
//        }

        $serializer = $this->get('campaignchain.core.serializer.default');

        return new Response($serializer->serialize($response, 'json'));
    }

    public function ctaTrackingAction(Request $request, $id){
        $channelService = $this->get('campaignchain.core.channel');
        $channel = $channelService->getChannel($id);

        return $this->render(
            'CampaignChainCoreBundle:Channel:cta_tracking.html.twig',
            array(
                'page_title' => 'Enable CTA Tracking',
                'channel' => $channel,
                'tracking_js_init' => $this->getParameter('campaignchain.tracking.js_init'),
                'tracking_js_route' => $this->getParameter('campaignchain.tracking.js_route'),
            ));
    }

    public function apiTestCtaTrackingAction(Request $request, $id){
        $response = array();

        $channel = $this->getDoctrine()
            ->getRepository('CampaignChainCoreBundle:Channel')
            ->find($id);

        if (!$channel) {
            throw new \Exception(
                'No channel found for id '.$id
            );
        }

        $channelService = $this->get('campaignchain.core.channel');
        $locations = $channelService->getRootLocations($channel);

        if(count($locations)){
            $trackingFileCode = 'src="'.$request->getSchemeAndHttpHost().$this->getParameter('campaignchain.tracking.js_route').'"';
            $trackingIdCode = 'cc(\''.$channel->getTrackingId().'\');';
            $trackingStatus = true;
<commit_msg>CampaignChain/campaignchain#56 Move installer and tracking.js to web/ folder with composer plugin<commit_after><?php
/*
 * This file is part of the CampaignChain package.
 *
 * (c) CampaignChain, Inc. <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CampaignChain\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CampaignChain\CoreBundle\Entity\Channel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityRepository;

class ChannelController extends Controller
{
    public function indexAction(){
        $repository = $this->getDoctrine()
            ->getRepository('CampaignChainCoreBundle:Channel');

        $query = $repository->createQueryBuilder('c')
            ->select('c, cm, b, l')
            ->join('c.channelModule', 'cm')
            ->join('c.locations', 'l')
            ->join('cm.bundle', 'b')
            ->orderBy('c.name', 'ASC')
            ->getQuery();

        $repository_channels = $query->getResult();

        if(!count($repository_channels)){
            $system = $this->get('campaignchain.core.system')->getActiveSystem();
            $this->get('session')->getFlashBag()->add(
                'warning',
                'No channels defined yet. To learn how to create one, please <a href="#" onclick="popupwindow(\''.
                $system->getDocsURL().'/user/get_started.html#connect-to-a-channel'.
                '\',\'\',900,600)">consult the documentation</a>.'
            );
        }

        return $this->render(
            'CampaignChainCoreBundle:Channel:index.html.twig',
            array(
                'page_title' => 'Channels',
                'repository_channels' => $repository_channels
            ));
    }

    public function newAction(Request $request)
    {
        $form = $this->createFormBuilder()
            ->add('module', 'entity', array(
                'label' => 'Channel',
                'class' => 'CampaignChainCoreBundle:ChannelModule',
                'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('m')
                            ->orderBy('m.displayName', 'ASC');
                    },
                'property' => 'displayName',
                'empty_value' => 'Select a channel',
                'empty_data' => null,
                'attr' => array(
                    'show_image' => true,
                )
            ))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isValid()) {
            $channel = new Channel();

            $module = $form->getData()['module'];
            $wizard = $this->get('campaignchain.core.channel.wizard');
            $wizard->start($channel, $module);

            return $this->redirect(
                $this->generateUrl(
                    $module->getRoutes()['new']
                )
            );
        }

        return $this->render(
            'CampaignChainCoreBundle:Base:new.html.twig',
            array(
                'page_title' => 'Connect New Location',
                'form' => $form->createView(),
            ));
    }

    public function apiListActivitiesAction(Request $request, $id){
        $location = $this->getDoctrine()
            ->getRepository('CampaignChainCoreBundle:Location')
            ->find($id);

        if (!$location) {
            throw new \Exception(
                'No channel found for id '.$id
            );
        }

        // Get the modules of type "activity" that are related to the channel.
        $activityModules = $location->getChannel()->getChannelModule()->getActivityModules();

        $response = array();

        // TODO: Check whether there are any activity modules.
//        if($activityModules->count()){
            foreach($activityModules as $activityModule){
                $response[] = array(
                    'id' => $activityModule->getId(),
                    'display_name' => $activityModule->getDisplayName(),
                    'name' => $activityModule->getIdentifier(),
                );
            }
//        }

        $serializer = $this->get('campaignchain.core.serializer.default');

        return new Response($serializer->serialize($response, 'json'));
    }

    public function ctaTrackingAction(Request $request, $id){
        $channelService = $this->get('campaignchain.core.channel');
        $channel = $channelService->getChannel($id);

        return $this->render(
            'CampaignChainCoreBundle:Channel:cta_tracking.html.twig',
            array(
                'page_title' => 'Enable CTA Tracking',
                'channel' => $channel,
                'tracking_js_init' => $this->getParameter('campaignchain.tracking.js_init'),
                'tracking_js_route' => $this->getParameter('campaignchain.tracking.js_route'),
            ));
    }

    public function apiTestCtaTrackingAction(Request $request, $id){
        $response = array();

        $channel = $this->getDoctrine()
            ->getRepository('CampaignChainCoreBundle:Channel')
            ->find($id);

        if (!$channel) {
            throw new \Exception(
                'No channel found for id '.$id
            );
        }

        $channelService = $this->get('campaignchain.core.channel');
        $locations = $channelService->getRootLocations($channel);

        if(count($locations)){
            $trackingFileCode = 'src="//'.$request->getHttpHost().$this->getParameter('campaignchain.tracking.js_route').'"';
            $trackingIdCode = 'cc(\''.$channel->getTrackingId().'\');';
            $trackingStatus = true;

{'fraction_non_alphanumeric': 0.14436037916340552, 'fraction_numerical': 0.0012174971736672753, 'mean_word_length': 1.9932326913066112, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 163, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1985067', 'n_tokens_mistral': 3061, 'n_tokens_neox': 2921, 'n_words': 568}