Advertisement
himel29

controller

Jan 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.76 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\OperationActivities;
  4. use Illuminate\Support\Facades\Auth;
  5. use App\Http\Controllers\CommonController;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Input;
  8. use App\Http\Controllers\Controller;
  9.  
  10. use App\AntiSmugling;
  11. use App\ship;
  12. use App\AntiSmuglingCell;
  13. use App\country;
  14. use App\album;
  15. use Yajra\Datatables\Facades\Datatables;
  16. use DateTime;
  17.  
  18. class AntiSmuglingController extends Controller
  19. {
  20.  
  21.     public function __construct()
  22.     {
  23.         $this->middleware('auth');
  24.     }
  25.     public function index()
  26.     {  
  27.         $antiSmuglings = AntiSmugling::where('visible', 1)
  28.         ->orderBy('id', 'desc')
  29.         ->get();
  30.         $ship_cell_dropdown = AntiSmugling::groupBy('ship_cell')->orderBy('ship_cell', 'asc')->get();
  31.         $countrys = country::orderBy('country_name', 'asc')->get();
  32.         return view('OperationActivities.antiSmugling.index', compact('antiSmuglings', 'ship_cell_dropdown', 'countrys'));
  33.     }
  34.  
  35.     public function anyData(Request $request)
  36.     {
  37.    
  38.      $antiSmuglings = Datatables::of(AntiSmugling::with('getCountryInfoRow')
  39.         ->where('visible', 1)
  40.         ->orderBy('id', 'desc'))
  41.         ->addColumn('action', function ($row) {
  42.             $album = album::where('type', 'smuggling')->where('type_id', $row->id)->first();
  43.                 if($album && \Illuminate\Support\Facades\Auth::User()->user_type == 'admin')
  44.                  return '<a href="/antiSmugling/'.$row->id.'" class="btn btn-edit btn-xs" style="margin-Bottom:5px;"><i class="fa fa-eye"></i> View </a>
  45.                       <a href="/antiSmugling/'.$row->id.'/edit" class="btn btn-edit btn-xs"><i class="fa fa-edit"></i> Edit </a>
  46.                       <button onclick="confirmDelete(this);" type="button" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i> Delete</button>
  47.                        <a href="imageGallery/' .$album->id . '"><button class="btn btn-edit"><i class="fa fa-image"></i>View Gallery</button></a>';
  48.  
  49.                 if($album && \Illuminate\Support\Facades\Auth::User()->user_type != 'admin')
  50.                  return '<a href="/antiSmugling/'.$row->id.'" class="btn btn-edit btn-xs" style="margin-Bottom:5px;"><i class="fa fa-eye"></i> View </a>
  51.                        <a href="imageGallery/' .$album->id . '"><button class="btn btn-edit"><i class="fa fa-image"></i>View Gallery</button></a>';    
  52.  
  53.                 if(\Illuminate\Support\Facades\Auth::User()->user_type == 'admin')
  54.                 return '<a href="/antiSmugling/'.$row->id.'" class="btn btn-edit btn-xs" style="margin-Bottom:5px;"><i class="fa fa-eye"></i> View </a>
  55.                       <a href="/antiSmugling/'.$row->id.'/edit" class="btn btn-edit btn-xs"><i class="fa fa-edit"></i> Edit </a>
  56.                       <button onclick="confirmDelete(this);" type="button" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i> Delete</button>';
  57.                
  58.                 elseif(\Illuminate\Support\Facades\Auth::User()->user_type != 'admin')
  59.                    return '<a href="/antiSmugling/'.$row->id.'" class="btn btn-edit btn-xs" style="margin-Bottom:5px;"><i class="fa fa-eye"></i> View </a>';
  60.                  
  61.  
  62.             })
  63.             ->addColumn('country', function($m){
  64.                 return $m->country ? $m->getCountryInfoRow->country_name : '';
  65.             })
  66.             ->editColumn('from_date', function($m){
  67.                 return $m->from_date ? date_format(new DateTime($m->from_date), "d M Y") : '';
  68.             })
  69.             ->editColumn('to_date', function($m){
  70.                 return $m->to_date ? date_format(new DateTime($m->to_date), "d M Y") : '';
  71.             })
  72.             ->editColumn('date', function($m){
  73.                 return $m->date ? date_format(new DateTime($m->date), "d M Y") : '';
  74.             });
  75.  
  76.             return $antiSmuglings->addIndexColumn()
  77.                                  ->filter(function ($query) use ($request) {
  78.             if ($request->has('ship_cell')) {
  79.                 $query->where('ship_cell', 'like', "%{$request->get('ship_cell')}%");
  80.             }
  81.             if ($request->has('operation_name')) {
  82.                 $query->where('operation_name', 'like', "%{$request->get('operation_name')}%");
  83.             }
  84.             if ($request->has('country')) {
  85.                 $query->where('country', '=', $request->get('country'));
  86.             }
  87.             if ($request->has('from_date')) {
  88.                 $query->whereDate('from_date', '>=', date('Y-m-d', strtotime($request->from_date)) );
  89.             }
  90.             if ($request->has('to_date')) {
  91.                 $query->whereDate('to_date', '<=', date('Y-m-d', strtotime($request->to_date)) );
  92.             }
  93.             if ($request->has('date')) {
  94.                 $query->whereDate('date', '=', date('Y-m-d', strtotime($request->date)) );
  95.             }
  96.         }, true)
  97.                                    ->make(true);
  98.     }
  99.  
  100.  
  101.      public function search(Request $request){
  102.         $q = AntiSmugling::query();
  103.         $q->where('visible', 1);
  104.         if(Input::has('ship_cell')){
  105.             $q->where('ship_cell', 'LIKE', '%' . Input::get('ship_cell') . '%');
  106.         }
  107.  
  108.         if(Input::has('operation_name')){
  109.             $q->where('operation_name', 'LIKE', '%' . Input::get('operation_name') . '%');
  110.         }
  111.  
  112.         if(Input::has('from_date')){
  113.             $q->whereDate('from_date', '>=', $request->from_date);
  114.             //$q->whereDate('to_date', '>=', $request->from_date);
  115.         }
  116.         if(Input::has('to_date')){
  117.             //$q->whereDate('from_date', '<=', $request->to_date);
  118.             $q->whereDate('to_date', '<=', $request->to_date);
  119.         }
  120.  
  121.         if(Input::has('date')){
  122.             //$q->whereDate('from_date', '<=', $request->to_date);
  123.             $q->whereDate('date', '=', $request->date);
  124.         }
  125.  
  126.         if(Input::has('country')){
  127.             //$q->whereDate('from_date', '<=', $request->to_date);
  128.             $q->where('country', '=', $request->country);
  129.         }
  130.  
  131.  
  132.         $antiSmuglings = $q->get();
  133.          $ship_cell_dropdown = AntiSmugling::groupBy('ship_cell')->orderBy('ship_cell', 'asc')->get();
  134.         $countrys = country::orderBy('country_name', 'asc')->get();
  135.         return view('OperationActivities.antiSmugling.index', compact('antiSmuglings', 'ship_cell_dropdown', 'countrys'));
  136.  
  137.     }
  138.  
  139.     public function create()
  140.     {  
  141.         if(Auth::User()->user_type != 'admin'){
  142.             return redirect()->back();
  143.         }  
  144.          $ship_dropdown = ship::orderBy('ship_name', 'asc')
  145.          ->where('commissioned', 1)
  146.          ->where('office_type', 'Ship')
  147.          //->where('is_active', 1)
  148.          ->where('visible', 1)
  149.          ->get();
  150.          $countrys = country::all();
  151.          $cell_dropdown = AntiSmuglingCell::all();
  152.          $ship_id = '';
  153.          if (isset($_GET['ship_id'])) {
  154.             $ship_id = $_GET['ship_id'];
  155.         }
  156.          return view('OperationActivities.antiSmugling.create', compact('ship_dropdown', 'cell_dropdown', 'countrys', 'ship_id'));
  157.     }
  158.  
  159.  
  160.     public function store(Request $request)
  161.     {
  162.         if(Auth::User()->user_type != 'admin'){
  163.             return redirect()->back();
  164.         }
  165.             $response= AntiSmugling::create($request->all());        
  166.             if(Input::has('from_date')){
  167.              $response->from_date =  date('Y-m-d', strtotime($request->from_date));  
  168.             }
  169.             if(Input::has('to_date')){
  170.              $response->to_date =  date('Y-m-d', strtotime($request->to_date));  
  171.             }
  172.             if(Input::has('date')){
  173.              $response->date =  date('Y-m-d', strtotime($request->date));  
  174.             }
  175.            
  176.             if(Input::has('duration')){
  177.                  $response->duration = $request->duration;
  178.             }
  179.            
  180.            if(Input::has('number_of_pirates')){
  181.                 $response->number_of_pirates = str_replace(',', '', $request->number_of_pirates);
  182.            }
  183.            if(Input::has('number_of_boats')){
  184.                 $response->number_of_boats = str_replace(',', '', $request->number_of_boats);
  185.            }
  186.            if(Input::has('number_of_smuggler')){
  187.                 $response->number_of_smuggler = str_replace(',', '', $request->number_of_smuggler);
  188.            }
  189.            
  190.            if(Input::has('quantity_of_product')){
  191.                 $response->quantity_of_product = str_replace(',', '', $request->quantity_of_product);
  192.            }
  193.            if(Input::has('estimated_value_product')){
  194.                 $response->estimated_value_product = str_replace(',', '', $request->estimated_value_product);
  195.            }
  196.            if(Input::has('name_of_other_product')){
  197.                 $response->name_of_other_product = $request->name_of_other_product;
  198.            }          
  199.             $response->save();
  200.            
  201.             if(isset($request->img)){
  202.             CommonController::createActivityAlbum('smuggling', $response->id, $request->file('img'), $response->from_date, NULL);
  203.                 }
  204.             session()->flash('success', 'Successfully Submitted.');
  205.             return redirect('antiSmugling');
  206.     }
  207.  
  208.     public function show($id)
  209.     {
  210.         $antiSmugling = AntiSmugling::find($id);        
  211.          // $ship_dropdown = ship::select('id', 'ship_name')->where('commissioned', 1)->get();
  212.          // $cell_dropdown = AntiSmuglingCell::all();
  213.          // $countrys = country::all();
  214.         return view('OperationActivities.antiSmugling.show', compact('antiSmugling'));
  215.     }
  216.  
  217.     public function edit($id)
  218.     {  
  219.         if(Auth::User()->user_type != 'admin'){
  220.             return redirect()->back();
  221.         }
  222.          $antiSmugling = AntiSmugling::find($id);
  223.          $ship_dropdown = ship::orderBy('ship_name', 'asc')
  224.          ->where('commissioned', 1)
  225.          ->where('office_type', 'Ship')
  226.          //->where('is_active', 1)
  227.          ->where('visible', 1)
  228.          ->get();
  229.          $cell_dropdown = AntiSmuglingCell::all();
  230.          $countrys = country::all();
  231.          return view('OperationActivities.antiSmugling.edit', compact('antiSmugling', 'ship_dropdown', 'cell_dropdown', 'countrys'));
  232.     }
  233.  
  234.     public function update(Request $request, $id)
  235.     {
  236.         if(Auth::User()->user_type != 'admin'){
  237.             return redirect()->back();
  238.         }
  239.  
  240.         $response = AntiSmugling::find($id);
  241.            if(Input::has('from_date')){
  242.              $response->from_date =  date('Y-m-d', strtotime($request->from_date));  
  243.             }
  244.             if(Input::has('to_date')){
  245.              $response->to_date =  date('Y-m-d', strtotime($request->to_date));  
  246.             }
  247.             if(Input::has('date')){
  248.              $response->date =  date('Y-m-d', strtotime($request->date));  
  249.             }
  250.            
  251.             if(Input::has('duration')){
  252.                  $response->duration = $request->duration;
  253.             }
  254.             if(Input::has('number_of_smuggler')){
  255.                 $response->number_of_smuggler = str_replace(',', '', $request->number_of_smuggler);
  256.             }
  257.            
  258.            if(Input::has('number_of_pirates')){
  259.                 $response->number_of_pirates = str_replace(',', '', $request->number_of_pirates);
  260.            }
  261.            if(Input::has('number_of_boats')){
  262.                 $response->number_of_boats = str_replace(',', '', $request->number_of_boats);
  263.            }
  264.            
  265.        
  266.            if(Input::has('quantity_of_product')){
  267.                 $response->quantity_of_product = str_replace(',', '', $request->quantity_of_product);
  268.            }
  269.            if(Input::has('estimated_value_product')){
  270.                 $response->estimated_value_product = str_replace(',', '', $request->estimated_value_product);
  271.            }
  272.            if(Input::has('name_of_other_product')){
  273.                 $response->name_of_other_product = $request->name_of_other_product;
  274.            }            
  275.         $response->update($request->all());
  276.         $response->save();
  277.         if(isset($request->img)){
  278.             CommonController::createActivityAlbum('smuggling', $response->id, $request->file('img'), $response->from_date, NULL);
  279.                 }
  280.         session()->flash('success', 'Successfully Updated.');
  281.  
  282.  
  283.         return redirect('antiSmugling');
  284.     }
  285.  
  286.  
  287.     public function destroy($id)
  288.     {
  289.         if(Auth::User()->user_type != 'admin'){
  290.             return redirect()->back();
  291.         }
  292.         $response = AntiSmugling::find($id);
  293.         $response ->visible = 0;
  294.         $response ->save();
  295.         return redirect()->back();
  296.     }
  297.  
  298.     public function getShipCell(Request $request)
  299.     {
  300.         $value = $request->input('value');
  301.  
  302.         if($value == 'ship')
  303.         {
  304.             $getShip = ship::orderBy('ship_name', 'asc')
  305.                        ->where('commissioned', 1)
  306.                        ->where('office_type', 'Ship')
  307.                        ->get();
  308.             echo '<option value="">---Please Select---</option>';
  309.             foreach ( $getShip as $dropdown ):
  310.                 $val = $dropdown->ship_name;
  311.                 $name = $dropdown->ship_name;
  312.                 echo '<option value="' . $val . '">' . $name . '</option>';
  313.             endforeach;
  314.         }
  315.        
  316.         else if($value == 'cell')
  317.         {    
  318.             $getCell = AntiSmuglingCell::all();
  319.             echo '<option value="">---Please Select---</option>';
  320.             foreach ( $getCell as $dropdown ):
  321.                 $val = $dropdown->name;
  322.                 $name = $dropdown->name;
  323.                 echo '<option value="' . $val . '">' . $name . '</option>';
  324.             endforeach;
  325.         }
  326.        
  327.         else {
  328.             echo '<option value = "">No data found</option>';
  329.         }
  330.        
  331.         echo '</select>';
  332.     }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement