Skip to content

Instantly share code, notes, and snippets.

@chrisjimallen
Created February 18, 2013 08:03
Show Gist options
  • Save chrisjimallen/4975787 to your computer and use it in GitHub Desktop.
Save chrisjimallen/4975787 to your computer and use it in GitHub Desktop.
Country Imports
<?php
class CountryImportsController extends AppController {
var $name = 'CountryImports';
/**
* displays the import link
*
*/
function index() {
}
/**
* reads country_imports.csv and imports country/tariff data into relevant tables.
*
*This function reads the csv file and operates on it as if it were reading a mysql table. It grabs the data that
* it needs, truncates the countries & countries_networks tables, and inserts the new data.
*/
function add(){
$csv = $this->CountryImport->find('all',array(
'fields'=>array(
'"Country"',
'"Incoming"',
'"National"',
'"CallstoUSA_Canada"',
'"CallstoUK"',
'"CallstoOther"',
'"ISO3166"',
'"O2"',
'"Iridium"',
'"D-AMPS"',
'"Japanese"',
'"Korean"',
'"GSM900"',
'"GSM1800"',
'"GSM1900"',
'"GSM850"',
'"CDMA"',
'"CDMA Other"',
'"iDEN"',
'"Thuraya"',
'"O2 GPRS"',
'"O2 GPRS Rate/10KB"',
'"O2 3G"'
))
);
ClassRegistry::init('Country')->query("TRUNCATE `countries`");
ClassRegistry::init('Country')->query("TRUNCATE `countries_networks`");
foreach ($csv['country_imports'] as $import){
//insert into Countries
ClassRegistry::init('Country')->save(
array('id' => '',
'countryName'=> str_replace('&',' and ',$import['"Country"']),
'countrySlug'=> strtolower(Inflector::slug($import['"Country"'],'-')),
'iso3166'=> $import['"ISO3166"'],
'incoming' => $import['"Incoming"'],
'national' => $import['"National"'],
'usaCanada' => $import['"CallstoUSA_Canada"'],
'otherPhones' => $import['"CallstoUK"'],
'otherCountries' => $import['"CallstoOther"'],
'sms' => 0.80,
'dataRate' => $import['"O2 GPRS Rate/10KB"']
));
//insert into Countries_Networks
$networks['o2'] = ($import['"O2"'] == 'yes') ? true : false;
$networks['iridium'] = ($import['"Iridium"'] == 'yes') ? true : false;
$networks['dAmps'] = ($import['"D-AMPS"'] == 'yes') ? true : false;
$networks['japanese'] = ($import['"Japanese"'] == 'yes') ? true : false;
$networks['korean'] = ($import['"Korean"'] == 'yes') ? true : false;
$networks['gsm900'] = ($import['"GSM900"'] == 'yes') ? true : false;
$networks['gsm1800'] = ($import['"GSM1800"'] == 'yes') ? true : false;
$networks['gsm1900'] = ($import['"GSM1900"'] == 'yes') ? true : false;
$networks['gsm850'] = ($import['"GSM850"'] == 'yes') ? true : false;
$networks['cdma'] = ($import['"CDMA"'] == 'yes') ? true : false;
$networks['cdmaOther'] = ($import['"CDMA Other"'] == 'yes') ? true : false;
$networks['iDen'] = ($import['"iDEN"'] == 'yes') ? true : false;
$networks['thuraya'] = ($import['"Thuraya"'] == 'yes') ? true : false;
$networks['o2 3g'] = ($import['"O2 3G"'] == 'yes') ? true : false;
$networks['o2 gprs'] = ($import['"O2 GPRS"'] == 'yes') ? true : false;
$i = 1;
foreach($networks as $network => $inCountry) {
if($inCountry) {
ClassRegistry::init('CountriesNetworks')->save(
array('id' => '',
'country_id'=> ClassRegistry::init('Country')->id ,
'network_id'=> $i
));
}
++$i;
}
}
$this->set('countryCount', count($csv['country_imports']));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment