fe_profile.module 2.4 KB

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