function advpoll_merge_writein_form_submit in Advanced Poll 7.3
Same name and namespace in other branches
- 7 includes/advpoll.pages.inc \advpoll_merge_writein_form_submit()
- 7.2 includes/advpoll.pages.inc \advpoll_merge_writein_form_submit()
Submit handler for merging write-in votes.
File
- includes/
advpoll.pages.inc, line 640 - Advanced Poll Pages Include.
Code
function advpoll_merge_writein_form_submit($form, &$form_state) {
$lang = $form_state['build_info']['args'][0]->language;
if (!isset($form_state['build_info']['args'][0]->advpoll_choice[$lang])) {
$lang = LANGUAGE_NONE;
}
$merge_from = strtolower($form['merge_writein']['choices']['#value']);
$merge_to = strtolower($form['merge_writein']['all_choices']['#value']);
$nid = $form_state['build_info']['args'][0]->nid;
$choices = $form_state['build_info']['args'][0]->advpoll_choice[$lang];
$from_tag = '';
$to_tag = '';
// Match up unique choice id with selected choice.
foreach ($choices as $choice) {
if (strtolower(strip_tags($choice['choice'])) == $merge_from) {
$from_tag = $choice['choice_id'];
}
elseif (strtolower(strip_tags($choice['choice'])) == $merge_to) {
$to_tag = $choice['choice_id'];
}
}
// No matches, no merge.
if (!$from_tag || !$to_tag) {
drupal_set_message(t('Unidentifed index for index merging.'));
return;
}
// Get votes that belonged to the merge from value.
$criteria = array(
'entity_id' => $nid,
'tag' => $from_tag,
);
$merge_from_votes = votingapi_select_votes($criteria);
// Transfer merge from votes to new vote object and update votingapi.
foreach ($merge_from_votes as $vote) {
$votes = array(
'entity_type' => 'node',
'entity_id' => $nid,
'value' => $vote['value'],
'tag' => $to_tag,
'uid' => $vote['uid'],
'vote_source' => $vote['vote_source'],
);
votingapi_set_votes($votes);
}
// Remove votes for merge from tag.
votingapi_delete_votes($merge_from_votes);
// Remove choice from node.
$node = node_load($nid);
if ($node) {
$lang = $node->language;
if (!isset($node->advpoll_choice[$node->language])) {
$lang = LANGUAGE_NONE;
}
$node_choices = $node->advpoll_choice[$lang];
$updated_choices = array();
foreach ($node_choices as $item) {
$id = $item['choice_id'];
if ($id != $from_tag) {
$updated_choices[] = $item;
}
}
$node->advpoll_choice[$lang] = $updated_choices;
node_save($node);
}
}