Adding mapping ability. Incomplete as of yet, but coming along nicely.

git-svn-id: file:///svn-source/pmgr/branches/initial_20090526@32 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
abijah
2009-05-29 01:47:12 +00:00
parent 3cc8e95ff4
commit 9fc5562dd8
5 changed files with 492 additions and 0 deletions

View File

@@ -0,0 +1,308 @@
<?php
class MapsController extends AppController {
var $helpers = array('Html');
function index() {
$this->Map->recursive = 0;
/* $this->Map->MapSize->unbindModel(array('hasMany' => array('Map'))); */
/* $this->Map->MapSize->MapType->unbindModel(array('hasMany' => array('MapSize'))); */
//$this->Map->bindModel(array('hasOne' => array('MapType')));
//function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null) {
$this->set('maps', $this->paginate(null, null, null, null, null, 0));
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid Item.', true));
$this->redirect(array('action'=>'index'));
}
$this->Map->recursive = 0;
$this->set('map', $this->Map->read(null, $id));
/* $this->Map->MapSize->unbindModel(array('hasMany' => array('Map'))); */
/* $this->Map->MapSize->MapType->unbindModel(array('hasMany' => array('MapSize'))); */
/* $this->Map->Lease->unbindModel(array('belongsTo' => array('Map'))); */
/* $this->Map->Lease->LeaseType->unbindModel(array('hasMany' => array('Lease'))); */
/* $this->Map->Lease->Charge->unbindModel(array('belongsTo' => array('Lease'))); */
/* $this->Map->Lease->Charge->ChargeType->unbindModel(array('hasMany' => array('Charge'))); */
/* $this->Map->Lease->Charge->Receipt->unbindModel(array('hasMany' => array('ChargesReceipt'))); */
/* //$this->Map->Lease->unbindModel(array('hasMany' => array('Contact'))); */
/* $this->Map->Lease->unbindModel(array('hasAndBelongsToMany' => array('Contact'))); */
/* //$this->Map->Lease->unbindModel(array('hasOne' => array('Contact'))); */
/* //$this->Map->Lease->unbindModel(array('belongsTo' => array('Contact'))); */
/* //$this->Map->Lease->bindModel(array('hasOne' => array('ContactsLease'))); */
/* //$this->Map->Lease->bindModel(array('hasOne' => array('Contact'))); */
/* /\* $this->Map->Lease->bindModel(array('hasOne' => array('ContactsLease', *\/ */
/* /\* 'Contact' => array( *\/ */
/* /\* 'foreignKey' => false, *\/ */
/* /\* 'conditions' => *\/ */
/* /\* array('Contact.id = ContactsLease.contact_id', *\/ */
/* /\* //'ContactsLease.type = "TENANT"' *\/ */
/* /\* ))))); *\/ */
/* $this->Map->bindModel(array('hasOne' => array('Lease' => array( */
/* 'foreignKey' => false, */
/* 'conditions' => array('Lease.map_id = Map.id') */
/* ), */
/* 'ContactsLease' => array( */
/* 'foreignKey' => false, */
/* 'conditions' => array('ContactsLease.lease_id = Lease.id') */
/* ), */
/* 'Contact' => array( */
/* 'foreignKey' => false, */
/* 'conditions' => array('Contact.id = ContactsLease.contact_id') */
/* ))), */
/* false); */
/* $this->Map->Lease->bindModel(array('hasOne' => array('Contact' => array( */
/* 'className' => 'ContactsLease', */
/* 'foreignKey' => false, */
/* 'conditions' => */
/* array('ContactsLease.lease_id = Lease.id', */
/* 'ContactsLease.type = "TENANT"'))))); */
/* # 'hasOne' => array( */
/* # 'RecipesTag', */
/* # 'FilterTag' => array( */
/* # 'className' => 'Tag', */
/* # 'foreignKey' => false, */
/* # 'conditions' => array('FilterTag.id = RecipesTag.tag_id') */
/* # )))); */
/* $this->Map->id = $id; */
/* if ($id !== null && $id !== false) { */
/* $this->Map->data = $this->Map->find('first', */
/* array('conditions' => array( */
/* $this->Map->alias . '.' . $this->Map->primaryKey => $id, */
/* 'ContactsLease.type = "TENANT"' */
/* ) */
/* )); */
/* $this->set('map', $this->Map->data); */
/* } */
}
// Temporary function
function unitStatusList() {
return
array('DELETED' => array(),
'DAMAGED' => array(),
'COMPANY' => array(),
'UNAVAILABLE' => array(),
'RESERVED' => array(),
'DIRTY' => array(),
'VACANT' => array(),
'OCCUPIED' => array(),
'LATE' => array(),
'LOCKED' => array(),
'LIENED' => array(),
);
}
function map($id = null, $requested_width = 600) {
$info = $this->mapInfo($id);
$info['border'] = true;
$info['clickable'] = true;
$this->image($info, $requested_width);
}
function legend($id = null, $requested_width = 400) {
$status = $this->unitStatusList();
$cols = 6;
$rows = (int)((count($status) + $cols - 1) / $cols);
$info = array('extents' => array(), 'units' => array(), 'legend' => array());
$info['legend']['width'] = 360;
$info['legend']['depth'] = 120;
$info['extents']['right'] = $info['legend']['width'] * $cols;
$info['extents']['bottom'] = $info['legend']['depth'] * $rows;
$top = $lft = 0;
foreach ($status AS $code => $color) {
$info['units'][] = array('name' => $code,
'status' => $code,
'width' => $info['legend']['width'],
'depth' => $info['legend']['depth'],
'left' => $lft,
'right' => $lft + $info['legend']['width'],
'top' => $top,
'bottom' => $top + $info['legend']['depth']);
$top += $info['legend']['depth'];
if ($top >= $info['legend']['depth'] * $rows) {
$top = 0; $lft += $info['legend']['width'];
}
}
$this->image($info, $requested_width, true);
}
function image($info, $requested_width, $legend = false) {
//var $helpers = array('Html', 'Form', 'Javascript', 'Graph');
$this->layout = null;
$this->autoLayout = false;
Configure::write('debug', '0');
// Define our color palate
// REVISIT <AP>: 20090513
// Get colors from DB option tables
$info['palate']['main']['layout']['bg'] = array('red' => 255, 'green' => 255, 'blue' => 255);
$info['palate']['main']['layout']['border'] = array('red' => 192, 'green' => 192, 'blue' => 192);
$info['palate']['main']['layout']['wall'] = array('red' => 0, 'green' => 0, 'blue' => 0);
$info['palate']['unit']['DELETED']['bg'] = array('red' => 0, 'green' => 0, 'blue' => 0);
$info['palate']['unit']['DAMAGED']['bg'] = array('red' => 192, 'green' => 128, 'blue' => 128);
$info['palate']['unit']['COMPANY']['bg'] = array('red' => 128, 'green' => 192, 'blue' => 128);
$info['palate']['unit']['UNAVAILABLE']['bg'] = array('red' => 128, 'green' => 128, 'blue' => 192);
$info['palate']['unit']['RESERVED']['bg'] = array('red' => 192, 'green' => 192, 'blue' => 128);
$info['palate']['unit']['DIRTY']['bg'] = array('red' => 128, 'green' => 192, 'blue' => 192);
$info['palate']['unit']['VACANT']['bg'] = array('red' => 0, 'green' => 255, 'blue' => 128);
$info['palate']['unit']['OCCUPIED']['bg'] = array('red' => 0, 'green' => 128, 'blue' => 255);
$info['palate']['unit']['LATE']['bg'] = array('red' => 255, 'green' => 64, 'blue' => 64);
$info['palate']['unit']['LOCKED']['bg'] = array('red' => 255, 'green' => 128, 'blue' => 128);
$info['palate']['unit']['LIENED']['bg'] = array('red' => 255, 'green' => 192, 'blue' => 192);
// Determine text color to go with each background
foreach ($info['palate']['unit'] AS &$code) {
$component = $code['bg'];
$method = 3;
if ($method == 1) {
foreach (array('red', 'green', 'blue') AS $prim)
$component[$prim] = 255 - $component[$prim];
} elseif ($method == 2) {
foreach (array('red', 'green', 'blue') AS $prim)
$component[$prim] = ($component[$prim]) >= 128 ? 0 : 255;
} elseif ($method == 3) {
$val = (sqrt(pow($component['red'], 2) +
pow($component['green'], 2) +
pow($component['blue'], 2)) >= sqrt(3) * 128) ? 0 : 255;
foreach (array('red', 'green', 'blue') AS $prim)
$component[$prim] = $val;
}
$code['fg'] = $component;
}
//pr($info);
/*****
* The preference would be to leave all things "screen" related
* to reside in the view. However, two separate views need this
* information. The 'view' needs it to include a clickable map
* that corresponds to the map image, and of course, the 'map'
* (or 'image') view needs it to render the image. So, in the
* controller for now, unless I come up with a better idea.
*****/
// Scale things according to desired display width
$screen_adjustment_factor = $requested_width / $info['extents']['right'];;
// Define image size
$info['width'] = $info['extents']['right'] * $screen_adjustment_factor;
$info['depth'] = $info['extents']['bottom'] * $screen_adjustment_factor;
// Go through each unit, adjusting map locations
foreach ($info['units'] AS $unit) {
$unit['left'] *= $screen_adjustment_factor;
$unit['right'] *= $screen_adjustment_factor;
$unit['top'] *= $screen_adjustment_factor;
$unit['bottom'] *= $screen_adjustment_factor;
}
$this->set(compact('info'));
$this->render('image');
}
/**************************************************************************
**************************************************************************
**************************************************************************
* mapInfo
*/
function mapInfo($site_area_id) {
// Set up array to hold the map information
$info = array('extents' => array(), 'units' => array());
$map = $this->Map->SiteArea->read(null, $site_area_id);
//pr($map);
$top_adjustment = 5;
$left_adjustment = 5;
$info['extents']['top'] = 0;
$info['extents']['left'] = 0;
$info['extents']['bottom'] = $top_adjustment + $map['Map']['depth'] + 5;
$info['extents']['right'] = $left_adjustment + $map['Map']['width'] + 5;
return $info;
// Get the overall site limits
$query = "SELECT M.id, M.width, M.depth";
$query .= " FROM pmgr_maps M";
$query .= " WHERE site_area_id = $site_area_id";
$result = $this->Map->query($query);
//pr($result);
$info['extents']['top'] = 0;
$info['extents']['left'] = 0;
$info['extents']['bottom'] = 360;
$info['extents']['right'] = 480;
$info['map_id'] = 1;
return $info;
/* // Fetch and verify the result */
/* $row = mysql_fetch_array($result); */
/* if (!$row) die("Site map query failed!"); */
/* mysql_free_result($result); */
/* // Compute the actual boundaries, adjusting for a border */
/* $top_adjustment = 5; */
/* $left_adjustment = 5; */
/* $info['extents']['top'] = 0; */
/* $info['extents']['left'] = 0; */
/* $info['extents']['bottom'] = $top_adjustment + $row['depth'] + 5; */
/* $info['extents']['right'] = $left_adjustment + $row['width'] + 5; */
/* $info['map_id'] = $row['id']; */
/* // Get list of units and positions */
/* $query = "SELECT U.id, U.name, U.status,"; */
/* $query .= " ($top_adjustment + Mu.pt_top) AS pt_top,"; */
/* $query .= " ($left_adjustment + Mu.pt_left) AS pt_left,"; */
/* //$query .= " ($top_adjustment + Mu.pt_bottom) AS pt_bottom,"; */
/* //$query .= " ($left_adjustment + Mu.pt_right) AS pt_right,"; */
/* $query .= " IF(Mu.transpose, S.depth, S.width) AS width,"; */
/* $query .= " IF(Mu.transpose, S.width, S.depth) AS depth"; */
/* //$query .= " Mu.transpose"; */
/* $query .= " FROM pmgr_maps_units Mu"; */
/* $query .= " JOIN pmgr_units U ON U.id = Mu.unit_id"; */
/* $query .= " JOIN pmgr_unit_sizes S ON S.id = U.size_id"; */
/* $query .= " WHERE Mu.map_id = $info[map_id]"; */
/* $result = sql_query($query); */
/* // Go through each one, calculating the map location */
/* while ($row = mysql_fetch_array($result)) { */
/* $info['units'][$row['id']] = */
/* array( 'id' => $row['id'], */
/* 'name' => $row['name'], */
/* 'left' => $row['pt_left'], */
/* 'right' => $row['pt_left'] + $row['width'], */
/* 'top' => $row['pt_top'], */
/* 'bottom' => $row['pt_top'] + $row['depth'], */
/* 'width' => $row['width'], */
/* 'depth' => $row['depth'], */
/* 'n-s' => $row['reverseWL'], */
/* 'status' => $row['status'] */
/* ); */
/* } */
/* // Free the result */
/* mysql_free_result($result); */
/* return $info; */
}
}
?>

View File

@@ -0,0 +1,61 @@
<?php /* -*- mode:PHP -*- */ ?>
<p>
<?php
if (isset($heading))
echo $heading;
else
echo '<h2>'.__('Maps',true).'</h2>';
?>
</p>
<table cellpadding="0" cellspacing="0">
<?php
{
if (isset($paginator)) {
echo $paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records (%start% - %end%) of %count% total', true)));
$rows = array($paginator->sort('id'),
$paginator->sort('name'),
$paginator->sort('site_area_id'),
$paginator->sort('width'),
$paginator->sort('depth'),
$paginator->sort('comment'));
} else {
$rows = array('Id', 'Name', 'Area', 'Width', 'Depth', 'Comment');
}
echo $html->tableHeaders($rows);
$rows = array();
foreach ($maps as $map) {
$rows[] = array($html->link($map['Map']['id'],
array('controller' => 'maps',
'action' => 'view',
$map['Map']['id'])),
$html->link($map['Map']['name'],
array('controller' => 'maps',
'action' => 'view',
$map['Map']['id'])),
$map['SiteArea']['name'],
$map['Map']['width'] / 12,
$map['Map']['depth'] / 12,
$map['Map']['comment']);
}
echo $html->tableCells($rows, null, array('class' => "altrow"), false, false);
}
?>
</table>
<?php
if (isset($paginator)) {
echo('<div class="paging">' . "\n");
echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));
echo(' | ');
echo $paginator->numbers();
echo(' | ');
echo $paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));
echo('</div>' . "\n");
}
?>

95
site/views/maps/image.ctp Normal file
View File

@@ -0,0 +1,95 @@
<?php /* -*- mode:PHP -*- */
// Create the image
$image=imagecreate($info['width'], $info['depth']);
// Allocate our color palate
foreach ($info['palate'] AS &$area) {
foreach ($area AS &$type) {
foreach ($type AS &$rgb) {
if (is_array($rgb)
&& isset($rgb['red'])
&& isset($rgb['green'])
&& isset($rgb['blue'])) {
$rgb['color'] = imagecolorallocatealpha($image,
$rgb['red'],
$rgb['green'],
$rgb['blue'],
isset($rgb['alpha']) ? $rgb['alpha'] : 0);
}
}
}
}
if ($info['border']) {
// Create border around image
imageline($image,
0, 0,
0, $info['depth'],
$info['palate']['main']['layout']['border']['color']);
imageline($image,
0, 0,
$info['width'], 0,
$info['palate']['main']['layout']['border']['color']);
imageline($image,
$info['width']-1, 0,
$info['width']-1, $info['depth']-1,
$info['palate']['main']['layout']['border']['color']);
imageline($image,
0, $info['depth']-1,
$info['width']-1, $info['depth']-1,
$info['palate']['main']['layout']['border']['color']);
}
// Go through each unit, placing it on the map
foreach ($info['units'] AS $unit) {
// Draw the unit borders
imageline($image,
$unit['left'], $unit['top'],
$unit['right'], $unit['top'],
$info['palate']['main']['layout']['wall']['color']);
imageline($image,
$unit['right'], $unit['top'],
$unit['right'], $unit['bottom'],
$info['palate']['main']['layout']['wall']['color']);
imageline($image,
$unit['right'], $unit['bottom'],
$unit['left'], $unit['bottom'],
$info['palate']['main']['layout']['wall']['color']);
imageline($image,
$unit['left'], $unit['bottom'],
$unit['left'], $unit['top'],
$info['palate']['main']['layout']['wall']['color']);
// Fill the unit according to its status
imagefilledrectangle($image,
$unit['left']+1, $unit['top']+1, $unit['right']-1, $unit['bottom']-1,
$info['palate']['unit'][$unit['status']]['bg']['color']);
// If the unit is wide enough, run the text horizontal,
// otherwise, we'll have to run it vertical.
if ($unit['width'] > 100) {
imagestring($image, 1,
$unit['left']+3, $unit['top']+3,
$unit['name'],
$info['palate']['unit'][$unit['status']]['fg']['color']);
}
else {
imagestringup($image, 1,
$unit['left']+3, $unit['bottom']-3,
$unit['name'],
$info['palate']['unit'][$unit['status']]['fg']['color']);
}
}
header("Content-type: image/png");
if (! imagepng($image)) {
die("Couldn't output image!");
}
// Clear image from memory
imagedestroy($image);
// Closing PHP tag intentionally omitted

View File

@@ -0,0 +1,3 @@
<div class="maps index">
<?php echo $this->element('maps') ?>
</div>

25
site/views/maps/view.ctp Normal file
View File

@@ -0,0 +1,25 @@
<?php /* -*- mode:PHP -*- */ ?>
<DIV>
<?php
echo $html->image(array('controller' => 'maps',
'action' => 'map',
$map['Map']['id']),
array('alt' => 'Site Map',
'class' => 'imagemap',
'usemap' => '#mapimg'));
?>
</DIV>
<DIV>
<?php
/*
$html->image(array('controller' => 'maps',
'action' => 'legend',
$map['Map']['id']),
array('alt' => 'Site Map Legend'));
*/
?>
</DIV>