Guest User

Untitled

a guest
Mar 31st, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Dashboard\Admin_Panel;
  4.  
  5. use Validator;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. use App\Http\Controllers\Dashboard\Admin_Panel\GuardParams as GuardParams;
  9. use App\Http\Controllers\Dashboard\DashboardController as DashboardController;
  10.  
  11.  
  12. class ArticlesController extends DashboardController
  13. {
  14.  
  15. use GuardParams;
  16.  
  17. protected $_mainTable = 'articles';
  18. protected $_mainModel = 'App\Article';
  19. protected $_param_btn = 'article';
  20. protected $_param_title = 'Articles';
  21. protected $_param_success = 'article';
  22. protected $_uploads_folder = '/images/uploads';
  23.  
  24. public function __construct() {
  25. /*\DB::listen(function($query) {
  26. \Log::info(
  27. $query->sql,
  28. $query->bindings,
  29. $query->time
  30. );
  31. });*/
  32. parent::__construct();
  33. if(!request()->ajax()) { // not need to do this, datatable or other response type
  34. $this->_page_params = $this->get_page_params();
  35. }
  36. }
  37.  
  38. protected function item_description($item) {
  39. return $item->title. ' (' .$item->id. ')';
  40. }
  41.  
  42. protected function get_items_params() {
  43.  
  44. $allType = request()->type_all;
  45.  
  46. $params = array(
  47. 'active' => array(
  48. 'section_title' => 'Active ' .$this->_param_title,
  49. 'page_title' => 'Active ' .$this->_param_title,
  50. 'table_title' => 'Active ' .$this->_param_title,
  51. 'box_class' => 'box-success',
  52. ),
  53. 'inactive' => array(
  54. 'section_title' => $this->_param_title. ' deleted articles',
  55. 'page_title' => $this->_param_title. ' deleted articles',
  56. 'table_title' => $this->_param_title. ' deleted articles',
  57. 'box_class' => 'box-danger',
  58. ),
  59. );
  60.  
  61. if(!isset($params[$allType])) {
  62. abort(404);
  63. }
  64.  
  65. return $params[$allType];
  66. }
  67.  
  68. private function get_table_builder() {
  69. $model = $this->_mainModel;
  70.  
  71. $builder = $model::select(['articles.id', 'articles.title', 'articles.text', 'users.name', 'articles.created_at', 'categories.name as category', 'articles.deleted_at'/*, 'images.file_name as main_img_file'*/])
  72. /*->selectRaw('count(*) as comments_count, group_concat(comments.text, "<br>") as comments')*/
  73. /*->selectRaw('group_concat(comments.text, "<br>") as comments')*/
  74. ->leftJoin('users', 'users.id','=','articles.id_user')
  75. /*->leftJoin('images', 'articles.id','=','images.id_article')*/
  76. /*->where('images.main', 1)*/
  77. ->leftJoin('categories', 'categories.id','=','articles.id_category');
  78. /*->leftJoin('comments', 'articles.id', '=', 'comments.id_article')
  79. ->groupBy('articles.id');*/
  80.  
  81. /*$col_interval = request()->column_date_interval;
  82. if (!is_null($col_interval)) {
  83. $range = [date("Y-m-d H:i:s", strtotime(request()->min_date)), date("Y-m-d H:i:s", strtotime(request()->max_date))];
  84. $builder->whereBetween($col_interval, $range);
  85. }*/
  86.  
  87. if(request()->type_all == 'inactive')
  88. $builder->onlyTrashed();
  89. else
  90. $builder->withoutTrashed();
  91. // $builder->withTrashed();
  92. // \Log::info('Table builder: ' .$builder->toSql());
  93. return $builder;
  94. }
  95.  
  96. private function get_columns() {
  97.  
  98. // inner array -> data: col_unique name from select, if same cols names diferent tables, use alias in select
  99. // inner array -> name: table.column in database structure
  100.  
  101. return array(
  102. 'ID' => array('data' => 'id', 'name' => 'articles.id', 'visible' => false),
  103. /*'Main Image' => array('data' => 'main_img_file', 'sortable' => false, 'searchable' => false),*/
  104. 'Title' => array('data' => 'title'),
  105. 'Text' => array('data' => 'text', 'name' => 'articles.text', 'visible' => false),
  106. 'User' => array('data' => 'name', 'name' => 'users.name'),
  107. 'Category' => array('data' => 'category', 'name' => 'categories.name'),
  108. /*'Comments' => array('data' => 'comments', 'name' => 'comments.text'),*/
  109. /*'Comments Count' => array('data' => 'comments_count', 'name' => 'comments_count', 'searchable' => false),*/
  110. 'Created' => array('data' => 'created_at', 'name' => 'articles.created_at', 'searchable' => false),
  111. 'Actions' => array('data' => 'actions', 'orderable' => false, 'searchable' => false)
  112. );
  113. }
  114.  
  115.  
  116. /* private function get_table_builder() {
  117. $model = $this->_mainModel;
  118.  
  119. $builder = $model::with([
  120. 'comments' => function($q) {
  121. $q->select('id_article', 'text');
  122. },
  123. 'category' => function($q) {
  124. $q->select('id', 'name');
  125. },
  126. 'user' => function($q) {
  127. $q->select('id', 'name', 'id_role');
  128. },
  129. 'user.role' => function($q) {
  130. $q->select('id', 'name');
  131. },
  132. 'main_img' => function($q) {
  133. $q->select('id_article', 'file_name');
  134. }
  135. ])
  136. ->join('users', 'users.id','=','articles.id_user')
  137. ->join('categories', 'categories.id','=','articles.id_category')
  138. ->join('roles', 'roles.id','=','users.id_role')
  139. ->join('comments', 'articles.id', '=', 'comments.id_article')
  140. ->select(['articles.id', 'articles.title', 'articles.text', 'articles.created_at', 'articles.id_category', 'articles.deleted_at', 'articles.id_user']);
  141. ->selectRaw('count(comments.id_article) as comments_count')
  142. ->groupBy('articles.id');
  143.  
  144.  
  145. if(request()->type_all == 'inactive')
  146. $builder->onlyTrashed();
  147. else
  148. $builder->withoutTrashed();
  149. // $builder->withTrashed();
  150.  
  151. return $builder;
  152. }
  153.  
  154. private function get_columns() {
  155.  
  156. // inner array -> data: col_unique name from select, if same cols names diferent tables, use alias in select
  157. // inner array -> name: table.column in database structure
  158.  
  159. return array(
  160. 'ID' => array('data' => 'id', 'name' => 'articles.id', 'visible' => false),
  161. 'Main Image' => array('data' => 'main_img', 'sortable' => false, 'searchable' => false),
  162. 'Title' => array('data' => 'title'),
  163. 'Text' => array('data' => 'text', 'name' => 'articles.text'),
  164. 'User' => array('data' => 'user.name', 'name' => 'user.name'),
  165. 'Role' => array('data' => 'user.role.name', 'name' => 'user.role.name', 'visible' => false),
  166. 'Category' => array('data' => 'category.name', 'name' => 'category.name'),
  167. 'Comments' => array('data' => 'comments', 'name' => 'comments.text'),
  168. 'Comments Count' => array('data' => 'comments_count', 'name' => 'comments_count', 'searchable' => false),
  169. 'Created' => array('data' => 'created_at', 'visible' => false),
  170. 'Actions' => array('data' => 'actions', 'orderable' => false, 'searchable' => false)
  171. );
  172. } */
  173.  
  174. private function get_table() {
  175. $builder = $this->get_table_builder();
  176. $datatable = \Datatables::of($builder);
  177.  
  178. if(request()->type_all == 'inactive')
  179. $datatable->onlyTrashed();
  180.  
  181. $datatable->addColumn('actions', function ($item) {
  182. if($item->trashed())
  183. $actions = ['view', 'restore', 'force_delete'];
  184. else
  185. $actions = ['view', 'edit', 'delete'];
  186.  
  187. $actions = $this->get_buttons($item->id, $actions, $this->item_description($item));
  188. return $this->get_buttons_html($actions);
  189. });
  190.  
  191. /*$datatable->addColumn('user_role', function ($item) {
  192. return $item->user->role->name;
  193. });*/
  194.  
  195. $datatable->editColumn('created_at', function ($item) {
  196. return $item->created_at->format('d-m-Y');
  197. });
  198.  
  199. $datatable->editColumn('text', function ($item) {
  200. return str_limit(strip_tags($item->text), 20, '...');
  201. });
  202.  
  203. /* $datatable->editColumn('comments', function ($item) {
  204. return $item->comments->map(function($i) {
  205. return str_limit($i->text, 30, '...');
  206. })->implode('<br>');
  207. });
  208.  
  209. $datatable->addColumn('comments_count', function ($item) {
  210. return count($item->comments);
  211. }); */
  212.  
  213. /*$datatable->editColumn('main_img_file', function ($item) {
  214. return '<img class="table_image" src="' .url($this->_uploads_folder. '/' .$item->main_img_file). '" alt="' .$item->main_img_file. '">';
  215.  
  216. }); */
  217.  
  218. $datatable->setRowClass(function ($item) {
  219. return $item->id % 2 == 0 ? 'success' : 'danger';
  220. });
  221.  
  222. $col_interval = request()->column_date_interval;
  223. if (!is_null($col_interval)) {
  224. $datatable->filter(function($q) use ($col_interval) {
  225. $range = [date("Y-m-d H:i:s", strtotime(request()->min_date)), date("Y-m-d H:i:s", strtotime(request()->max_date))];
  226. $q->whereBetween($col_interval, $range);
  227. });
  228. }
  229.  
  230. $datatable->rawColumns(['actions']);
  231.  
  232. // setting setTotalRecords
  233. // return $datatable->rawColumns(['actions', 'comments'])->setTotalRecords(1000);
  234.  
  235. /*return $datatable->rawColumns(['actions', 'comments'])
  236. ->limit(function ($query) {
  237. $query->where('articles.id', '>', 499000);
  238. })
  239. ->setTotalRecords(1000);*/
  240. // return $datatable->setTotalRecords($builder->count());
  241. return $datatable;
  242. }
  243.  
  244. public function get_all() {
  245. $datepicker = Null; // no datepicker
  246.  
  247.  
  248. $datepicker = array(
  249. 'min_label' => 'From',
  250. 'max_label' => 'To',
  251. 'columnNameSQL' => 'articles.created_at',
  252. );
  253.  
  254.  
  255. // is datatable
  256. if(request()->ajax()) {
  257. $st = microtime(true);
  258. $ret = $this->get_table()->make(true);
  259. // \Log::info(microtime(true)-$st);
  260. return $ret;
  261. }
  262.  
  263. $cols_params = $this->get_columns();
  264.  
  265. $cols_names = array_keys($cols_params);
  266. $cols_params = array_values($cols_params);
  267.  
  268. $itemsParams = $this->get_items_params();
  269.  
  270. $myTables = array(
  271. array(
  272. 'title' => $itemsParams['table_title'],
  273. 'box_class' => $itemsParams['box_class'],
  274. 'cols_names' => $cols_names,
  275. 'cols_params' => $cols_params,
  276. 'costum_filter_params' => []
  277. // 'rows' => $rows,
  278. ),
  279. );
  280.  
  281. $this->_page_params['section_title'] = $itemsParams['section_title'];
  282. $this->_page_params['page_title'] = $itemsParams['page_title'];
  283. $dataToView = array(
  284. 'page_params' => $this->_page_params,
  285. 'data' => array(
  286. 'tables' => $myTables,
  287. 'datepicker' => $datepicker,
  288. 'add_btn' => array(
  289. 'href' => $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/add',
  290. 'title' => 'Add ' .$this->_param_btn,
  291. 'text' => 'Add ' .$this->_param_btn,
  292. ),
  293. )
  294. );
  295.  
  296. $dataToView['datatable'] = array(
  297. 'server_side' => True,
  298. );
  299. $dataToView['data']['export_btn'] = array(
  300. 'href' => $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/export_csv/' .request()->type_all,
  301. 'title' => 'Export data to csv/excel',
  302. 'text' => 'Export data to csv/excel'
  303. );
  304. return view('dashboard/sections/datatable_server')->with($dataToView);
  305. }
  306.  
  307.  
  308. public function export_csv() {
  309. $builder = $this->get_table_builder();
  310. $has_data = false;
  311.  
  312. foreach($builder->cursor() as $key => $row) {
  313. if(!$has_data) {
  314. $t = $this->get_columns();
  315. $delimiter = ';';
  316. $file_name = $this->_mainTable. '.csv';
  317. $export_file = storage_path('/exports/' .$file_name);
  318. file_put_contents($export_file, '');
  319. $text = "\xEF\xBB\xBF".implode($delimiter, array_keys($t)). "\n";
  320. $cols = array_column(array_values($t), 'data');
  321. $cols = array_diff($cols, ['actions']); // don't use actions
  322. $has_data = true;
  323. }
  324. if($key%10000 == 0) {
  325. file_put_contents($export_file, $text, FILE_APPEND);
  326. $text = ''; // save ram for big data
  327. }
  328. $text .= implode($delimiter, array_map(function($c) use ($row) {
  329. return $row->$c;
  330. }, $cols)). "\n";
  331. }
  332.  
  333. if(!$has_data) { // no text
  334. return redirect()->back()->with('flash_error', 'No data to export');
  335. }
  336.  
  337. file_put_contents($export_file, $text, FILE_APPEND);
  338.  
  339. return response()->download($export_file);
  340.  
  341. /*return response($text)->withHeaders([
  342. 'Content-Type' => 'application/octet-stream',
  343. 'Content-disposition' => 'attachment; filename="' .$file_name,
  344. 'Content-Transfer-Encoding' => 'Binary',
  345. ]);*/
  346.  
  347. /*header("Content-Type: application/octet-stream");
  348. header("Content-Transfer-Encoding: Binary");
  349. header('Content-disposition: attachment; filename="' .$this->_mainTable. '.csv"');
  350. return $text;*/
  351. }
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361. //INSPECT/VIEW ITEM
  362.  
  363.  
  364. public function view_item() {
  365.  
  366. $model = $this->_mainModel;
  367. $item = $model::with(['category', 'main_img', 'thumbnail_img', 'user'])->withTrashed()->findOrFail(request()->id);
  368.  
  369. $data = array(
  370. 'inspect' => array(
  371. 'Title' => $item->title,
  372. 'Category' => (!is_null($item->category)) ? $item->category->name : '',
  373. 'User' => '<a href="' .url($this->_baseUrl. '/' .$this->_guard. '/users/view/' .$item->user->id). '">' .$item->user->name. '</a>',
  374. 'Main Image' => '<br><a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '" alt="' .$item->main_img->file_name. '"></a>',
  375. 'Thumbnail' => '<br><a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '" alt="' .$item->thumbnail_img->file_name. '"></a>',
  376. 'On main Slider' => ($item->on_main_slider==1)?'yes':'no',
  377. 'Created At' => $item->created_at->format('d-m-Y H:i'),
  378. 'Updated At' => $item->updated_at->format('d-m-Y H:i'),
  379. 'Deleted At' => (is_null($item->deleted_at))?'':$item->deleted_at->format('d-m-Y H:i'),
  380. 'Body' => '<pre style="white-space: normal;">'.$item->text.'</pre>',
  381. ),
  382. );
  383.  
  384. if($item->trashed())
  385. $buttons = array('restore');
  386. else
  387. $buttons = array('delete', 'edit');
  388.  
  389. $item_desc = $this->item_description($item);
  390. $data['buttons'] = $this->get_buttons($item->id, $buttons, $item_desc);
  391.  
  392.  
  393. $this->_page_params['section_title'] = $this->_param_title. ': ' .$item_desc;
  394. $this->_page_params['page_title'] = 'Dashboard | '.$this->_param_title;
  395. $dataToView = array(
  396. 'page_params' => $this->_page_params,
  397. 'data' => $data
  398. );
  399. return view('dashboard/sections/inspect')->with($dataToView);
  400. }
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408. protected function get_form_inputs($item=null) {
  409.  
  410. $categories = collect(array((object) array(
  411. 'name' => '',
  412. 'id' => '',
  413. 'opt_text' => '',
  414. 'props' => array(
  415. 'value' => '',
  416. 'disabled' => 'disabled',
  417. 'selected' => 'selected'
  418. )
  419. )));
  420.  
  421. $on_main_slider = collect(
  422. array(
  423. (object) array(
  424. 'opt_text' => '',
  425. 'props' => array(
  426. 'type' => 'checkbox',
  427. 'value' => '1',
  428. 'name' => 'on_main_slider'
  429. )
  430. ),
  431. )
  432. );
  433.  
  434.  
  435. $categories = $categories->merge(\App\Category::all());
  436.  
  437. //dd(old());
  438.  
  439. foreach ($categories as $category)
  440. {
  441. $category->opt_text = $category->name;
  442.  
  443. if(!isset($category->props)) {
  444. $props = array(
  445. 'value' => $category->id,
  446. );
  447. if(old('id_category') == $category->id)
  448. {
  449. $props['selected'] = 1;
  450. }
  451. else if (!is_null($item) && $item->id_category == $category->id)
  452. $props['selected'] = 1;
  453.  
  454. $category->props = $props;
  455. }
  456.  
  457. }
  458.  
  459. if(!is_null(old('on_main_slider')))
  460. $on_main_slider[0]->props['checked'] = true;
  461. else if(!is_null($item) && $item->on_main_slider == 1)
  462. $on_main_slider[0]->props['checked'] = true;
  463.  
  464. $inputs = array(
  465. '_token' => array(
  466. 'type' => 'hidden',
  467. 'props' => array(
  468. 'name' => '_token',
  469. 'type' => 'hidden',
  470. 'value' => csrf_token()
  471. ),
  472. ),
  473. 'title' => array(
  474. 'input-group-addon' => '<i class="fa fa-font"></i>',
  475. 'label' => 'Title',
  476. 'type' => 'text',
  477. 'parent_class' => 'input-group',
  478. 'props' => array(
  479. 'class' => 'form-control',
  480. 'name' => 'title',
  481. 'value' => $this->get_input_value($item, old('title'), 'title', ''),
  482. 'id' => 'title',
  483. 'required' => true
  484. ),
  485. ),
  486. 'category' => array(
  487. 'input-group-addon' => '<i class="fa fa-user"></i>',
  488. 'label' => 'Category',
  489. 'type' => 'select',
  490. 'parent_class' => 'input-group',
  491. 'select_props' => array(
  492. 'class' => 'form-control',
  493. 'name' => 'id_category',
  494. 'id' => 'id_category',
  495. 'required' => 1
  496. ),
  497. 'param' => 'name',
  498. 'opt1' => '',
  499. 'opts' => $categories,
  500. ),
  501.  
  502. 'text' => array(
  503. 'input-group-addon' => '<i class="fa fa-font"></i>',
  504. 'value' => $this->get_input_value($item, old('text'), 'text', ''),
  505. 'label' => 'Text',
  506. 'type' => 'textarea',
  507. 'parent_class' => 'input-group max_viewport',
  508. 'props' => array(
  509. 'name' => 'text',
  510. 'id' => 'text',
  511. 'class' => 'form-control tinymce',
  512. 'rows' => '5'
  513. ),
  514. ),
  515.  
  516. 'main_img' => array(
  517. 'input-group-addon' => '<i class="fa fa-euro"></i>',
  518. 'label' => 'Main Image <small class="text-muted">(Horizontal top)</small>',
  519. 'type' => 'file',
  520. 'parent_class' => 'input-group',
  521. 'props' => array(
  522. 'type' => 'file',
  523. 'name' => 'main_img',
  524. 'id' => 'main_img'
  525. ),
  526. ),
  527.  
  528. 'thumbnail_img' => array(
  529. 'input-group-addon' => '<i class="fa fa-euro"></i>',
  530. 'label' => 'Thumbnail image',
  531. 'type' => 'file',
  532. 'parent_class' => 'input-group',
  533. 'props' => array(
  534. 'type' => 'file',
  535. 'name' => 'thumbnail_img',
  536. 'id' => 'thumbnail_img'
  537. ),
  538. ),
  539. 'on_main_slider' => array(
  540. 'input-group-addon' => '<i class="fa fa-tags"></i>',
  541. 'label' => 'On main Slider',
  542. 'type' => 'checkbox',
  543. 'param' => 'name',
  544. 'parent_class' => '',
  545. 'id' => 'on_main_slider',
  546. 'opts' => $on_main_slider,
  547. ),
  548.  
  549.  
  550. );
  551. return $inputs;
  552. }
  553.  
  554. protected function get_page_data($form_action, $item=null) {
  555.  
  556. $data = array(
  557. 'form_params' => array(
  558. 'action' => $form_action,
  559. 'button_text' => 'Add ' .$this->_param_title,
  560. 'inputs' => $this->get_form_inputs($item)
  561. ),
  562. );
  563. if($item != null) {
  564. $data['form_params']['button_text'] = 'Edit ' .$this->_param_title;
  565. $data['form_params']['inputs']['id'] = array(
  566. 'type' => 'hidden',
  567. 'props' => array(
  568. 'name' => 'id',
  569. 'type' => 'hidden',
  570. 'value' => $item->id
  571. ),
  572. );
  573. }
  574. return $data;
  575. }
  576.  
  577. public function add() {
  578.  
  579. $form_action = $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/post_add';
  580.  
  581. $this->_page_params['section_title'] = 'Add ' .$this->_param_title;
  582. $this->_page_params['page_title'] = 'Dashboard | Add ' .$this->_param_title;
  583. $dataToView = array(
  584. 'page_params' => $this->_page_params,
  585. 'tinymce' => true,
  586. 'data' => $this->get_page_data($form_action),
  587. );
  588. return view('dashboard/sections/form')->with($dataToView);
  589. }
  590.  
  591. public function edit() {
  592.  
  593. $model = $this->_mainModel;
  594. $item = $model::findOrfail(request()->id);
  595.  
  596. $form_action = $this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/post_edit';
  597.  
  598. $this->_page_params['section_title'] = 'Editar ' .$this->_param_title. ': ' .$item->title;
  599. $this->_page_params['page_title'] = 'Dashboard | Editar ' .$this->_param_title. ': ' .$item->title;
  600.  
  601. //dd($item->images);
  602. $extra_form_html='';
  603. if (!is_null($item->main_img))
  604. $extra_form_html .= '<div>Main Image<a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->main_img->file_name). '" alt="' .$item->main_img->file_name. '"></a></div>';
  605. if (!is_null($item->thumbnail_img))
  606. $extra_form_html .= '<div>Thumbnail Image<a target="_blank" href="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '"><img class="image_inspect" src="' .url($this->_uploads_folder. '/' .$item->thumbnail_img->file_name). '" alt="' .$item->thumbnail_img->file_name. '"></a><br></div>';
  607.  
  608. $dataToView = array(
  609. 'tinymce' => true,
  610. 'page_params' => $this->_page_params,
  611. 'data' => $this->get_page_data($form_action, $item),
  612. 'extra_form_html' => $extra_form_html,
  613. );
  614.  
  615. return view('dashboard/sections/form')->with($dataToView);
  616. }
  617.  
  618.  
  619.  
  620. // POSTS
  621.  
  622. private function val_item($inputs, $item=null) {
  623.  
  624. $rules = array(
  625. 'title' => 'required',
  626. 'main_img' => 'required|image',
  627. 'thumbnail_img' => 'required|image',
  628. 'id_category' => 'required',
  629. );
  630. if(!is_null($item)) {
  631. $rules['main_img'] = 'image';
  632. $rules['thumbnail_img'] = 'image';
  633. }
  634.  
  635. return Validator::make($inputs, $rules);
  636. }
  637.  
  638. public function post_add() {
  639. $inputs = request()->except('_token', 'main_img', 'thumbnail_img');
  640. //dd($inputs);
  641.  
  642. $validator = $this->val_item(request()->all());
  643. if($validator->fails()) {
  644. return redirect()->back()->withInput()->withErrors($validator);
  645. }
  646. $ret = $this->base64_to_images($inputs['text']);
  647.  
  648. $inputs['text'] = $ret['html'];
  649. $inputs['id_user'] = \Auth::user()->id;
  650.  
  651. $model = $this->_mainModel;
  652. $new_item = $model::create($inputs);
  653. $this->store_imgs($ret['images'], $new_item);
  654.  
  655. if(!is_null(request()->on_main_slider)) {
  656. $inputs['on_main_slider'] = 1;
  657. }
  658.  
  659.  
  660. $new_item->images()->createMany([
  661. [
  662. 'file_name' => $this->store_file(request()->main_img),
  663. 'main' => 1,
  664. ],
  665. [
  666. 'file_name' => $this->store_file(request()->thumbnail_img),
  667. 'thumbnail' => 1,
  668. ]
  669. ]);
  670.  
  671. return redirect()->back()->with('flash_success', $this->_param_title. ' added (' .$new_item->title. ')');
  672. }
  673.  
  674. public function post_edit() {
  675.  
  676. $model = $this->_mainModel;
  677. $item = $model::findOrfail(request()->id);
  678.  
  679. $inputs = request()->except('_token', 'main_img', 'thumbnail_img');
  680.  
  681. //dd($inputs);
  682.  
  683. $validator = $this->val_item(request()->all(), $item);
  684. if($validator->fails()) {
  685. return redirect()->back()->withInput()->withErrors($validator);
  686. }
  687.  
  688. $ret = $this->base64_to_images($inputs['text']);
  689.  
  690. $inputs['text'] = $ret['html'];
  691. $this->store_imgs($ret['images'], $item);
  692.  
  693. $inputs['on_main_slider'] = 0;
  694. if(!is_null(request()->on_main_slider)) {
  695. $inputs['on_main_slider'] = 1;
  696. }
  697.  
  698. $item->update($inputs);
  699.  
  700. $name_main_img = '';
  701. if (!is_null($item->main_img))
  702. $name_main_img = $item->main_img->file_name;
  703.  
  704. $name_thumbnail_img = '';
  705. if (!is_null($item->thumbnail_img))
  706. $name_thumbnail_img = $item->thumbnail_img->file_name;
  707.  
  708.  
  709. if(!is_null(request()->main_img)) {
  710. $item->images()->where('main', 1)->delete();
  711. $item->images()->create([
  712. 'file_name' => $this->store_file(request()->main_img, $name_main_img),
  713. 'main' => 1,
  714. ]);
  715. }
  716. if(!is_null(request()->thumbnail_img)) {
  717. $item->images()->where('thumbnail', 1)->delete();
  718. $item->images()->create([
  719. 'file_name' => $this->store_file(request()->thumbnail_img, $name_thumbnail_img),
  720. 'thumbnail' => 1,
  721. ]);
  722. }
  723.  
  724. return redirect()->back()->with('flash_success', $this->_param_title. ' edited (' .$item->title. ')');
  725. }
  726.  
  727. private function store_imgs($imgs, $item) {
  728. $del_imgs_builder = $item->images()->select(['file_name'])->where('main', 0)->where('thumbnail', 0)->whereNotIn('file_name', $imgs);
  729. $del_imgs = $del_imgs_builder->get();
  730.  
  731. foreach ($del_imgs as $img) {
  732. $img_path = public_path($this->_uploads_folder . '/' . $img->file_name);
  733. if(is_file($img_path)) {
  734. unlink($img_path);
  735. }
  736. }
  737. $del_imgs_builder->delete();
  738.  
  739. $create_imgs = [];
  740. foreach($imgs as $img) {
  741. if($item->images()->where('file_name', $img)->count() == 0) {
  742. $create_imgs[] = array('file_name' => $img);
  743. }
  744. }
  745. $item->images()->createMany($create_imgs);
  746. }
  747.  
  748.  
  749.  
  750. private function store_file($file=null, $file_name=null) {
  751.  
  752. if(!is_null($file)) {
  753. if(!is_null($file_name)) {
  754. $f_path = public_path($this->_uploads_folder. '/' .$file_name);
  755. if(is_file($f_path)) {
  756. unlink($f_path);
  757. }
  758. }
  759. // $original_f_name = $file->getClientOriginalName();
  760. $fileName = time(). '_' .str_random(15). '_' .$file->getClientOriginalName();
  761. $file->move(public_path($this->_uploads_folder), $fileName);
  762. return $fileName;
  763. }
  764. return $file_name;
  765. }
  766.  
  767.  
  768.  
  769. public function delete() {
  770. $model = $this->_mainModel;
  771. $item = $model::withTrashed()->findOrFail(request()->id);
  772. $item->delete();
  773.  
  774. return redirect()->back();
  775. }
  776.  
  777. public function force_delete() {
  778. $model = $this->_mainModel;
  779. $item = $model::with(['images'])->withTrashed()->findOrFail(request()->id);
  780. foreach ($item->images as $img) {
  781. $img_path = public_path($this->_uploads_folder . '/' . $img->file_name);
  782. if(is_file($img_path)) {
  783. unlink($img_path);
  784. }
  785. }
  786. $item->images()->delete();
  787. $item->forceDelete();
  788. return redirect($this->_baseUrl. '/' .$this->_guard. '/' .$this->_mainTable. '/inactive/all');
  789. }
  790.  
  791. public function restore() {
  792. $model = $this->_mainModel;
  793. $item = $model::withTrashed()->findOrFail(request()->id);
  794. $item->restore();
  795.  
  796. return redirect()->back();
  797. }
  798.  
  799. }
Add Comment
Please, Sign In to add comment