Skip to content

Instantly share code, notes, and snippets.

@PetengDedet PetengDedet/api
Created Nov 24, 2016

Embed
What would you like to do?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use Hash;
use Input;
use Image;
use File;
use App\User;
use App\Library\CekToken;
use DB;
class ApiController extends Controller
{
public function apiPostLogin(Request $request)
{
/*-----------------------------------------------
| STEP 1
| Validate the input
*/
$validator = Validator::make($request->all(), [
'username' => 'required|min:3|max:255',
'password' => 'required',
'cloud_id' => 'required'
]);
if ($validator->fails()) {
return response()->json(['status' => false, 'data'=> '', 'pesan' => 'Username atau password tidak benar. Jangan lupa kirimkan cloud id.']);
}
/*------------
| STEP 2
| Try to check the given input in local database record
| If the is already registered, then redirect to dashboard
| Otherwise, Step 3
*/
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials, $request->has('remember'))) {
$token = CekToken::gen($request->username, $request->cloud_id);
$user = User::where('username', $request->username)->first();
$user->cover_photo = \App\Library\CustomLibrary::ambilHeaderCoy($user->id);
$user->photo_profile = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
return response()->json(['status'=>true, 'data'=> $user, 'pesan' => '', 'token' => $token]);
}else{
$adServer = "ldap://corp.ojk.go.id";
$ldap = @ldap_connect($adServer);
if (!$ldap) {
return response()->json(['status'=>false, 'data'=> '', 'pesan'=>'Tidak dapat tersambung dengan LDAP']);
}
$ldaprdn = 'corp' . "\\" . $request->username;
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = @ldap_bind($ldap, $ldaprdn, $request->password);
if ($bind) {
//Cek data user di dalam database
$checkInside = User::where('username',$request->username)->first();
//User sudah terdaftar di database
if ($checkInside) {
Auth::login($checkInside);
$checkInside->cover_photo = \App\Library\CustomLibrary::ambilHeaderCoy($checkInside->id);
$checkInside->photo_profile = \App\Library\CustomLibrary::ambilGambarCoy($checkInside->id);
$token = CekToken::gen($request->username, $request->cloud_id);
//Respon request
return response()->json(['status' => true, 'data'=> $checkInside, 'pesan' => '', 'token' => $token]);
}
//User belum tercatat di database
//Ambil credential user dari LDAP
$filter = "(sAMAccountName=" . $request->username . ")";
$result = @ldap_search($ldap,"OU=User Accounts,OU=Users OJK,DC=corp,DC=ojk,DC=go,DC=id",$filter);
//Credential tidak valid
if (! $result) {
return response()->json(['status' => false, 'data'=> '', 'pesan' => 'Can not query LDAP']);
}
//Credential valid
//Ambil data LDAP
$info = @ldap_get_entries($ldap, $result);
if (! $info) {
return response()->json(['status' => false, 'data'=> '', 'pesan' => 'Can not get user from LDAP']);
}
//Buat user baru di database sesuai data LDAP
$user = new User();
$user->status = "Default Status User";
$user->city = "jakarta";
$dname = @explode(' ',$info[0]['cn'][0]);
$user->first_name = $dname[0];
$user->last_name = (array_key_exists(1, $dname)) ? $dname[1] : '';
$user->email = $info[0]['mail'][0];
$usrnm = explode('@', $info[0]['mail'][0]);
$user->username = $usrnm[0];
$user->password = Hash::make($request->password);
$user->name = $info[0]['cn'][0];
$user->last_login_date = "2016-08-17 00:00:00";
$user->enabled = 1;
$user->auth_type = 'internal';
$user->role = 2;
$user->save();
$user->cover_photo = \App\Library\CustomLibrary::ambilHeaderCoy($user->id);
$user->photo_profile = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
//Buat Role default
DB::table('role_user')->insert(['user_id'=>$user->id, 'role_id'=>'2']);
//Ambil data SIMFOSIA
$url = 'http://10.208.125.36/webservices/rest/?method=MasterDataPegawaiPOINTER&format=xml&pin=d2F5&email=' . $info[0]['mail'][0];
$xml = @simplexml_load_file($url) or die("Can not read simfosia feed");
//Gagal menghubungi data simfosia
if(! $xml = @simplexml_load_file($url)) {
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not connect to simfosia'], 500);
}
//Simfosia merespon tidak baik
if($xml->response->total_seluruh_data < 1){
return response()->json(['status' => false, 'data' => '', 'message' => 'Tidak dapat menghubungi simfosia'], 200);
}
//Respon simfosia sesuai harapan
$simDat = $xml->response->data->record[0];
//Update user yang baru dibuat sesuai data dari Simfosia
$user = \App\User::find($user->id);
$user->email = $simDat->email;
$user->nip = $simDat->nip;
$user->name = $simDat->nm_peg;
$user->nm_jabatan = $simDat->nm_jabatan;
$user->designation = $simDat->nm_jabatan;
$user->kd_unit_kerja = $simDat->kd_unit_kerja;
$user->nm_unit_kerja = $simDat->nm_unit_kerja;
$user->kd_bidang = $simDat->kd_bidang;
$user->nm_bidang = $simDat->nm_bidang;
$user->kd_deputi_komisioner = $simDat->kd_deputi_komisioner;
$user->nm_deputi_komisioner = $simDat->nm_deputi_komisioner;
$user->kd_deputi_direktur = $simDat->kd_deputi_direktur;
$user->nm_deputi_direktur = $simDat->nm_deputi_direktur;
$user->kd_bagian = $simDat->kd_bagian;
$user->nm_sbagian = $simDat->nm_bagian;
$user->kd_subbagian = $simDat->kd_subbagian;
$user->nm_subbagian = $simDat->nm_subbagian;
$user->kd_kantor = $simDat->kd_kantor;
$user->nm_kantor = $simDat->nm_kantor;
$user->phone_kantor = $simDat->phone_kantor;
$user->phone_kantor_ext = $simDat->phone_kantor_ext;
$user->dob = $simDat->tgl_lahir;
$user->kd_departemen = $simDat->kd_departemen;
$user->nm_departemen = $simDat->nm_departemen;
$user->save();
$user->cover_photo = \App\Library\CustomLibrary::ambilHeaderCoy($user->id);
$user->photo_profile = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
//Daftarkan user ke Satuan Kerjanya
$satkerMember = new SatkerMember();
$satkerMember->user_id = $user->id;
$satkerMember->nama_departemen = $user->nm_departemen;
$satkerMember->save();
$token = CekToken::gen($request->username, $request->cloud_id);
return response()->json(['status'=>true, 'data'=> $user, 'pesan' => '', 'token' => $token]);
@ldap_close($ldap);
} else {
return response()->json(['status'=>false, 'data'=> '', 'pesan'=>'Username atau Password tidak cocok.']);
}
}
}
public function apiPostLogout(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
//Hapus Token
$api_token = \App\ApiToken::where('token', $request->token)->delete();
return response()->json(['status'=>true, 'data'=> '', 'pesan'=>'Berhasil logout']);
}
public function profile(Request $request)
{
//Validasi token
$userdata = Cektoken::cek($request->token);
$data = [];
//Token valid
$data['user_detail'] = $userdata['data'];
$data['user_detail']['photo_profile'] = \App\Library\CustomLibrary::ambilGambarCoy($userdata['data']['id']);
$data['user_detail']['cover_photo'] = \App\Library\CustomLibrary::ambilHeaderCoy($userdata['data']['id']);
//Ambil timeline user bersangkutan
$posts = \App\Models\ActivityAction::where('activity_type', 'status')->where('object_type', 'user')->where('user_id', $userdata['data']['id'])->orderBy('action_id', 'DESC')->take(20)->skip(0)->get();
//Ada post di timeline YBS
if ($posts) {
foreach ($posts as $k => $v) {
//Post telah di-like oleh YBS
$is_liked = \App\Models\ActivityLikes::where('action_id', $v->action_id)->where('user_id', $userdata['data']['id'])->first();
$data['post'][$k]['id'] = $v->action_id;
$data['post'][$k]['text'] = $v->body;
$data['post'][$k]['image'] = null;
$data['post'][$k]['comment_count'] = \App\Models\ActivityComment::where('action_id', $v->action_id)->count();
$data['post'][$k]['like_count'] = \App\Models\ActivityLikes::where('action_id', $v->action_id)->count();
$data['post'][$k]['is_liked'] = ($is_liked) ? '1' : '0';
$data['post'][$k]['timestamp'] = $v->date_added;
//Post mengandung gambar?
$postImg = \App\Models\ImagesTable::where('parent_type', 'user_status')->where('parent_id', $v->action_id)->first();
//Mengandung gambar
if ($postImg) {
$data['post'][$k]['image'] = $postImg->path;
}
//Ambil nama, dan url user pembuat POST
$user = User::select('id', 'first_name', 'last_name')->where('id', '=', $v->user_id)->first();
$photo = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['post'][$k]['user']['id'] = $user->id;
$data['post'][$k]['user']['first_name'] = $user->first_name;
$data['post'][$k]['user']['last_name'] = $user->last_name;
$data['post'][$k]['user']['photo_profile'] = $photo;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function profileOrangLain($id, Request $request)
{
//Validasi Token
$userdata = Cektoken::cek($request->token);
$data = [];
//Cari user
$user = User::find($id);
//User tidak ditemukan
if (!$user) {
return response()->json(['status' => false, 'data' => [], 'message' => 'User tidak ditemukan'], 404);
}
//User ditemukan
$data['user_detail'] = $user;
$data['user_detail']['photo_profile'] = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['user_detail']['cover_photo'] = \App\Library\CustomLibrary::ambilHeaderCoy($user->id);
//Ambil timeline user bersangkutan
$posts = \App\Models\ActivityAction::where('activity_type', 'status')->where('user_id', $user->id)->orderBy('action_id', 'DESC')->take(20)->skip(0)->get();
//Ada post di timeline YBS
if ($posts) {
foreach ($posts as $k => $v) {
$is_liked = \App\Models\ActivityLikes::where('action_id', $v->action_id)->where('user_id', $userdata['data']['id'])->first();
$data['post'][$k]['id'] = $v->action_id;
$data['post'][$k]['text'] = $v->body;
$data['post'][$k]['image'] = null;
$data['post'][$k]['comment_count'] = \App\Models\ActivityComment::where('action_id', $v->action_id)->count();
$data['post'][$k]['like_count'] = \App\Models\ActivityLikes::where('action_id', $v->action_id)->count();
$data['post'][$k]['is_liked'] = ($is_liked) ? '1' : '0';
$data['post'][$k]['timestamp'] = $v->date_added;
$postImg = \App\Models\ImagesTable::where('parent_type', 'user_status')->where('parent_id', $v->action_id)->first();
if ($postImg) {
$data['post'][$k]['image'] = $postImg->path;
}
$user = User::select('id', 'first_name', 'last_name')->where('id', '=', $v->user_id)->first();
$photo = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['post'][$k]['user']['id'] = $user->id;
$data['post'][$k]['user']['first_name'] = $user->first_name;
$data['post'][$k]['user']['last_name'] = $user->last_name;
$data['post'][$k]['user']['photo_profile'] = $photo;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function updateProfile(Request $request)
{
$userdata = Cektoken::cek($request->token);
//Initiate user model
$user = User::find($userdata['data']['id']);
//Get XML from simfosia
$url = 'http://10.208.125.36/webservices/rest/?method=MasterDataPegawaiPOINTER&format=xml&pin=d2F5&email=' . $userdata['data']['email'];
//Failed while connecting to simfosia
if(! $xml = @simplexml_load_file($url)) {
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not connect to simfosia'], 500);
}
//Simfosia return empty or invalid response
if($xml->response->total_seluruh_data < 1){
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not read simfosia feed'], 200);
}
//Parse XML to PHP Object
$simDat = $xml->response->data->record[0];
//Apply parsed XML to User Object
$user->email = $simDat->email;
$user->nip = $simDat->nip;
$user->name = $simDat->nm_peg;
$user->nm_jabatan = $simDat->nm_jabatan;
$user->kd_unit_kerja = $simDat->kd_unit_kerja;
$user->nm_unit_kerja = $simDat->nm_unit_kerja;
$user->kd_bidang = $simDat->kd_bidang;
$user->nm_bidang = $simDat->nm_bidang;
$user->kd_deputi_komisioner = $simDat->kd_deputi_komisioner;
$user->nm_deputi_komisioner = $simDat->nm_deputi_komisioner;
$user->kd_deputi_direktur = $simDat->kd_deputi_direktur;
$user->nm_deputi_direktur = $simDat->nm_deputi_direktur;
$user->kd_bagian = $simDat->kd_bagian;
$user->nm_sbagian = $simDat->nm_bagian;
$user->kd_subbagian = $simDat->kd_subbagian;
$user->nm_subbagian = $simDat->nm_subbagian;
$user->kd_kantor = $simDat->kd_kantor;
$user->nm_kantor = $simDat->nm_kantor;
$user->phone_kantor = $simDat->phone_kantor;
$user->phone_kantor_ext = $simDat->phone_kantor_ext;
$user->dob = $simDat->tgl_lahir;
//Save user
if(! $user->save()) {
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not save user'], 200);
}
//Assign user to Satker
$membership1 = \App\Models\SatkerMember::where('user_id', $user->id)->where('nama_departemen', $user->nm_departemen)->count();
if ($membership1 < 1) {
$satkerMember = new SatkerMember();
$satkerMember->user_id = $user->id;
$satkerMember->nama_departemen = $user->nm_departemen;
if(! $satkerMember->save()) {
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not asssign user to Satker'], 200);
}
}
$membership2 = \App\Models\SatkerMember::where('user_id', $user->id)->where('nama_departemen', $user->nm_deputi_direktur)->count();
if ($membership2 < 1) {
$satkerMember = new SatkerMember();
$satkerMember->user_id = $user->id;
$satkerMember->nama_departemen = $user->nm_deputi_direktur;
$satkerMember->save();
if(! $satkerMember->save()) {
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not asssign user to Satker'], 200);
}
}
return response()->json(['status' => true, 'data' => '', 'message' => 'Berhasil memperbaharui data dari simfosia'], 200);
}
public function ubahProfile(Request $request)
{
$userdata = Cektoken::cek($request->token);
//Initiate user model
$user = User::find($userdata['data']['id']);
$user->about_me = $request->about_me;
$user->status = $request->status;
//Save user
if(! $user->save()) {
return response()->json(['status' => false, 'data' => $userdata, 'message' => 'Can not save user'], 200);
}
//Upload Android
if ($request->has('base64Avatar')) {
\App\Models\ImagesTable::where('parent_type', 'user_avatar_image')->where('parent_id', $user->id)->delete();
$destinationPath = 'images/avatar/';
$fileName = uniqid().'.png';
$img = $request->base64Avatar;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $user->id;
$imageTable->parent_type = 'user_avatar_image';
$imageTable->path = $fileName;
$imageTable->save();
}
//IOS
if ($request->hasFile('avatar')) {
\App\Models\ImagesTable::where('parent_type', 'user_avatar_image')->where('parent_id', $user->id)->delete();
$intervention = Image::make($request->file('avatar'));
$fileName = str_random(10) . '.png';
$destinationPath = 'images/avatar';
$intervention->save($destinationPath.'/'.$fileName);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $user->id;
$imageTable->parent_type = 'user_avatar_image';
$imageTable->path = $fileName;
$imageTable->save();
}
return response()->json(['status' => true, 'data' => '', 'message' => 'Berhasil memperbaharui data dari simfosia'], 200);
}
public function profilePost($profile_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$profile = User::find($profile_id);
if (! $profile) {
return response()->json(['status' => 'false', 'data' => $data, 'message' => 'Profile tidak ditemukan'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$status = new \App\Models\ActivityAction();
$status->activity_type = 'status';
$status->subject_type = 'user';
$status->user_id = $userdata['data']['id'];
$status->object_type = 'user';
$status->object_id = $profile->id;
$status->body = $request->text;
$status->save();
if ($request->hasFile('image')) {
$intervention = Image::make($request->file('image'));
$fileName = str_random(10) . '.png';
$destinationPath = 'images/user_status';
$intervention->save($destinationPath.'/'.$fileName);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $status->action_id;
$imageTable->parent_type = 'user_status';
$imageTable->path = $fileName;
$imageTable->save();
}
$data['post']['id'] = $status->action_id;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Behasil mengirimkan post'],200);
}
public function profilePostAndroid($profile_id, Request $request){
$userdata = Cektoken::cek($request->token);
$data = [];
$profile = User::find($profile_id);
if (! $profile) {
return response()->json(['status' => 'false', 'data' => $data, 'message' => 'Profile tidak ditemukan'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$status = new \App\Models\ActivityAction();
$status->activity_type = 'status';
$status->subject_type = 'user';
$status->user_id = $userdata['data']['id'];
$status->object_type = 'user';
$status->object_id = $profile->id;
$status->body = $request->text;
$status->save();
if ($request->has('image')) {
$destinationPath = 'images/user_status/';
$fileName = uniqid().'.png';
$img = $request->image;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $status->action_id;
$imageTable->parent_type = 'user_status';
$imageTable->path = $fileName;
$imageTable->save();
}
$data['post']['id'] = $status->action_id;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Behasil mengirimkan post'],200);
}
public function fjb(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$cat = [];
$categories = \App\Models\FjbCategory::all();
if (!$categories) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Belum ada categori apapun'], 200);
}
foreach ($categories as $key => $category) {
$cat[$key]['category_id'] = $category->cat_id;
$cat[$key]['category_title'] = $category->cat_title;
$cat[$key]['category_description'] = $category->cat_description;
$cat[$key]['category_icon'] = $category->cat_icon;
$cat[$key]['thread_count'] = \App\Models\FjbThread::where('forum_category', $category->cat_id)->count();
}
$data['categories'] = $cat;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function category($id,Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$category = \App\Models\FjbCategory::find($id);
if (!$category) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Category tidak ditemukan'], 200);
}
$data['category'] = [
'category_id' => $category->cat_id,
'category_title' => $category->cat_title,
'category_description' => $category->cat_description,
'category_icon' => $category->cat_icon,
'thread_count' => \App\Models\FjbThread::where('forum_category', $category->cat_id)->count()
];
$thread = \App\Models\FjbThread::where('forum_category', $category->cat_id)->orderBy('id', 'DESC')->get();
$data['thread'] = [];
if (!$thread) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada theread pada categori ini.'], 200);
}
foreach ($thread as $key => $value) {
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$value->created_by)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$getDataUser->photo_profile = $image;
$data['thread'][$key]['id'] = $value->id;
$data['thread'][$key]['title'] = $value->forum_title;
$data['thread'][$key]['description'] = $value->forum_description;
$data['thread'][$key]['price'] = $value->forum_price;
$dataImageCoy = \App\Models\ImagesTable::where('parent_type', 'commerce_photo')->where('parent_id', $value->id);
if($dataImageCoy->count() > 0){
$data['thread'][$key]['image'] = $dataImageCoy->first()->path;
}else{
$data['thread'][$key]['image'] = '';
}
$data['thread'][$key]['timestamp'] = $value->created_date;
$data['thread'][$key]['seller'] = $getDataUser;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function thread($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$thread = \App\Models\FjbThread::find($id);
if(!$thread){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.']);
}
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$thread->created_by)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$commerce_photo = \App\Models\ImagesTable::select('path AS filename' )->where('parent_type','=','commerce_photo')->where('parent_id','=',$thread->id)->get();
$category = \App\Models\FjbCategory::find($thread->forum_category);
$data['thread'] = [
'id' => $thread->id,
'category_id' => $thread->forum_category,
'category_title'=> $category->cat_title,
'title' => $thread->forum_title,
'excerpt' => str_limit(strip_tags($thread->forum_description), 160),
'description' => strip_tags($thread->forum_description),
'price' => $thread->forum_price,
'timestamp' => $thread->created_date,
'seller' => [
'id' => $thread->created_by,
'first_name' => $getDataUser->first_name,
'last_name' => $getDataUser->last_name,
'photo_profile' => $image
],
'images' => $commerce_photo,
'comment_count' => \App\Models\FjbThreadComment::where('action_id', $thread->id)->count(),
'created_at' => $thread->created_date
];
$comments = \App\Models\FjbThreadComment::where('action_id', $thread->id)->get();
$data['comments'] = [];
if ($comments) {
foreach ($comments as $key => $value) {
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$value->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['comments'][$key]['id'] = $value->activity_comment_id;
$data['comments'][$key]['text'] = $value->body;
$data['comments'][$key]['timestamp'] = $value->date_added;
$data['comments'][$key]['user'] = [
'id' => $thread->created_by,
'first_name' => $getDataUser->first_name,
'last_name' => $getDataUser->last_name,
'photo_profile' => $image
];
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function createThread(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$cats = '';
$categories = \App\Models\FjbCategory::all();
if ($categories) {
foreach ($categories as $key => $value) {
$cats .= $value->cat_id . ',';
}
}
$rules = [
'category' => 'required|integer|in:' . trim($cats, ','),
'title' => 'required|min:3|max:255',
'description' => 'required|min:3',
'price' => 'required|integer'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$thread = new \App\Models\FjbThread();
$thread->forum_category = $request->category;
$thread->forum_title = $request->title;
$thread->forum_price = $request->price;
$thread->forum_description = $request->description;
$thread->status = 1;
$thread->created_by = $userdata['data']['id'];
if ( ! $thread->save()) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Tidak dapat menyimpan thread.'],200);
}
if ($request->hasFile('photo')) {
foreach ($request->photo as $key => $value) {
$intervention = Image::make($request->file('photo')[$key]);
$fileName = str_random(10) . '.png';
$destinationPath = 'images/commerce';
$intervention->save($destinationPath.'/'.$fileName);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $thread->id;
$imageTable->parent_type = 'commerce_photo';
$imageTable->path = $fileName;
$imageTable->save();
}
}
return response()->json(['status' => true, 'data' => $thread, 'message' => 'Success membuat thread baru'], 200);
}
public function createThreadAndroid(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$cats = '';
$categories = \App\Models\FjbCategory::all();
if ($categories) {
foreach ($categories as $key => $value) {
$cats .= $value->cat_id . ',';
}
}
$rules = [
'category' => 'required|integer|in:' . trim($cats, ','),
'title' => 'required|min:3|max:255',
'description' => 'required|min:3',
'price' => 'required|integer'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$thread = new \App\Models\FjbThread();
$thread->forum_category = $request->category;
$thread->forum_title = $request->title;
$thread->forum_price = $request->price;
$thread->forum_description = $request->description;
$thread->status = 1;
$thread->created_by = $userdata['data']['id'];
if ( ! $thread->save()) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Tidak dapat menyimpan thread.'],200);
}
if ($request->has('photo')) {
foreach ($request->photo as $key => $value) {
$destinationPath = 'images/commerce/';
$fileName = uniqid().'.png';
$img = $request->photo[$key];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $thread->id;
$imageTable->parent_type = 'commerce_photo';
$imageTable->path = $fileName;
$imageTable->save();
}
}
return response()->json(['status' => true, 'data' => $thread, 'message' => 'Success membuat thread baru'], 200);
}
public function editThread($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$thread = \App\Models\FjbThread::find($id);
if(!$thread){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'], 200);
}
if ($thread->created_by != $userdata['data']['id'] AND $userdata['data']['id'] != 1) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'], 200);
}
return $this->thread($id, $request);
}
public function updateThread($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$thread = \App\Models\FjbThread::find($id);
if(!$thread){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'], 200);
}
if ($thread->created_by != $userdata['data']['id'] AND $userdata['data']['id'] != 1) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'], 200);
}
$cats = '';
$categories = \App\Models\FjbCategory::all();
if ($categories) {
foreach ($categories as $key => $value) {
$cats .= $value->cat_id . ',';
}
}
$rules = [
'category' => 'required|integer|in:' . trim($cats, ','),
'title' => 'required|min:3|max:255',
'description' => 'required|min:3',
'price' => 'required|integer'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$thread_photo = \App\Models\ImagesTable::select('path AS filename' )->where('parent_type','=','commerce_photo')->where('parent_id','=',$thread->id)->get();
if (count($thread_photo) > 0) {
foreach ($thread_photo as $key => $value) {
if (file_exists($value->filename)) {
File::delete('images/commerce/' . $value->filename);
}
}
$image = \App\Models\ImagesTable::where('parent_type','commerce_photo')->where('parent_id','=',$thread->id)->delete();
}
if ($request->hasFile('photo')) {
foreach ($request->photo as $key => $value) {
$intervention = Image::make($request->file('photo')[$key]);
$fileName = str_random(10) . '.png';
$destinationPath = 'images/commerce';
$intervention->save($destinationPath.'/'.$fileName);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $thread->id;
$imageTable->parent_type = 'commerce_photo';
$imageTable->path = $fileName;
$imageTable->save();
}
}
$thread->forum_category = $request->category;
$thread->forum_title = $request->title;
$thread->forum_price = $request->price;
$thread->forum_description = $request->description;
$thread->status = 1;
$thread->created_by = $userdata['data']['id'];
if ( ! $thread->save()) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Tidak dapat menyimpan thread.'],200);
}
return response()->json(['status' => true, 'data' => $thread, 'message' => 'Success memperbaharui thread'],200);
}
public function updateThreadAndroid($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$thread = \App\Models\FjbThread::find($id);
if(!$thread){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'], 200);
}
if ($thread->created_by != $userdata['data']['id'] OR $userdata['data']['id'] != 1) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'], 200);
}
$rules = [
'category' => 'required|integer',
'title' => 'required|min:3|max:255',
'description' => 'required|min:3',
'price' => 'required|integer'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$thread_photo = \App\Models\ImagesTable::select('path AS filename' )->where('parent_type','=','commerce_photo')->where('parent_id','=',$thread->id)->get();
if (count($thread_photo) > 0) {
foreach ($thread_photo as $key => $value) {
if (file_exists($value->filename)) {
File::delete('images/commerce/' . $value->filename);
}
}
$image = \App\Models\ImagesTable::where('parent_type','commerce_photo')->where('parent_id',$thread->id)->delete();
}
if ($request->has('photo')) {
foreach ($request->photo as $key => $value) {
$destinationPath = 'images/commerce/';
$fileName = uniqid().'.png';
$img = $request->photo[$key];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $thread->id;
$imageTable->parent_type = 'commerce_photo';
$imageTable->path = $fileName;
$imageTable->save();
}
}
$thread->forum_category = $request->category;
$thread->forum_title = $request->title;
$thread->forum_price = $request->price;
$thread->forum_description = $request->description;
$thread->status = 1;
$thread->created_by = $userdata['data']['id'];
if ( ! $thread->save()) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Tidak dapat menyimpan thread.'],200);
}
return response()->json(['status' => true, 'data' => $thread, 'message' => 'Success memperbaharui thread'],200);
}
public function threadDelete($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$thread = \App\Models\FjbThread::find($id);
if(!$thread OR $thread->created_by != $userdata['data']['id']){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.']);
}
//Delete Comment
$comments = \App\Models\FjbThreadComment::where('action_id', $thread->id)->delete();
//Delete Image
$thread_photo = \App\Models\ImagesTable::select('path AS filename' )->where('parent_type','=','commerce_photo')->where('parent_id','=',$thread->id)->get();
if (count($thread_photo) > 0) {
foreach ($thread_photo as $key => $value) {
if (file_exists($value->filename)) {
File::delete('images/commerce/' . $value->filename);
}
}
$image = \App\Models\ImagesTable::where('parent_type','commerce_photo')->where('parent_id',$thread->id)->delete();
}
//$commerce_photo = \App\Models\ImagesTable::select('path AS filename' )->where('parent_type','=','commerce_photo')->where('parent_id','=',$thread->id)->delete();
//Delete Thread
$thread->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus thread'], 200);
}
public function commentThread($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$thread = \App\Models\FjbThread::find($id);
if(!$thread){
return ['status' => false, 'data' => $data, 'message' => 'Thread tidak valid.'];
}
$rules['comment_text'] = 'required|min:3';
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$comment = new \App\Models\FjbThreadComment();
$comment->user_id = $userdata['data']['id'];
$comment->user_type = 'user';
$comment->action_id = $thread->id;
$comment->body = $request->comment_text;
if (!$comment->save()) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Gagal menyimpan komentar'], 503);
}
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$comment->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$getDataUser->gambar = $image;
$comment->user = $getDataUser;
//$comment->user->imageProfile = $image;
$data['comment'] = $comment;
//$data['comment']['user']['first_name'] =$getDataUser->first_name;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan komentar'], 200);
}
public function majalah(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$majalah = \App\Models\Majalah::orderBy('majalah_id', 'DESC')->get();
if (!$majalah) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada majalah'], 200);
}
foreach ($majalah as $k => $v){
$data['majalah'][$k]['id'] = $v->majalah_id;
$data['majalah'][$k]['edisi'] = $v->edisi;
$data['majalah'][$k]['photo1'] = $v->photo1;
$data['majalah'][$k]['photo2'] = $v->photo2;
$data['majalah'][$k]['photo3'] = $v->photo3;
$data['majalah'][$k]['download_link'] = $v->pdf;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function majalahShow($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$majalah = \App\Models\Majalah::find($id);
if (!$majalah) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Majalah tidak valid'], 200);
}
$data['id'] = $majalah->majalah_id;
$data['edisi'] = $majalah->edisi;
$data['photo1'] = $majalah->photo1;
$data['photo2'] = $majalah->photo2;
$data['photo3'] = $majalah->photo3;
$data['download_link'] = $majalah->pdf;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function fileSharing(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$categories = \App\Models\FileSharingCategory::all();
if (!$categories) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada kategori'], 200);
}
foreach ($categories as $k => $v){
$data['categories'][$k]['id'] = $v->id;
$data['categories'][$k]['name'] = $v->category_name;
$data['categories'][$k]['icon'] = $v->icon;
$data['categories'][$k]['background'] = $v->background;
$data['categories'][$k]['file_count'] = \App\Models\FileSharing::where('category', $v->id)->count();
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function fileSharingCategory($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$category = \App\Models\FileSharingCategory::find($id);
if (!$category) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Category tidak valid'], 200);
}
$data['id'] = $category->id;
$data['name'] = $category->category_name;
$data['icon'] = $category->icon;
$data['background'] = $category->background;
$data['file_count'] = \App\Models\FileSharing::where('category', $category->id)->count();
$files = \App\Models\FileSharing::where('category', $category->id)->get();
if ($files) {
foreach ($files as $k => $file) {
$getDataUser = User::select('id', 'first_name', 'last_name')->where('id', '=', $file->user_id)->first();
$getDataUser->profile_photo = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['files'][$k]['id'] = $file->id;
$data['files'][$k]['title'] = $file->title;
$data['files'][$k]['description'] = $file->description;
$data['files'][$k]['file_path'] = \App\Models\ImagesTable::where('parent_type', 'file_sharing')->where('parent_id', $file->id)->first()->path;
$data['files'][$k]['category_id'] = $file->category;
$data['files'][$k]['category_title'] = \App\Models\FileSharingCategory::where('id', '=', $file->category)->first()->category_name;
$data['files'][$k]['uploader'] = $getDataUser;
}
}else{
$data['files'] = null;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function fileSharingShow($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$file = \App\Models\FileSharing::find($id);
if (!$file OR $file->status != '1') {
return response()->json(['status' => true, 'data' => $data, 'message' => 'File tidak valid'], 200);
}
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$file->user_id)->first();
$getDataUser->profile_photo = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['id'] = $file->id;
$data['title'] = $file->title;
$data['description'] = $file->description;
$data['file_path'] = \App\Models\ImagesTable::where('parent_type', 'file_sharing')->where('parent_id', $file->id)->first()->path;
$data['category_id'] = $file->category;
$data['category_title'] = \App\Models\FileSharingCategory::where('id','=', $file->category)->first()->category_name;
$data['uploader'] = $getDataUser;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function news(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$news = \App\Models\ActivityAction::where('activity_type', 'news')->orWhere('activity_type', 'artical')->orderBy('action_id', 'DESC')->get();
if (!$news) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada berita atau article'], 200);
}
foreach ($news as $k => $v) {
$data['news'][$k]['id'] = $v->action_id;
$data['news'][$k]['type'] = ($v->activity_type == 'artical') ? 'article' : 'news';
$data['news'][$k]['title'] = $v->title;
$data['news'][$k]['excerpt'] = strip_tags(str_limit($v->body, 160));
//Cek image
$data['news'][$k]['image'] = '';
$image = \App\Models\ImagesTable::where('parent_type', 'news')->where('parent_id', $v->action_id)->first();
if ($image) {
$data['news'][$k]['image'] = $image->path;
}
# code...
$data['news'][$k]['excerpt'] = strip_tags(str_limit($v->body, 160));
$data['news'][$k]['timestamp'] = $v->date_added;
$data['news'][$k]['comment_count'] = \App\Models\ActivityComment::where('action_id', $v->action_id)->count();
$data['news'][$k]['like_count'] = \App\Models\ActivityLikes::where('action_id', $v->action_id)->count();
// $data['news'][$k]['body'] = strip_tags($v->body);
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
function newsShow($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$news = \App\Models\ActivityAction::find($id);
if (!$news) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berita atau Article tidak valid'], 200);
}
$data['id'] = $news->action_id;
$data['type'] = ($news->activity_type == 'artical') ? 'article' : 'news';
$data['title'] = $news->title;
$data['excerpt'] = strip_tags(str_limit($news->body, 160));
$data['text'] = strip_tags($news->body);
//Cek image
$data['image'] = '';
$image = \App\Models\ImagesTable::where('parent_type', 'news')->where('parent_id', $news->action_id)->first();
if($image) {
$data['image'] = $image->path;
}
$data['timestamp'] = $news->date_added;
$data['like_count'] = \App\Models\ActivityLikes::where('action_id', $news->action_id)->count();
$comment = [];
if ($news->activity_type == 'artical') {
$comments = \App\Models\ActivityComment::where('action_id', $news->action_id)->get();
if ($comments) {
foreach ($comments as $k => $v) {
$getDataUser = User::select('id', 'first_name', 'last_name')->where('id', '=', $v->user_id)->first();
$getDataUser->profile_photo = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$comment[$k]['id'] = $v->activity_comment_id;
$comment[$k]['text'] = $v->body;
$comment[$k]['timestamp'] = $v->date_added;
$comment[$k]['user'] = $getDataUser;
}
}
$data['comments'] = $comment;
$data['comment_count'] = \App\Models\ActivityComment::where('action_id', $news->action_id)->count();
$related = \App\Models\ActivityAction::where('activity_type', 'artical')->where('action_id','!=' ,$id)->limit(4)->orderByRaw("RAND()")->get();
if ($related) {
foreach ($related as $k => $v) {
$data['related'][$k]['id'] = $v->action_id;
$data['related'][$k]['title'] = $v->title;
$data['related'][$k]['excerpt'] = strip_tags(str_limit($v->body, 160));
$data['related'][$k]['timestamp'] = $v->date_added;
}
}
}else{
$related = \App\Models\ActivityAction::where('activity_type', 'news')->where('action_id','!=' ,$id)->limit(4)->orderByRaw("RAND()")->get();
if ($related) {
foreach ($related as $k => $v) {
$data['related'][$k]['id'] = $v->action_id;
$data['related'][$k]['title'] = $v->title;
$data['related'][$k]['excerpt'] = strip_tags(str_limit($v->body, 160));
$data['related'][$k]['timestamp'] = $v->date_added;
}
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
function articleComment($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$news = \App\Models\ActivityAction::find($id);
if (!$news OR $news->activity_type != 'artical') {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Article tidak valid'], 200);
}
$rules = [
'comment_text' => 'required'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$comment = new \App\Models\ActivityComment();
$comment->user_id = $userdata['data']['id'];
$comment->action_id = $news->action_id;
$comment->body = $request->comment_text;
$comment->save();
$data=$comment;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan Komentar'],200);
}
function newsLike($id, Request $request){
$userdata = Cektoken::cek($request->token);
$data = [];
$news = \App\Models\ActivityAction::find($id);
if (!$news) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Like tidak valid'], 200);
}
$like = new \App\Models\ActivityLikes();
$like->user_id = $userdata['data']['id'];
$like->action_id = $news->action_id;
$like->save();
$data=$like;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan like'],200);
}
public function removeArticleComment($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$comment = \App\Models\ActivityComment::find($id);
if (!$comment){
return response()->json(['status' => true, 'data' => $data, 'message' => 'Comment tidak valid'], 200);
}
if ($comment->user_id != $userdata['data']['id'] AND $comment->user_id != 1) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Comment tidak valid'], 200);
}
$comment->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus Komentar'],200);
}
public function unlike($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$like = \App\Models\ActivityLikes::where('user_id', $userdata['data']['id'])->where('action_id', $id)->first();
if (!$like) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Like tidak valid'], 200);
}
if ($like->user_id != $userdata['data']['id'] AND $like->user_id != 1) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Comment tidak valid'], 200);
}
$like->delete();
$data=$like;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus like'],200);
}
public function pesanAdk(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$pesanAdk = \App\Models\ImagesTable::where('parent_type', 'anggota_dewan_komisioner')->get();
$data['pesan_ketua_adk'] = [];
$data['pesan_adk'] = [];
$pesanKetuaAdk = \App\Models\ImagesTable::where('parent_type', 'ketua_dewan_komisioner')->get();
if (!$pesanAdk AND !$pesanKetuaAdk) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada pesan ADK'], 200);
}
foreach ($pesanKetuaAdk as $k => $v) {
$data['pesan_ketua_adk'][$k]['id'] = $v->image_id;
$data['pesan_ketua_adk'][$k]['image_path'] = $v->path;
$data['pesan_ketua_adk'][$k]['description'] = $v->description;
$data['pesan_ketua_adk'][$k]['timestamp'] = $v->added_date;
}
foreach ($pesanAdk as $key => $value) {
$data['pesan_adk'][$key]['id'] = $value->image_id;
$data['pesan_adk'][$key]['image_path'] = $value->path;
$data['pesan_adk'][$key]['description'] = $value->description;
$data['pesan_adk'][$key]['timestamp'] = $value->added_date;
}
$data['total_count'] = $pesanAdk->count();
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function pesanAdkShow($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$pesanAdk = \App\Models\ImagesTable::find($id);
if (!$pesanAdk) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Pesan ADK tidak valid'], 200);
}
$data['id'] = $pesanAdk->image_id;
$data['image_path'] = $pesanAdk->path;
$data['description'] = $pesanAdk->description;
$data['timestamp'] = $pesanAdk->added_date;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function calendar(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$calendar = \App\Models\Calendar::where('user_id', $userdata['data']['id'])->orWhere('user_id', 1)->get();
// $todolist = \App\Models\Calendar::where('user_id', $userdata['data']['id'])->where('start')->orderBy('calendar_id','DESC')->get();
if (!$calendar AND !$todolist) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada calendar atau to do list'], 200);
}
$data['calendar'] = $calendar;
// $data['todolist'] = $todolist;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function calendarStore(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$rules = [
'title' => 'required:max255',
'start' => 'required|date_format:Y-m-d',
// 'end' => 'required|date_format:Y-m-d',
'color' => 'sometimes|in:#f44336,#E91E63,#9C27B0,#3F51B5,#009688,#dae027',
'icon' => 'sometimes|in:fa-info,fa-warning,fa-check,fa-user,fa-lock,fa-clock-o'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$colors = [
'#f44336',
'#E91E63',
'#9C27B0',
'#3F51B5',
'#009688',
'#dae027'
];
$color = array_rand($colors,1);
$event = new \App\Models\Calendar();
$event->user_id = $userdata['data']['id'];
$event->start = $request->start;
$event->end = $request->end;
$event->color = ($request->color) ? $request->color : $colors[$color];
$event->title = $request->title;
$event->description = $request->description;
$event->allDay= 1;
$event->icon= $request->icon;
$event->url= $request->url;
if (! $event->save()) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Gagal menyimpan event'],500);
}
$data = $event;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan event'],200);
}
public function calendarShow($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$event = \App\Models\Calendar::find($id);
if (! $event) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Event invalid'],404);
}
$data = $event;
return response()->json(['status' => true, 'data' => $data, 'message' => ''],200);
}
public function calendarDelete($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$event = \App\Models\Calendar::find($id);
if (! $event) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Event invalid'],404);
}
$event->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus event'],200);
}
public function satker(Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$mySatker = \App\Models\SatkerMember::where('user_id', $userdata['data']['id'])->get();
$satkers = \App\Models\Satker::select('id', 'name', 'description', 'daerah', 'photo')->get();
// $daerah = Petanya::all();
$data['my_satker'] = [];
if ($mySatker) {
foreach ($mySatker as $k => $v) {
$satker = \App\Models\Satker::select('id', 'name', 'description', 'daerah', 'photo')->where('name', $v->nama_departemen)->first();
if ($satker) {
$data['my_satker'][$k] = $satker;
}
}
}
$data['satker'] = $satkers;
return response()->json(['status' => true, 'data' => $data, 'message' => ''],200);
// $daerah = DB::select('SELECT *, (select count(*) from satker where daerah = hc_key) as jumSat FROM petanya');
}
public function satkerShow($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($id);
if (! $satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$data['satker']['id'] = $satker->id;
$data['satker']['name'] = $satker->name;
$data['satker']['description'] = $satker->description;
$data['satker']['daerah'] = $satker->daerah;
$data['satker']['photo'] = $satker->photo;
$data['satker']['isSatkerMember'] = 'no';
//Is member
$isMember = \App\Models\SatkerMember::where('nama_departemen', $satker->name)->where('user_id', $userdata['data']['id'])->count();
if ($isMember > 0) {
$data['satker']['isSatkerMember'] = 'yes';
}
//Member list
$member = \App\Models\SatkerMember::where('nama_departemen', $satker->name)->orderBy('id', 'DESC')->limit(20)->get();
if ($member) {
foreach ($member as $k => $v) {
$user = User::find($v->user_id);
$image = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['satker']['member'][$k]['id'] = $user->id;
$data['satker']['member'][$k]['first_name'] = $user->first_name;
$data['satker']['member'][$k]['last_name'] = $user->last_name;
$data['satker']['member'][$k]['photo_profile'] = $image;
$data['satker']['member'][$k]['member_type'] = ($v->member_type == 'moderator') ? 'moderator' : 'member';
}
}
//Gallery
$gallery = \App\Models\ImagesTable::where('parent_type', 'satker_photo')->where('parent_id', $satker->id)->get();
if ($gallery) {
foreach ($gallery as $k => $v) {
$data['satker']['gallery'][$k]['image_id'] = $v->image_id;
$data['satker']['gallery'][$k]['file_name'] = $v->path;
}
}
//Timeline
$posts = \App\Models\SatkerPost::where('satker_id', $satker->id)->orderBy('id', 'DESC')->limit(20)->get();
if ($posts) {
foreach ($posts as $k => $v) {
$user = User::find($v->poster_id);
$image = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['satker']['post'][$k]['id'] = $v->id;
$data['satker']['post'][$k]['text'] = $v->text;
$data['satker']['post'][$k]['image'] = $v->image;
$data['satker']['post'][$k]['timestamp'] = $v->created_at;
//Post Comment
$comments = \App\Models\SatkerPostComment::where('post_id', $v->id)->get();
$data['satker']['post'][$k]['comment_count'] = $comments->count();
$data['satker']['post'][$k]['user']['id'] = $user->id;
$data['satker']['post'][$k]['user']['first_name'] = $user->first_name;
$data['satker']['post'][$k]['user']['last_name'] = $user->last_name;
$data['satker']['post'][$k]['user']['photo_profile'] = $image;
if ($comments) {
foreach ($comments as $key => $value) {
$commentUser = User::find($value->poster_id);
$commentUserimage = \App\Library\CustomLibrary::ambilGambarCoy($commentUser->id);
$data['satker']['post'][$k]['comment']['id'] = $value->id;
$data['satker']['post'][$k]['comment']['text'] = $value->text;
$data['satker']['post'][$k]['comment']['timestamp'] = $value->created_at;
$data['satker']['post'][$k]['comment']['user']['id'] = $commentUser->id;
$data['satker']['post'][$k]['comment']['user']['first_name'] = $commentUser->first_name;
$data['satker']['post'][$k]['comment']['user']['last_name'] = $commentUser->last_name;
$data['satker']['post'][$k]['comment']['user']['photo_profile'] = $commentUserimage;
}
}
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function satkerGallery($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($id);
if (! $satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$data['satker'] = $satker;
$gallery = \App\Models\ImagesTable::where('parent_type', 'satker_photo')->where('parent_id', $satker->id)->orderBy('image_id', 'DESC')->limit(100)->get();
if ($gallery) {
foreach ($gallery as $index => $item) {
$data['gallery'][$index]['id'] = $item->image_id;
$data['gallery'][$index]['file_path'] = $item->path;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function satkerMember($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($id);
if (! $satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$data['satker'] = $satker;
//Member list
$member = \App\Models\SatkerMember::where('nama_departemen', $satker->name)->orderBy('id', 'DESC')->get();
if ($member) {
foreach ($member as $k => $v) {
$user = User::find($v->user_id);
$image = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['member'][$k]['id'] = $user->id;
$data['member'][$k]['first_name'] = $user->first_name;
$data['member'][$k]['last_name'] = $user->last_name;
$data['member'][$k]['photo_profile'] = $image;
$data['member'][$k]['member_type'] = ($v->member_type == 'moderator') ? 'moderator' : 'member';
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function satkerPostContent($satker_id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
//Cek if the user is a member
$member = \App\Models\SatkerMember::where('nama_departemen', $satker->name)->where('user_id', $userdata['data']['id'])->get();
if (! $member AND $userdata['data']['id'] != 1) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Ups..Anda bukan member sarket ini.'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$post = new \App\Models\SatkerPost();
$post->satker_id = $satker->id;
$post->poster_id = $userdata['data']['id'];
$post->text = $request->text;
if ($request->hasFile('image')) {
$intervention = Image::make($request->file('image'));
$fileName = str_random(10) . '.png';
$destinationPath = 'images/user_status';
$intervention->save($destinationPath.'/'.$fileName);
$post->image = $fileName;
}
$post->save();
$data['satker'] = $satker;
$data['post'] = $post;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan kontent post'],200);
}
public function satkerPostContentAndroid($satker_id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
//Cek if the user is a member
$member = \App\Models\SatkerMember::where('nama_departemen', $satker->name)->where('user_id', $userdata['data']['id'])->get();
if (! $member AND $userdata['data']['id'] != 1) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Ups..Anda bukan member sarket ini.'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$post = new \App\Models\SatkerPost();
$post->satker_id = $satker->id;
$post->poster_id = $userdata['data']['id'];
$post->text = $request->text;
if ($request->has('image')) {
$destinationPath = 'images/user_status/';
$fileName = uniqid().'.png';
$img = $request->image;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$post->image = $fileName;
}
$post->save();
$data['satker'] = $satker;
$data['post'] = $post;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan kontent post'],200);
}
public function satkerGetPostContent($satker_id, $content_id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$post = \App\Models\SatkerPost::find($content_id);
if (! $post) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Content tidak valid'], 404);
}
$user = User::find($post->poster_id);
$image = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['content']['id'] = $post->id;
$data['content']['text'] = $post->text;
$data['content']['timestamp'] = $post->created_at;
//Post Comment
$comments = \App\Models\SatkerPostComment::where('post_id', $post->id)->get();
$data['content']['comment_count'] = $comments->count();
$data['content']['user']['id'] = $user->id;
$data['content']['user']['first_name'] = $user->first_name;
$data['content']['user']['last_name'] = $user->last_name;
$data['content']['user']['photo_profile'] = $image;
if ($comments) {
foreach ($comments as $key => $value) {
$commentUser = User::find($value->poster_id);
$commentUserimage = \App\Library\CustomLibrary::ambilGambarCoy($commentUser->id);
$data['content']['comment'][$key]['id'] = $value->id;
$data['content']['comment'][$key]['text'] = $value->text;
$data['content']['comment'][$key]['timestamp'] = $value->created_at;
$data['content']['comment'][$key]['user']['id'] = $commentUser->id;
$data['content']['comment'][$key]['user']['first_name'] = $commentUser->first_name;
$data['content']['comment'][$key]['user']['last_name'] = $commentUser->last_name;
$data['content']['comment'][$key]['user']['photo_profile'] = $commentUserimage;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan kontent post'],200);
}
public function satkerDeletePostContent($satker_id, $content_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$post = \App\Models\SatkerPost::find($content_id);
if (! $post) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Content tidak valid'], 404);
}
//Check if the user owned the post content
if ($post->poster_id != $userdata['data']['id'] AND $userdata['data']['id'] != '1') {
return response()->json(['status' => false, 'data' => '', 'message' => 'Ups, menghapus content ini, bukan hakmu.'], 404);
}
//Get Comments
$comments = \App\Models\SatkerPostComment::where('post_id', $post->id)->delete();
$post->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus post'],200);
}
public function satkerGetSinglePostContent($satker_id, $content_id, $comment_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$post = \App\Models\SatkerPost::find($content_id);
if (! $post) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Content tidak valid'], 404);
}
$comment = \App\Models\SatkerPostComment::find($comment_id);
if (! $comment) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Comment tidak valid'], 404);
}
$data['satker'] = $satker;
$data['content']['id'] = $post->id;
$data['content']['text'] = $post->text;
$data['content']['timestamp'] = $post->created_at;
$postUser = User::find($post->poster_id);
$postUserImage = \App\Library\CustomLibrary::ambilGambarCoy($postUser->id);
$data['content']['user']['id'] = $post->poster_id;
$data['content']['user']['first_name'] = $postUser->first_name;
$data['content']['user']['last_name'] = $postUser->last_name;
$data['content']['user']['photo_profile'] = $postUserImage;
$data['comment']['id'] = $comment->id;
$data['comment']['text'] = $comment->text;
$data['comment']['timestamp'] = $comment->created_at;
$commentUser = User::find($comment->poster_id);
$commentUserImage = \App\Library\CustomLibrary::ambilGambarCoy($commentUser->id);
$data['comment']['user']['id'] = $comment->poster_id;
$data['comment']['user']['first_name'] = $commentUser->first_name;
$data['comment']['user']['last_name'] = $commentUser->last_name;
$data['comment']['user']['photo_profile'] = $commentUserImage;
return response()->json(['status' => true, 'data' => $data, 'message' => '']);
}
public function satkerPostContentComment($satker_id, $content_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$post = \App\Models\SatkerPost::find($content_id);
if (! $post) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Content tidak valid'], 404);
}
//Cek if the user is a member
$member = \App\Models\SatkerMember::where('nama_departemen', $satker->name)->where('user_id', $userdata['data']['id'])->get();
if (! $member AND $userdata['data']['id'] != 1) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Ups..Anda bukan member sarket ini.'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$comment = new \App\Models\SatkerPostComment();
$comment->satker_id = $satker->id;
$comment->post_id = $post->id;
$comment->poster_id = $userdata['data']['id'];
$comment->text = $request->text;
$comment->save();
$data = $comment;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menimpan comment'],200);
}
public function satkerDeletePostContentComment($satker_id, $content_id, $comment_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$satker = \App\Models\Satker::find($satker_id);
if (!$satker) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Satker tidak valid'], 404);
}
$post = \App\Models\SatkerPost::find($content_id);
if (! $post) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Content tidak valid'], 404);
}
$comment = \App\Models\SatkerPostComment::find($comment_id);
if (! $comment) {
return response()->json(['status' => false, 'data' => '', 'message' => 'Conmment tidak valid'], 404);
}
if ($comment->poster_id != $userdata['data']['id'] AND $userdata['data']['id'] != '1') {
return response()->json(['status' => false, 'data' => '', 'message' => 'Ups, anda tidak berhak menghapus comment ini'], 404);
}
if (!$comment->delete()) {
return response()->json(['status' => false, 'data' => $comment, 'message' => 'Ups, Gagal menghapus comment'], 501);
}
return response()->json(['status' => false, 'data' => $comment, 'message' => 'Berhasil menghapus comment'], 200);
}
public function survey(Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$participatingSurvey = \App\Models\SurveyResult::select('survey_id')->where('user_id', $userdata['data']['id'])->distinct()->get();
if (count($participatingSurvey) > 0) {
foreach ($participatingSurvey as $k) {
$an[] = $k->survey_id;
}
$survey = \App\Models\Survey::where('is_active', 'y')->whereNotIn('id', $an)->orderBy('id', 'DESC')->get();
}else{
$survey = \App\Models\Survey::where('is_active', 'y')->orderBy('id', 'DESC')->get();
}
if ($survey) {
foreach ($survey as $k => $v) {
$data['survey'][$k]['id'] = $v->id;
$data['survey'][$k]['title'] = $v->title;
$data['survey'][$k]['description'] = $v->description;
$data['survey'][$k]['timestamp'] = $v->created_at;
$data['survey'][$k]['question_count'] = \App\Models\SurveyQuestion::where('survey_id', $v->id)->count();
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function surveyPlay($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$survey = \App\Models\Survey::find($id);
if (!$survey OR $survey->is_active == 'n') {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Survey tidak valid'], 404);
}
//Check if the member already take this survey
if (\App\Models\SurveyResult::where('survey_id', $survey->id)->where('user_id', $userdata['data']['id'])->count() > 0) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Anda sudah pernah berpartisipasi dalam survey ini. Terimakasih.'], 404);
}
$data['survey']['id'] = $survey->id;
$data['survey']['title'] = $survey->title;
$data['survey']['description'] = $survey->description;
$data['survey']['timestamp'] = $survey->created_at;
$surveyQuestion = \App\Models\SurveyQuestion::where('survey_id', $survey->id)->get();
$data['survey']['question_count'] = $surveyQuestion->count();
if ($surveyQuestion) {
foreach ($surveyQuestion as $k => $v) {
$data['question'][$k]['id'] = $v->id;
$data['question'][$k]['type'] = $v->type;
$data['question'][$k]['text'] = $v->text;
$data['question'][$k]['timestamp'] = $v->crated_at;
switch ($v->type) {
case 'pg' :
$data['question'][$k]['option_a'] = $v->pil_a;
$data['question'][$k]['option_b'] = $v->pil_b;
$data['question'][$k]['option_c'] = $v->pil_c;
$data['question'][$k]['option_d'] = $v->pil_d;
$data['question'][$k]['option_e'] = $v->pil_e;
$data['question'][$k]['option_f'] = $v->pil_f;
break;
case 'multi' :
$data['question'][$k]['option_a'] = $v->pil_a;
$data['question'][$k]['option_b'] = $v->pil_b;
$data['question'][$k]['option_c'] = $v->pil_c;
$data['question'][$k]['option_d'] = $v->pil_d;
$data['question'][$k]['option_e'] = $v->pil_e;
$data['question'][$k]['option_f'] = $v->pil_f;
break;
case 'scale' :
$data['question'][$k]['option_1'] = 1;
$data['question'][$k]['option_2'] = 2;
$data['question'][$k]['option_3'] = 3;
$data['question'][$k]['option_4'] = 4;
$data['question'][$k]['option_5'] = 5;
break;
default:
break;
}
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function surveyPlayStore($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$survey = \App\Models\Survey::find($id);
if (!$survey OR $survey->is_active == 'n') {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Survey tidak valid'], 404);
}
//Check if the member already take this survey
if (\App\Models\SurveyResult::where('survey_id', $survey->id)->where('user_id', $userdata['data']['id'])->count() > 0) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Anda sudah pernah berpartisipasi dalam survey ini. Terimakasih.'], 404);
}
if(!is_array($request->question_id)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
foreach ($request->question_id as $k => $v) {
$question = \App\Models\SurveyQuestion::find($k);
if (!$question){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, pertanyaan dan jawaban yang anda kirim tidak valid. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
$answer = $v;
switch ($question->type) {
case 'pg':
$validAnswer = [$question->pil_a, $question->pil_b, $question->pil_c, $question->pil_d, $question->pil_e, $question->pil_f];
if (! in_array($v, $validAnswer)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak valid untuk pertanyaan ' . $k . '. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
$answer = $v;
break;
case 'multi':
$validAnswer = [$question->pil_a, $question->pil_b, $question->pil_c, $question->pil_d, $question->pil_e, $question->pil_f];
if (! in_array($v, $validAnswer)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak valid untuk pertanyaan ' . $k . '. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
$answer = $v;
break;
case 'scale':
$validAnswer = ['1', '2', '3', '4', '5'];
if (! in_array($v, $validAnswer)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak valid untuk pertanyaan ' . $k . '. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
$answer = $v;
break;
case 'essay':
$answer = $v;
break;
default:
if (! in_array($v, $validAnswer)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, pertanyaan dan jawaban yang anda kirim tidak valid. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
break;
}
$result = new \App\Models\SurveyResult();
$result->survey_id = $survey->id;
$result->question_id = $question->id;
$result->user_id = $userdata['data']['id'];
$result->answer = $answer;
$result->save();
}
$data['survey']['id'] = $survey->id;
$data['survey']['title'] = $survey->title;
$data['survey']['description'] = $survey->description;
$data['survey']['timestamp'] = $survey->created_at;
$surveyQuestion = \App\Models\SurveyQuestion::where('survey_id', $survey->id)->get();
$data['survey']['question_count'] = $surveyQuestion->count();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan survey. Terimakasih.'], 200);
}
public function surveyStats($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$survey = \App\Models\Survey::find($id);
if (!$survey OR $survey->is_active == 'n') {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Survey tidak valid'], 404);
}
$question = \App\Models\SurveyQuestion::where('survey_id', $survey->id)->where('type', '<>', 'essay')->get();
if (!$question){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Belum ada pertanyaan untuk survey ini'], 400);
}
$data['survey']['id'] = $survey->id;
$data['survey']['title'] = $survey->title;
$data['survey']['description'] = $survey->description;
$data['survey']['timestamp'] = $survey->created_at;
foreach ($question as $k => $v) {
$result = \App\Models\SurveyResult::where('question_id', $v->id)->get();
$data['question'][$k]['id'] = $v->id;
$data['question'][$k]['type'] = $v->type;
$data['question'][$k]['text'] = $v->text;
$data['question'][$k]['timestamp'] = $v->created_at;
foreach ($result as $key => $value) {
switch ($v->type) {
case 'pg' :
$data['question'][$k]['answer']['option_a']['answer_text'] = $v->pil_a;
$data['question'][$k]['answer']['option_b']['answer_text'] = $v->pil_b;
$data['question'][$k]['answer']['option_c']['answer_text'] = $v->pil_c;
$data['question'][$k]['answer']['option_d']['answer_text'] = $v->pil_d;
$data['question'][$k]['answer']['option_e']['answer_text'] = $v->pil_e;
$data['question'][$k]['answer']['option_f']['answer_text'] = $v->pil_f;
if( ! isset($data['question'][$k]['answer']['option_a']['sum']) ) {
$data['question'][$k]['answer']['option_a']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_b']['sum']) ) {
$data['question'][$k]['answer']['option_b']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_c']['sum']) ) {
$data['question'][$k]['answer']['option_c']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_d']['sum']) ) {
$data['question'][$k]['answer']['option_d']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_e']['sum']) ) {
$data['question'][$k]['answer']['option_e']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_f']['sum']) ) {
$data['question'][$k]['answer']['option_f']['sum'] = 0;
}
switch ($value->answer){
case $v->pil_a:
$data['question'][$k]['answer']['option_a']['sum']++;
break;
case $v->pil_b:
$data['question'][$k]['answer']['option_b']['sum']++;
break;
case $v->pil_c:
$data['question'][$k]['answer']['option_c']['sum']++;
break;
case $v->pil_d:
$data['question'][$k]['answer']['option_d']['sum']++;
break;
case $v->pil_e:
$data['question'][$k]['answer']['option_e']['sum']++;
break;
case $v->pil_f:
$data['question'][$k]['answer']['option_f']['sum']++;
break;
default:
break;
}
break;
case 'multi':
$data['question'][$k]['answer']['option_a']['answer_text'] = $v->pil_a;
$data['question'][$k]['answer']['option_b']['answer_text'] = $v->pil_b;
$data['question'][$k]['answer']['option_c']['answer_text'] = $v->pil_c;
$data['question'][$k]['answer']['option_d']['answer_text'] = $v->pil_d;
$data['question'][$k]['answer']['option_e']['answer_text'] = $v->pil_e;
$data['question'][$k]['answer']['option_f']['answer_text'] = $v->pil_f;
if( ! isset($data['question'][$k]['answer']['option_a']['sum']) ) {
$data['question'][$k]['answer']['option_a']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_b']['sum']) ) {
$data['question'][$k]['answer']['option_b']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_c']['sum']) ) {
$data['question'][$k]['answer']['option_c']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_d']['sum']) ) {
$data['question'][$k]['answer']['option_d']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_e']['sum']) ) {
$data['question'][$k]['answer']['option_e']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_f']['sum']) ) {
$data['question'][$k]['answer']['option_f']['sum'] = 0;
}
switch ($value->answer){
case $v->pil_a:
$data['question'][$k]['answer']['option_a']['sum']++;
break;
case $v->pil_b:
$data['question'][$k]['answer']['option_b']['sum']++;
break;
case $v->pil_c:
$data['question'][$k]['answer']['option_c']['sum']++;
break;
case $v->pil_d:
$data['question'][$k]['answer']['option_d']['sum']++;
break;
case $v->pil_e:
$data['question'][$k]['answer']['option_e']['sum']++;
break;
case $v->pil_f:
$data['question'][$k]['answer']['option_f']['sum']++;
break;
default:
break;
}
break;
case 'scale':
$data['question'][$k]['answer']['option_1']['answer_text'] = 'Tidak Setuju';
$data['question'][$k]['answer']['option_2']['answer_text'] = 'Kurang Setuju';
$data['question'][$k]['answer']['option_3']['answer_text'] = 'Netral';
$data['question'][$k]['answer']['option_4']['answer_text'] = 'Setuju';
$data['question'][$k]['answer']['option_5']['answer_text'] = 'Sangat Setuju';
if( ! isset($data['question'][$k]['answer']['option_1']['sum']) ) {
$data['question'][$k]['answer']['option_1']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_2']['sum']) ) {
$data['question'][$k]['answer']['option_2']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_3']['sum']) ) {
$data['question'][$k]['answer']['option_3']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_4']['sum']) ) {
$data['question'][$k]['answer']['option_4']['sum'] = 0;
}
if( ! isset($data['question'][$k]['answer']['option_5']['sum']) ) {
$data['question'][$k]['answer']['option_5']['sum'] = 0;
}
switch ($value->answer){
case '1':
$data['question'][$k]['answer']['option_1']['sum']++;
break;
case '2':
$data['question'][$k]['answer']['option_2']['sum']++;
break;
case '3':
$data['question'][$k]['answer']['option_3']['sum']++;
break;
case '4':
$data['question'][$k]['answer']['option_4']['sum']++;
break;
case '5':
$data['question'][$k]['answer']['option_5']['sum']++;
break;
default:
break;
}
break;
default:
break;
}
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function quiz(Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
//MyScore
$myScrore = \App\Models\QuizResult::where('user_id', $userdata['data']['id'])->where('is_finished', 'y');
if ($myScrore->count() > 0) {
$data['my_score'] = round($myScrore->sum('result') / $myScrore->count(), 1);
}else{
$data['my_score'] = round(0,1);
}
//TopScore
$topScore = \App\Models\QuizResult::where('is_finished', 'y')->groupBy('user_id')
->selectRaw('user_id, sum(result) as sum, count(*) as jum, sum(result) / count(*) as has')
->orderBy('has', 'desc')
->take(10)->get();
if ($topScore) {
foreach ($topScore as $k => $v) {
$user = User::find($v->user_id);
$image = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['top_score'][$k]['user_id'] = $v->user_id;
$data['top_score'][$k]['first_name'] = $user->first_name;
$data['top_score'][$k]['last_name'] = $user->last_name;
$data['top_score'][$k]['photo_profile'] = $image;
$data['top_score'][$k]['score'] = round($v->has,1);
}
}else{
$data['top_score'] = null;
}
//Quiz
$quiz = \App\Models\Quiz::where('status', 'live')->orderBy('updated_at', 'desc')->get();
if($quiz) {
foreach ($quiz as $k => $v) {
$data['quiz'][$k]['id'] = $v->id;
$data['quiz'][$k]['title'] = $v->title;
$data['quiz'][$k]['description'] = $v->description;
$data['quiz'][$k]['timestamp'] = $v->created_at;
$data['quiz'][$k]['question_count'] = \App\Models\Question::where('quiz_id', $v->id)->count();
}
}
//Quiz Module
$quizmodule = \App\Models\QuizModule::latest()->get();
if ($quizmodule) {
foreach ($quizmodule as $k => $v) {
$data['quiz_module'][$k]['id'] = $v->id;
$data['quiz_module'][$k]['title'] = $v->id;
$data['quiz_module'][$k]['excerp'] = str_limit(strip_tags($v->text), 200);
$data['quiz_module'][$k]['timestamp'] = $v->created_at;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function quizPlay($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$quiz = \App\Models\Quiz::find($id);
if (!$quiz OR $quiz->is_active == 'n') {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Quiz tidak valid'], 404);
}
$data['quiz']['id'] = $quiz->id;
$data['quiz']['title'] = $quiz->title;
$data['quiz']['description'] = $quiz->description;
$data['quiz']['timestamp'] = $quiz->created_at;
$data['quiz']['question_count'] = \App\Models\Question::where('quiz_id', $quiz->id)->count();
//Check if the member already take this quiz
//Check if all question is answered
$myResult = \App\Models\QuizResult::where('quiz_id', $quiz->id)->where('user_id', $userdata['data']['id'])->get();
if (count($myResult)) {
if ($myResult->first()->is_finished == 'y') {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Anda sudah pernah berpartisipasi dalam quiz ini. Terimakasih.'], 404);
}else {
//Get last questions he/she left
$question = \App\Models\Question::where('quiz_id', $id)->where('id', '>', $myResult->first()->current_question)->get();
}
} else{
$question = \App\Models\Question::where('quiz_id', $id)->get();
}
if ($question) {
foreach ($question as $k => $v) {
$data['question'][$k]['id'] = $v->id;
$data['question'][$k]['type'] = $v->type;
$data['question'][$k]['text'] = $v->text;
$data['question'][$k]['timestamp'] = $v->created_at;
if ($v->type == 'pg') {
$data['question'][$k]['option_1'] = $v->pil_a;
$data['question'][$k]['option_2'] = $v->pil_b;
$data['question'][$k]['option_3'] = $v->pil_c;
$data['question'][$k]['option_4'] = $v->pil_d;
$data['question'][$k]['option_5'] = $v->pil_e;
$data['question'][$k]['option_6'] = $v->pil_f;
}else{
$data['question'][$k]['option_1'] = $v->pil_a;
$data['question'][$k]['option_2'] = $v->pil_b;
}
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function quizCheckAnswer($quiz_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
//Cek Quiz
$quiz = \App\Models\Quiz::find($quiz_id);
//Quiz tidak ditemukan
if (! $quiz) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Quiz tidak valid',404]);
}
//Asumsikan user belum pernah mengikuti kuis
$didParticipated = false;
$didFinished = false;
$currentScore = 0;
//Pertanyaan aktif adalah pertanyaan quiz yang pertama
$currentQuestion = \App\Models\Question::where('quiz_id', $quiz->id)->first()->id;
//Apakah user pernah mengikuti quiz ini?
$myResult = \App\Models\QuizResult::where('user_id', $userdata['data']['id'])->where('quiz_id', $quiz_id)->first();
//User pernah mengikuti
if (count($myResult) > 0) {
$didParticipated = true;
//Apakah user telah menjawab semua pertanyaan yang ada?
if ($myResult->is_finished == 'y') {
$didFinished = true;
$currentScore = $myResult->result;
//Tampilkan pesan dan hasil yang telah dicapai
$data['my-score'] = $currentScore;
return response()->json(['status' => false, 'data' => $data, 'message' => 'Anda sudah menyelesaikan quiz ini',404]);
}else{
$didFinished = false;
//User pernah mengikuti tapi belum menjawab semua pertanyaan
//Definisikan Hasil terakhir user
$currentScore = $myResult->result;
//Pertanyaan yang seharusnya dijawab oleh user
$currentQuestion = $myResult->current_question;
//Result adalah record yang sudah ada
$result = $myResult;
}
}else{
//User belum pernah mengikuti quiz ini
//Definisikan Quiz Result
$result = new \App\Models\QuizResult();
}
//User tidak mengirimkan data yang benar
if (!is_array($request->question_id)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak . Harap kirim response dalam bentuk array untuk masing-masing question \'question_id\'[id] => \'answer\''], 400);
}
//Check the answer
foreach ($request->question_id as $k => $v) {
//
if ($didParticipated) {
$currentQuestion = $myResult->current_question+1;
}else{
$currentQuestion = \App\Models\Question::where('id', $k)->first()->id+1;
}
// if ($currentQuestion !== $k){
// return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Pertanyaan tidak valid. Anda mengirimkan jawaban yang tidak . Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
// }
//Answer Correction
$question = \App\Models\Question::where('id', $currentQuestion)->first();
if ($question->correct_key == $request->question_id[$k]){
//Correct
$currentScore++;
}
$nextQuestion = \App\Models\Question::where('quiz_id', $quiz->id)->where('id', '>', $k)->first();
}
//Save the result
$result->quiz_id = $quiz->id;
$result->user_id = $userdata['data']['id'];
$result->question_count = \App\Models\Question::where('quiz_id', $quiz->id)->count();
$result->result = $currentScore;
if (count($nextQuestion) > 0) {
$data['next_question']['id'] = $nextQuestion->id;
$data['next_question']['type'] = $nextQuestion->type;
$data['next_question']['text'] = $nextQuestion->text;
if ($nextQuestion->type == 'pg') {
$data['next_question']['option_1'] = $nextQuestion->pil_a;
$data['next_question']['option_2'] = $nextQuestion->pil_b;
$data['next_question']['option_3'] = $nextQuestion->pil_c;
$data['next_question']['option_4'] = $nextQuestion->pil_d;
$data['next_question']['option_5'] = $nextQuestion->pil_e;
$data['next_question']['option_6'] = $nextQuestion->pil_f;
}else{
$data['next_question']['option_1'] = $nextQuestion->pil_a;
$data['next_question']['option_2'] = $nextQuestion->pil_b;
}
$result->is_finished = 'n';
}else{
$result->is_finished = 'y';
}
$result->current_question = $currentQuestion+1;
$result->save();
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function quizModule(Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
//Quiz Module
$quizmodule = \App\Models\QuizModule::latest()->get();
if ($quizmodule) {
foreach ($quizmodule as $k => $v) {
$data['quiz_module'][$k]['id'] = $v->id;
$data['quiz_module'][$k]['title'] = $v->id;
$data['quiz_module'][$k]['excerp'] = str_limit(strip_tags($v->text), 200);
$data['quiz_module'][$k]['timestamp'] = $v->created_at;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function quizModuleShow($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
//Quiz Module
$quizmodule = \App\Models\QuizModule::find($id);
if(! $quizmodule) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Module tidak valid'], 404);
}
$data['quiz_module']['id'] = $quizmodule->id;
$data['quiz_module']['title'] = $quizmodule->id;
$data['quiz_module']['text'] = strip_tags($quizmodule->text);
$data['quiz_module']['timestamp'] = $quizmodule->created_at;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function notification($take = 10, $skip = 0)
{
$userdata = Cektoken::cek(request('token'));
$data = [];
// $data['notificationCount'] = 0;
// $notification = \App\Models\Notification::leftJoin('notification_types', function($join){
// $join->on('notifications.notification_type', '=', 'notification_types.notification_type');
// })->where('user_id', $userdata['data']['id'])->orderBy('date_added', 'DESC')->take($take)->skip($skip)->get();
$notif = \App\Models\Notification::where('user_id', $userdata['data']['id'])->take($take)->skip($skip)->orderBy('notification_id', 'DESC')->get();
if($notif) {
foreach($notif as $k => $v) {
$getNotifType = DB::table('notification_types')->where('notification_type', $v->notification_type)->first();
if($getNotifType->is_request != "1"){
$data['latestNotification'][$k]['id'] = $v->notification_id;
$data['latestNotification'][$k]['subject_id'] = $v->subjectid;
$data['latestNotification'][$k]['subject_type'] = $v->subject_type;
$data['latestNotification'][$k]['notification_type'] = $v->notification_type;
$data['latestNotification'][$k]['object_id'] = $v->object_id;
$data['latestNotification'][$k]['object_type'] = $v->object_type;
$data['latestNotification'][$k]['kata-kata'] = 'Terdapat pembaruan dari ' . $v->object_type;
//$data['latestNotification'][$k]['timestamp'] = $v->created_at;
$data['latestNotification'][$k]['content'] = unserialize($v->param);
$params = unserialize($v->param);
if($params['subject'] == ""){
$vars = array('{SUBJECT}' => "<strong>Admin</strong>", '{OBJECT}' => "<strong>" . $params['object'] . "</strong>");
}else{
$vars = array('{SUBJECT}' => "<strong>" . $params['subject'] . "</strong>", '{OBJECT}' => "<strong>" . $params['object'] . "</strong>");
}
$varBody = strtr($getNotifType->body, $vars);
$data['latestNotification'][$k]['varBody'] = $varBody;
}
}
}
$data['notificationCount'] = \App\Models\Notification::where('user_id', $userdata['data']['id'])->where('read', '0')->get()->count();
//\App\Library\NotificationLibrary::setAsReadAllNotification($userdata['data']['id']);
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function timeline(Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$posts = \App\Models\ActivityAction::where('activity_type', 'status')->where('object_type', 'user')->orderBy('action_id', 'DESC')->take(20)->skip(0)->get();
if ($posts) {
foreach ($posts as $k => $v) {
$is_liked = \App\Models\ActivityLikes::where('action_id', $v->action_id)->where('user_id', $userdata['data']['id'])->first();
$data['post'][$k]['id'] = $v->action_id;
$data['post'][$k]['text'] = $v->body;
$data['post'][$k]['image'] = null;
$data['post'][$k]['comment_count'] = \App\Models\ActivityComment::where('action_id', $v->action_id)->count();
$data['post'][$k]['like_count'] = \App\Models\ActivityLikes::where('action_id', $v->action_id)->count();
$data['post'][$k]['is_liked'] = ($is_liked) ? '1' : '0';
$data['post'][$k]['timestamp'] = $v->date_added;
$postImg = \App\Models\ImagesTable::where('parent_type', 'user_status')->where('parent_id', $v->action_id)->first();
if ($postImg) {
$data['post'][$k]['image'] = $postImg->path;
}
$user = User::select('id', 'first_name', 'last_name')->where('id', '=', $v->user_id)->first();
$photo = \App\Library\CustomLibrary::ambilGambarCoy($user->id);
$data['post'][$k]['user']['id'] = $user->id;
$data['post'][$k]['user']['first_name'] = $user->first_name;
$data['post'][$k]['user']['last_name'] = $user->last_name;
$data['post'][$k]['user']['photo_profile'] = $photo;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function timelinePost(Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$status = new \App\Models\ActivityAction();
$status->activity_type = 'status';
$status->subject_type = 'user';
$status->user_id = $userdata['data']['id'];
$status->object_type = 'user';
$status->body = $request->text;
$status->save();
if ($request->hasFile('image')) {
$intervention = Image::make($request->file('image'));
$fileName = str_random(10) . '.png';
$destinationPath = 'images/user_status';
$intervention->save($destinationPath.'/'.$fileName);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $status->action_id;
$imageTable->parent_type = 'user_status';
$imageTable->path = $fileName;
$imageTable->save();
}
$data['post']['id'] = $status->action_id;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Behasil mengirimkan post'],200);
}
public function timelinePostAndroid(Request $request){
$userdata = Cektoken::cek($request->token);
$data = [];
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$status = new \App\Models\ActivityAction();
$status->activity_type = 'status';
$status->subject_type = 'user';
$status->user_id = $userdata['data']['id'];
$status->object_type = 'user';
$status->body = $request->text;
$status->save();
if ($request->has('image')) {
$destinationPath = 'images/user_status/';
$fileName = uniqid().'.png';
$img = $request->image;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $status->action_id;
$imageTable->parent_type = 'user_status';
$imageTable->path = $fileName;
$imageTable->save();
}
$data['post']['id'] = $status->action_id;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Behasil mengirimkan post'],200);
}
public function timelineGetSingle($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$post = \App\Models\ActivityAction::find($id);
if (! $post) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Post tidak ditemukan'],404);
}
$data['post']['id'] = $post->action_id;
$data['post']['text'] = $post->body;
$data['post']['comment_count'] = \App\Models\ActivityComment::where('action_id', $post->action_id)->count();
$data['post']['like_count'] = \App\Models\ActivityLikes::where('action_id', $post->action_id)->count();
$is_liked = \App\Models\ActivityLikes::where('action_id', $post->action_id)->where('user_id', $userdata['data']['id'])->first();
$data['post']['is_liked'] = ($is_liked) ? '1' : '0';
$data['post']['timestamp'] = $post->date_added;
//Check if the post has image
$cekImage = \App\Models\ImagesTable::where('parent_type', 'user_status')->where('parent_id', $post->action_id)->first();
if ($cekImage) {
$data['post']['image'] = $cekImage->path;
}
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$post->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['post']['user']['id'] = $getDataUser->id;
$data['post']['user']['first_name'] = $getDataUser->first_name;
$data['post']['user']['last_name'] = $getDataUser->last_name;
$data['post']['user']['photo_profile'] = $image;
$data['comment'] = [];
$comment = \App\Models\ActivityComment::where('action_id', $post->action_id)->get();
if ($comment) {
foreach ($comment as $k => $v) {
$data['comment'][$k]['id'] = $v->activity_comment_id;
$data['comment'][$k]['text'] = $v->body;
$data['comment'][$k]['timestamp'] = $v->date_added;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['comment'][$k]['user']['id'] = $getDataUser->id;
$data['comment'][$k]['user']['first_name'] = $getDataUser->first_name;
$data['comment'][$k]['user']['last_name'] = $getDataUser->last_name;
$data['comment'][$k]['user']['photo_profile'] = $image;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''],200);
}
public function timelineComment($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$comment = \App\Models\ActivityComment::find($id);
if (! $comment) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Comment tidak ditemukan'],404);
}
$data['comment']['id'] = $comment->activity_comment_id;
$data['comment']['text'] = $comment->body;
$data['comment']['timestamp'] = $comment->date_added;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$comment->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['comment']['user']['id'] = $getDataUser->id;
$data['comment']['user']['first_name'] = $getDataUser->first_name;
$data['comment']['user']['last_name'] = $getDataUser->last_name;
$data['comment']['user']['photo_profile'] = $image;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function timelineCommentPost($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$post = \App\Models\ActivityAction::find($id);
if (! $post) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Post tidak ditemukan'],404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$comment = new \App\Models\ActivityComment();
$comment->user_id = $userdata['data']['id'];
$comment->action_id = $post->action_id;
$comment->body = $request->text;
$comment->save();
$data['id'] = $comment->activity_comment_id;
$data['text'] = $comment->body;
$data['timestamp'] = $comment->date_added;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$comment->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['user']['id'] = $getDataUser->id;
$data['user']['first_name'] = $getDataUser->first_name;
$data['user']['last_name'] = $getDataUser->last_name;
$data['user']['photo_profile'] = $image;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan Comment'], 200);
}
public function timelineDelete($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$post = \App\Models\ActivityAction::find($id);
if (! $post) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Post tidak ditemukan'],404);
}
\App\Models\ActivityComment::where('action_id', $post->action_id)->delete();
$post->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus post beserta komentarnya'],200);
}
public function timelineCommentDelete($id, Request $request)
{
$userdata = Cektoken::cek($request->token);
$data = [];
$comment= \App\Models\ActivityComment::find($id);
if (! $comment) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Komentar tidak ditemukan'],404);
}
$comment->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menghapus komentar'],200);
}
public function community(Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$community = \App\Models\Community::all();
if (! $community) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Belum ada komunitas.'],200);
}
foreach ($community as $k => $v) {
$data['community'][$k]['id'] = $v->group_id;
$data['community'][$k]['title'] = $v->title;
$data['community'][$k]['description'] = $v->description;
$cover = \App\Models\ImagesTable::where('parent_type','=','community_cover_img')->where('parent_id','=',$v->group_id)->first();
$data['community'][$k]['cover_photo'] = ($cover) ? $cover->path : null;
$data['community'][$k]['icon'] = $v->icon;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function communityShow($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$community = \App\Models\Community::find($id);
if (! $community) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Komunitas tidak ditemukan'],404);
}
//Viewer
$community_view = \App\Models\CommunityView::where('group_id', $community->group_id)->where('user_id', $userdata['data']['id'])->count();
if ($community_view == 0) {
$view = new \App\Models\CommunityView();
$view->group_id = $community->group_id;
$view->user_id = $userdata['data']['id'];
$view->save();
}
$data['community']['id'] = $community->group_id;
$data['community']['title'] = $community->title;
$data['community']['description'] = $community->description;
$cover = \App\Models\ImagesTable::where('parent_type','=','community_cover_img')->where('parent_id','=',$community->group_id)->first();
$data['community']['cover_photo'] = ($cover) ? $cover->path : null;
$data['community']['icon'] = $community->icon;
$isMember = \App\Models\CommunityMember::where('group_id', $community->group_id)->where('sender_id', $userdata['data']['id'])->get();
if (count($isMember) > 0) {
if ($isMember->first()->approved == '1') {
$data['community']['membership_status'] = 'joined';
}elseif ($isMember->first()->approved == '0') {
$data['community']['membership_status'] = 'waiting_approval';
}
}else{
$data['community']['membership_status'] = 'not_joined';
}
//Community Timeline
$data['post'] = [];
$posts = \App\Models\ActivityAction::where('object_type', 'group')->where('group_id', $community->group_id)->orderBy('action_id', 'DESC')->take(20)->get();
if ($posts){
foreach ($posts as $k => $v) {
$data['post'][$k]['id'] = $v->action_id;
$data['post'][$k]['text'] = $v->body;
$data['post'][$k]['image'] = '';
$postImage = \App\Models\ImagesTable::where('parent_type', 'user_status')->where('parent_id', $v->action_id)->first();
if ($postImage) {
$data['post'][$k]['image'] = $postImage->path;
}
//Comment Count
$data['post'][$k]['comment_count'] = \App\Models\ActivityComment::where('action_id', $v->action_id)->count();
//Like Count
$data['post'][$k]['like_count'] = \App\Models\ActivityLikes::where('action_id', $v->action_id)->count();
//Is Liked post
$is_liked = \App\Models\ActivityLikes::where('action_id', $v->action_id)->where('user_id', $userdata['data']['id'])->first();
$data['post'][$k]['is_liked'] = ($is_liked) ? '1' : '0';
$data['post'][$k]['timestamp'] = $v->date_added;
//Post User
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['post'][$k]['user']['id'] = $getDataUser->id;
$data['post'][$k]['user']['first_name'] = $getDataUser->first_name;
$data['post'][$k]['user']['last_name'] = $getDataUser->last_name;
$data['post'][$k]['user']['photo_profile'] = $image;
}
}
//Community Member
$data['member'] = [];
$member = \App\Models\CommunityMember::where('group_id', $community->group_id)->where('approved', '1')->get();
if ($member) {
foreach ($member as $k => $v) {
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->member_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['member'][$k]['id'] = $getDataUser->id;
$data['member'][$k]['first_name'] = $getDataUser->first_name;
$data['member'][$k]['last_name'] = $getDataUser->last_name;
$data['member'][$k]['photo_profile'] = $image;
}
}
//Gallery
$gallery = \App\Models\ImagesTable::where('parent_type', 'group_photo')->where('parent_id', $community->group_id)->orderBy('image_id', 'DESC')->get();
if ($gallery) {
foreach ($gallery as $k => $v) {
$data['gallery'][$k]['id'] = $v->image_id;
$data['gallery'][$k]['file_path'] = $v->path;
$data['gallery'][$k]['title'] = $v->title;
$data['gallery'][$k]['description'] = $v->description;
$data['gallery'][$k]['timestamp'] = $v->added_date;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function communityJoin($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$community = \App\Models\Community::find($id);
if (! $community) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Komunitas tidak ditemukan'],404);
}
$isMember = \App\Models\CommunityMember::where('group_id', $community->group_id)->where('sender_id', $userdata['data']['id']);
if ($isMember->count() > 0) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Permintaan sudah pernah dikirim'],400);
}
$membership = new \App\Models\CommunityMember();
$membership->sender_id = $userdata['data']['id'];
$membership->group_id = $community->group_id;
$membership->reciever_id = '1';
$membership->member_id = $userdata['data']['id'];
$membership->approved = '0';
$membership->role = '0';
$membership->approved_by = '1';
$membership->group_approved = '0';
$membership->save();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Permintaan bergabung telah dikirim'],200);
}
public function communityLeave($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$community = \App\Models\Community::find($id);
if (! $community) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Komunitas tidak ditemukan'],404);
}
$membership = \App\Models\CommunityMember::where('group_id', $community->group_id)->where('sender_id', $userdata['data']['id'])->first();
$membership->delete();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil meninggalkan komunitas'],200);
}
public function communityGetSinglePost($id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$post = \App\Models\ActivityAction::find($id);
if (! $post) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Post tidak ditemukan'],404);
}
$data['post']['id'] = $post->action_id;
$data['post']['text'] = $post->body;
$data['post']['comment_count'] = \App\Models\ActivityComment::where('action_id', $post->action_id)->count();
$data['post']['like_count'] = \App\Models\ActivityLikes::where('action_id', $post->action_id)->count();
$is_liked = \App\Models\ActivityLikes::where('action_id', $post->action_id)->where('user_id', $userdata['data']['id'])->first();
$data['post']['is_liked'] = ($is_liked) ? '1' : '0';
$data['post']['timestamp'] = $post->date_added;
//Check if the post has image
$cekImage = \App\Models\ImagesTable::where('parent_type', 'user_status')->where('parent_id', $post->action_id)->first();
if ($cekImage) {
$data['post']['image'] = $cekImage->path;
}
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$post->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['post']['user']['id'] = $getDataUser->id;
$data['post']['user']['first_name'] = $getDataUser->first_name;
$data['post']['user']['last_name'] = $getDataUser->last_name;
$data['post']['user']['photo_profile'] = $image;
$data['comment'] = [];
$comment = \App\Models\ActivityComment::where('action_id', $post->action_id)->get();
if ($comment) {
foreach ($comment as $k => $v) {
$data['comment'][$k]['id'] = $v->activity_comment_id;
$data['comment'][$k]['text'] = $v->body;
$data['comment'][$k]['timestamp'] = $v->date_added;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['comment'][$k]['user']['id'] = $getDataUser->id;
$data['comment'][$k]['user']['first_name'] = $getDataUser->first_name;
$data['comment'][$k]['user']['last_name'] = $getDataUser->last_name;
$data['comment'][$k]['user']['photo_profile'] = $image;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''],200);
}
public function communityComment($post_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$comment = \App\Models\ActivityComment::find($post_id);
if (! $comment) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Comment tidak ditemukan'],404);
}
$data['comment']['id'] = $comment->activity_comment_id;
$data['comment']['text'] = $comment->body;
$data['comment']['timestamp'] = $comment->date_added;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$comment->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['comment']['user']['id'] = $getDataUser->id;
$data['comment']['user']['first_name'] = $getDataUser->first_name;
$data['comment']['user']['last_name'] = $getDataUser->last_name;
$data['comment']['user']['photo_profile'] = $image;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function comunityPost($community_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
$community = \App\Models\Community::find($community_id);
if (! $community) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Komunitas tidak ditemukan'],404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$status = new \App\Models\ActivityAction();
$status->activity_type = 'status';
$status->subject_type = 'user';
$status->user_id = $userdata['data']['id'];
$status->object_type = 'group';
$status->group_id = $community->group_id;
$status->body = $request->text;
$status->save();
if ($request->hasFile('image')) {
$intervention = Image::make($request->file('image'));
$fileName = str_random(10) . '.png';
$destinationPath = 'images/user_status';
$intervention->save($destinationPath.'/'.$fileName);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $status->action_id;
$imageTable->parent_type = 'group_photo';
$imageTable->path = $fileName;
$imageTable->save();
}
$data['post']['id'] = $status->action_id;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Behasil mengirimkan post'],200);
}
public function communityPostAndroid($community_id,Request $request){
$userdata = Cektoken::cek($request->token);
$data = [];
$community = \App\Models\Community::find($community_id);
if (! $community) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Komunitas tidak ditemukan'],404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$status = new \App\Models\ActivityAction();
$status->activity_type = 'status';
$status->subject_type = 'user';
$status->user_id = $userdata['data']['id'];
$status->object_type = 'group';
$status->group_id = $community->group_id;
$status->body = $request->text;
$status->save();
if ($request->has('image')) {
$destinationPath = 'images/user_status/';
$fileName = uniqid().'.png';
$img = $request->image;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$gam = base64_decode($img);
file_put_contents($destinationPath.$fileName, $gam);
$imageTable = new \App\Models\ImagesTable();
$imageTable->parent_id = $status->action_id;
$imageTable->parent_type = 'group_photo';
$imageTable->path = $fileName;
$imageTable->save();
}
$data['post']['id'] = $status->action_id;
return response()->json(['status' => true, 'data' => $data, 'message' => 'Behasil mengirimkan post'],200);
}
public function adk(Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$adk = \App\Models\Adk::orderBy('id', 'DESC')->get();
if(! $adk) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada ADK'], 200);
}
foreach ($adk as $k => $v) {
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['adk'][$k]['id'] = $v->id;
$data['adk'][$k]['user_id'] = $getDataUser->id;
$data['adk'][$k]['first_name'] = $getDataUser->first_name;
$data['adk'][$k]['last_name'] = $getDataUser->last_name;
$data['adk'][$k]['photo_profile'] = $image;
//Jumlah Pertanyaan
$data['adk'][$k]['pertanyaan'] = \App\Models\AdkPertanyaan::where('untuk', $v->id)->count();
$data['adk'][$k]['jawaban'] = \App\Models\AdkJawaban::where('user_id', $v->user_id)->count();
}
$tiket = \App\Models\AdkPertanyaan::where('dari', $userdata['data']['id'])->get();
if ($tiket) {
foreach ($tiket as $k => $v) {
$data['my_ticket'][$k]['id'] = $v->id;
if($v->status == '2'){
$status = 'closed';
}elseif($v->status == '1'){
$status = 'approved';
}else {
$status = 'waiting';
}
$data['my_ticket'][$k]['status'] = $status;
$data['my_ticket'][$k]['text'] = $v->pertanyaan;
$data['my_ticket'][$k]['timestamp'] = $v->created_at;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->dari)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['my_ticket'][$k]['dari']['id'] = $v->dari;
$data['my_ticket'][$k]['dari']['user_id'] = $getDataUser->id;
$data['my_ticket'][$k]['dari']['first_name'] = $getDataUser->first_name;
$data['my_ticket'][$k]['dari']['last_name'] = $getDataUser->last_name;
$data['my_ticket'][$k]['dari']['photo_profile'] = $image;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',\App\Models\Adk::find($v->untuk)->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['my_ticket'][$k]['untuk']['id'] = $v->untuk;
$data['my_ticket'][$k]['untuk']['user_id'] = $getDataUser->id;
$data['my_ticket'][$k]['untuk']['first_name'] = $getDataUser->first_name;
$data['my_ticket'][$k]['untuk']['last_name'] = $getDataUser->last_name;
$data['my_ticket'][$k]['untuk']['photo_profile'] = $image;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function adkShow($id, Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$adk = \App\Models\Adk::find($id);
if(! $adk) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'ADK tidak valid'], 404);
}
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$adk->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['adk']['id'] = $adk->id;
$data['adk']['user_id'] = $getDataUser->id;
$data['adk']['first_name'] = $getDataUser->first_name;
$data['adk']['last_name'] = $getDataUser->last_name;
$data['adk']['photo_profile'] = $image;
//Pertanyaan
$tiket = \App\Models\AdkPertanyaan::where('untuk', $adk->id)->where('status', '2')->get();
if ($tiket) {
foreach ($tiket as $k => $v) {
$data['ticket'][$k]['id'] = $v->id;
$data['ticket'][$k]['text'] = $v->pertanyaan;
$data['ticket'][$k]['timestamp'] = $v->created_at;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->dari)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['ticket'][$k]['dari']['id'] = $v->dari;
$data['ticket'][$k]['dari']['user_id'] = $getDataUser->id;
$data['ticket'][$k]['dari']['first_name'] = $getDataUser->first_name;
$data['ticket'][$k]['dari']['last_name'] = $getDataUser->last_name;
$data['ticket'][$k]['dari']['photo_profile'] = $image;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function adkPostPertanyaan($adk_id, Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$adk = \App\Models\Adk::find($adk_id);
if(! $adk) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'ADK tidak valid'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$ticket = new \App\Models\AdkPertanyaan();
$ticket->dari = $userdata['data']['id'];
$ticket->untuk = $adk->id;
$ticket->pertanyaan = $request->text;
$ticket->status = 0;
$ticket->save();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan pertanyaan ADK'],200);
}
public function adkPostJawaban($pertanyaan_id, Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$pertanyaan = \App\Models\AdkPertanyaan::find($pertanyaan_id);
if(! $pertanyaan) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Pertanyaan tidak valid'], 404);
}
$rules = [
'text' => 'required|min:3'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$jawaban = new \App\Models\AdkJawaban();
$jawaban->pertanyaan_id = $pertanyaan->id;
$jawaban->user_id = $userdata['data']['id'];
$jawaban->jawaban = $request->text;
$jawaban->status = '1';
$jawaban->save();
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan pertanyaan ADK'],200);
}
public function adkTicketDetail($ticket_id, Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$ticket = \App\Models\AdkPertanyaan::find($ticket_id);
if(! $ticket) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Ticket tidak valid'], 404);
}
if ($ticket->status == 0){
if ($userdata['data']['id'] != '1' AND $userdata['data']['id'] != $ticket->dari){
return response()->json(['status' => true, 'data' => $data, 'message' => 'Ticket tidak valid'], 404);
}
}
if ($ticket->status == 1){
if ($userdata['data']['id'] != '1' AND $userdata['data']['id'] != $ticket->dari AND $userdata['data']['id'] != $ticket->untuk){
return response()->json(['status' => true, 'data' => $data, 'message' => 'Ticket tidak valid'], 404);
}
}
//Ticket Definition
$data['ticket']['id'] = $ticket->id;
if($ticket->status == '2'){
$status = 'closed';
}elseif($ticket->status == '1'){
$status = 'approved';
}else {
$status = 'waiting';
}
$data['ticket']['status'] = $status;
$data['ticket']['text'] = $ticket->pertanyaan;
$data['ticket']['timestamp'] = $ticket->created_at;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$ticket->dari)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['ticket']['dari']['id'] = $ticket->dari;
$data['ticket']['dari']['user_id'] = $getDataUser->id;
$data['ticket']['dari']['first_name'] = $getDataUser->first_name;
$data['ticket']['dari']['last_name'] = $getDataUser->last_name;
$data['ticket']['dari']['photo_profile'] = $image;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',\App\Models\Adk::find($ticket->untuk)->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['ticket']['untuk']['id'] = $ticket->untuk;
$data['ticket']['untuk']['user_id'] = $getDataUser->id;
$data['ticket']['untuk']['first_name'] = $getDataUser->first_name;
$data['ticket']['untuk']['last_name'] = $getDataUser->last_name;
$data['ticket']['untuk']['photo_profile'] = $image;
//Jawaban
$jawaban = \App\Models\AdkJawaban::where('pertanyaan_id', $ticket->id)->get();
if ($jawaban) {
foreach ($jawaban as $k => $v) {
$data['jawaban'][$k]['id'] = $v->id;
$data['jawaban'][$k]['text'] = $v->jawaban;
$data['jawaban'][$k]['timestamp'] = $v->created_at;
$getDataUser = User::select('id','first_name','last_name')->where('id','=',$v->user_id)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['jawaban'][$k]['user']['user_id'] = $getDataUser->id;
$data['jawaban'][$k]['user']['first_name'] = $getDataUser->first_name;
$data['jawaban'][$k]['user']['last_name'] = $getDataUser->last_name;
$data['jawaban'][$k]['user']['photo_profile'] = $image;
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function gallery($take = 20, $skip = 0, Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$gallery = \App\Models\ImagesTable::where('parent_type', 'vid')->orWhere('parent_type', 'img')->take($take)->skip($skip)->get();
if(! $gallery) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada galeri yang tersedia'], 200);
}
foreach ($gallery as $k => $v) {
$data['gallery'][$k]['id'] = $v->image_id;
$data['gallery'][$k]['type'] = ($v->parent_type == 'vid') ? 'video' : 'image';
$data['gallery'][$k]['title'] = $v->title;
$data['gallery'][$k]['url'] = $v->path;
$data['gallery'][$k]['timestamp'] = $v->added_date;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function galleryImage($page = "", Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
if($page == ""){
$page = 1;
}
$perhalaman = 20;
$hitungSkip = ($page - 1 ) * $perhalaman;
$gallery = \App\Models\ImagesTable::where('parent_type', 'img')->take($perhalaman)->skip($hitungSkip)->get();
if(! $gallery) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada galeri yang tersedia'], 200);
}
foreach ($gallery as $k => $v) {
$data['gallery'][$k]['id'] = $v->image_id;
$data['gallery'][$k]['type'] = ($v->parent_type == 'vid') ? 'video' : 'image';
$data['gallery'][$k]['title'] = $v->title;
$data['gallery'][$k]['url'] = $v->path;
$data['gallery'][$k]['timestamp'] = $v->added_date;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function galleryVideo($page = "", Request $request) {
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
if($page == ""){
$page = 1;
}
$perhalaman = 20;
$hitungSkip = ($page - 1 ) * $perhalaman;
$gallery = \App\Models\ImagesTable::where('parent_type', 'vid')->take($perhalaman)->skip($hitungSkip)->get();
if(! $gallery) {
return response()->json(['status' => true, 'data' => $data, 'message' => 'Belum ada galeri yang tersedia'], 200);
}
foreach ($gallery as $k => $v) {
$data['gallery'][$k]['id'] = $v->image_id;
$data['gallery'][$k]['type'] = ($v->parent_type == 'vid') ? 'video' : 'image';
$data['gallery'][$k]['title'] = $v->title;
$data['gallery'][$k]['url'] = $v->path;
$data['gallery'][$k]['timestamp'] = $v->added_date;
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function latest_chat(Request $request)
{
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$latest_chat = \App\Models\LatestChat::where('user_id_to', $userdata['data']['id'])
->orWhere('user_id_from', $userdata['data']['id'])
->orderBy('id', 'DESC')
->get();
if ($latest_chat) {
foreach ($latest_chat as $k => $v) {
$data['chat'][$k]['id'] = $v->id;
$data['chat'][$k]['text'] = $v->text;
$data['chat'][$k]['timestamp'] = $v->created_at;
//From
$getDataUser = User::select('id','first_name','last_name')->where('id',$v->user_id_from)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUser->id);
$data['chat'][$k]['from']['id'] = $getDataUser->id;
$data['chat'][$k]['from']['first_name'] = $getDataUser->first_name;
$data['chat'][$k]['from']['last_name'] = $getDataUser->last_name;
$data['chat'][$k]['from']['photo_profile'] = $image;
//To
$getDataUserTo = User::select('id','first_name','last_name')->where('id',$v->user_id_to)->first();
$image = \App\Library\CustomLibrary::ambilGambarCoy($getDataUserTo->id);
$data['chat'][$k]['to']['id'] = $getDataUserTo->id;
$data['chat'][$k]['to']['first_name'] = $getDataUserTo->first_name;
$data['chat'][$k]['to']['last_name'] = $getDataUserTo->last_name;
$data['chat'][$k]['to']['photo_profile'] = $image;
if ($userdata['data']['id'] == $v->user_id_to) {
$data['chat'][$k]['unread'] = \App\Models\LatestChat::where('user_id_from', $v->user_id_from)->where('user_id_to', $v->user_id_to)->where('status', '0')->count();
}elseif ($userdata['data']['id'] == $v->user_id_from) {
$data['chat'][$k]['unread'] = \App\Models\LatestChat::where('user_id_from', $v->user_id_to)->where('user_id_to', $v->user_id_from)->where('status', '0')->count();
}
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function latest_chat_count(Request $request)
{
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$latest_chat = \App\Models\LatestChat::where('user_id_to', $userdata['data']['id'])->where('status', '0')->count();
$data['unread_chat'] = $latest_chat;
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function latest_chat_post(Request $request){
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
$rules = [
'user_id_from' => 'required',
'user_id_to' => 'required',
'text' => 'required'
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'data' => $data, 'message' => $validator->messages()],200);
}
$latestChat = new \App\Models\LatestChat();
$latestChat->user_id_from = $request->user_id_from;
$latestChat->user_id_to = $request->user_id_to;
$latestChat->text = $request->text;
$latestChat->status = '0';
$latestChat->save();
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
public function readChat($to, Request $request)
{
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
\App\Models\LatestChat::where('user_id_from','=',$to)->where('user_id_to', $userdata['data']['id'])->update(["status"=>"1"]);
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil mengupdate status read chat'], 200);
}
public function search($keyword, Request $request)
{
$userdata = \App\Library\CekToken::cek($request->token);
$data = [];
//User
$user = \App\User::where('first_name', 'LIKE', '%' . $keyword . '%')
->orWhere('last_name', 'LIKE', '%' . $keyword . '%')
->orWhere('username', 'LIKE', '%' . $keyword . '%')
->get();
if ($user) {
foreach ($user as $k => $v) {
$data['user'][$k]['display_name'] = $v->first_name . ' ' . $v->last_name;
$data['user'][$k]['url'] = url('profile') . '/' . $v->id;
$image = \App\Library\CustomLibrary::ambilGambarCoy($v->id);
$data['user'][$k]['avatar'] = url() . $image;
$data['user'][$k]['type'] = 'user';
}
}
//FJB
$fjb = \App\Models\FjbThread::where('forum_title', 'LIKE', '%' . $keyword . '%')
->orWhere('forum_description', 'LIKE', '%' . $keyword . '%')
->get();
if ($fjb) {
foreach ($fjb as $k => $v) {
$data['fjb'][$k]['display_name'] = $v->forum_title;
$data['fjb'][$k]['url'] = url('fjb/thread') . '/' . $v->id;
$image = \App\Models\ImagesTable::select('path' )->where('parent_type','=','commerce_photo')->where('parent_id','=',$v->id)->first();
$data['fjb'][$k]['avatar'] = ($image) ? url('images/commerce') . '/' . $image->path : null;
$data['fjb'][$k]['type'] = 'fjb';
}
}
//Community
$community = \App\Models\Community::where('title', 'LIKE', '%' . $keyword . '%')
->orWhere('description', 'LIKE', '%' . $keyword . '%')
->get();
if ($community) {
foreach ($community as $k => $v) {
$data['community'][$k]['display_name'] = $v->title;
$data['community'][$k]['url'] = url('community') . '/' . $v->group_id;
$data['community'][$k]['icon'] = $v->icon;
$data['community'][$k]['type'] = 'community';
}
}
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.