features.contact.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file
  4. * Features integration for 'contact' module.
  5. */
  6. /**
  7. * Implements hook_features_api().
  8. */
  9. function contact_features_api() {
  10. return array(
  11. 'contact_categories' => array(
  12. 'name' => t('Contact categories'),
  13. 'feature_source' => TRUE,
  14. 'default_hook' => 'contact_categories_defaults',
  15. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_features_export_options().
  21. */
  22. function contact_categories_features_export_options() {
  23. $options = array();
  24. $categories = db_select('contact', 'c')->fields('c')->execute()->fetchAll();
  25. foreach ($categories as $category) {
  26. $options["$category->category"] = "$category->category";
  27. }
  28. return $options;
  29. }
  30. /**
  31. * Implements hook_features_export().
  32. */
  33. function contact_categories_features_export($data, &$export, $module_name = '') {
  34. $export['dependencies']['features'] = 'features';
  35. $export['dependencies']['contact'] = 'contact';
  36. foreach ($data as $name) {
  37. $export['features']['contact_categories'][$name] = $name;
  38. }
  39. return array();
  40. }
  41. /**
  42. * Implements hook_features_export_render().
  43. */
  44. function contact_categories_features_export_render($module, $data, $export = NULL) {
  45. $render = array();
  46. foreach ($data as $name) {
  47. $export_category = db_select('contact', 'c')
  48. ->fields('c', array('cid', 'category'))
  49. ->condition('category', $name, 'LIKE')
  50. ->execute()
  51. ->fetchAll();
  52. if (isset($export_category[0]->cid) && ($category = contact_load($export_category[0]->cid))) {
  53. unset($category['cid']);
  54. $render[$name] = $category;
  55. }
  56. }
  57. return array('contact_categories_defaults' => ' return ' . features_var_export($render, ' ') . ';');
  58. }
  59. /**
  60. * Implements hook_features_revert().
  61. */
  62. function contact_categories_features_revert($module) {
  63. return contact_categories_features_rebuild($module);
  64. }
  65. /**
  66. * Implements hook_features_rebuild().
  67. */
  68. function contact_categories_features_rebuild($module) {
  69. if ($defaults = features_get_default('contact_categories', $module)) {
  70. foreach ($defaults as $default_category) {
  71. $existing_categories = db_select('contact', 'c')
  72. ->fields('c', array('cid', 'category'))
  73. ->execute()
  74. ->fetchAll();
  75. if ($existing_categories) {
  76. foreach ($existing_categories as $existing_category) {
  77. if ($default_category['category'] == $existing_category->category) {
  78. db_update('contact')
  79. ->fields(
  80. array(
  81. 'recipients' => $default_category['recipients'],
  82. 'reply' => $default_category['reply'],
  83. 'weight' => $default_category['weight'],
  84. 'selected' => $default_category['selected'],
  85. )
  86. )
  87. ->condition('category', $existing_category->category, '=')
  88. ->execute();
  89. }
  90. else {
  91. db_merge('contact')
  92. ->key(array('category' => $default_category['category']))
  93. ->fields($default_category)
  94. ->execute();
  95. }
  96. }
  97. }
  98. else {
  99. db_merge('contact')
  100. ->key(array('category' => $default_category['category']))
  101. ->fields($default_category)
  102. ->execute();
  103. }
  104. }
  105. }
  106. }