Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.79 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\CabinetBundle\Controller;
  4.  
  5. use App\CoreBundle\Entity\Anketa;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Component\HttpFoundation\Request;
  9.  
  10. class AnketaController extends Controller
  11. {
  12.     /** @Template() */
  13.     public function indexAction(Request $request)
  14.     {
  15.         $anketa=$this->get('repo.anketa')->find($this->getUser()->getId());
  16.         if (!$anketa) return $this->redirect($this->generateUrl('cabinet_anketa_add'));
  17.  
  18.         return array(
  19.             'anketa'=>$anketa
  20.         );
  21.     }
  22.  
  23.     /** @Template() */
  24.     public function editAction(Request $request)
  25.     {
  26.         $anketa=$this->get('repo.anketa')->find($this->getUser()->getId());
  27.         if (!$anketa) return $this->redirect($this->generateUrl('cabinet_anketa_add'));
  28.         $form=$this->createForm($this->get('cabinet.form.anketa'), $anketa, array(
  29.             'action' => $this->generateUrl($request->get('_route')),
  30.             'method' => 'POST',
  31.             'attr' => array(
  32.                 'class' => 'form-horizontal',
  33.                 'id' => 'form'
  34.             )
  35.         ));
  36.         if ($request->getMethod() == 'POST') {
  37.             $form->submit($request);
  38.             if ($form->isValid()) {
  39.                 try {
  40.                     $town=$this->get('repo.town')->find($request->get('town'));
  41.                     $region=$this->get('repo.region')->find($request->get('region'));
  42.                     $country=$this->get('repo.country')->find($request->get('country'));
  43.                     $anketa->setTown($town);
  44.                     $anketa->setRegion($region);
  45.                     $anketa->setCountry($country);
  46.                     $anketa->setUser($this->getUser());
  47.                     $this->getDoctrine()->getManager()->persist($anketa);
  48.                     $this->getDoctrine()->getManager()->flush();
  49.                     $this->getDoctrine()->getManager()->clear();
  50.                     $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('cabinet.flash.saved'));
  51.                 } catch (\Exception $e) {
  52.                     $this->get('session')->getFlashBag()->add('danger', $this->get('translator')->trans('cabinet.flash.saved_error'));
  53.                 }
  54.  
  55.                 return $this->redirect($this->generateUrl('cabinet_anketa_index'));
  56.             }
  57.         }
  58.  
  59.         return array(
  60.             'form'=>$form->createView(),
  61.             'town'=>$anketa->getTown(),
  62.             'countries'=>$this->get('repo.country')->findAll()
  63.         );
  64.     }
  65.  
  66.     /** @Template() */
  67.     public function addAction(Request $request)
  68.     {
  69.         $anketa=$this->get('repo.anketa')->find($this->getUser()->getId());
  70.         if ($anketa instanceof Anketa) return $this->redirect($this->generateUrl('cabinet_anketa_edit'));
  71.         $anketa=$this->get('entity.anketa');
  72.         $form=$this->createForm($this->get('cabinet.form.anketa'), $anketa, array(
  73.             'action' => $this->generateUrl($request->get('_route')),
  74.             'method' => 'POST',
  75.             'attr' => array(
  76.                 'class' => 'form-horizontal',
  77.                 'id' => 'form'
  78.             )
  79.         ));
  80.         if ($request->getMethod() == 'POST') {
  81.             $form->submit($request);
  82.             if ($form->isValid()) {
  83.                 try {
  84.                     $town=$this->get('repo.town')->find($request->get('town'));
  85.                     $region=$this->get('repo.region')->find($request->get('region'));
  86.                     $country=$this->get('repo.country')->find($request->get('country'));
  87.                     $anketa->setTown($town);
  88.                     $anketa->setRegion($region);
  89.                     $anketa->setCountry($country);
  90.                     $anketa->setUser($this->getUser());
  91.                     $this->getDoctrine()->getManager()->persist($anketa);
  92.                     $this->getDoctrine()->getManager()->flush();
  93.                     $this->getDoctrine()->getManager()->clear();
  94.                     $this->get('session')->getFlashBag()->add('success', $this->get('translator')->trans('cabinet.flash.saved'));
  95.                 } catch (\Exception $e) {
  96.                     $this->get('session')->getFlashBag()->add('danger', $this->get('translator')->trans('cabinet.flash.saved_error'));
  97.                 }
  98.  
  99.                 return $this->redirect($this->generateUrl('cabinet_anketa_index'));
  100.             }
  101.         }
  102.  
  103.         return array(
  104.             'form'=>$form->createView(),
  105.             'countries'=>$this->get('repo.country')->findAll()
  106.         );
  107.     }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. <script type="text/javascript">
  114.         $('#save').click(function () {
  115.             $('#save').attr('disabled', 'disabled');
  116.             $('#gbox_table_user').showLoading();
  117.             $.getJSON( Routing.generate('back_default_setUsersByProgram', {'id':{{ app.request.get('id') }}, 'users_ids': idsOfSelectedRows.join() }), function( data ) {
  118.                 $('#save').removeAttr('disabled');
  119.                 $('#gbox_table_user').hideLoading();
  120.                 noty({type: 'success', layout: 'topRight', timeout: 5000, text: '{{ 'flash.saved'|trans }}'});
  121.             });
  122.         });
  123.         var idsOfSelectedRows=[{{ users }}];
  124.  
  125.         function updateIdsOfSelectedRows(id, isSelected){
  126.             var index = $.inArray(parseInt(id), idsOfSelectedRows);
  127.             if (!isSelected && index >= 0) {
  128.                 idsOfSelectedRows.splice(index, 1); // remove id from the list
  129.             } else if (index < 0) {
  130.                 idsOfSelectedRows.push(parseInt(id));
  131.             }
  132.         }
  133.  
  134.         $("#table_user").jqGrid({
  135.             url: Routing.generate('back_default_getUsers'),
  136.             datatype: "json",
  137.             colNames: [
  138.                 '{{ 'back.page.programConfig.user.grid.username'|trans }}',
  139.                 '{{ 'back.page.programConfig.user.grid.firstname'|trans }}',
  140.                 '{{ 'back.page.programConfig.user.grid.lastname'|trans }}'
  141.             ],
  142.             colModel: [
  143.                 {
  144.                     name: 'username',
  145.                     index: 'username',
  146.                     searchoptions:{
  147.                         sopt:['eq','ne','bw','cn']
  148.                     }
  149.                 },
  150.                 {
  151.                     name: 'firstname',
  152.                     index: 'firstname',
  153.                     searchoptions:{
  154.                         sopt:['eq','ne','bw','cn']
  155.                     }
  156.                 },
  157.                 {
  158.                     name: 'lastname',
  159.                     index: 'lastname',
  160.                     searchoptions:{
  161.                         sopt:['eq','ne','bw','cn']
  162.                     }
  163.                 }
  164.             ],
  165.             rowNum: 30,
  166.             pager: '#pager_user',
  167.             multiselect: true,
  168.             rownumbers: true,
  169.             rowList:[ 10, 30, 50, 100],
  170.             onSelectAll: function (aRowids, isSelected) {
  171.                 var i, count, id;
  172.                 for (i = 0, count = aRowids.length; i < count; i++) {
  173.                     id = aRowids[i];
  174.                     updateIdsOfSelectedRows(id, isSelected);
  175.                 }
  176.             },
  177.             onSelectRow: function(id, isSelected){
  178.                 updateIdsOfSelectedRows(id, isSelected);
  179.             },
  180.             loadComplete: function () {
  181.                 var $this = $(this), i, count;
  182.                 for (i = 0, count = idsOfSelectedRows.length; i < count; i++) {
  183.                     $this.jqGrid('setSelection', idsOfSelectedRows[i], false);
  184.                 }
  185.             },
  186.             caption: "{{ 'back.page.programConfig.user.grid.title'|trans }}"
  187.         }).navGrid('#pager_user',
  188.                 {edit:false, add:false, del:false, view:false}, {}, {}, {}, {/*closeAfterSearch:true*/}
  189.         );
  190.  
  191.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement