ConversationController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use App\Notifications\OrderNotification;
  5. use Illuminate\Http\Request;
  6. use App\Models\Conversation;
  7. use App\Models\BusinessSetting;
  8. use App\Models\Message;
  9. use App\Models\Product;
  10. use Auth;
  11. use Mail;
  12. use App\Mail\ConversationMailManager;
  13. use App\Models\ProductQuery;
  14. use Illuminate\Support\Facades\Notification;
  15. class ConversationController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function check_new_msg()
  23. {
  24. $uid = $pid = Auth::user()->id;
  25. if( $uid )
  26. {
  27. $have_no_read = Conversation::where( 'receiver_id',$uid )->where('is_tip',0 )->first();
  28. if($have_no_read && $have_no_read['id'] )
  29. {
  30. $c = Conversation::findOrFail( $have_no_read['id'] );
  31. $c->is_tip = 1;
  32. $c->save();
  33. echo json_encode( ['code'=>1, 'msg'=> 'Yes'] );exit;
  34. }
  35. }
  36. echo json_encode( ['code'=>0, 'msg'=> 'No'] );exit;
  37. }
  38. public function check_new_reply()
  39. {
  40. $uid = $pid = Auth::user()->id;
  41. if( $uid )
  42. {
  43. $list = Conversation::where( 'sender_id',$uid )->get()->toArray();
  44. $ids= [0];
  45. foreach($list as $v)
  46. {
  47. array_push( $ids, $v['id']);
  48. }
  49. $message = Message::whereIn('conversation_id', $ids)->where('is_tip',0)->first();
  50. if( $message['id'] )
  51. {
  52. $c = Message::findOrFail( $message['id'] );
  53. $c->is_tip = 1;
  54. $c->save();
  55. echo json_encode( ['code'=>1, 'msg'=> 'Yes'] );exit;
  56. }
  57. }
  58. echo json_encode( ['code'=>0, 'msg'=> 'No'] );exit;
  59. }
  60. public function index()
  61. {
  62. if (BusinessSetting::where('type', 'conversation_system')->first()->value == 1) {
  63. $conversations = Conversation::where('sender_id', Auth::user()->id)->orWhere('receiver_id', Auth::user()->id)->orderBy('created_at', 'desc')->paginate(5);
  64. return view('frontend.user.conversations.index', compact('conversations'));
  65. }
  66. else {
  67. flash(translate('Conversation is disabled at this moment'))->warning();
  68. return back();
  69. }
  70. }
  71. /**
  72. * Display a listing of the resource.
  73. *
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function admin_index()
  77. {
  78. if (BusinessSetting::where('type', 'conversation_system')->first()->value == 1) {
  79. $conversations = Conversation::orderBy('created_at', 'desc')->get();
  80. return view('backend.support.conversations.index', compact('conversations'));
  81. }
  82. else {
  83. flash(translate('Conversation is disabled at this moment'))->warning();
  84. return back();
  85. }
  86. }
  87. /**
  88. * Show the form for creating a new resource.
  89. *
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function create()
  93. {
  94. //
  95. }
  96. /**
  97. * Store a newly created resource in storage.
  98. *
  99. * @param \Illuminate\Http\Request $request
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function store(Request $request)
  103. {
  104. $user_type = Product::findOrFail($request->product_id)->user->user_type;
  105. $conversation = new Conversation;
  106. $conversation->sender_id = Auth::user()->id;
  107. $conversation->receiver_id = Product::findOrFail($request->product_id)->user->id;
  108. $conversation->title = $request->title;
  109. if($conversation->save()) {
  110. $message = new Message;
  111. $message->conversation_id = $conversation->id;
  112. $message->user_id = Auth::user()->id;
  113. $message->message = $request->message;
  114. if ($message->save()) {
  115. $this->send_message_to_seller($conversation, $message, $user_type);
  116. }
  117. }
  118. flash(translate('Message has been sent to seller'))->success();
  119. return back();
  120. }
  121. public function admin_store(Request $request)
  122. {
  123. $order_notification = array();
  124. $order_notification['order_id'] = 0;
  125. $order_notification['order_code'] = $request->title;
  126. $order_notification['user_id'] = $request->receiver_id;
  127. $order_notification['seller_id'] = $request->receiver_id;
  128. $order_notification['status'] = 'confirmed';
  129. $users = User::findMany([$request->receiver_id]);
  130. Notification::send($users, new OrderNotification($order_notification));
  131. flash(translate('Message has been sent to seller'))->success();
  132. return back();
  133. }
  134. public function salesman_store(Request $request)
  135. {
  136. $order_notification = array();
  137. $order_notification['order_id'] = 0;
  138. $order_notification['order_code'] = $request->title;
  139. $order_notification['user_id'] = $request->receiver_id;
  140. $order_notification['seller_id'] = $request->receiver_id;
  141. $order_notification['status'] = 'confirmed';
  142. $users = User::findMany([$request->receiver_id]);
  143. Notification::send($users, new OrderNotification($order_notification));
  144. flash(translate('Message has been sent to seller'))->success();
  145. return back();
  146. }
  147. public function send_message_to_seller($conversation, $message, $user_type)
  148. {
  149. $array['view'] = 'emails.conversation';
  150. $array['subject'] = 'Sender:- '.Auth::user()->name;
  151. $array['from'] = env('MAIL_FROM_ADDRESS');
  152. $array['content'] = 'Hi! You recieved a message from '.Auth::user()->name.'.';
  153. $array['sender'] = Auth::user()->name;
  154. if($user_type == 'admin') {
  155. $array['link'] = route('conversations.admin_show', encrypt($conversation->id));
  156. } else {
  157. $array['link'] = route('conversations.show', encrypt($conversation->id));
  158. }
  159. $array['details'] = $message->message;
  160. try {
  161. Mail::to($conversation->receiver->email)->queue(new ConversationMailManager($array));
  162. } catch (\Exception $e) {
  163. //dd($e->getMessage());
  164. }
  165. }
  166. /**
  167. * Display the specified resource.
  168. *
  169. * @param int $id
  170. * @return \Illuminate\Http\Response
  171. */
  172. public function show($id)
  173. {
  174. $conversation = Conversation::findOrFail(decrypt($id));
  175. if ($conversation->sender_id == Auth::user()->id) {
  176. $conversation->sender_viewed = 1;
  177. }
  178. elseif($conversation->receiver_id == Auth::user()->id) {
  179. $conversation->receiver_viewed = 1;
  180. }
  181. $conversation->save();
  182. return view('frontend.user.conversations.show', compact('conversation'));
  183. }
  184. /**
  185. * Display the specified resource.
  186. *
  187. * @param int $id
  188. * @return \Illuminate\Http\Response
  189. */
  190. public function refresh(Request $request)
  191. {
  192. $conversation = Conversation::findOrFail(decrypt($request->id));
  193. foreach($conversation->messages as $message) {
  194. if( $message->user_id != Auth::user()->id ) {
  195. $message->updated_at = date('Y-m-d H:i:s');
  196. $message->save();
  197. }
  198. }
  199. if($conversation->sender_id == Auth::user()->id){
  200. $conversation->sender_viewed = 1;
  201. $conversation->save();
  202. }
  203. else{
  204. $conversation->receiver_viewed = 1;
  205. $conversation->save();
  206. }
  207. return view('frontend.partials.messages', compact('conversation'));
  208. }
  209. /**
  210. * Display the specified resource.
  211. *
  212. * @param int $id
  213. * @return \Illuminate\Http\Response
  214. */
  215. public function admin_show($id)
  216. {
  217. $conversation = Conversation::findOrFail(decrypt($id));
  218. if ($conversation->sender_id == Auth::user()->id) {
  219. $conversation->sender_viewed = 1;
  220. }
  221. elseif($conversation->receiver_id == Auth::user()->id) {
  222. $conversation->receiver_viewed = 1;
  223. }
  224. $conversation->save();
  225. return view('backend.support.conversations.show', compact('conversation'));
  226. }
  227. /**
  228. * Show the form for editing the specified resource.
  229. *
  230. * @param int $id
  231. * @return \Illuminate\Http\Response
  232. */
  233. public function edit($id)
  234. {
  235. //
  236. }
  237. /**
  238. * Update the specified resource in storage.
  239. *
  240. * @param \Illuminate\Http\Request $request
  241. * @param int $id
  242. * @return \Illuminate\Http\Response
  243. */
  244. public function update(Request $request, $id)
  245. {
  246. //
  247. }
  248. /**
  249. * Remove the specified resource from storage.
  250. *
  251. * @param int $id
  252. * @return \Illuminate\Http\Response
  253. */
  254. public function destroy($id)
  255. {
  256. $conversation = Conversation::findOrFail(decrypt($id));
  257. foreach ($conversation->messages as $key => $message) {
  258. $message->delete();
  259. }
  260. if(Conversation::destroy(decrypt($id))){
  261. flash(translate('Conversation has been deleted successfully'))->success();
  262. return back();
  263. }
  264. }
  265. /**
  266. * Update the specified resource in storage.
  267. *
  268. * @param \Illuminate\Http\Request $request
  269. * @param int $id
  270. * @return \Illuminate\Http\Response
  271. */
  272. public function message_count(Request $request){
  273. $conversations = \App\Models\Conversation::where('sender_id', Auth::user()->id)
  274. ->orWhere('receiver_id', Auth::user()->id)
  275. ->with('messages')
  276. ->get();
  277. $count = 0;
  278. foreach ($conversations as $k=>$v){
  279. if($v->sender_id === Auth::user()->id && $v->sender_viewed==0) $count++;
  280. if($v->receiver_id === Auth::user()->id && $v->receiver_viewed==0) $count++;
  281. foreach ($v->messages as &$vv){
  282. if(strtotime($vv->created_at) === strtotime($vv->updated_at) && $vv->user_id != Auth::user()->id) {
  283. $count++;
  284. }
  285. }
  286. }
  287. return response()->json([
  288. 'result' => $count,
  289. ]);
  290. }
  291. }