Issues (3627)

app/bundles/FormBundle/Model/ActionModel.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\FormBundle\Model;
13
14
use Mautic\CoreBundle\Model\FormModel as CommonFormModel;
15
use Mautic\FormBundle\Entity\Action;
16
use Mautic\FormBundle\Form\Type\ActionType;
17
18
/**
19
 * Class ActionModel.
20
 */
21
class ActionModel extends CommonFormModel
22
{
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @return \Mautic\FormBundle\Entity\ActionRepository
27
     */
28
    public function getRepository()
29
    {
30
        return $this->em->getRepository('MauticFormBundle:Action');
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getPermissionBase()
37
    {
38
        return 'form:forms';
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getEntity($id = null)
45
    {
46
        if (null === $id) {
47
            return new Action();
48
        }
49
50
        return parent::getEntity($id);
51
    }
52
53
    /**
54
     * @param object                              $entity
55
     * @param \Symfony\Component\Form\FormFactory $formFactory
56
     * @param null                                $action
57
     * @param array                               $options
58
     */
59
    public function createForm($entity, $formFactory, $action = null, $options = [])
60
    {
61
        if (!$entity instanceof Action) {
62
            throw new \InvalidArgumentException('Entity must be of class Action');
63
        }
64
65
        if ($action) {
0 ignored issues
show
$action is of type null, thus it always evaluated to false.
Loading history...
66
            $options['action'] = $action;
67
        }
68
69
        if (empty($options['formId']) && null !== $entity->getForm()) {
70
            $options['formId'] = $entity->getForm()->getId();
71
        }
72
73
        return $formFactory->create(ActionType::class, $entity->convertToArray(), $options);
74
    }
75
76
    /**
77
     * Get segments which are dependent on given segment.
78
     *
79
     * @param int $segmentId
80
     *
81
     * @return array
82
     */
83
    public function getFormsIdsWithDependenciesOnSegment($segmentId)
84
    {
85
        $filter = [
86
            'force'  => [
87
                ['column' => 'e.type', 'expr' => 'LIKE', 'value'=>'lead.changelist'],
88
            ],
89
        ];
90
        $entities = $this->getEntities(
91
            [
92
                'filter'     => $filter,
93
            ]
94
        );
95
        $dependents = [];
96
        foreach ($entities as $entity) {
97
            $properties = $entity->getProperties();
98
            foreach ($properties as $property) {
99
                if (in_array($segmentId, $property)) {
100
                    $dependents[] = $entity->getForm()->getId();
101
                }
102
            }
103
        }
104
105
        return $dependents;
106
    }
107
}
108