HEX
Server: LiteSpeed
System: Linux sv4.hami.host 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64
User: armgroup (1008)
PHP: 8.2.32
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, mail, socket_create, socket_create_listen, socket_create_pair, link, dl, openlog, syslog, stream_socket_server, curl_multi_init
Upload Files
File: //home/armgroup/libs/app/Imports/CommentsProductImport.php
<?php

namespace App\Imports;

use App\Models\Product;
use App\Models\Score;
use App\Models\Comment;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Carbon\Carbon;


HeadingRowFormatter::default('none');

class CommentsProductImport implements
    SkipsEmptyRows,
    ToCollection,
    WithHeadingRow,
    WithValidation,
    WithChunkReading
{
    public function collection(Collection $rows)
    {
        
    // dd($rows);

        foreach ($rows as $row) {
            try {
                
                DB::beginTransaction();
                $data = $this->mapRow($row);
  

                if (empty($data['excel_name'])) {
                    continue;
                }

                $product = Product::where('name', 'like', "%{$data['excel_name']}%")->first();
                
               
              
                if (!$product) {
                    continue;
                }
              
              
                if (empty($data['text_comment'])) {
                    continue;
                }
                
             
                
                
    
                $createdAt = Date::excelToDateTimeObject($data['created_at']);

                $comments = [
                    [
                        'name' => $data['writer'],  
                        'email' => null,
                        'comment' =>  $data['text_comment'],
                         'reply' =>  $data['reply'],
                         'score' =>  $data['score'],
                        'created_at' => Carbon::instance($createdAt),
                    ]
                ];
                
                
          
                

              foreach ($comments as $item) {
                        
                     $comment = Comment::create([
                            'user_id' => null,
                            'parent_id' => null,
                            'commentable_id' => $product->id,
                            'commentable_type' =>  Product::class,
                            'name' => $item['name'],
                            'email' => $item['email'],
                            'comment' => $item['comment'],
                            'checked' => true,
                            'status' => 1,
                            'created_at' => $item['created_at'],
    
                        ]);
                    
       
                    
                    Score::create([
                        'user_id' => null,
                        'scorable_id' => $product->id,
                        'scorable_type' => Product::class,
                        'score' => $item['score'],
                    ]);
                
                    if (!empty($item['reply'])) {
                        $replyDate = $comment->created_at->copy()->addMinutes(random_int(30, 300));

                        Comment::create([
                            'user_id' => 1, 
                            'parent_id' => $comment->id,
                            'commentable_id' => $comment->commentable_id,
                            'commentable_type' => $comment->commentable_type,
                            'name' => 'پشتیبانی',
                            'email' => 'support@nilper.com',
                            'comment' => $item['reply'],
                            'checked' => true,
                            'status' => 1,
                             'created_at' =>  $replyDate,
                             
                        ]);
                    }
                }
                
                    DB::commit();

           
            } catch (\Throwable $e) {
               
                dd($e);
                Log::error('attribute Import Error', [
                    'message' => $e->getMessage(),
                    'row' => $row->toArray(),
                ]);
            }
        }
        

    }

       private function mapRow($row): array
    {
        return [
            'excel_name' => $this->clean($row['نام'] ?? ''),
            'text_comment' => $this->clean($row['متن نظر'] ?? ''),
            'writer' => $this->clean($row['نویسنده'] ?? ''),
            'reply' => $this->clean($row['پاسخ ادمین'] ?? ''),
            'score' => $this->clean($row['امتیاز'] ?? ''),
             'created_at' => $this->clean($row['تاریخ'] ?? ''),
        ];
    }
    
        private function clean($value)
    {
        return trim(str_replace("\u{00A0}", ' ', $value));
    }   

    public function rules(): array
    {
        return [
            'نام' => ['required'],
            'متن نظر'        => ['nullable'],
            'نویسنده'        => ['nullable'],
            'پاسخ ادمین'        => ['nullable'],
            'امتیاز'        => ['nullable'],
            'تاریخ'        => ['nullable'],
        ];
    }

    public function chunkSize(): int
    {
        return 2000;
    }
}