function flag_lists_generate_lists_form_submit in Flag Lists 7.3
Same name and namespace in other branches
- 6 flag_lists.admin.inc \flag_lists_generate_lists_form_submit()
- 7 flag_lists.admin.inc \flag_lists_generate_lists_form_submit()
Submit handler for flag_lists_generate_lists_form.
File
- ./
flag_lists.admin.inc, line 497 - Contains administrative pages for creating, editing, and deleting flag lists.
Code
function flag_lists_generate_lists_form_submit($form, &$form_state) {
global $user;
module_load_include('inc', 'devel_generate');
// Delete listings.
if ($form_state['values']['kill_listings']) {
$flags = flag_lists_get_flags();
foreach ($flags as $flag) {
$result = db_select('flag_lists_content', 'f')
->fields('f', array(
'fcid',
'entity_id',
))
->condition('fid', $flag->fid)
->execute();
foreach ($result as $row) {
db_delete('flag_lists_content')
->condition('fid', $flag->fid)
->execute();
db_delete('flag_lists_counts')
->condition('fid', $flag->fid)
->execute();
module_invoke_all('flag', 'unflag', $flag, $row->entity_id, $account, $row->fcid);
}
}
drupal_set_message(t('All listings were deleted.'));
}
// Delete lists and listings.
if ($form_state['values']['kill_lists']) {
// If we loaded all flags above, don't reload them here.
if (!count($flags)) {
$flags = flag_lists_get_flags();
}
foreach ($flags as $flag) {
flag_lists_fl_delete($flag, $user);
}
drupal_set_message(t('All lists and their listings were deleted.'));
}
$templates = array_filter($form_state['values']['templates']);
$uids = array_filter(devel_get_users());
// Don't use the anon user.
// Generate lists.
if ($form_state['values']['lists'] && count($templates)) {
for ($i = 1; $i <= $form_state['values']['lists']; $i++) {
$template = flag_get_flag(NULL, array_rand($templates));
$account->uid = $uids[array_rand($uids)];
$edit['values']['title'] = devel_create_greeking(7, TRUE);
$edit['values']['type'] = $template->types[array_rand($template->types)];
$form = array();
flag_lists_form_submit($form, $edit, $account);
}
drupal_set_message(t('@lists created.', array(
'@lists' => format_plural($form_state['values']['lists'], '1 list', '@count lists'),
)));
}
// Generate listings.
if ($form_state['values']['listings']) {
$count = 0;
for ($i = 1; $i <= $form_state['values']['listings']; $i++) {
$account->uid = $uids[array_rand($uids)];
$lists = flag_lists_get_user_flags(NULL, $account);
// Remove any lists that don't use the chosen templates.
foreach ($lists as $key => $list) {
if (!isset($templates[$list->pfid])) {
unset($lists[$key]);
}
}
// Ensure user has lists.
if (!count($lists)) {
continue;
}
$list = $lists[array_rand($lists)];
$content_type = $list->types[array_rand($list->types)];
// We get the random nid with 2 SELECTS to avoid slow ORDER BY Rand().
// Get max nid for our content type.
$query = db_select('node')
->condition('type', $content_type);
$query
->addExpression('MAX(nid)', 'max');
$max_nid = $query
->execute()
->fetchField();
// Ensure a node exists for the type.
if (!$max_nid) {
continue;
}
$r = rand(1, $max_nid);
$content_id = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('type', $content_type)
->condition('nid', $r)
->range(0, 1)
->execute()
->fetchField();
// We can't assume that every attempt will result in a listing because
// we may have a node type with no nodes yet or a user with no lists.
// Also, if a listing already exists, we count it, but it can only be in
// the db once.
if (flag_lists_do_flag($list, 'flag', $content_id, $account, TRUE)) {
$count++;
}
}
drupal_set_message(t('@listings listed.', array(
'@listings' => format_plural($count, '1 node', '@count nodes'),
)));
if ($count < $form_state['values']['listings']) {
drupal_set_message(t('Listings are generated randomly. Occassionally a listing will not be created if the random user has no lists or if the node type to be listed has no nodes.'));
}
}
}