public function run() { $bannerPlace = BannerPlace::findOne($this->place); if ($bannerPlace) { $banners = Banner::find()->active()->language()->with('stat')->all(); if (!empty($banners)) { $bannersSum = 0; foreach ($banners as $banner) { $bannersSum += $banner->priority; } if ($bannersSum > 0) { $bannersRandNum = mt_rand(1, $bannersSum); foreach ($banners as $banner) { if ($banner->priority > 0) { $data = $banner; $bannersRandNum = $bannersRandNum - $banner->priority; if ($bannersRandNum <= 0) { break; } } } } else { $data = $banners[array_rand($banners)]; } /** @var $data Banner */ if (isset($data)) { $data->addView(); return $this->render('BannerPlace', ['data' => $data]); } else { return ''; } } } }
/** * Finds the BannerPlace model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return BannerPlace the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BannerPlace::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }