2026-01-24 14:03:11 +00:00
<? php
namespace App\Http\Controllers ;
use App\DB\Te\TableEngine ;
use App\Models\Cnt ;
2026-07-02 16:12:39 +03:30
use App\Models\Customer ;
2026-07-06 19:21:20 +03:30
use App\Models\User ;
2026-01-24 14:03:11 +00:00
use Illuminate\Http\Request ;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException ;
class SuperController extends Controller
{
public function __construct ( $model , $with , $select = [ '*' ], $name_model = null , $query = null , $search = [])
{
$this -> model = $model ;
$this -> with = $with ;
$this -> select = $select ;
$this -> name_model = $name_model ;
$this -> query = $query ;
$this -> search = $search ;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\jsonResponse
*/
public function store ( Request $request )
{
$data = $request -> all ();
if ( isset ( $data [ 'fname' ]) && isset ( $data [ 'lname' ])){
$data [ 'name' ] = $data [ 'fname' ] . ' ' . $data [ 'lname' ];
}
if ( isset ( $data [ 'user_type' ]) && ! is_int ( $data [ 'user_type' ])){
$cnt = Cnt :: set ( 'user_type' , $data [ 'user_type' ]);
$data [ 'user_type' ] = $cnt -> id ;
}
if ( ! is_null ( $this -> name_model )){
if ( $this -> name_model == 'user' | $this -> name_model == 'customer' ){
if ( ! isset ( $data [ 'password' ])){
$data [ 'password' ] = trim ( $data [ 'mobile' ]);
}
if ( $this -> name_model == 'customer' ){
2026-06-13 15:41:34 +03:30
$data [ 'active' ] = true ;
2026-01-24 14:03:11 +00:00
2026-07-02 16:12:39 +03:30
if ( ! empty ( $data [ 'mobile' ])) {
Customer :: assertMobileUnique ( $data [ 'mobile' ]);
}
2026-01-24 14:03:11 +00:00
$characters = '0123456789abcdefghijklmnopqrstuvwxyz' ;
$charactersLength = strlen ( $characters );
do {
$uniqueId = '' ;
for ( $i = 0 ; $i < 4 ; $i ++ ) { // ایجاد یک کد چهار رقمی
$index = random_int ( 0 , $charactersLength - 1 );
$uniqueId .= $characters [ $index ];
}
// بررسی اینکه آیا کد ایجاد شده یونیک است
$existingCustomer = $this -> model :: where ( 'referral_id' , $uniqueId ) -> first ();
} while ( $existingCustomer ); // تکرار تا زمانی که کد یونیک ایجاد شود
$data [ 'referral_id' ] = $uniqueId ; // ذخیره کد یونیک در فیلد referral_id
$message = " { $data [ 'name' ] } عزیز، اکانت شما در پلتفرم { $this -> Config ( 'name_fa' ) } ایجاد شد. \n نام کاربری: { $data [ 'mobile' ] } \n ورود به نرم افزار: { $this -> Config ( 'app_url' ) } " ;
$this -> Send ( $data [ 'mobile' ], $message );
}
}
}
$user = $this -> model :: create ( $data );
return $this -> resultCreate ( $user -> id );
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\jsonResponse
*/
public function show ( $id )
{
$user = $this -> model :: with ( $this -> with )
-> select ( $this -> select )
-> findOrFail ( $id );
return response () -> json ( $user );
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\jsonResponse
*/
public function update ( Request $request , $id )
{
$data = $request -> all ();
2026-07-06 19:21:20 +03:30
if ( isset ( $data [ 'mobile' ]) && ! is_null ( $data [ 'mobile' ])) {
$data [ 'mobile' ] = Customer :: normalizeMobile ( $data [ 'mobile' ]);
if ( $this -> name_model === 'customer' ) {
Customer :: assertMobileUnique ( $data [ 'mobile' ], ( int ) $id );
} elseif ( $this -> name_model === 'user' ) {
User :: assertMobileUnique ( $data [ 'mobile' ], ( int ) $id );
}
2026-01-24 14:03:11 +00:00
}
if ( isset ( $data [ 'fname' ]) && isset ( $data [ 'lname' ])){
$data [ 'name' ] = $data [ 'fname' ] . ' ' . $data [ 'lname' ];
}
if ( isset ( $data [ 'user_type' ]) && ! is_int ( $data [ 'user_type' ])){
$cnt = Cnt :: set ( 'user_type' , $data [ 'user_type' ]);
$data [ 'user_type' ] = $cnt -> id ;
}
$this -> model :: findOrFail ( $id )
-> update ( $data );
return response () -> json ( true );
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\jsonResponse
*/
public function destroy ( $id )
{
$this -> model :: findOrFail ( $id ) -> delete ();
return response () -> json ( true );
}
/**
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function active ( $id ){
$query = $this -> model :: findOrFail ( $id );
$query -> update ([
'active' =>! $query -> active
]);
return response () -> json ( true );
}
public function table ( Request $request )
{
$data = $request -> all ();
if ( isset ( $data [ "select" ]) && is_null ( $data [ "select" ])){
$data [ "select" ] = [];
}
if ( is_null ( $this -> query )){
$query_table = $this -> model :: with ( $this -> with );
//*******کداضافه شده
// ->where('active', '<=', now()) // فیلتر محصولات فعال
// ->get();
//*********
} else {
$query_table = $this -> query ;
// $query_table = $query_table->where('active', '<=', now());
}
TableEngine :: create ( $query_table , $data )
-> initSearch ( $this -> search )
-> select ( $this -> select )
-> export ( $query_table );
return $query_table ;
}
}