?? GreyFile — Mystic File Browser

Current path: home/webdevt/cryptoimpot.fr/module/Application/src/Controller/



?? Go up: /home/webdevt/cryptoimpot.fr/module/Application/src

?? Viewing: InformationController.php

<?php

namespace Application\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Laminas\Mail\Message;
use Laminas\Mime;
use Laminas\Mail\Transport\Sendmail as SendmailTransport;

class InformationController extends AbstractActionController {

    const ROUTE_INFORMATION = 'information';
    const ROUTE_IMPORTATION = 'importation';

    /**
     * Entity manager.
     * @var Doctrine\ORM\EntityManager
     */
    private $entityManager;

    /**
     * Auth manager.
     * @var User\Service\AuthManager
     */
    private $authManager;

    /**
     * User manager.
     * @var User\Service\UserManager
     */
    private $userManager;

    /**
     * Constructor.
     */
    public function __construct($entityManager, $authManager, $userManager) {

        $this->entityManager = $entityManager;
        $this->authManager = $authManager;
        $this->userManager = $userManager;
    }

    public function getEntityManager() {
        return $this->entityManager;
    }

    /*
     * Display in multi div select
     */

    public function addAction() {
        if ($user = $this->currentUser()) {
            
        } else {
            return $this->redirect()->toRoute(\Application\Controller\UserController::ROUTE_LOGIN);
        }

        // Entity Manager Doctrine
        $entityManager = $this->getEntityManager();
        // Récupération des exchanges séléctionnés par l'utilisateur
        $arrayIdExchangesUser = [];
        if ($user->getExchanges()) {
            foreach ($user->getExchanges() as $exchange) {
                $arrayIdExchangesUser[$exchange->getId()] = $exchange->getId();
            }
        }
        // Recupération des exchanges
        $exchangeMapper = new \Application\Mapper\ExchangeMapper($entityManager);


        $exchangeArray = $exchangeMapper->getArrayForSelect();

//        var_dump($exchangeArrayForSelect); exit;

        $this->layout('layout/layout-ledgio');

        return new ViewModel(
                array(
            'exchangeArray' => $exchangeArray,
            'arrayIdExchangesUser' => $arrayIdExchangesUser,
        ));
    }

    /*
     * Display in multi select
     */

    public function addOldAction() {

        if ($user = $this->currentUser()) {
            
        } else {
            return $this->redirect()->toRoute(\Application\Controller\UserController::ROUTE_LOGIN);
        }

        // Entity Manager Doctrine
        $entityManager = $this->getEntityManager();

        // Création du formulaire
        $form = new \Application\Form\InformationForm\CreateInformationForm($entityManager);
        $form->setHydrator(new \Laminas\Hydrator\ArraySerializable());

        // Récupération des exchanges séléctionnés par l'utilisateur
        if ($user->getExchanges()) {
            foreach ($user->getExchanges() as $exchange) {
                $arrayIdExchanges[] = $exchange->getId();
            }
            $form->setData(['exchanges' => $arrayIdExchanges]);
        }

        if ($this->getRequest()->isPost()) {
            // Récupération des données du post
            $data = array_merge_recursive(
                    $this->getRequest()->getPost()->toArray()
            );

            $form->setData($data);


            if ($form->isValid()) {
                $data = $form->getData();
                $exchangeMapper = $entityManager->getRepository(\Application\Entity\Exchange::class);
                $newExchanges = [];
                foreach ($data['exchanges'] as $exchangeId) {
                    $newExchanges[] = $exchangeMapper->findOneById($exchangeId);
                    if ($newExchanges) {
                        $user->setExchanges($newExchanges);
                    }
                }

                $this->getEntityManager()->persist($user);
                $this->getEntityManager()->flush();

                $hasError = '';

                if (!empty($hasError)) {
                    $this->flashMessenger()->addErrorMessage('Une erreur est survenue lors de l\'enregistrement de la catégorie.');
                    $this->redirect()->toRoute(self::ROUTE_INFORMATION);
                }
                //$this->flashMessenger()->addSuccessMessage('Les informations ont été mises à jour avec succès.');

                $this->redirect()->toRoute(self::ROUTE_IMPORTATION, array('exchange' => 'exchange-generique'));
            } else {
                $messages = $form->getMessages();
            }
        }

        $this->layout('layout/layout-ledgio');

        return new ViewModel(
                array(
            'form' => $form,
        ));
    }
    
    public function updateExchangeAction() {
        if ($user = $this->currentUser()) {
            
        } else {
            return $this->redirect()->toRoute(\Application\Controller\UserController::ROUTE_LOGIN);
        }

        
        if ($this->getRequest()->isPost()) {
            // Récupération des données du post
            $data = array_merge_recursive(
                    $this->getRequest()->getPost()->toArray()
            );

            //var_dump($data); exit;
            
            $responseArray = array();
            
            if (!empty($data)) {
                
                $exchangeId = (int) $data['id'];

                if ($exchangeId > 1) {
                    $mapperExchange = new \Application\Mapper\ExchangeMapper($this->getEntityManager());
                    $findExchange = $mapperExchange->findById($exchangeId);
                    if (empty($findExchange)) {
                        $responseArray['error']['messages'][] = array(
                            'header' => 'Données non reconnues',
                            'body' => 'Impossible de supprimer ces données',
                            'footer' => ''
                        );
                    } else {
                        if ($data['type'] === "add") {
                            $user->addExchange($findExchange);
                        } else if ($data['type'] === "remove") {
                            $user->removeExchange($findExchange);
                        } 
                    }

                    
                    
                

                    $this->getEntityManager()->persist($user);
                    $this->getEntityManager()->flush();
              
$responseArray =  array('id' => $data['id'], 'type' => $data['type'], 'response' => 'success');
                 
                    
                        
                    
                } else {
                    $responseArray =  array('id' => $data['id'], 'type' => $data['type'], 'response' => 'error');
                    
                }
                
            } else {
               $responseArray =  array('id' => $data['id'], 'type' => $data['type'], 'response' => 'error');
            }
        }

        $response = $this->getResponse();
        $response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
        $response->setContent(json_encode($responseArray));
        return $response;
    }
    
    public function messageAction() {
        if ($user = $this->currentUser()) {
            
        } else {
            return $this->redirect()->toRoute(\Application\Controller\UserController::ROUTE_LOGIN);
        }
        $mapperMessage = new \Application\Mapper\MessageMapper($this->getEntityManager());
        $messages = $mapperMessage->findByUser($user);
        if (!empty($messages)) {
            $returnReponse = [
        //            'messages' => $messages
                    'message' => $messages[0]
            ];
            $mapperMessage->delete($messages[0]);
        } else {
            $returnReponse = [
        //            'messages' => $messages
                    'message' => ''
            ];
        }
        $response = $this->getResponse();
        $response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
        $response->setContent(json_encode($returnReponse));
        return $response;
    }

}


??

??