PageRenderTime 47ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/app/Http/Controllers/Admin/TropicController.php

https://bitbucket.org/coredeveloper2013/navipi-test
PHP | 452 lines | 210 code | 109 blank | 133 comment | 21 complexity | 4a1d85090c0d12a43c1b718a0d76f901 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Image;
  4. use App\AllTropic;
  5. use App\TropicGroup;
  6. use App\Language;
  7. use Illuminate\Http\Request;
  8. use App\Http\Requests;
  9. use App\Http\Controllers\Controller;
  10. use Illuminate\Support\Facades\Validator;
  11. class TropicController extends Controller
  12. {
  13. protected $languages;
  14. protected $fallback_language;
  15. /**
  16. * CmsPageController constructor.
  17. */
  18. public function __construct()
  19. {
  20. $this->languages = app('languages');
  21. $this->fallback_language = $this->languages->where('fallback_locale', 'Y')->first();
  22. }
  23. /**
  24. * Display a listing of the resource.
  25. *
  26. * @param string $locale
  27. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  28. */
  29. public function index($locale = '')
  30. {
  31. /* if($locale && in_array($locale, config('translatable.locales'))) {
  32. app()->setLocale($locale);
  33. } */
  34. $current_language = Language::current();
  35. $per_page = config('constants.ADMIN_PER_PAGE');
  36. $tropics = AllTropic::orderBy('id', 'desc')->paginate($per_page);
  37. //$cms = Page::translatedIn(app()->getLocale())->orderBy('id', 'desc')->paginate($per_page);
  38. return view('admin.tropics.list', compact('tropics','current_language'));
  39. }
  40. /**
  41. * Show the form for creating a new resource.
  42. *
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function create()
  46. {
  47. return view('admin.tropics.add');
  48. }
  49. protected function csvValidator($file)
  50. {
  51. return $validator = Validator::make(
  52. [
  53. 'file' => $file,
  54. 'extension' => strtolower($file->getClientOriginalExtension()),
  55. ],
  56. [
  57. 'file' => 'required',
  58. 'extension' => 'required|in:csv',
  59. ],
  60. [
  61. 'extension.in' => 'Please upload a valid CSV file'
  62. ]
  63. );
  64. }
  65. public function addCsv()
  66. {
  67. $allTropic = AllTropic::all();
  68. /* dd($allTropic->toArray());
  69. $list = array (); */
  70. $list = array (
  71. array('TOPIC', 'GROUP'),
  72. array('', ''),
  73. );
  74. if(!empty($allTropic))
  75. {
  76. foreach($allTropic as $val){
  77. $list[]=array($val->title, $val->group['name']);
  78. }
  79. }
  80. $file = fopen("assets/upload/topic_csv/tropics.csv","w");
  81. foreach ($list as $line)
  82. {
  83. fputcsv($file,$line);
  84. }
  85. fclose($file);
  86. return view('admin.tropics.add-csv');
  87. }
  88. public function uploadCsv(Request $request)
  89. {
  90. $this->validate($request, [
  91. 'topic_csv' => 'required',
  92. ]
  93. );
  94. $inp_data=$request->all();
  95. //dd($inp_data);
  96. if ($request->hasFile('topic_csv')){
  97. $file = $request->file('topic_csv');
  98. $validator = $this->csvValidator($file);
  99. if ($validator->fails()) {
  100. $this->throwValidationException(
  101. $request, $validator
  102. );
  103. }
  104. $name = time() . '-' . $file->getClientOriginalName();
  105. //check out the edit content on bottom of my answer for details on $storage
  106. $storage = public_path();
  107. $path = $storage . '/assets/upload/topic_csv/';
  108. // Moves file to folder on server
  109. $file->move($path, $name);
  110. //echo $name;
  111. // Import the moved file to DB and return OK if there were rows affected
  112. //echo $updb = ( $this->_import_csv($path, $name) ? 'OK' : 'No rows affected' );
  113. $old_file = 'assets/upload/topic_csv/'.$name;
  114. $row = 1;
  115. if (($handle = fopen($old_file, "r")) !== FALSE) {
  116. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  117. $num = count($data);
  118. //echo "<p> $num fields in line $row: <br /></p>\n";
  119. for ($c=0; $c < $num; $c++) {
  120. /* echo $data[$c] . "<br />\n"; */
  121. if($c==0) $title = $data[$c];
  122. if($c==1 && $row >2)
  123. {
  124. $grup = $data[$c];
  125. if(trim($grup)!='')
  126. {
  127. $tgrp = TropicGroup::where('name',strtolower($grup))->first();
  128. if(empty($tgrp))
  129. {
  130. $tgrp = TropicGroup::create(array('name'=>$grup));
  131. }
  132. $group_id = $tgrp->id;
  133. }
  134. else $group_id = 0;
  135. }
  136. if($c==2){
  137. return redirect()->back()->with('error', 'Inappropiate CSV file.');
  138. break;
  139. }
  140. }
  141. if($row >2){
  142. $fdata = AllTropic::where('title',$title)->first();
  143. if(empty($fdata))
  144. AllTropic::create(['title'=>$title,'group_id'=>$group_id]);
  145. }
  146. $row++;
  147. }
  148. fclose($handle);
  149. }
  150. \File::delete($old_file);
  151. return redirect()->back()->with('success', 'Tropic successfully added.');
  152. }
  153. return redirect()->back()->with('error', 'Tropic add error.');
  154. }
  155. /**
  156. * Store a newly created resource in storage.
  157. *
  158. * @param \Illuminate\Http\Request $request
  159. * @return \Illuminate\Http\Response
  160. */
  161. public function store(Request $request)
  162. {
  163. $this->validate($request, [
  164. 'title' => 'required|max:255',
  165. ]
  166. );
  167. $inp_data=$request->all();
  168. //dd($inp_data);
  169. $title = $inp_data['title'];
  170. $title_arr = explode('(',$inp_data['title']);
  171. //dd($title_arr);
  172. if(isset($title_arr[1]))
  173. {
  174. $grup = preg_replace("/[^a-zA-Z0-9]/", "", trim($title_arr[1]));
  175. $tgrp = TropicGroup::where('name',strtolower($grup))->first();
  176. //dd($tgrp->toArray());
  177. if(empty($tgrp))
  178. {
  179. $tgrp = TropicGroup::create(array('name'=>$grup));
  180. }
  181. $data['group_id']= $tgrp->id;
  182. }else $data['group_id']= 0;
  183. $data['title']= $title_arr[0];
  184. $time = time();
  185. if($request->hasFile('image')){
  186. /* $old_image = 'assets/upload/topic/'.$settings->image;
  187. \File::delete($old_image); */
  188. $path = public_path().'/assets/upload/topic/';
  189. $image = $request->file('image');
  190. $save_name = $time.str_random(10).'.'.$image->getClientOriginalExtension();
  191. Image::make($image->getRealPath())->save($path . $save_name, 100);
  192. $data['image'] = $save_name;
  193. }
  194. AllTropic::create($data);
  195. return redirect()->back()->with('success', 'Tropic successfully added.');
  196. }
  197. /**
  198. * Display the specified resource.
  199. *
  200. * @param int $id
  201. * @return \Illuminate\Http\Response
  202. */
  203. public function show($id)
  204. {
  205. //
  206. }
  207. /**
  208. * Show the form for editing the specified resource.
  209. *
  210. * @param int $id
  211. * @return \Illuminate\Http\Response
  212. */
  213. public function edit($id)
  214. {
  215. $tropic = AllTropic::find($id);
  216. $groups = TropicGroup::all();
  217. //dd($packages->toArray());
  218. return view('admin.tropics.edit', compact('tropic','groups'));
  219. }
  220. /**
  221. * Update the specified resource in storage.
  222. *
  223. * @param \Illuminate\Http\Request $request
  224. * @param int $id
  225. * @return \Illuminate\Http\Response
  226. */
  227. public function update(Request $request, $id)
  228. {
  229. $this->validate($request, [
  230. 'title' => 'required|max:255',
  231. 'group_id' => 'required',
  232. ]
  233. );
  234. $inp_data=$request->all();
  235. $data['title']= $inp_data['title'];
  236. $data['group_id']= $inp_data['group_id'];
  237. $tropic = AllTropic::find($id);
  238. $time = time();
  239. if($request->hasFile('image')){
  240. $old_image = 'assets/upload/topic/'.$tropic->image;
  241. \File::delete($old_image);
  242. $path = public_path().'/assets/upload/topic/';
  243. $image = $request->file('image');
  244. $save_name = $time.str_random(10).'.'.$image->getClientOriginalExtension();
  245. Image::make($image->getRealPath())->save($path . $save_name, 100);
  246. $data['image'] = $save_name;
  247. }
  248. \App\Tropic::where('group_id',$tropic->group_id)->update(array('group_id'=>$inp_data['group_id']));
  249. //unset($data['_method']);unset($data['_token']);
  250. //dd($data);
  251. $tropic->fill($data)->save();
  252. return redirect()->back()->with('success', 'Update was successfully done.');
  253. }
  254. /**
  255. * Remove the specified resource from storage.
  256. *
  257. * @param int $id
  258. * @return \Illuminate\Http\Response
  259. */
  260. public function destroy($id)
  261. {
  262. AllTropic::destroy($id);
  263. \App\Tropic::where('tropic_id',$id)->delete();
  264. return redirect()->back()->with('success', 'Tropic successfully removed!');
  265. }
  266. public function deleteAll(Request $request)
  267. {
  268. $input_data = $request->all();
  269. if(isset($input_data['data_ids']) && $input_data['data_ids']!='')
  270. {
  271. $data_ids=explode(',',$input_data['data_ids']);
  272. //dd($data_ids);
  273. $d= AllTropic::whereIn('id',$data_ids)->delete();
  274. $d= \App\Tropic::whereIn('tropic_id',$data_ids)->delete();
  275. return 1;
  276. //return redirect()->back()->with('success', 'Records successfully removed!');
  277. }
  278. else return 0;
  279. }
  280. /* public function deleteTranslation($locale = '', $id)
  281. {
  282. if($locale && in_array($locale, config('translatable.locales'))) {
  283. if($locale == '' || !in_array($locale, config('translatable.locales'))) {
  284. return redirect()->back()->with('error', 'Sorry! Unable to process your request.');
  285. }
  286. app()->setLocale($locale);
  287. $cms = Page::find($id)->getTranslation($locale);
  288. $cms->delete();
  289. $current_language = Language::current();
  290. return redirect()->back()->with('success', 'Page for ' . $current_language->name . ' successfully removed!');
  291. }
  292. return redirect()->back()->with('error', 'Sorry! Unable to process your request.');
  293. } */
  294. }