/** * Creates a new Uploadedforms model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $username = Yii::$app->user->identity->username; $users = User::find()->all(); $scholars = Scholar::find()->all(); $model = new Upload(); foreach ($users as $user) { foreach ($scholars as $scholar) { if ($user->username == $username && $user->id == $scholar->scholar_id) { $model->scholar_scholar_id = $scholar->scholar_id; if ($model->load(Yii::$app->request->post())) { $fileName = $model->upload_file_name . $model->scholar_scholar_id; $model->upload_form = UploadedFile::getInstance($model, 'upload_form'); if ($model->upload_form != null) { $model->upload_file_name->saveAs('uploads/' . $fileName . '.' . $model->upload_form->extension); $model->upload_form = 'uploads/' . $fileName . '.' . $model->upload_form->extension; } $model->save(); return $this->redirect(['index', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } } } } }
public function actionUpload() { if (Yii::$app->user->can('createResource')) { $form = new UploadForm(); if (Yii::$app->request->isPost) { $file = UploadedFile::getInstance($form, 'file'); if ($file->getHasError()) { Yii::$app->getSession()->setFlash('error', $file->error); return $this->render('index'); } $upload = Upload::findByUploadedFile($file); // TODO: if upload resource already exists for this file, edit existing firmware or create new, take ownership into account if (!$upload) { $upload = Upload::createFromUploadedFile($file); if ($upload->hasErrors()) { Yii::$app->getSession()->setFlash('error', $upload->getErrors()); return $this->render('index'); } } $firmware = Firmware::createFromUpload($upload); if ($firmware->hasErrors()) { Yii::$app->getSession()->setFlash('error', $firmware->getErrors()); return $this->render('index'); } return $this->render('edit', ['model' => FirmwareForm::createFromFirmware($firmware)]); } } else { Yii::$app->getSession()->setFlash('error', 'Not allowed.'); return $this->render('index'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Upload::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['upload_id' => $this->upload_id, 'scholar_scholar_id' => $this->scholar_scholar_id, 'scholar_school_school_id' => $this->scholar_school_school_id]); $query->andFilterWhere(['like', 'upload_form', $this->upload_form])->andFilterWhere(['like', 'upload_file_name', $this->upload_file_name]); return $dataProvider; }
public function actionDelete() { if (Yii::$app->user->can('deleteResource')) { if ($id = Yii::$app->request->get('id')) { $model = Upload::findOne(['id' => $id]); if ($model) { if ($model->delete()) { Yii::$app->getSession()->setFlash('success', 'Upload deleted.'); } else { Yii::$app->getSession()->setFlash('error', 'Failed to delete upload.'); } } } } else { Yii::$app->getSession()->setFlash('error', 'Not allowed.'); } return $this->redirect(['index']); }
/** * Updates an existing Partners model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $image = Upload::uploadImage($model); if (!$image) { $model->image = $model->oldAttributes['image']; } if ($model->save()) { // upload only if valid uploaded file instance found if ($image !== false) { $path = Upload::getImageFile($image); $image->saveAs($path); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model]); } }
/** * Finds the Upload model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $upload_id * @param integer $scholar_scholar_id * @param integer $scholar_school_school_id * @return Upload the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Upload::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing news model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $params = Yii::$app->request->post(); if (empty($params['updated_images'])) { $params['updated_images'] = array(); } if (empty($params['updated_files'])) { $params['updated_files'] = array(); } if ($model->load(Yii::$app->request->post())) { $image = Upload::uploadImage($model); $images = Upload::uploadImages($model); $attachments = Upload::uploadFiles($model); if (!$image) { $model->image = $model->oldAttributes['image']; } if (!$images) { //if(!empty($params['updated_images'])) { $model->images = json_encode($params['updated_images']); // } else { // $model->images = ''; // } } else { foreach ($images as $img) { $path = Upload::getImageFile($img); $image_array[] = $img->name; $img->saveAs($path); } $model->images = json_encode(array_merge($params['updated_images'], $image_array)); } if (!$attachments) { //if(!empty($params['updated_files'])) { $model->attachment = json_encode($params['updated_files']); // } else { // $model->attachment = ''; // } } else { foreach ($attachments as $attachment) { $path = Upload::getImageFile($attachment); $attachment_array[] = $attachment->name; $attachment->saveAs($path); } $model->attachment = json_encode(array_merge($params['updated_files'], $attachment_array)); } if ($model->save()) { // upload only if valid uploaded file instance found if ($image !== false) { $path = Upload::getImageFile($image); $image->saveAs($path); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model]); } }
/** * @return \yii\db\ActiveQuery */ public function getUpload() { return $this->hasOne(Upload::className(), ['id' => 'upload_id']); }
<?php use yii\grid\GridView; use yii\data\ActiveDataProvider; use common\models\Upload; /* @var $this yii\web\View */ $this->title = 'Uploads'; $this->params['breadcrumbs'][] = $this->title; $dataProvider = new ActiveDataProvider(['query' => Upload::find(), 'pagination' => ['pageSize' => 20]]); ?> <div class="box"> <div class="box-body"> <?php if (Yii::$app->user->can('listResources')) { echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'filename', 'filesize', 'mimetype', ['attribute' => 'created_by'], ['attribute' => 'created_at', 'format' => ['datetime', 'php:Y-m-d H:i:s']], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{update} {delete}']]]); } ?> </div> </div>
/** * @return \yii\db\ActiveQuery */ public function getUploads0() { return $this->hasMany(Upload::className(), ['scholar_school_school_id' => 'school_school_id']); }