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/agile-selling-wpb/app/Http/Controllers/DataManagerController.php
<?php

namespace App\Http\Controllers;

use DB;
use Excel;
use DataTables;
use App\Attribute;
use App\AttributeType;
use Box\Spout\Common\Type;
use Illuminate\Http\Request;
use Box\Spout\Reader\ReaderFactory;
use App\Http\Controllers\Exports\ReportAttributes;

class DataManagerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('attributes.list');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $attribute_types = AttributeType::where('active', 1)->get();
        return view('attributes.create', compact('attribute_types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if (!Attribute::where('name', $request->input('name'))->first()) {
            if (Attribute::create($request->all())) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => null));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
            }
        } else {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
        }
    }

    /**
     * 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)
    {
        $attribute = Attribute::findOrFail($id);
        $attribute_types = AttributeType::where('active', 1)->get();
        if ($attribute) {
            return view('attributes.edit', compact('attribute','attribute_types'));
        } else {
            return redirect()->back();
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if (!Attribute::where([['id', '!=', $id], ['name', $request->input('name')]])->first()) {
            if (Attribute::where('id', $id)->update(array_slice($request->all(), 1))) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => null));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
            }
        } else {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

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

        $attribute = Attribute::find($id);
        $attribute->active = $state;
        $attribute->update();

        if ($attribute) {
          return array('r' => true, 'd' => null, 'm'=> __('messages.updated_successfully'));
        }else{
          return array('r' => false,'d' => null, 'm'=> __('messages.error_updating'));
        }
    }

    public function tableFilter(){
        $obj = $obj = DB::table('attributes')
        ->select('attributes.id','attributes.name','attributes.display_name','attributes.is_more_one','attributes.required','attributes.active');
        return DataTables::of($obj)
        ->addColumn('actions', function ($obj) {
            return '
               <i class="fa fa-pencil iconMini " onClick="clickEdit('.$obj->id.')" data-id="'.$obj->id.'" data-toggle="tooltip" data-placement="bottom"  style="cursor:pointer;"></i>
            ';
        })
        ->editColumn('active', function ($obj) {
             if($obj->active == 0){
                return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk('.$obj->id.')" data-id="'.$obj->id.'" id="Checkactive'.$obj->id.'" name="Checkactivo" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
            }else{
                return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label>   ' . __('messages.no') . ' <input type="checkbox" onChange="chk('.$obj->id.')" data-id="'.$obj->id.'" id="Checkactive'.$obj->id.'" name="Checkactivo" checked="" />
                        <span></span> ' . __('messages.yes') . ' </label> </div>  </label> </div>';
            }
           })
        ->editColumn('is_more_one', function ($obj) {
             if($obj->is_more_one == 1){
                return '<div> '. __('messages.yes') . '</div>';
            }else{
                return '<div>' . __('messages.no') . '</div>';
            }
           })
        ->editColumn('required', function ($obj) {
             if($obj->required == 1){
                return '<div> '. __('messages.yes') . '</div>';
            }else{
                return '<div>' . __('messages.no') . '</div>';
            }
           })
        ->rawColumns([ 'active','actions','is_more_one','required'])
        ->make(true);
    }

    public function export()
    {
        return Excel::download(new ReportAttributes, trans('messages.attributes.title_10').'.xlsx');
    }

    public function import(Request $request)
    {
        try {
            $this->edit = [
                'creados' => 0,
                'editados' => 0,
            ];

            DB::beginTransaction();
            $reader = ReaderFactory::create(Type::XLSX); // for XLSX files
            $reader->open($request->importAttributes);
            $cnt = 0;
            foreach ($reader->getSheetIterator() as $sheet) {
                foreach ($sheet->getRowIterator() as $row) {
                    if ($cnt > 0) {
                        if($attribute = Attribute::where('name', trim($row[1]))->first()){
                            $attribute->name = $row[1];
                            $attribute->display_name = $row[2];
                            $attribute->is_more_one = $row[3] == 'Si' ? true : false;
                            $attribute->required = $row[4] == 'Si' ? true : false;
                            $attribute->active = $row[5] == 'Si' ? true : false;
                            $attribute->attribute_type_id = $row[6];
                            $attribute->update();
                            $this->edit['editados'] += 1;
                        }else{
                            Attribute::create([
                                "name" => $row[1],
                                "display_name" => $row[2],
                                "is_more_one" => $row[3] == 'Si' ? true : false,
                                "required" => $row[4] == 'Si' ? true : false,
                                "active" => $row[5] == 'Si' ? true : false,
                                "attribute_type_id" => $row[6],
                            ]);
                            $this->edit['creados'] += 1;
                        }
                    }
                    $cnt++;
                }
            }
            DB::commit();
            $reader->close();
            return array('r' => true, 'd' => null, 'm' => trans('messages.screen_products_tag45') . $this->edit['creados'] . ' creados, ' . $this->edit['editados'] . ' editados.');
        } catch (\Exception $e) {
            return array('r' => false, 'd' => null, 'm' => trans('messages.screen_products_tag46') . $e->getMessage());
        }
    }
}