A set of plugins to help on managing geographic information
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
|
||||
namespace App\Geo\Postal;
|
||||
|
||||
use \App\Core\Template\Output as Output;
|
||||
use \App\Geo\State\Classes\State as State;
|
||||
use \App\Geo\Postal\Classes\Postal as Postal;
|
||||
use App\Geo\City\Classes\City;
|
||||
|
||||
//Facades
|
||||
use ORM\DBInstance;
|
||||
|
||||
class PostalController
|
||||
{
|
||||
|
||||
/**
|
||||
* Index
|
||||
* Show the main Postal list
|
||||
*/
|
||||
function index()
|
||||
{
|
||||
$list = array();
|
||||
|
||||
for ($i = 0; $i <= 10; $i++) {
|
||||
$obj = array();
|
||||
$obj['id'] = $i;
|
||||
$obj['value'] = md5($i);
|
||||
$obj['description'] = sha1($i);
|
||||
$list[] = $obj;
|
||||
}
|
||||
Output::render('index', array('list' => $list));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create
|
||||
*
|
||||
* Render the main Postal formulary
|
||||
*/
|
||||
function create()
|
||||
{
|
||||
Output::setView('form', ['id' => 0]);
|
||||
Output::render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store
|
||||
*
|
||||
* Store the param on the database
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function store(Postal $postal)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* Store the param on the database
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function search($code)
|
||||
{
|
||||
|
||||
//Search database for the record
|
||||
if ($json = DBInstance::getRecord('geo_postal_caches', ['code' => ['=', $code]])) {
|
||||
$json = json_decode(stripslashes($json[0]->json));
|
||||
$json->cached = true;
|
||||
} else {
|
||||
$json = json_decode(file_get_contents('https://viacep.com.br/ws/' . $code . '/json'));
|
||||
}
|
||||
|
||||
//Respond with errror if fail #TODO add message
|
||||
if (!$json) {
|
||||
http_response_code(404);
|
||||
return;
|
||||
}
|
||||
|
||||
$state = State::findOne(['shortname' => ['=', $json->uf]]);
|
||||
$json->state = $state->id;
|
||||
|
||||
if (!$json->cached) {
|
||||
$record = [];
|
||||
$record['code'] = $code;
|
||||
$record['json'] = addslashes(json_encode($json));
|
||||
DBInstance::insertRecord('geo_postal_caches', $record);
|
||||
} else {
|
||||
return \RR\Response::json($json)->send();
|
||||
}
|
||||
|
||||
if (!$city = City::findOne(['code' => ['=', $json->ibge]])) {
|
||||
$city = new City();
|
||||
$city->state_id = $state->id;
|
||||
$city->country_id = $state->country_id;
|
||||
$city->code = $json->ibge;
|
||||
$city->name = $json->localidade;
|
||||
$city->save();
|
||||
}
|
||||
|
||||
if (!$postal = Postal::findOne(['code' => ['=', $code]])) {
|
||||
$postal = new Postal();
|
||||
$postal->country_id = $state->country_id;
|
||||
$postal->state_id = $state->id;
|
||||
$postal->city_id = $city->id;
|
||||
@$postal->road = $json->road;
|
||||
$postal->code = preg_replace('/[^0-9]/is', '', $json->cep);
|
||||
$postal->description = $json->complemento;
|
||||
$postal->save();
|
||||
}
|
||||
|
||||
return \RR\Response::json($json)->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show *
|
||||
* Render one register
|
||||
*
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function show(Postal $postal)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit
|
||||
*
|
||||
* Render the formular for a database Postal
|
||||
*
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function edit(Postal $postal)
|
||||
{
|
||||
Output::render('form', $postal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update
|
||||
* Store the changes of the param on the database
|
||||
*
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function update(Postal $postal)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy
|
||||
* If the object has soft delete.
|
||||
*
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function destroy(Postal $postal)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge
|
||||
* Remove object even with soft delete.
|
||||
*
|
||||
* @param Postal $postal
|
||||
*/
|
||||
function purge(Postal $postal)
|
||||
{
|
||||
}
|
||||
|
||||
public static function searchAndCreate($code)
|
||||
{
|
||||
$code = preg_replace('/[^0-9]/is', '', $code);
|
||||
|
||||
if ($postal = Postal::findOne(['code' => ['=', $code]])) {
|
||||
return $postal;
|
||||
}
|
||||
|
||||
//Search database for the record of load the database
|
||||
if ($json = DBInstance::getRecord('geo_postal_caches', ['code' => ['=', $code]])) {
|
||||
$json = json_decode(stripslashes($json[0]->json));
|
||||
$json->cached = true;
|
||||
} else {
|
||||
$json = json_decode(file_get_contents('https://viacep.com.br/ws/' . $code . '/json/unicode'));
|
||||
}
|
||||
|
||||
$state = State::findOne(['shortname' => ['=', $json->uf]]);
|
||||
|
||||
$json->state = $state->id;
|
||||
|
||||
if (!$json->cached) {
|
||||
$record = [];
|
||||
$record['code'] = $code;
|
||||
$record['json'] = addslashes(json_encode($json));
|
||||
DBInstance::insertRecord('geo_postal_caches', $record);
|
||||
}
|
||||
|
||||
if (!$city = City::findOne(['code' => ['=', $json->ibge]])) {
|
||||
$city = new City();
|
||||
$city->state_id = $state->id;
|
||||
$city->country_id = $state->country_id;
|
||||
$city->code = $json->ibge;
|
||||
$city->name = $json->localidade;
|
||||
$city->save();
|
||||
}
|
||||
|
||||
if (!$postal = Postal::findOne(['code' => ['=', $code]])) {
|
||||
$postal = new Postal();
|
||||
$postal->country_id = $state->country_id;
|
||||
$postal->state_id = $state->id;
|
||||
$postal->city_id = $city->id;
|
||||
$postal->road = $json->logradouro;
|
||||
$postal->code = preg_replace('/[^0-9]/is', '', $json->cep);
|
||||
$postal->description = $json->complemento;
|
||||
$postal->save();
|
||||
}
|
||||
|
||||
return $postal;
|
||||
}
|
||||
|
||||
public static function fixPostal($postal)
|
||||
{
|
||||
$json = DBInstance::getRecord('geo_postal_caches', ['code' => ['=', $postal->code]]);
|
||||
$json = json_decode(stripslashes($json[0]->json));
|
||||
|
||||
$city = City::findOne(['code' => ['=', $json->ibge]]);
|
||||
|
||||
$postal->code = preg_replace('/[^0-9]/is', '', $json->cep);
|
||||
$postal->city_id = $city->id;
|
||||
$postal->state_id = $city->state_id;
|
||||
$postal->country_id = $city->country_id;
|
||||
$postal->save();
|
||||
|
||||
return $postal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use App\Core\Template\Output as Output;
|
||||
use Routes\RouteCollection as RouteCollection;
|
||||
|
||||
//Menu itens
|
||||
RouteCollection::get('*', function() {
|
||||
//Output::addMenu('/postal', 'postal', "<i class='fa fa-fa-cubes'></i>", ['class' => 'nav-link']);
|
||||
Output::addOnSubmenu('geo', '/postal', 'Codigos postais', "", ['class' => 'nav-link']);
|
||||
}, -10)->doIgnore();
|
||||
|
||||
RouteCollection::group("/postal", function(){
|
||||
RouteCollection::get ("/", "\App\Geo\Postal\PostalController@index");
|
||||
RouteCollection::get ("/form", "\App\Geo\Postal\PostalController@create");
|
||||
RouteCollection::post ("/", "\App\Geo\Postal\PostalController@store");
|
||||
RouteCollection::get ("/search/[d:code]", "\App\Geo\Postal\PostalController@search");
|
||||
RouteCollection::get ("/[i:id]", "\App\Geo\Postal\PostalController@show");
|
||||
RouteCollection::get ("/[i:id]/edit", "\App\Geo\Postal\PostalController@edit");
|
||||
RouteCollection::put ("/[i:id]/edit", "\App\Geo\Postal\PostalController@update");
|
||||
RouteCollection::delete("/[i:id]", "\App\Geo\Postal\PostalController@destroy");
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Geo\Postal\Classes;
|
||||
|
||||
use ORM\Entity as Entity;
|
||||
use App\Geo\City\Classes\City as City;
|
||||
use App\Geo\Zone\Classes\Zone as Zone;
|
||||
use App\Geo\State\Classes\State as State;
|
||||
use App\Geo\Country\Classes\Country as Country;
|
||||
|
||||
class Postal extends Entity {
|
||||
|
||||
//const _idPolice = Array("type" => "", "min" => 0, "max" => 100000, "step" => 2);
|
||||
|
||||
const _tableName = "geo_postals";
|
||||
|
||||
/* const _properties = Array(
|
||||
"id" => ['listable' => true, 'searcheble' => false, 'orderable' => false],
|
||||
"name" => ['listable' => true, 'searcheble' => true]);
|
||||
*/
|
||||
|
||||
//const _timestamps = true;
|
||||
//const _softdelete = true;
|
||||
//const _connectionName = "";
|
||||
|
||||
public function country() {
|
||||
return $this->belongsTo(Country::class, 'country_id', 'id', $this);
|
||||
}
|
||||
|
||||
public function state() {
|
||||
return $this->belongsTo(State::class, 'state_id', 'id', $this);
|
||||
}
|
||||
|
||||
public function city() {
|
||||
return $this->belongsTo(City::class, 'city_id', 'id', $this);
|
||||
}
|
||||
|
||||
public function zone() {
|
||||
return $this->belongsTo(Zone::class, 'zone_id', 'id', $this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
use Schema\Wrapper as Wrapper;
|
||||
use App\Core\Sanity\MigratorController as Migrator;
|
||||
|
||||
function geo_postal_upgrade($pluginversion) {
|
||||
if ($pluginversion < "0.0.1") {
|
||||
$table = Wrapper::get_table("geo_postals");
|
||||
$table->addColumn("country_id", "integer", Array("null" => false));
|
||||
$table->addColumn("state_id", "integer", Array("null" => false));
|
||||
$table->addColumn("city_id", "integer", Array("null" => false));
|
||||
$table->addColumn("zone_id", "integer", Array("null" => true));
|
||||
$table->addColumn("code", "string", Array("null" => false));
|
||||
$table->addColumn("road", "string", Array("null" => true));
|
||||
$table->addColumn("description", "string", Array("null" => true));
|
||||
$table->addTimestamps();
|
||||
$table->addSoftDelete();
|
||||
$table->addForeignKey('state_id', Wrapper::get_table('geo_states'));
|
||||
$table->addForeignKey('country_id', Wrapper::get_table('geo_countries'));
|
||||
$table->addForeignKey('city_id', Wrapper::get_table('geo_cities'));
|
||||
$table->addForeignKey('zone_id', Wrapper::get_table('geo_zones'));
|
||||
$table->create();
|
||||
|
||||
$table = Wrapper::get_table("geo_postal_caches");
|
||||
$table->addColumn("code", "string", Array("null" => false));
|
||||
$table->addColumn("json", "text", Array("null" => false));
|
||||
$table->addTimestamps();
|
||||
$table->create();
|
||||
|
||||
Migrator::getInstance()->update_plugin_version("geo_postal", "1.0.0");
|
||||
return;
|
||||
}
|
||||
|
||||
if ($pluginversion < "1.0.1") {
|
||||
|
||||
$table = Wrapper::get_table("geo_postal_caches");
|
||||
$table->addColumn("code", "string", Array("null" => false));
|
||||
$table->addColumn("json", "text", Array("null" => false));
|
||||
$table->addTimestamps();
|
||||
$table->create();
|
||||
|
||||
Migrator::getInstance()->update_plugin_version("geo_postal", "1.0.1");
|
||||
return;
|
||||
}
|
||||
|
||||
//if ($pluginversion < "0.0.2") {
|
||||
//$table = Wrapper::get_table("geo_postal");
|
||||
//Migrator::getInstance()->update_plugin_version("geo_postal", "1.0.1");
|
||||
//return;
|
||||
//}
|
||||
}
|
||||
|
||||
function geo_postal_rollback($pluginversion) {
|
||||
if($pluginversion > "0.0.1"){
|
||||
$table = Wrapper::get_table("geo_postal");
|
||||
$table->drop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
$lang["postal"]["module_name"] = "Postal";
|
||||
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
$lang["postal"]["module_name"] = "Postal";
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
$plugin->name = "geo_postal";
|
||||
$plugin->version = "1.0.1";
|
||||
|
||||
$plugin->require = Array(
|
||||
Array("name" => "geo_params", "version" => "1.0.0"),
|
||||
Array("name" => "geo_country", "version" => "1.0.0"),
|
||||
Array("name" => "geo_state", "version" => "1.0.0"),
|
||||
Array("name" => "geo_city", "version" => "1.0.0"),
|
||||
Array("name" => "geo_zone", "version" => "1.0.0")
|
||||
);
|
||||
@@ -0,0 +1,10 @@
|
||||
{{@ if( {{id}} ): @}}
|
||||
<form method="POST" action="/postal/{{id}}/edit">
|
||||
<input type="hidden" name="id" value="{{id}}" />
|
||||
<input type="hidden" name="_method" value="put" />
|
||||
{{@ else: @}}
|
||||
<form method="POST" action="/postal">
|
||||
<input type="hidden" name="id" value="" />
|
||||
{{@ endif; @}}
|
||||
|
||||
</form>
|
||||
@@ -0,0 +1,4 @@
|
||||
<a href='/postal/form' >Adicionar </a>
|
||||
<div class="table-responsive">
|
||||
|
||||
</div>
|
||||
Reference in New Issue
Block a user