Exemplo n.º 1
0
 public function actionCreate()
 {
     $model = new Import();
     if ($model->load(Yii::$app->request->post())) {
         $file_path = \yii\web\UploadedFile::getInstance($model, 'file_path');
         if (!empty($file_path)) {
             $model->file_path = \yii\web\UploadedFile::getInstance($model, 'file_path');
             $ext = FileHelper::getExtention($model->file_path);
             if (!empty($ext)) {
                 $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/');
                 $fileName = uniqid() . StringHelper::asUrl(Yii::$app->controller->module->id) . '.' . $ext;
                 $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
                 FileHelper::createDirectory($folder);
                 $model->file_path->saveAs($folder . $fileName);
                 $model->file_path = $fileDir . $fileName;
             }
         }
         if ($model->save()) {
             return $this->redirect(['update', 'id' => (string) $model->_id]);
         }
     }
     Yii::$app->view->title = Yii::t($this->module->id, 'Create');
     Yii::$app->view->params['breadcrumbs'][] = ['label' => Yii::t($this->module->id, 'Import'), 'url' => ['index']];
     Yii::$app->view->params['breadcrumbs'][] = Yii::$app->view->title;
     return $this->render('create', ['model' => $model]);
 }
Exemplo n.º 2
0
 public function actionIndex()
 {
     $id = Yii::$app->request->get('id');
     $modelName = Yii::$app->request->get('model');
     if (!empty($id)) {
         $model = $modelName::findOne($id);
         if ($model) {
             //                $model->gallery = \yii\web\UploadedFile::getInstance($model, 'gallery');
             $image = \yii\web\UploadedFile::getInstance($model, 'gallery');
             $ext = FileHelper::getExtention($image);
             if (!empty($ext)) {
                 // Thêm validate ext ____________________________
                 $file_id = uniqid();
                 $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/');
                 $fileName = $file_id . '.' . $ext;
                 $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
                 FileHelper::createDirectory($folder);
                 $image->saveAs($folder . $fileName);
                 $output = ["out" => ['file_id' => $file_id, 'item_id' => Yii::$app->controller->module->id]];
                 if (empty($model->gallery)) {
                     $model->gallery = $fileDir . $fileName;
                 } else {
                     $model->gallery .= ',' . $fileDir . $fileName;
                 }
                 $model->save();
                 echo json_encode($output);
             }
         }
     }
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     $attributes = array_keys($this->getAttributes());
     // ID
     if ($this->isNewRecord and empty($this->_id)) {
         $this->_id = uniqid();
     }
     // SEO
     if (in_array('title', $attributes) and in_array('seo_url', $attributes) and empty($this->seo_url)) {
         $this->seo_url = StringHelper::asUrl($this->title);
     }
     if (in_array('title', $attributes) and in_array('seo_title', $attributes) and empty($this->seo_title)) {
         $this->seo_title = $this->title;
     }
     if (in_array('description', $attributes) and in_array('seo_desc', $attributes) and empty($this->seo_desc)) {
         $this->seo_desc = $this->description;
     }
     // Upload image
     if (in_array('image', $attributes)) {
         $image = \yii\web\UploadedFile::getInstance($this, 'image');
         if (!empty($image)) {
             $this->image = \yii\web\UploadedFile::getInstance($this, 'image');
             $ext = FileHelper::getExtention($this->image);
             if (!empty($ext)) {
                 $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/');
                 if (property_exists($this, 'title')) {
                     $title = $this->title;
                 } elseif (property_exists($this, 'name')) {
                     $title = $this->name;
                 } else {
                     $title = uniqid();
                 }
                 $fileName = StringHelper::asUrl($title) . '.' . $ext;
                 $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
                 FileHelper::createDirectory($folder);
                 $this->image->saveAs($folder . $fileName);
                 $this->image = $fileDir . $fileName;
             }
         } else {
             $this->image = $this->image_old;
         }
     }
     // creator, editor and time
     $now = new MongoDate();
     if (in_array('update_time', $attributes) and empty($this->update_time)) {
         $this->update_time = $now;
     }
     if (in_array('editor', $attributes) and !ClientHelper::isCommandLine()) {
         $this->editor = Yii::$app->user->id;
     }
     if ($this->isNewRecord) {
         if (in_array('creator', $attributes) and !ClientHelper::isCommandLine()) {
             $this->creator = Yii::$app->user->id;
         }
         if (in_array('create_time', $attributes) and $this->create_time == null) {
             $this->create_time = $now;
         }
     }
     return parent::beforeSave($insert);
 }
Exemplo n.º 4
0
 public function beforeSave($insert)
 {
     $attributes = array_keys($this->getAttributes());
     // ID
     if ($this->isNewRecord and empty($this->_id)) {
         $this->_id = uniqid();
     }
     // SEO
     if (in_array('title', $attributes) and in_array('seo_url', $attributes) and empty($this->seo_url)) {
         $this->seo_url = StringHelper::asUrl($this->title);
     }
     if (in_array('title', $attributes) and in_array('seo_title', $attributes) and empty($this->seo_title)) {
         $this->seo_title = $this->title;
     }
     if (in_array('description', $attributes) and in_array('seo_desc', $attributes) and empty($this->seo_desc)) {
         $this->seo_desc = $this->description;
     }
     // Upload image
     $image = \yii\web\UploadedFile::getInstance($this, 'image');
     if (!empty($image)) {
         $this->image = \yii\web\UploadedFile::getInstance($this, 'image');
         $ext = FileHelper::getExtention($this->image);
         if (!empty($ext)) {
             $fileDir = LetHelper::getAvatarDir($this->primaryKey) . '/';
             $fileName = $this->primaryKey . '.jpg';
             //$fileName = $this->primaryKey . '.' . $ext;
             $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
             FileHelper::createDirectory($folder);
             $this->image->saveAs($folder . $fileName);
             $this->image = $fileDir . $fileName;
         }
     } else {
         $this->image = $this->image_old;
     }
     // creator, editor and time
     $now = new \MongoDate();
     if (in_array('update_time', $attributes) and empty($this->update_time)) {
         $this->update_time = $now;
     }
     if (in_array('editor', $attributes) and !ClientHelper::isCommandLine()) {
         $this->editor = Yii::$app->user->id;
     }
     if ($this->isNewRecord) {
         if (in_array('creator', $attributes) and !ClientHelper::isCommandLine()) {
             $this->creator = Yii::$app->user->id;
         }
         if (in_array('create_time', $attributes) and $this->create_time == null) {
             $this->create_time = $now;
         }
     }
     if (!empty($this->password)) {
         $this->password_hash = Yii::$app->getSecurity()->generatePasswordHash($this->password);
     }
     if ($this->isNewRecord) {
         $this->auth_key = bin2hex(Yii::$app->getSecurity()->generateRandomKey());
         $this->auth_key = substr($this->auth_key, 0, 32);
     }
     return true;
 }
Exemplo n.º 5
0
 protected function saveUploaded(UploadedFile $file)
 {
     $dir = $this->getPath(false, true);
     $file_name = $file->getBaseName();
     $file_ext = $file->getExtension();
     $filename = $file_name . '.' . $file_ext;
     $index = 0;
     while (file_exists($dir . $filename)) {
         $index++;
         $filename = $file_name . '-' . $index . '.' . $file_ext;
     }
     FileHelper::createDirectory($dir, 0777, true);
     $path = $dir . $filename;
     if ($file->saveAs($path)) {
         return [$filename, filesize($path)];
     }
     return false;
 }
Exemplo n.º 6
0
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     $attributes = array_keys($this->getAttributes());
     if (property_exists($this, 'category_id')) {
         $this->isCategory();
     }
     // Upload image
     if (in_array('image', $attributes)) {
         $this->image = \yii\web\UploadedFile::getInstance($this, 'image');
         $ext = FileHelper::getExtention($this->image);
         if (!empty($ext)) {
             $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/');
             $fileName = LetHelper::string_to_url($this->title) . '.' . $ext;
             $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
             FileHelper::createDirectory($folder);
             $this->image->saveAs($folder . $fileName);
             $this->image = $fileDir . $fileName;
         }
     }
     // creator, editor and time
     $now = date('Y-m-d H:i:s', time());
     if (in_array('update_time', $attributes)) {
         $this->update_time = $now;
     }
     if (in_array('editor', $attributes)) {
         $this->editor = Yii::$app->user->id;
     }
     if ($this->isNewRecord) {
         if (in_array('creator', $attributes)) {
             $this->creator = Yii::$app->user->id;
         }
         if (in_array('create_time', $attributes)) {
             $this->create_time = $now;
         }
     }
     return parent::beforeSave($insert);
 }