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 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.º 3
0
 public function buildUrl($id = NULL, $title = NULL)
 {
     $attributes = array_keys($this->getAttributes());
     if (empty($id)) {
         $id = $this->primaryKey;
     }
     $routes = ['/category/frontend/default/list', 'id' => $id, 'module' => $this->module];
     $slug = '';
     if (!empty($title)) {
         $slug = $title;
     } elseif (in_array('seo_title', $attributes) and !empty($this->seo_title)) {
         $slug = $this->seo_title;
     } elseif (in_array('name', $attributes) and !empty($this->name)) {
         $slug = $this->name;
     }
     $routes['slug'] = StringHelper::asUrl($slug);
     return Url::to($routes);
 }
Exemplo n.º 4
0
 public function generationUrl($id = NULL, $title = NULL)
 {
     $attributes = array_keys($this->getAttributes());
     if (empty($id)) {
         $id = $this->primaryKey;
     }
     // Create alias
     if (!empty($title)) {
         $alias = $title;
     } elseif (in_array('seo_title', $attributes) and !empty($this->seo_title)) {
         $alias = $this->seo_title;
     } elseif (in_array('title', $attributes) and !empty($this->title)) {
         $alias = $this->title;
     } else {
         $alias = '';
     }
     $alias = StringHelper::asUrl($alias);
     if (empty($alias)) {
         $alias = '/' . $this->moduleName . '-' . $id;
     } else {
         $alias = '/' . $alias . '-' . $this->moduleName . '-' . $id;
     }
     return $this->url = Url::to([$alias]);
 }