Skip to content

Commit 77a0e5c

Browse files
committed
Add/Edit SQL from action
1 parent 51fe3b0 commit 77a0e5c

9 files changed

Lines changed: 302 additions & 82 deletions

File tree

Assets/js/sqlconditions.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Mautic.disabledSqlConditionsActions = function(opener) {
2+
if (typeof opener == 'undefined') {
3+
opener = window;
4+
}
5+
var email = opener.mQuery('#campaignevent_properties_sql').val();
6+
7+
var disabled = email === '' || email === null;
8+
9+
opener.mQuery('#campaignevent_properties_editButton').prop('disabled', disabled);
10+
};
11+
12+
Mautic.standardSqlConditionsUrl = function(options) {
13+
14+
if (!options) {
15+
return;
16+
}
17+
18+
var url = options.windowUrl;
19+
if (url) {
20+
var editKey = '/sqlConditions/edit/objectId';
21+
if (url.indexOf(editKey) > -1) {
22+
options.windowUrl = url.replace('objectId', mQuery('#campaignevent_properties_sql').val());
23+
}
24+
}
25+
26+
return options;
27+
};

Config/config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
'mautic.sqlConditions.form.campaign.type' => [
2828
'class' => \MauticPlugin\MauticSqlConditionsBundle\Form\Type\SqlListType::class,
2929
'arguments' => [
30-
'mautic.sqlConditions.model.sqlConditions',
3130
],
3231
],
32+
'mautic.sqlConditions.form.list.type' => [
33+
'class' => \MauticPlugin\MauticSqlConditionsBundle\Form\Type\SqlConditionsCampaignType::class,
34+
'arguments' => 'router',
35+
'alias' => 'sqlconditions_list',
36+
],
3337
],
3438
'command' => [
3539

Controller/SqlConditionsController.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,77 @@ public function batchCronsAction($objectId = 0)
199199
]
200200
);
201201
}
202+
203+
204+
/**
205+
* @param array $args
206+
* @param $action
207+
*
208+
* @return array
209+
*/
210+
protected function getPostActionRedirectArguments(array $args, $action)
211+
{
212+
$updateSelect = ($this->request->getMethod() == 'POST')
213+
? $this->request->request->get('sqlConditions[updateSelect]', false, true)
214+
: $this->request->get(
215+
'updateSelect',
216+
false
217+
);
218+
if ($updateSelect) {
219+
switch ($action) {
220+
case 'new':
221+
case 'edit':
222+
$passthrough = $args['passthroughVars'];
223+
$passthrough = array_merge(
224+
$passthrough,
225+
[
226+
'updateSelect' => $updateSelect,
227+
'id' => $args['entity']->getId(),
228+
'name' => $args['entity']->getName(),
229+
]
230+
);
231+
$args['passthroughVars'] = $passthrough;
232+
break;
233+
}
234+
}
235+
236+
return $args;
237+
}
238+
239+
/**
240+
* @return array
241+
*/
242+
protected function getEntityFormOptions()
243+
{
244+
$updateSelect = ($this->request->getMethod() == 'POST')
245+
? $this->request->request->get('sqlConditions[updateSelect]', false, true)
246+
: $this->request->get(
247+
'updateSelect',
248+
false
249+
);
250+
if ($updateSelect) {
251+
return ['update_select' => $updateSelect];
252+
}
253+
}
254+
255+
/**
256+
* Return array of options update select response.
257+
*
258+
* @param string $updateSelect HTML id of the select
259+
* @param object $entity
260+
* @param string $nameMethod name of the entity method holding the name
261+
* @param string $groupMethod name of the entity method holding the select group
262+
*
263+
* @return array
264+
*/
265+
protected function getUpdateSelectParams($updateSelect, $entity, $nameMethod = 'getName', $groupMethod = 'getLanguage')
266+
{
267+
$options = [
268+
'updateSelect' => $updateSelect,
269+
'id' => $entity->getId(),
270+
'name' => $entity->$nameMethod(),
271+
];
272+
273+
return $options;
274+
}
202275
}

EventListener/CampaignConditionSubscriber.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Mautic\CampaignBundle\Event\CampaignBuilderEvent;
1616
use Mautic\CampaignBundle\Event\ConditionEvent;
1717
use MauticPlugin\MauticSqlConditionsBundle\Executioner\SqlExecutioner;
18-
use MauticPlugin\MauticSqlConditionsBundle\Form\Type\SqlConditionsCampaignType;
1918
use MauticPlugin\MauticSqlConditionsBundle\SqlConditionsEvents;
2019
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2120

@@ -46,7 +45,7 @@ public function __construct(SqlExecutioner $sqlExecutioner)
4645
public static function getSubscribedEvents()
4746
{
4847
return [
49-
CampaignEvents::CAMPAIGN_ON_BUILD => ['onCampaignBuild', 0],
48+
CampaignEvents::CAMPAIGN_ON_BUILD => ['onCampaignBuild', 0],
5049
SqlConditionsEvents::ON_CAMPAIGN_CONDITION_TRIGGER => ['onCampaignTriggerCondition', 0],
5150
];
5251
}
@@ -59,9 +58,11 @@ public function onCampaignBuild(CampaignBuilderEvent $event)
5958
$event->addCondition(
6059
'sql.condition',
6160
[
62-
'label' => 'mautic.sqlConditions',
63-
'eventName' => SqlConditionsEvents::ON_CAMPAIGN_CONDITION_TRIGGER,
64-
'formType' => SqlConditionsCampaignType::class,
61+
'label' => 'mautic.sqlConditions',
62+
'eventName' => SqlConditionsEvents::ON_CAMPAIGN_CONDITION_TRIGGER,
63+
'formType' => 'sqlconditions_list',
64+
'formTheme' => 'MauticSqlConditionsBundle:FormTheme\SqlConditionsList',
65+
'formTypeOptions' => ['update_select' => 'campaignevent_properties_sql'],
6566

6667
]
6768
);
@@ -74,7 +75,7 @@ public function onCampaignTriggerCondition(ConditionEvent $event)
7475
{
7576
if ($this->sqlExecutioner->execute($event)) {
7677
$event->pass();
77-
}else{
78+
} else {
7879
$event->fail();
7980
}
8081
}

Form/Type/SqlConditionsCampaignType.php

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@
1414
use MauticPlugin\MauticSqlConditionsBundle\Validator\Constraint\UrlDnsConstraint;
1515
use Symfony\Component\Form\AbstractType;
1616
use Symfony\Component\Form\FormBuilderInterface;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
use Symfony\Component\Routing\RouterInterface;
1719
use Symfony\Component\Validator\Constraints\NotBlank;
1820

1921
class SqlConditionsCampaignType extends AbstractType
2022
{
23+
/**
24+
* @var RouterInterface
25+
*/
26+
protected $router;
27+
28+
/**
29+
* @param RouterInterface $router
30+
*/
31+
public function __construct(RouterInterface $router)
32+
{
33+
$this->router = $router;
34+
}
35+
36+
2137
/**
2238
* @param FormBuilderInterface $builder
2339
* @param array $options
@@ -29,13 +45,89 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2945
'sql',
3046
SqlListType::class,
3147
[
32-
'label' => 'mautic.sqlConditions.sql',
33-
'label_attr' => ['class' => 'control-label'],
34-
'attr' => ['class' => 'form-control'],
48+
'label' => 'mautic.sqlConditions.sql',
49+
'label_attr' => ['class' => 'control-label'],
50+
'attr' => [
51+
'class' => 'form-control',
52+
'onchange' => 'Mautic.disabledSqlConditionsActions()',
53+
],
54+
'multiple' => false,
55+
'required' => true,
3556
'constraints' => [
3657
new NotBlank(),
3758
],
3859
]
3960
);
61+
62+
63+
64+
if (!empty($options['update_select'])) {
65+
$windowUrl = $this->router->generate(
66+
'mautic_sqlConditions_action',
67+
[
68+
'objectAction' => 'new',
69+
'contentOnly' => 1,
70+
'updateSelect' => $options['update_select'],
71+
]
72+
);
73+
74+
$builder->add(
75+
'newButton',
76+
'button',
77+
[
78+
'attr' => [
79+
'class' => 'btn btn-primary btn-nospin',
80+
'onclick' => 'Mautic.loadNewWindow({
81+
"windowUrl": "'.$windowUrl.'"
82+
})',
83+
'icon' => 'fa fa-plus',
84+
],
85+
'label' => 'mautic.core.form.add',
86+
]
87+
);
88+
89+
// create button edit focus
90+
$windowUrlEdit = $this->router->generate(
91+
'mautic_sqlConditions_action',
92+
[
93+
'objectAction' => 'edit',
94+
'objectId' => 'objectId',
95+
'contentOnly' => 1,
96+
'updateSelect' => $options['update_select'],
97+
]
98+
);
99+
100+
$builder->add(
101+
'editButton',
102+
'button',
103+
[
104+
'attr' => [
105+
'class' => 'btn btn-primary btn-nospin',
106+
'onclick' => 'Mautic.loadNewWindow(Mautic.standardSqlConditionsUrl({"windowUrl": "'.$windowUrlEdit.'"}))',
107+
'disabled' => !isset($options['data']['sql']),
108+
'icon' => 'fa fa-edit',
109+
],
110+
'label' => 'mautic.core.form.edit',
111+
]
112+
);
113+
}
114+
115+
}
116+
117+
118+
/**
119+
* @param OptionsResolver $resolver
120+
*/
121+
public function configureOptions(OptionsResolver $resolver)
122+
{
123+
$resolver->setDefined(['update_select']);
124+
}
125+
126+
/**
127+
* @return string
128+
*/
129+
public function getName()
130+
{
131+
return 'sqlconditions_list';
40132
}
41133
}

Form/Type/SqlConditionsType.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\AbstractType;
1616
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
1717
use Symfony\Component\Form\FormBuilderInterface;
18+
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1819
use Symfony\Component\Validator\Constraints\NotBlank;
1920

2021
class SqlConditionsType extends AbstractType
@@ -58,11 +59,42 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5859

5960
$builder->add('isPublished', 'yesno_button_group');
6061

61-
$builder->add(
62-
'buttons',
63-
'form_buttons'
64-
);
6562

63+
if (!empty($options['update_select'])) {
64+
$builder->add(
65+
'buttons',
66+
'form_buttons',
67+
[
68+
'apply_text' => false,
69+
]
70+
);
71+
$builder->add(
72+
'updateSelect',
73+
'hidden',
74+
[
75+
'data' => $options['update_select'],
76+
'mapped' => false,
77+
]
78+
);
79+
} else {
80+
$builder->add(
81+
'buttons',
82+
'form_buttons'
83+
);
84+
}
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function setDefaultOptions(OptionsResolverInterface $resolver)
91+
{
92+
$resolver->setDefaults(
93+
[
94+
'data_class' => 'MauticPlugin\MauticSqlConditionsBundle\Entity\SqlConditions',
95+
]
96+
);
97+
$resolver->setDefined(['update_select']);
6698
}
6799

68100

0 commit comments

Comments
 (0)