public function actionUpdate($id) { $scorecard = $this->findScorecard($id); if (in_array($scorecard->registration->competition->status, [Competition::STATUS_COMPLETED, Competition::STATUS_CLOSED])) { Yii::$app->session->setFlash('warning', Yii::t('igolf', 'Scorecard already published cannot be edited.')); return $this->render('view', ['scorecard' => $scorecard]); } if (isset($_POST['Score'])) { $count = 0; foreach (Yii::$app->request->post('Score') as $k => $dataToLoad) { $pk = explode('_', $k); if ($model = Score::findOne(['scorecard_id' => $pk[0], 'hole_id' => $pk[1]])) { $ret = $model->setAttributes($dataToLoad); if ($model->save()) { $count++; } } } if ($count > 0) { $scorecard->updateScorecard(); Yii::$app->session->setFlash('success', Yii::t('igolf', 'Scorecard updated.')); } } else { $scorecard->makeScores(); } return $this->render('update', ['scorecard' => $scorecard, 'dataProvider' => new ActiveDataProvider(['query' => $scorecard->getScores()])]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Score::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['idscore' => $this->idscore, 'type' => $this->type, 'iduser' => $this->iduser, 'value' => $this->value, 'idtodo' => $this->idtodo, 'idcomment' => $this->idcomment, 'cdate' => $this->cdate, 'mdate' => $this->mdate]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Score::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['scorecard_id' => $this->scorecard_id, 'hole_id' => $this->hole_id, 'score' => $this->score, 'putts' => $this->putts, 'penalty' => $this->penalty, 'sand' => $this->sand]); $query->andFilterWhere(['like', 'note', $this->note])->andFilterWhere(['like', 'drive', $this->drive])->andFilterWhere(['like', 'regulation', $this->regulation])->andFilterWhere(['like', 'approach', $this->approach])->andFilterWhere(['like', 'putt', $this->putt]); return $dataProvider; }
/** * Creates Score entry for each hole of the scorecard if none exists. */ public function makeScores() { if (!$this->hasDetails() && $this->tees) { $allowed = $this->player->allowed($this->tees); $holes = $this->tees->getHoles()->orderBy('position')->indexBy('position')->all(); $hole_count = count($holes); $holes_to_play = $this->holes(); $start_hole = $this->startHole(); Yii::trace('toplay=' . $holes_to_play . ', tees=' . $this->tees->holes . ', start=' . $start_hole, 'Scorecard::makeScores'); if ($holes_to_play > $this->tees->holes) { return; } for ($i = 0; $i < $holes_to_play; $i++) { $hole_num = $start_hole + $i % $hole_count; $score = new Score(['scorecard_id' => $this->id, 'hole_id' => $holes[$hole_num]->id, 'allowed' => $allowed[$hole_num - 1]]); $score->save(); } } }
<?php use common\models\Score; use kartik\builder\TabularForm; use kartik\grid\GridView; use kartik\widgets\ActiveForm; use yii\helpers\Html; use yii\helpers\Url; /* @var $this yii\web\View */ /* @var $searchModel app\models\RegistrationSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('igolf', 'Scorecard'); ?> <div class="scorecard-update"> <?php $form = ActiveForm::begin(['action' => Url::to(['update-scores', 'id' => $model->id])]); ?> <?php echo TabularForm::widget(['form' => $form, 'dataProvider' => $dataProvider, 'gridSettings' => ['panel' => ['heading' => '<h4>' . $this->title . '</h4>', 'footer' => Html::submitButton('Save', ['class' => 'btn btn-primary'])]], 'serialColumn' => [], 'actionColumn' => false, 'checkboxColumn' => false, 'attributes' => ['score' => ['type' => TabularForm::INPUT_TEXT], 'putts' => ['type' => TabularForm::INPUT_TEXT], 'teeshot' => ['type' => TabularForm::INPUT_DROPDOWN_LIST, 'items' => Score::getLocalizedConstants('TARGET_')], 'teeshot_length' => ['type' => TabularForm::INPUT_TEXT], 'regulation' => ['type' => TabularForm::INPUT_CHECKBOX], 'penalty' => ['type' => TabularForm::INPUT_TEXT], 'sand' => ['type' => TabularForm::INPUT_CHECKBOX], 'approach' => ['type' => TabularForm::INPUT_DROPDOWN_LIST, 'items' => Score::getLocalizedConstants('TARGET_')], 'approach_length' => ['type' => TabularForm::INPUT_TEXT], 'putt_length' => ['type' => TabularForm::INPUT_TEXT], 'putt' => ['type' => TabularForm::INPUT_DROPDOWN_LIST, 'items' => Score::getLocalizedConstants('TARGET_')]]]); ?> <?php ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getScores() { return $this->hasMany(Score::className(), ['scorecard_id' => 'id']); }
public function actionUpdateScores($id) { $practice = $this->findModel($id); $scorecard = $practice->getScorecard(true); if (isset($_POST['Score'])) { $count = 0; foreach (Yii::$app->request->post('Score') as $k => $dataToLoad) { $pk = explode('_', $k); if ($model = Score::findOne(['scorecard_id' => $pk[0], 'hole_id' => $pk[1]])) { $ret = $model->setAttributes($dataToLoad); if ($model->save()) { $count++; } } } if ($count > 0) { Yii::$app->session->setFlash('success', Yii::t('igolf', 'Scorecard updated.')); } } $scorecard->makeScores(); return $this->render('update', ['model' => $practice]); }
use kartik\builder\TabularForm; use kartik\grid\GridView; use kartik\widgets\ActiveForm; use yii\helpers\Html; use yii\helpers\Url; /* @var $this yii\web\View */ /* @var $searchModel app\models\RegistrationSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = $scorecard->player->name; $this->params['breadcrumbs'][] = ['label' => Yii::t('igolf', 'Competitions'), 'url' => ['competition/index']]; $this->params['breadcrumbs'][] = ['label' => $scorecard->registration->competition->name, 'url' => ['competition', 'id' => $scorecard->registration->competition_id]]; $this->params['breadcrumbs'][] = ['label' => $this->title, 'url' => ['view', 'id' => $scorecard->id]]; $this->params['breadcrumbs'][] = Yii::t('igolf', 'Update'); ?> <div class="scorecard-update"> <?php $form = ActiveForm::begin(); ?> <?php echo TabularForm::widget(['form' => $form, 'dataProvider' => $dataProvider, 'gridSettings' => ['panel' => ['heading' => '<h4>' . $this->title . '</h4>', 'footer' => Html::submitButton('Save', ['class' => 'btn btn-primary']) . ' ' . Html::a(Yii::t('igolf', 'Publish'), Url::to(['publish', 'id' => $scorecard->id]), ['class' => 'btn btn-success'])]], 'serialColumn' => [], 'actionColumn' => false, 'checkboxColumn' => false, 'attributes' => ['allowed' => ['type' => TabularForm::INPUT_STATIC, 'value' => function ($model, $key, $index, $widget) { return $model->hole->par . str_repeat(' •', $model->allowed); }], 'score' => ['type' => TabularForm::INPUT_TEXT], 'putts' => ['type' => TabularForm::INPUT_TEXT], 'teeshot' => ['type' => TabularForm::INPUT_DROPDOWN_LIST, 'items' => Score::getLocalizedConstants('TARGET_')], 'teeshot_length' => ['type' => TabularForm::INPUT_TEXT], 'regulation' => ['type' => TabularForm::INPUT_CHECKBOX], 'penalty' => ['type' => TabularForm::INPUT_TEXT], 'sand' => ['type' => TabularForm::INPUT_CHECKBOX], 'approach' => ['type' => TabularForm::INPUT_DROPDOWN_LIST, 'items' => Score::getLocalizedConstants('TARGET_')], 'approach_length' => ['type' => TabularForm::INPUT_TEXT], 'putt_length' => ['type' => TabularForm::INPUT_TEXT], 'putt' => ['type' => TabularForm::INPUT_DROPDOWN_LIST, 'items' => Score::getLocalizedConstants('TARGET_')]]]); ?> <?php ActiveForm::end(); ?> </div>
/** * Finds the Score model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Score the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Score::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }