Exemple #1
1
 function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         $file = UploadedFile::getInstance($this, 'image');
         if ($file && $file->error === UPLOAD_ERR_OK) {
             // Ищем, есть ли уже такой загруженный файл
             $file_model = Files::findOne(['hash' => md5_file($file->tempName)]);
             if ($file_model) {
                 $file_model->repeats++;
             } else {
                 $file_model = new Files();
                 $file_model->saveImageFile($file);
                 $file_model->repeats = 1;
             }
             $file_model->save();
             $this->file_id = $file_model->id;
         } else {
             $this->addError('image', 'Невозможно загрузить файл');
             return false;
         }
         return true;
     } else {
         return false;
     }
 }
Exemple #2
0
 /**
  * Удаляет прикрепленные к комментарию файл
  * @inheritdoc
  * @return bool
  */
 public function beforeDelete()
 {
     if (parent::beforeDelete()) {
         if ($image = Files::findOne($this->image_id)) {
             $image->delete();
         }
         return true;
     } else {
         return false;
     }
 }
Exemple #3
0
 public function send()
 {
     //Yii::import('application.extensions.smtpmail.PHPMailer');
     $mailer = new \yii\swiftmailer\Mailer(["transport" => ['class' => 'Swift_SmtpTransport', 'host' => \app\helpers\Linet3Helper::getSetting('company.mail.server'), 'username' => \app\helpers\Linet3Helper::getSetting('company.mail.user'), 'password' => \app\helpers\Linet3Helper::getSetting('company.mail.password'), 'port' => \app\helpers\Linet3Helper::getSetting('company.mail.port'), 'encryption' => \app\helpers\Linet3Helper::getSetting('company.mail.ssl') ? 'tls' : '']]);
     $mail = $mailer->compose('layouts/html', ['content' => $this->body]);
     //$mail->SetFrom($this->from);
     //echo $this->files;
     if ($this->files != '') {
         $file = Files::findOne($this->files);
         if ($file != null) {
             //echo $file->getFullPath().";;".$file->name;
             $mail->attach($file->getFullFilePath(), ["fileName" => $file->name]);
         }
     }
     $mail->setFrom(\app\helpers\Linet3Helper::getSetting('company.mail.address'))->setTo($this->to)->setSubject($this->subject);
     if ($this->cc != '') {
         $mail->setCc($this->cc);
     }
     if ($this->bcc != '') {
         $mail->setBcc($this->bcc);
     }
     //$mail->AddCC($this->cc); //.$this->cc
     //$mail->AddBcc($this->bcc);
     //$mail->
     //$mail->setHtmlBody($this->body);
     //$mail;
     if (!$mail->send()) {
         //echo "Mailer Error: " . $mail->ErrorInfo;
         throw new Exception(Yii::t('app', "Mailer Error: ") . $mail->ErrorInfo . $mail->Username);
     } else {
         $this->sent++;
         $this->save();
         if (!\app\helpers\Linet3Helper::isConsole()) {
             \Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Message sent!'));
         }
         //echo "Message sent!";
     }
     //*/
     //Yii::$app->end();
 }
Exemple #4
0
 public function actionDownload($id)
 {
     $id = (int) $id;
     $model = Files::findOne($id);
     if ($model === null) {
         throw new \yii\web\HttpException(404, 'The requested page does not exist.');
     }
     //$configPath=\app\helpers\Linet3Helper::getSetting("company.path");
     $file = $model->getFullFilePath();
     //????.$model->id;
     //echo $file.'end';
     return Yii::$app->getResponse()->sendFile($file, $model->name);
     //, file_get_contents()
 }
 protected function findImage($id)
 {
     if (($model = Files::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('Ресурс не найден');
     }
 }
 /**
  * Takes file link by id.
  * @param integer $id id file.
  * @return string link to file or false if error
  * @throws GoodException if field with this id doesn't exist
  */
 protected static function getFilePathById($id)
 {
     try {
         if (!($file = Files::findOne($id))) {
             throw new GoodException('Error', 'Doesn\'t exist field with this id...');
         }
         if (Yii::$app->user->identity['id'] != $file['id_user']) {
             throw new GoodException('Error', 'Wrong file id...');
         }
         return '../upload/' . Yii::$app->user->identity['login'] . '/' . $file['path'];
     } catch (Exception $e) {
         $e->getMessage();
     }
     return false;
 }
Exemple #7
0
 public function actionDownload($id)
 {
     $fileModel = Files::findOne(['id' => $id]);
     if (empty($fileModel)) {
         throw new NotFoundHttpException('File not found');
     }
     if (!empty($fileModel->password)) {
         if (Yii::$app->request->isPost) {
             $password = Yii::$app->request->post('password', '');
             if ($password == $fileModel->password) {
                 $path = $fileModel->path;
                 if (file_exists($path)) {
                     return Yii::$app->response->sendFile($path);
                 }
             } else {
                 throw new \yii\web\HttpException('400', 'Пароль неверный!');
             }
         } else {
             return $this->render('//site/file_password');
         }
     } else {
         $path = $fileModel->path;
         if (file_exists($path)) {
             return Yii::$app->response->sendFile($path);
         }
     }
 }
Exemple #8
0
 public function actionDownload($id)
 {
     $model = \app\models\Download::findOne(['id' => $id]);
     if ($model == null) {
     } else {
         //throw new \yii\web\HttpException(404, 'The requested page does not exist.');
         $comp = \app\models\Company::findOne($model->company_id);
         $comp->select($model->company_id);
         $id = (int) $model->file_id;
         $model = \app\models\Files::findOne($id);
         if ($model === null) {
             throw new \yii\web\HttpException(404, 'The requested page does not exist.');
         }
         $file = $model->getFullPath() . $model->id;
         return Yii::$app->getResponse()->sendFile($file, $model->name);
     }
 }
 /**
  * Finds the Files model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Files the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Files::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemple #10
0
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = Files::findOne($id);
     if ($model === null) {
         throw new \yii\web\HttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
Exemple #11
0
 public static function getLogo()
 {
     if (!\app\helpers\Linet3Helper::isConsole()) {
         return \Yii::$app->urlManager->createAbsoluteUrl("/site/download/" . Linet3Helper::getSetting('company.logo'));
         //$base . ;
     } else {
         //console
         $download = \app\models\Download::findOne(["id" => Linet3Helper::getSetting('company.logo')]);
         $id = (int) $download->file_id;
         $logo = \app\models\Files::findOne($id);
         return $logo->getFullFilePath();
     }
 }