<?php

namespace OfficeBrain\Bundle\SocialMediaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * This class for base functionality related toSocial Media Functionalities. @author Office Brain - 4246
 *
 * @Route("/admin/{_locale}/social-media")
 */
class SocialMediaAdminController extends Controller
{
	private $responseArray;
	private $socialMediaManager;
	public function __construct(){
		$this->responseArray = null;
		$this->socialMediaManager = null;
		$this->request = null;
	}
	public function __desstruct(){
		unset($this->responseArray);
		unset($this->request);
	}
	public function initAction(){
		$this->responseArray = array();
		$this->request = $this->getRequest();
		$this->socialMediaManager = $this->container->get('office_brain_social_media_manager');
	}
    /**
     * @Route("/feeds",
     * name="officebrain_bundle_social_media_feeds_list")
     * @Template("OfficeBrainSocialMediaBundle:Admin:feedsList.html.twig")
     */
    public function feedsListAction()
    {
    	$this->initAction();
    	$form = $this->socialMediaManager->getForm();
    	
    	if ($this->request->isMethod('POST')) {
    		$form->handleRequest($this->request);
    		$formData = $form->getData();
    		$this->socialMediaManager->saveFormData($formData);
    	}
    	$formData = $this->socialMediaManager->getFormData('feed');
    	$this->responseArray['data'] = count($formData > 0)? $formData[0]: null;
    	$this->responseArray['form'] = $form->createView();
    	$this->responseArray['form_action'] = "";
    	return $this->responseArray;
    }
    
    /**
     * @Route("/links",
     * name="officebrain_bundle_social_media_links_list")
     * @Template("OfficeBrainSocialMediaBundle:Admin:linksList.html.twig")
     */
    public function linksListAction()
    {
    	$this->initAction();
    	$form = $this->socialMediaManager->getForm('link');
    	
    	if ($this->request->isMethod('POST')) {
    		$form->handleRequest($this->request);
    		$formData = $form->getData();
    		$this->socialMediaManager->saveFormData($formData);
    	}
    	$formData = $this->socialMediaManager->getFormData('link');
    	$this->responseArray['data'] = count($formData > 0)? $formData[0]: null;
    	$this->responseArray['form'] = $form->createView();
    	$this->responseArray['social_type'] = 'link';
    	$this->responseArray['form_action'] = "";
    	return $this->responseArray;
    }
}
