menu.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Support for menu destinations.
  5. */
  6. /**
  7. * Destination class implementing migration into {menu_custom}.
  8. */
  9. class MigrateDestinationMenu extends MigrateDestination {
  10. static public function getKeySchema() {
  11. return array(
  12. 'menu_name' => array(
  13. 'type' => 'varchar',
  14. 'length' => 32,
  15. 'not null' => TRUE,
  16. 'default' => '',
  17. 'description' => 'Primary Key: Unique key for menu. This is used as a block delta so length is 32.',
  18. ),
  19. );
  20. }
  21. public function __construct() {
  22. parent::__construct();
  23. }
  24. public function __toString() {
  25. $output = t('Menu');
  26. return $output;
  27. }
  28. /**
  29. * Returns a list of fields available to be mapped for menus.
  30. *
  31. * @param Migration $migration
  32. * Optionally, the migration containing this destination.
  33. * @return array
  34. * Keys: machine names of the fields (to be passed to addFieldMapping)
  35. * Values: Human-friendly descriptions of the fields.
  36. */
  37. public function fields($migration = NULL) {
  38. $fields = array(
  39. 'menu_name' => t('The menu name. Primary key.'),
  40. 'title' => t('The human-readable name of the menu.'),
  41. 'description' => t('A description of the menu'),
  42. );
  43. return $fields;
  44. }
  45. /**
  46. * Import a single row.
  47. *
  48. * @param $menu
  49. * Menu object to build. Prefilled with any fields mapped in the Migration.
  50. * @param $row
  51. * Raw source data object - passed through to prepare/complete handlers.
  52. * @return array
  53. * Array of key fields of the object that was saved if
  54. * successful. FALSE on failure.
  55. */
  56. public function import(stdClass $menu, stdClass $row) {
  57. // Invoke migration prepare handlers
  58. $this->prepare($menu, $row);
  59. // Menus are handled as arrays, so clone the object to an array.
  60. $menu = clone $menu;
  61. $menu = (array) $menu;
  62. // Check to see if this is a new menu.
  63. $update = FALSE;
  64. if ($data = menu_load($menu['menu_name'])) {
  65. $update = TRUE;
  66. }
  67. // menu_save() provides no return callback, so we can't really test this
  68. // without running a menu_load() check.
  69. migrate_instrument_start('menu_save');
  70. menu_save($menu);
  71. migrate_instrument_stop('menu_save');
  72. // Return the new id or FALSE on failure.
  73. if ($data = menu_load($menu['menu_name'])) {
  74. // Increment the count if the save succeeded.
  75. if ($update) {
  76. $this->numUpdated++;
  77. }
  78. else {
  79. $this->numCreated++;
  80. }
  81. // Return the primary key to the mapping table.
  82. $return = array($data['menu_name']);
  83. }
  84. else {
  85. $return = FALSE;
  86. }
  87. // Invoke migration complete handlers.
  88. $menu = (object) $data;
  89. $this->complete($menu, $row);
  90. return $return;
  91. }
  92. /**
  93. * Implementation of MigrateDestination::prepare().
  94. */
  95. public function prepare($menu, stdClass $row) {
  96. // We do nothing here but allow child classes to act.
  97. $migration = Migration::currentMigration();
  98. $menu->migrate = array(
  99. 'machineName' => $migration->getMachineName(),
  100. );
  101. // Call any general handlers.
  102. migrate_handler_invoke_all('menu', 'prepare', $menu, $row);
  103. // Then call any prepare handler for this specific Migration.
  104. if (method_exists($migration, 'prepare')) {
  105. $migration->prepare($menu, $row);
  106. }
  107. }
  108. public function complete($menu, stdClass $row) {
  109. // We do nothing here but allow child classes to act.
  110. $migration = Migration::currentMigration();
  111. $menu->migrate = array(
  112. 'machineName' => $migration->getMachineName(),
  113. );
  114. // Call any general handlers.
  115. migrate_handler_invoke_all('menu', 'complete', $menu, $row);
  116. // Then call any complete handler for this specific Migration.
  117. if (method_exists($migration, 'complete')) {
  118. $migration->complete($menu, $row);
  119. }
  120. }
  121. /**
  122. * Delete a single menu.
  123. *
  124. * @param $id
  125. * Array of fields representing the key (in this case, just menu_name).
  126. */
  127. public function rollback(array $id) {
  128. $menu_name = reset($id);
  129. migrate_instrument_start('menu_delete');
  130. $this->prepareRollback($menu_name);
  131. if ($menu = menu_load($menu_name)) {
  132. menu_delete($menu);
  133. }
  134. $this->completeRollback($menu_name);
  135. migrate_instrument_stop('menu_delete');
  136. }
  137. /**
  138. * Give handlers a shot at cleaning up before a menu has been rolled back.
  139. *
  140. * @param $menu_name
  141. * ID of the menu about to be deleted.
  142. */
  143. public function prepareRollback($menu_name) {
  144. // We do nothing here but allow child classes to act.
  145. $migration = Migration::currentMigration();
  146. // Call any general handlers.
  147. migrate_handler_invoke_all('menu', 'prepareRollback', $menu_name);
  148. // Then call any complete handler for this specific Migration.
  149. if (method_exists($migration, 'prepareRollback')) {
  150. $migration->prepareRollback($menu_name);
  151. }
  152. }
  153. /**
  154. * Give handlers a shot at cleaning up after a menu has been rolled back.
  155. *
  156. * @param $menu_name
  157. * ID of the menu which has been deleted.
  158. */
  159. public function completeRollback($menu_name) {
  160. // We do nothing here but allow child classes to act.
  161. $migration = Migration::currentMigration();
  162. // Call any general handlers.
  163. migrate_handler_invoke_all('menu', 'completeRollback', $menu_name);
  164. // Then call any complete handler for this specific Migration.
  165. if (method_exists($migration, 'completeRollback')) {
  166. $migration->completeRollback($menu_name);
  167. }
  168. }
  169. }