protected function findBoard($id)
 {
     if (($board = Board::findOne($id)) !== null) {
         return $board;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function beforeAction($action)
 {
     $request = \Yii::$app->urlManager->parseRequest(\Yii::$app->request);
     $board = Board::findOne(['name' => $request[1]['name']]);
     $thread = isset($request[1]['threadNum']) ? Thread::findOne($request[1]['threadNum']) : null;
     $ban = new Ban($board, $thread);
     return $ban->check();
 }
 /**
  * Updates board settings
  * @param $name
  * @return array|\yii\web\Response
  */
 public function actionUpdate($name)
 {
     $board = Board::findOne(['name' => $name]);
     $board->scenario = Board::SCENARIO_UPDATE;
     if ($board->load(\Yii::$app->request->post()) && $board->validate()) {
         if ($board->save()) {
             return $this->actionGet($board->name);
         }
     }
     return $board->errors;
 }
 public function create(string $boardName, array $data)
 {
     /** @var Board $board */
     if (!($board = Board::findOne(['name' => $boardName]))) {
         throw new UserException('Board was not found', 404);
     }
     $validationParams = $board->postsSettings->getValidationParams();
     $thread = new Thread(['boardId' => $board->id]);
     $postData = new PostData(['validationParams' => $validationParams]);
     $uploadForm = new UploadForm(['files' => PostedFile::getPostedFiles($validationParams['maxFiles']), 'validationParams' => $validationParams]);
     $toLoad = [$thread, $postData];
     $toValidate = [$thread, $postData, $uploadForm];
     if (!ARExtended::loadMultiple($toLoad, $data) || !Model::validateMultiple($toValidate)) {
         throw new UserException('Failed to load/validate data', 400);
     }
     if (strlen($postData->messageText) == 0 && count($uploadForm->files)) {
         throw new UserException('Must be attached atleast 1 file or non-empty message', 400);
     }
     $toSave = [$postData, $thread, $uploadForm];
     $postData->on(PostData::EVENT_AFTER_INSERT, function ($event) use($thread) {
         $thread->postDataId = $event->sender->id;
     });
     $saved = true;
     foreach ($toSave as $model) {
         if (method_exists($model, 'save')) {
             $saved = $saved && $model->save();
             if (!$saved) {
                 break;
             }
         }
     }
     if (!$saved) {
         for ($i = count($toSave) - 1; $i >= 0; $i--) {
             method_exists($toSave[$i], 'delete') && $toSave[$i]->delete();
         }
         throw new UserException('Failed to create thread', 500);
     }
     foreach ($uploadForm->getFileInfos() as $fileInfo) {
         $postData->link('fileInfos', $fileInfo);
     }
     $thread->link('board', $board);
     return $thread;
 }