|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|
* This file is part of open source system FreeNetIS
|
|
* and it is released under GPLv3 licence.
|
|
*
|
|
* More info about licence can be found:
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|
*
|
|
* More info about project can be found:
|
|
* http://www.freenetis.org/
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* Gets number of current SVN revision ID
|
|
*
|
|
* @return integer
|
|
*/
|
|
function get_SVN_rev()
|
|
{
|
|
$svnid = '$Rev: 1254 $';
|
|
$svnid = substr($svnid , 6);
|
|
$svnid = substr($svnid , 0, -2);
|
|
return $svnid;
|
|
}
|
|
|
|
/**
|
|
* Upgrates streets
|
|
*
|
|
* @author Ondřej Fibich
|
|
* @throws Exception
|
|
* @return bool
|
|
*/
|
|
function upgrade_sql_after()
|
|
{
|
|
// My servant
|
|
$ap_model = new Address_point_Model();
|
|
// My map
|
|
$aps = $ap_model->find_all();
|
|
// My notebook
|
|
$street_id_cache = array();
|
|
// My connection to my kingdom
|
|
$db = Database::instance();
|
|
|
|
try
|
|
{
|
|
// My journey begins!
|
|
$ap_model->transaction_start();
|
|
|
|
// One day I'd like to walk throught all streets in my kingdom.
|
|
/* @var $ap Address_point_Model */
|
|
foreach ($aps as $ap)
|
|
{
|
|
// Some streets didn't exists in my map.
|
|
if (!$ap->street_id)
|
|
{
|
|
continue;
|
|
}
|
|
// Sometimes I get lost and I went to street with same name.
|
|
if (array_key_exists($ap->street_id, $street_id_cache))
|
|
{
|
|
// But sometimes the street was in the different town.
|
|
// Than I had to mark it to my map and notebook.
|
|
if ($ap->town_id != $street_id_cache[$ap->street_id])
|
|
{
|
|
$street = new Street_Model();
|
|
$street = $street->get_street($ap->street->street, $ap->town_id);
|
|
|
|
$ap->street_id = $street->id;
|
|
$ap->save_throwable();
|
|
|
|
$street_id_cache[$ap->street_id] = $ap->town_id;
|
|
}
|
|
// Street belongs to town which I knew.
|
|
else
|
|
{
|
|
$ap->street->town_id = $ap->town_id;
|
|
$ap->street->save_throwable();
|
|
}
|
|
}
|
|
// When I get to streets I had never been before,
|
|
// I marked it to my note.
|
|
else
|
|
{
|
|
$ap->street->town_id = $ap->town_id;
|
|
$ap->street->save_throwable();
|
|
|
|
$street_id_cache[$ap->street_id] = $ap->town_id;
|
|
}
|
|
}
|
|
|
|
// My journey ends!
|
|
$ap_model->transaction_commit();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
// Something terrible happends!
|
|
$ap_model->transaction_rollback();
|
|
// I'am dying :-(
|
|
throw $e;
|
|
}
|
|
|
|
// I'm back home with cup of tea :-)
|
|
return TRUE;
|
|
}
|
|
|
|
/**
|
|
* Column for town key in streets
|
|
*
|
|
* @author Ondřej Fibich
|
|
*/
|
|
$upgrade_sql[get_SVN_rev()] = array
|
|
(
|
|
"ALTER TABLE `streets` ADD `town_id` INT( 11 ) NULL AFTER `id`;",
|
|
|
|
"ALTER TABLE `streets` ADD INDEX ( `town_id` );",
|
|
|
|
"ALTER TABLE `streets` ADD FOREIGN KEY ( `town_id` ) REFERENCES `towns` ( `id` ) ON DELETE SET NULL;",
|
|
);
|