flag.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @file
  4. * Flag module integration
  5. */
  6. /**
  7. * Destination class implementing when you want just an insert into flag_content table.
  8. */
  9. class MigrateDestinationFlagSimple extends MigrateDestination {
  10. public function __construct($fid) {
  11. parent::__construct();
  12. $this->fid = $fid;
  13. }
  14. public function __toString() {
  15. $flag = flag_get_flag(NULL, $this->fid);
  16. return t('flag (!flag)', array('!flag' => $flag->name));
  17. }
  18. static public function getKeySchema() {
  19. return array(
  20. 'fcid' => array(
  21. 'type' => 'int',
  22. 'not null' => TRUE,
  23. ),
  24. );
  25. }
  26. /**
  27. * Delete a membership.
  28. *
  29. * @param $id
  30. * IDs to be deleted.
  31. */
  32. public function bulkRollback(array $ids) {
  33. migrate_instrument_start(__METHOD__);
  34. db_delete('flag_content')
  35. ->condition('fcid', $ids, 'IN')
  36. ->execute();
  37. migrate_instrument_stop(__METHOD__);
  38. }
  39. /**
  40. * Import a single flag_content.
  41. *
  42. * @param $entity
  43. * Object object to build. Prefilled with any fields mapped in the Migration.
  44. * @param $row
  45. * Raw source data object - passed through to prepare/complete handlers.
  46. * @return array
  47. * Array of key fields of the object that was saved if
  48. * successful. FALSE on failure.
  49. */
  50. public function import(stdClass $entity, stdClass $row) {
  51. if (isset($entity->timestamp)) {
  52. $entity->timestamp = Migration::timestamp($entity->timestamp);
  53. }
  54. $entity->fid = $this->fid;
  55. if (!empty($entity->fcid)) {
  56. $return = drupal_write_record('flag_content', $entity, 'fcid');
  57. }
  58. else {
  59. $return = drupal_write_record('flag_content', $entity);
  60. }
  61. if ($return) {
  62. // Update the flag_counts table.
  63. $count = db_select('flag_content')
  64. ->fields('flag_content')
  65. ->condition('fid', $this->fid)
  66. ->condition('content_type', $entity->content_type)
  67. ->condition('content_id', $entity->content_id)
  68. ->countQuery()
  69. ->execute()
  70. ->fetchField();
  71. db_merge('flag_counts')
  72. ->key(array(
  73. 'fid' => $this->fid,
  74. 'content_id' => $entity->content_id,
  75. ))
  76. ->fields(array(
  77. 'content_type' => $entity->content_type,
  78. 'count' => $count,
  79. 'last_updated' => REQUEST_TIME,
  80. ))
  81. ->execute();
  82. return array($entity->fcid);
  83. }
  84. }
  85. public function fields() {
  86. return array(
  87. 'fcid' => 'Flag content ID',
  88. 'fid' => 'Flag ID',
  89. 'content_type' => '',
  90. 'content_id' => '',
  91. 'uid' => 'User ID',
  92. 'timestamp' => '',
  93. );
  94. }
  95. }
  96. /**
  97. * Field handler - this will expose flags as fields on the object they're
  98. * attached to, and set the flag to the value mapped in addFieldMapping().
  99. */
  100. abstract class MigrateExtrasFlagHandler extends MigrateDestinationHandler {
  101. /**
  102. * Make the flags assigned to this object visible.
  103. */
  104. public function fields($entity_type, $bundle) {
  105. $fields = array();
  106. if (module_exists('flag')) {
  107. $flags = flag_get_flags($entity_type, $bundle);
  108. foreach ($flags as $flag_name => $flag) {
  109. $fields[$flag_name] = $flag->title;
  110. }
  111. }
  112. return $fields;
  113. }
  114. }
  115. /**
  116. * Because we can't identify what kind of entity is passed to complete, we
  117. * implement a separate handler for each type.
  118. */
  119. class MigrateExtrasNodeFlagHandler extends MigrateExtrasFlagHandler {
  120. public function __construct() {
  121. $this->registerTypes(array('node'));
  122. }
  123. public function complete($node, stdClass $row) {
  124. if (!module_exists('flag')) return;
  125. $flags = flag_get_flags('node', $node->type);
  126. $fields = array();
  127. foreach ($flags as $flag_name => $flag) {
  128. if (!empty($node->$flag_name)) {
  129. flag('flag', $flag_name, $node->nid);
  130. }
  131. }
  132. }
  133. }
  134. class MigrateExtrasUserFlagHandler extends MigrateExtrasFlagHandler {
  135. public function __construct() {
  136. $this->registerTypes(array('user'));
  137. }
  138. public function complete($user, stdClass $row) {
  139. if (!module_exists('flag')) return;
  140. $flags = flag_get_flags('user', 'user');
  141. $fields = array();
  142. foreach ($flags as $flag_name => $flag) {
  143. if (!empty($user->$flag_name)) {
  144. flag('flag', $flag_name, $user->uid);
  145. }
  146. }
  147. }
  148. }