Thank you first for this great module! It is a great addition to my website!

I would like to show a flag counter on page/node which updates as nodes are flagged/unflagged via javascript in a view.

For example: A view is setup with a list of Nodes and Add Flag link via javascript next to each node.
When user clicks add flag for node 1 - a counter on the page shows "[1] node flagged"
When user clicks add flag for node 2 - a counter on the page shows "[2] nodes flagged"
When user clicks add flag for node 3 - a counter on the page shows "[3] nodes flagged"
When user clicks remove flag for node 3 - a counter on the page shows "[2] nodes flagged"

Is this currently possible with this module?

Comments

garamvideos’s picture

Use flag counter tokens like [flag-bookmarks-count] in your link / unlink description. Will need to get Token module.

quicksketch’s picture

Category: feature » support

See the Flag API for Javascript: http://drupal.org/node/336122

Bilmar’s picture

@garamvideos - thank you for the pointer. To clarify, I would like to have this flag count code printed in the page.tpl.php to have it always showing at the top of the page. Is your suggestion limited to creating a view page display?

@quicksketch - I apologize but I am not a programmer and have looked at the Flag API for javascript link and having trouble with the code to input below.

$(document).bind('flagGlobalAfterLinkUpdate', function(event, data) {
// i am not sure what code to type here
});

I believe this would be a support request for someone to help myself (and other in the community that may need help) with an example/snippet that we would be able to reference and build on to get the most out of utilizing the Flag module.

Thanks in advance

Bilmar’s picture

Also, as another example:
I would like to have a View like below where at the top it always show how many nodes are flagged and then below is the list of nodes. Even when you go to the next page of nodes I want the counter at the top, which updates when Add/Remove is clicked on the view.

-----------------
You have [5] nodes flagged
-----------------
Node 1 Remove Flag
Node 2 Remove Flag
Node 3 Remove Flag
Node 4 Remove Flag
Node 5 Remove Flag
Node 6 Add Flag
Node 7 Add Flag
Node 8 Add Flag

next page
-----------------

Thanks

YK85’s picture

fyi - http://drupal.org/node/305086 shows the code for the counter of how many times flagged.

But I am not sure how to make it update via javascript when nodes are flagged. I'm looking into it myself but having some trouble..

YK85’s picture

kindly bumping..would anyone be able to help with a simple example?

merilainen’s picture

Using tokens in the links works pretty well, but is not ideal solution to use with Views.
Has anyone managed to add a separate count display into a node.tpl.php for example, and update it dynamically when a link is clicked?

ceerwk’s picture

subscribing, thanks!

luksak’s picture

i had the same issue. here is how i solved the problem:

bindFlagUpdate();
	
function bindFlagUpdate(){
	$(document).bind('flagGlobalAfterLinkUpdate', function(event, data) {
		var count = $('#favotites-count-value').html();
		if(data.flagStatus == 'flagged')
			count++;
		else
			count--;
		$('#favotites-count-value').html(count);
		$(document).unbind();
		bindFlagUpdate();
		return false;
	});
}

it is definitely not the nicest jQuery code ive written so far. there were two problems:
- the event gets triggered multiple times (each time you update the flage once more).
- the method live() would resolve this issue but cannot take the data argument.

so i had to stick with the bind() method and unbind and rebind the event listener each time the flag update event is fired.

i hope this helps someone

lukas

mooffie’s picture

Status: Active » Closed (duplicate)

As was pointed out, our JavaScript API is the key for doing this.

On the other hand, your code will need access to new data: the count of flagged nodes (that's quite different than the "flag counter" of a node!). Here's a feature request that will allow us to feed our javascript snippets new data easily: #926252: Factor out the JavaScript info building . I'm marking this issue a duplicate of it.

zazinteractive’s picture

Thanks luksak. Got it working with some modification

ananth_kiran’s picture

Now, we can see every flag statistics by adding the below code in the corresponding files in flag module.
Statistics of Flags.
1. Total no of Flags used
2. Flags & Nodes
3. Flags & Content Types

There 3 steps for getting Flag statistics page in flag module.
STEP 1:
In the file “flag\includes\flag.export.inc”, At line no.251, please append the below code:

/**
* Statistics of Flags.
* 1. Total no of Flags used
* 2. Flags & Nodes
* 3. Flags & Content Types
**/
function flag_stat_form($form, &$form_state) {

$form['flag_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Flag Statistics'),
);
$form['flag_settings']['container1'] = array(
'#type' => 'container',
'#weight' => -10,
);

$form['flag_settings']['container1']['flag_stats'] = array(
'#type' => 'fieldset',
'#title' => t('1. Flag Names and their count'),
'#weight' => -10,
);
$form['flag_settings']['container2']['flag_stats'] = array(
'#type' => 'fieldset',
'#title' => t('2.Nodes & Flags Statistics'),
'#weight' => -10,
);
$form['flag_settings']['container3']['flag_stats'] = array(
'#type' => 'fieldset',
'#title' => t('3. Content types & Flags Statistics'),
'#weight' => -10,
);

// Retrieve a list of all flags, and their count for all users
$result1 = db_query("SELECT flags.name,sum(flag_counts.count) as Count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid GROUP BY flags.name ORDER BY flags.fid ASC")->fetchAll();

// Retrieve a list of all flags, nodes list.
$result2 = db_query("SELECT node.title,flags.fid,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid LEFT JOIN node ON node.nid=flag_counts.content_id UNION SELECT node.title, flags.fid,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid RIGHT JOIN node ON node.nid=flag_counts.content_id")->fetchAll();

// Retrieve a list of all flags, content types list.
$result3 = db_query("SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid LEFT JOIN node ON node.nid=flag_counts.content_id LEFT JOIN node_type ON node.type=node_type.type UNION SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid RIGHT JOIN node ON node.nid=flag_counts.content_id LEFT JOIN node_type ON node.type=node_type.type UNION SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid LEFT JOIN node ON node.nid=flag_counts.content_id RIGHT JOIN node_type ON node.type=node_type.type UNION SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid RIGHT JOIN node ON node.nid=flag_counts.content_id RIGHT JOIN node_type ON node.type=node_type.type")->fetchAll();

// Implementing array of flags, their counts
$flag_count = array();
$flag_list = array();
foreach ($result1 as $row) {
if ($row->Count != '') {
$temp = array('0' => $row->name, '1' => $row->Count);
}
else {
$temp = array('0' => $row->name, '1' => 0);
}
array_push($flag_count,$temp);
array_push($flag_list, $row->name);
}

// Retriving the products list.
$products_list = array();
foreach ($result2 as $row) {
if($row->title != "") {
array_push($products_list,$row->title);
}
}

$products_list = array_values(array_unique($products_list));

// forming a dummy array with zeros.
$final_prod_flags = array_fill(0, count($products_list), array_fill(0, count($flag_list)+1, 0));

// Implementation of second statistics. i.e, Products and Flags relationship.
for($j=0;$j $final_prod_flags[$j][0] = $products_list[$j];
for($i=0;$i foreach ($result2 as $row) {
if($row->title == $products_list[$j]){
if($row->name == $flag_list[$i]){
$final_prod_flags[$j][$i+1] = $row->count;
}// flag if
}//prod if
}// row loop
}//flag for loop
}//prod for loop

// Retriving the content types list.
$content_type_list = array();
foreach ($result3 as $row) {
if($row->type != "") {
array_push($content_type_list,$row->type);
}
}

$content_type_list = array_values(array_unique($content_type_list));

// forming a dummy array with zeros for content type & flag statitics
$final_content_types_flags = array_fill(0, count($content_type_list), array_fill(0, count($flag_list)+1, 0));

// Implementation of third statistics. i.e, content type and Flags relationship.
for($j=0;$j $final_content_types_flags[$j][0] = $content_type_list[$j];
for($i=0;$i $count = 0;
foreach ($result3 as $row) {
if($row->type == $content_type_list[$j]){
if($row->name == $flag_list[$i]) {
$count = $count + ($row->count);
$final_content_types_flags[$j][$i+1] = $count;
}// flag if
}//prod if
}// row loop
}//flag for loop
}//prod for loop

// Flag-Count Statistics table
$header1 = array(t('Flag name'));
$header1 = array_merge($header1, array(t('Flag count')));

// Node-Flags Statistics table
$header2 = array(t('Product name'));
$header2 = array_merge($header2, $flag_list);

// Content types-Flags Statistics table
$header3 = array(t('Content type name'));
$header3 = array_merge($header3, $flag_list);

$form['flag_settings']['container1']['flag_stats']['flags'] = array(
'#theme' => 'table',
'#header' => $header1,
'#rows' => $flag_count,
'#empty' => t('No content available.'),
);

$form['flag_settings']['container2']['flag_stats']['flags'] = array(
'#theme' => 'table',
'#header' => $header2,
'#rows' => $final_prod_flags,
'#empty' => t('No content available.'),
);

$form['flag_settings']['container3']['flag_stats']['flags'] = array(
'#theme' => 'table',
'#header' => $header3,
'#rows' => $final_content_types_flags,
'#empty' => t('No content available.'),
);
return $form;
}

STEP 2:
In the file, “flag\flag.module”, at line no.69, please add below code:

$items[FLAG_ADMIN_PATH . '/stats'] = array(
'title' => 'Stats',
'page callback' => 'drupal_get_form',
'page arguments' => array('flag_stat_form'),
'access arguments' => array('administer flags'),
'file' => 'includes/flag.export.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 4,
);

STEP 3:
Now just clear the cache,

That’s it.
Now Modules > Flags > configure
You can see another tab ”STATS” at the righthand side parallel to “LIST”,“ACTIONS”,“IMPORT”,“EXPORT”.
If you click on it, we can see three flag statistics tables.

First table says, how many flags being used for how many times.
Second table says, how many nodes are flagged for how many time for each flag.
Third table says, how content types with nodes being flagged with each flag.