{{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. }}
{{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.'}}
  People    Companies
{{localize 'Keyword'}} | People | Companies | Total | {{foreach dict top do}}
{{dict.name}} | {{dict.contacts}} | {{dict.organizations}} | {{dict.total}} |
{{localize 'Keyword'}} | People | Companies | Total | {{foreach dict others do}}
{{dict.name}} | {{dict.contacts}} | {{dict.organizations}} | {{dict.total}} |