2023-05-16 14:06:35 +02:00
< ? php
class OrganisationMapWidget
{
public $title = 'Organisation world map' ;
public $render = 'WorldMap' ;
public $description = 'The countries represented via organisations on the current instance.' ;
public $width = 3 ;
public $height = 4 ;
public $params = [
'filter' => 'A list of filters by organisation meta information (sector, type, local (- expects a boolean or a list of boolean values)) to include. (dictionary, prepending values with ! uses them as a negation)' ,
2023-08-09 14:37:31 +02:00
'start_date' => 'The ISO 8601 date format at which to start' ,
'end_date' => 'The ISO 8601 date format at which to end. (Leave empty for today)' ,
'limit' => 'Limits the number of displayed tags. Default: 10' ,
2023-05-16 14:06:35 +02:00
];
public $cacheLifetime = null ;
public $autoRefreshDelay = false ;
private $validFilterKeys = [
'sector' ,
'type' ,
'local'
];
public $placeholder =
' {
2023-05-31 09:31:46 +02:00
" filter " : {
" type " : " Member " ,
" local " : [ 0 , 1 ]
}
2023-05-16 14:06:35 +02:00
} ' ;
private $Organisation = null ;
2023-06-27 13:20:13 +02:00
public $countryCodes = [];
2023-05-16 14:06:35 +02:00
public function handler ( $user , $options = array ())
{
2023-06-27 13:20:13 +02:00
App :: uses ( 'WidgetToolkit' , 'Lib/Dashboard/Tools' );
$WidgetToolkit = new WidgetToolkit ();
$this -> countryCodes = $WidgetToolkit -> getCountryCodeMapping ();
2023-05-16 14:06:35 +02:00
$params = [
'conditions' => [
'Nationality !=' => ''
]
];
if ( ! empty ( $options [ 'filter' ]) && is_array ( $options [ 'filter' ])) {
foreach ( $this -> validFilterKeys as $filterKey ) {
if ( ! empty ( $options [ 'filter' ][ $filterKey ])) {
if ( ! is_array ( $options [ 'filter' ][ $filterKey ])) {
$options [ 'filter' ][ $filterKey ] = [ $options [ 'filter' ][ $filterKey ]];
}
$tempConditionBucket = [];
foreach ( $options [ 'filter' ][ $filterKey ] as $value ) {
if ( $value [ 0 ] === '!' ) {
$tempConditionBucket [ 'Organisation.' . $filterKey . ' NOT IN' ][] = mb_substr ( $value , 1 );
} else {
$tempConditionBucket [ 'Organisation.' . $filterKey . ' IN' ][] = $value ;
}
}
if ( ! empty ( $tempConditionBucket )) {
$params [ 'conditions' ][ 'AND' ][] = $tempConditionBucket ;
}
}
}
}
2023-08-09 14:37:31 +02:00
if ( $options [ 'start_date' ]) {
$params [ 'conditions' ][ 'AND' ][ 'Organisation.date_created >=' ] = ( new DateTime ( $options [ 'start_date' ])) -> format ( 'Y-m-d H:i:s' );
if ( empty ( $options [ 'end_date' ])) {
$params [ 'conditions' ][ 'AND' ][ 'Organisation.date_created <=' ] = ( new DateTime ( $options [ 'end_date' ])) -> format ( 'Y-m-d H:i:s' );
}
}
2023-05-16 14:06:35 +02:00
$this -> Organisation = ClassRegistry :: init ( 'Organisation' );
$orgs = $this -> Organisation -> find ( 'all' , [
'recursive' => - 1 ,
'fields' => [ 'Organisation.nationality' , 'COUNT(Organisation.nationality) AS frequency' ],
'conditions' => $params [ 'conditions' ],
'group' => [ 'Organisation.nationality' ]
]);
$results = [ 'data' => [], 'scope' => 'Organisations' ];
foreach ( $orgs as $org ) {
$country = $org [ 'Organisation' ][ 'nationality' ];
$count = $org [ '0' ][ 'frequency' ];
if ( isset ( $this -> countryCodes [ $country ])) {
$countryCode = $this -> countryCodes [ $country ];
$results [ 'data' ][ $countryCode ] = $count ;
}
}
return $results ;
}
}
?>