PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Event/Import/Field.php

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