Нет описания

CustomField.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Entities\CustomFieldEntity;
  5. use MailPoet\Util\DateConverter;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. /**
  8. * @property string $name
  9. * @property string $type
  10. * @property string|array|null $params
  11. */
  12. class CustomField extends Model {
  13. public static $_table = MP_CUSTOM_FIELDS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
  14. const TYPE_DATE = CustomFieldEntity::TYPE_DATE;
  15. const TYPE_TEXT = CustomFieldEntity::TYPE_TEXT;
  16. const TYPE_TEXTAREA = CustomFieldEntity::TYPE_TEXTAREA;
  17. const TYPE_RADIO = CustomFieldEntity::TYPE_RADIO;
  18. const TYPE_CHECKBOX = CustomFieldEntity::TYPE_CHECKBOX;
  19. const TYPE_SELECT = CustomFieldEntity::TYPE_SELECT;
  20. public function __construct() {
  21. parent::__construct();
  22. $this->addValidations('name', [
  23. 'required' => WPFunctions::get()->__('Please specify a name.', 'mailpoet'),
  24. ]);
  25. $this->addValidations('type', [
  26. 'required' => WPFunctions::get()->__('Please specify a type.', 'mailpoet'),
  27. ]);
  28. }
  29. public function asArray() {
  30. $model = parent::asArray();
  31. if (isset($model['params'])) {
  32. $model['params'] = (is_array($this->params))
  33. ? $this->params
  34. : ($this->params !== null ? unserialize($this->params) : false);
  35. }
  36. return $model;
  37. }
  38. public function save() {
  39. if (is_null($this->params)) {
  40. $this->params = [];
  41. }
  42. $this->set('params', (
  43. is_array($this->params)
  44. ? serialize($this->params)
  45. : $this->params
  46. ));
  47. return parent::save();
  48. }
  49. public function formatValue($value = null) {
  50. // format custom field data depending on type
  51. if (is_array($value) && $this->type === self::TYPE_DATE) {
  52. $customFieldData = $this->asArray();
  53. $dateFormat = $customFieldData['params']['date_format'];
  54. $dateType = (isset($customFieldData['params']['date_type'])
  55. ? $customFieldData['params']['date_type']
  56. : 'year_month_day'
  57. );
  58. $dateParts = explode('_', $dateType);
  59. switch ($dateType) {
  60. case 'year_month_day':
  61. $value = str_replace(
  62. ['DD', 'MM', 'YYYY'],
  63. [$value['day'], $value['month'], $value['year']],
  64. $dateFormat
  65. );
  66. break;
  67. case 'year_month':
  68. $value = str_replace(
  69. ['MM', 'YYYY'],
  70. [$value['month'], $value['year']],
  71. $dateFormat
  72. );
  73. break;
  74. case 'month':
  75. if ((int)$value['month'] === 0) {
  76. $value = '';
  77. } else {
  78. $value = sprintf(
  79. '%s',
  80. $value['month']
  81. );
  82. }
  83. break;
  84. case 'day':
  85. if ((int)$value['day'] === 0) {
  86. $value = '';
  87. } else {
  88. $value = sprintf(
  89. '%s',
  90. $value['day']
  91. );
  92. }
  93. break;
  94. case 'year':
  95. if ((int)$value['year'] === 0) {
  96. $value = '';
  97. } else {
  98. $value = sprintf(
  99. '%04d',
  100. $value['year']
  101. );
  102. }
  103. break;
  104. }
  105. if (!empty($value) && is_string($value)) {
  106. $value = (new DateConverter())->convertDateToDatetime($value, $dateFormat);
  107. }
  108. }
  109. return $value;
  110. }
  111. public function subscribers() {
  112. return $this->hasManyThrough(
  113. __NAMESPACE__ . '\Subscriber',
  114. __NAMESPACE__ . '\SubscriberCustomField',
  115. 'custom_field_id',
  116. 'subscriber_id'
  117. )->selectExpr(MP_SUBSCRIBER_CUSTOM_FIELD_TABLE . '.value');
  118. }
  119. public static function createOrUpdate($data = []) {
  120. // set name as label by default
  121. if (empty($data['params']['label']) && isset($data['name'])) {
  122. $data['params']['label'] = $data['name'];
  123. }
  124. return parent::_createOrUpdate($data);
  125. }
  126. }