Merge pull request #14023 from seamuslee001/core_dedupe_custom
[civicrm-core.git] / CRM / Event / Import / Field.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Event_Import_Field
30 */
31 class CRM_Event_Import_Field {
32
33 /**
34 * #@+
35 * @var string
36 */
37
38 /**
39 * Name of the field
40 * @var string
41 */
42 public $_name;
43
44 /**
45 * Title of the field to be used in display
46 * @var string
47 */
48 public $_title;
49
50 /**
51 * Type of field
52 * @var enum
53 */
54 public $_type;
55
56 /**
57 * Regexp to match the CSV header of this column/field
58 * @var string
59 */
60 public $_headerPattern;
61
62 /**
63 * Regexp to match the pattern of data from various column/fields
64 * @var string
65 */
66 public $_dataPattern;
67
68 /**
69 * Value of this field
70 * @var object
71 */
72 public $_value;
73
74 /**
75 * @param string $name
76 * @param $title
77 * @param int $type
78 * @param string $headerPattern
79 * @param string $dataPattern
80 */
81 public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
82 $this->_name = $name;
83 $this->_title = $title;
84 $this->_type = $type;
85 $this->_headerPattern = $headerPattern;
86 $this->_dataPattern = $dataPattern;
87
88 $this->_value = NULL;
89 }
90
91 public function resetValue() {
92 $this->_value = NULL;
93 }
94
95 /**
96 * Convert the value to the type of this field and set the field value with the appropriate type.
97 *
98 * @param string $value
99 */
100 public function setValue($value) {
101 $this->_value = $value;
102 }
103
104 /**
105 * @return bool
106 */
107 public function validate() {
108 if (CRM_Utils_System::isNull($this->_value)) {
109 return TRUE;
110 }
111
112 switch ($this->_name) {
113 case 'contact_id':
114 // note: we validate extistence of the contact in API, upon
115 // insert (it would be too costlty to do a db call here)
116 return CRM_Utils_Rule::integer($this->_value);
117
118 case 'register_date':
119 return CRM_Utils_Rule::date($this->_value);
120
121 /* @codingStandardsIgnoreStart
122 case 'event_id':
123 static $events = null;
124 if (!$events) {
125 $events = CRM_Event_PseudoConstant::event();
126 }
127 if (in_array($this->_value, $events)) {
128 return true;
129 }
130 else {
131 return false;
132 }
133 break;
134 @codingStandardsIgnoreEnd */
135
136 default:
137 break;
138 }
139
140 // check whether that's a valid custom field id
141 // and if so, check the contents' validity
142 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) {
143 static $customFields = NULL;
144 if (!$customFields) {
145 $customFields = CRM_Core_BAO_CustomField::getFields('Participant');
146 }
147 if (!array_key_exists($customFieldID, $customFields)) {
148 return FALSE;
149 }
150 return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value);
151 }
152 return TRUE;
153 }
154
155 }