<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 ff=unix fenc=utf8: */
namespace App\Service;
use App\ClassTheatre\asides;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Url;
use TheatreCore\Entity\Aidecontacts;
use TheatreCore\Entity\Aides;
use TheatreCore\Entity\Contacts;
/**
* Ajout/modification d'un dossier de spectacle
*
* PHP version 5
*
* LICENSE: Ce programme est un logiciel libre distribue sous licence GNU/GPL
*
* @author Boussad Sadadou <boussad@theatre-contemporain.net>
* @copyright 2008 C.R.I.S.
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version 0.1.0
* @link https://www.theatre-contemporain.net
*/
/**
* Contrôleur standard
*/
class Ajoutaides
{
//L'object courant que l'on modifie
private $idobject;
//Le type de catégorie que l'on souhaite ajouter
private $idaidecontact;
//Les données de l'objet courant que l'on modifie
private $data_object;
//La table courante que l'on modifie (spectacle, textes etc.)
private $table_object;
//datas
private $datas;
//formulaires
private $form;
//vue
private $view;
// paramètres
private $params;
//Utilisateur actif
private $idextranetuser;
//Contact sélectionné
private $idcontact;
//Type d'aide sélectionné
private $typeAide;
//l'url clean de l'object
private $url_clean;
//Gestion des asides
private $asides;
private $utilsEdit;
public function __construct(
EntityManagerInterface $em,
UtilsEdit $utilsEdit,
SessionInterface $session,
array &$context,
array $params = []
)
{
$this->session = $session;
$this->em = $em;
// le formulaire standard
$this->formFactory = &$context['formFactory'];
$this->utilsEdit = $utilsEdit;
// la vue
$this->context = &$context;
$this->view = &$context['view'];
//asides
$this->asides = new asides();
// action possible
if(!empty($params['possible_do'])) {
$params['do'] = $this->getDoAction($params['possible_do']);
}
// les paramètres en plus
$this->params = $params;
//On initie les valeur suivant les options de l'utilisateur
$this->setObject();
//valeur par defaut
$this->idaidecontact = null;
$this->idcontact = null;
if($idaidecontact = $this->session->get('ajoutaides[idaidecontact]')){
$this->idaidecontact = $idaidecontact;
}
if($idcontact = $this->session->get('ajoutaides[idcontact]')){
$this->idcontact = $idcontact;
}
$this->getAjoutspectacleCommon();
}
/** Etape initial - génération du formulaire de de l'aide
* Fonds d'insertion professionelle, aide à la création
*
*/
public function getFormTypeAides($request = null)
{
$type_object = $this->table_object.'_aide';
$listes_types = $this->em->getRepository(Aidecontacts::class)->getListAidecontacts(array(
'from' => [$type_object],
'aide_is_null' => true,
'where' => ['AND '.$type_object.'.idaide = aides.idaide'],
'group' => ['aidecontacts' => 'title'],
'special' => array(
'no_order' => true,
'aides' => array(
'type_aides' => $this->typeAide
)
),
'order' => ['aides' => 'title'],
'direction' => 'ASC'
));
foreach($listes_types AS $k => $v){
if(!empty($v['nameview'])){
$datas_select[$v['idaidecontact']] = $v['title'];
}
}
$this->formFactory->add('idtypecategorie', ChoiceType::class, [
'label' => 'Choisir une catégorie',
'choices' => array_flip($datas_select),
'attr' => [
'class'=>'select_type'
]
]);
$this->formFactory->add('validation', SubmitType::class, [
'label' => 'Commencer la procédure',
'attr' => [
'class'=>'boutform boutbig nextstep'
]
]);
$this->context['form'] = $this->formFactory->getForm();
$this->context['form']->handleRequest($request);
}
public function validateAidesForm(){
return ($this->context['form']->isSubmitted() && $this->context['form']->isValid()) ? true : false;
}
/** Etape initial - sauvegarde du formulaire de de l'aide
*
*/
public function saveAides()
{
$idaidecontact = $this->context['form']->get('idtypecategorie')->getViewData();
// mettre les idtypecategorie en session
$this->session->set('ajoutaides[idaidecontact]', $idaidecontact);
}
/** Etape 1 - génération du formulaire de sélection des contacts
*
*/
public function getFormContacts($request = null)
{
if(is_null($this->idaidecontact)){
return false;
}
$params = [
'special' => array(
'idaidecontact' => $this->idaidecontact
),
'group' => array(
'contacts' => 'idcontact'
),
'limit' => ['all'],
'order' => ['contacts' => 'organisation'],
'direction' => 'ASC'
];
$aides_listes = $this->em->getRepository(Aides::class)->getListAides($params);
foreach($aides_listes AS $value){
if(!empty($value['contact'])){
$contact = $value['contact'];
$datas_select[$contact['idcontact']] = $contact['organisation'].' '.$contact['nomSuite'];
}
}
$this->formFactory->add('contacts_list', ChoiceType::class, [
'label' => 'Choisir la structure',
'choices' => array_flip($datas_select),
'attr' => [
'class'=>'select_type'
]
]);
$this->formFactory->add('validation', SubmitType::class, [
'label' => 'Étape suivante',
'attr' => [
'class'=>'boutform boutbig nextstep'
]
]);
$this->context['form'] = $this->formFactory->getForm();
$this->context['form']->handleRequest($request);
return true;
}
/** Etape 1 - validation du formulaire de sélection des contacts
*
*/
public function validateContactsForm(){
return ($this->context['form']->isSubmitted() && $this->context['form']->isValid()) ? true : false;
}
/** Etape 1 - sauvegarde du formulaire de sélection des contacts
*
*/
public function saveContacts()
{
$idcontact = $this->context['form']->get('contacts_list')->getViewData();
// mettre les contacts_list en sessions
$this->session->set('ajoutaides[idcontact]', $idcontact);
}
/** Etape 2 - génération du formulaire de création de l'aide
*
*/
public function getFormAides($request = null){
if(is_null($this->idcontact) && is_null($this->idaidecontact)){
return false;
}
$contact = $this->em->getRepository(Contacts::class);
$this->view->contact = $contact->getInfosContact($this->idcontact);
$params = array(
'special' => array(
'aides' => [
'idcontacts' => [$this->idcontact]
],
'idaidecontact' => $this->idaidecontact
// 'type_aides' => $this->idaidecontact
)
);
$datas_select = array();
$type_list = $this->em->getRepository(Aides::class)->getListAides($params); //idtypespectacle
foreach($type_list AS $k => $v){
if(!empty($v['nameview'])){
$datas_select[$v['idaide']] = $v['nameview'];
}
else{
$datas_select[$v['idaide']] = 'Pas de catégorie à préciser';
}
}
$this->formFactory->add('idtypespectacle', ChoiceType::class, [
'label' => 'Choisir une catégorie',
'choices' => array_flip($datas_select),
'attr' => [
'class'=>'select_type'
]
]);
$this->formFactory->add('url', TextType::class, [
'label' => 'Plus d\'infos - lien facultatif vers la page de la structure (seulement si infos complémentaires)',
'constraints' => [
New Url([
'protocols' => ['http', 'https'],
'message' => 'lien facultatif : l\'adresse du site semble incorrecte (elle doit commencer par https://)'
]),
],
'attr' => [
'size'=> 50,
'placeholder' => '(doit commencer par http:// ou https://)'
],
'required' => false
]);
$this->formFactory->add('comments', TextType::class, [
'label' => 'Infos non publiques pour l\'administrateur du site',
'attr' => [
'size'=> 50
],
'required' => false
]);
$this->formFactory->add('date_start_year', TextType::class, [
'label' => 'Date de début (année obligatoire)',
'constraints' => [
new NotBlank([
'message' => 'Veuillez préciser l\'année de début'
]),
new Length([
'min' => 4,
'max' => 4,
'exactMessage' => 'Date de début : veuillez préciser une année correcte (YYYY)'
])
],
'attr' => [
'size'=>'4',
'maxlength'=>'4',
'placeholder' => 'Année', 'class'=>'input-group-field'
]
]);
$this->formFactory->add('date_start_month', TextType::class, [
'attr' => [
'size'=>'2',
'maxlength'=>'2',
'placeholder' => 'Mois',
'class'=>'input-group-field'
],
'required' => false
]);
$this->formFactory->add('date_start_day', TextType::class, [
'attr' => [
'size'=>'2',
'maxlength'=>'2',
'placeholder' => 'Jour',
'class'=>'input-group-field'
],
'required' => false
]);
$this->formFactory->add('date_end_year', TextType::class, [
'label' => 'Date de fin',
'attr' => [
'size'=>'4',
'maxlength'=>'4',
'placeholder' => 'Année',
'class'=>'input-group-field'
],
'required' => false
]);
$this->formFactory->add('date_end_month', TextType::class, [
'attr' => [
'size'=>'2',
'maxlength'=>'2',
'placeholder' => 'Mois',
'class'=>'input-group-field'
],
'required' => false
]);
$this->formFactory->add('date_end_day', TextType::class, [
'attr' => [
'size'=>'2',
'maxlength'=>'2',
'placeholder' => 'Jour',
'class'=>'input-group-field'
],
'required' => false
]);
$this->formFactory->add('validation', SubmitType::class, [
'label' => 'Enregistrer les informations',
'attr' => [
'class'=>'boutform boutbig nextstep'
]
]);
$this->context['form'] = $this->formFactory->getForm();
$this->context['form']->handleRequest($request);
return true;
}
/** Etape 2 - validation du formulaire de création de l'aide
*
*/
public function getValidAides(){
return ($this->context['form']->isSubmitted() && $this->context['form']->isValid()) ? true : false;
}
/** Etape 2 - sauvegarde du formulaire de création de l'aide
*
*/
public function getSaveAides(){
$id = null;
$obj = $this->em->getRepository('TheatreCore\Entity\\'.ucfirst($this->table_object).'Aide');
$entityName = 'TheatreCore\Entity\\'.ucfirst($this->table_object).'Aide';
$EntityAide = new $entityName();
//tableau pour insertion dans la table texts
// $values = array(
// 'idaide' => $this->context['form']->get('idtypespectacle')->getViewData(),
//
// 'comments' => $this->context['form']->get('comments')->getViewData(),
// 'url' => $this->context['form']->get('url')->getViewData(),
// );
$aideSelected = $this->context['form']->get('idtypespectacle')->getData();
$aideobj = $this->em->getRepository(Aides::class)->find($aideSelected);
$EntityAide->setIdaide($aideobj);
$EntityAide->setComments($this->context['form']->get('comments')->getData());
$EntityAide->setUrl($this->context['form']->get('url')->getData());
$key = 'setId'.$this->table_object;
$keyGet = 'getId'.$this->table_object.'aide';
$spectacleobj = $this->em->getRepository('TheatreCore\Entity\\'.ucfirst($this->table_object).'s')->find($this->idobject);
$EntityAide->$key($spectacleobj);
foreach(array('date_start','date_end') as $f) {
foreach(array('year','month','day') as $t) {
$field = $this->context['form']->get($f.'_'.$t)->getData();
if(empty($field)) {
$field = '00';
}
$date_format[$t] = $field;
}
$values[$f] = join('-', $date_format).' 00:00:00';
}
$EntityAide->setDateStart(new \DateTime($values["date_start"]));
$EntityAide->setDateEnd(new \DateTime($values["date_end"]));
// timestamp creation
$EntityAide->setDateinsert(new \DateTime());
// si on commit ou si on n'est pas admin ne pas mettre a jour le champ publish
if(!empty($rights['isadmin'])) {
// publié ou pas ? validé ou pas
foreach(array('publish') as $f) {
$val = $this->context['form']->get($f)->getData();
if(empty($val)) {
$values[$f] = 0;
} else {
$values[$f] = $val;
}
}
}
else{
$values['publish'] = 0;
}
$EntityAide->setPublish($values['publish']);
// commencer la transaction avec la variable error null
$error = null;
// go !
$em = $this->em;
$em->beginTransaction();
try {
//... do some work
$em->persist($EntityAide);
$em->flush();
$res = $EntityAide->$keyGet();
$reponse['insert_ok'] = "ok"; //$tr->get('update_ok');
} catch (Exception $e) {
$em->getConnection()->rollBack();
throw $e;
}
// l'identifiant de la nouvelle fiche (mode creation)
if(empty($id)) {
$newid = $id = $res; // on active l'id et on precise que c'est un nouvel id
} else {
$newid = null;
}
// // si pas d'erreur, on commit la transaction
if (!$error) {
if(!$em->getConnection()->commit()) {
$error[] = $this->context['tr']->trans('transaction_no_commit');
}
} else {
$error[] = $this->context['tr']->trans('transaction_no_commit_echec');
}
// // // pour loguer les actions
if(!$error) {
$this->utilsEdit->logAction($id,$newid,$this->table_object."_aide",$this->context['session']->getIdextranetuser());
}
// // // pour une creation, il faut reselectionner l'enregistrement et enregistrer l'url_clean
if(!empty($newid) && !$error) {
// $this->utilsEdit->createUrlClean($newid,$EntityAide,"aidecontacts");
}
}
//Gestion de la prévisualistion (nécessaire ?)
public function getValidation(){
$this->session->remove('ajoutaides[idcontact]');
$this->session->remove('ajoutaides[idaidecontact]');
}
// $params = array(
// 'object' => array(
// 'type' => 'specacles'
// 'value' => '';
// )
// )
private function setObject(){
$params = $this->params;
if(empty($params['object'])){
return false;
}
if(empty($params['aides'])){
return false;
}
$arrays_objects = ['spectacle', 'text', 'person'];
if(in_array($params['object']['type'], $arrays_objects) && !empty($object_value = $params['object']['value'])){
$this->context['view']->{'object_'.$params['object']['type']} = true;
$this->table_object = $params['object']['type'];
$this->idobject = (int)$object_value;
if(!empty($params['aides']['typeaide'])){
$this->typeAide = (int)$params['aides']['typeaide'];
$this->context['view']->{'type_'.$this->typeAide} = true;
}
if(!empty($params['url'])){
$this->context['view']->url_clean_aide = $params['url'];
}
if(!empty($params['aides']['url_clean'])){
$this->url_clean = (int)$params['aides']['url_clean'];
}
return true;
}
else{
return false;
}
}
// {{{ getSteps()
/** etapes
*
*/
public function getSteps($title_page,$num_step) {
if(isset($num_step)) {
$steps = array(
1 => array('nom' => 'Structures'),
2 => array('nom' => 'Informations'),
3 => array('nom' => 'Validation'),
);
// retourner le titre de la page ou un array
if($num_step!='all') {
if (in_array($num_step, array_keys($steps))) {
$steps[$num_step]['statut'] = ' step-on';
$this->view->steps = $steps;
$title_page = $steps[$num_step]['nom'].' (étape '.$num_step.') - '.$title_page;
}
}
// marquer les steps infériereure
foreach($steps as $k=>$s) {
if($k==$num_step) {
$steps[$k]['statut'] = ' step-on';
} elseif($k<$num_step) {
$steps[$k]['statut'] = ' step-done';
}
// if($this->checkStep($k)) {
// $steps[$k]['statut_step'] = 'ok';
// } else {
// $steps[$k]['statut_step'] = 'no';
// }
}
// retourner le titre de la page ou un array
if($num_step!='all') {
$this->view->steps = $steps;
return $title_page;
} else {
return $steps;
}
}
}
// }}}
// {{{ getAjoutspectacleCommon()
/** Elements communs aux vues
*
*/
public function getAjoutspectacleCommon() {
// js en plus
$this->view->js_more = array('ajax.relation.js');
}
}