Commit ab6d9045406e5d61ea9238cb1e39079c37457cd4

Authored by Administrator
1 parent 06d508f3

add Vitaliy's widgets

common/config/bootstrap.php
... ... @@ -4,3 +4,4 @@ Yii::setAlias('frontend', dirname(dirname(__DIR__)) . '/frontend');
4 4 Yii::setAlias('backend', dirname(dirname(__DIR__)) . '/backend');
5 5 Yii::setAlias('console', dirname(dirname(__DIR__)) . '/console');
6 6 Yii::setAlias('saveImageDir', '@frontend/web/images/upload/');
  7 +Yii::setAlias('storage', dirname(dirname(__DIR__)) . '/storage');
... ...
common/config/main.php
... ... @@ -34,6 +34,10 @@ return [
34 34 'blog' => [
35 35 'class' => 'common\modules\blog\Module',
36 36 ],
  37 + 'file' => [
  38 + 'class' => 'common\modules\file\Module',
  39 + ],
  40 +
37 41 ],
38 42 'bootstrap' => [
39 43 'options'
... ...
common/modules/file/controller/DefaultController.php deleted
1   -<?php
2   -/**
3   - * Created by PhpStorm.
4   - * User: Cibermag
5   - * Date: 31.08.2015
6   - * Time: 9:58
7   - */
8   -namespace common\modules\file\controllers;
9   -use Yii;
10   -use yii\web\UploadedFile;
11   -use common\models\ImageSizerForm;
12   -use yii\web\Controller;
13   -use Imagine\Gd\Imagine;
14   -use Imagine\Image\Box;
15   -use yii\imagine\Image;
16   -
17   -class DefaultController extends Controller {
18   -
19   - public function isBigger($width,$height,$w,$h)
20   - {
21   - if($width>$w){
22   - return true;
23   - }else if($height >$h) {
24   - return true;
25   - }
26   - return false;
27   - }
28   -
29   - public function actionIndex(){
30   - die('hello');
31   - }
32   -
33   - private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
34   - $img = Image::getImagine()->open(Yii::getAlias($imageAlias));
35   -
36   - $size = $img->getSize();
37   -
38   - $width = $size->getWidth();
39   - $height = $size->getHeight();
40   -
41   - $e_width = $w/$h;
42   - $e_height = $h/$w;
43   -
44   - $e1_width = $width/$height;
45   - $e1_height = $height/$width;
46   -
47   -
48   - if($this->isBigger($width,$height,$w,$h)){
49   - if($e_width<$e1_width){
50   -
51   - $new_width = $width*($e_width/$e1_width);
52   -
53   - $y = 0;
54   - $x = $width/ 2-($new_width/2);
55   - $width = $new_width;
56   -
57   - }else {
58   -
59   - $new_height = $height*($e_height/$e1_height);
60   - $x = 0;
61   - $y = $height/2-($new_height/2);
62   - $height = $new_height;
63   - }
64   -
65   -
66   - } else {
67   - $img->save($imageAliasSave, array('flatten' => false));
68   - return true;
69   - }
70   -
71   -
72   - Image::crop($imageAlias, $width, $height,[$x,$y])
73   - ->save(Yii::getAlias($imageAliasSave), ['quality' =>
74   - 100]);
75   -
76   -
77   - $imagine = new Imagine();
78   - $imagine->open($imageAliasSave)
79   - ->resize(new Box($w, $h))
80   - ->save($imageAliasSave, array('flatten' => false));
81   -
82   -
83   -
84   - }
85   -
86   - private function deleteImages($old_img){
87   -
88   - if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){
89   -
90   -// $rootDir = explode("/", $old_img);
91   -//
92   -// $row = $_SERVER['DOCUMENT_ROOT'].'/'.$rootDir[1].'/'.$rootDir[2].'/';
93   -//
94   -// $allFiles = scandir($row);
95   -//
96   -// $allFiles = array_slice($allFiles, 2);
97   -//
98   -// foreach($allFiles as $oldFile){
99   -//
100   -// unlink($row.$oldFile);
101   -//
102   -// }
103   -
104   - }
105   - }
106   -
107   - public function actionDeleteImage(){
108   -
109   - $request = Yii::$app->request->post();
110   -
111   - if($request){
112   - if ($request['old_img']) {
113   - $this->deleteImages($request['old_img']);
114   - }
115   - if(isset($request['action']) && $request['action']=='save'){
116   - $object = str_replace('-', '\\',$request['model']);
117   - $model = new $object;
118   - $model = $model->findOne($request['id']);
119   - $model->$request['field'] = $request['new_url'];
120   -// print_r($model->validate());
121   -// print_r($model->getErrors());
122   -// die(var_dump($model->save()));
123   - $model->save();
124   - }
125   - }
126   -
127   - }
128   -
129   -
130   - public function actionDownloadPhoto()
131   - {
132   -
133   - $model = new ImageSizerForm();
134   -
135   - $request = Yii::$app->request->post();
136   - if ($request) {
137   -
138   - $model->file = UploadedFile::getInstance($model, 'file');
139   -
140   - $md5_file = md5_file($model->file->tempName);
141   -
142   - $imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
143   -
144   - $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension);
145   -
146   - if(!is_dir($imgDir)) {
147   - mkdir($imgDir, 0755, true);
148   - }
149   -
150   - $model->file->saveAs($imageOrigAlias);
151   -
152   - if($request['width'] && $request['height']){
153   -
154   - $imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension);
155   -
156   - $imageLink = '/storage/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension;
157   -
158   - $this->resizeImg($request['width'], $request['height'], $imageOrigAlias,$imageAlias);
159   -
160   - } else {
161   -
162   - $imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension;
163   -
164   - }
165   -
166   -
167   - if($model->multi){
168   - $view = $this->renderPartial('@app/components/views/_gallery_item', [
169   - 'item' => ['image'=>$imageLink],
170   - ]);
171   -
172   - return json_encode(['link'=>$imageLink, 'view' =>$view]);
173   -
174   -
175   - } else {
176   - return json_encode(['link'=>$imageLink]);
177   - }
178   -
179   -
180   - }
181   - }
182   -
183   -
184   - public function getex($filename) {
185   - return end(explode(".", $filename));
186   - }
187   -
188   -
189   - public function actionImagesUpload(){
190   - if($_FILES['upload'])
191   - {
192   - if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) )
193   - {
194   - $message = "Вы не выбрали файл";
195   - }
196   - else if ($_FILES['upload']["size"] == 0 OR $_FILES['upload']["size"] > 2050000)
197   - {
198   - $message = "Размер файла не соответствует нормам";
199   - }
200   - else if (($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png") AND ($_FILES['upload']['type'] != 'image/gif'))
201   - {
202   - $message = "Допускается загрузка только картинок JPG и PNG.";
203   - }
204   - else if (!is_uploaded_file($_FILES['upload']["tmp_name"]))
205   - {
206   - $message = "Что-то пошло не так. Попытайтесь загрузить файл ещё раз.";
207   - }
208   - else{
209   - $name =rand(1, 1000).md5($_FILES['upload']['name']).'.'.$this->getex($_FILES['upload']['name']);
210   - move_uploaded_file($_FILES['upload']['tmp_name'], "../../storage/images/".$name);
211   - $full_path = '/storage/images/'.$name;
212   - $message = "Файл ".$_FILES['upload']['name']." загружен";
213   - $size=@getimagesize('images/'.$name);
214   -
215   - }
216   - $callback = $_REQUEST['CKEditorFuncNum'];
217   - echo '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction("'.$callback.'", "'.$full_path.'", "'.$message.'" );</script>';
218   - }
219   - }
220   -
221   -
222   -}
223 0 \ No newline at end of file
frontend/controllers/FileController.php renamed to common/modules/file/controllers/UploaderController.php
... ... @@ -5,7 +5,7 @@
5 5 * Date: 31.08.2015
6 6 * Time: 9:58
7 7 */
8   -namespace frontend\controllers;
  8 +namespace common\modules\file\controllers;
9 9 use Yii;
10 10 use yii\web\UploadedFile;
11 11 use common\models\ImageSizerForm;
... ... @@ -14,7 +14,7 @@ use Imagine\Gd\Imagine;
14 14 use Imagine\Image\Box;
15 15 use yii\imagine\Image;
16 16  
17   -class FileController extends Controller {
  17 +class UploaderController extends Controller {
18 18  
19 19 public function isBigger($width,$height,$w,$h)
20 20 {
... ... @@ -27,6 +27,14 @@ class FileController extends Controller {
27 27 }
28 28  
29 29  
  30 + private function getUserPath(){
  31 + if(isset(Yii::$app->user->id)){
  32 + return 'user_'.Yii::$app->user->id;
  33 + }else {
  34 + return 'guest';
  35 + }
  36 + }
  37 +
30 38 private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
31 39 $img = Image::getImagine()->open(Yii::getAlias($imageAlias));
32 40  
... ... @@ -134,9 +142,9 @@ class FileController extends Controller {
134 142  
135 143 $model->file = UploadedFile::getInstance($model, 'file');
136 144  
137   - $md5_file = md5_file($model->file->tempName);
  145 + $md5_file = md5_file($model->file->tempName).rand(1, 1000);
138 146  
139   - $imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
  147 + $imgDir = Yii::getAlias('@storage/'.$this->getUserPath().'/'.$md5_file.'/');
140 148  
141 149 $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension);
142 150  
... ... @@ -150,13 +158,13 @@ class FileController extends Controller {
150 158  
151 159 $imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension);
152 160  
153   - $imageLink = '/storage/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension;
  161 + $imageLink = '/storage/'.$this->getUserPath().'/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension;
154 162  
155 163 $this->resizeImg($request['width'], $request['height'], $imageOrigAlias,$imageAlias);
156 164  
157 165 } else {
158 166  
159   - $imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension;
  167 + $imageLink = '/storage/'.$this->getUserPath().'/'.$md5_file.'/'.'original'.'.'.$model->file->extension;
160 168  
161 169 }
162 170  
... ... @@ -184,6 +192,7 @@ class FileController extends Controller {
184 192  
185 193  
186 194 public function actionImagesUpload(){
  195 +
187 196 if($_FILES['upload'])
188 197 {
189 198 if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) )
... ... @@ -203,11 +212,21 @@ class FileController extends Controller {
203 212 $message = "Что-то пошло не так. Попытайтесь загрузить файл ещё раз.";
204 213 }
205 214 else{
206   - $name =rand(1, 1000).md5($_FILES['upload']['name']).'.'.$this->getex($_FILES['upload']['name']);
207   - move_uploaded_file($_FILES['upload']['tmp_name'], "../../storage/images/".$name);
208   - $full_path = '/storage/images/'.$name;
  215 + $name =$_FILES['upload']['name'].'.'.$this->getex($_FILES['upload']['name']);
  216 +
  217 + $path = "../../storage/".$this->getUserPath()."/images/";
  218 + if(!is_dir($path)) {
  219 + mkdir($path, 0755, true);
  220 + }
  221 +
  222 +
  223 +
  224 + move_uploaded_file($_FILES['upload']['tmp_name'], $path.$name);
  225 +
  226 + $full_path = '/storage/'.$this->getUserPath().'/images/'.$name;
  227 +
209 228 $message = "Файл ".$_FILES['upload']['name']." загружен";
210   - $size=@getimagesize('images/'.$name);
  229 +
211 230  
212 231 }
213 232 $callback = $_REQUEST['CKEditorFuncNum'];
... ...
common/widgets/views/image_sizer.php
... ... @@ -16,7 +16,7 @@ $id = $model::tableName().&#39;_id&#39;;
16 16 <i class="glyphicon glyphicon-plus"></i>
17 17 <span><?=$name?></span>
18 18  
19   - <?= Html::activeFileInput( new \common\models\ImageSizerForm(),'file',['id'=>$field, 'data-url'=>"/file/download-photo" ]);?>
  19 + <?= Html::activeFileInput( new \common\models\ImageSizerForm(),'file',['id'=>$field, 'data-url'=>Yii::$app->getUrlManager()->createUrl('file/uploader/download-photo')]);?>
20 20 </span>
21 21  
22 22 <?= Html::activeHiddenInput( $model,$field,['id' => "{$field}_picture_link"]) ?>
... ... @@ -83,7 +83,7 @@ $id = $model::tableName().&#39;_id&#39;;
83 83 <i class="glyphicon glyphicon-plus"></i>
84 84 <span><?=$name?></span>
85 85  
86   - <?= Html::activeFileInput( new \common\models\ImageSizerForm(),'file',['id'=>$field, 'data-url'=>"/file/download-photo", 'multiple'=> 'multiple' ]);?>
  86 + <?= Html::activeFileInput( new \common\models\ImageSizerForm(),'file',['id'=>$field, 'data-url'=>Yii::$app->getUrlManager()->createUrl('file/uploader/download-photo'), 'multiple'=> 'multiple' ]);?>
87 87 </span>
88 88  
89 89 <?= Html::activeHiddenInput( $model,$field,['id' => "{$field}_picture_link"]) ?>
... ...
console/migrations/m160203_082111_jobs.php 100644 → 100755
console/migrations/m160203_133308_add_field_specialization.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\db\Migration;
  4 +
  5 +class m160203_133308_add_field_specialization extends Migration
  6 +{
  7 + public function up()
  8 + {
  9 + $this->addColumn('specialization', 'image', $this->string(255));
  10 + }
  11 +
  12 + public function down()
  13 + {
  14 + $this->dropColumn('option', 'image');
  15 + }
  16 +}
... ...
frontend/views/accounts/_form.php
... ... @@ -46,7 +46,7 @@ use mihaildev\elfinder\ElFinder;
46 46 'editorOptions' => ElFinder::ckeditorOptions('elfinder',[
47 47 'preset' => 'full', //разработанны стандартные настройки basic, standard, full данную возможность не обязательно использовать
48 48 'inline' => false, //по умолчанию false]),
49   - 'filebrowserUploadUrl'=>Yii::$app->getUrlManager()->createUrl('file/images-upload')
  49 + 'filebrowserUploadUrl'=>Yii::$app->getUrlManager()->createUrl('file/uploader/images-upload')
50 50 ]
51 51 )
52 52 ]); ?>
... ...