field_mapping.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @file
  4. * The MigrateFieldMapping class - tracking mappings between source and
  5. * destination.
  6. */
  7. class MigrateFieldMapping {
  8. /**
  9. * Destination field name for the mapping. If empty, the mapping is just a
  10. * stub for annotating the source field.
  11. *
  12. * @var string
  13. */
  14. protected $destinationField;
  15. public function getDestinationField() {
  16. return $this->destinationField;
  17. }
  18. /**
  19. * Source field name for the mapping. If empty, the defaultValue will be
  20. * applied.
  21. *
  22. * @var string
  23. */
  24. protected $sourceField;
  25. public function getSourceField() {
  26. return $this->sourceField;
  27. }
  28. /**
  29. * @var int
  30. */
  31. const MAPPING_SOURCE_CODE = 1;
  32. const MAPPING_SOURCE_DB = 2;
  33. protected $mappingSource = self::MAPPING_SOURCE_CODE;
  34. public function getMappingSource() {
  35. return $this->mappingSource;
  36. }
  37. public function setMappingSource($mapping_source) {
  38. $this->mappingSource = $mapping_source;
  39. }
  40. /**
  41. * Default value for simple mappings, when there is no source mapping or the
  42. * source field is empty. If both this and the sourceField are omitted, the
  43. * mapping is just a stub for annotating the destination field.
  44. *
  45. * @var mixed
  46. */
  47. protected $defaultValue;
  48. public function getDefaultValue() {
  49. return $this->defaultValue;
  50. }
  51. /**
  52. * Separator string. If present, the destination field will be set up as an
  53. * array of values exploded from the corresponding source field.
  54. *
  55. * @var string
  56. */
  57. protected $separator;
  58. public function getSeparator() {
  59. return $this->separator;
  60. }
  61. /**
  62. * Class name of source migration for a field. If present, the value in the
  63. * source field is considered to be a source ID in the mapping table of this
  64. * migration, and the corresponding destination ID will be retrieved.
  65. *
  66. * @var mixed
  67. * An array of source migrations, or string for a single migration.
  68. */
  69. protected $sourceMigration;
  70. public function getSourceMigration() {
  71. return $this->sourceMigration;
  72. }
  73. /**
  74. * Array of callbacks to be called on a source value.
  75. *
  76. * @var string
  77. */
  78. protected $callbacks = array();
  79. public function getCallbacks() {
  80. return $this->callbacks;
  81. }
  82. /**
  83. * An associative array with keys:
  84. * - table: The table for querying for a duplicate.
  85. * - column: The column for querying for a duplicate.
  86. *
  87. * @todo: Let fields declare this data and a replacement pattern. Then
  88. * developers won't have to specify this.
  89. *
  90. * @var string
  91. */
  92. protected $dedupe;
  93. public function getDedupe() {
  94. return $this->dedupe;
  95. }
  96. /**
  97. * Argument overrides. If present this will be an array, keyed by
  98. * a field API array key, with one or both of these entries:
  99. * 'source_field' - Name of the source field in the incoming row containing
  100. * the value to be assigned
  101. * 'default_value' - A constant value to be assigned in the absence of
  102. * source_field Deprecated - subfield notation is now preferred.
  103. *
  104. * @var array
  105. */
  106. protected $arguments;
  107. public function getArguments() {
  108. return $this->arguments;
  109. }
  110. protected $description = '';
  111. public function getDescription() {
  112. return $this->description;
  113. }
  114. protected $issueGroup;
  115. public function getIssueGroup() {
  116. return $this->issueGroup;
  117. }
  118. protected $issueNumber;
  119. public function getIssueNumber() {
  120. return $this->issueNumber;
  121. }
  122. protected $issuePriority = self::ISSUE_PRIORITY_OK;
  123. public function getIssuePriority() {
  124. return $this->issuePriority;
  125. }
  126. const ISSUE_PRIORITY_OK = 1;
  127. const ISSUE_PRIORITY_LOW = 2;
  128. const ISSUE_PRIORITY_MEDIUM = 3;
  129. const ISSUE_PRIORITY_BLOCKER = 4;
  130. public static $priorities = array();
  131. public function __construct($destination_field, $source_field) {
  132. // Must have one or the other
  133. if (!$destination_field && !$source_field) {
  134. throw new Exception('Field mappings must have a destination field or a source field');
  135. }
  136. $this->destinationField = $destination_field;
  137. $this->sourceField = $source_field;
  138. $this->issueGroup = t('Done');
  139. if (count(self::$priorities) == 0) {
  140. self::$priorities[self::ISSUE_PRIORITY_OK] = t('OK');
  141. self::$priorities[self::ISSUE_PRIORITY_LOW] = t('Low');
  142. self::$priorities[self::ISSUE_PRIORITY_MEDIUM] = t('Medium');
  143. self::$priorities[self::ISSUE_PRIORITY_BLOCKER] = t('Blocker');
  144. }
  145. }
  146. public function defaultValue($default_value) {
  147. $this->defaultValue = $default_value;
  148. return $this;
  149. }
  150. public function separator($separator) {
  151. $this->separator = $separator;
  152. return $this;
  153. }
  154. public function sourceMigration($source_migration) {
  155. $this->sourceMigration = $source_migration;
  156. return $this;
  157. }
  158. public function callbacks($callbacks) {
  159. foreach (func_get_args() as $callback) {
  160. $this->callback($callback);
  161. }
  162. return $this;
  163. }
  164. public function callback($callback) {
  165. $this->callbacks[] = array(
  166. 'callback' => $callback,
  167. 'params' => array_slice(func_get_args(), 1),
  168. );
  169. return $this;
  170. }
  171. public function dedupe($table, $column) {
  172. $this->dedupe = array('table' => $table, 'column' => $column);
  173. return $this;
  174. }
  175. public function arguments($arguments) {
  176. if (variable_get('migrate_deprecation_warnings', 1)) {
  177. MigrationBase::displayMessage(t('The field mapping arguments() method is now deprecated - please use subfield notation instead.'));
  178. }
  179. $this->arguments = $arguments;
  180. return $this;
  181. }
  182. public function description($text) {
  183. $this->description = $text;
  184. return $this;
  185. }
  186. public function issueGroup($group) {
  187. if (!$group) {
  188. $group = t('Done');
  189. }
  190. $this->issueGroup = $group;
  191. return $this;
  192. }
  193. public function issueNumber($number) {
  194. $this->issueNumber = $number;
  195. return $this;
  196. }
  197. public function issuePriority($priority) {
  198. $this->issuePriority = $priority;
  199. return $this;
  200. }
  201. }