src/Service/Ajoutaides.php line 119

Open in your IDE?
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 ff=unix fenc=utf8: */
  3. namespace App\Service;
  4. use App\ClassTheatre\asides;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\Validator\Constraints\Length;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. use Symfony\Component\Validator\Constraints\Url;
  13. use TheatreCore\Entity\Aidecontacts;
  14. use TheatreCore\Entity\Aides;
  15. use TheatreCore\Entity\Contacts;
  16. /**
  17.  * Ajout/modification d'un dossier de spectacle
  18.  *
  19.  * PHP version 5
  20.  *
  21.  * LICENSE: Ce programme est un logiciel libre distribue sous licence GNU/GPL
  22.  *
  23.  * @author     Boussad Sadadou <boussad@theatre-contemporain.net>
  24.  * @copyright  2008 C.R.I.S.
  25.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  26.  * @version    0.1.0
  27.  * @link       https://www.theatre-contemporain.net
  28.  */
  29. /**
  30.  * Contrôleur standard
  31.  */
  32. class Ajoutaides
  33. {
  34.     //L'object courant que l'on modifie
  35.     private $idobject;
  36.     //Le type de catégorie que l'on souhaite ajouter
  37.     private $idaidecontact;
  38.     //Les données de l'objet courant que l'on modifie
  39.     private $data_object;
  40.     //La table courante que l'on modifie (spectacle, textes etc.)
  41.     private $table_object;
  42.     //datas
  43.     private $datas;
  44.     //formulaires
  45.     private $form;
  46.     //vue
  47.     private $view;
  48.     // paramètres
  49.     private $params;
  50.     //Utilisateur actif
  51.     private $idextranetuser;
  52.     //Contact sélectionné
  53.     private $idcontact;
  54.     //Type d'aide sélectionné
  55.     private $typeAide;
  56.     //l'url clean de l'object
  57.     private $url_clean;
  58.     //Gestion des asides
  59.     private $asides;
  60.     private $utilsEdit;
  61.     public function __construct(
  62.         EntityManagerInterface $em,
  63.         UtilsEdit $utilsEdit,
  64.         SessionInterface $session,
  65.         array &$context,
  66.         array $params = []
  67.     )
  68.     {
  69.         $this->session $session;
  70.         $this->em $em;
  71.         // le formulaire standard
  72.         $this->formFactory = &$context['formFactory'];
  73.         $this->utilsEdit $utilsEdit;
  74.         // la vue
  75.         $this->context = &$context;
  76.         $this->view = &$context['view'];
  77.         //asides
  78.         $this->asides = new asides();
  79.         // action possible
  80.         if(!empty($params['possible_do'])) {
  81.             $params['do'] = $this->getDoAction($params['possible_do']);
  82.         }
  83.         // les paramètres en plus
  84.         $this->params $params;
  85.         //On initie les valeur suivant les options de l'utilisateur
  86.         $this->setObject();
  87.         //valeur par defaut
  88.         $this->idaidecontact null;
  89.         $this->idcontact null;
  90.         if($idaidecontact $this->session->get('ajoutaides[idaidecontact]')){
  91.             $this->idaidecontact $idaidecontact;
  92.         }
  93.         if($idcontact $this->session->get('ajoutaides[idcontact]')){
  94.             $this->idcontact $idcontact;
  95.         }
  96.         $this->getAjoutspectacleCommon();
  97.     }
  98.     /** Etape initial - génération du formulaire de de l'aide
  99.      *  Fonds d'insertion professionelle, aide à la création
  100.      *
  101.      */
  102.     public function getFormTypeAides($request null)
  103.     {
  104.         $type_object $this->table_object.'_aide';
  105.         $listes_types $this->em->getRepository(Aidecontacts::class)->getListAidecontacts(array(
  106.             'from' => [$type_object],
  107.             'aide_is_null' => true,
  108.             'where' => ['AND '.$type_object.'.idaide = aides.idaide'],
  109.             'group' => ['aidecontacts' => 'title'],
  110.             'special' => array(
  111.                 'no_order' => true,
  112.                 'aides' => array(
  113.                     'type_aides' => $this->typeAide
  114.                 )
  115.             ),
  116.             'order' => ['aides' => 'title'],
  117.             'direction' => 'ASC'
  118.         ));
  119.         foreach($listes_types AS $k => $v){
  120.             if(!empty($v['nameview'])){
  121.                 $datas_select[$v['idaidecontact']] = $v['title'];
  122.             }
  123.         }
  124.         $this->formFactory->add('idtypecategorie'ChoiceType::class, [
  125.             'label' => 'Choisir une catégorie',
  126.             'choices' => array_flip($datas_select),
  127.             'attr' => [
  128.                 'class'=>'select_type'
  129.             ]
  130.         ]);
  131.         $this->formFactory->add('validation'SubmitType::class, [
  132.             'label' => 'Commencer la procédure',
  133.             'attr' => [
  134.                 'class'=>'boutform boutbig nextstep'
  135.             ]
  136.         ]);
  137.         $this->context['form'] = $this->formFactory->getForm();
  138.         $this->context['form']->handleRequest($request);
  139.     }
  140.     public function validateAidesForm(){
  141.         return ($this->context['form']->isSubmitted() && $this->context['form']->isValid()) ? true false;
  142.     }
  143.     /** Etape initial - sauvegarde du formulaire de de l'aide
  144.      *
  145.      */
  146.     public function saveAides()
  147.     {
  148.         $idaidecontact $this->context['form']->get('idtypecategorie')->getViewData();
  149.         // mettre les idtypecategorie en session
  150.         $this->session->set('ajoutaides[idaidecontact]'$idaidecontact);
  151.     }
  152.     /** Etape 1 - génération du formulaire de sélection des contacts
  153.      *
  154.      */
  155.     public function getFormContacts($request null)
  156.     {
  157.         if(is_null($this->idaidecontact)){
  158.             return false;
  159.         }
  160.         $params = [
  161.             'special' => array(
  162.                 'idaidecontact' => $this->idaidecontact
  163.             ),
  164.             'group' => array(
  165.                 'contacts' => 'idcontact'
  166.             ),
  167.             'limit' => ['all'],
  168.             'order' => ['contacts' => 'organisation'],
  169.             'direction' => 'ASC'
  170.         ];
  171.         $aides_listes $this->em->getRepository(Aides::class)->getListAides($params);
  172.         foreach($aides_listes AS $value){
  173.             if(!empty($value['contact'])){
  174.                 $contact $value['contact'];
  175.                 $datas_select[$contact['idcontact']] = $contact['organisation'].' '.$contact['nomSuite'];
  176.             }
  177.         }
  178.         $this->formFactory->add('contacts_list'ChoiceType::class, [
  179.             'label' => 'Choisir la structure',
  180.             'choices' => array_flip($datas_select),
  181.             'attr' => [
  182.                 'class'=>'select_type'
  183.             ]
  184.         ]);
  185.         $this->formFactory->add('validation'SubmitType::class, [
  186.             'label' => 'Étape suivante',
  187.             'attr' => [
  188.                 'class'=>'boutform boutbig nextstep'
  189.             ]
  190.         ]);
  191.         $this->context['form'] = $this->formFactory->getForm();
  192.         $this->context['form']->handleRequest($request);
  193.         return true;
  194.     }
  195.     /** Etape 1 - validation du formulaire de sélection des contacts
  196.      *
  197.      */
  198.     public function validateContactsForm(){
  199.         return ($this->context['form']->isSubmitted() && $this->context['form']->isValid()) ? true false;
  200.     }
  201.     /** Etape 1 - sauvegarde du formulaire de sélection des contacts
  202.      *
  203.      */
  204.     public function saveContacts()
  205.     {
  206.         $idcontact $this->context['form']->get('contacts_list')->getViewData();
  207.         // mettre les contacts_list en sessions
  208.         $this->session->set('ajoutaides[idcontact]'$idcontact);
  209.     }
  210.     /** Etape 2 - génération du formulaire de création de l'aide
  211.      *
  212.      */
  213.     public function getFormAides($request null){
  214.         if(is_null($this->idcontact) && is_null($this->idaidecontact)){
  215.             return false;
  216.         }
  217.         $contact $this->em->getRepository(Contacts::class);
  218.         $this->view->contact $contact->getInfosContact($this->idcontact);
  219.         $params = array(
  220.             'special' => array(
  221.                 'aides' => [
  222.                     'idcontacts' => [$this->idcontact]
  223.                 ],
  224.                 'idaidecontact' => $this->idaidecontact
  225.                 // 'type_aides' => $this->idaidecontact
  226.             )
  227.         );
  228.         $datas_select = array();
  229.         $type_list $this->em->getRepository(Aides::class)->getListAides($params); //idtypespectacle
  230.         foreach($type_list AS $k => $v){
  231.             if(!empty($v['nameview'])){
  232.                 $datas_select[$v['idaide']] = $v['nameview'];
  233.             }
  234.             else{
  235.                 $datas_select[$v['idaide']] = 'Pas de catégorie à préciser';
  236.             }
  237.         }
  238.         $this->formFactory->add('idtypespectacle'ChoiceType::class, [
  239.             'label' => 'Choisir une catégorie',
  240.             'choices' => array_flip($datas_select),
  241.             'attr' => [
  242.                 'class'=>'select_type'
  243.             ]
  244.         ]);
  245.         $this->formFactory->add('url'TextType::class, [
  246.             'label' => 'Plus d\'infos - lien facultatif vers la page de la structure (seulement si infos complémentaires)',
  247.             'constraints' => [
  248.                 New Url([
  249.                     'protocols' => ['http''https'],
  250.                     'message' => 'lien facultatif : l\'adresse du site semble incorrecte (elle doit commencer par https://)'
  251.                 ]),
  252.             ],
  253.             'attr' => [
  254.                 'size'=> 50,
  255.                 'placeholder' => '(doit commencer par http:// ou https://)'
  256.             ],
  257.             'required' => false
  258.         ]);
  259.         $this->formFactory->add('comments'TextType::class, [
  260.             'label' => 'Infos non publiques pour l\'administrateur du site',
  261.             'attr' => [
  262.                 'size'=> 50
  263.             ],
  264.             'required' => false
  265.         ]);
  266.         $this->formFactory->add('date_start_year'TextType::class, [
  267.             'label' => 'Date de début (année obligatoire)',
  268.             'constraints' => [
  269.                 new NotBlank([
  270.                     'message' => 'Veuillez préciser l\'année de début'
  271.                 ]),
  272.                 new Length([
  273.                     'min' => 4,
  274.                     'max' => 4,
  275.                     'exactMessage' => 'Date de début : veuillez préciser une année correcte (YYYY)'
  276.                 ])
  277.             ],
  278.             'attr' => [
  279.                 'size'=>'4',
  280.                 'maxlength'=>'4',
  281.                 'placeholder' => 'Année''class'=>'input-group-field'
  282.             ]
  283.         ]);
  284.         $this->formFactory->add('date_start_month'TextType::class, [
  285.             'attr' => [
  286.                 'size'=>'2',
  287.                 'maxlength'=>'2',
  288.                 'placeholder' => 'Mois',
  289.                 'class'=>'input-group-field'
  290.             ],
  291.             'required' => false
  292.         ]);
  293.         $this->formFactory->add('date_start_day'TextType::class, [
  294.             'attr' => [
  295.                 'size'=>'2',
  296.                 'maxlength'=>'2',
  297.                 'placeholder' => 'Jour',
  298.                 'class'=>'input-group-field'
  299.             ],
  300.             'required' => false
  301.         ]);
  302.         $this->formFactory->add('date_end_year'TextType::class, [
  303.             'label' => 'Date de fin',
  304.             'attr' => [
  305.                 'size'=>'4',
  306.                 'maxlength'=>'4',
  307.                 'placeholder' => 'Année',
  308.                 'class'=>'input-group-field'
  309.             ],
  310.             'required' => false
  311.         ]);
  312.         $this->formFactory->add('date_end_month'TextType::class, [
  313.             'attr' => [
  314.                 'size'=>'2',
  315.                 'maxlength'=>'2',
  316.                 'placeholder' => 'Mois',
  317.                 'class'=>'input-group-field'
  318.             ],
  319.             'required' => false
  320.         ]);
  321.         $this->formFactory->add('date_end_day'TextType::class, [
  322.             'attr' => [
  323.                 'size'=>'2',
  324.                 'maxlength'=>'2',
  325.                 'placeholder' => 'Jour',
  326.                 'class'=>'input-group-field'
  327.             ],
  328.             'required' => false
  329.         ]);
  330.         $this->formFactory->add('validation'SubmitType::class, [
  331.             'label' => 'Enregistrer les informations',
  332.             'attr' => [
  333.                 'class'=>'boutform boutbig nextstep'
  334.             ]
  335.         ]);
  336.         $this->context['form'] = $this->formFactory->getForm();
  337.         $this->context['form']->handleRequest($request);
  338.         return true;
  339.     }
  340.     /** Etape 2 - validation du formulaire de création de l'aide
  341.      *
  342.      */
  343.     public function getValidAides(){
  344.         return ($this->context['form']->isSubmitted() && $this->context['form']->isValid()) ? true false;
  345.     }
  346.     /** Etape 2 - sauvegarde du formulaire de création de l'aide
  347.      *
  348.      */
  349.     public function getSaveAides(){
  350.         $id null;
  351.         $obj $this->em->getRepository('TheatreCore\Entity\\'.ucfirst($this->table_object).'Aide');
  352.         $entityName 'TheatreCore\Entity\\'.ucfirst($this->table_object).'Aide';
  353.         $EntityAide = new $entityName();
  354.         //tableau pour insertion dans la table texts
  355. //        $values = array(
  356. //            'idaide' => $this->context['form']->get('idtypespectacle')->getViewData(),
  357. //
  358. //            'comments' => $this->context['form']->get('comments')->getViewData(),
  359. //            'url' => $this->context['form']->get('url')->getViewData(),
  360. //        );
  361.         $aideSelected $this->context['form']->get('idtypespectacle')->getData();
  362.         $aideobj $this->em->getRepository(Aides::class)->find($aideSelected);
  363.         $EntityAide->setIdaide($aideobj);
  364.         $EntityAide->setComments($this->context['form']->get('comments')->getData());
  365.         $EntityAide->setUrl($this->context['form']->get('url')->getData());
  366.         $key =  'setId'.$this->table_object;
  367.         $keyGet =  'getId'.$this->table_object.'aide';
  368.         $spectacleobj $this->em->getRepository('TheatreCore\Entity\\'.ucfirst($this->table_object).'s')->find($this->idobject);
  369.         $EntityAide->$key($spectacleobj);
  370.         foreach(array('date_start','date_end') as $f) {
  371.             foreach(array('year','month','day') as $t) {
  372.                 $field $this->context['form']->get($f.'_'.$t)->getData();
  373.                 if(empty($field)) {
  374.                     $field '00';
  375.                 }
  376.                 $date_format[$t] = $field;
  377.             }
  378.             $values[$f] = join('-'$date_format).' 00:00:00';
  379.         }
  380.         $EntityAide->setDateStart(new \DateTime($values["date_start"]));
  381.         $EntityAide->setDateEnd(new \DateTime($values["date_end"]));
  382.         // timestamp creation
  383.         $EntityAide->setDateinsert(new \DateTime());
  384.         // si on commit ou si on n'est pas admin ne pas mettre a jour le champ publish
  385.         if(!empty($rights['isadmin'])) {
  386.             // publié ou pas ? validé ou pas
  387.             foreach(array('publish') as $f) {
  388.                 $val $this->context['form']->get($f)->getData();
  389.                 if(empty($val))  {
  390.                     $values[$f] = 0;
  391.                 } else {
  392.                     $values[$f] = $val;
  393.                 }
  394.             }
  395.         }
  396.         else{
  397.             $values['publish'] = 0;
  398.         }
  399.         $EntityAide->setPublish($values['publish']);
  400.         // commencer la transaction avec la variable error null
  401.         $error null;
  402.         // go !
  403.         $em $this->em;
  404.         $em->beginTransaction();
  405.         try {
  406.             //... do some work
  407.             $em->persist($EntityAide);
  408.             $em->flush();
  409.             $res $EntityAide->$keyGet();
  410.             $reponse['insert_ok'] = "ok"//$tr->get('update_ok');
  411.         } catch (Exception $e) {
  412.             $em->getConnection()->rollBack();
  413.             throw $e;
  414.         }
  415.         // l'identifiant de la nouvelle fiche (mode creation)
  416.         if(empty($id)) {
  417.             $newid $id $res// on active l'id et on precise que c'est un nouvel id
  418.         } else {
  419.             $newid null;
  420.         }
  421.         // // si pas d'erreur, on commit la transaction
  422.         if (!$error) {
  423.             if(!$em->getConnection()->commit()) {
  424.                 $error[] = $this->context['tr']->trans('transaction_no_commit');
  425.             }
  426.         } else {
  427.              $error[] = $this->context['tr']->trans('transaction_no_commit_echec');
  428.         }
  429.         // // // pour loguer les actions
  430.         if(!$error) {
  431.             $this->utilsEdit->logAction($id,$newid,$this->table_object."_aide",$this->context['session']->getIdextranetuser());
  432.         }
  433.         // // // pour une creation, il faut reselectionner l'enregistrement et enregistrer l'url_clean
  434.         if(!empty($newid) && !$error) {
  435. //            $this->utilsEdit->createUrlClean($newid,$EntityAide,"aidecontacts");
  436.         }
  437.     }
  438.     //Gestion de la prévisualistion (nécessaire ?)
  439.     public function getValidation(){
  440.         $this->session->remove('ajoutaides[idcontact]');
  441.         $this->session->remove('ajoutaides[idaidecontact]');
  442.     }
  443.     // $params = array(
  444.     //     'object' => array(
  445.     //         'type' => 'specacles'
  446.     //         'value' => '';
  447.     //     )
  448.     // )
  449.     private function setObject(){
  450.         $params $this->params;
  451.         if(empty($params['object'])){
  452.             return false;
  453.         }
  454.         if(empty($params['aides'])){
  455.             return false;
  456.         }
  457.         $arrays_objects = ['spectacle''text''person'];
  458.         if(in_array($params['object']['type'], $arrays_objects) && !empty($object_value $params['object']['value'])){
  459.             $this->context['view']->{'object_'.$params['object']['type']}  = true;
  460.             $this->table_object $params['object']['type'];
  461.             $this->idobject = (int)$object_value;
  462.             if(!empty($params['aides']['typeaide'])){
  463.                 $this->typeAide = (int)$params['aides']['typeaide'];
  464.                 $this->context['view']->{'type_'.$this->typeAide} = true;
  465.             }
  466.             if(!empty($params['url'])){
  467.                 $this->context['view']->url_clean_aide $params['url'];
  468.             }
  469.             if(!empty($params['aides']['url_clean'])){
  470.                 $this->url_clean = (int)$params['aides']['url_clean'];
  471.             }
  472.             return true;
  473.         }
  474.         else{
  475.             return false;
  476.         }
  477.     }
  478.     // {{{ getSteps()
  479.     /** etapes
  480.      *
  481.      */
  482.     public function getSteps($title_page,$num_step) {
  483.         if(isset($num_step)) {
  484.             $steps = array(
  485.                 => array('nom' => 'Structures'),
  486.                 => array('nom' => 'Informations'),
  487.                 => array('nom' => 'Validation'),
  488.             );
  489.             // retourner le titre de la page ou un array
  490.             if($num_step!='all') {
  491.                 if (in_array($num_steparray_keys($steps))) {
  492.                     $steps[$num_step]['statut'] = ' step-on';
  493.                     $this->view->steps $steps;
  494.                     $title_page $steps[$num_step]['nom'].' (étape '.$num_step.') - '.$title_page;
  495.                 }
  496.             }
  497.             // marquer les steps infériereure
  498.             foreach($steps as $k=>$s) {
  499.                 if($k==$num_step) {
  500.                     $steps[$k]['statut'] = ' step-on';
  501.                 } elseif($k<$num_step) {
  502.                     $steps[$k]['statut'] = ' step-done';
  503.                 }
  504.                 // if($this->checkStep($k)) {
  505.                 //     $steps[$k]['statut_step'] = 'ok';
  506.                 // } else {
  507.                 //     $steps[$k]['statut_step'] = 'no';
  508.                 // }
  509.             }
  510.             // retourner le titre de la page ou un array
  511.             if($num_step!='all') {
  512.                 $this->view->steps $steps;
  513.                 return $title_page;
  514.             } else {
  515.                 return $steps;
  516.             }
  517.         }
  518.     }
  519.     // }}}
  520.     // {{{ getAjoutspectacleCommon()
  521.     /** Elements communs aux vues
  522.      *
  523.      */
  524.     public function getAjoutspectacleCommon() {
  525.         // js en plus
  526.         $this->view->js_more = array('ajax.relation.js');
  527.     }
  528. }