trigger.install 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the trigger module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function trigger_schema() {
  10. // The total index length (hook and aid) must be less than 333. Since the aid
  11. // field is 255 characters, the hook field can have a maximum length of 78.
  12. $schema['trigger_assignments'] = array(
  13. 'description' => 'Maps trigger to hook and operation assignments from trigger.module.',
  14. 'fields' => array(
  15. 'hook' => array(
  16. 'type' => 'varchar',
  17. 'length' => 78,
  18. 'not null' => TRUE,
  19. 'default' => '',
  20. 'description' => 'Primary Key: The name of the internal Drupal hook; for example, node_insert.',
  21. ),
  22. 'aid' => array(
  23. 'type' => 'varchar',
  24. 'length' => 255,
  25. 'not null' => TRUE,
  26. 'default' => '',
  27. 'description' => "Primary Key: Action's {actions}.aid.",
  28. ),
  29. 'weight' => array(
  30. 'type' => 'int',
  31. 'not null' => TRUE,
  32. 'default' => 0,
  33. 'description' => 'The weight of the trigger assignment in relation to other triggers.',
  34. ),
  35. ),
  36. 'primary key' => array('hook', 'aid'),
  37. 'foreign keys' => array(
  38. 'action' => array(
  39. 'table' => 'actions',
  40. 'columns' => array('aid' => 'aid'),
  41. ),
  42. ),
  43. );
  44. return $schema;
  45. }
  46. /**
  47. * Implements hook_install().
  48. */
  49. function trigger_install() {
  50. // Do initial synchronization of actions in code and the database.
  51. actions_synchronize();
  52. }
  53. /**
  54. * Alter the "hook" field and drop the "op field" of {trigger_assignments}.
  55. *
  56. * Increase the length of the "hook" field to 78 characters and adds operation
  57. * names to the hook names, and drops the "op" field.
  58. */
  59. function trigger_update_7000() {
  60. db_drop_primary_key('trigger_assignments');
  61. db_change_field('trigger_assignments', 'hook', 'hook', array('type' => 'varchar', 'length' => 78, 'not null' => TRUE, 'default' => '', 'description' => 'Primary Key: The name of the internal Drupal hook; for example, node_insert.'));
  62. $result = db_query("SELECT hook, op, aid FROM {trigger_assignments} WHERE op <> ''");
  63. foreach ($result as $record) {
  64. db_update('trigger_assignments')
  65. ->fields(array('hook' => $record->hook . '_' . $record->op))
  66. ->condition('hook', $record->hook)
  67. ->condition('op', $record->op)
  68. ->condition('aid', $record->aid)
  69. ->execute();
  70. }
  71. db_drop_field('trigger_assignments', 'op');
  72. db_add_primary_key('trigger_assignments', array('hook', 'aid'));
  73. }
  74. /**
  75. * @addtogroup updates-7.x-extra
  76. * @{
  77. */
  78. /**
  79. * Increase the length of the "hook" field to 78 characters.
  80. *
  81. * This is a separate function for those who ran an older version of
  82. * trigger_update_7000() that did not do this.
  83. */
  84. function trigger_update_7001() {
  85. db_drop_primary_key('trigger_assignments');
  86. db_change_field('trigger_assignments', 'hook', 'hook', array('type' => 'varchar', 'length' => 78, 'not null' => TRUE, 'default' => '', 'description' => 'Primary Key: The name of the internal Drupal hook; for example, node_insert.', ), array('primary key' => array('hook', 'aid')));
  87. }
  88. /**
  89. * Renames nodeapi to node.
  90. */
  91. function trigger_update_7002() {
  92. $result = db_query("SELECT hook, aid FROM {trigger_assignments}");
  93. foreach($result as $record) {
  94. $new_hook = str_replace('nodeapi', 'node', $record->hook);
  95. db_update('trigger_assignments')
  96. ->fields(array('hook' => $new_hook))
  97. ->condition('hook', $record->hook)
  98. ->condition('aid', $record->aid)
  99. ->execute();
  100. }
  101. }
  102. /**
  103. * @} End of "addtogroup updates-7.x-extra".
  104. */