123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
-
- namespace App\Http\Controllers\Admin\Raharja;
-
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Http\Request;
- use App\Helpers\Helper;
- use App\Model\Raharja\News;
- use Yajra\Datatables\Datatables;
- use Auth;
- use Illuminate\Support\Facades\Storage;
- use Validator;
- use Hashids;
-
- //load modelmu
- class NewsController extends Controller
- {
- /**
- * Title untuk judul di web
- * route digunakan untuk tempat resource (file path) + routing (route/web) diusahain sama ya biar gak ngubah"
- */
- private $title = 'News'; /**jangan lupa diganti*/
- private $route = 'admin.news.'; //path awal foldernya ajah (misal folder di admin/dashboard) => 'admin.dashboard' | jangan lupa diganti
- private $header = 'News';
- private $sub_header = 'News';
- private $url = 'admin.raharja.news.';
-
- public function __construct()
- {
- DB::enableQueryLog();
- /** nyalakan jika sudah set rolenya, jika ini dinyalakan halaman ini tidak akan keluar */
- // $this->middleware('permission:searching-list|searching-create|searching-update|searching-delete', ['only' => ['index', 'create', 'update']]);
- // $this->middleware('permission:searching-create', ['only' => ['create', 'create_action']]);
- // $this->middleware('permission:searching-update', ['only' => ['update', 'update_action']]);
- // $this->middleware('permission:searching-delete', ['only' => ['delete']]);
- }
-
- /**
- * Ini contoh crud yang sudah jalan
- * index digunakna untuk tampilan awal dari menu yang akan dibuat
- * Untuk CRUD Biar cepat
- * Silahkan ganti
- * EloquentMu:: => dengan model anda
- * $id_modelmu => ganti dengan id di model anda
- * id_di_routenya => jangan lupa disesuaikan dengan yang dideclare di route
- */
-
-
- public function index()
- {
- Helper::swal();
-
- $data = [
- //bawaan
- 'title' => $this->title,
- 'route' => $this->route,
- 'header' => $this->header,
- 'sub_header' => $this->sub_header,
- ];
- // dd($data);
- return view($this->url . 'index', $data);
- }
- public function getData($udd_id = null)
- {
- $query = News::query();
-
- return Datatables::of($query)
- ->addIndexColumn()
- ->addColumn('count_like', function($query){
- return count($query->newsLikes);
- })
- ->addColumn('aksi', function ($query) {
-
- /**cek role */
- $aksi = '';
- // if (Auth::user()->can('master-jenis-kantong-update')) {
- $aksi .= "<a href='" . route($this->route . 'update', ['news_id' => Hashids::encode($query->id)]) . "' class='btn btn-sm btn-primary btn-edit'>Edit</a>";
- // }
-
- // if (Auth::user()->can('master-jenis-kantong-delete')) {
- $aksi .= "<a href='javascript:;' data-route='" . route($this->route . 'delete_action', ['news_id' => Hashids::encode($query->id)]) . "' class='btn btn-danger btn-sm btn-delete'>Delete</a>";
- // }
- return $aksi;
- })
- ->rawColumns(['aksi'])
- ->toJson();
- }
- public function create()
- {
- $data = [
- //bawaan
- 'title' => $this->title,
- 'route' => $this->route,
- 'header' => $this->header,
- 'sub_header' => $this->sub_header,
-
- ];
-
- Helper::swal();
-
- return view($this->url . 'create', $data);
- }
-
- public function create_action(Request $request)
- {
- //declare post untuk simpan semua request yang diinputkan oleh form name=""
- $post = $request->all();
- $file = $request->file('gambar');
- //dd($post);
- /**digunakan untuk set rule validator */
- $rules = [
- 'title' => 'required',
- 'gambar' => 'required|file|max:1000',
- 'content' => 'required',
- 'sub_title' => 'required',
- 'date_publish' => 'required',
- ];
- /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
- $alert = [
- 'file' => 'File maksimal 1MB'
- ];
- $validator = Validator::make($post, $rules, $alert);
-
- if ($validator->passes()) {
- /**menggunakan transaction */
- DB::beginTransaction();
-
- $nama_file = time()."_".$file->getClientOriginalName();
-
- $tujuan_upload = 'gambar';
-
- //$file->move($tujuan_upload,$nama_file);
- Storage::putFileAs('public/images/news/', $file, $nama_file );
-
- $query = new News();
- $query->title = $request->title;
- $query->content = Helper::input_summernote($request->content);
- $query->sub_title = Helper::input_summernote($request->sub_title);
- $query->date_publish = $request->date_publish;
- $query->user_id = Auth::user()->id;
- $query->thumbnail = $nama_file;
- $query->save();
-
- if ($query) {
- DB::commit();
- $message = 'Berhasil';
- return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
- } else {
- DB::rollback();
- $message = 'Gagal';
- return redirect()->back()->with('error', Helper::parsing_alert($message));
- }
- }
- /**kenapa menggunakan back ? karena baliknya pasti ke halaman sebelumnya */
- $message = Helper::parsing_alert($validator->errors()->all());
- return redirect()->back()->with('error', $message)->withInput();
- }
-
- public function update($data_id)
- {
-
- Helper::swal();
-
-
- $data_id = Hashids::decode($data_id);
- if (!empty($data_id)) {
-
- $cek_data = News::where('id', $data_id[0])->first();
-
- if ($cek_data) {
- $data = [
- //bawaan
- 'title' => $this->title,
- 'route' => $this->route,
- 'header' => $this->header,
- 'sub_header' => $this->sub_header,
- 'data' => $cek_data
- ];
- return view($this->url . 'update', $data);
- }
- $message = 'Id tidak ditemukan atau sudah dihapus';
- return redirect()->back()->with('error', $message);
- }
- $message = 'Id tdak ditemukan';
- return redirect()->back()->with('error', $message);
- }
-
- public function update_action(Request $request)
- {
- $post = $request->all();
- /**digunakan untuk set rule validator */
- $rules = [
- 'title' => 'required',
- 'content' => 'required',
- 'sub_title' => 'required',
- 'date_publish' => 'required',
- ];
- /**digunakan untuk set message dari validatornya yang akan keluar gimna, :attribue itu udah langsung mendeteksi inputtan dari name="attribue" */
- $alert = [];
- $validator = Validator::make($post, $rules, $alert);
-
- $data_id = Hashids::decode($request['id']);
-
- if (!empty($data_id)) {
- /**cek apakah data_idnya ada ? */
- $query = News::find($data_id[0]);
-
- if ($query) {
- /**cek apakah id data benar" ada di DB ? */
- if ($validator->passes()) {
- /**validatornya */
- DB::beginTransaction();
- $file = $request->file('gambar');
- if($file){
- $nama_file = time()."_".$file->getClientOriginalName();
- $tujuan_upload = 'gambar';
- //$file->move($tujuan_upload,$nama_file);
- Storage::putFileAs('public/images/news/', $file, $nama_file );
- //File::delete('gambar/'.$query->gambar);
- Storage::delete('public/images/news/'.$query->thumbnail);
- $query->thumbnail = $nama_file;
- }
-
- $query->title = $request->title;
- $query->content = Helper::input_summernote($request->content);
- $query->sub_title = Helper::input_summernote($request->sub_title);
- $query->date_publish = $request->date_publish;
- $query->save();
-
- if ($query) {
- DB::commit();
- $message = 'Berhasil';
- return redirect(route($this->route . 'index'))->with('success', Helper::parsing_alert($message));
- } else {
- DB::rollback();
- $message = 'Gagal';
- return redirect()->back()->with('error', Helper::parsing_alert($message));
- }
- }
-
- $message = Helper::parsing_alert($validator->errors()->all());
-
- return redirect()->back()->with('error', $message);
- } else {
- $message = 'Id tdak ditemukan';
- return redirect()->back()->with('error', $message);
- }
- } else {
- $message = 'Id tidak boleh kosong';
- return redirect()->back()->with('error', $message);
- }
- }
-
- public function delete_action($id_modelmu)
- {
- $id = Hashids::decode($id_modelmu);
- /**cek idnya apakah ada ? */
-
- if (!empty($id)) {
-
- $cek_data = News::find($id[0]);
-
- if ($cek_data) {
- DB::beginTransaction();
- try {
- //File::delete('gambar/'.$cek_data->gambar);
- $arr_likes = $cek_data->newsLikes;
- foreach($arr_likes as $like){
- $like->delete();
- }
- Storage::delete('public/images/news/'.$cek_data->thumbnail);
- $delete = $cek_data->delete();
- DB::commit();
- $message = 'Sukses';
- $response = [
- 'message' => $message,
- 'status' => true,
- ];
- return response()->json($response);
- } catch (Exception $e) {
- DB::rollback();
- $message = 'Gagal';
- $response = [
- 'message' => $message,
- 'status' => false,
- ];
- return response()->json($response);
- }
- }
- $message = 'Id tidak ditemukan atau sudah dihapus';
- $response = [
- 'message' => $message,
- 'status' => false,
- ];
- return response()->json($response);
- }
- $message = 'Id tdak ditemukan';
- $response = [
- 'message' => $message,
- 'status' => false,
- ];
- return response()->json($response);
- }
-
- }
|