HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/FacebookController.php
<?php

namespace App\Http\Controllers;

use Datatables;
use App\OfficialFacebook;
use App\SocialNetworkAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FacebookController extends Controller
{
  public function index()
  {
    return view('official_facebook.list');
  }

  public function indexAdd()
  {
    $socialNetworkAccounts = SocialNetworkAccount::select('id', 'name')->where('active', true)->get();
    return view('official_facebook.create', compact('socialNetworkAccounts'));
  }

  public function indexEdit($id)
  {
    $object = OfficialFacebook::find($id);
    $socialNetworkAccounts = SocialNetworkAccount::select('id', 'name')->where('active', true)->get();
    return view('official_facebook.edit', compact('object', 'socialNetworkAccounts'));
  }

  public function tableFilter()
  {
    DB::statement("SET sql_mode = ''");
    $obj = OfficialFacebook::select(
      'official_facebooks.id',
      'title',
      'link',
      'social_network_accounts.name AS social_network_accounts',
      'official_facebooks.active',
      'official_facebooks.created_at'
    )
      ->leftJoin('social_network_accounts', 'social_network_accounts.id', '=', 'official_facebooks.social_network_account_id')
      ->groupBy('official_facebooks.id');
    DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");

    return Datatables::of($obj)
      ->addColumn('actions', function ($obj) {
        return '
               <i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" title="Editar"></i>
               <i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" title="Eliminar"></i>
              ';
      })
      ->editColumn('created_at', function ($obj) {
        return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
      })
      ->editColumn('active', function ($obj) {
        if ($obj->active == 0) {
          return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" class="chk" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>Si </label></div> </label> </div>';
        } else {
          return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label>   No <input type="checkbox" class="chk" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" /><span></span> Si </label> </div>  </label> </div>';
        }
      })
      ->rawColumns(['active', 'actions'])
      ->make(true);
  }

  public function create(Request $request)
  {
    try {
      if (OfficialFacebook::where('link', $request->input('link'))->first()) {
        return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('link')]), "data" => null));
      }
      if ($model = OfficialFacebook::create($request->all())) {
        return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
      } else {
        return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
      }
    } catch (\Exception $e) {
      return response(
        array(
          "r" => false,
          "type" => "error",
          "title" => "Oops...",
          "m" => __($e->getMessage()),
          "data" => null
        )
      );
    }
  }

  public function update(Request $request, $id)
  {
    try {
      $request['id'] = $id;
      if (OfficialFacebook::where([['id', '!=', $id], ['link', $request->input('link')]])->first()) {
        return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('link')]), "data" => null));
      }
      if (OfficialFacebook::where('id', $id)->update($request->all())) {
        return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
      } else {
        return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
      }
    } catch (\Exception $e) {
      return response(
        array(
          "r" => false,
          "type" => "error",
          "title" => "Oops...",
          "m" => __($e->getMessage()),
          "data" => null
        )
      );
    }
  }

  public function delete($id)
  {
    try {
      if (OfficialFacebook::where('id', $id)->delete()) {
        return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.deleted_successfully'), "data" => null));
      } else {
        return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_removing'), "data" => null));
      }
    } catch (\Illuminate\Database\QueryException $e) {
      return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.delete_relation_data'), "data" => null));
    }
  }

  public function activate(Request $request)
  {
    try {
      $id = $request['id'];
      $state = $request['state'];

      $facebook = OfficialFacebook::find($id);
      $facebook->active = $state;
      $facebook->update();

      return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
    } catch (\Throwable $th) {
      return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
    }
  }
}