index.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. use yii\helpers\Html;
  3. use yii\helpers\Url;
  4. //use yii\grid\ActionColumn;
  5. //use yii\widgets\ListView;
  6. //use yii\grid\GridView;
  7. use yii\widgets\Pjax;
  8. /** @var yii\web\View $this */
  9. /** @var yii\data\ActiveDataProvider $dataProvider */
  10. use dosamigos\fileupload\FileUpload;
  11. use yii\web\View;
  12. $this->title = 'Документы';
  13. $this->params['breadcrumbs'][] = ['label' => 'Документы', 'url' => ['index'] ];
  14. $ucat = Yii::$app->request->get('cat', '');
  15. if( $ucat ){
  16. $this->params['breadcrumbs'][] = ['label' => $ucat, 'url' => ['index', 'cat'=>$ucat]];
  17. }
  18. $croute = '/manager/docs';
  19. $rootcat = $model->GetRootCat();
  20. $curcat = $model->GetCat();
  21. $cat = yii\helpers\FileHelper::findDirectories($curcat);
  22. $files = yii\helpers\FileHelper::findFiles($curcat,['recursive'=>false]);
  23. $ckey = 'dosc_cat_'.md5($ucat);
  24. Yii::$app->cache->delete($ckey);
  25. $docs = Yii::$app->cache->getOrSet( $ckey, function () use ($model, $ucat) {
  26. return $model->find()->where(['cat'=>$ucat?$ucat:null])->all();
  27. },2 );
  28. //var_dump($docs);
  29. $docsf = [];
  30. foreach($docs as $key => $item){
  31. $docsf[$item->uid] = $key;
  32. }
  33. //$docsf = array_flip( yii\helpers\ArrayHelper::getColumn($docs, "uid") );
  34. ?>
  35. <style>
  36. .docs-index {
  37. font-size:1rem;
  38. }
  39. </style>
  40. <?php if (Yii::$app->session->hasFlash('success')): ?>
  41. <div class="alert alert-success alert-dismissible">
  42. <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
  43. <h5><i class="icon fas fa-check"></i> info!</h5>
  44. <?=Yii::$app->session->getFlash('success');?>
  45. </div>
  46. <?php endif; ?>
  47. <div class="docs-index">
  48. <p>
  49. <?= Html::a('Создать Docs', ['create', 'cat' => $ucat], ['class' => 'btn btn-success']) ?>
  50. <?= Html::a('Создать Папку', ['catcreate'], ['class' => 'btn btn-success']) ?>
  51. <?= Html::a('Массовая загрузка', ['uploads', 'cat'=>$ucat ], ['class' => 'btn btn-success']) ?><br>
  52. </p>
  53. <div class="card" id="conteiner">
  54. <div class="card-header">
  55. <h3 class="card-title"><a href="/<?=Yii::$app->controller->route?>"><i class="icon fas fa-home mx-2"></i></a> <a href="/<?=Yii::$app->controller->route?>?cat=<?=$ucat?>"><i class="fas fa-1x fa-sync-alt mx-2"></i></a> Папка <?=$ucat?></h3><i class="fas fa-upload mx-3"></i>
  56. <div class="card-tools">
  57. <button type="button" class="btn btn-tool" data-card-widget="collapse">
  58. <i class="fas fa-minus"></i>
  59. </button>
  60. </div>
  61. </div>
  62. <div class="card-body p-0">
  63. <ul class="nav nav-pills flex-column" id="filesbox">
  64. <?
  65. foreach($cat as $item ){
  66. $file = str_replace( $rootcat.'/', '', $item );
  67. ?>
  68. <li class="nav-item">
  69. <a href="?cat=<?=$file?>" class="nav-link">
  70. <i class="fas fa-inbox"></i> <?=$file?>
  71. </a>
  72. </li>
  73. <?
  74. }
  75. foreach($files as $item ){
  76. $ext = pathinfo($item, PATHINFO_EXTENSION);
  77. // $bsaename = str_replace( '.'.$ext, '', pathinfo($item, PATHINFO_BASENAME));
  78. $bsaename = str_replace( '.', '_', pathinfo($item, PATHINFO_BASENAME));
  79. if( $ext == 'webp' ) continue;
  80. $file = str_replace( $rootcat.'/', '', $item );
  81. $ext = pathinfo($item, PATHINFO_EXTENSION);
  82. $webp = str_replace( '.'.$ext, '.webp', $item );
  83. if( !file_exists($webp) && $ext != 'webp' && ( $ext == 'pdf' || $ext == 'jpg' || $ext == 'png' || $ext == 'jpeg' || $ext == 'svg' )){
  84. exec('convert -thumbnail x200 -background white -alpha remove '.$item.'[0] '.$webp);
  85. }
  86. $url = $model::$story_dir. DIRECTORY_SEPARATOR .$file;
  87. ?>
  88. <li class="nav-item">
  89. <a href="<?=$url?>" class="nav-link" target="_blank" style="display: inline-block;">
  90. <?
  91. if( file_exists($webp) ){
  92. ?>
  93. <img src="<?=$model::$story_dir. DIRECTORY_SEPARATOR .str_replace( '.'.$ext, '.webp', $file );?>" style="max-height:60px;max-width: 90px;">
  94. <?
  95. }else{
  96. ?>
  97. <i class="far fa-file-alt"></i>
  98. <?
  99. }
  100. $key = pathinfo($item, PATHINFO_BASENAME);
  101. // $p = $model->find()->where(['uid'=>$key])->one();
  102. $p = isset($docsf[$key])?$docs[$docsf[$key]]:false;
  103. if( $p ){
  104. $title = $p->title;
  105. $id = $p->id;
  106. unset($docsf[$key]);
  107. }else{
  108. $title = $file;
  109. $id = $key."&cat=$ucat";
  110. }
  111. ?>
  112. <span id="<?=$bsaename?>"><?=$title?></span>
  113. </a><button type="button" class="btn btn-default btn-sm"><i class="fas fa-edit" onclick="edit('<?=$bsaename?>')"></i></button> <a href="<?=$croute?>/delete?id=<?=$id?>&cat=<?=$ucat?>"><button type="button" class="btn btn-default btn-sm"><i class="fas fa-trash"></i></button></a>
  114. </li>
  115. <?
  116. }
  117. ?>
  118. </li>
  119. <?
  120. foreach($docsf as $item ){
  121. $p = $docs[$item];
  122. $id = $p->id;
  123. ?>
  124. <li class="nav-item">
  125. <a href="<?=$croute?>/view?id=<?=$p->id?>" class="nav-link" style="display: inline-block;">
  126. <i class="fas fa-file"></i> <?=$p->title?>
  127. </a> <a href="<?=$croute?>/update?id=<?=$id?>"><button type="button" class="btn btn-default btn-sm"><i class="fas fa-edit"></i></button></a> <a href="<?=$croute?>/delete?id=<?=$id?>&cat=<?=$ucat?>"><button type="button" class="btn btn-default btn-sm"><i class="fas fa-trash"></i></button></a>
  128. </li>
  129. <?
  130. }
  131. ?>
  132. </ul>
  133. </div>
  134. </div>
  135. </div>
  136. <div class="modal" tabindex="-1" id="modal-lg" aria-modal="true" role="dialog" aria-hidden="true">
  137. <div class="modal-dialog modal-lg">
  138. <div class="modal-content">
  139. <div class="modal-header">
  140. <h4 class="modal-title">Заголовок файла</h4>
  141. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  142. <span aria-hidden="true">×</span>
  143. </button>
  144. </div>
  145. <div class="modal-body">
  146. <input type="text" id="titlev" value="" class="form-control form-control-lg">
  147. <input type="hidden" id="uid" value="">
  148. </div>
  149. <div class="modal-footer justify-content-between">
  150. <button type="button" class="btn btn-default" data-dismiss="modal">Отменить</button>
  151. <button type="button" class="btn btn-primary" onclick="save()">Сохранить</button>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <div class="info-box bg-info" style="width: 300px; position: fixed; bottom: 0px; right: 0px;display:none" id="templ1">
  157. <span class="info-box-icon bg-info"><i class="far fa-copy"></i></span>
  158. <div class="info-box-content">
  159. <span class="info-box-text">Uploads</span>
  160. <div class="progress">
  161. <div class="progress-bar" style="width: 0%"></div>
  162. </div>
  163. </div>
  164. </div>
  165. <script>
  166. function edit(id){
  167. val = $('#'+id).html();
  168. $('#titlev').val(val);
  169. $('#uid').val(id);
  170. $('#modal-lg').modal('show');
  171. }
  172. function save(){
  173. uidl = $('#uid').val();
  174. val = $('#titlev').val();
  175. $('#'+uidl).html(val);
  176. $('#modal-lg').modal('hide');
  177. $.post( "updatetitle", {'uid':uidl,'title':val, 'text':'', 'cat':'<?=$ucat?>','_csrf':'<?=Yii::$app->request->csrfToken?>'}, function( data, status, xhr ) {
  178. if( data.status == 'ok' ){
  179. }
  180. if( data.status == 'err' ){
  181. console.log( data.text );
  182. alert(data.text);
  183. }
  184. }, "json");
  185. }
  186. var poper = new Map();
  187. </script>
  188. <?
  189. \Yii::$app->assetManager->bundles['yii\\bootstrap\\BootstrapAsset'] = [
  190. 'css' => [],
  191. 'js' => []
  192. ];
  193. ?>
  194. <?= FileUpload::widget([
  195. 'model' => $model,
  196. 'attribute' => 'file',
  197. 'url' => ['docsupload', 'id' => 0, 'cat'=>$ucat],
  198. 'options' => ['accept' => '*'],
  199. 'clientOptions' => [
  200. 'limitConcurrentUploads' => 3,
  201. 'disableImageMetaDataLoad' => true,
  202. 'autoUpload' => true,
  203. ],
  204. // ...
  205. 'clientEvents' => [
  206. 'fileuploaddone' => 'function(e, data) {
  207. res = JSON.parse(data.result);
  208. $("#filesbox").append("<a href="+res.files[0].name+" class=\"nav-link\" target=\"_blank\" style=\"display: inline-block;\"><i class=\"far fa-file-alt\"></i> <span>"+res.files[0].name+"</span></a>");
  209. var obj = poper.get(res.files[0].oname);
  210. setTimeout(() => {
  211. obj.css("display", "none");
  212. }, 2000);
  213. e.preventDefault();
  214. }',
  215. 'fileuploadfail' => 'function(e, data) {
  216. // console.log(e);
  217. console.log(data);
  218. e.preventDefault();
  219. }',
  220. 'fileuploadchange' => 'function(e, data) {
  221. starting(e, data);
  222. }',
  223. 'fileuploaddrop' => 'function(e, data) {
  224. starting(e, data);
  225. }',
  226. 'fileuploadprogress' =>'function(e,data) {
  227. e.preventDefault();
  228. progress = parseInt(data.loaded / data.total * 100, 10);
  229. var obj = poper.get(data.files[0].name);
  230. obj.find(".progress-bar").css("width", progress+"%");
  231. }',
  232. 'fileuploadstop' => 'function(e) {
  233. console.log("fin");
  234. setTimeout(() => {
  235. window.location.reload();
  236. }, 3000);
  237. }'
  238. ],
  239. ]); ?>
  240. <script>
  241. function starting(e, data){
  242. data.files.forEach( function(value,key){
  243. res = value.name;
  244. console.log(res);
  245. var i = $("#templ1").find(".info-box-text");
  246. i.text("Upload "+res);
  247. var templ = $("#templ1").clone();
  248. templ.css("display", "block");
  249. var index = poper.size;
  250. if( index > 0 ){
  251. poper.forEach( function(value,key) {
  252. y = parseInt(value.css("bottom"), 10);
  253. y = y +90;
  254. if( y > 800 ) y = y - 800;
  255. });
  256. }else{
  257. y = 0;
  258. }
  259. templ.css("bottom", y+"px");
  260. templ.attr( "id", "poper_"+index );
  261. poper.set(res, templ);
  262. $("body").append(templ);
  263. });
  264. console.log("start");
  265. }
  266. </script>
  267. <?
  268. $this->registerJs(<<<JS
  269. var dzone=$('#conteiner');
  270. $('#docs-file').fileupload({'dropZone':dzone});
  271. JS, View::POS_READY
  272. );