<?php
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\User;
use App\Entity\Server;
use App\Entity\Service;
use App\Entity\Module;
use App\Entity\EshopLink;
use App\Entity\Feed;
use App\Entity\Language;
use App\Entity\Department;
use App\Entity\Product;
use App\Entity\ShoppingWindow;
use Symfony\Contracts\Translation\TranslatorInterface;
class MarketplaceController extends DefaultController
{
protected ManagerRegistry $doctrine;
protected TranslatorInterface $translator;
public function __construct(ManagerRegistry $doctrine,
TranslatorInterface $translator)
{
$this->doctrine = $doctrine;
$this->translator = $translator;
}
/**
* @Route("/marketplace", name="marketplace")
*/
public function marketplaceAction(Request $request)
{
// we load session data
parent::init($request);
$session = $request->getSession();
// we load entity managers
$em = $this->doctrine->getManager();
$hostServer = $em->getRepository(Server::class)->getServerFromHost($_SERVER['HTTP_HOST']);
//$hostServer->getParentSectionId()
//$treeObject = $em->getRepository(Department::class)->getTree();
$treeObject = null;
$rootDepartments = $em->getRepository(Department::class)->getRootNodes();
// we get root departments
if (!empty($hostServer->getParentSectionId()) && $hostServer->getParentSectionId() > 0) {
$customRootDepartment = $em->getRepository(Department::class)->getDepartment($hostServer->getParentSectionId());
$treeObject->createRoot($customRootDepartment);
$tree = $treeObject->fetchTree();
$rootDepartments = $tree->getRootNodes();
} else {
$rootDepartments = $em->getRepository(Department::class)->getRootNodes();
}
// we add sub departments
$subDepartments = array();
foreach($rootDepartments as $dep)
{
$depId = $dep->getDepartmentId();
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
$subDeps = $em->getRepository(Server::class)->getSubDepartments($depId);
$subDepartments[$depId] = $subDeps;
}
$service = $em->getRepository(Service::class)->getService(4);
/* we load list of departments for marketplace service */
$departmentArray[] = $em->getRepository(Server::class)->getDepartment(4);
$departmentArray[] = $em->getRepository(Server::class)->getDepartment(9);
/* we load list of shopping windows */
$shoppingWindowList = $em->getRepository(ShoppingWindow::class)->getShoppingWindowList($departmentArray, 0, 6);
foreach($shoppingWindowList as $shoppingWindow)
{
//print('<hr>qqw shoppingWindow: ');
//\Doctrine\Common\Util\Debug::dump($shoppingWindow);
}
//print('<hr>qqw shoppingWindowList: ');
//\Doctrine\Common\Util\Debug::dump($shoppingWindowList);
/* we render data */
return $this->render('marketplace.html.twig',
array('headerData' => $this -> getPageHeader($request),
'rootDepartments' => $rootDepartments,
'subDepartments' => $subDepartments,
'moduleList' => $service->getModules(),
'service' => $service,
'shoppingWindowList' => $shoppingWindowList,
)
);
}
/**
* @Route("/marketplace/department/{departmentId}", name="department")
*/
public function departmentAction(Request $request, $departmentId)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
/* we get root departments */
$department = $em->getRepository(Server::class)->getDepartment($departmentId);
$department->setLocale($session->get('lang')->getLangKey());
$em->refresh($department);
$departmentPath = $em->getRepository(Department::class)->getPath($department);
foreach ($departmentPath as $dep) {
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
}
//print('<br>departmentPath: ');
//\Doctrine\Common\Util\Debug::dump($departmentPath);
$childDepartments = $department->getChildren();
//$childDepartments = $em->getRepository(Department::class)->getRootNodes();
/* we get languages */
$langCollection = $em->getRepository(Language::class)->getLanguageList();
/* we get e-shop links */
$eshopLinkList = $em->getRepository(EshopLink::class)->getEshopLinkListByDepartment($departmentId);
//$eshopLink = $em->getRepository(EshopLink::class)->getEshopLink($request->query->get('setCurrent'));
// we get root departments
$rootDepartments = $em->getRepository(Department::class)->getRootNodes();
// we add sub departments
$subDepartments = array();
foreach($childDepartments as $dep)
{
$depId = $dep->getDepartmentId();
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
$subDeps = $em->getRepository(Server::class)->getSubDepartments($depId);
$subDepartments[$depId] = $subDeps;
}
$departmentArray = array();
$departmentArray[] = $department;
/* we get product list */
$productPerPage = 20;
$page = $request->query->get('page');
$firstRecord = ($page * $productPerPage) - $productPerPage;
if($firstRecord < 1) {
$firstRecord = 0;
}
// print('<br>DepartmentArray: ');
// \Doctrine\Common\Util\Debug::dump($departmentArray);
$productList = $em->getRepository(Product::class)->getProductListByDepartment($departmentArray, $firstRecord, $productPerPage);
$productCount = $em->getRepository(Product::class)->getProductCountByDepartment($departmentArray);
// print('<br>productList:');
// \Doctrine\Common\Util\Debug::dump($productList);
$productIds = array();
foreach($productList as $product) {
$pId = $product->getProductId();
$product->setLocale($session->get('lang')->getLangKey());
$em->refresh($product);
$productIds[] = $pId;
/* we setup product names - if empty then we load multilingual values */
$productName = '';
if(!empty($product->getProductName()) && $product->getProductName() != '') {
$productName = $product->getProductName();
} else {
//we try to get the product name from multilangual fields
foreach($langCollection as $lang) {
$product->setLocale($lang->getLangKey());
$em->refresh($product);
if(!empty($product->getProductName()) && $product->getProductName() != '') {
$productName = $product->getProductName();
}
}
}
//print('<br>qqw pname 1: '.$productName);
$product->setProductName($productName);
$em->flush();
}
// print('<br>productIds: ');
// \Doctrine\Common\Util\Debug::dump($productIds);
/* we load prices */
$productPriceList = $em->getRepository(Product::class)->getProductPriceList($productIds);
//$defaultCurrency = $em->getRepository('AppBundle:Currency')->getCurrency($eshop->getPreferredCurrencyId());
//$targetCurrency = $session->get('eshopCurrency');
/* we load currency rated prices */
//$productPriceList = $dem->getRepository(Product::class)->getCurrencyRatedProductPriceList($productPriceList, $defaultCurrency, $targetCurrency);
/* we get price(s) to display */
// print('<br>productPriceList: ');
// \Doctrine\Common\Util\Debug::dump($productPriceList);
$displayPrice = null;
foreach($productPriceList as $price) {
// print('<hr>productPrice: ');
// \Doctrine\Common\Util\Debug::dump($price);
if($price->getProductId() == $product->getProductId()) {
// we get default price level
if($price->getPriceLevelId() == 1) {
//$displayPrice = round($price->getBruttoValue(),$eshop->getRoundPrice());
$displayPrice = round($price->getNettoValue(),2);
}
}
}
/* we render data */
return $this->render('marketplaceDepartment.html.twig',
array('headerData' => $this -> getPageHeader($request),
'childDepartments' => $childDepartments,
'department' => $department,
'departmentPath' => $departmentPath,
'eshopLinkList' => $eshopLinkList,
'productList' => $productList,
'productPriceList' => $productPriceList,
'productCount' => $productCount,
'subDepartments' => $subDepartments,
)
);
}
/**
* @Route("/marketplace/product/{productId}", name="marketplaceProduct")
*/
public function marketplaceProductAction(Request $request, $productId)
{
/* we load session data */
parent::init($request);
/* we load entity managers */
$em = $this->doctrine->getManager();
/* we get product */
$product = $em->getRepository(Product::class)->getProduct($productId);
if(empty($product)) {
$this->addFlash('error', 'Product with this ID does not exist.');
return $this->redirectToRoute('marketplace');
}
if(!empty($request->get('redirect_to_eshop'))) {
print('<br>redirect_to_eshop ...');
$clickedCount = (int)$product->getClickedCount();
$product->setClickedCount($clickedCount + 1);
$em->persist($product);
$em->flush();
return $this->redirect($product->getProductLink());
}
/*
$departmentPath = $em->getRepository(Department::class)->getPath($department);
$childDepartments = $department->getChildren();
//$childDepartments = $em->getRepository(Department::class)->getRootNodes();
*/
/*
print('<br>qqw eshopLinkList: ');
\Doctrine\Common\Util\Debug::dump($eshopLinkList);
*/
/* we load prices */
$productIds = [];
$productIds[] = $product->getProductId();
$productPriceList = $em->getRepository(Product::class)->getProductPriceList($productIds);
$displayPrice = null;
foreach($productPriceList as $price) {
// print('<hr>productPrice: ');
// \Doctrine\Common\Util\Debug::dump($price);
if($price->getProductId() == $product->getProductId()) {
// we get default price level
if($price->getPriceLevelId() == 1) {
//$displayPrice = round($price->getBruttoValue(),$eshop->getRoundPrice());
$displayPrice = round($price->getNettoValue(),2);
}
}
}
// increments product view count
$viewedCount = (int)$product->getViewedCount();
$product->setViewedCount($viewedCount + 1);
$em->persist($product);
$em->flush();
/* we render data */
return $this->render('marketplaceProduct.html.twig',
array('headerData' => $this -> getPageHeader($request),
'product' => $product,
'productPriceList' => $productPriceList,
)
);
}
/**
* @Route("/marketplaceAdmin", name="marketplaceAdmin")
*/
public function marketplaceAdminAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
$dem = $this->doctrine->getManager('dynamic_em');
/* we get current user */
$currentUserId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($currentUserId);
/* eshopLink module */
$moduleId = 19;
$module = $em->getRepository(Module::class)->getModule($moduleId);
$eshopLinkList = null;
$isEshopLinkModule = $em->getRepository(User::class)->userHasModule($user, $module);
if($isEshopLinkModule) {
/* setting current eshopLink */
if(!empty($request->query->get('setCurrent'))) {
$eshopLink = $em->getRepository(EshopLink::class)->getEshopLink($request->query->get('setCurrent'));
$em->getRepository(EshopLink::class)->setCurrentEshopLink($currentUserId, $request->query->get('setCurrent'));
$session->set('eshopLink', $eshopLink);
$this->addFlash('notice', 'The eshop link '.$eshopLink->getEshopLinkName().' was set as current one.');
return $this->redirectToRoute('marketplaceAdmin');
}
/* we get current e-shop link */
$currentEshopLink = $em->getRepository(EshopLink::class)->getCurrentEshopLink($currentUserId);
/*
print('<br>qqw currentEshop: ');
\Doctrine\Common\Util\Debug::dump($currentEshop->getEshopId());
*/
/* we load list of eshop links for logged user */
$eshopLinkList = $em->getRepository(EshopLink::class)->getEshopLinkListByUser($currentUserId);
}
/* feed module */
$moduleId = 20;
$module = $em->getRepository(Module::class)->getModule($moduleId);
$feedList = null;
$isFeedModule = $em->getRepository(User::class)->userHasModule($user, $module);
if($isFeedModule) {
/* setting current feed */
if(!empty($request->query->get('setCurrent'))) {
$feed = $em->getRepository(Feed::class)->getFeed($request->query->get('setCurrent'));
$em->getRepository(Feed::class)->setCurrentFeed($currentUserId, $request->query->get('setCurrent'));
$session->set('feed', $feed);
$this->addFlash('notice', 'The feed '.$feed->getFeedName().' was set as current one.');
return $this->redirectToRoute('marketplaceAdmin');
}
/* we get current feed */
$currentFeed = $em->getRepository(Feed::class)->getCurrentFeed($currentUserId);
/*
print('<br>qqw currentEshop: ');
\Doctrine\Common\Util\Debug::dump($currentEshop->getEshopId());
*/
/* we load list of feeds for logged user */
$feedList = $em->getRepository(Feed::class)->getFeedListByUser($currentUserId);
}
/* we load list of modules for marketplace service */
$service = $em->getRepository(Service::class)->getService(4);
/* we render data */
return $this->render('marketplaceAdmin.html.twig',
array('headerData' => $this -> getPageHeader($request),
'moduleList' => $service->getModules(),
'userModuleList' => $user->getModules(),
'eshopLinkList' => $eshopLinkList,
'feedList' => $feedList,
'isEshopLinkModule' => $isEshopLinkModule,
'isFeedModule' => $isFeedModule,
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/eshopLinkAdmin", name="eshopLinkAdmin")
*/
public function eshopLinkAdminAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
$dem = $this->doctrine->getManager('dynamic_em');
/* we get current user */
$currentUserId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($currentUserId);
/* setting current eshopLink */
if(!empty($request->query->get('setCurrent'))) {
$eshopLink = $em->getRepository(EshopLink::class)->getEshopLink($request->query->get('setCurrent'));
$em->getRepository(EshopLink::class)->setCurrentEshopLink($currentUserId, $request->query->get('setCurrent'));
$session->set('eshopLink', $eshopLink);
$this->addFlash('notice', 'The eshop link '.$eshopLink->getEshopLinkName().' was set as current one.');
return $this->redirectToRoute('eshopLinkAdmin');
}
/* we get current e-shop link */
$currentEshopLink = $em->getRepository(EshopLink::class)->getCurrentEshopLink($currentUserId);
/*
print('<br>qqw currentEshop: ');
\Doctrine\Common\Util\Debug::dump($currentEshop->getEshopId());
*/
/* we load list of eshop links for logged user */
$eshopLinkList = $em->getRepository(EshopLink::class)->getEshopLinkListByUser($currentUserId);
/* we render data */
return $this->render('eshopLinkAdmin.html.twig',
array('headerData' => $this -> getPageHeader($request),
'eshopLinkList' => $eshopLinkList,
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/addproducts", name="eshopLinkNew")
*/
public function eshopLinkNewAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
$em = $this->doctrine->getManager();
$departmentCollection = $em->getRepository(Server::class)->getDepartmentList();
/* we get current user */
if (!empty($session->get('user'))) {
$currentUserId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($currentUserId);
} else {
$user = null;
}
//$user = $em->getRepository(User::class)->getUser($currentUserId);
/* we build login form */
$eshopLink = new EshopLink;
$formBuilder = $this->createFormBuilder($eshopLink);
$formBuilder->add('eshopLinkName', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('module.eshopLink_name'),
'attr' => array('class' => 'text_form', 'size' => 62),
'label_attr' => array('class' => 'form_field_label'),
));
$formBuilder->add('eshopLinkNameHelp', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => '',
'attr' => array('class' => 'text_form_help', 'size' => 62, 'readonly' => true, 'value' => $this->translator->trans('module.eshopLink_name_help'), 'title' => $this->translator->trans('module.eshopLink_name_help')),
'label_attr' => array('class' => 'form_field_label_help'),
));
$formBuilder->add('eshopLinkUrl', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('system.url'),
'attr' => array('class' => 'text_form', 'size' => 62, 'title'=>'text help', 'help_block'=>'text help block'),
'label_attr' => array('class' => 'form_field_label'),
));
$formBuilder->add('eshopLinkUrlHelp', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => '',
'attr' => array('class' => 'text_form_help', 'size' => 90, 'readonly' => true, 'value' => $this->translator->trans('module.eshopLink_url_help'), 'title' => $this->translator->trans('module.eshopLink_url_help')),
'label_attr' => array('class' => 'form_field_label_help'),
));
$formBuilder->add('eshopLinkDescription', TextareaType::class, array(
'required' => false,
'label' => $this->translator->trans('module.eshopLink_description'),
'attr' => array('class' => 'textarea_form', 'cols' => 60, 'rows' => 5),
'label_attr' => array('class' => 'form_textarea_label'),
));
$formBuilder->add('eshopLinkDescriptionHelp', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => '',
'attr' => array('class' => 'text_form_help', 'size' => 90, 'readonly' => true, 'value' => $this->translator->trans('module.eshopLink_description_help'), 'title' => $this->translator->trans('module.eshopLink_description_help')),
'label_attr' => array('class' => 'form_field_label_help'),
));
/* we add department list */
$departments = array();
//$departments['Root'] = 0;
foreach($departmentCollection as $dep)
{
$depId = $dep->getDepartmentId();
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
//print('<br>qqw department: '.$departmentId);
$departments[$dep->getDepartmentName()] = $depId;
}
$formBuilder->add('department1', ChoiceType::class, array(
'choices' => $departments,
'attr' => array('class' => 'selector'),
'mapped' => false,
'label' => $this->translator->trans('marketplace.parent_department'),
'label_attr' => array('class' => 'form_field_label')
));
$formBuilder->add('departmentHelp', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => '',
'attr' => array('class' => 'text_form_help', 'size' => 90, 'readonly' => true, 'value' => $this->translator->trans('module.eshopLink_department_help'), 'title' => $this->translator->trans('module.eshopLink_department_help')),
'label_attr' => array('class' => 'form_field_label_help'),
));
$formBuilder->add('feedUrl', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => $this->translator->trans('marketplace.feed_url'),
'attr' => array('class' => 'text_form', 'size' => 62),
'label_attr' => array('class' => 'form_field_label')
));
$formBuilder->add('feedUrlHelp', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => '',
'attr' => array('class' => 'text_form_help', 'size' => 90, 'readonly' => true, 'value' => $this->translator->trans('module.eshopLink_feed_help'), 'title' => $this->translator->trans('module.eshopLink_feed_help')),
'label_attr' => array('class' => 'form_field_label_help'),
));
$formBuilder->add('feedNotes', TextareaType::class, array(
'required' => false,
'mapped' => false,
'label' => $this->translator->trans('system.notes'),
'attr' => array('class' => 'textarea_form', 'cols' => 60, 'rows' => 2),
'label_attr' => array('class' => 'form_textarea_label'),
));
$formBuilder->add('feedNotesHelp', TextType::class, array(
'required' => false,
'mapped' => false,
'label' => '',
'attr' => array('class' => 'text_form_help', 'size' => 90, 'readonly' => true, 'value' => $this->translator->trans('module.eshopLink_feed_notes_help'), 'title' => $this->translator->trans('module.eshopLink_feed_notes_help')),
'label_attr' => array('class' => 'form_field_label_help'),
));
$formBuilder->add('save', SubmitType::class, array('label' => $this->translator->trans('form.button.save'),
'attr' => array('class' => 'butt_big')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
if ($form->isValid()) {
$formData = $form->getData();
$eshopLink->setEshopLinkName($formData->getEshopLinkName());
$eshopLink->setEshopLinkUrl($formData->getEshopLinkUrl());
/* we add department */
if($form['department1']->getData() !== null and $form['department1']->getData() !== 0) {
$department1 = $em->getRepository(Server::class)->getDepartment($form['department1']->getData());
if(!empty($department1)) {
$eshopLink->addDepartment($department1);
}
}
/* we create feed */
$feed = new Feed;
$feed->setFeedName($formData->getEshopLinkName()." - Feed");
$feed->setFeedUrl($form['feedUrl']->getData());
$feed->setNotes($form['feedNotes']->getData());
/* we add department */
if($form['department1']->getData() !== null and $form['department1']->getData() !== 0) {
$department1 = $em->getRepository(Server::class)->getDepartment($form['department1']->getData());
if(!empty($department1)) {
$feed->addDepartment($department1);
}
}
/* we add server language */
$eshopLink->addLanguage($session->get('lang'));
if(!empty($session->get('user'))) {
/* user is logged in - we persist the new eshopLink */
$eshopLink->setUserId($session->get('user')->getUserId());
$feed->setUserId($session->get('user')->getUserId());
/* we set this eshop link as current one */
$em->getRepository(EshopLink::class)->unsetCurrentEshopLink($session->get('user')->getUserId());
$eshopLink->setEshopLinkCurrent(true);
/* we persist and save eshopLink */
$em->persist($feed);
$em->persist($eshopLink);
// print('<br>qqw Dep: ');
// print_r($form['department1']->getData());
if($form['department1']->getData() !== null and $form['department1']->getData() !== 0) {
$em->persist($department1);
}
$em->flush();
$this->addFlash('notice', 'New Eshop Link and Feed were created.');
return $this->redirectToRoute('eshopLinkConfig');
} else {
/* we store new eshopLink in session and redirect to new account form */
$session->set('eshopLink', $eshopLink);
$session->set('feed', $feed);
$this->addFlash('notice', 'Please add your user account info.');
return $this->redirectToRoute('newaccount', array('addModule' => 19, 'addModule2' => 20));
//$this->redirectToRoute('eshopHomeContact', array('eshopId' => $eshopId))
}
}
}
/* we render data */
return $this->render('eshopLinkNew.html.twig',
array( 'form' => $formBuilder->getForm()->createView(),
'headerData' => $this -> getPageHeader($request),
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/eshopLinkConfig", name="eshopLinkConfig")
*/
public function eshopLinkConfigAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
$dem = $this->doctrine->getManager('dynamic_em');
/* we get e-shop link */
$userId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($userId);
/* we get current e-shop link */
$eshopLink = $em->getRepository(EshopLink::class)->getCurrentEshopLink($userId);
/* we get current feed */
$feed = $em->getRepository(Feed::class)->getCurrentFeed($userId);
/* we get languages */
$langCollection = $em->getRepository(Language::class)->getLanguageList();
/* we get departments */
$departmentCollection = $em->getRepository(Server::class)->getDepartmentList();
$userDirs = $this->getUserFolderPaths($request);
/* we build edit form */
$formBuilder = $this->createFormBuilder($eshopLink);
/* e-shop link name - multilangual fields */
foreach($eshopLink->getLanguages() as $lang) {
$fieldLabel = $this->translator->trans('module.eshopLink_name').' ['.$lang->getLangKey().']';
$eshopLink->setLocale($lang->getLangKey());
$em->refresh($eshopLink);
$fieldValue = $eshopLink->getEshopLinkName();
$formBuilder->add('eshopLinkName_'.$lang->getLangKey(), TextType::class, array(
'required' => false,
'label' => $fieldLabel,
'attr' => array('class' => 'text_form', 'size' => 35, 'value' => $fieldValue),
'label_attr' => array('class' => 'form_field_label'),
'mapped' => false,
));
}
/* e-shop link description - multilangual fields */
foreach($eshopLink->getLanguages() as $lang) {
$fieldLabel = $this->translator->trans('module.eshopLink_description').' ['.$lang->getLangKey().']';
$eshopLink->setLocale($lang->getLangKey());
$em->refresh($eshopLink);
$fieldValue = $eshopLink->getEshopLinkDescription();
$formBuilder->add('eshopLinkDescription_'.$lang->getLangKey(), TextareaType::class, array(
'required' => false,
'label' => $fieldLabel,
'attr' => array('class' => 'textarea_form', 'cols' => 69, 'rows' => 7, 'value' => $fieldValue),
'label_attr' => array('class' => 'form_textarea_label'),
'data' => $fieldValue,
'mapped' => false,
));
}
$formBuilder->add('eshopLinkUrl', TextType::class, array(
'required' => false,
'label' => $this->translator->trans('system.url'),
'attr' => array('class' => 'text_form', 'size' => 50, 'value' => $eshopLink->getEshopLinkUrl()),
'label_attr' => array('class' => 'form_field_label')
));
$formBuilder->add('logoUrl', TextType::class, array(
'required' => false,
'label' => $this->translator->trans('system.logo_url'),
'attr' => array('class' => 'text_form', 'size' => 50, 'value' => $eshopLink->getLogoUrl()),
'label_attr' => array('class' => 'form_field_label')
));
/* we add language list */
$languages = array();
foreach($langCollection as $lang)
{
$langId = $lang->getLangId();
$languages[$lang->getLangName()] = $lang->getLangId();
}
$selectedLanguages = array();
foreach($eshopLink->getLanguages() as $lang) {
$selectedLanguages[] = $lang->getLangId();
}
$formBuilder->add('languages', ChoiceType::class, array(
'choices' => $languages,
'required' => false,
'mapped' => false,
'multiple' => true,
'expanded' => true,
'label_attr' => array('class' => 'form_field_label'),
'attr' => array('class' => 'form_field_text'),
'data' => $selectedLanguages
));
/* we add department list */
$departments = array();
foreach($departmentCollection as $dep)
{
$depId = $dep->getDepartmentId();
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
//print('<br>qqw dep: '.$dep->getDepartmentId());
//print('<br>qqw cat: '.$catId);
$depKey = $dep->getDepartmentName().' (id:'.$dep->getDepartmentId().')';
$departments[$depKey] = $depId;
}
$selectedDepartment = 0;
// we load current category associations
foreach($eshopLink->getDepartments() as $dep) {
//print('<br>qqw sel dep: '.$dep->getDepartmentId());
$selectedDepartment = $dep->getDepartmentId();
}
$formBuilder->add('department1', ChoiceType::class, array(
'choices' => $departments,
'label' => $this->translator->trans('marketplace.department'),
'attr' => array('class' => 'selector'),
'label_attr' => array('class' => 'form_field_label'),
'data' => $selectedDepartment,
'mapped' => false,
));
/*
$formBuilder->add('feedUrl', TextType::class, array(
'required' => false,
'label' => $this->translator->trans('marketplace.feed_url'),
'attr' => array('class' => 'text_form', 'size' => 57, 'value' => $eshopLink->getFeedUrl()),
'label_attr' => array('class' => 'form_field_label')
));
*/
$formBuilder->add('save', SubmitType::class, array('label' => $this->translator->trans('form.button.save'),
'attr' => array('class' => 'butt_big')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
//$form->bindRequest($request);
if ($form->isValid()) {
$formData = $form->getData();
$eshopLink->setEshopLinkUrl($formData->getEshopLinkUrl());
$eshopLink->setLogoUrl($formData->getLogoUrl());
//$eshopLink->setFeedUrl($request->request->get('form')['feedUrl']);
/* multilangual fields */
foreach($eshopLink->getLanguages() as $lang) {
$eshopLink->setLocale($lang->getLangKey());
//$dem->refresh($product);
$eshopLink->setEshopLinkName($form['eshopLinkName_'.$lang->getLangKey()]->getData());
$eshopLink->setEshopLinkDescription($form['eshopLinkDescription_'.$lang->getLangKey()]->getData());
//$dem->persist($product);
//$product->setProductDescription($request->request->get('form')['productDescription_'.$lang->getLangKey()]);
//$product->setProductShortDescription($request->request->get('form')['productShortDescription_'.$lang->getLangKey()]);
$em->persist($eshopLink);
$em->flush();
}
/* we remove old language associations */
foreach($eshopLink->getLanguages() as $lang) {
$eshopLink->removeLanguage($lang);
}
/* we load eshop languages */
if(!empty($form['languages']->getData())) {
foreach($form['languages']->getData() as $langId) {
$addLang = $em->getRepository(Language::class)->getLanguage($langId);
$eshopLink->addLanguage($addLang);
//$session->get('user')->addService($addService);
}
}
//print('<br>QQW cat 1: '.$request->request->get('form')['category1']);
//Doctrine\Common\Util\Debug::dump($request-equest->get('form')['category1']);
/* departments */
/* we remove old associations */
foreach($eshopLink->getDepartments() as $dep) {
$eshopLink->removeDepartment($dep);
}
if($form['department1']->getData() !== 0) {
$department1 = $em->getRepository(Server::class)->getDepartment($form['department1']->getData());
if(!empty($department1)) {
$eshopLink->addDepartment($department1);
}
}
/* we persist and save */
foreach($eshopLink->getDepartments() as $dep) {
$em->persist($dep);
}
//we create new feed
if(!empty($form['feedUrl']) && empty($feed)) {
$feed = new Feed;
$feed->setFeedName($eshopLink->getEshopLinkName());
$feed->setFeedUrl($form['feedUrl']->getData());
/* we add department */
if(!empty($department1)) {
$feed->addDepartment($department1);
}
if(!empty($session->get('user'))) {
/* user is logged in - we persist the new feed */
$feed->setUserId($session->get('user')->getUserId());
/* we set this feed as current one */
$em->getRepository(Feed::class)->unsetCurrentFeed($session->get('user')->getUserId());
$feed->setFeedCurrent(true);
/* we persist and save feed */
$em->persist($feed);
$em->flush();
$this->addFlash('notice', 'New Feed was created.');
return $this->redirectToRoute('feedConfig');
} else {
/* we store new feed in session and redirect to new account form */
$session->set('feed', $feed);
$this->addFlash('notice', 'Please add your user account info.');
return $this->redirectToRoute('newaccount');
}
}
$em->persist($eshopLink);
$em->flush();
$this->addFlash('notice', 'E-shop Link was updated.');
return $this->redirectToRoute('eshopLinkConfig');
}
}
/* we render data */
return $this->render('eshopLinkConfig.html.twig',
array('headerData' => $this -> getPageHeader($request),
'form' => $formBuilder->getForm()->createView(),
'eshopLink' => $eshopLink,
'userDirs' => $userDirs,
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/feedNew", name="feedNew")
*/
public function feedNewAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
$dem = $this->doctrine->getManager('dynamic_em');
$userId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($userId);
$departmentCollection = $em->getRepository(Server::class)->getDepartmentList();
/* we build login form */
$feed = new Feed;
$formBuilder = $this->createFormBuilder($feed);
$formBuilder->add('feedName', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('marketplace.feed_name'),
'attr' => array('class' => 'text_form', 'size' => 33),
'label_attr' => array('class' => 'form_field_label'),
));
$formBuilder->add('feedUrl', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('system.url'),
'attr' => array('class' => 'text_form', 'size' => 33),
'label_attr' => array('class' => 'form_field_label'),
));
/* we add department list */
$departments = array();
//$departments['Root'] = 0;
foreach($departmentCollection as $dep)
{
$depId = $dep->getDepartmentId();
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
// print('<br>qqw department: '.$depId);
$departments[$dep->getDepartmentName()] = $depId;
}
$formBuilder->add('department1', ChoiceType::class, array(
'choices' => $departments,
'attr' => array('class' => 'selector'),
'label' => 'Parent department',
'mapped' => false,
'label_attr' => array('class' => 'form_field_label')
));
$formBuilder->add('save', SubmitType::class, array('label' => $this->translator->trans('form.button.save'),
'attr' => array('class' => 'butt_big')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
if ($form->isValid()) {
$formData = $form->getData();
/* we load users entity manager */
//$dem = $this->doctrine->getManager('dynamic_em');
$feed->setFeedName($formData->getFeedName());
$feed->setFeedUrl($formData->getFeedUrl());
/* we add department */
if($form['department1']->getData() !== 0) {
$department1 = $em->getRepository(Server::class)->getDepartment($form['department1']->getData());
if(!empty($department1)) {
$feed->addDepartment($department1);
}
}
if(!empty($session->get('user'))) {
/* user is logged in - we persist the new feed */
$feed->setUserId($session->get('user')->getUserId());
/* we set this feed as current one */
$em->getRepository(Feed::class)->unsetCurrentFeed($session->get('user')->getUserId());
$feed->setFeedCurrent(true);
/* we persist and save feed */
$em->persist($feed);
$em->flush();
$this->addFlash('notice', 'New Feed was created.');
return $this->redirectToRoute('feedConfig');
} else {
/* we store new feed in session and redirect to new account form */
$session->set('feed', $feed);
$this->addFlash('notice', 'Please add your user account info.');
return $this->redirectToRoute('newaccount');
}
}
}
/* we render data */
return $this->render('feedNew.html.twig',
array( 'form' => $formBuilder->getForm()->createView(),
'headerData' => $this -> getPageHeader($request),
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/feedConfig", name="feedConfig")
*/
public function feedConfigAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
$dem = $this->doctrine->getManager('dynamic_em');
/* we get feed */
$userId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($userId);
/* we get current feed */
$feed = $em->getRepository(Feed::class)->getCurrentFeed($userId);
/* we get departments */
$departmentCollection = $em->getRepository(Server::class)->getDepartmentList();
$userDirs = $this->getUserFolderPaths($request);
/* we build edit form */
$formBuilder = $this->createFormBuilder($feed);
$formBuilder->add('feedName', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('marketplace.feed_name'),
'attr' => array('class' => 'text_form', 'size' => 50, 'value' => $feed->getFeedName()),
'label_attr' => array('class' => 'form_field_label')
));
$formBuilder->add('feedUrl', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('system.url'),
'attr' => array('class' => 'text_form', 'size' => 50, 'value' => $feed->getFeedUrl()),
'label_attr' => array('class' => 'form_field_label')
));
$formBuilder->add('feedType', TextType::class, array(
'required' => true,
'label' => $this->translator->trans('marketplace.feed_type'),
'attr' => array('class' => 'text_form', 'size' => 50, 'value' => $feed->getFeedType()),
'label_attr' => array('class' => 'form_field_label')
));
/* we add department list */
$departments = array();
foreach($departmentCollection as $dep)
{
$depId = $dep->getDepartmentId();
$dep->setLocale($session->get('lang')->getLangKey());
$em->refresh($dep);
//print('<br>qqw dep: '.$dep->getDepartmentId());
//print('<br>qqw cat: '.$catId);
$depKey = $dep->getDepartmentName().' (id:'.$dep->getDepartmentId().')';
$departments[$depKey] = $depId;
}
$selectedDepartment = 0;
// we load current category associations
foreach($feed->getDepartments() as $dep) {
//print('<br>qqw sel dep: '.$dep->getDepartmentId());
$selectedDepartment = $dep->getDepartmentId();
}
$formBuilder->add('department1', ChoiceType::class, array(
'choices' => $departments,
'mapped' => false,
'label' => $this->translator->trans('marketplace.department'),
'attr' => array('class' => 'selector'),
'label_attr' => array('class' => 'form_field_label'),
'data' => $selectedDepartment
));
$formBuilder->add('notes', TextareaType::class, array(
'required' => false,
'label' => $this->translator->trans('system.notes'),
'attr' => array('class' => 'textarea_form', 'cols' => 55, 'rows' => 5, 'value' => $feed->getNotes()),
'label_attr' => array('class' => 'form_textarea_label'),
'data' => $feed->getNotes(),
));
$formBuilder->add('save', SubmitType::class, array('label' => $this->translator->trans('form.button.save'),
'attr' => array('class' => 'butt_big')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
//$form->bindRequest($request);
if ($form->isValid()) {
$formData = $form->getData();
$feed->setFeedName($formData->getFeedName());
$feed->setFeedUrl($formData->getFeedUrl());
$feed->setFeedType($formData->getFeedType());
//print('<br>QQW cat 1: '.$request->request->get('form')['category1']);
//Doctrine\Common\Util\Debug::dump($request->request->get('form')['category1']);
/* departments */
/* we remove old associations */
foreach($feed->getDepartments() as $dep) {
$feed->removeDepartment($dep);
}
if($form['department1']->getData() !== 0) {
$department1 = $em->getRepository(Server::class)->getDepartment($form['department1']->getData());
if(!empty($department1)) {
$feed->addDepartment($department1);
}
}
/* we persist and save */
foreach($feed->getDepartments() as $dep) {
$em->persist($dep);
}
$em->persist($feed);
$em->flush();
$this->addFlash('notice', 'Feed was updated.');
return $this->redirectToRoute('feedConfig');
}
}
/* we render data */
return $this->render('feedConfig.html.twig',
array('headerData' => $this -> getPageHeader($request),
'form' => $formBuilder->getForm()->createView(),
'feed' => $feed,
'userDirs' => $userDirs,
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/marketplaceProductAdmin", name="marketplaceProductAdmin")
*/
public function marketplaceProductAdminAction(Request $request)
{
/* we load session data */
parent::init($request);
$session = $request->getSession();
/* we load entity managers */
$em = $this->doctrine->getManager();
$dem = $this->doctrine->getManager('dynamic_em');
/* we get current user */
$currentUserId = $session->get('user')->getUserId();
$user = $em->getRepository(User::class)->getUser($currentUserId);
/* setting current feed */
if(!empty($request->query->get('setCurrent'))) {
$feed = $em->getRepository(Feed::class)->getFeed($request->query->get('setCurrent'));
$em->getRepository(Feed::class)->setCurrentFeed($currentUserId, $request->query->get('setCurrent'));
$session->set('feed', $feed);
$this->addFlash('notice', 'The feed '.$feed->getFeedName().' was set as current one.');
return $this->redirectToRoute('marketplaceProductAdmin');
}
/* we get current feed */
$currentFeed = $em->getRepository(Feed::class)->getCurrentFeed($currentUserId);
/*
print('<br>qqw currentEshop: ');
\Doctrine\Common\Util\Debug::dump($currentEshop->getEshopId());
*/
/* we load list of eshop links for logged user */
$feedList = $em->getRepository(Feed::class)->getFeedListByUser($currentUserId);
// print('<br>qqw feedList: ');
// \Doctrine\Common\Util\Debug::dump($feedList);
/* we render data */
return $this->render('feedAdmin.html.twig',
array('headerData' => $this -> getPageHeader($request),
'feedList' => $feedList,
'menu' => $this -> adminMenu($request),
'mainMenu' => $this -> adminMainMenu($request),
'user' => $user,
)
);
}
/**
* @Route("/marketplace/eshop/{eshopId}", name="marketplaceEshop")
*/
public function marketplaceEshopAction(Request $request, $eshopId)
{
/* we load session data */
parent::init($request);
/* we load entity managers */
$em = $this->doctrine->getManager();
/* we get eshop */
// $eshopLink = $em->getRepository(Product::class)->getProduct($eshopId);
$eshopLink = $em->getRepository(EshopLink::class)->getEshopLink($eshopId);
if(empty($eshopLink)) {
$this->addFlash('error', 'eShop with this ID does not exist.');
return $this->redirectToRoute('marketplace');
}
if(!empty($request->get('redirect_to_eshop'))) {
print('<br>redirect_to_eshop 22...');
$clickedCount = (int)$eshopLink->getClickedCount();
$eshopLink->setClickedCount($clickedCount + 1);
$em->persist($eshopLink);
$em->flush();
return $this->redirect($eshopLink->getEshopLinkUrl());
// die('<br>redirect to shop ...');
}
/*
$departmentPath = $em->getRepository(Department::class)->getPath($department);
$childDepartments = $department->getChildren();
//$childDepartments = $em->getRepository(Department::class)->getRootNodes();
*/
/*
print('<br>qqw eshopLinkList: ');
\Doctrine\Common\Util\Debug::dump($eshopLinkList);
*/
// increments product view count
$viewedCount = (int)$eshopLink->getClickedCount();
$eshopLink->setClickedCount($viewedCount + 1);
$em->persist($eshopLink);
$em->flush();
/* we render data */
return $this->render('marketplaceEshop.html.twig',
array('headerData' => $this -> getPageHeader($request),
'eshopLink' => $eshopLink,
)
);
}
public function adminMenu(Request $request)
{
$menuItems = array(0 => array('link' => 'marketplaceAdmin', 'langKey' => 'marketplace.admin', 'routeName' => 'marketplaceAdmin'),
1 => array('link' => 'marketplaceProductListEdit', 'langKey' => 'marketplace.products', 'routeName' => 'marketplaceProductListEdit'),
2 => array('link' => 'marketplaceProductAdmin', 'langKey' => 'module.marketplaceProductAdmin', 'routeName' => 'marketplaceProductAdmin'),
3 => array('link' => 'feedNew', 'langKey' => 'marketplace.new_feed', 'routeName' => 'feedNew'),
4 => array('link' => 'eshopLinkAdmin', 'langKey' => 'module.eshopLinkAdmin', 'routeName' => 'eshopLinkAdmin'),
5 => array('link' => 'addproducts', 'langKey' => 'module.eshopLink_new', 'routeName' => 'eshopLinkNew'),
);
return $menuItems;
}
}