2024-06-10 12:21:58 +00:00
< ? php
namespace App\Http\Controllers ;
use App\Http\Controllers\Controller ;
use App\Models\Menuitems ;
use App\Service\CommonModelService ;
use Exception ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\DB ;
2024-06-18 05:46:51 +00:00
use Illuminate\Support\Str ;
2024-06-10 12:21:58 +00:00
use Illuminate\Support\Facades\Validator ;
use Log ;
class MenuitemsController extends Controller
{
protected $modelService ;
protected static $menuTypes ;
public function __construct ( Menuitems $model )
{
$this -> modelService = new CommonModelService ( $model );
self :: initializeController ();
}
public function initializeController ()
{
$menuTypes = [
2024-06-18 05:46:51 +00:00
[ 'display' => " Economies " , 'value' => " tbl_economies " ],
[ 'display' => " News " , 'value' => " tbl_news " ],
[ 'display' => " News Categories " , 'value' => " tbl_newscategories " ],
[ 'display' => " News Type " , 'value' => " tbl_news_type " ],
[ 'display' => " Provinces " , 'value' => " tbl_provinces " ],
[ 'display' => " Articles " , 'value' => " tbl_articles " ],
[ 'display' => " Teams " , 'value' => " tbl_teams " ],
2024-06-10 12:21:58 +00:00
[ 'display' => " Custom " , 'value' => " " ],
];
foreach ( $menuTypes as & $menuType ) {
switch ( $menuType [ 'value' ]) {
2024-06-18 05:46:51 +00:00
case 'tbl_economies' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select economy_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
2024-06-10 12:21:58 +00:00
break ;
case 'tbl_news' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select news_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
break ;
2024-06-18 05:46:51 +00:00
case 'tbl_newscategories' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select category_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
break ;
case 'tbl_news_type' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select news_type_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
break ;
case 'tbl_provinces' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select province_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
break ;
case 'tbl_articles' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select article_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
break ;
case 'tbl_teams' :
$menuType [ 'values' ] = json_encode ( DB :: select ( " select team_id as value,title as display from " . $menuType [ 'value' ] . " where status=1 Order by title " ));
break ;
2024-06-10 12:21:58 +00:00
default :
$menuType [ 'values' ] = " " ;
}
}
self :: $menuTypes = $menuTypes ;
}
public function index ( Request $request )
{
createActivityLog ( MenuitemsController :: class , 'index' , ' Menuitems index' );
$data = Menuitems :: where ( 'status' , '<>' , - 1 );
$menulocation = $request -> query ( " menulocation " );
if ( null != $menulocation ) {
$data = $data -> where ( 'menulocations_id' , $menulocation );
}
$data = $data -> orderBy ( 'display_order' ) -> get ();
return view ( " crud.generated.menuitems.index " , compact ( 'data' , 'menulocation' ));
}
public function create ( Request $request )
{
createActivityLog ( MenuitemsController :: class , 'create' , ' Menuitems create' );
$TableData = Menuitems :: where ( 'status' , '<>' , - 1 );
$menulocation = $request -> query ( " menulocation " );
if ( null != $menulocation ) {
$TableData = $TableData -> where ( 'menulocations_id' , $menulocation );
}
$TableData = $TableData -> orderBy ( 'display_order' ) -> get ();
$menuTypes = self :: $menuTypes ;
return view ( " crud.generated.menuitems.create " , compact ( 'TableData' , 'menuTypes' , 'menulocation' ));
}
public function store ( Request $request )
{
createActivityLog ( MenuitemsController :: class , 'store' , ' Menuitems store' );
$validator = Validator :: make ( $request -> all (), [
//ADD REQUIRED FIELDS FOR VALIDATION
]);
if ( $validator -> fails ()) {
return response () -> json ([
'error' => $validator -> errors (),
], 500 );
}
2024-06-18 05:46:51 +00:00
$request -> mergeIfMissing ([
'alias' => Str :: slug ( $request -> title ),
]);
2024-06-10 12:21:58 +00:00
$request -> request -> add ([ 'display_order' => getDisplayOrder ( 'tbl_menuitems' )]);
$requestData = $request -> all ();
array_walk_recursive ( $requestData , function ( & $value ) {
$value = str_replace ( env ( 'APP_URL' ) . '/' , '' , $value );
});
array_walk_recursive ( $requestData , function ( & $value ) {
$value = str_replace ( env ( 'APP_URL' ), '' , $value );
});
DB :: beginTransaction ();
try {
$operationNumber = getOperationNumber ();
$this -> modelService -> create ( $operationNumber , $operationNumber , null , $requestData );
} catch ( \Exception $e ) {
DB :: rollBack ();
Log :: info ( $e -> getMessage ());
createErrorLog ( MenuitemsController :: class , 'store' , $e -> getMessage ());
return response () -> json ([ 'status' => false , 'message' => $e -> getMessage ()], 500 );
}
DB :: commit ();
if ( $request -> ajax ()) {
return response () -> json ([ 'status' => true , 'message' => 'The Menuitems Created Successfully.' ], 200 );
}
return redirect () -> back () -> with ( 'success' , 'The Menuitems Added successfully.' );
// return redirect()->route('menuitems.index')->with('success', 'The Menuitems created Successfully.');
}
public function sort ( Request $request )
{
$idOrder = $request -> input ( 'id_order' );
foreach ( $idOrder as $index => $id ) {
$companyArticle = Menuitems :: find ( $id );
$companyArticle -> display_order = $index + 1 ;
$companyArticle -> save ();
}
return response () -> json ([ 'status' => true , 'content' => 'The articles sorted successfully.' ], 200 );
}
public function updatealias ( Request $request )
{
$articleId = $request -> input ( 'articleId' );
$newAlias = $request -> input ( 'newAlias' );
$companyArticle = Menuitems :: find ( $articleId );
if ( ! $companyArticle ) {
return response () -> json ([ 'status' => false , 'content' => 'Company article not found.' ], 404 );
}
$companyArticle -> alias = $newAlias ;
$companyArticle -> save ();
return response () -> json ([ 'status' => true , 'content' => 'Alias updated successfully.' ], 200 );
}
public function show ( Request $request , $id )
{
createActivityLog ( MenuitemsController :: class , 'show' , ' Menuitems show' );
$data = Menuitems :: findOrFail ( $id );
return view ( " crud.generated.menuitems.show " , compact ( 'data' ));
}
public function edit ( Request $request , $id )
{
createActivityLog ( MenuitemsController :: class , 'edit' , ' Menuitems edit' );
$TableData = Menuitems :: where ( 'status' , '<>' , - 1 );
$menulocation = $request -> query ( " menulocation " );
if ( null != $menulocation ) {
$TableData = $TableData -> where ( 'menulocations_id' , $menulocation );
}
$TableData = $TableData -> orderBy ( 'display_order' ) -> get ();
$menuTypes = self :: $menuTypes ;
$data = Menuitems :: findOrFail ( $id );
return view ( " crud.generated.menuitems.edit " , compact ( 'data' , 'TableData' , 'menulocation' , 'menuTypes' ));
}
public function update ( Request $request , $id )
{
createActivityLog ( MenuitemsController :: class , 'update' , ' Menuitems update' );
$validator = Validator :: make ( $request -> all (), [
//ADD VALIDATION FOR REQIRED FIELDS
]);
if ( $validator -> fails ()) {
return response () -> json ([
'error' => $validator -> errors (),
], 500 );
}
2024-06-18 05:46:51 +00:00
$request -> mergeIfMissing ([
'alias' => Str :: slug ( $request -> title ),
]);
2024-06-10 12:21:58 +00:00
$requestData = $request -> all ();
array_walk_recursive ( $requestData , function ( & $value ) {
$value = str_replace ( env ( 'APP_URL' ) . '/' , '' , $value );
});
array_walk_recursive ( $requestData , function ( & $value ) {
$value = str_replace ( env ( 'APP_URL' ), '' , $value );
});
// dd($requestData);
DB :: beginTransaction ();
try {
$OperationNumber = getOperationNumber ();
$this -> modelService -> update ( $OperationNumber , $OperationNumber , null , $requestData , $id );
} catch ( Exception $e ) {
DB :: rollBack ();
Log :: info ( $e -> getMessage ());
createErrorLog ( MenuitemsController :: class , 'update' , $e -> getMessage ());
return response () -> json ([ 'status' => false , 'message' => $e -> getMessage ()], 500 );
}
DB :: commit ();
if ( $request -> ajax ()) {
return response () -> json ([ 'status' => true , 'message' => 'The Menuitems updated Successfully.' ], 200 );
}
return redirect () -> route ( 'menuitems.index' ) -> with ( 'success' , 'The Menuitems updated Successfully.' );
// return redirect()->back()->with('success', 'The Menuitems updated successfully.');
}
public function destroy ( Request $request , $id )
{
createActivityLog ( MenuitemsController :: class , 'destroy' , ' Menuitems destroy' );
DB :: beginTransaction ();
try {
$OperationNumber = getOperationNumber ();
$this -> modelService -> destroy ( $OperationNumber , $OperationNumber , $id );
} catch ( Exception $e ) {
DB :: rollBack ();
Log :: info ( $e -> getMessage ());
createErrorLog ( MenuitemsController :: class , 'destroy' , $e -> getMessage ());
return response () -> json ([ 'status' => false , 'message' => $e -> getMessage ()], 500 );
}
DB :: commit ();
return response () -> json ([ 'status' => true , 'message' => 'The Menuitems Deleted Successfully.' ], 200 );
}
}