/home/dvjjulio/softtrash/app/Http/Controllers/ActorController.php
<?php

namespace Trash\Http\Controllers;

use Illuminate\Http\Request;

use Trash\Http\Requests;
use Trash\Http\Controllers\Controller;
use Trash\Typepost;
use Trash\Actor;

class ActorController extends Controller
{
    /**
     * Display a listing of the resource.
     * 
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = \Auth::user();
        /*if( isset( $user ) 
           ->hasRole('admin') ) */
           if( !isset( $user )){
            $data = Actor::all();
            //return response()->json( array( 'status' => true, 'data' => $data ) );
            return view( 'listActors' )->with( compact( 'data' ) ) ;
        }
        //
        return  redirect()->to('/');
    }

    public function form()
    {
        return view( 'addFormActor' );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        try {
            $data_actor = \Input::all();

            $validator = \Validator::make( $data_actor, 
                array(      'name'              => 'required',
                            'type_entity'       => 'required'  ), //ACTIVAR CON SESIONES ,'updated_user_id'   => 'required' 
                array(      'name.required'         => 'El nombre es obligatorio :S',
                            'type_entity.required'  => 'El tipo de actor es obligatorio :S' )
            );

            if( $validator->fails() ){
                return response()->json( $validator->messages() );
            }
            $actor              = new Actor();
            $actor->name        = $data_actor['name'];
            $actor->type_entity = $data_actor['type_entity'];
        
            if( $actor->save() ){
                return response()->json( 'Done' );
            }
            return response()->json( 'Fail' );
        }catch( Exception $e ){
            return response()->json( 'Fail.' );
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try{
            $actor = Actor::find( $id );
            if( !isset( $actor ) ){
                return response()->json( array( 'status' => false, 'message' => 'Error, Actor no encontrado' ) );
            }

            return response()->json( array( 'status' => true, 'data' => $actor ) );
        }catch( Exception $e ){
            return response()->json( array( 'status' => false, 'message' => 'Error, Actor no encontrado.' ) );
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $actor = Actor::find( $id );
        if( isset( $actor ) ){
            return response()->json( array( 'status' => true, 'data' => $actor ) );
        }
        return response()->json( array( 'status' => false, 'message' => 'Error, Actor no encontrado :S' ) );
    }

    /**
     * Update the specified resource in storage.
     * @return \Illuminate\Http\Response
     * @internal param Request $request
     * @internal param int $id
     */
    public function update()
    {
        return response()->json( \Input::all() );
        $actor = Actor::find( \Input::get('id') );
        if( !isset( $actor ) )
        {
            return response()->json( array( 'status' => false, 'message' => 'Error, no se encuentra el dato' ) );
        }

        $actor->name        = \Input::get('name');
        $actor->type_entity = \Input::get('type_entity');
        //METER LOS DEMAS DATOS....
        if( $actor->save() ){
            return response()->json( array( 'status' => true, 'data' => $actor ) );
        }else {
            return response()->json( array( 'status' => false, 'message' => 'Error, hubo un error al guardar el dato' ) );
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @params  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $actor = Actor::find( \Input::get('id') );
        if( !isset( $actor ) )
        {
            return response()->json( array( 'status' => false, 'message' => 'Error, no se pudo eliminar el dato :)' ) );
        }

        if( $actor->delete() )
            return response()->json( array( 'status' => true, 'message' => 'Dato eliminado :)' ) );
        return response()->json( array( 'status' => false, 'message' => 'Error, no se pudo eliminar el dato :)' ) );
    }

    /**
     * Method to link two actors
     * @params Request $input
     * @return mixed   
     */
    public function addRelationship(){
        $input  = \Input::all();
        $act    = Actor::find( $input[ 'actor_id' ] );
        if( isset( $act ) && isset( $input[ 'related_actor_id' ] ) && isset( $input[ 'typerelationship_id' ] )  ) {
            return response()->json( $act->createRelationship( $input[ 'related_actor_id' ] , $input[ 'typerelationship_id' ] , 1 ) );
        }
        return response()->json( array( 'status' => false, 'message' => 'Error, datos proporcionados no validos' ), 418 );
    }

    /**
     * Method to get all the actors related with this one
     * @param $id
     * @return mixed
     */
    public function getRelationships( $id ) {
        $actor = Actor::find($id);
        return response()->json(
            array('status' => true, 'data' => $actor->getRelationships())
        );
    }

    /**
     * Method to get a list of actors
     * @return \Illuminate\Http\JsonResponse
     */
    public function listActor() {
         $actors = Actor::all();
        return response()->json( array( 'status' => true, 'data' => $actors ) );
    }
    
    public function getActorByName( $name ){
        $actor = Actor::where( 'name', $name )->first();
        if( isset( $actor ) )
            return response()->json( array( 'status' => true , 'data' => $actor->getRelationships() ) );
        return response()->json( array( 'status' => false, 'message' => 'Error, Actor no encontrado' ) );
    }
}