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