public function LikeBtn::updateVotes in Like Button 8.2
Same name and namespace in other branches
- 7 likebtn.php \LikeBtn::updateVotes()
Update votes in database from API response.
1 call to LikeBtn::updateVotes()
- LikeBtn::syncVotes in ./
likebtn.php - Comment sync function.
File
- ./
likebtn.php, line 155 - LikeBtn like button.
Class
Code
public function updateVotes($response) {
$votes = array();
if (!empty($response['response']['items'])) {
foreach ($response['response']['items'] as $item) {
$entity_type = '';
$entity_id = '';
$field_id = '';
$field_index = 0;
// Parse identifier.
if (strstr($item['identifier'], '_field_')) {
// Item is a field.
preg_match("/^(.*)_(\\d+)_field_(\\d+)(?:_index_(\\d+))?\$/", $item['identifier'], $identifier_parts);
if (!empty($identifier_parts[1])) {
$entity_type = $identifier_parts[1];
}
else {
continue;
}
if (!empty($identifier_parts[2])) {
$entity_id = $identifier_parts[2];
}
else {
continue;
}
if (!empty($identifier_parts[3])) {
$field_id = $identifier_parts[3];
}
else {
continue;
}
if (!empty($identifier_parts[4])) {
$field_index = $identifier_parts[4];
}
}
else {
// Item is an entity.
preg_match("/^(.*)_(\\d+)\$/", $item['identifier'], $identifier_parts);
if (!empty($identifier_parts[1])) {
$entity_type = $identifier_parts[1];
}
else {
continue;
}
if (!empty($identifier_parts[2])) {
$entity_id = $identifier_parts[2];
}
else {
continue;
}
}
$vote_source = LIKEBTN_VOTING_VOTE_SOURCE;
if ($field_id) {
$vote_source = 'field_' . $field_id . '_index_' . $field_index;
}
$likes = 0;
if (!empty($item['likes'])) {
$likes = $item['likes'];
}
$dislikes = 0;
if (!empty($item['dislikes'])) {
$dislikes = $item['dislikes'];
}
// If vote for this entity/field has been already stored - continue.
foreach ($votes as $vote) {
if ($vote['entity_type'] == $entity_type && $vote['entity_id'] == $entity_id && $vote['vote_source'] == $vote_source) {
continue 2;
}
}
// Get entity info.
try {
$entity_type_info = entity_get_info($entity_type);
if (empty($entity_type_info['controller class'])) {
continue;
}
} catch (Exception $e) {
continue;
}
// Likes and Disliked stored in Voting API.
$votes[] = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'value_type' => 'points',
'value' => $likes,
'tag' => LIKEBTN_VOTING_TAG,
'uid' => 0,
'vote_source' => $vote_source,
);
$votes[] = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'value_type' => 'points',
'value' => $dislikes * -1,
'tag' => LIKEBTN_VOTING_TAG,
'uid' => 0,
'vote_source' => $vote_source,
);
// Remove (backup) votes cast on this entity by other modules.
/*$remove_old_votes_fields = array(
'entity_type' => $entity_type . '_backup',
);
try {
db_update('votingapi_vote')
->fields($remove_old_votes_fields)
->condition('entity_type', $entity_type)
->condition('vote_source', array('like', 'dislike'), 'NOT IN')
->execute();
}
catch (Exception $e) {}
*/
// Remove votes cast on this entity by the previous version
// of the plugin, when vote_source was ''
/*try {
db_delete('votingapi_vote')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id)
->condition('tag', 'vote')
->condition('vote_source', '')
->execute();
}
catch (Exception $e) {
}
*/
// Update LikeBtn fields.
if ($vote_source) {
$entities = entity_load($entity_type, array(
$entity_id,
));
if (empty($entities[$entity_id])) {
continue;
}
$entity = $entities[$entity_id];
list($tmp_entity_id, $entity_revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
// Get entity LikeBtn fields.
$entity_fields = field_info_instances($entity_type, $bundle);
// Set field value.
$likes_minus_dislikes = $likes - $dislikes;
foreach ($entity_fields as $field_name => $field_info) {
if ($field_info['widget']['module'] != 'likebtn') {
continue;
}
$field_fields_data = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'entity_id' => $entity_id,
'revision_id' => $entity_id,
'delta' => $field_index,
'language' => isset($entity->language) ? $entity->language : LANGUAGE_NONE,
);
$field_fields_data[$field_name . '_likebtn_likes'] = $likes;
$field_fields_data[$field_name . '_likebtn_dislikes'] = $dislikes;
$field_fields_data[$field_name . '_likebtn_likes_minus_dislikes'] = $likes_minus_dislikes;
try {
// Insert value.
db_insert('field_data_' . $field_name)
->fields($field_fields_data)
->execute();
} catch (Exception $e) {
// Update value.
try {
db_update('field_data_' . $field_name)
->fields($field_fields_data)
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->condition('entity_id', $entity_id)
->execute();
} catch (Exception $e) {
}
}
}
}
}
if ($votes) {
// Prepare criteria for removing previous vote values.
$criteria = array();
foreach ($votes as $vote) {
$criteria[] = array(
'entity_type' => $vote['entity_type'],
'entity_id' => $vote['entity_id'],
'value_type' => $vote['value_type'],
'tag' => $vote['tag'],
'uid' => $vote['uid'],
'vote_source' => $vote['vote_source'],
);
}
// Votes must be saved altogether.
// echo "<pre>";
// print_r($votes);
// print_r($criteria);
// exit();
votingapi_set_votes($votes, $criteria);
return TRUE;
}
return FALSE;
}
}