Ecco controller,model e view.
app/controllers/SistopController
namespace app\controllers;
use Yii;
use app\models\Sistop;
use app\models\SistopSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* SistopController implements the CRUD actions for Sistop model.
*/
class SistopController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view'],
'roles' => ['view']
],
[
'allow' => true,
'actions' => ['update'],
'roles' => ['update']
],
[
'allow' => true,
'actions' => ['create'],
'roles' => ['create']
],
[
'allow' => true,
'actions' => ['delete'],
'roles' => ['delete']
],
[
'allow' => false
]
]
]
];
}
/**
* Lists all Sistop models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new SistopSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Sistop model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Sistop model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Sistop();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_sistop]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Sistop model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_sistop]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Sistop model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Sistop model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Sistop the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Sistop::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
app/models/Sistop.php
namespace app\models;
use Da\User\Model\User;
use Yii;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
/**
* This is the model class for table "{{%sistop}}".
*
* @property int $id_sistop
* @property string|null $elencosistop Sistema Operativo
* @property string|null $licsistop Licenza Uso
* @property int|null $qdsistop Q.tà disp.
* @property int|null $qrsistop Q.tà rim.
* @property string|null $created_at Data inserimento
* @property int|null $created_by Inserito da
* @property string|null $updated_at Data modifica
* @property int|null $updated_by Modificato da
*
* @property Gestionalepc[] $gestionalepcs
*/
class Sistop extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%sistop}}';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
[
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'created_by',
'updatedByAttribute' => 'updated_by',
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['qdsistop', 'qusistop'], 'integer'],
[['elencosistop'], 'string', 'max' => 255],
[['elencosistop'], 'unique'],
[['elencosistop'], 'required'],
[['licsistop'], 'string', 'max' => 80],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id_sistop' => Yii::t('app', 'ID'),
'elencosistop' => Yii::t('app', 'Sistema Operativo'),
'licsistop' => Yii::t('app', 'Product Key'),
'qdsistop' => Yii::t('app', 'Q.tà disp.'),
'qusistop' => Yii::t('app', 'Q.tà util.'),
'created_at' => Yii::t('app', 'Data inserimento'),
'created_by' => Yii::t('app', 'Inserito da'),
'updated_at' => Yii::t('app', 'Data modifica'),
'updated_by' => Yii::t('app', 'Modificato da'),
];
}
public function getCreatore()
{
return $this->hasOne(User::className(), ['id' => 'created_by']);
}
public function getModificatore()
{
return $this->hasOne(User::className(), ['id' => 'updated_by']);
}
/**
* Gets query for [[Gestionepcs]].
*
* @return \yii\db\ActiveQuery|GestionalepcQuery
*/
public function getGestionalepcs()
{
return $this->hasMany(Gestionalepc::className(), ['os' => 'id_sistop']);
}
/**
* {@inheritdoc}
* @return SistopQuery the active query used by this AR class.
*/
public static function find()
{
return new SistopQuery(get_called_class());
}
}
app/models/SistopSearch.php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Sistop;
class SistopSearch extends Sistop
{
public function rules()
{
return [
[['id_sistop', 'qdsistop', 'qusistop'], 'integer'],
[['elencosistop', 'licsistop'], 'safe'],
];
}
public function scenarios()
{
return Model::scenarios();
}
public function search($params)
{
$query = Sistop::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_sistop' => $this->id_sistop,
'qdsistop' => $this->qdsistop,
'qusistop' => $this->qusistop,
'created_at' => $this->created_at,
'created_by' => $this->created_by,
'updated_at' => $this->updated_at,
'updated_by' => $this->updated_by,
]);
$query->andFilterWhere(['like', 'elencosistop', $this->elencosistop])
->andFilterWhere(['like', 'licsistop', $this->licsistop]);
return $dataProvider;
}
}
app/views/sistop/index.php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\Sistop */
$this->title = $model->elencosistop;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Elenco Sistemi Operativi'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="sistop-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a(Yii::t('app', 'Modifica'), ['update', 'id' => $model->id_sistop], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('app', 'Elimina'), ['delete', 'id' => $model->id_sistop], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
<?= Html::a(Yii::t('app', 'Aggiungi'), ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Elenco Completo'), ['index'], ['class' => 'btn btn-warning']) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
//'id_sistop',
'elencosistop',
'licsistop',
'qdsistop',
[
'attribute' => 'qusistop',
'value' => function ($sistopModel) {
return $sistopModel->getGestionalepcs()->count();
}
],
[
'header' => 'Totale',
'value' => function($data) { return $data->qdsistop - $data->qusistop; }
]
'created_at:datetime',
'creatore.username',
'updated_at:datetime',
'modificatore.username',
],
]) ?>
</div>