/app/bundles/FormBundle/Helper/FormFieldHelper.php

https://gitlab.com/mautic-master/mautic · PHP · 212 lines · 146 code · 23 blank · 43 comment · 16 complexity · e228673b22e933f2ef0819b3cd966861 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2014 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. namespace Mautic\FormBundle\Helper;
  10. use Symfony\Component\Translation\TranslatorInterface;
  11. use Symfony\Component\Validator\Validation;
  12. /**
  13. * Class FormFieldHelper
  14. */
  15. class FormFieldHelper
  16. {
  17. /**
  18. * @var TranslatorInterface
  19. */
  20. private $translator;
  21. /**
  22. * @var array
  23. */
  24. private $types = array(
  25. 'text' => array(),
  26. 'textarea' => array(),
  27. 'country' => array(),
  28. //'button' => array(),
  29. 'select' => array(),
  30. 'date' => array(),
  31. 'email' => array(
  32. 'filter' => 'email',
  33. 'constraints' => array(
  34. '\Symfony\Component\Validator\Constraints\Email' =>
  35. array('message' => 'mautic.form.submission.email.invalid')
  36. )
  37. ),
  38. 'number' => array(
  39. 'filter' => 'float'
  40. ),
  41. 'tel' => array(),
  42. 'url' => array(
  43. 'filter' => 'url',
  44. 'constraints' => array(
  45. '\Symfony\Component\Validator\Constraints\Url' =>
  46. array('message' => 'mautic.form.submission.url.invalid')
  47. )
  48. ),
  49. 'freetext' => array(),
  50. 'checkboxgrp' => array(),
  51. 'radiogrp' => array(),
  52. 'hidden' => array(),
  53. 'captcha' => array(
  54. 'constraints' => array(
  55. '\Symfony\Component\Validator\Constraints\NotBlank' =>
  56. array('message' => 'mautic.form.submission.captcha.invalid'),
  57. '\Symfony\Component\Validator\Constraints\EqualTo' =>
  58. array('message' => 'mautic.form.submission.captcha.invalid')
  59. )
  60. )
  61. );
  62. /**
  63. * @param TranslatorInterface $translator
  64. */
  65. public function __construct(TranslatorInterface $translator)
  66. {
  67. $this->translator = $translator;
  68. }
  69. /**
  70. * @param array $customFields
  71. *
  72. * @return array
  73. */
  74. public function getList($customFields = array())
  75. {
  76. $choices = array();
  77. foreach ($this->types as $v => $type) {
  78. $choices[$v] = $this->translator->transConditional("mautic.core.type.{$v}", "mautic.form.field.type.{$v}");
  79. }
  80. foreach ($customFields as $v => $f) {
  81. $choices[$v] = $this->translator->trans($f['label']);
  82. }
  83. natcasesort($choices);
  84. return $choices;
  85. }
  86. /**
  87. * @return array
  88. */
  89. public function getTypes()
  90. {
  91. return $this->types;
  92. }
  93. /**
  94. * Get fields input filter
  95. *
  96. * @param $type
  97. *
  98. * @return string
  99. */
  100. public function getFieldFilter($type)
  101. {
  102. if (array_key_exists($type, $this->types)) {
  103. if (isset($this->types[$type]['filter'])) {
  104. return $this->types[$type]['filter'];
  105. }
  106. return 'clean';
  107. }
  108. return 'alphanum';
  109. }
  110. /**
  111. * @param $type
  112. * @param $value
  113. * @param null $f
  114. *
  115. * @return array
  116. */
  117. public function validateFieldValue($type, $value, $f = null)
  118. {
  119. $errors = array();
  120. if (isset($this->types[$type]['constraints'])) {
  121. $validator = Validation::createValidator();
  122. foreach ($this->types[$type]['constraints'] as $constraint => $opts) {
  123. //don't check empty values unless the constraint is NotBlank
  124. if (strpos($constraint, 'NotBlank') === false && empty($value))
  125. continue;
  126. if ($type == 'captcha' && strpos($constraint, 'EqualTo') !== false) {
  127. $props = $f->getProperties();
  128. $opts['value'] = $props['captcha'];
  129. }
  130. $violations = $validator->validateValue($value, new $constraint($opts));
  131. if (count($violations)) {
  132. foreach ($violations as $v) {
  133. $transParameters = $v->getMessageParameters();
  134. if ($f !== null) {
  135. $transParameters['%label%'] = "&quot;" . $f->getLabel() . "&quot;";
  136. }
  137. $errors[] = $this->translator->trans($v->getMessage(), $transParameters, 'validators');
  138. }
  139. }
  140. }
  141. }
  142. return $errors;
  143. }
  144. public function populateField($field, $value, $formName, &$formHtml)
  145. {
  146. $alias = $field->getAlias();
  147. switch ($field->getType()) {
  148. case 'text':
  149. case 'email':
  150. case 'hidden':
  151. if (preg_match('/<input(.*?)id="mauticform_input_' . $formName . '_' . $alias . '"(.*?)value="(.*?)"(.*?)\/>/i', $formHtml, $match)) {
  152. $replace = '<input' . $match[1] . 'id="mauticform_input_' . $formName . '_' . $alias . '"' . $match[2] . 'value="' . urldecode($value) . '"' . $match[4] . '/>';
  153. $formHtml = str_replace($match[0], $replace, $formHtml);
  154. }
  155. break;
  156. case 'textarea':
  157. if (preg_match('/<textarea(.*?)id="mauticform_input_' . $formName . '_' . $alias . '"(.*?)>(.*?)<\/textarea>/i', $formHtml, $match)) {
  158. $replace = '<textarea' . $match[1] . 'id="mauticform_input_' . $formName . '_' . $alias . '"' . $match[2] . '>' . urldecode($value) . '</textarea>';
  159. $formHtml = str_replace($match[0], $replace, $formHtml);
  160. }
  161. break;
  162. case 'checkboxgrp':
  163. if (!is_array($value)) {
  164. $value = array($value);
  165. }
  166. foreach ($value as $val) {
  167. $val = urldecode($val);
  168. if (preg_match(
  169. '/<input(.*?)id="mauticform_checkboxgrp_checkbox(.*?)"(.*?)value="' . $val . '"(.*?)\/>/i',
  170. $formHtml,
  171. $match
  172. )) {
  173. $replace = '<input' . $match[1] . 'id="mauticform_checkboxgrp_checkbox' . $match[2] . '"' . $match[3] . 'value="' . $val . '"'
  174. . $match[4] . ' checked />';
  175. $formHtml = str_replace($match[0], $replace, $formHtml);
  176. }
  177. }
  178. break;
  179. case 'radiogrp':
  180. $value = urldecode($value);
  181. if (preg_match('/<input(.*?)id="mauticform_radiogrp_radio(.*?)"(.*?)value="' . $value . '"(.*?)\/>/i', $formHtml, $match)) {
  182. $replace = '<input' . $match[1] . 'id="mauticform_radiogrp_radio' . $match[2] . '"' . $match[3] . 'value="' . $value . '"' . $match[4]
  183. . ' checked />';
  184. $formHtml = str_replace($match[0], $replace, $formHtml);
  185. }
  186. break;
  187. }
  188. }
  189. }