Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mautic 5 Upgrade #4

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 0 additions & 1 deletion .php-cs-fixer.cache

This file was deleted.

94 changes: 20 additions & 74 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
<?php
/**
* @author Aivie
* @copyright 2022 Aivie. All rights reserved
*
* @see https://aivie.ch
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

declare(strict_types=1);
use MauticPlugin\MauticTrelloBundle\Integration\Support\ConfigSupport;
use MauticPlugin\MauticTrelloBundle\Integration\TrelloIntegration;

return [
'name' => 'Mautic Trello',
'description' => 'Create Trello cards from Mautic contacts',
'version' => '1.0.0',
'version' => '2.0.0',
'author' => 'Aivie',
'routes' => [
'main' => [
'plugin_create_cards_show_new' => [
'path' => '/trello/card/show-new/{contactId}',
'controller' => 'MauticTrelloBundle:Card:showNewCard',
'controller' => 'MauticPlugin\MauticTrelloBundle\Controller\CardController::showNewCardAction',
],
'plugin_trello_card_add' => [
'path' => '/trello/card',
'method' => 'POST',
'controller' => 'MauticTrelloBundle:Card:add',
'controller' => 'MauticPlugin\MauticTrelloBundle\Controller\CardController::addAction',
'returnRoute' => '',
],
],
Expand All @@ -30,71 +27,20 @@
'favorite_board' => '',
],
'services' => [
'forms' => [
'mautic.trello.form.card' => [
'class' => 'MauticPlugin\MauticTrelloBundle\Form\NewCardType',
'arguments' => [
'mautic.trello.service.trello_api',
'monolog.logger.mautic',
],
],
'mautic.trello.form.config' => [
'class' => 'MauticPlugin\MauticTrelloBundle\Form\ConfigType',
'arguments' => [
'mautic.lead.model.field',
'mautic.trello.service.trello_api',
'monolog.logger.mautic',
],
],
],
'events' => [
'mautic.channel.button.subscriber.trello' => [
'class' => \MauticPlugin\MauticTrelloBundle\Event\ButtonSubscriber::class,
'arguments' => [
'router',
'translator',
'request_stack',
'mautic.helper.integration',
],
],
'mautic.trello.event.config' => [
'class' => \MauticPlugin\MauticTrelloBundle\Event\ConfigSubscriber::class,
'arguments' => [
'mautic.helper.integration',
'monolog.logger.mautic',
],
],
],
'others' => [
'mautic.trello.service.trello_api' => [
'class' => \MauticPlugin\MauticTrelloBundle\Service\TrelloApiService::class,
'arguments' => [
'mautic.helper.integration',
'mautic.helper.core_parameters',
'monolog.logger.mautic',
'integrations' => [
// Basic definitions with name, display name and icon
'mautic.integration.trello' => [
'class' => TrelloIntegration::class,
'tags' => [
'mautic.integration',
'mautic.basic_integration',
],
],
],
'integrations' => [
'mautic.integration.trello' => [
'class' => \MauticPlugin\MauticTrelloBundle\Integration\TrelloIntegration::class,
'arguments' => [
'event_dispatcher',
'mautic.helper.cache_storage',
'doctrine.orm.entity_manager',
'session',
'request_stack',
'router',
'translator',
'logger',
'mautic.helper.encryption',
'mautic.lead.model.lead',
'mautic.lead.model.company',
'mautic.helper.paths',
'mautic.core.model.notification',
'mautic.lead.model.field',
'mautic.plugin.model.integration_entity',
'mautic.lead.model.dnc',
// Provides the form types to use for the configuration UI
'trello.integration.configuration' => [
'class' => ConfigSupport::class,
'tags' => [
'mautic.config_integration',
],
],
],
Expand Down
18 changes: 18 additions & 0 deletions Config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Mautic\CoreBundle\DependencyInjection\MauticCoreExtension;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator) {
$services = $configurator->services()
->defaults()
->autowire()
->autoconfigure()
->public();

$excludes = [];
$services->load('MauticPlugin\\MauticTrelloBundle\\', '../')
->exclude('../{'.implode(',', array_merge(MauticCoreExtension::DEFAULT_EXCLUDES, $excludes)).'}');
};
101 changes: 50 additions & 51 deletions Controller/CardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,93 @@

declare(strict_types=1);

/**
* @author Aivie
* @copyright 2022 Aivie. All rights reserved
*
* @see https://aivie.ch
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace MauticPlugin\MauticTrelloBundle\Controller;

use Doctrine\Persistence\ManagerRegistry;
use Mautic\CoreBundle\Controller\AbstractFormController;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Service\FlashBag;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\LeadBundle\Controller\LeadAccessTrait;
use Mautic\LeadBundle\Entity\Lead;
use MauticPlugin\MauticTrelloBundle\Form\NewCardType;
use MauticPlugin\MauticTrelloBundle\Openapi\lib\Model\Card;
use MauticPlugin\MauticTrelloBundle\Openapi\lib\Model\NewCard;
use Symfony\Component\Form\Form;
use MauticPlugin\MauticTrelloBundle\Service\TrelloApiService;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

/**
* Setup a a form and send it to Trello to create a new card.
* Set up a form and send it to Trello to create a new card.
*/
class CardController extends AbstractFormController
{
use LeadAccessTrait;

/**
* Logger.
*
* @var \Monolog\Logger
*/
protected $logger;

/**
* @var \MauticPlugin\MauticTrelloBundle\Service\TrelloApiService
* @phpstan-ignore-next-line
*/
private $apiService;
public function __construct(
private LoggerInterface $logger,
private TrelloApiService $apiService,
ManagerRegistry $doctrine,
MauticFactory $factory,
ModelFactory $modelFactory,
UserHelper $userHelper,
CoreParametersHelper $coreParametersHelper,
EventDispatcherInterface $dispatcher,
Translator $translator,
FlashBag $flashBag,
RequestStack $requestStack,
CorePermissions $security
) {
parent::__construct($doctrine, $factory, $modelFactory, $userHelper, $coreParametersHelper, $dispatcher, $translator, $flashBag, $requestStack, $security);
}

/**
* Show a new Trello card form with prefilled information from the Contact.
*/
public function showNewCardAction(int $contactId): Response
public function showNewCardAction(Request $request, int $contactId): Response
{
$this->logger = $this->get('monolog.logger.mautic');
$this->apiService = $this->get('mautic.trello.service.trello_api');

// returns the Contact or an error Response to show to the user
$contact = $this->checkLeadAccess($contactId, 'view');
if ($contact instanceof Response) {
return $contact;
}

// build the form
$form = $this->getForm($contactId);
$form = $this->getForm($request, $contactId);

// display empty form
return $this->delegateView(
[
'viewParameters' => [
'form' => $form->createView(),
],
'contentTemplate' => 'MauticTrelloBundle:Card:new.html.php',
'contentTemplate' => '@MauticTrello/Card/new.html.twig',
]
);
}

/**
* Add a new card by POST or handle the cancelation of the form.
*
* @return JsonResponse|RedirectResponse
*/
public function addAction()
public function addAction(Request $request): JsonResponse|Response
{
$this->logger = $this->get('monolog.logger.mautic');
$this->apiService = $this->get('mautic.trello.service.trello_api');

$returnRoute = $this->request->get('returnRoute', '');
$returnRoute = $request->get('returnRoute', '');

$contactId = 0;
$data = $this->request->request->get('new_card', false);
$data = $request->request->get('new_card', false);
if (is_array($data) && isset($data['contactId'])) {
$contactId = (int) $data['contactId'];
}
Expand All @@ -97,22 +100,22 @@ public function addAction()
}

// Check for a submitted form and process it
$form = $this->getForm();
$form = $this->getForm($request);

if ($this->isFormCancelled($form)) {
return $this->closeModal();
}

// process form data from HTTP variables
$form->handleRequest($this->request);
$form->handleRequest($request);

// MauticPlugin\MauticTrelloBundle\Openapi\lib\Model\NewCard;
$newCard = $form->getData();

if (!$newCard->valid()) {
$invalid = current($newCard->listInvalidProperties());
$message = sprintf($this->translator->trans('mautic.trello.card_data_not_valid'), $invalid);
$this->addFlash($message, [], 'error');
$this->addFlash(FlashBag::LEVEL_ERROR, $message);

return new JsonResponse(['error' => $message]);
}
Expand All @@ -127,14 +130,14 @@ public function addAction()
if ($card instanceof Card) {
// successfully added
$this->addFlash(
'plugin.trello.card_added',
['%url%' => $card->getUrl(), '%title%' => $card->getName()]
FlashBag::LEVEL_NOTICE,
$this->translator->trans('plugin.trello.card_added', ['%url%' => $card->getUrl(), '%title%' => $card->getName()], 'flashes'),
);
} else {
// not successfully added
$this->addFlash(
'plugin.trello.card_not_added',
['not-added']
FlashBag::LEVEL_ERROR,
$this->translator->trans('plugin.trello.card_not_added', [], 'flashes')
);
}

Expand All @@ -143,10 +146,8 @@ public function addAction()

/**
* Close the modal after adding a card in Trello.
*
* @return JsonResponse|RedirectResponse
*/
protected function closeAndRedirect(string $returnRoute, int $contactId)
protected function closeAndRedirect(string $returnRoute, int $contactId): Response
{
if (empty($returnRoute) || empty($contactId)) {
$this->logger->warning('Trello: No return url or contact for add to Trello specified', ['contactId' => $contactId, 'returnRoute' => $returnRoute]);
Expand All @@ -156,7 +157,7 @@ protected function closeAndRedirect(string $returnRoute, int $contactId)
if ('mautic_contact_index' === $returnRoute) {
$func = 'index';
$viewParameters = [
'page' => $this->get('session')->get('mautic.lead.page', 1),
'page' => $this->container->get('session')->get('mautic.lead.page', 1),
'objectId' => $contactId,
];
} else {
Expand All @@ -172,7 +173,7 @@ protected function closeAndRedirect(string $returnRoute, int $contactId)
[
'returnUrl' => $this->generateUrl($returnRoute, $viewParameters),
'viewParameters' => $viewParameters,
'contentTemplate' => 'MauticLeadBundle:Lead:'.$func,
'contentTemplate' => 'Mautic\LeadBundle\Controller\LeadController::'.$func.'Action',
'passthroughVars' => [
'mauticContent' => 'lead',
'closeModal' => 1,
Expand All @@ -196,15 +197,13 @@ protected function closeModal(): JsonResponse

/**
* Build the form.
*
* @param int $contactId
*/
protected function getForm(int $contactId = null): ?FormInterface
protected function getForm(Request $request, int $contactId = null): ?FormInterface
{
$returnRoute = $this->request->get('returnRoute');
$returnRoute = $request->get('returnRoute');
if (empty($returnRoute)) {
$this->logger->warning('Trello: No return route for add to Trello specified', ['contactId' => $contactId]);
$returnRoute = "mautic_contact_action"; //somehow the returnRoute can be empty, so set it to the contact detail by default
$returnRoute = 'mautic_contact_action'; // somehow the returnRoute can be empty, so set it to the contact detail by default
}
$card = new NewCard();

Expand Down
19 changes: 19 additions & 0 deletions DependencyInjection/MauticTrelloExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace MauticPlugin\MauticTrelloBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

class MauticTrelloExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Config'));
$loader->load('services.php');
}
}
Loading