/var/www/vhosts/nabawater/backend/controllers
Edit: /var/www/vhosts/nabawater/backend/controllers/AdsController.php (9743B)
[
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Ads models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new AdsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Ads model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Ads model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Ads();
$modelLang = new AdsLang();
$appLanguages = Language::getAppLanguages();
if ($model->load(Yii::$app->request->post())) {
// Handle file upload
$uploadedFile = \yii\web\UploadedFile::getInstance($model, 'image_path');
if ($uploadedFile) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setpath($uploadHelper->join([$uploadHelper::TEMP, $uploadHelper::ADS]));
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$realpath = $uploadHelper->getRealPath($path);
$uploadedFile->saveAs($path);
$model->image_path = DIRECTORY_SEPARATOR . $realpath;
}
// Set title from first language to pass validation
$langData = Yii::$app->request->post('AdsLang', []);
$firstLang = array_key_first($appLanguages);
$model->title = isset($langData['title'][$firstLang]) ? $langData['title'][$firstLang] : '';
if ($model->save()) {
// Save language data for all languages
foreach ($appLanguages as $langCode => $langName) {
$adsLang = new AdsLang();
$adsLang->ads_id = $model->ads_id;
$adsLang->language_code = $langCode;
$adsLang->title = isset($langData['title'][$langCode]) ? $langData['title'][$langCode] : '';
$adsLang->description = isset($langData['description'][$langCode]) ? $langData['description'][$langCode] : '';
$adsLang->save();
}
Yii::$app->session->setFlash('success', Yii::t('backend', 'Ad created successfully'));
return $this->redirect(['index']);
} else {
// Show validation errors
Yii::$app->session->setFlash('error', 'Validation failed: ' . json_encode($model->errors));
}
}
return $this->render('create', [
'model' => $model,
'modelLang' => $modelLang,
'appLanguages' => $appLanguages,
]);
}
/**
* Updates an existing Ads model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$appLanguages = Language::getAppLanguages();
// Load existing translation data for all languages
$modelLang = [];
foreach ($appLanguages as $langCode => $langName) {
$langModel = AdsLang::findOne(['ads_id' => $id, 'language_code' => $langCode]);
if ($langModel) {
$modelLang[$langCode] = $langModel;
}
}
if ($model->load(Yii::$app->request->post())) {
// Handle file upload
$uploadedFile = \yii\web\UploadedFile::getInstance($model, 'image_path');
if ($uploadedFile) {
$uploadHelper = UploadHelper::getInstance();
$uploadHelper->setpath($uploadHelper->join([$uploadHelper::TEMP, $uploadHelper::ADS]));
$uploadHelper->setUploadedFile($uploadedFile);
$path = $uploadHelper->getPath(true);
$realpath = $uploadHelper->getRealPath($path);
$uploadedFile->saveAs($path);
$model->image_path = DIRECTORY_SEPARATOR . $realpath;
}
// Set title from first language to pass validation
$langData = Yii::$app->request->post('AdsLang', []);
$firstLang = array_key_first($appLanguages);
$model->title = isset($langData['title'][$firstLang]) ? $langData['title'][$firstLang] : $model->title;
if ($model->save()) {
// Update language data for all languages
foreach ($appLanguages as $langCode => $langName) {
$adsLang = AdsLang::findOne(['ads_id' => $model->ads_id, 'language_code' => $langCode]);
if (!$adsLang) {
$adsLang = new AdsLang();
$adsLang->ads_id = $model->ads_id;
$adsLang->language_code = $langCode;
}
$adsLang->title = isset($langData['title'][$langCode]) ? $langData['title'][$langCode] : '';
$adsLang->description = isset($langData['description'][$langCode]) ? $langData['description'][$langCode] : '';
$adsLang->save();
}
Yii::$app->session->setFlash('success', Yii::t('backend', 'Ad updated successfully'));
return $this->redirect(['index']);
} else {
// Show validation errors
Yii::$app->session->setFlash('error', 'Validation failed: ' . json_encode($model->errors));
}
}
return $this->render('update', [
'model' => $model,
'modelLang' => $modelLang,
'appLanguages' => $appLanguages,
]);
}
/**
* Change status
* @return \yii\web\Response
*/
public function actionChangeStatus()
{
$post = Yii::$app->request->post();
$result = ['status' => STATUS_FAIL, 'msg' => ''];
if (!isset($post['key']) || !isset($post['status'])) {
$result['msg'] = Yii::t('backend', 'PARAM_MISSING');
return $this->asJson($result);
}
$model = $this->findModel($post['key']);
$model->status = $post['status'];
if ($model->save()) {
$result['status'] = STATUS_SUCCESS;
$result['msg'] = Yii::t('backend', 'Status updated successfully');
} else {
$result['msg'] = Yii::t('backend', 'Failed to update status');
}
return $this->asJson($result);
}
/**
* Deletes an existing Ads model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Publish the ad and send push notification.
* @param integer $id
* @return mixed
*/
public function actionPublish($id)
{
$model = $this->findModel($id);
$model->status = Ads::STATUS_ACTIVE;
$model->save();
// Send push notification
$oneSignal = OneSignal::getInstance();
$oneSignal->sendCustomerNotification(
['en' => Configuration::get(Configuration::APP_NAME)],
['en' => 'New Ad: ' . $model->title],
1 // Assuming app_type 1 for all
);
// Save notification
$notification = new Notification();
$notification->notification_title = 'New Advertisement';
$notification->notification_description = 'Check out our new ad: ' . $model->title;
$notification->is_all_user = Notification::GLOBAL_NOTIFICATION;
$notification->save();
Yii::$app->session->setFlash('success', 'Ad published and notification sent.');
return $this->redirect(['view', 'id' => $model->ads_id]);
}
/**
* Finds the Ads model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Ads the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Ads::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}