Merge pull request #23616 from pradpnayak/dev2540-1
[civicrm-core.git] / CRM / Event / Import / Field.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
0cf587a7
EM
11
12/**
13 * Class CRM_Event_Import_Field
14 */
6a488035
TO
15class CRM_Event_Import_Field {
16
90b461f1
SL
17 /**
18 * #@+
6a488035
TO
19 * @var string
20 */
21
22 /**
100fef9d 23 * Name of the field
90b461f1 24 * @var string
6a488035
TO
25 */
26 public $_name;
27
28 /**
100fef9d 29 * Title of the field to be used in display
90b461f1 30 * @var string
6a488035
TO
31 */
32 public $_title;
33
34 /**
100fef9d 35 * Type of field
5ebfe76b 36 * @var int
6a488035
TO
37 */
38 public $_type;
39
40 /**
100fef9d 41 * Regexp to match the CSV header of this column/field
6a488035
TO
42 * @var string
43 */
44 public $_headerPattern;
45
46 /**
100fef9d 47 * Regexp to match the pattern of data from various column/fields
6a488035
TO
48 * @var string
49 */
50 public $_dataPattern;
51
52 /**
100fef9d 53 * Value of this field
5ebfe76b 54 * @var string|null
6a488035
TO
55 */
56 public $_value;
0cf587a7
EM
57
58 /**
100fef9d 59 * @param string $name
5ebfe76b 60 * @param string $title
0cf587a7
EM
61 * @param int $type
62 * @param string $headerPattern
63 * @param string $dataPattern
64 */
00be9182 65 public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
6a488035
TO
66 $this->_name = $name;
67 $this->_title = $title;
68 $this->_type = $type;
69 $this->_headerPattern = $headerPattern;
70 $this->_dataPattern = $dataPattern;
71
72 $this->_value = NULL;
73 }
74
00be9182 75 public function resetValue() {
6a488035
TO
76 $this->_value = NULL;
77 }
78
79 /**
ad37ac8e 80 * Convert the value to the type of this field and set the field value with the appropriate type.
81 *
82 * @param string $value
6a488035 83 */
00be9182 84 public function setValue($value) {
6a488035
TO
85 $this->_value = $value;
86 }
87
0cf587a7
EM
88 /**
89 * @return bool
90 */
00be9182 91 public function validate() {
6a488035
TO
92 if (CRM_Utils_System::isNull($this->_value)) {
93 return TRUE;
94 }
95
96 switch ($this->_name) {
97 case 'contact_id':
5ebfe76b
BT
98 // note: we validate existence of the contact in API, upon
99 // insert (it would be too costly to do a db call here)
6a488035
TO
100 return CRM_Utils_Rule::integer($this->_value);
101
102 case 'register_date':
103 return CRM_Utils_Rule::date($this->_value);
ea100cb5 104
e70a7fc0 105 /* @codingStandardsIgnoreStart
6a488035
TO
106 case 'event_id':
107 static $events = null;
108 if (!$events) {
109 $events = CRM_Event_PseudoConstant::event();
110 }
111 if (in_array($this->_value, $events)) {
112 return true;
0db6c3e1
TO
113 }
114 else {
6a488035
TO
115 return false;
116 }
117 break;
e70a7fc0 118 @codingStandardsIgnoreEnd */
6a488035
TO
119
120 default:
121 break;
122 }
123
124 // check whether that's a valid custom field id
125 // and if so, check the contents' validity
126 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) {
127 static $customFields = NULL;
128 if (!$customFields) {
719a6fec 129 $customFields = CRM_Core_BAO_CustomField::getFields('Participant');
6a488035
TO
130 }
131 if (!array_key_exists($customFieldID, $customFields)) {
132 return FALSE;
133 }
134 return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value);
135 }
136 return TRUE;
137 }
96025800 138
6a488035 139}