fe_profile.module 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Main functions and hook implementations for FE Profile.
  5. */
  6. /**
  7. * Implements hook_features_api().
  8. */
  9. function fe_profile_features_api() {
  10. return array(
  11. 'fe_profile' => array(
  12. 'name' => t('Profile fields'),
  13. 'feature_source' => TRUE,
  14. 'default_hook' => 'fe_profile_export_fields',
  15. 'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_features_export_options().
  21. */
  22. function fe_profile_features_export_options() {
  23. $options = array();
  24. $table = 'profile_field';
  25. $query = "SELECT * FROM {{$table}}";
  26. $fields = db_query($query);
  27. while ($row = $fields->fetchObject()) {
  28. $options[$row->name] = $row->name;
  29. }
  30. return $options;
  31. }
  32. /**
  33. * Implements hook_features_export().
  34. */
  35. function fe_profile_features_export($data, &$export, $module_name = '') {
  36. $export['dependencies']['profile'] = 'profile';
  37. foreach ($data as $machine_name) {
  38. $export['features']['fe_profile'][$machine_name] = $machine_name;
  39. }
  40. return array();
  41. }
  42. /**
  43. * Implements hook_features_export_render().
  44. */
  45. function fe_profile_features_export_render($module_name, $data) {
  46. $items = array();
  47. $table = 'profile_field';
  48. foreach ($data as $key) {
  49. $field = db_query("SELECT * FROM {{$table}} WHERE name = :profile_field_name", array(':profile_field_name' => $key))->fetchObject();
  50. $items[$key] = $field;
  51. }
  52. $code = " \$items = " . features_var_export($items, ' ') . ";\n";
  53. $code .= ' return $items;';
  54. return array('fe_profile_export_fields' => $code);
  55. }
  56. /**
  57. * Implements hook_features_revert().
  58. */
  59. function fe_profile_features_revert($module) {
  60. $table = 'profile_field';
  61. $defaults = features_get_default('fe_profile', $module);
  62. if (empty($defaults)) {
  63. return;
  64. }
  65. // Revert.
  66. foreach ($defaults as $object) {
  67. _fe_profile_save_field($object);
  68. }
  69. }
  70. /**
  71. * Saves a profile field to the database.
  72. *
  73. * @param array $field_data
  74. * The field data to save.
  75. */
  76. function _fe_profile_save_field($field_data) {
  77. if (!isset($field_data['options'])) {
  78. $field_data['options'] = '';
  79. }
  80. if (!isset($field_data['page'])) {
  81. $field_data['page'] = '';
  82. }
  83. if (!isset($field_data['fid'])) {
  84. $field_data = (object) $field_data;
  85. drupal_write_record('profile_field', $field_data);
  86. }
  87. else {
  88. $field_data = (object) $field_data;
  89. if (!db_query("SELECT * FROM {profile_field} WHERE fid = :fid", array(':fid' => $field_data->fid))->fetchObject()) {
  90. drupal_write_record('profile_field', $field_data);
  91. }
  92. else {
  93. drupal_write_record('profile_field', $field_data, array('fid'));
  94. }
  95. }
  96. }