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