Upload lesson documents

parent 37ac2dd5
<?php
namespace common\modules\school\models;
use Yii;
use common\modules\school\models\Lessons;
/**
* This is the model class for table "testings_questions_image".
*
* @property integer $id
* @property integer $question_id
* @property string $filename
*/
class LessonImage extends \common\components\ActiveRecordModel
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'school_lessons_image';
}
public function name()
{
return 'Документы для уроков';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['lesson_id', 'filename'], 'required'],
[['lesson_id'], 'integer'],
[['filename'], 'string', 'max' => 50],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'lesson_id' => 'Урок',
'filename' => 'Документ',
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
];
}
public function getLesson()
{
return $this->hasOne(Lessons::className(), ['id' => 'lesson_id']);
}
public function getUrl()
{
return Yii::$app->params['frontUrl'] . Lessons::IMAGES_FOLDER . $this->filename;
}
}
......@@ -17,6 +17,9 @@ class Lessons extends \common\components\ActiveRecordModel
{
const PAGE_SIZE = 10;
const IMAGES_FOLDER = '/uploads/lessons_docs/';
public $filesUpload;
/**
* @inheritdoc
*/
......@@ -41,6 +44,7 @@ class Lessons extends \common\components\ActiveRecordModel
[['video_id'], 'string', 'max' => 100],
[['course_id', 'number'], 'integer'],
[['text'], 'safe'],
[['filesUpload'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, rar, ai, ppt, doc, docx'],
];
}
......@@ -99,4 +103,57 @@ class Lessons extends \common\components\ActiveRecordModel
return false;
}
}
public function getFiles()
{
return $this->hasMany(LessonImage::className(), ['lesson_id' => 'id']);
}
public function getPath()
{
return Yii::getAlias('@frontend/web') . self::IMAGES_FOLDER;
}
public function upload()
{
if ($this->validate())
{
if(!file_exists($this->getPath()))
{
mkdir($this->getPath(), 0777, true);
}
foreach ($this->filesUpload as $file)
{
$filename = date('dmYHis-') . uniqid() . '.' . $file->extension;
$file->saveAs($this->getPath() . $filename);
$image = new LessonImage;
$image->lesson_id = $this->id;
$image->filename = $filename;
$image->save();
}
return true;
}
else
{
return false;
}
}
public function deleteFiles()
{
if($this->files)
{
foreach ($this->files as $file)
{
if(file_exists($this->getPath() . $file->filename))
{
unlink($this->getPath() . $file->filename);
}
$file->delete();
}
}
}
}
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160217_095148_create_school_lessons_image extends Migration
{
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql')
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
}
// Структура таблицы `school_lessons_image`
$this->createTable('school_lessons_image', [
'id' => Schema::TYPE_PK,
'filename' => Schema::TYPE_STRING . '(50) NOT NULL',
'lesson_id' => Schema::TYPE_INTEGER . '(11) NOT NULL',
], $tableOptions);
$this->createIndex('FK_school_lessons_image_lessons', 'school_lessons_image', 'lesson_id');
}
public function safeDown()
{
$this->dropTable('school_lessons_image');
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment