INFRA-132 - phpcbf Drupal.WhiteSpace.ScopeIndent.IncorrectExact
[civicrm-core.git] / CRM / Event / Import / Field.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
0cf587a7
EM
27
28/**
29 * Class CRM_Event_Import_Field
30 */
6a488035
TO
31class CRM_Event_Import_Field {
32
33 /**#@+
6a488035
TO
34 * @var string
35 */
36
37 /**
100fef9d 38 * Name of the field
6a488035
TO
39 */
40 public $_name;
41
42 /**
100fef9d 43 * Title of the field to be used in display
6a488035
TO
44 */
45 public $_title;
46
47 /**
100fef9d 48 * Type of field
6a488035
TO
49 * @var enum
50 */
51 public $_type;
52
53 /**
100fef9d 54 * Regexp to match the CSV header of this column/field
6a488035
TO
55 * @var string
56 */
57 public $_headerPattern;
58
59 /**
100fef9d 60 * Regexp to match the pattern of data from various column/fields
6a488035
TO
61 * @var string
62 */
63 public $_dataPattern;
64
65 /**
100fef9d 66 * Value of this field
6a488035
TO
67 * @var object
68 */
69 public $_value;
0cf587a7
EM
70
71 /**
100fef9d 72 * @param string $name
0cf587a7
EM
73 * @param $title
74 * @param int $type
75 * @param string $headerPattern
76 * @param string $dataPattern
77 */
00be9182 78 public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') {
6a488035
TO
79 $this->_name = $name;
80 $this->_title = $title;
81 $this->_type = $type;
82 $this->_headerPattern = $headerPattern;
83 $this->_dataPattern = $dataPattern;
84
85 $this->_value = NULL;
86 }
87
00be9182 88 public function resetValue() {
6a488035
TO
89 $this->_value = NULL;
90 }
91
92 /**
100fef9d 93 * The value is in string format. convert the value to the type of this field
6a488035
TO
94 * and set the field value with the appropriate type
95 */
00be9182 96 public function setValue($value) {
6a488035
TO
97 $this->_value = $value;
98 }
99
0cf587a7
EM
100 /**
101 * @return bool
102 */
00be9182 103 public function validate() {
6a488035
TO
104 if (CRM_Utils_System::isNull($this->_value)) {
105 return TRUE;
106 }
107
108 switch ($this->_name) {
109 case 'contact_id':
110 // note: we validate extistence of the contact in API, upon
111 // insert (it would be too costlty to do a db call here)
112 return CRM_Utils_Rule::integer($this->_value);
113
114 case 'register_date':
115 return CRM_Utils_Rule::date($this->_value);
ea100cb5 116
e70a7fc0 117 /* @codingStandardsIgnoreStart
6a488035
TO
118 case 'event_id':
119 static $events = null;
120 if (!$events) {
121 $events = CRM_Event_PseudoConstant::event();
122 }
123 if (in_array($this->_value, $events)) {
124 return true;
0db6c3e1
TO
125 }
126 else {
6a488035
TO
127 return false;
128 }
129 break;
e70a7fc0 130 @codingStandardsIgnoreEnd */
6a488035
TO
131
132 default:
133 break;
134 }
135
136 // check whether that's a valid custom field id
137 // and if so, check the contents' validity
138 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) {
139 static $customFields = NULL;
140 if (!$customFields) {
719a6fec 141 $customFields = CRM_Core_BAO_CustomField::getFields('Participant');
6a488035
TO
142 }
143 if (!array_key_exists($customFieldID, $customFields)) {
144 return FALSE;
145 }
146 return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value);
147 }
148 return TRUE;
149 }
96025800 150
6a488035 151}