EVOLUTION-MANAGER
Edit File: chatbotController.php
<?php namespace App\Http\Controllers; use mPDF; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use App\chatbot_preguntas; use App\chatbot_respuestas; use App\tipoarticuloModel; use View; use Auth; use Session; use Redirect; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Input; use File; use App\archivoModel; use App\pregunta_respuestaModel; class chatbotController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $chatbots = chatbot_preguntas::join('chatbot_respuestas','chatbot_respuestas.id','=','chatbot_preguntas.respuesta_id')->select('chatbot_preguntas.pregunta','chatbot_respuestas.respuesta','chatbot_preguntas.id')->get(); //dd($tipos); return View::make('admin.chatbot.index', compact('chatbots')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return View::make('admin.chatbot.crear'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'pregunta' => 'required', 'respuesta' => 'required', ] ); if(count($request->pregunta)&&count($request->respuesta)){ $respuestas_botones=count($request->respuesta); foreach ($request->pregunta as $pre => $pregunta_id) { pregunta_respuestaModel::where('chatbot_preguntas_id',$pregunta_id)->delete(); foreach ($request->respuesta as $res => $respuesta_id) { $respuestasx=chatbot_respuestas::find($respuesta_id); if($respuestas_botones>1){ $respuestasx->tipo_respuesta='button'; }else{ $respuestasx=chatbot_respuestas::find($respuesta_id); $respuestasx->tipo_respuesta='text'; } $respuestasx->save(); $pregunta_respuesta=new pregunta_respuestaModel(); $pregunta_respuesta->chatbot_respuestas_id=$respuesta_id; $pregunta_respuesta->chatbot_preguntas_id=$pregunta_id; $pregunta_respuesta->save(); } } } Session::flash('message','Se han ingresado nuevas preguntas y respuestas'); return Redirect::to('administracion/preguntas'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // $chatbot = chatbot_preguntas::find($id); $chatbot = chatbot_preguntas::join('chatbot_respuestas','chatbot_respuestas.id','=','chatbot_preguntas.respuesta_id')->where('chatbot_preguntas.id',$id)->select('chatbot_preguntas.pregunta','chatbot_respuestas.respuesta','chatbot_preguntas.id as id','chatbot_respuestas.id as respuesta_id')->first(); // dd($chatbot); return View::make('admin.chatbot.editar', compact('chatbot','id')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { try { $chatbot = chatbot_preguntas::find($id); $chatbot->pregunta=$request->pregunta; $chatbot->respuesta_id=$request->respuesta; $chatbot->save(); Session::flash('message','pregunta y respuesta actualizado correctamente.'); } catch (\Illuminate\Database\QueryException $e) { Session::flash('message-error','No se pudo añadir el pregunta y respuesta'.$e); } return Redirect::to('administracion/chatbot'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { try { chatbot_preguntas::find($id)->delete(); Session::flash('message','Se ha eliminado correctamente la pregunta y respuesta'); } catch (\Illuminate\Database\QueryException $e) { Session::flash('message-error','Existen pregunta y respuesta '); } return Redirect::to('administracion/chatbot'); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ function consulta_respuestas(Request $request){ $dato=$request->dato; $respuesta=null; if($request->tipo=='respuesta'){ $respuesta = chatbot_respuestas::where('respuesta','like','%'.$dato.'%') ->select('respuesta as value','id') ->get(); } if($request->tipo=='pregunta'){ $respuesta = chatbot_preguntas::where('pregunta','like','%'.$dato.'%') ->select('pregunta as value','id') ->get(); } // dd($respuesta); return $respuesta; } function guardar_respuestas(Request $request){ if($request->tipo=='respuesta'){ $respuesta=chatbot_respuestas::where('respuesta',$request->respuesta)->first(); if(is_null($respuesta)){ $respuesta=new chatbot_respuestas(); $respuesta->respuesta=$request->respuesta; $respuesta->save(); } return $respuesta->id; } if($request->tipo=='pregunta'){ $respuesta=chatbot_preguntas::where('pregunta',$request->respuesta)->first(); if(is_null($respuesta)){ $respuesta=new chatbot_preguntas(); $respuesta->pregunta=$request->respuesta; $respuesta->save(); } return $respuesta->id; } } }