Slide 21
Slide 21 text
=== 6/src/Controller/VictimController.php ===!
!
class VictimController
{
const MODULE_NAME = 'uani_victims';
public static function legendOrder()
{
return array('all', 'usa', 'iran', 'foreign');
}
public static function getVictims()
{
$arr = array();
$res = db_query("SELECT nid from {node} where
type='%s' ORDER BY title", IranVictim::NODE_TYPE);
while($o = mysql_fetch_object($res))
{
$node = node_load($o->nid);
$arr[] = $node;
}
return $arr;
}
protected static function getVictimIdFromPath($path)
{
$res = db_query("SELECT nid from
{content_type_uani_victims} uv WHERE
uv.field_uani_iran_path_value='%s'", $path);
$o = mysql_fetch_object($res);
if($o)
return $o->nid;
else
return null;
}
!
!
public static function getVictimFromId($id)
{
if(!is_numeric($id) || (int)$id != $id)
$id = self::getVictimIdFromPath($id);
return node_load($id);
}
protected static function parsePoly($polyStr)
{
$arr = array();
foreach(explode("\n", $polyStr) as $coord)
{
if(preg_match('/(\d+\.?\d*)\s*,\s*(\d+\.?
\d*)/', $coord, $out))
$arr[] = array((float)$out[1], (float)
$out[2]);
}
if($arr)
return $arr;
else
return null;
}
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
public static function getPlacemarks()
{
$arr = array();
$res = db_query("SELECT n.title, uv.*
FROM {node} n
INNER JOIN {content_type_uani_victims} uv ON
n.vid=uv.vid
WHERE
n.type='%s'
AND
uv.field_uani_iran_placemark_type_value <> ''
", IranVictim::NODE_TYPE);
while($o = mysql_fetch_object($res))
{
if($o->field_uani_iran_placemark_type_value ==
'iran_domestic')
$poly = self::parsePoly($o-
>field_uani_iran_placemark_poly_value);
else
$poly = $o-
>field_uani_iran_placemark_poly_value;
$arr[] = array(
'victimType' => $o-
>field_uani_iran_victim_type_value,
'type' => $o-
>field_uani_iran_placemark_type_value,
'name' => $o->title,
'cc' => $o-
>field_uani_iran_foreign_country_value,
'lat' => $o-
>field_uani_iran_placemark_lat_value,
'lon' => $o-
>field_uani_iran_placemark_lon_value,
'poly' => $poly,
'color' => $o-
>field_uani_iran_placemark_poly_c_value,
'nid' => $o->nid,
);
}
return $arr;
}
!
public static function victimUrl($victim)
{
$url = '/veritas/';
if(!empty($victim->field_uani_iran_path[0]
['value']))
$url .= $victim->field_uani_iran_path[0]
['value'];
else
$url .= $victim->nid;
return $url;
}
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
protected static function getVictimsData($victims,
$showVictimID)
{
$placemarks = array_merge(self::getPlacemarks(),
array(
array(
'type' => 'iran_zoom',
'name' => '',
'lat' => 32.4,
'lon' => 54,
),
));
$victimTypes =
array_keys(IranVictim::victimTypes());
$victimTypes[] = 'all';
$nav = array_fill_keys($victimTypes, array());
foreach($victims as $victim)
{
$victimType = $victim-
>field_uani_iran_victim_type[0]['value'];
$ns = array(
'nid' => $victim->nid,
'type' => $victimType,
'name' => $victim->title,
'date' => $victim-
>field_uani_iran_sort_date[0]['value'],
'url' => self::victimUrl($victim),
);
$nav[$victimType][] = $ns;
$nav['all'][] = $ns;
}
$victimSortOrder = array(
'usa' => 0,
'foreign' => 1,
'iran' => 2,
);
foreach($nav as $navType => &$navs)
{
usort($navs, function($a, $b)
use($victimSortOrder) {
$aord = $victimSortOrder[$a['type']];
$bord = $victimSortOrder[$b['type']];
if($aord != $bord)
return $aord - $bord;
if($a['type'] == 'usa')
return -strcmp($a['date'],
$b['date']);
else
return strcmp($a['name'], $b['name']);
});
}
return array('UANIVictims' => array(
'basePath' => '/' . drupal_get_path('module',
'uani_victims'),
'placemarks' => $placemarks,
'nav' => $nav,
'initialVictim' => $showVictimID,
));
}
!
!
public static function ajaxVictimAction($id)
{
$node = self::getVictimFromId($id);
if(!$node || $node->type != IranVictim::NODE_TYPE)
return drupal_not_found();
header('Content-type: application/json');
echo json_encode(array(
'id' => $node->nid,
'view' => theme('uani_victim_overlay', array(
'victim' => $node,
'victimURL' => self::victimUrl($node),
)),
'url' => $url,
'title' => "Iran VERITAS Project: Documenting
Iran's Violence, Extremism, Repression and Terror | UANI |
" . $node->title,
'type' => $node-
>field_uani_iran_victim_type[0]['value'],
));
exit;
}
!
public static function victimsAction()
{
$showVictimID = arg(1);
$victims = self::getVictims();
$victimsSorted = array(
'all' => array(),
'usa' => array(),
'foreign' => array(),
'iran' => array(),
);
foreach($victims as $v)
$victimsSorted[$v-
>field_uani_iran_victim_type[0]['value']][] = $v;
foreach($victimsSorted as $type => &$victimList)
{
usort($victimList, function($a, $b) use($type)
{
if($type == 'usa')
return -strcmp($a-
>field_uani_iran_sort_date[0]['value'], $b-
>field_uani_iran_sort_date[0]['value']);
else
return strcmp($a->title, $b->title);
});
}
$modulePath = drupal_get_path('module',
'uani_victims');
drupal_add_js($modulePath . '/js/
jquery.history.js', 'module');
drupal_add_js($modulePath . '/js/uani_victims.js',
'module');
drupal_add_css($modulePath . '/templates/
uani_victims.css', 'module');
drupal_add_js(self::getVictimsData($victims,
$showVictimID), 'setting');
$types = IranVictim::victimTypes();
$types['all'] = 'Home';
$victimTypes = array();
foreach(self::legendOrder() as $type)
$victimTypes[$type] = $types[$type];
return theme('uani_victims', array(
'victims' => $victims,
'victimsSorted' => $victimsSorted,
'victimTypes' => $victimTypes,
));
}
}
!
!
!
!
!
!
!