<?php
namespace App\Controller;
use App\Entity\ColsByRow;
use App\Entity\ContentBySection;
use App\Entity\ContentElementsByColumns;
use App\Entity\ContentElementsByMeta;
use App\Entity\Page;
use App\Entity\ResourcesByPage;
use App\Entity\RowsBySection;
use App\Entity\SectionsByPage;
use App\Entity\SectionStyles;
use App\Entity\Services;
use App\Entity\Sliders;
use App\Entity\Team;
use App\Form\PageType;
use App\Repository\PageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @Route("/admin/page")
*/
class PageController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $em;
private $serializer;
/**
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager,
SerializerInterface $serializer){
$this->em = $entityManager;
$this->serializer = $serializer;
}
/**
* @Route("/create-section", name="section_create", methods={"POST"})
*/
public function sectionCreate(Request $request): Response
{
$resp = array(
'success' => false,
'error' => false,
'msg' => "Something went wrong"
);
$all = $request->request->all();
//dd($all);
try{
$style = $this->em->getRepository(SectionStyles::class)->findOneBy( array( 'code' => 'Row' ) );
$page = $this->em->getRepository(Page::class)->findOneBy( array( 'id' => $all['idPage'] ) );
$sections = $this->em->getRepository(SectionsByPage::class)->findBy( array( 'pageId' => $page->getId() ) );
$section = new SectionsByPage();
$section->setActive( true );
$section->setCreatedAt( new \DateTime() );
$section->setTitle( strtolower(str_replace(' ', '-', $all['name'])) );
$section->setPageId( $page );
$section->setStyleId($style);
$section->setIndexer( COUNT($sections) + 1 );
$this->em->persist($section);
$row = new RowsBySection();
$row->setNumberCols(0);
$row->setCssClass('row');
$row->setSectionId($section);
$row->setName('row-' . $section->getTitle());
$row->setIndexer(1);
$this->em->persist($row);
$columns = $this->em->getRepository(ColsByRow::class)->findBy( array( 'rowId' => $row->getId() ) );
switch ($all['size']){
case 'one':
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-12 large-12 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(1);
$this->em->persist($col);
break;
case 'one-margin':
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-2 large-2 small-2 columns' );
$col->setIndexer(1);
$col->setNumberColumns(2);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-8 large-8 small-8 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(8);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-2 large-2 small-2 columns' );
$col->setIndexer(1);
$col->setNumberColumns(2);
$this->em->persist($col);
break;
case 'two':
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-6 large-6 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(6);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-6 large-6 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(6);
$this->em->persist($col);
break;
case 'two-left':
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-3 large-3 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(3);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-9 large-9 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(9);
$this->em->persist($col);
break;
case 'two-right':
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-9 large-9 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(9);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-3 large-3 small-12 columns' );
$col->setType( 'html' );
$col->setNumberColumns(3);
$col->setIndexer(1);
$this->em->persist($col);
break;
case 'three':
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-4 large-4 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(4);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-4 large-4 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(4);
$this->em->persist($col);
$col = new ColsByRow();
$col->setRowId($row);
$col->setName('column');
$col->setCssClass( 'col-md-4 large-4 small-12 columns' );
$col->setType( 'html' );
$col->setIndexer(1);
$col->setNumberColumns(4);
$this->em->persist($col);
break;
}
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'sectionId' => $section->getId(),
'columns' => $all['size'],
'sectionName' => $section->getTitle(),
'msg' => 'The section was created'
);
} catch (\Exception $e){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
return new JsonResponse($resp);
}
/**
* @Route("/", name="page_index", methods={"GET"})
*/
public function index(PageRepository $pageRepository): Response
{
return $this->render('admin/page/index.html.twig', [
'pages' => $pageRepository->findAll(),
]);
}
/**
* @Route("/html-builder/{id}", name="page_new", methods={"GET","POST"})
*/
public function pageBuilder($id = null, Request $request): Response
{
$page = $sections = $rows = $cols = $content = array();
if( $id !== null ){
$page = $this->em->getRepository(Page::class)->findOneBy(array( 'id' => $id ));
if(!is_object($page)){
return $this->redirectToRoute('page_index');
}
$sections = $this->em->getRepository(SectionsByPage::class)->findBy( array( 'pageId' => $page->getId() ), array( 'indexer' => 'ASC' ));
if(is_object($sections)){
$rows = $this->em->getRepository(RowsBySection::class)->findOneBy( array(
'sectionId' => $sections[0]->getId()
));
}
}
$styles = $this->em->getRepository(SectionStyles::class)->findAll();
return $this->render('admin/page/new.html.twig', array(
'styles' => $styles,
'page' => $page,
'sections' => $sections,
'rows' => $rows
));
}
/**
* @Route("/create", name="page_create", methods={"POST"})
*/
public function create(Request $request): Response
{
$resp = array(
'success' => false,
'error' => false,
'msg' => "Something went wrong"
);
$all = $request->request->all();
try{
$url = "/" . ( $all['url'] !== '' ? strtolower(str_replace(' ', '-', $all['url'])) : strtolower(str_replace(' ', '-', $all['name'])) );
$page = $this->em->getRepository(Page::class)->validatePageExist( $url, $all["name"]);
if(is_object($page)){
$resp = array(
'success' => false,
'error' => false,
'msg' => 'The page already exist'
);
} else {
$page = new Page();
$page->setName($all["name"]);
$page->setUrl($all["name"]);
$page->setCreatedAt(new \DateTime());
$page->setVisible(1);
$this->em->persist($page);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'pageId' => $page->getId(),
'msg' => 'The page was created'
);
}
} catch (\Exception $e){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
return new JsonResponse($resp);
}
/**
* @Route("/create-element", name="element_create", methods={"POST"})
*/
public function createElement(Request $req){
$all = $req->request->all();
$column = $this->em->getRepository(ColsByRow::class)->findOneBy( array(
'id' => $all["column"]
));
if(is_object($column)){
$content = $this->em->getRepository(ContentElementsByColumns::class)->findBy( array(
'columnId' => $column->getId()
));
switch ($all["action"]){
case 'add':
switch ( $all["option"] ){
case 'text':
$hash = uniqid();
$element = new ContentElementsByColumns();
$element->setColumnId($column);
$element->setInitHtml('<p>');
$element->setEndHtml('</p>');
$element->setType('p');
$element->setKeyname('text');
$element->setConfig('text');
$element->setValue('demo');
$element->setIndexer(COUNT($content) + 1 );
$element->setActive(0);
$element->setCreatedAt(new \DateTime());
$element->setAutoload(1);
$element->setSlug('text-'.$hash);
$element->setName('text-'.$hash);
$this->em->persist($element);
$this->em->flush();
return $this->render('admin/page/components/element-render.html.twig', array(
'type' => $all["option"],
'element' => $element
) );
break;
}
break;
}
}
return new Response(true);
}
/**
* @Route("/get-sections-by-index", name="get_sections_by_index", methods={"POST"})
*/
public function getSectionsByIndex(Request $request){
$idPage = $request->get('idPage', null);
$sections = $this->em->getRepository(SectionsByPage::class)->findBy(array( 'pageId' => $idPage ),array('indexer' => 'ASC'));
return $this->render('admin/page/components/section-list.html.twig',array(
'sections' => $sections
));
}
/**
* @Route("/new-meta-item", name="new_meta_item", methods={"POST"})
*/
public function newMetaItem(Request $req){
$resp = array(
'success' => false,
'error' => false,
'msg' => 'No se pudo crear el item'
);
if($req->isXmlHttpRequest()){
$all = $req->request->all();
$element = $this->em->getRepository(ContentElementsByColumns::class)->findOneBy(array('id' => $all["id"]));
switch ($all["element"]){
case 'list-item':
$item = $this->em->getRepository(ContentElementsByMeta::class)->findOneBy(array('type' => 'list-item'));
if(is_object($item)){
$newItem = clone $item;
$newItem->setValue($all["title"]);
$newItem->setContainerValue($all["text"]);
$newItem->setContentElementId((int)$element->getId());
$this->em->persist($newItem);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'msg' => 'Se creo el item'
);
}
break;
}
return new JsonResponse($resp);
}
return $this->redirectToRoute('page_index');
}
/**
* @Route("/get-content-section", name="get_content_by_section", methods={"POST"})
*/
public function getContentBySection(Request $request){
$sectionId = $request->get('sectionId',null);
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy(array( 'id' => $sectionId ));
if(is_object($section)){
$rows = $this->em->getRepository(RowsBySection::class)->findBy( array(
'sectionId' => $section->getId()
), array( 'indexer' => 'ASC' ));
//render grid with $rows
//call cols and content from twig
return $this->render('admin/page/components/grid-render-with-content.html.twig', array(
'rows' => $rows
));
}
return new Response('');
}
/**
* @Route("/get-component", name="get_component_html", methods={"POST"})
*/
public function getComponentHTMLByParam(Request $request){
$all = $request->request->all();
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all['sectionId'] ) );
$row = $this->em->getRepository(RowsBySection::class)->findOneBy( array( 'sectionId' => (int)$section->getId() ));
switch ( $all["columns"] ){
case 'one':
$col = $this->em->getRepository(ColsByRow::class)->findOneBy( array( 'rowId' => (int)$row->getId() ));
return $this->render('admin/page/components/single-column.html.twig', array(
'column' => $col
));
break;
default:
$cols = $this->em->getRepository(ColsByRow::class)->findBy( array( 'rowId' => (int)$row->getId() ));
return $this->render('admin/page/components/grid-render.html.twig', array(
'columns' => $cols
));
break;
}
}
/**
* @Route("/get-config-modal", name="get_config_to_modal_edit_element", methods={"POST"})
*/
public function getConfigToModalEditElement(Request $request){
$all = $request->request->all();
switch ( $all["element"] ){
case 'Section':
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all['id'] ) );
return $this->render('admin/page/html-builder/modal-change-section.html.twig', array(
'section' => $section
));
break;
case 'Slider':
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all['id'] ) );
$reference = $this->em->getRepository(ResourcesByPage::class)->findOneBy( array( 'id' => $section->getResourceId() ) );
$slider = $this->em->getRepository(Sliders::class)->findOneBy( array( 'id' => $reference->getIdItem() ) );
$sliders = $this->em->getRepository(Sliders::class)->findBy( array( 'visible' => true ) );
return $this->render('admin/page/html-builder/modal-change-slider.html.twig', array(
'slider' => $slider,
'section' => $section,
'sliders' => $sliders
));
break;
case 'Column':
$column = $this->em->getRepository(ColsByRow::class)->findOneBy( array( 'id' => $all['id'] ) );
return $this->render('admin/page/html-builder/modal-edit-column.html.twig', array(
'column' => $column
));
break;
case 'Element':
$element = $this->em->getRepository(ContentElementsByColumns::class)->findOneBy( array( 'id' => $all['id'] ) );
switch ($all["argument"]){
case 'text':
return $this->render('admin/page/html-builder/modal-change-text.html.twig', array(
'element' => $element
));
break;
case 'image':
return $this->render('admin/page/html-builder/modal-change-image.html.twig', array(
'element' => $element
));
break;
case 'separator':
return $this->render('admin/page/html-builder/modal-change-separator.html.twig', array(
'element' => $element
));
break;
case 'list':
if(array_key_exists('meta',$all)){
$meta = $this->em->getRepository(ContentElementsByMeta::class)->findOneBy( array( 'id' => $all["id"] ) );
return $this->render('admin/page/html-builder/modal-meta-list.html.twig', array(
'element' => $element,
'meta' => $meta
));
} else {
$items = $this->em->getRepository(ContentElementsByMeta::class)->findBy( array( 'contentElementId' => $element->getId() ) );
return $this->render('admin/page/html-builder/modal-change-list.html.twig', array(
'element' => $element,
'items' => $items
));
}
break;
}
case 'Reorder':
switch ($all["argument"]){
case 'section-reorder':
$sections = $this->em->getRepository(SectionsByPage::class)->findBy( array( 'pageId' => $all['id'] ), array( 'indexer' => 'ASC' ) );
return $this->render('admin/page/html-builder/modal-reorder-sections.html.twig', array(
'sections' => $sections,
'sectionClicked' => (int)$all['aux']
));
break;
case 'element-reorder':
$element = $this->em->getRepository(ContentElementsByColumns::class)->findOneBy(array( 'id' => $all["id"] ));
$elements = $this->em->getRepository(ContentElementsByColumns::class)->findBy(array( 'columnId' => $element->getColumnId()), array('indexer' => 'ASC'));
return $this->render('admin/page/html-builder/modal-reorder-elements.html.twig', array(
'element' => $element,
'elements' => $elements
));
break;
case 'separator':
return $this->render('admin/page/html-builder/modal-change-separator.html.twig', array(
'element' => $element
));
break;
}
break;
default:
break;
}
return new Response('');
}
/**
* @Route("/save-changes-element", name="save_changes_element", methods={"POST"})
*/
public function saveChangesElement(Request $request){
$all = $request->request->all();
$files = $request->files->all();
$resp = array(
'success' => false,
'error' => false,
'msg' => null
);
//dd($all, $files);
error_log($all["element"]);
try{
switch ( $all["element"] ){
case 'background':
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all['id'] ) );
$section->setHasContainer( ( array_key_exists('fullwidth', $all) ? false : true) );
if($files["image"] !== null){
$img = $files["image"];
$fileName = md5 ( uniqid () ) . '.' . $img->guessExtension();
$original_name = $img->getClientOriginalName();
$img->move ( $this->getParameter( 'images_folder' ), $fileName );
$path = '/' . $this->getParameter( 'images_folder' ) . $fileName;
$section->setBackground($path);
}
$this->em->persist($section);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'url' => $section->getBackground(),
'element' => $all["element"],
'msg' => 'Success'
);
break;
case 'text':
case 'title':
case 'subtitle':
$content = $this->em->getRepository(ContentElementsByColumns::class)->findOneBy(array(
'id' => $all['id']
));
if(is_object($content)){
$content->setColor($all["colorText"]);
$content->setAlign($all["align"]);
$content->setValue($all["text"]);
if( $all["type"] == 'h2' ){
$content->setInitHtml('<h2 style="margin:0;font-family:;font-weight:400;">');
$content->setEndHtml('</h2>');
$content->setType('h2');
$content->setKeyname('title');
} elseif ($all["type"] == 'h4'){
$content->setInitHtml('<h4 style="font-family:;font-weight:400;">');
$content->setEndHtml('</h4>');
$content->setType('h4');
$content->setKeyname('subtitle');
} else {
$content->setInitHtml('<p>');
$content->setEndHtml('</p>');
$content->setType('p');
$content->setKeyname('text');
}
$this->em->persist($content);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'element' => $all["element"],
'msg' => 'Success'
);
}
break;
case 'column':
$column = $this->em->getRepository(ColsByRow::class)->findOneBy( array( 'id' => $all['id'] ) );
if(is_object($column)){
if($files["image"] !== null){
$img = $files["image"];
$fileName = md5 ( uniqid () ) . '.' . $img->guessExtension();
$original_name = $img->getClientOriginalName();
$img->move ( $this->getParameter( 'images_folder' ), $fileName );
$path = '/' . $this->getParameter( 'images_folder' ) . $fileName;
$column->setBackgroundImage($path);
}
$column->setBackgroundColor( ( $all["colorText"] !== "" ? $all["colorText"] : null ) );
$column->setName( ( $all["name"] !== "" ? $all["name"] : null ) );
$column->setAlign( ( $all["align"] !== "" ? $all["align"] : null ) );
$column->setMargin( ( $all["margin"] !== "" ? $all["margin"] : null ) );
$column->setPadding( ( $all["padding"] !== "" ? $all["padding"] : null ) );
$column->setCssClass( ( $all["class-css"] !== "" ? $all["class-css"] : null ) );
$column->setJsId( ( $all["js-id"] !== "" ? $all["js-id"] : null ) );
$column->setStylesInline( ( $all["styles-css"] !== "" ? $all["styles-css"] : null ) );
$this->em->persist($column);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'element' => $all["element"],
'msg' => 'Success'
);
}
break;
case 'section':
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all['id'] ) );
if(is_object($section)){
if($files["image"] !== null){
$img = $files["image"];
$fileName = md5 ( uniqid () ) . '.' . $img->guessExtension();
$original_name = $img->getClientOriginalName();
$img->move ( $this->getParameter( 'images_folder' ), $fileName );
$path = '/' . $this->getParameter( 'images_folder' ) . $fileName;
$section->setBackground($path);
}
$section->setBackgroundColor( ( $all["colorText"] !== "" ? $all["colorText"] : null ) );
$section->setTitle( ( $all["title"] !== "" ? $all["title"] : null ) );
$section->setMargin( ( $all["margin"] !== "" ? $all["margin"] : null ) );
$section->setPadding( ( $all["padding"] !== "" ? $all["padding"] : null ) );
$section->setCssClass( ( $all["class-css"] !== "" ? $all["class-css"] : null ) );
$section->setJsId( ( $all["js-id"] !== "" ? $all["js-id"] : null ) );
$section->setStylesInline( ( $all["styles-css"] !== "" ? $all["styles-css"] : null ) );
$this->em->persist($section);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'element' => $all["element"],
'msg' => 'Success'
);
}
break;
case 'list-item':
$item = $this->em->getRepository(ContentElementsByMeta::class)->findOneBy(array(
'id' => $all["id"]
));
if(is_object($item)){
$item->setValue($all["title"]);
$item->setContainerValue($all["text"]);
$this->em->persist($item);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'element' => $all["element"],
'msg' => 'Success'
);
}
break;
default:
// do anything
break;
}
} catch ( \Exception $e ){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
return new JsonResponse($resp);
}
/**
* @Route("/update-index-elements", name="update_index_elements", methods={"POST"})
*/
public function updateIndexElements(Request $request){
if( $request->isXmlHttpRequest() ){
$resp = array(
'success' => false,
'error' => false,
'msg' => null
);
$all = $request->request->all();
switch ( $all["element"] ){
case 'section':
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all["id"] ) );
if(is_object($section)){
try{
//update index records in left or right direction
if( (int)$all["index"] < $section->getIndexer() ){
$this->em->getRepository(SectionsByPage::class)->updateIndexerSectionRigth( $section->getPageId()->getId(), (int)$all["index"] , $section->getIndexer() );
} else {
$this->em->getRepository(SectionsByPage::class)->updateIndexerSectionLeft( $section->getPageId()->getId(), (int)$all["index"] + 1 , $section->getIndexer() );
}
$section->setIndexer((int)$all["index"]);
$this->em->persist($section);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'msg' => 'The item position was updated'
);
} catch (\Exception $e){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
}
break;
case 'element':
$element = $this->em->getRepository(ContentElementsByColumns::class)->findOneBy( array('id' => $all["id"]) );
if(is_object($element)){
try{
//update index records in left or right direction
if( (int)$all["index"] < $element->getIndexer() ){
$this->em->getRepository(ContentElementsByColumns::class)->updateIndexerSectionRigth( $element->getColumnId()->getId(), (int)$all["index"] , $element->getIndexer() );
} else {
$this->em->getRepository(ContentElementsByColumns::class)->updateIndexerSectionLeft( $element->getColumnId()->getId(), (int)$all["index"] + 1 , $element->getIndexer() );
}
$element->setIndexer((int)$all["index"]);
$this->em->persist($element);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'msg' => 'The item position was updated'
);
} catch (\Exception $e){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
}
break;
case 'team':
$single = $this->em->getRepository(Team::class)->findOneBy( array('id' => $all["id"]) );
if(is_object($single)){
try{
//update index records in left or right direction
if( (int)$all["index"] < $single->getIndexer() ){
error_log('El nuevo indice es menor al que tenia antes el elemento');
$this->em->getRepository(Team::class)->updateIndexerSectionRigth( (int)$all["index"] , $single->getIndexer() );
} else {
error_log('El nuevo indice es MAYOR al que tenia antes el elemento');
$this->em->getRepository(Team::class)->updateIndexerSectionLeft((int)$all["index"] + 1 , $single->getIndexer() );
}
error_log('Nuevo Index: ' . ((int)$all["index"]));
error_log($single->getIndexer());
$single->setIndexer((int)$all["index"]);
$this->em->persist($single);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'reload' => true,
'msg' => 'The item position was updated'
);
} catch (\Exception $e){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
}
break;
case 'services':
$single = $this->em->getRepository(Services::class)->findOneBy( array('id' => $all["id"]) );
if(is_object($single)){
try{
//update index records in left or right direction
if( (int)$all["index"] < $single->getIndexer() ){
error_log('El nuevo indice es menor al que tenia antes el elemento');
$this->em->getRepository(Services::class)->updateIndexerSectionRigth( (int)$all["index"] , $single->getIndexer() );
} else {
error_log('El nuevo indice es MAYOR al que tenia antes el elemento');
$this->em->getRepository(Services::class)->updateIndexerSectionLeft((int)$all["index"] + 1 , $single->getIndexer() );
}
error_log('Nuevo Index: ' . ((int)$all["index"]));
error_log($single->getIndexer());
$single->setIndexer((int)$all["index"]);
$this->em->persist($single);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'reload' => true,
'msg' => 'The item position was updated'
);
} catch (\Exception $e){
$resp = array(
'success' => false,
'error' => true,
'msg' => $e->getMessage()
);
}
}
break;
}
return new JsonResponse($resp);
}
return $this->redirectToRoute('page_index');
}
/**
* @Route("/delete-element", name="delete_element", methods={"POST"})
*/
public function deleteElement(Request $request){
if($request->isXmlHttpRequest()){
$all = $request->request->all();
$resp = array(
'success' => false,
'error' => false,
'msg' => null
);
switch ( $all["element"] ){
case 'section':
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $all["id"] ) );
if(is_object($section)){
//delete all rows, cols and content
$this->em->remove($section);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'msg' => null,
'element' => $all["element"]
);
}
break;
}
return new JsonResponse($resp);
}
return $this->redirectToRoute('page_index');
}
/**
* @Route("/delete-meta-element", name="delete_meta_element", methods={"POST"})
*/
public function deleteMetaElement(Request $request){
if($request->isXmlHttpRequest()){
$all = $request->request->all();
$resp = array(
'success' => false,
'error' => false,
'msg' => null
);
$meta = $this->em->getRepository(ContentElementsByMeta::class)->findOneBy( array( 'id' => $all["meta"] ) );
if(is_object($meta)){
//delete all rows, cols and content
$this->em->remove($meta);
$this->em->flush();
$resp = array(
'success' => true,
'error' => false,
'msg' => null
);
}
return new JsonResponse($resp);
}
return $this->redirectToRoute('page_index');
}
public function getContentElements( $arg, $sectionId, $rows = null, $cols = null ){
$section = $this->em->getRepository(SectionsByPage::class)->findOneBy( array( 'id' => $sectionId ) );
switch ( $section->getStyleId()->getCode() ){
case 'Slider':
case 'Background':
return $this->render('admin/page/components/single-column-with-content.html.twig', array(
'section' => $section,
'argument' => $arg
));
break;
default:
return new Response(" ");
break;
}
}
public function getGridWithContent( $arg, $elementId, $cols = null ){
if($cols != null){
} else {
switch ( $arg ){
case 'column':
$cols = $this->em->getRepository(ColsByRow::class)->findBy( array( 'rowId' => $elementId ) );
return $this->render('admin/page/components/grid-render-cols-by-row.html.twig', array(
'columns' => $cols
));
break;
case 'content':
$contents = $this->em->getRepository(ContentElementsByColumns::class)->findBy( array( 'columnId' => $elementId ), array('indexer' => 'ASC') );
return $this->render('admin/page/components/grid-element-render.html.twig', array(
'contents' => $contents
));
break;
}
}
return new Response('');
}
}