Issues (3627)

app/bundles/LeadBundle/Helper/TokenHelper.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 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\LeadBundle\Helper;
13
14
use Mautic\CoreBundle\Helper\DateTimeHelper;
15
use Mautic\CoreBundle\Helper\ParamsLoaderHelper;
16
17
/**
18
 * Class TokenHelper.
19
 */
20
class TokenHelper
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $parameters;
26
27
    /**
28
     * @param string $content
29
     * @param array  $lead
30
     * @param bool   $replace If true, search/replace will be executed on $content and the modified $content returned
31
     *                        rather than an array of found matches
32
     *
33
     * @return array|string
34
     */
35
    public static function findLeadTokens($content, $lead, $replace = false)
36
    {
37
        if (!$lead) {
38
            return $replace ? $content : [];
39
        }
40
41
        // Search for bracket or bracket encoded
42
        $tokenList    = [];
43
        $foundMatches = preg_match_all('/({|%7B)contactfield=(.*?)(}|%7D)/', $content, $matches);
44
45
        if ($foundMatches) {
46
            foreach ($matches[2] as $key => $match) {
47
                $token = $matches[0][$key];
48
49
                if (isset($tokenList[$token])) {
50
                    continue;
51
                }
52
53
                $alias             = self::getFieldAlias($match);
54
                $defaultValue      = self::getTokenDefaultValue($match);
55
                $tokenList[$token] = self::getTokenValue($lead, $alias, $defaultValue);
56
            }
57
58
            if ($replace) {
59
                $content = str_replace(array_keys($tokenList), $tokenList, $content);
60
            }
61
        }
62
63
        return $replace ? $content : $tokenList;
64
    }
65
66
    /**
67
     * Returns correct token value from provided list of tokens and the concrete token.
68
     *
69
     * @param array  $tokens like ['{contactfield=website}' => 'https://mautic.org']
70
     * @param string $token  like '{contactfield=website|https://default.url}'
71
     *
72
     * @return string empty string if no match
73
     */
74
    public static function getValueFromTokens(array $tokens, $token)
75
    {
76
        $token   = str_replace(['{', '}'], '', $token);
77
        $alias   = self::getFieldAlias($token);
78
        $default = self::getTokenDefaultValue($token);
79
80
        return empty($tokens["{{$alias}}"]) ? $default : $tokens["{{$alias}}"];
81
    }
82
83
    /**
84
     * @param $alias
85
     * @param $defaultValue
86
     *
87
     * @return mixed
88
     */
89
    private static function getTokenValue(array $lead, $alias, $defaultValue)
90
    {
91
        $value = '';
92
        if (isset($lead[$alias])) {
93
            $value = $lead[$alias];
94
        } elseif (isset($lead['companies'][0][$alias])) {
95
            $value = $lead['companies'][0][$alias];
96
        }
97
98
        if ('' !== $value) {
99
            switch ($defaultValue) {
100
                case 'true':
101
                    $value = urlencode($value);
102
                    break;
103
                case 'datetime':
104
                case 'date':
105
                case 'time':
106
                    $dt   = new DateTimeHelper($value);
107
                    $date = $dt->getDateTime()->format(
108
                        self::getParameter('date_format_dateonly')
109
                    );
110
                    $time = $dt->getDateTime()->format(
111
                        self::getParameter('date_format_timeonly')
112
                    );
113
                    switch ($defaultValue) {
114
                        case 'datetime':
115
                            $value = $date.' '.$time;
116
                            break;
117
                        case 'date':
118
                            $value = $date;
119
                            break;
120
                        case 'time':
121
                            $value = $time;
122
                            break;
123
                    }
124
                    break;
125
            }
126
        }
127
        if (in_array($defaultValue, ['true', 'date', 'time', 'datetime'])) {
128
            return $value;
129
        } else {
130
            return '' !== $value ? $value : $defaultValue;
131
        }
132
    }
133
134
    /**
135
     * @param $match
136
     *
137
     * @return string
138
     */
139
    private static function getTokenDefaultValue($match)
140
    {
141
        $fallbackCheck = explode('|', $match);
142
        if (!isset($fallbackCheck[1])) {
143
            return '';
144
        }
145
146
        return $fallbackCheck[1];
147
    }
148
149
    /**
150
     * @param $match
151
     *
152
     * @return mixed
153
     */
154
    private static function getFieldAlias($match)
155
    {
156
        $fallbackCheck = explode('|', $match);
157
158
        return $fallbackCheck[0];
159
    }
160
161
    /**
162
     * @param string $parameter
163
     *
164
     * @return mixed
165
     */
166
    private static function getParameter($parameter)
167
    {
168
        if (null === self::$parameters) {
0 ignored issues
show
The condition null === self::parameters is always false.
Loading history...
169
            self::$parameters = (new ParamsLoaderHelper())->getParameters();
170
        }
171
172
        return self::$parameters[$parameter];
173
    }
174
}
175