/**
  * Deletes a entity.
  * 
  * @param string $type ['widget', 'page']
  * 
  * @return Response
  * @access public
  * @author Etienne de Longeaux <*****@*****.**>
  */
 public function deletetwigcacheajaxAction($type)
 {
     $request = $this->container->get('request');
     $em = $this->getDoctrine()->getManager();
     if ($request->isXmlHttpRequest()) {
         $data = $request->get('data', null);
         $new_data = null;
         foreach ($data as $key => $value) {
             $values = explode('_', $value);
             $id = $values[2];
             $position = $values[0];
             $new_data[$key] = array('position' => $position, 'id' => $id);
             $new_pos[$key] = $position;
         }
         array_multisort($new_pos, SORT_ASC, $new_data);
         // get all locales
         $all_locales = $this->container->get('sfynx.auth.locale_manager')->getAllLocales();
         //
         foreach ($new_data as $key => $value) {
             if ($type == "widget") {
                 $entity = $em->getRepository("SfynxCmfBundle:Widget")->find($value['id']);
             } elseif ($type == "page") {
                 $entity = $value['id'];
             } else {
                 throw ControllerException::callAjaxOnlySupported('deleteajax');
             }
             foreach ($all_locales as $lang_page) {
                 if ($type == "widget") {
                     $this->container->get('pi_app_admin.manager.page')->cacheRefreshWidget($entity, $lang_page);
                 } elseif ($type == "page") {
                     $this->container->get('pi_app_admin.manager.page')->cacheRefreshPage($entity, $lang_page);
                 }
             }
         }
         // we disable all flash message
         $this->container->get('session')->getFlashBag()->clear();
         // we encode results
         $tab = array();
         $tab['id'] = '-1';
         $tab['error'] = '';
         $tab['fieldErrors'] = '';
         $tab['data'] = '';
         $response = new Response(json_encode($tab));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } else {
         throw ControllerException::callAjaxOnlySupported('deleteajax');
     }
 }
 /**
  * Admin Ajax action management of all blocks and widgets of a page
  * 
  * @Secure(roles="ROLE_EDITOR")
  * @return Response
  * @author Etienne de Longeaux <*****@*****.**>
  * @since  2012-05-04
  */
 public function urlmanagementAction()
 {
     $request = $this->container->get('request');
     if ($request->isXmlHttpRequest()) {
         $urls = null;
         //
         if ($request->query->has('id')) {
             $id = $request->query->get('id');
         } else {
             $id = null;
         }
         if ($request->query->has('type')) {
             $type = $request->query->get('type');
         } else {
             $type = null;
         }
         if ($request->query->has('routename')) {
             $routename = $request->query->get('routename');
         } else {
             $routename = "";
         }
         if ($request->query->has('action')) {
             $action = $request->query->get('action');
         } else {
             $action = "no";
         }
         // we get the page manager
         $pageManager = $this->get('pi_app_admin.manager.page');
         //
         if ($type == 'routename') {
             // we return the url result of the routename
             $urls[$action] = $this->get('sfynx.tool.route.factory')->getRoute($routename);
         } elseif ($type == 'page') {
             // we get the object Translation Page by route
             $page = $pageManager->setPageByRoute($routename);
             if ($page instanceof Page) {
                 $urls = $pageManager->getUrlByType('page', $page);
             } else {
                 $urls = $pageManager->getUrlByType('page');
             }
             // we get all the urls in order to the management of widgets.l
             $urls = $pageManager->getUrlByType('page', $page);
         } elseif ($type == 'block') {
             // we get the object block by id
             $block = $pageManager->getBlockById($id);
             // we get all the urls in order to the management of a block.
             $urls = $pageManager->getUrlByType('block', $block);
         } elseif ($type == 'widget') {
             // we get the object widget by id
             $widget = $pageManager->getWidgetById($id);
             // we get all the urls in order to the management of a widget.
             $urls = $pageManager->getUrlByType('widget', $widget);
         }
         // we return the desired url
         $values[0]['url'] = $urls[$action];
         $response = new Response(json_encode($values));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } else {
         throw ControllerException::callAjaxOnlySupported('urlmanagement');
     }
 }
 /**
  * archive BlocGeneral entities.
  *
  * @Route("/admin/gedmo/blocgeneral/archive", name="admin_gedmo_blocgeneral_archiventity_ajax")
  * @Secure(roles="ROLE_USER")
  * @return \Symfony\Component\HttpFoundation\Response
  *     
  * @access  public
  * @author Etienne de Longeaux <*****@*****.**>
  */
 public function archiveajaxAction()
 {
     $request = $this->container->get('request');
     $em = $this->getDoctrine()->getManager();
     if ($request->isXmlHttpRequest()) {
         $data = $request->get('data', null);
         $new_data = null;
         foreach ($data as $key => $value) {
             $values = explode('_', $value);
             $id = $values[2];
             $type = $values[1];
             $position = $values[0];
             $new_data[$key] = array('position' => $position, 'id' => $id, 'type' => $type);
             $new_pos[$key] = $position;
         }
         array_multisort($new_pos, SORT_ASC, $new_data);
         krsort($new_data);
         foreach ($new_data as $key => $value) {
             if ($value['type'] == "article") {
                 $entity = $em->getRepository("PluginsContentBundle:Article")->find($value['id']);
             } elseif ($value['type'] == "dossier") {
                 $entity = $em->getRepository("PluginsContentBundle:Diaporama")->find($value['id']);
             } elseif ($value['type'] == "page") {
                 $entity = $em->getRepository("PluginsContentBundle:Page")->find($value['id']);
             } elseif ($value['type'] == "test") {
                 $entity = $em->getRepository("PluginsContentBundle:Test")->find($value['id']);
             }
             $entity->setArchived(true);
             $entity->setEnabled(false);
             $entity->setArchiveAt(new \DateTime());
             $entity->getBlocgeneral()->setArchived(true);
             $entity->getBlocgeneral()->setEnabled(false);
             $entity->getBlocgeneral()->setArchiveAt(new \DateTime());
             if (method_exists($entity, 'setPosition')) {
                 $entity->setPosition(null);
             }
             $em->persist($entity);
             $em->flush();
         }
         $em->clear();
         // we disable all flash message
         $this->container->get('session')->clearFlashes();
         $tab = array();
         $tab['id'] = '-1';
         $tab['error'] = '';
         $tab['fieldErrors'] = '';
         $tab['data'] = '';
         $response = new Response(json_encode($tab));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } else {
         throw ControllerException::callAjaxOnlySupported('enabledajax');
     }
 }
 /**
  * get entities in ajax request for select form.
  *
  * @return Response
  * @access protected
  * @author Etienne de Longeaux <*****@*****.**>
  */
 protected function selectajaxQuery($pagination, $MaxResults, $keywords = null, $query = null, $locale = '', $only_enabled = true, $cacheQuery_hash = null)
 {
     $request = $this->container->get('request');
     $em = $this->getDoctrine()->getManager();
     //
     if (empty($locale)) {
         $locale = $this->container->get('request')->getLocale();
     }
     //
     if ($request->isXmlHttpRequest()) {
         if (!$query instanceof \Doctrine\DBAL\Query\QueryBuilder && !$query instanceof \Doctrine\ORM\QueryBuilder) {
             $query = $em->getRepository($this->_entityName)->getAllByCategory('', null, '', '', false);
         }
         if ($only_enabled) {
             $query->andWhere('a.enabled = 1');
         }
         // groupe by
         $query->groupBy('a.id');
         // autocompletion
         $array_params = array();
         if (is_array($keywords) && count($keywords) >= 1) {
             $i = 0;
             foreach ($keywords as $info) {
                 $is_trans = false;
                 if (isset($info['field_trans']) && !empty($info['field_trans'])) {
                     $is_trans = $info['field_trans'];
                     if (!isset($info['field_trans_name']) || empty($info['field_trans_name'])) {
                         $is_trans = false;
                     }
                 }
                 if ($is_trans && isset($info['field_trans_name']) && isset($info['field_value']) && !empty($info['field_value']) && isset($info['field_name']) && !empty($info['field_name'])) {
                     $current_encoding = mb_detect_encoding($info['field_value'], 'auto');
                     $info['field_value'] = iconv($current_encoding, 'UTF-8', $info['field_value']);
                     $info['field_value'] = PiStringManager::withoutaccent($info['field_value']);
                     $trans_name = $info['field_trans_name'];
                     $andModule_title = $query->expr()->andx();
                     $andModule_title->add($query->expr()->eq("LOWER({$trans_name}.locale)", "'{$locale}'"));
                     $andModule_title->add($query->expr()->eq("LOWER({$trans_name}.field)", "'" . $info['field_name'] . "'"));
                     //$andModule_title->add($query->expr()->like("LOWER({$trans_name}.content)", $query->expr()->literal('%'.strtolower(addslashes($info['field_value'])).'%')));
                     $andModule_title->add("LOWER({$trans_name}.content) LIKE :var1" . $i . "");
                     $array_params["var1" . $i] = '%' . strtolower($info['field_value']) . '%';
                     $andModule_id = $query->expr()->andx();
                     //$andModule_id->add($query->expr()->like('LOWER(a.id)', $query->expr()->literal('%'.strtolower(addslashes($info['field_value'])).'%')));
                     $andModule_id->add("LOWER(a.id) LIKE :var2" . $i . "");
                     $array_params["var2" . $i] = '%' . strtolower($info['field_value']) . '%';
                     $orModule = $query->expr()->orx();
                     $orModule->add($andModule_title);
                     $orModule->add($andModule_id);
                     $query->andWhere($orModule);
                 } elseif (!$is_trans && isset($info['field_value']) && !empty($info['field_value']) && isset($info['field_name']) && !empty($info['field_name'])) {
                     $current_encoding = mb_detect_encoding($info['field_value'], 'auto');
                     $info['field_value'] = iconv($current_encoding, 'UTF-8', $info['field_value']);
                     $info['field_value'] = PiStringManager::withoutaccent($info['field_value']);
                     //$query->add($query->expr()->like('LOWER('.$info['field_name'].')', $query->expr()->literal('%'.strtolower(addslashes($info['field_value'])).'%')));
                     $query->add("LOWER(" . $info['field_name'] . ") LIKE :var3" . $i . "");
                     $array_params["var3" . $i] = '%' . strtolower($info['field_value']) . '%';
                 }
                 $i++;
             }
             $query->setParameters($array_params);
         }
         // pagination
         if (!is_null($pagination)) {
             $query->setFirstResult((intVal($pagination) - 1) * intVal($MaxResults));
             $query->setMaxResults(intVal($MaxResults));
             //$query_sql = $query->getQuery()->getSql();
             //var_dump($query_sql);
         }
         //
         if (is_null($cacheQuery_hash)) {
             $query = $query->getQuery();
         } elseif (is_array($cacheQuery_hash)) {
             // we define all options
             if (!isset($cacheQuery_hash['time'])) {
                 $cacheQuery_hash['time'] = 3600;
             }
             if (!isset($cacheQuery_hash['mode'])) {
                 $cacheQuery_hash['mode'] = 3;
             }
             // \Doctrine\ORM\Cache::MODE_NORMAL;
             if (!isset($cacheQuery_hash['setCacheable'])) {
                 $cacheQuery_hash['setCacheable'] = true;
             }
             if (!isset($cacheQuery_hash['input_hash'])) {
                 $cacheQuery_hash['input_hash'] = '';
             }
             if (!isset($cacheQuery_hash['namespace'])) {
                 $cacheQuery_hash['namespace'] = '';
             }
             // we set the query result
             $query = $em->getRepository($this->_entityName)->cacheQuery($query->getQuery(), $cacheQuery_hash['time'], $cacheQuery_hash['mode'], $cacheQuery_hash['setCacheable'], $cacheQuery_hash['namespace'], $cacheQuery_hash['input_hash']);
         }
         // result
         $entities = $em->getRepository($this->_entityName)->findTranslationsByQuery($locale, $query, 'object', false);
         $tab = $this->renderselectajaxQuery($entities, $locale);
         // response
         $response = new Response(json_encode($tab));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } else {
         throw ControllerException::callAjaxOnlySupported(' selectajax');
     }
 }
 /**
  * Disable Langue  entities.
  *
  * @Route("/admin/langue/disable", name="admin_langue_disablentity_ajax")
  * @Secure(roles="ROLE_EDITOR")
  * @return \Symfony\Component\HttpFoundation\Response
  * 
  * @access  public
  * @author Etienne de Longeaux <*****@*****.**>
  */
 public function disableajaxAction()
 {
     $request = $this->container->get('request');
     $em = $this->getDoctrine()->getManager();
     if ($request->isXmlHttpRequest()) {
         $data = $request->get('data', null);
         foreach ($data as $key => $id) {
             $entity = $em->getRepository($this->_entityName)->find($id);
             $entity->setEnabled(false);
             $em->persist($entity);
             $em->flush();
         }
         $em->clear();
         // we disable all flash message
         $this->container->get('session')->getFlashBag()->clear();
         $tab = array();
         $tab['id'] = '-1';
         $tab['error'] = '';
         $tab['fieldErrors'] = '';
         $tab['data'] = '';
         $response = new Response(json_encode($tab));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } else {
         throw ControllerException::callAjaxOnlySupported('disableajax');
     }
 }
 /**
  * Move up/down widget action
  *
  * @Secure(roles="ROLE_EDITOR")
  * @return \Symfony\Component\HttpFoundation\Response
  *
  * @author Etienne de Longeaux <*****@*****.**>
  * @since 2012-07-05
  */
 public function moveajaxAction()
 {
     $request = $this->container->get('request');
     $em = $this->getDoctrine()->getManager();
     if ($request->isXmlHttpRequest()) {
         if ($request->query->has('id')) {
             $id = $request->query->get('id');
         } else {
             $id = null;
         }
         if ($request->query->has('type')) {
             $type = $request->query->get('type');
         } else {
             $type = null;
         }
         if (!is_null($id) && !is_null($type) && in_array($type, array('up', 'down'))) {
             $entity_widget = $em->getRepository('SfynxCmfBundle:Widget')->find($id);
             $entity_block = $entity_widget->getBlock();
             $entity_widget_pos = $entity_widget->getPosition();
             $break = false;
             if (is_null($entity_widget_pos)) {
                 $entity_widget_pos = 1;
                 $entity_widget->setPosition($entity_widget_pos);
                 $em->persist($entity_widget);
                 $em->flush();
             }
             if ($type == 'up') {
                 $all_widget_block = $em->getRepository('SfynxCmfBundle:Widget')->getAllWidgetsByBlock($entity_block->getId(), "DESC")->getResult();
                 //$entity_block->getWidgets();
                 foreach ($all_widget_block as $key => $widget) {
                     $widg_pos = $widget->getPosition();
                     if (!$break && $widg_pos < $entity_widget_pos) {
                         $widget->setPosition($entity_widget_pos);
                         $em->persist($widget);
                         $entity_widget->setPosition($widg_pos);
                         $em->persist($entity_widget);
                         $em->flush();
                         $break = true;
                     }
                 }
             } elseif ($type == 'down') {
                 $all_widget_block = $em->getRepository('SfynxCmfBundle:Widget')->getAllWidgetsByBlock($entity_block->getId(), "ASC")->getResult();
                 //$entity_block->getWidgets();
                 foreach ($all_widget_block as $key => $widget) {
                     $widg_pos = $widget->getPosition();
                     if (!$break && $widg_pos > $entity_widget_pos) {
                         $widget->setPosition($entity_widget_pos);
                         $em->persist($widget);
                         $entity_widget->setPosition($widg_pos);
                         $em->persist($entity_widget);
                         $em->flush();
                         $break = true;
                     }
                 }
             }
         }
         // we return the desired url
         $values[0]['request'] = true;
         $response = new Response(json_encode($values));
         $response->headers->set('Content-Type', 'application/json');
         return $response;
     } else {
         throw ControllerException::callAjaxOnlySupported('moveajax');
     }
 }