CurdForm.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace addons\RfDemo\common\forms;
  3. use yii\helpers\Json;
  4. use yii\db\Expression;
  5. use common\helpers\ArrayHelper;
  6. use addons\RfDemo\common\models\Curd;
  7. use addons\RfDemo\common\models\CurdMap;
  8. /**
  9. * Class CurdForm
  10. * @package addons\RfDemo\common\forms
  11. */
  12. class CurdForm extends Curd
  13. {
  14. /**
  15. * 地图定位
  16. *
  17. * @var array
  18. */
  19. public $longitude_and_latitude = [];
  20. /**
  21. * 地图范围
  22. *
  23. * @var
  24. */
  25. public $map_overlay;
  26. /**
  27. * @return array
  28. */
  29. public function rules()
  30. {
  31. return ArrayHelper::merge(parent::rules(), [
  32. [['map_overlay'], 'safe']
  33. ]);
  34. }
  35. /**
  36. * @return array|string[]
  37. */
  38. public function attributeLabels()
  39. {
  40. return ArrayHelper::merge(parent::attributeLabels(), [
  41. 'longitude_and_latitude' => '地图位置',
  42. 'map_overlay' => '地图范围',
  43. ]);
  44. }
  45. /**
  46. * @return void
  47. */
  48. public function afterFind()
  49. {
  50. $this->map_overlay = CurdMap::find()
  51. ->select(['name', 'type', 'path', 'radius', 'shipping_fee'])
  52. ->where(['curd_id' => $this->id])
  53. ->asArray()
  54. ->all();
  55. parent::afterFind();
  56. }
  57. public function afterSave($insert, $changedAttributes)
  58. {
  59. if (!empty($this->map_overlay) && !is_array($this->map_overlay)) {
  60. $this->map_overlay = Json::decode($this->map_overlay);
  61. }
  62. CurdMap::deleteAll(['curd_id' => $this->id]);
  63. foreach ($this->map_overlay as $item) {
  64. $model = new CurdMap();
  65. $model->curd_id = $this->id;
  66. $model->attributes = $item;
  67. $path = [];
  68. foreach ($item['path'] as $value) {
  69. $path[] = implode(' ', $value);
  70. }
  71. // 说明
  72. // 线串至少有两个点
  73. // 多边形至少有一个环
  74. // 多边形环关闭(第一个和最后一个点相同)
  75. // 多边形环至少有 4 个点(最小多边形是一个三角形,第一个和最后一个点相同)
  76. // 集合不为空(除了GeometryCollection)
  77. $path[] = $path[0];
  78. $path = implode(',', $path);
  79. $model->polygon = new Expression("GeomFromText(:point)", [':point' => 'POLYGON((' . $path . '))']);
  80. $model->save();
  81. }
  82. parent::afterSave($insert, $changedAttributes);
  83. }
  84. }
粤ICP备19079148号