/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/DeliveryAreaController.php (7506B)
render('index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->queryParams)
]
);
}
/**
* @return string
*
* @Title("Create")
*/
public function actionCreate()
{
$model = new DeliveryArea();
$post = Yii::$app->getRequest()->post();
if(Yii::$app->request->post() && Yii::$app->request->isAjax) {
$post = $post['deliveryZone'];
$zoneData = $post;
if ((int)$post['id'] !== 0) {
$model = DeliveryArea::findOne($post['id']);
if ($model === null) {
$result['msg'] = 'Unable to find the zone data';
return json_encode([]);
}
} else {
$model = new DeliveryArea();
}
if ($post['type'] == 'circle') {
$model->delivery_area_type = $model::AREA_TYPE_CIRCLE;
} else {
$model->delivery_area_type = $model::AREA_TYPE_POLYGON;
}
$model->delivery_area_name = $post['delivery_area_name'];
$post = $post['shapeJson'];
switch ( $model->delivery_area_type ) {
case $model::AREA_TYPE_CIRCLE:
$model->delivery_area_radius = $post['radius'];
$model->delivery_area_data = json_encode(array((float)$post['data']['lat'], (float)$post['data']['lng']));
break;
case $model::AREA_TYPE_POLYGON:
$post['data'] = array_map(function ($point) {
return [(float)$point['lat'], (float)$point['lng']];
}, $post['data']);
$post['data'][] = $post['data'][0];
$model->delivery_area_data = json_encode($post['data']);
break;
}
if ($model->save() && $model->validate()) {
$result['status'] = 1;
$result['data'] = $zoneData;
$result['data']['id'] = (string)$model->getPrimaryKey();
} else {
$result['error'] = $model->getErrors();
}
return json_encode($result);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* [actionGet description]
* @return [type] [description]
*/
public function actionGet()
{
if( Yii::$app->request->post() && Yii::$app->request->isAjax) {
$post = Yii::$app->request->post();
$result = ['status' => STATUS_SUCCESS, 'msg' => '', 'zone' => []];
if ( $post['id'] != '' ) {
$model = DeliveryArea::find()->where(['delivery_area_key' => $post['id']])->asArray()->all();
}
else {
$model = DeliveryArea::find()->asArray()->all();
}
if ( $model === [] ) {
return json_encode([]);
}
$zoneArr = [];
foreach ($model as $index => $zone ) {
$index = $zone;
$index['shapeJson'] = [];
if ($zone['delivery_area_type'] == DeliveryArea::AREA_TYPE_CIRCLE) {
$index['type'] = 'circle';
} else {
$index['type'] = 'polygon';
}
$index['id'] = (string)$zone['delivery_area_id'];
unset($index['zone_type'], $index['delivery_area_id']);
$zone['delivery_area_lat_lon'] = json_decode($zone['delivery_area_data']);
switch ($zone['delivery_area_type']) {
case DeliveryArea::AREA_TYPE_CIRCLE:
$index['shapeJson'] = [
'radius' => $zone['delivery_area_radius'],
'data' => [
'lng' => $zone['delivery_area_lat_lon'][1],
'lat' => $zone['delivery_area_lat_lon'][0]
]
];
break;
case DeliveryArea::AREA_TYPE_POLYGON:
$zone = $zone['delivery_area_lat_lon'];
$index['shapeJson']['data'] = [];
foreach ((array)$zone as $point) {
$index['shapeJson']['data'][] = [
'lng' => $point[1],
'lat' => $point[0],
];
}
break;
}
$zoneArr[] = $index;
}
$result['zone'] = $zoneArr;
echo json_encode($result);
} else {
return json_encode([]);
}
}
/**
*
* @param type $id
* @return string
*
* @Title("Update")
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$post = Yii::$app->getRequest()->post();
if ($model->load($post) && $model->validate()) {
$model->save();
Com::successFlash('Updated Successfully');
return $this->redirect(['index']);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
*
* @param type $id
* @return string
*
* @Title("Delete")
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
if ($model === null) {
Com::failureFlash('Unable to find the DeliveryArea');
return $this->redirect(['index']);
}
$adminUserData = Yii::$app->getUser()->getIdentity();
$adminUserName = $adminUserData->attributes['user_name'];
$branchName = DeliveryArea::find()->where(['delivery_area_id' => $model->delivery_area_id])->one();
ActivityLog::log($adminUserName.' '. 'deleted delivery area '.' '. $branchName->delivery_area_name);
DeliveryArea::deleteAll(['delivery_area_key' => $id]);
Com::successFlash(Yii::t('backend','Deleted Successfully'));
return $this->redirect(['index']);
}
/**
*
* @param type $id
* @return type
* @throws NotFoundHttpException
*/
protected function findModel($id)
{
if (($model = DeliveryArea::findOne(['delivery_area_key' => $id])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}