| 0e31af24 |
1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Form Builder; Integrates with Devel Generate. |
| 5 | * |
| 6 | * @see privatemsg_devel_generate_form_validate() |
| 7 | * @see privatemsg_devel_generate_form_submit() |
| 8 | */ |
| 9 | function privatemsg_devel_generate_form() { |
| 10 | |
| 11 | // Check if authenticated users can write new messages. |
| 12 | $perm = 'write privatemsg'; |
| 13 | $role_perms = user_role_permissions(array(DRUPAL_AUTHENTICATED_RID => 'authenticated user')); |
| 14 | $perms = $role_perms[DRUPAL_AUTHENTICATED_RID]; |
| 15 | |
| 16 | if (!in_array($perm, $perms)) { |
| 17 | drupal_set_message( |
| 18 | t('Authenticated users do not have %perm permission. This function will not run correctly. <a href="@url">Permissions page</a>.', |
| 19 | array( |
| 20 | '%perm' => $perm, |
| 21 | '@url' => url( |
| 22 | 'admin/people/permissions', |
| 23 | array( |
| 24 | 'fragment' => 'module-privatemsg', |
| 25 | 'query' => array( |
| 26 | 'destination' => 'admin/generate/privatemsg' |
| 27 | ) |
| 28 | ) |
| 29 | ) |
| 30 | ) |
| 31 | ), |
| 32 | 'warning'); |
| 33 | } |
| 34 | |
| 35 | $options = array(); |
| 36 | for ($i = 1; $i <= 10; $i++) { |
| 37 | $options[$i] = $i; |
| 38 | } |
| 39 | |
| 40 | $form['kill_content'] = array( |
| 41 | '#type' => 'checkbox', |
| 42 | '#title' => t('<strong>Delete all messages</strong> before generating new messages.'), |
| 43 | '#default_value' => FALSE, |
| 44 | ); |
| 45 | $form['num_threads'] = array( |
| 46 | '#type' => 'textfield', |
| 47 | '#title' => t('How many threads would you like to generate?'), |
| 48 | '#default_value' => 10, |
| 49 | '#size' => 10, |
| 50 | ); |
| 51 | $form['max_thread_length'] = array( |
| 52 | '#type' => 'textfield', |
| 53 | '#title' => t('Max thread length?'), |
| 54 | '#default_value' => 5, |
| 55 | '#size' => 10, |
| 56 | ); |
| 57 | $form['recipients'] = array( |
| 58 | '#type' => 'fieldset', |
| 59 | '#title' => t('Recipients'), |
| 60 | '#description' => t('A random number of recipients will be generated for each message.'), |
| 61 | ); |
| 62 | $form['recipients']['min_recipients'] = array( |
| 63 | '#type' => 'select', |
| 64 | '#title' => t('Minimum number of recipients for each message'), |
| 65 | '#default_value' => 1, |
| 66 | '#options' => $options, |
| 67 | ); |
| 68 | $form['recipients']['max_recipients'] = array( |
| 69 | '#type' => 'select', |
| 70 | '#title' => t('Maximum number of recipients for each message'), |
| 71 | '#default_value' => 3, |
| 72 | '#options' => $options, |
| 73 | ); |
| 74 | |
| 75 | $options = array(1 => t('Now')); |
| 76 | foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) { |
| 77 | $options[$interval] = format_interval($interval, 1) . ' ' . t('ago'); |
| 78 | } |
| 79 | $form['time_range'] = array( |
| 80 | '#type' => 'select', |
| 81 | '#title' => t('How far back in time should the messages be dated?'), |
| 82 | '#description' => t('Message creation dates will be distributed randomly from the current time, back to the selected time.'), |
| 83 | '#options' => $options, |
| 84 | '#default_value' => 604800, |
| 85 | ); |
| 86 | |
| 87 | $form['subject_length'] = array( |
| 88 | '#type' => 'textfield', |
| 89 | '#title' => t('Max word length of subjects'), |
| 90 | '#default_value' => 8, |
| 91 | '#size' => 10, |
| 92 | ); |
| 93 | |
| 94 | $form['submit'] = array( |
| 95 | '#type' => 'submit', |
| 96 | '#value' => t('Generate'), |
| 97 | ); |
| 98 | $form['#redirect'] = FALSE; |
| 99 | |
| 100 | return $form; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Validate; Integrates with Devel Generate. |
| 105 | * |
| 106 | * @see privatemsg_devel_generate_form() |
| 107 | * @see privatemsg_devel_generate_form_submit() |
| 108 | */ |
| 109 | function privatemsg_devel_generate_form_validate($form_id, &$form_state) { |
| 110 | module_load_include('inc', 'devel_generate'); |
| 111 | $uids = devel_get_users(); |
| 112 | $max_recipients = $form_state['values']['max_recipients']; |
| 113 | |
| 114 | // Ensure that there are enough users to fit the max recipients. |
| 115 | // Note, messages cannot be sent from anonymous users, so exclude them. |
| 116 | if (count($uids) - 1 < $max_recipients) { |
| 117 | form_set_error('recipients][max_recipients', t('You only have @count_uids users to handle @count_recipients recipients. You must increase the number of users.', array('@count_uids' => count($uids) - 1, '@count_recipients' => $max_recipients))); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Submit; Integrates with Devel Generate. |
| 123 | * |
| 124 | * @see privatemsg_devel_generate_form() |
| 125 | * @see privatemsg_devel_generate_form_validate() |
| 126 | */ |
| 127 | function privatemsg_devel_generate_form_submit($form_id, &$form_state) { |
| 128 | if (!$form_state['values']['kill_content'] && $form_state['values']['num_threads'] <= 50 && $form_state['values']['max_thread_length'] <= 10) { |
| 129 | privatemsg_devel_generate_threads($form_state); |
| 130 | } |
| 131 | else { |
| 132 | privatemsg_devel_batch_generate_threads($form_state); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Mass Message Generation |
| 138 | */ |
| 139 | function privatemsg_devel_generate_threads($form_state) { |
| 140 | if ($form_state['values']['kill_content']) { |
| 141 | privatemsg_devel_generate_kill_threads(); |
| 142 | } |
| 143 | |
| 144 | // Generate threads. |
| 145 | for ($i = 1; $i <= $form_state['values']['num_threads']; $i++) { |
| 146 | privatemsg_devel_generate_new_thread($form_state['values']); |
| 147 | } |
| 148 | |
| 149 | drupal_set_message(format_plural($form_state['values']['num_threads'], '1 thread created.', '@count threads created')); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Single Message Generation |
| 154 | */ |
| 155 | function privatemsg_devel_generate_new_thread($values) { |
| 156 | module_load_include('inc', 'devel_generate'); |
| 157 | |
| 158 | // Make sure that uids are keyed by the actual user id. |
| 159 | $uids = drupal_map_assoc(devel_get_users()); |
| 160 | // Do not allow anonymous (key) to send/receive private messages |
| 161 | unset($uids[key($uids)]); |
| 162 | |
| 163 | $author = user_load($uids[array_rand($uids)]); |
| 164 | $subject = devel_create_greeking(rand(1, $values['subject_length']), TRUE); |
| 165 | $body = devel_create_content(); |
| 166 | |
| 167 | $timestamp = rand(0, $values['time_range']); |
| 168 | $options = array( |
| 169 | 'author' => $author, |
| 170 | 'timestamp' => REQUEST_TIME - $timestamp, |
| 171 | ); |
| 172 | |
| 173 | // Remove author when adding new recipients. |
| 174 | unset($uids[array_search($author->uid, $uids)]); |
| 175 | |
| 176 | // Get a random amount of user ids. |
| 177 | $num_recipients = rand($values['min_recipients'], $values['max_recipients']); |
| 178 | $recipient_uids = array_rand($uids, $num_recipients); |
| 179 | |
| 180 | // Convert to array if just a single user has been loaded. |
| 181 | if ($num_recipients > 1) { |
| 182 | $recipients = user_load_multiple($recipient_uids); |
| 183 | } else { |
| 184 | $recipients = user_load($recipient_uids); |
| 185 | $recipients = array($recipients); |
| 186 | } |
| 187 | |
| 188 | // Generate message. |
| 189 | $validated = privatemsg_new_thread($recipients, $subject, $body, $options); |
| 190 | |
| 191 | // Get thread information for generating replies. |
| 192 | $thread_id = $validated['message']->thread_id; |
| 193 | $num_replies = rand(0, $values['max_thread_length']); |
| 194 | $reply_timestamp = $timestamp; |
| 195 | |
| 196 | // Add author back in to possible senders. |
| 197 | $recipients[$author->uid] = $author; |
| 198 | |
| 199 | // Generate thread replies. |
| 200 | for ($j = 0; $j <= $num_replies; $j++) { |
| 201 | $reply_body = devel_create_content(); |
| 202 | $reply_author = $recipients[array_rand($recipients)]; |
| 203 | $reply_timestamp = rand(0, $reply_timestamp); |
| 204 | |
| 205 | $reply_options = array( |
| 206 | 'author' => $reply_author, |
| 207 | 'timestamp' => REQUEST_TIME - $reply_timestamp, |
| 208 | ); |
| 209 | privatemsg_reply($thread_id, $reply_body, $reply_options); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Handle the privatemsg_devel_generate_form request to kill all of the threads. |
| 215 | * This is used by both the batch and non-batch branches of the code. |
| 216 | */ |
| 217 | function privatemsg_devel_generate_kill_threads() { |
| 218 | db_delete('pm_index')->execute(); |
| 219 | $i = db_delete('pm_message')->execute(); |
| 220 | |
| 221 | drupal_set_message(format_plural($i, 'Deleted one message', 'Deleted @count messages')); |
| 222 | } |
| 223 | |
| 224 | function privatemsg_devel_batch_generate_threads($form_state) { |
| 225 | $operations = array(); |
| 226 | |
| 227 | // add the kill operation |
| 228 | if ($form_state['values']['kill_content']) { |
| 229 | $operations[] = array('privatemsg_devel_generate_batch_kill_threads', array()); |
| 230 | } |
| 231 | |
| 232 | // add the operations to create the nodes |
| 233 | for ($num = 0; $num < $form_state['values']['num_threads']; $num ++) { |
| 234 | $operations[] = array('privatemsg_devel_generate_batch_new_thread', array($form_state['values'])); |
| 235 | } |
| 236 | |
| 237 | // start the batch |
| 238 | $batch = array( |
| 239 | 'title' => t('Generating private messages'), |
| 240 | 'operations' => $operations, |
| 241 | 'finished' => 'privatemsg_devel_generate_batch_finished', |
| 242 | 'file' => drupal_get_path('module', 'privatemsg') . '/privatemsg.devel_generate.inc', |
| 243 | ); |
| 244 | batch_set($batch); |
| 245 | } |
| 246 | |
| 247 | function privatemsg_devel_generate_batch_kill_threads(&$context) { |
| 248 | privatemsg_devel_generate_kill_threads(); |
| 249 | } |
| 250 | |
| 251 | function privatemsg_devel_generate_batch_new_thread($values, &$context) { |
| 252 | privatemsg_devel_generate_new_thread($values); |
| 253 | $context['results']['num_mids'] ++; |
| 254 | } |
| 255 | |
| 256 | function privatemsg_devel_generate_batch_finished($success, $results, $operations) { |
| 257 | if ($success) { |
| 258 | $message = t('Created @num_mids messages successfully.', array('@num_mids' => $results['num_mids'])); |
| 259 | } |
| 260 | else { |
| 261 | $message = t('Finished with an error.'); |
| 262 | } |
| 263 | drupal_set_message($message); |
| 264 | } |