Skip to content

Commit

Permalink
Merge pull request #1282 from proditis/master
Browse files Browse the repository at this point in the history
team improvements
  • Loading branch information
proditis authored Nov 2, 2024
2 parents eb8e72f + ff8bbb8 commit 728329b
Show file tree
Hide file tree
Showing 31 changed files with 2,006 additions and 949 deletions.
33 changes: 33 additions & 0 deletions backend/migrations/m241101_080235_create_team_invite_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use yii\db\Migration;

/**
* Handles the creation of table `{{%team_invite}}`.
*/
class m241101_080235_create_team_invite_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%team_invite}}', [
'id' => $this->primaryKey(),
'team_id' => $this->integer(),
'token' => $this->string(32)->notNull()->unique()->defaultValue(''),
'created_at' => $this->dateTime(),
'updated_at' => $this->timestamp(),
]);
$this->addForeignKey('fk_team_id', 'team_invite', 'team_id', 'team', 'id', 'CASCADE', 'CASCADE');
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropForeignKey('fk_team_id', 'team_invite');
$this->dropTable('{{%team_invite}}');
}
}
30 changes: 30 additions & 0 deletions backend/migrations/m241101_102909_populate_invite_urls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use yii\db\Migration;

/**
* Class m241101_102909_populate_invite_urls
*/
class m241101_102909_populate_invite_urls extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$res = $this->db->createCommand("SELECT id FROM team")->queryAll();
foreach($res as $rec)
{
$now=new \yii\db\Expression('NOW()');
$this->upsert('team_invite',['team_id'=>$rec['id'],'token'=>\Yii::$app->security->generateRandomString(8),'created_at'=>$now]);
}
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m241101_102909_populate_invite_urls cannot be reverted.\n";
}
}
24 changes: 24 additions & 0 deletions backend/modules/frontend/controllers/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,30 @@ protected function findModel($id)

throw new NotFoundHttpException('The requested page does not exist.');
}

public function actionAjaxSearch($term, $load = false, $active = null, $status = null)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$results = [];
if (Yii::$app->request->isAjax) {
$pq = Team::find()->where(['=', 'id', $term]);
if ($load === false) {
$pq->orWhere(['like', 'name', $term]);
}
$results = array_values(ArrayHelper::map(
$pq->all(),
'id',
function ($model) {
return [
'id' => $model->id,
'label' => sprintf("(id: %d / pid: %d) %s", $model->id, $model->owner->id, \yii\helpers\Html::encode($model->name)),
];
}
));
}
return $results;
}

public function actionFreePlayerAjaxSearch($term, $load = false, $active = null, $status = null)
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
Expand Down
134 changes: 134 additions & 0 deletions backend/modules/frontend/controllers/TeamInviteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace app\modules\frontend\controllers;

use app\modules\frontend\models\TeamInvite;
use app\modules\frontend\models\TeamInviteSearch;
use app\components\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* TeamInviteController implements the CRUD actions for TeamInvite model.
*/
class TeamInviteController extends BaseController
{
/**
* @inheritDoc
*/
public function behaviors()
{
return array_merge(
parent::behaviors(),
[
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
]
);
}

/**
* Lists all TeamInvite models.
*
* @return string
*/
public function actionIndex()
{
$searchModel = new TeamInviteSearch();
$dataProvider = $searchModel->search($this->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

/**
* Displays a single TeamInvite model.
* @param int $id ID
* @return string
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Creates a new TeamInvite model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|\yii\web\Response
*/
public function actionCreate()
{
$model = new TeamInvite();

if ($this->request->isPost) {
if ($model->load($this->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
$model->loadDefaultValues();
}

return $this->render('create', [
'model' => $model,
]);
}

/**
* Updates an existing TeamInvite model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param int $id ID
* @return string|\yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}

return $this->render('update', [
'model' => $model,
]);
}

/**
* Deletes an existing TeamInvite model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param int $id ID
* @return \yii\web\Response
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Finds the TeamInvite model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param int $id ID
* @return TeamInvite the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = TeamInvite::findOne(['id' => $id])) !== null) {
return $model;
}

throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
9 changes: 9 additions & 0 deletions backend/modules/frontend/models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,13 @@ public function getStreams()
{
return $this->hasMany(TeamStream::class, ['team_id' => 'id'])->orderBy(['ts'=>SORT_DESC]);
}

/**
* @return \yii\db\ActiveQuery
*/
public function getInvite()
{
return $this->hasOne(TeamInvite::class, ['team_id' => 'id']);
}

}
90 changes: 90 additions & 0 deletions backend/modules/frontend/models/TeamInvite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace app\modules\frontend\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

/**
* This is the model class for table "team_invite".
*
* @property int $id
* @property int|null $team_id
* @property string $token
* @property string|null $created_at
* @property string $updated_at
*
* @property Team $team
*/
class TeamInvite extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'team_invite';
}

public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['team_id','token'],'required'],
[['token'],'unique'],
[['team_id'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['token'], 'string', 'max' => 32],
[['token'], 'default', 'value' => Yii::$app->security->generateRandomString(8),'on'=>'insert'],
[['team_id'], 'exist', 'skipOnError' => true, 'targetClass' => Team::class, 'targetAttribute' => ['team_id' => 'id']],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'team_id' => Yii::t('app', 'Team ID'),
'token' => Yii::t('app', 'Token'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
];
}

/**
* Gets query for [[Team]].
*
* @return \yii\db\ActiveQuery|yii\db\ActiveQuery
*/
public function getTeam()
{
return $this->hasOne(Team::class, ['id' => 'team_id']);
}

/**
* {@inheritdoc}
* @return TeamInviteQuery the active query used by this AR class.
*/
public static function find()
{
return new TeamInviteQuery(get_called_class());
}
}
34 changes: 34 additions & 0 deletions backend/modules/frontend/models/TeamInviteQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace app\modules\frontend\models;

/**
* This is the ActiveQuery class for [[TeamInvite]].
*
* @see TeamInvite
*/
class TeamInviteQuery extends \yii\db\ActiveQuery
{
/*public function active()
{
return $this->andWhere('[[status]]=1');
}*/

/**
* {@inheritdoc}
* @return TeamInvite[]|array
*/
public function all($db = null)
{
return parent::all($db);
}

/**
* {@inheritdoc}
* @return TeamInvite|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
Loading

0 comments on commit 728329b

Please sign in to comment.