| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- namespace app\controllers;
- use app\models\Bidding;
- use app\models\Lots;
- use app\models\Messages;
- use app\models\Eventlog;
- use dektrium\user\Mailer;
- use Yii;
- use app\models\Auctions;
- use app\models\AuctionsSearch;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use DateTime;
- use app\models\Subscriptions;
- use yii\helpers\ArrayHelper;
- /**
- * AuctionsController implements the CRUD actions for Auctions model.
- */
- class AuctionsController extends Controller
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['post'],
- ],
- ],
- ];
- }
- /**
- * Lists all Auctions models.
- * @return mixed
- */
- public function init()
- {
- /*if(Yii::$app->user->isGuest)
- {
- Yii::$app->getSession()->setFlash('warning', Yii::t('app', 'EnterWatcher ID'));
- return $this->redirect('/user/login');
- }*/
- if(@!Yii::$app->user->identity->confirmed_at && (@Yii::$app->user->identity->role == 1)){
- return $this->redirect('/registration/organizer');
- }
- $this->layout = '@app/views/layouts/backend/user';
- parent::init();
- }
- private function getIP()
- {
- return $_SERVER['REMOTE_ADDR'];
- }
- public function actionIndex()
- {
- $searchModel = new AuctionsSearch();
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
- /**
- * Displays a single Auctions model.
- * @param integer $id
- * @return mixed
- */
- public function actionView($id)
- {
- if(Yii::$app->user->isGuest)
- {
- Yii::$app->getSession()->setFlash('success', Yii::t('app', 'EnterWatcher ID'));
- return $this->redirect('/user/login');
- }
- $model = $this->findModel($id);
- if($model->user_id==Yii::$app->user->identity->id) {
- Yii::createObject(Eventlog::className())->PutLog([
- 'user_id' => Yii::$app->user->identity->id,
- 'ip' => $this->getIP(),
- 'auk_id' => $model->id,
- 'action' => Yii::t('app','OrgEnterAuk ID'),
- ]);
- $model->setFlags($id);
- return $this->render('view', [
- 'model' => $model,
- ]);
- }
- if(Yii::$app->user->can('admin'))
- {
- Yii::createObject(Eventlog::className())->PutLog([
- 'user_id' => Yii::$app->user->identity->id,
- 'ip' => $this->getIP(),
- 'auk_id' => $model->id,
- 'action' => Yii::t('app','EnterAuk ID'),
- ]);
- $model->setFlags($id);
- return $this->render('view', [
- 'model' => $model,
- ]);
- }
- if(Yii::$app->user->can('watcher'))
- {
- Yii::createObject(Eventlog::className())->PutLog([
- 'user_id' => Yii::$app->user->identity->id,
- 'ip' => $this->getIP(),
- 'auk_id' => $model->id,
- 'action' => Yii::t('app','WatcherEnterAuk ID'),
- ]);
- $model->setFlags($id);
- return $this->render('view', [
- 'model' => $model,
- ]);
- }
- else
- {
- $sql = Yii::$app->db->createCommand("SELECT status from bidding WHERE auction_id=:auction_id and user_id=:user_id");
- $sql->bindValues([':auction_id' => $model->id,':user_id' => Yii::$app->user->identity->id]);
- $res = $sql->queryOne();
- if($res==false)
- {
- Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'NeedBidding ID'));
- return $this->redirect('/auctions/index');
- }
- elseif($res['status']=="2") // 2=reject
- {
- Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'BidRejected ID'));
- return $this->redirect('/auctions/index');
- }
- elseif($res['status']=="0") // 0=default
- {
- Yii::$app->getSession()->setFlash('warning', Yii::t('app', 'BidWaiting ID'));
- return $this->redirect('/auctions/index');
- }
- elseif($res['status']=="1") // 0=accept
- {
- Yii::createObject(Eventlog::className())->PutLog([
- 'user_id' => Yii::$app->user->identity->id,
- 'ip' => $this->getIP(),
- 'auk_id' => $model->id,
- 'action' => Yii::t('app','EnterAuk ID'),
- ]);
- $model->setFlags($id);
- return $this->render('view', [
- 'model' => $model,
- ]);
- }
- else
- {
- throw new NotFoundHttpException('Page not found');
- }
- }
- }
- /**
- * Creates a new Auctions model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- print_r(Yii::$app->request->post());
- exit;
-
- if(Yii::$app->user->isGuest)
- {
- return $this->redirect('/user/login');
- }
- if(Yii::$app->user->can('admin') && Yii::$app->user->can('org')) {
- $model = new Auctions();
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- } else {
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- }
- else
- {
- return $this->redirect(['index']);
- }
- }
- /**
- * Updates an existing Auctions model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param integer $id
- * @return mixed
- */
- public function actionUpdate($id)
- {
- if(Yii::$app->user->isGuest)
- {
- return $this->redirect('/user/login');
- }
- if(Yii::$app->user->can('admin')) {
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
- else
- {
- return $this->redirect(['index']);
- }
- }
- /**
- * Deletes an existing Auctions model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param integer $id
- * @return mixed
- */
- public function actionDelete($id)
- {
- if(Yii::$app->user->isGuest)
- {
- return $this->redirect('/user/login');
- }
- if(Yii::$app->user->can('admin')) {
- $lotId = $this->findModel($id)->lot_id;
- $lots = Lots::findOne($lotId);
- $lots->updateAttributes(['lot_lock' => 0]);
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- else
- {
- return $this->redirect(['index']);
- }
- }
- public function actionTradelog($id)
- {
- $user_id = '0';
- $query = Yii::$app->db->createCommand('SELECT * FROM trade_logs WHERE auk_id=:id'); //AND user_id=:user_id'
- $query->bindValues([':id' => $id]); //':user_id' => $user_id]
- $result = $query->queryAll();
- $filename = "../uploads/temp/tradelog-".$id.$user_id.date('Ymdhis').".doc";
- $file = fopen($filename,'w+');
- foreach ($result as $item => $value)
- {
- fputs($file,$value['date']." ".$value['comment']."\n");
- }
- fclose($file);
- Yii::$app->response->SendFile($filename);
- }
- public function actionBid($id, $special = null)
- {
- if(Yii::$app->user->isGuest)
- {
- return $this->redirect('/user/login');
- }
- $model = $this->findModel($id);
- $bid_date = new DateTime($model->bidding_date);
- $now_date = new DateTime(date("Y-m-d H:i:s"));
- //var_dump($bid_date->diff($now_date));
- if($bid_date < $now_date)
- {
- Yii::$app->session->setFlash('danger', Yii::t('app', 'BiddingTimeEnd ID'));
- return $this->redirect(['/lots/view', 'id' => $model->lot_id]);
- }
- // test for documents
- $sql = Yii::$app->db->createCommand("SELECT id from files WHERE auction_id=:auction_id and user_id=:user_id order by id desc");
- $sql->bindValues([':auction_id' => $model->id,':user_id' => Yii::$app->user->identity->id]);
- $result = $sql->queryOne();
- if(!$result)
- {
- Yii::$app->session->setFlash('danger', Yii::t('app', 'NeedDownloadFiles ID'));
- return $this->redirect(['/lots/view', 'id' => $model->lot_id]);
- }
- else
- {
- $file_id = $result['id'];
- }
- // test for repeat
- $sql = Yii::$app->db->createCommand("SELECT * from bidding WHERE auction_id=:auction_id and user_id=:user_id");
- $sql->bindValues([':auction_id' => $model->id,':user_id' => Yii::$app->user->identity->id]);
- $result = $sql->queryOne();
- if(!$result)
- {
- Yii::createObject(Bidding::className())->CreateBid([
- 'auction_id' => $model->id,
- 'user_id' => Yii::$app->user->identity->id,
- 'org_id'=>$model->user_id,
- 'file_id'=>$file_id,
- 'special_price' => ArrayHelper::getValue(YII::$app->request->post('Bidding'), 'special_price', 0)
- ]);
- $notes_org = Yii::$app->user->identity->at_org ." ". Yii::t('app', 'NotesOrg ID') .": ". $model->name ." / ".
- Yii::t('app','LotNumber ID'). $model->lot_num ." ". $model->lot->name; //Yii::t('app', 'Notes2Org ID');
- $notes_self = Yii::t('app', 'NotesMember ID') .": ". $model->name. " / ". Yii::t('app','Lot ID'). " " .$model->lot_num ." ". $model->lot->name;
- Yii::createObject(Messages::className())->CreateMessage(['user_id' => $model->user_id, 'notes' => $notes_org]);
- Subscriptions::subscribe($model->category->id, Yii::$app->user->getId());
- // отправка на почту (временно)
- //$mailer = Yii::$container->get(Mailer::className());
- //$mailer->sendMessage("vanouub@meta.ua", $notes_org, 'welcome.php');
- Yii::createObject(Messages::className())->CreateMessage(['user_id' => Yii::$app->user->identity->id, 'notes' => $notes_self]);
- Yii::$app->session->setFlash('success', Yii::t('app', 'BidWellDone ID'));
- return $this->redirect(['index', 'id' => Yii::$app->user->identity->id]);
- }
- else
- {
- Yii::$app->session->setFlash('warning', Yii::t('app', 'BidExist ID'));
- return $this->redirect(['index', 'id' => Yii::$app->user->identity->id]);
- }
- }
- /**
- * Finds the Auctions model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param integer $id
- * @return Auctions the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = Auctions::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- }
|