PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/Http/Controllers/Controller.php

https://gitlab.com/haque.mdmanzurul/TripAdviosrScrapper
PHP | 530 lines | 415 code | 107 blank | 8 comment | 102 complexity | aebbee1abaa248321889af3db24df956 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Carbon\Carbon;
  4. use Illuminate\Foundation\Bus\DispatchesJobs;
  5. use Illuminate\Routing\Controller as BaseController;
  6. use Illuminate\Foundation\Validation\ValidatesRequests;
  7. use Illuminate\Http\Request;
  8. use Validator, Input, Redirect ;
  9. abstract class Controller extends BaseController {
  10. use DispatchesJobs, ValidatesRequests;
  11. public function __construct()
  12. {
  13. $this->middleware('ipblocked');
  14. $driver = config('database.default');
  15. $database = config('database.connections');
  16. $this->db = $database[$driver]['database'];
  17. $this->dbuser = $database[$driver]['username'];
  18. $this->dbpass = $database[$driver]['password'];
  19. $this->dbhost = $database[$driver]['host'];
  20. if(\Auth::check() == true)
  21. {
  22. if(!\Session::get('gid'))
  23. {
  24. \Session::put('uid', \Auth::user()->id);
  25. \Session::put('gid', \Auth::user()->group_id);
  26. \Session::put('eid', \Auth::user()->email);
  27. \Session::put('ll', \Auth::user()->last_login);
  28. \Session::put('fid', \Auth::user()->first_name.' '. \Auth::user()->last_name);
  29. \Session::put('themes', 'sximo-light-blue');
  30. }
  31. }
  32. if(!\Session::get('themes'))
  33. {
  34. \Session::put('themes', 'sximo');
  35. }
  36. if (defined('CNF_MULTILANG') && CNF_MULTILANG == 1) {
  37. $lang = (\Session::get('lang') != "" ? \Session::get('lang') : CNF_LANG);
  38. \App::setLocale($lang);
  39. }
  40. $data = array(
  41. 'last_activity'=> strtotime(Carbon::now())
  42. );
  43. \DB::table('tb_users')->where('id',\Session::get('uid'))->update($data);
  44. }
  45. function getComboselect( Request $request)
  46. {
  47. if($request->ajax() == true && \Auth::check() == true)
  48. {
  49. $param = explode(':',$request->input('filter'));
  50. $parent = (!is_null($request->input('parent')) ? $request->input('parent') : null);
  51. $limit = (!is_null($request->input('limit')) ? $request->input('limit') : null);
  52. $rows = $this->model->getComboselect($param,$limit,$parent);
  53. $items = array();
  54. $fields = explode("|",$param[2]);
  55. foreach($rows as $row)
  56. {
  57. $value = "";
  58. foreach($fields as $item=>$val)
  59. {
  60. if($val != "") $value .= $row->$val." ";
  61. }
  62. $items[] = array($row->$param['1'] , $value);
  63. }
  64. return json_encode($items);
  65. } else {
  66. return json_encode(array('OMG'=>" Ops .. Cant access the page !"));
  67. }
  68. }
  69. public function getCombotable( Request $request)
  70. {
  71. if(Request::ajax() == true && Auth::check() == true)
  72. {
  73. $rows = $this->model->getTableList($this->db);
  74. $items = array();
  75. foreach($rows as $row) $items[] = array($row , $row);
  76. return json_encode($items);
  77. } else {
  78. return json_encode(array('OMG'=>" Ops .. Cant access the page !"));
  79. }
  80. }
  81. public function getCombotablefield( Request $request)
  82. {
  83. if($request->input('table') =='') return json_encode(array());
  84. if(Request::ajax() == true && Auth::check() == true)
  85. {
  86. $items = array();
  87. $table = $request->input('table');
  88. if($table !='')
  89. {
  90. $rows = $this->model->getTableField($request->input('table'));
  91. foreach($rows as $row)
  92. $items[] = array($row , $row);
  93. }
  94. return json_encode($items);
  95. } else {
  96. return json_encode(array('OMG'=>" Ops .. Cant access the page !"));
  97. }
  98. }
  99. function postMultisearch( Request $request)
  100. {
  101. $post = $_POST;
  102. $items ='';
  103. foreach($post as $item=>$val):
  104. if($_POST[$item] !='' and $item !='_token' and $item !='md' && $item !='id'):
  105. $items .= $item.':'.trim($val).'|';
  106. endif;
  107. endforeach;
  108. return Redirect::to($this->module.'?search='.substr($items,0,strlen($items)-1).'&md='.Input::get('md'));
  109. }
  110. function buildSearch( )
  111. {
  112. $keywords = ''; $fields = ''; $param ='';
  113. $allowsearch = $this->info['config']['forms'];
  114. foreach($allowsearch as $as) $arr[$as['field']] = $as ;
  115. if($_GET['search'] !='')
  116. {
  117. $type = explode("|",$_GET['search'] );
  118. if(count($type) >= 1)
  119. {
  120. foreach($type as $t)
  121. {
  122. $keys = explode(":",$t);
  123. if(in_array($keys[0],array_keys($arr))):
  124. if($arr[$keys[0]]['type'] == 'select' || $arr[$keys[0]]['type'] == 'radio' )
  125. {
  126. $param .= " AND ".$arr[$keys[0]]['alias'].".".$keys[0]." = '".$keys[1]."' ";
  127. } else {
  128. $param .= " AND ".$arr[$keys[0]]['alias'].".".$keys[0]." REGEXP '".$keys[1]."' ";
  129. }
  130. endif;
  131. }
  132. }
  133. }
  134. return $param;
  135. }
  136. function inputLogs(Request $request, $note = NULL)
  137. {
  138. $data = array(
  139. 'module' => $request->segment(1),
  140. 'task' => $request->segment(2),
  141. 'user_id' => Session::get('uid'),
  142. 'ipaddress' => $request->getClientIp(),
  143. 'note' => $note
  144. );
  145. \DB::table( 'tb_logs')->insert($data); ;
  146. }
  147. function validateForm()
  148. {
  149. $forms = $this->info['config']['forms'];
  150. $rules = array();
  151. foreach($forms as $form)
  152. {
  153. if($form['required']== '' || $form['required'] !='0')
  154. {
  155. $rules[$form['field']] = 'required';
  156. } elseif ($form['required'] == 'alpa'){
  157. $rules[$form['field']] = 'required|alpa';
  158. } elseif ($form['required'] == 'alpa_num'){
  159. $rules[$form['field']] = 'required|alpa_num';
  160. } elseif ($form['required'] == 'alpa_dash'){
  161. $rules[$form['field']]='required|alpa_dash';
  162. } elseif ($form['required'] == 'email'){
  163. $rules[$form['field']] ='required|email';
  164. } elseif ($form['required'] == 'numeric'){
  165. $rules[$form['field']] = 'required|numeric';
  166. } elseif ($form['required'] == 'date'){
  167. $rules[$form['field']]='required|date';
  168. } else if($form['required'] == 'url'){
  169. $rules[$form['field']] = 'required|active_url';
  170. } else {
  171. }
  172. }
  173. return $rules ;
  174. }
  175. function validatePost( $table )
  176. {
  177. $request = new Request;
  178. /// return json_encode($_POST);
  179. $str = $this->info['config']['forms'];
  180. $data = array();
  181. foreach($str as $f){
  182. $field = $f['field'];
  183. if($f['view'] ==1)
  184. {
  185. if($f['type'] =='textarea_editor' || $f['type'] =='textarea')
  186. {
  187. $content = (isset($_POST[$field]) ? $_POST[$field] : '');
  188. $data[$field] = $content;
  189. } else {
  190. if(isset($_POST[$field]))
  191. {
  192. $data[$field] = $_POST[$field];
  193. }
  194. // if post is file or image
  195. if($f['type'] =='file')
  196. {
  197. $files ='';
  198. if(isset($f['option']['image_multiple']) && $f['option']['image_multiple'] ==1)
  199. {
  200. if(isset($_POST['curr'.$field]))
  201. {
  202. $curr = '';
  203. for($i=0; $i<count($_POST['curr'.$field]);$i++)
  204. {
  205. $files .= $_POST['curr'.$field][$i].',';
  206. }
  207. }
  208. if(!is_null(Input::file($field)))
  209. {
  210. $destinationPath = '.'. $f['option']['path_to_upload'];
  211. foreach($_FILES[$field]['tmp_name'] as $key => $tmp_name ){
  212. $file_name = $_FILES[$field]['name'][$key];
  213. $file_tmp =$_FILES[$field]['tmp_name'][$key];
  214. if($file_name !='')
  215. {
  216. move_uploaded_file($file_tmp,$destinationPath.'/'.$file_name);
  217. $files .= $file_name.',';
  218. }
  219. }
  220. if($files !='') $files = substr($files,0,strlen($files)-1);
  221. }
  222. $data[$field] = $files;
  223. } else {
  224. if(!is_null(Input::file($field)))
  225. {
  226. $file = Input::file($field);
  227. $destinationPath = '.'. $f['option']['path_to_upload'];
  228. $filename = $file->getClientOriginalName();
  229. $extension =$file->getClientOriginalExtension(); //if you need extension of the file
  230. $rand = rand(1000,100000000);
  231. $newfilename = strtotime(date('Y-m-d H:i:s')).'-'.$rand.'.'.$extension;
  232. $uploadSuccess = $file->move($destinationPath, $newfilename);
  233. if($f['option']['resize_width'] != '0' && $f['option']['resize_width'] !='')
  234. {
  235. if( $f['option']['resize_height'] ==0 )
  236. {
  237. $f['option']['resize_height'] = $f['option']['resize_width'];
  238. }
  239. $orgFile = $destinationPath.'/'.$newfilename;
  240. \SiteHelpers::cropImage($f['option']['resize_width'] , $f['option']['resize_height'] , $orgFile , $extension, $orgFile) ;
  241. }
  242. if( $uploadSuccess ) {
  243. $data[$field] = $newfilename;
  244. }
  245. } else {
  246. unset($data[$field]);
  247. }
  248. }
  249. }
  250. // if post is checkbox
  251. if($f['type'] =='checkbox')
  252. {
  253. if(!is_null($_POST[$field]))
  254. {
  255. $data[$field] = implode(",",$_POST[$field]);
  256. }
  257. }
  258. // if post is date
  259. if($f['type'] =='date')
  260. {
  261. $data[$field] = date("Y-m-d",strtotime($request->input($field)));
  262. }
  263. // if post is seelct multiple
  264. if($f['type'] =='select')
  265. {
  266. //echo '<pre>'; print_r( $_POST[$field] ); echo '</pre>';
  267. if( isset($f['option']['select_multiple']) && $f['option']['select_multiple'] ==1 )
  268. {
  269. $multival = (is_array($_POST[$field]) ? implode(",",$_POST[$field]) : $_POST[$field]);
  270. $data[$field] = $multival;
  271. } else {
  272. $data[$field] = $_POST[$field];
  273. }
  274. }
  275. }
  276. }
  277. }
  278. $global = (isset($this->access['is_global']) ? $this->access['is_global'] : 0 );
  279. if($global == 0 )
  280. $data['entry_by'] = \Session::get('uid');
  281. return $data;
  282. }
  283. function postFilter( Request $request)
  284. {
  285. $module = $this->module;
  286. $sort = (!is_null($request->input('sort')) ? $request->input('sort') : '');
  287. $order = (!is_null($request->input('order')) ? $request->input('order') : '');
  288. $rows = (!is_null($request->input('rows')) ? $request->input('rows') : '');
  289. $md = (!is_null($request->input('md')) ? $request->input('md') : '');
  290. $filter = '?';
  291. if($sort!='') $filter .= '&sort='.$sort;
  292. if($order!='') $filter .= '&order='.$order;
  293. if($rows!='') $filter .= '&rows='.$rows;
  294. if($md !='') $filter .= '&md='.$md;
  295. return Redirect::to($this->data['pageModule'] . $filter);
  296. }
  297. function injectPaginate()
  298. {
  299. $sort = (isset($_GET['sort']) ? $_GET['sort'] : '');
  300. $order = (isset($_GET['order']) ? $_GET['order'] : '');
  301. $rows = (isset($_GET['rows']) ? $_GET['rows'] : '');
  302. $search = (isset($_GET['search']) ? $_GET['search'] : '');
  303. $appends = array();
  304. if($sort!='') $appends['sort'] = $sort;
  305. if($order!='') $appends['order'] = $order;
  306. if($rows!='') $appends['rows'] = $rows;
  307. if($search!='') $appends['search'] = $search;
  308. return $appends;
  309. }
  310. function returnUrl()
  311. {
  312. $pages = (isset($_GET['page']) ? $_GET['page'] : '');
  313. $sort = (isset($_GET['sort']) ? $_GET['sort'] : '');
  314. $order = (isset($_GET['order']) ? $_GET['order'] : '');
  315. $rows = (isset($_GET['rows']) ? $_GET['rows'] : '');
  316. $search = (isset($_GET['search']) ? $_GET['search'] : '');
  317. $appends = array();
  318. if($pages!='') $appends['page'] = $pages;
  319. if($sort!='') $appends['sort'] = $sort;
  320. if($order!='') $appends['order'] = $order;
  321. if($rows!='') $appends['rows'] = $rows;
  322. if($search!='') $appends['search'] = $search;
  323. $url = "";
  324. foreach($appends as $key=>$val)
  325. {
  326. $url .= "&$key=$val";
  327. }
  328. return $url;
  329. }
  330. public function getRemovecurrentfiles( Request $request)
  331. {
  332. $id = $request->input('id');
  333. $field = $request->input('field');
  334. $file = $request->input('file');
  335. if(file_exists('./'.$file) && $file !='')
  336. {
  337. if(unlink('.'.$file))
  338. {
  339. \DB::table($this->info['table'])->where($this->info['key'],$id)->update(array($field=>''));
  340. }
  341. return Response::json(array('status'=>'success'));
  342. } else {
  343. return Response::json(array('status'=>'error'));
  344. }
  345. }
  346. function getDownload( Request $request)
  347. {
  348. if($this->access['is_excel'] ==0)
  349. return Redirect::to('')->with('messagetext',\Lang::get('core.note_restric'))->with('msgstatus','error');
  350. $info = $this->model->makeInfo( $this->module);
  351. // Take param master detail if any
  352. $filter = (!is_null($request->input('search')) ? $this->buildSearch() : '');
  353. $params = array(
  354. 'params' => $filter,
  355. 'global' => (isset($this->access['is_global']) ? $this->access['is_global'] : 0 )
  356. );
  357. $results = $this->model->getRows( $params );
  358. $fields = $info['config']['grid'];
  359. $rows = $results['rows'];
  360. $content = $this->data['pageTitle'];
  361. $content .= '<table border="1">';
  362. $content .= '<tr>';
  363. foreach($fields as $f )
  364. {
  365. if($f['download'] =='1') $content .= '<th style="background:#f9f9f9;">'. $f['label'] . '</th>';
  366. }
  367. $content .= '</tr>';
  368. foreach ($rows as $row)
  369. {
  370. $content .= '<tr>';
  371. foreach($fields as $f )
  372. {
  373. if($f['download'] =='1'):
  374. $conn = (isset($f['conn']) ? $f['conn'] : array() );
  375. $content .= '<td>'. \SiteHelpers::gridDisplay($row->$f['field'],$f['field'],$conn) . '</td>';
  376. endif;
  377. }
  378. $content .= '</tr>';
  379. }
  380. $content .= '</table>';
  381. @header('Content-Type: application/ms-excel');
  382. @header('Content-Length: '.strlen($content));
  383. @header('Content-disposition: inline; filename="'.$title.' '.date("d/m/Y").'.xls"');
  384. echo $content;
  385. exit;
  386. }
  387. function detailview( $model , $detail , $id )
  388. {
  389. $info = $model->makeInfo( $detail['module'] );
  390. $params = array(
  391. 'params' => " And `".$detail['key']."` ='". $id ."'",
  392. 'global' => 0
  393. );
  394. $results = $model->getRows( $params );
  395. $data['rowData'] = $results['rows'];
  396. $data['tableGrid'] = $info['config']['grid'];
  397. $data['tableForm'] = $info['config']['forms'];
  398. return $data;
  399. }
  400. function detailviewsave( $model ,$request , $detail , $id )
  401. {
  402. \DB::table($detail['table'])->where($detail['key'],$request[$detail['key']])->delete();
  403. $info = $model->makeInfo( $detail['module'] );
  404. $str = $info['config']['forms'];
  405. $data = array($detail['master_key'] => $id );
  406. $total = count($request['counter']);
  407. for($i=0; $i<$total;$i++)
  408. {
  409. foreach($str as $f){
  410. $field = $f['field'];
  411. if($f['view'] ==1)
  412. {
  413. //echo 'bulk_'.$field[$i]; echo '<br />';
  414. if(isset($request['bulk_'.$field][$i]))
  415. {
  416. $data[$f['field']] = $request['bulk_'.$field][$i];
  417. }
  418. }
  419. }
  420. \DB::table($detail['table'])->insert($data);
  421. }
  422. }
  423. }