<!DOCTYPE html>
<html lang="en">
{{fscript
	keywords := objectContext objectsForEntityNamed:'Keyword' qualifierLocum:'active == 1 and forContact == 1 and forOrganization == 1' bindings:nil.

	"pre filtered"
	all := {}.
	others := {}.


	"query the db to see how many we actually have"
	keywords do:[:keyword |	
		v1 := objectContext resultCountForEntityNamed:'ContactKeywordJoin' qualifierLocum:'keywordID == $kID' bindings:#{'kID' -> (keyword keywordID)}.
		v2 := objectContext resultCountForEntityNamed:'KeywordOrganizationJoin' qualifierLocum:'keywordID == $kID' bindings:#{'kID' -> (keyword keywordID)}.
				
		dict := #{'contacts' -> v1, 'organizations' -> v2, 'total' -> (v1 + v2), 'name' -> (keyword name), 'kID' -> (keyword keywordID)}.

		all add:dict.		
	].

	"Sort so we can figure out the 5 if we have more than 5"
	all := all sortedArrayUsingDescriptors:{(NSSortDescriptor sortDescriptorWithKey:'total' ascending:false)}.
	
	(keywords count > 5) ifTrue: [
		top := all at:{0,1,2,3,4}.		
		others := all difference:top.
			
		"we have to resort others..."		
		others:= (others sortedArrayUsingDescriptors:{(NSSortDescriptor sortDescriptorWithKey:'total' ascending:false)}).
	
	]
	ifFalse: [
		top := all.
	].

	globals setTop:top.
	globals setOthers:others.

	"now we iterate the top and morph it to work with the chart"
	labels := {}.
	values := {}.

	top do:[:dict |
		values add:{(dict valueForKey:'contacts'), (dict valueForKey:'organizations')}.
		labels add:(dict valueForKey:'name').
	
	].
	
	"If we have others, it would be nice to see the total as compared to the rest"
	(others count > 0) ifTrue: [
		t1 := others valueForKeyPath:'@sum.contacts'.
		t2 := others valueForKeyPath:'@sum.organizations'.
		
		values add:{t1, t2}.
		labels add:'Others'.
	].
		
	globals setLabels:labels JSONRepresentation.
	globals setValues:values JSONRepresentation.

}}
<head>
	<meta charset="utf-8">
	<link rel="stylesheet" href="{{framework}}/default.css" type="text/css" charset="utf-8">
	<link rel="stylesheet" href="{{framework}}/screen.css" type="text/css" charset="utf-8" media="screen">
	<link rel="stylesheet" href="{{framework}}/print.css" type="text/css" charset="utf-8" media="print">
	<script src="{{framework}}/raphael.js"></script>
	<script src="{{framework}}/ico.js"></script>
	<script type="text/javascript">
		function $(id) {
		  return document.getElementById(id);
		}
	</script>
	
	<style type="text/css" media="screen">
	.linegraph { width: 720px; height: 400px; background-color: #fff; margin-left: 10px; margin-bottom: 30px;}
	.contacts {border: 5px #3399ff solid;}
	.organizations {border: 5px #6666cc solid;}
	</style>
</head>
<body>
	<h1>{{localize 'Contacts by Top 5 Keywords'}}</h1>
	<p>{{localize 'Compare how people and companies have the same keywords. Only keywords that are set for both People and Companies are displayed here. Sorted by total.'}}</p>
	<p><span class="contacts"></span>&nbsp&nbspPeople&nbsp&nbsp<span class="organizations"></span>&nbsp&nbspCompanies</p>
	
	
	<div id="bargraph_5" class="linegraph"></div>
	<script type="text/javascript">

	new Ico.BarGraph($('bargraph_5'), 
		{{values}}, {
			grid: true,
			grid_colour: '#eee', 
			colours: ['#3399ff', '#6666cc'], 
			bar_labels: true,
			labels: {{labels}} });	
	</script>
	
	<h3>{{localize 'Top 5 Keywords'}}</h3>
	<table>
		<thead>
				<td>{{localize 'Keyword'}}</td>
				<td width="15%">People</td>
				<td width="15%">Companies</td>
				<td width="15%">Total</td>
		</thead>
		<tbody>{{foreach dict top do}}
			<tr>
			<td>
				{{dict.name}}
			</td>
			<td>
				<a href="{{baseURL}}/people?keywordID={{dict.kID}}">{{dict.contacts}}</a>
			</td>
			<td>
				<a href="{{baseURL}}/companies?keywordID={{dict.kID}}">{{dict.organizations}}</a>
			</td>
			<td>
				{{dict.total}}
			</td>
			</tr>{{endforeach do}}
		</tbody>	
	</table>
	
	{{if others.@count}}
	<h3>{{localize 'Others'}}</h3>
	<table>
		<thead>
				<td>{{localize 'Keyword'}}</td>
				<td width="15%">People</td>
				<td width="15%">Companies</td>
				<td width="15%">Total</td>
		</thead>
		<tbody>{{foreach dict others do}}
			<tr>
			<td>
				{{dict.name}}
			</td>
			<td>
				<a href="{{baseURL}}/people?keywordID={{dict.kID}}">{{dict.contacts}}</a>
			</td>
			<td>
				<a href="{{baseURL}}/companies?keywordID={{dict.kID}}">{{dict.organizations}}</a>
			</td>
			<td>
				{{dict.total}}
			</td>
			</tr>{{endforeach do}}
		</tbody>	
	</table>
	{{endif}}
	
	
	<div id="reportfooter">
	{{localize 'Generated by %@ on %@', objectContext.user.contact.cachedName, date}}.			
	</div>

</body>
</html>