field_mapping.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. * Default value for simple mappings, when there is no source mapping or the
  30. * source field is empty. If both this and the sourceField are omitted, the
  31. * mapping is just a stub for annotating the destination field.
  32. *
  33. * @var mixed
  34. */
  35. protected $defaultValue;
  36. public function getDefaultValue() {
  37. return $this->defaultValue;
  38. }
  39. /**
  40. * Separator string. If present, the destination field will be set up as an
  41. * array of values exploded from the corresponding source field.
  42. *
  43. * @var string
  44. */
  45. protected $separator;
  46. public function getSeparator() {
  47. return $this->separator;
  48. }
  49. /**
  50. * Class name of source migration for a field. If present, the value in the
  51. * source field is considered to be a source ID in the mapping table of this
  52. * migration, and the corresponding destination ID will be retrieved.
  53. *
  54. * @var mixed
  55. * An array of source migrations, or string for a single migration.
  56. */
  57. protected $sourceMigration;
  58. public function getSourceMigration() {
  59. return $this->sourceMigration;
  60. }
  61. /**
  62. * Array of callbacks to be called on a source value.
  63. *
  64. * @var string
  65. */
  66. protected $callbacks = array();
  67. public function getCallbacks() {
  68. return $this->callbacks;
  69. }
  70. /**
  71. * An associative array with keys:
  72. * - table: The table for querying for a duplicate.
  73. * - column: The column for querying for a duplicate.
  74. *
  75. * @todo: Let fields declare this data and a replacement pattern. Then
  76. * developers won't have to specify this.
  77. *
  78. * @var string
  79. */
  80. protected $dedupe;
  81. public function getDedupe() {
  82. return $this->dedupe;
  83. }
  84. /**
  85. * Argument overrides. If present this will be an array, keyed by
  86. * a field API array key, with one or both of these entries:
  87. * 'source_field' - Name of the source field in the incoming row containing the
  88. * value to be assigned
  89. * 'default_value' - A constant value to be assigned in the absence of source_field
  90. *
  91. * @var array
  92. */
  93. protected $arguments;
  94. public function getArguments() {
  95. return $this->arguments;
  96. }
  97. protected $description = '';
  98. public function getDescription() {
  99. return $this->description;
  100. }
  101. protected $issueGroup;
  102. public function getIssueGroup() {
  103. return $this->issueGroup;
  104. }
  105. protected $issueNumber;
  106. public function getIssueNumber() {
  107. return $this->issueNumber;
  108. }
  109. protected $issuePriority = self::ISSUE_PRIORITY_OK;
  110. public function getIssuePriority() {
  111. return $this->issuePriority;
  112. }
  113. const ISSUE_PRIORITY_OK = 1;
  114. const ISSUE_PRIORITY_LOW = 2;
  115. const ISSUE_PRIORITY_MEDIUM = 3;
  116. const ISSUE_PRIORITY_BLOCKER = 4;
  117. public static $priorities = array();
  118. public function __construct($destination_field, $source_field) {
  119. // Must have one or the other
  120. if (!$destination_field && !$source_field) {
  121. throw new Exception('Field mappings must have a destination field or a source field');
  122. }
  123. $this->destinationField = $destination_field;
  124. $this->sourceField = $source_field;
  125. $this->issueGroup = t('Done');
  126. if (count(self::$priorities) == 0) {
  127. self::$priorities[self::ISSUE_PRIORITY_OK] = t('OK');
  128. self::$priorities[self::ISSUE_PRIORITY_LOW] = t('Low');
  129. self::$priorities[self::ISSUE_PRIORITY_MEDIUM] = t('Medium');
  130. self::$priorities[self::ISSUE_PRIORITY_BLOCKER] = t('Blocker');
  131. }
  132. }
  133. public function defaultValue($default_value) {
  134. $this->defaultValue = $default_value;
  135. return $this;
  136. }
  137. public function separator($separator) {
  138. $this->separator = $separator;
  139. return $this;
  140. }
  141. public function sourceMigration($source_migration) {
  142. $this->sourceMigration = $source_migration;
  143. return $this;
  144. }
  145. public function callbacks($callbacks) {
  146. $this->callbacks = func_get_args();
  147. return $this;
  148. }
  149. public function dedupe($table, $column) {
  150. $this->dedupe = array('table' => $table, 'column' => $column);
  151. return $this;
  152. }
  153. public function arguments($arguments) {
  154. $this->arguments = $arguments;
  155. return $this;
  156. }
  157. public function description($text) {
  158. $this->description = $text;
  159. return $this;
  160. }
  161. public function issueGroup($group) {
  162. if (!$group) {
  163. $group = t('Done');
  164. }
  165. $this->issueGroup = $group;
  166. return $this;
  167. }
  168. public function issueNumber($number) {
  169. $this->issueNumber = $number;
  170. return $this;
  171. }
  172. public function issuePriority($priority) {
  173. $this->issuePriority = $priority;
  174. return $this;
  175. }
  176. }