Commit 1b0b689c3d9e6b10f6e5099052604f1c95424663

Authored by Anastasia
1 parent 96e1054c

stock map

migrations/m170721_133313_create_city_table.php
@@ -17,6 +17,7 @@ class m170721_133313_create_city_table extends Migration @@ -17,6 +17,7 @@ class m170721_133313_create_city_table extends Migration
17 'sort' => $this->integer(), 17 'sort' => $this->integer(),
18 'status' => $this->boolean() 18 'status' => $this->boolean()
19 ->defaultValue(true), 19 ->defaultValue(true),
  20 + 'coords' => $this->string(),
20 ]); 21 ]);
21 } 22 }
22 23
@@ -7,7 +7,8 @@ @@ -7,7 +7,8 @@
7 use artbox\core\behaviors\LanguageBehavior; 7 use artbox\core\behaviors\LanguageBehavior;
8 use yii\db\ActiveRecord; 8 use yii\db\ActiveRecord;
9 use yii\web\Request; 9 use yii\web\Request;
10 - 10 + use yii\helpers\Json;
  11 +
11 /** 12 /**
12 * This is the model class for table "city". 13 * This is the model class for table "city".
13 * 14 *
@@ -18,9 +19,8 @@ @@ -18,9 +19,8 @@
18 * @property Language[] $languages 19 * @property Language[] $languages
19 * @property CityLang[] $modelLangs 20 * @property CityLang[] $modelLangs
20 * @property Shop[] $shops 21 * @property Shop[] $shops
21 - *  
22 * @method bool saveWithLangs() 22 * @method bool saveWithLangs()
23 - * @method bool loadWithLangs( Request $request) 23 + * @method bool loadWithLangs( Request $request )
24 * @method void generateLangs() 24 * @method void generateLangs()
25 */ 25 */
26 class City extends ActiveRecord 26 class City extends ActiveRecord
@@ -56,6 +56,13 @@ @@ -56,6 +56,13 @@
56 [ 'status' ], 56 [ 'status' ],
57 'boolean', 57 'boolean',
58 ], 58 ],
  59 + [
  60 + [
  61 + 'coords',
  62 + 'coordsArr',
  63 + ],
  64 + 'safe',
  65 + ],
59 ]; 66 ];
60 } 67 }
61 68
@@ -96,4 +103,14 @@ @@ -96,4 +103,14 @@
96 { 103 {
97 return $this->hasMany(Shop::className(), [ 'city_id' => 'id' ]); 104 return $this->hasMany(Shop::className(), [ 'city_id' => 'id' ]);
98 } 105 }
  106 +
  107 + public function setCoordsArr($value)
  108 + {
  109 + $this->coords = Json::encode($value);
  110 + }
  111 +
  112 + public function getCoordsArr()
  113 + {
  114 + return Json::decode($this->coords);
  115 + }
99 } 116 }
views/city/_form.php
@@ -8,6 +8,8 @@ @@ -8,6 +8,8 @@
8 /* @var $model artbox\stock\models\City */ 8 /* @var $model artbox\stock\models\City */
9 /* @var $form yii\widgets\ActiveForm */ 9 /* @var $form yii\widgets\ActiveForm */
10 /* @var $modelLangs artbox\stock\models\CityLang */ 10 /* @var $modelLangs artbox\stock\models\CityLang */
  11 +
  12 + //\artbox\stock\assets\StockAsset::register($this);
11 ?> 13 ?>
12 14
13 <div class="city-form"> 15 <div class="city-form">
@@ -20,7 +22,24 @@ @@ -20,7 +22,24 @@
20 'form' => $form, 22 'form' => $form,
21 ] 23 ]
22 ) ?> 24 ) ?>
23 - 25 + <?= $form->field($model, 'coordsArr[lat]')
  26 + ->textInput(
  27 + [
  28 + 'value' => ( isset($model->coordsArr[ 'lat' ]) ) ? $model->coordsArr[ 'lat' ] : '',
  29 + 'id' => 'lat',
  30 + 'size' => 10,
  31 + ]
  32 + )
  33 + ->label(false); ?>
  34 + <?= $form->field($model, 'coordsArr[lng]')
  35 + ->textInput(
  36 + [
  37 + 'value' => ( isset($model->coordsArr[ 'lng' ]) ) ? $model->coordsArr[ 'lng' ] : '',
  38 + 'id' => 'lng',
  39 + 'size' => 10,
  40 + ]
  41 + )
  42 + ->label(false); ?>
24 <?= $form->field($model, 'sort') 43 <?= $form->field($model, 'sort')
25 ->textInput() ?> 44 ->textInput() ?>
26 45
@@ -37,3 +56,27 @@ @@ -37,3 +56,27 @@
37 <?php ActiveForm::end(); ?> 56 <?php ActiveForm::end(); ?>
38 57
39 </div> 58 </div>
  59 +<script>
  60 + window.onload = function() {
  61 + $(document)
  62 + .on('blur', '#city_title', function() {
  63 + var address = $("#city_title")
  64 + .val();
  65 + console.log(address);
  66 + var geocoder = new google.maps.Geocoder();
  67 + geocoder.geocode({'address': address}, function(results, status) {
  68 + console.log(status);
  69 + if (status == google.maps.GeocoderStatus.OK) {
  70 +
  71 + $("#lat")
  72 + .val(results[ 0 ].geometry.location.lat());
  73 + $("#lng")
  74 + .val(results[ 0 ].geometry.location.lng());
  75 + } else {
  76 + alert("Geocode was not successful for the following reason: " + status);
  77 + }
  78 + });
  79 + });
  80 + }
  81 +
  82 +</script>
views/city/_form_language.php
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 use yii\widgets\ActiveForm; 7 use yii\widgets\ActiveForm;
8 8
9 /** 9 /**
10 - * @var CityLang $model_lang 10 + * @var CityLang $model_lang
11 * @var Language $language 11 * @var Language $language
12 * @var ActiveForm $form 12 * @var ActiveForm $form
13 * @var View $this 13 * @var View $this
@@ -15,7 +15,12 @@ @@ -15,7 +15,12 @@
15 ?> 15 ?>
16 <?php 16 <?php
17 $attributeField = $form->field($model_lang, '[' . $language->id . ']title') 17 $attributeField = $form->field($model_lang, '[' . $language->id . ']title')
18 - ->textInput([ 'maxlength' => true ]); 18 + ->textInput(
  19 + [
  20 + 'maxlength' => true,
  21 + 'id' => 'city_title',
  22 + ]
  23 + );
19 echo $attributeField; 24 echo $attributeField;
20 ?> 25 ?>
21 26
@@ -36,6 +36,10 @@ $(function() { @@ -36,6 +36,10 @@ $(function() {
36 } 36 }
37 }); 37 });
38 }); 38 });
  39 +
  40 +
  41 +
  42 +
39 var lat, lng; 43 var lat, lng;
40 if ($("#lat").val() !== "" && $("#lng").val() !== ""){ 44 if ($("#lat").val() !== "" && $("#lng").val() !== ""){
41 lat = $("#lat").val(); 45 lat = $("#lat").val();