profile2.devel.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @file
  4. * Contains integration with Devel generate modules.
  5. * Provides possibility to generate dummy profiles for users.
  6. */
  7. /**
  8. * Form that allows to generate a user profiles with dummy data.
  9. */
  10. function profile2_generate_form($form, &$form_state) {
  11. // Generate a list with available profile types.
  12. $profile_types = profile2_get_types();
  13. foreach ($profile_types as $id => $type) {
  14. $profile_types[$id] = $type->label;
  15. }
  16. $form['profile2_types'] = array(
  17. '#type' => 'checkboxes',
  18. '#title' => t('Generate profiles of the following types'),
  19. '#description' => t('Select profile type(s) to create profile. If no types are selected, profiles of all types will be generated.'),
  20. '#options' => $profile_types,
  21. );
  22. $roles_list = user_roles(TRUE);
  23. // Don't show authorized role.
  24. unset($roles_list[DRUPAL_AUTHENTICATED_RID]);
  25. $form['profile2_roles'] = array(
  26. '#type' => 'checkboxes',
  27. '#title' => t('Generate profiles for following user roles'),
  28. '#options' => $roles_list,
  29. );
  30. $form['profile2_generate_limit'] = array(
  31. '#type' => 'textfield',
  32. '#title' => t('Maximum number of profiles per type'),
  33. '#element_validate' => array('element_validate_integer_positive'),
  34. '#default_value' => 50,
  35. '#size' => 10,
  36. );
  37. $form['profile2_delete'] = array(
  38. '#type' => 'checkbox',
  39. '#title' => t('Delete existing profiles'),
  40. );
  41. $form['actions'] = array(
  42. '#type' => 'actions',
  43. );
  44. $form['actions']['submit'] = array(
  45. '#type' => 'submit',
  46. '#value' => t('Generate'),
  47. );
  48. return $form;
  49. }
  50. /**
  51. * Submit callback for profile2_generate_form().
  52. * Generates profiles for users.
  53. */
  54. function profile2_generate_form_submit($form, &$form_state) {
  55. $values = $form_state['values'];
  56. // Initial database query that allows to fetch all user ids except anonymous.
  57. $query = db_select('users', 'u');
  58. $query->fields('u', array('uid'));
  59. $query->condition('u.uid', 0, '<>');
  60. // If user selected certain user roles - we need to filter by them.
  61. $roles_selected = array_filter($values['profile2_roles']);
  62. if (!empty($roles_selected)) {
  63. $query->innerJoin('users_roles', 'ur', 'ur.uid = u.uid');
  64. $query->condition('ur.rid', $roles_selected);
  65. }
  66. // Fetch uids for which profiles should be generated.
  67. $uids = $query->execute()->fetchCol('uid');
  68. // Delete all profiles before generation.
  69. if (!empty($values['profile2_delete'])) {
  70. $profile_ids = db_select('profile')
  71. ->fields('profile', array('pid'))
  72. ->execute()
  73. ->fetchCol('pid');
  74. profile2_delete_multiple($profile_ids);
  75. // Set message that indicates how much profiles were deleted.
  76. $message = format_plural(count($profile_ids), t('1 profile was deleted.'), t('@count profiles were deleted.'));
  77. drupal_set_message($message);
  78. }
  79. $new_pids = array();
  80. if (!empty($uids)) {
  81. // Get selected profile types. Load them all if no profile type was chosen.
  82. $profile_types = array_filter($values['profile2_types']);
  83. if (empty($profile_types)) {
  84. $profile_types = profile2_get_types();
  85. }
  86. // Generate user-defined amount of certain profile types.
  87. foreach ($profile_types as $profile_type_name => $profile_type) {
  88. $counter = 0;
  89. $uids_to_generate = $uids;
  90. while ($counter < $values['profile2_generate_limit'] && !empty($uids_to_generate)) {
  91. $uid = array_shift($uids_to_generate);
  92. // If user already has profile of certain type - skip the generation for it.
  93. if (profile2_load_by_user($uid, $profile_type_name)) {
  94. continue;
  95. }
  96. $profile2 = entity_create('profile2', array('type' => $profile_type_name, 'uid' => $uid));
  97. // Populate all core fields on behalf of field.module.
  98. module_load_include('fields.inc', 'devel_generate');
  99. module_load_include('inc', 'devel_generate');
  100. devel_generate_fields($profile2, 'profile2', $profile2->type);
  101. // Set profile language.
  102. $profile2->language = LANGUAGE_NONE;
  103. // Create new profile of the certain type.
  104. $new_pids[] = entity_save('profile2', $profile2);
  105. // Increase counter of generated profiles of certain type.
  106. $counter++;
  107. }
  108. }
  109. }
  110. // Show message that indicates how much profiles were created.
  111. $message = format_plural(count($new_pids), '1 profile were generated', '@count profiles were generated.');
  112. drupal_set_message($message);
  113. }