Last active
April 2, 2023 15:24
-
-
Save nedarta/15726858011b20f77981e4b7d770018e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use app\models\Event; | |
use app\models\EventArtwork; | |
use yii\helpers\ArrayHelper; | |
use yii\helpers\Html; | |
use yii\widgets\ActiveForm; | |
/** @var yii\web\View $this */ | |
/** @var app\models\EventForm $model */ | |
/** @var yii\widgets\ActiveForm $form */ | |
?> | |
<?php $this->registerJs(" | |
$('.delete-button').click(function() { | |
var detail = $(this).closest('.event-artwork'); | |
var updateType = detail.find('.update-type'); | |
if (updateType.val() === " . json_encode(EventArtwork::UPDATE_TYPE_UPDATE) . ") { | |
//marking the row for deletion | |
updateType.val(" . json_encode(EventArtwork::UPDATE_TYPE_DELETE) . "); | |
detail.hide(); | |
} else { | |
//if the row is a new row, delete the row | |
detail.remove(); | |
} | |
}); | |
"); | |
?> | |
<div class="event-form-form"> | |
<?php $form = ActiveForm::begin( | |
['enableClientValidation' => false, 'options'=>['enctype'=>'multipart/form-data']], | |
); ?> | |
<?= $form->field($model, 'event_id')->dropDownList(ArrayHelper::map(Event::find()->where(['is_active' => '1'])->all(),'id','title'), ['class'=> 'hidden'])->label(false); ?> | |
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> | |
<?= $form->field($model, 'surname')->textInput(['maxlength' => true]) ?> | |
<?= $form->field($model, 'year_of_birth')->textInput(['maxlength' => '4']) ?> | |
<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?> | |
<?= $form->field($model, 'phone_number')->textInput(['maxlength' => '8']) ?> | |
<?= $form->field($model, 'member_of_lms')->checkBox() ?> | |
<?php foreach ($modelArtworks as $i => $modelArtwork) : ?> | |
<div class="row event-artwork event-artwork-<?= $i ?>"> | |
<div class="col-md-10"> | |
<?= Html::activeHiddenInput($modelArtwork, "[$i]id") ?> | |
<?= Html::activeHiddenInput($modelArtwork, "[$i]updateType", ['class' => 'update-type']) ?> | |
<?= $form->field($modelArtwork, "[$i]image") ?> | |
<?= $form->field($modelArtwork, "[$i]upload")->fileInput() ?> | |
<?= $form->field($modelArtwork, "[$i]title") ?> | |
<?= $form->field($modelArtwork, "[$i]artwork_technique") ?> | |
<?= $form->field($modelArtwork, "[$i]size_dimensions") ?> | |
<?= $form->field($modelArtwork, "[$i]year") ?> | |
<?= $form->field($modelArtwork, "[$i]description") ?> | |
</div> | |
<div class="col-md-2"> | |
<?= Html::button('x', ['class' => 'delete-button btn btn-danger', 'data-target' => "receipt-detail-$i"]) ?> | |
</div> | |
</div> | |
<?php endforeach; ?> | |
<div class="form-group"> | |
<?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?> | |
<?= Html::submitButton('Add row', ['name' => 'addRow', 'value' => 'true', 'class' => 'btn btn-info']) ?> | |
</div> | |
<?php ActiveForm::end(); ?> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
use Yii; | |
/** | |
* This is the model class for table "event_artwork". | |
* | |
* @property int $id | |
* @property int $event_form_id | |
* @property string $image | |
* @property int $title | |
* @property string $artwork_technique | |
* @property string $size_dimensions | |
* @property int $year | |
* @property string|null $description | |
* | |
* @property EventForm $eventForm | |
*/ | |
class EventArtwork extends \yii\db\ActiveRecord | |
{ | |
public $upload; | |
/** | |
* these are flags that are used by the form to dictate how the loop will handle each item | |
*/ | |
const UPDATE_TYPE_CREATE = 'create'; | |
const UPDATE_TYPE_UPDATE = 'update'; | |
const UPDATE_TYPE_DELETE = 'delete'; | |
const SCENARIO_BATCH_UPDATE = 'batchUpdate'; | |
private $_updateType; | |
public function getUpdateType() | |
{ | |
if (empty($this->_updateType)) { | |
if ($this->isNewRecord) { | |
$this->_updateType = self::UPDATE_TYPE_CREATE; | |
} else { | |
$this->_updateType = self::UPDATE_TYPE_UPDATE; | |
} | |
} | |
return $this->_updateType; | |
} | |
public function setUpdateType($value) | |
{ | |
$this->_updateType = $value; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function tableName() | |
{ | |
return 'event_artwork'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rules() | |
{ | |
return [ | |
['updateType', 'required', 'on' => self::SCENARIO_BATCH_UPDATE], | |
['updateType', 'in', 'range' => [ | |
self::UPDATE_TYPE_CREATE, | |
self::UPDATE_TYPE_UPDATE, | |
self::UPDATE_TYPE_DELETE | |
],'on' => self::SCENARIO_BATCH_UPDATE], | |
[['title', 'artwork_technique', 'size_dimensions', 'year'], 'required'], | |
[['event_form_id', 'year'], 'integer'], | |
//[['year'], 'integer', 'max' => 4], | |
[['artwork_technique', 'description'], 'string'], | |
[['image', 'size_dimensions'], 'string', 'max' => 255], | |
[['upload'], 'file', 'extensions' => 'jpg, jpeg, png, JPEG, JPG, PNG'], | |
['event_form_id', 'required', 'except' => self::SCENARIO_BATCH_UPDATE], | |
[['event_form_id'], 'exist', 'skipOnError' => true, 'targetClass' => EventForm::class, 'targetAttribute' => ['event_form_id' => 'id']], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => Yii::t('app', 'ID'), | |
'event_form_id' => Yii::t('app', 'Event Form ID'), | |
//'image' => Yii::t('app', 'Image'), | |
'upload' => Yii::t('app', 'Upload'), | |
'title' => Yii::t('app', 'Title'), | |
'artwork_technique' => Yii::t('app', 'Artwork Technique'), | |
'size_dimensions' => Yii::t('app', 'Size Dimensions'), | |
'year' => Yii::t('app', 'Year'), | |
'description' => Yii::t('app', 'Description'), | |
]; | |
} | |
/** | |
* Gets query for [[EventForm]]. | |
* | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getEventForm() | |
{ | |
return $this->hasOne(EventForm::class, ['id' => 'event_form_id']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\models; | |
use Yii; | |
/** | |
* This is the model class for table "event_form". | |
* | |
* @property int $id | |
* @property int $event_id | |
* @property int|null $user_id | |
* @property string|null $code_id | |
* @property string $name | |
* @property string $surname | |
* @property int $year_of_birth | |
* @property string|null $email | |
* @property int $phone_number | |
* @property int $member_of_lms | |
* @property int|null $created_at | |
* @property int|null $updated_at | |
* | |
* @property Event $event | |
* @property EventArtwork[] $eventArtworks | |
* @property User $user | |
*/ | |
class EventForm extends \yii\db\ActiveRecord | |
{ | |
public $upload; | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function tableName() | |
{ | |
return 'event_form'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['event_id', 'name', 'surname', 'year_of_birth', 'phone_number', 'member_of_lms'], 'required'], | |
[['event_id', 'user_id', 'year_of_birth', 'phone_number', 'member_of_lms', 'created_at', 'updated_at'], 'integer'], | |
[['code_id'], 'string', 'max' => 3], | |
[['name', 'surname', 'email'], 'string', 'max' => 255], | |
[['code_id'], 'unique'], | |
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']], | |
[['event_id'], 'exist', 'skipOnError' => true, 'targetClass' => Event::class, 'targetAttribute' => ['event_id' => 'id']], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => Yii::t('app', 'ID'), | |
'event_id' => Yii::t('app', 'Event ID'), | |
'user_id' => Yii::t('app', 'User ID'), | |
'code_id' => Yii::t('app', 'Code ID'), | |
'name' => Yii::t('app', 'Name'), | |
'surname' => Yii::t('app', 'Surname'), | |
'year_of_birth' => Yii::t('app', 'Year Of Birth'), | |
'email' => Yii::t('app', 'Email'), | |
'phone_number' => Yii::t('app', 'Phone Number'), | |
'member_of_lms' => Yii::t('app', 'Member Of LMS'), | |
'created_at' => Yii::t('app', 'Created At'), | |
'updated_at' => Yii::t('app', 'Updated At'), | |
]; | |
} | |
/** | |
* Gets query for [[Event]]. | |
* | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getEvent() | |
{ | |
return $this->hasOne(Event::class, ['id' => 'event_id']); | |
} | |
/** | |
* Gets query for [[EventArtworks]]. | |
* | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getEventArtworks() | |
{ | |
return $this->hasMany(EventArtwork::class, ['event_form_id' => 'id']); | |
} | |
/** | |
* Gets query for [[User]]. | |
* | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getUser() | |
{ | |
return $this->hasOne(User::class, ['id' => 'user_id']); | |
} | |
public function beforeSave($insert) { | |
if (parent::beforeSave($insert)) { | |
if ($this->isNewRecord) { | |
$this->code_id = strtoupper(Yii::$app->security->generateRandomString(3)); | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\controllers; | |
use app\models\ReceiptDetail; | |
use Yii; | |
use app\models\EventForm; | |
use app\models\EventArtwork; | |
use app\models\EventFormSearch; | |
use yii\helpers\Url; | |
use yii\base\Model; | |
use yii\data\ActiveDataProvider; | |
use yii\web\Controller; | |
use yii\web\NotFoundHttpException; | |
use yii\filters\VerbFilter; | |
use yii\web\UploadedFile; | |
use yii\imagine\Image; | |
/** | |
* EventFormController implements the CRUD actions for EventForm model. | |
*/ | |
class EventFormController extends Controller | |
{ | |
/** | |
* @inheritDoc | |
*/ | |
public function behaviors() | |
{ | |
return array_merge( | |
parent::behaviors(), | |
[ | |
'verbs' => [ | |
'class' => VerbFilter::className(), | |
'actions' => [ | |
'delete' => ['POST'], | |
], | |
], | |
] | |
); | |
} | |
/** | |
* Lists all EventForm models. | |
* | |
* @return string | |
*/ | |
public function actionIndex() | |
{ | |
$searchModel = new EventFormSearch(); | |
$dataProvider = $searchModel->search($this->request->queryParams); | |
return $this->render('index', [ | |
'searchModel' => $searchModel, | |
'dataProvider' => $dataProvider, | |
]); | |
} | |
/** | |
* Displays a single EventForm 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 EventForm model. | |
* If creation is successful, the browser will be redirected to the 'view' page. | |
* @return string|\yii\web\Response | |
*/ | |
public function actionCreate() | |
{ | |
$model = new EventForm(); | |
$modelArtworks = []; | |
$formArtworks = Yii::$app->request->post('EventArtwork', []); | |
foreach ($formArtworks as $i => $formArtwork) { | |
$modelArtwork = new EventArtwork(['scenario' => EventArtwork::SCENARIO_BATCH_UPDATE]); | |
$modelArtwork->setAttributes($formArtwork); | |
$modelArtworks[] = $modelArtwork; | |
} | |
//handling if the addRow button has been pressed | |
if (Yii::$app->request->post('addRow') == 'true') { | |
$model->load(Yii::$app->request->post()); | |
$modelArtworks[] = new EventArtwork(['scenario' => EventArtwork::SCENARIO_BATCH_UPDATE]); | |
return $this->render('create', [ | |
'model' => $model, | |
'modelArtworks' => $modelArtworks | |
]); | |
} | |
if ($model->load(Yii::$app->request->post())) { | |
if (Model::validateMultiple($modelArtworks) && $model->validate()) { | |
$model->save(); | |
foreach($modelArtworks as $modelArtwork) { | |
$modelArtwork->event_form_id = $model->id; | |
$modelArtwork->save(); | |
} | |
return $this->redirect(['view', 'id' => $model->id]); | |
} | |
} | |
return $this->render('create', [ | |
'model' => $model, | |
'modelArtworks' => $modelArtworks | |
]); | |
} | |
/** | |
* Updates an existing EventForm 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); | |
$modelArtworks = $model->eventArtworks; | |
$formArtworks = Yii::$app->request->post('EventArtwork', []); | |
foreach ($formArtworks as $i => $formArtwork) { | |
//loading the models if they are not new | |
if (isset($formArtwork['id']) && isset($formArtwork['updateType']) && $formArtwork['updateType'] != EventArtwork::UPDATE_TYPE_CREATE) { | |
//making sure that it is actually a child of the main model | |
$modelArtwork = EventArtwork::findOne(['id' => $formArtwork['id'], 'event_form_id' => $model->id]); | |
$modelArtwork->setScenario(EventArtwork::SCENARIO_BATCH_UPDATE); | |
$modelArtwork->setAttributes($formArtwork); | |
$modelArtworks[$i] = $modelArtwork; | |
//validate here if the modelDetail loaded is valid, and if it can be updated or deleted | |
} else { | |
$modelArtwork = new EventArtwork(['scenario' => EventArtwork::SCENARIO_BATCH_UPDATE]); | |
$modelArtwork->setAttributes($formArtwork); | |
$modelArtworks[] = $modelArtwork; | |
$modelArtwork->handlePostSave($modelArtwork); | |
} | |
} | |
//handling if the addRow button has been pressed | |
if (Yii::$app->request->post('addRow') == 'true') { | |
$modelArtworks[] = new EventArtwork(['scenario' => EventArtwork::SCENARIO_BATCH_UPDATE]); | |
return $this->render('update', [ | |
'model' => $model, | |
'modelArtworks' => $modelArtworks | |
]); | |
} | |
if ($model->load(Yii::$app->request->post())) { | |
if (Model::validateMultiple($modelArtworks) && $model->validate()) { | |
$model->save(); | |
foreach($modelArtworks as $modelArtwork) { | |
//details that has been flagged for deletion will be deleted | |
if ($modelArtwork->updateType == EventArtwork::UPDATE_TYPE_DELETE) { | |
$modelArtwork->delete(); | |
} else { | |
//new or updated records go here | |
$modelArtwork->event_form_id = $model->id; | |
$modelArtwork->save(); | |
} | |
} | |
return $this->redirect(['view', 'id' => $model->id]); | |
} | |
} | |
return $this->render('update', [ | |
'model' => $model, | |
'modelArtworks' => $modelArtworks | |
]); | |
} | |
/** | |
* Deletes an existing EventForm 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) | |
{ | |
$model = $this->findModel($id); | |
foreach ($model->eventArtworks as $modelArtwork) { | |
$modelArtwork->delete(); | |
} | |
$model->delete(); | |
return $this->redirect(['index']); | |
} | |
protected function handlePostSave(EventArtwork $model) | |
{ | |
if ($model->load(Yii::$app->request->post())) { | |
$model->upload = UploadedFile::getInstance($model, 'upload'); | |
if ($model->validate()) { | |
if ($model->upload) { | |
if(!file_exists((Url::to('@web/images/')))) { | |
mkdir(Url::to('@web/images/'),0777,true); | |
} | |
$newName = 'lms-'.rand(1000,9999) . '.' .$model->upload->extension; | |
$filePath = Yii::getAlias('@web/images') .'/' . $newName; | |
//vieta, kurā jāielādē fails | |
if ($model->upload->saveAs($filePath)) { | |
$model->image = $newName; | |
Image::thumbnail('@web/images/'.$model->image, 640, 250)->save(Yii::getAlias('@web/images/r_'.$model->image), ['quality' => 100]) && | |
Image::thumbnail('@web/images/'.$model->image, 190, 75)->save(Yii::getAlias('@web/images//c_'.$model->image), ['quality' => 80]); | |
} | |
} | |
if ($model->save(false)) { | |
return $this->redirect(['view', 'id' => $model->id]); | |
} | |
} | |
} | |
} | |
/** | |
* Finds the EventForm 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 EventForm the loaded model | |
* @throws NotFoundHttpException if the model cannot be found | |
*/ | |
protected function findModel($id) | |
{ | |
if (($model = EventForm::findOne(['id' => $id])) !== null) { | |
return $model; | |
} | |
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment