Viewing File: /home/ubuntu/shop-website-base/app/Http/Controllers/Admin/LookupController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Helpers\Helper, App\Helpers\EnvEditorHelper;

use DB, Hash, Setting, Auth, Validator, Exception, Enveditor;

use App\Jobs\SendEmailJob;

use App\Models\Settings;

use App\Models\Document, App\Models\StaticPage, App\Models\Faq, App\Models\Coupon, App\Models\VehicleDocument,App\Models\SupportContact;


class LookupController extends Controller
{
	/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request) {

        $this->middleware('auth:admin');

        $this->skip = $request->skip ?: 0;
       
        $this->take = $request->take ?: (Setting::get('admin_take_count') ?: TAKE_COUNT);

        $this->paginate_count = Setting::get('admin_take_count', 10);

    }

    /**
     * @method settings()
     *
     * @uses To view the settings page
     *
     * @created vithya R 
     *
     * @updated vithya R 
     *
     * @param - 
     *
     * @return view page
     */
    public function settings() {

        $env_values = EnvEditorHelper::getEnvValues();
        
        return view('admin.settings.settings')
                ->with('env_values',$env_values)
                ->with('page' , 'settings')
                ->with('sub_page' , 'settings-view');
   
    }

    /**
     * @method settings_save()
     * 
     * @uses to update settings details
     *
     * @created Vithya R
     *
     * @updated Vithya R
     *
     * @param (request) setting details
     *
     * @return success/error message
     */
    public function settings_save(Request $request) {

        try {
            
            DB::beginTransaction();
            
            $rules =  
                [
                    'site_logo' => 'mimes:jpeg,jpg,bmp,png',
                    'site_icon' => 'mimes:jpeg,jpg,bmp,png',
                ];

            $custom_errors = 
                [
                    'mimes' => tr('image_error')
                ];

            Helper::custom_validator($request->all(),$rules,$custom_errors);

            foreach( $request->toArray() as $key => $value) {

                if($key != '_token') {

                    $check_settings = Settings::where('key' ,'=', $key)->count();

                    if( $check_settings == 0 ) {

                        throw new Exception( $key.tr('settings_key_not_found'), 101);
                    }
                    
                    if( $request->hasFile($key) ) {
                                            
                        $file = Settings::where('key' ,'=', $key)->first();
                       
                        Helper::storage_delete_file($file->value, FILE_PATH_SITE);

                        $file_path = Helper::storage_upload_file($request->file($key) , FILE_PATH_SITE);    

                        $result = Settings::where('key' ,'=', $key)->update(['value' => $file_path]); 

                        if( $result == TRUE ) {
                     
                            DB::commit();
                   
                        } else {

                            throw new Exception(tr('settings_save_error'), 101);
                        } 
                   
                    } else {
                    
                        $result = Settings::where('key' ,'=', $key)->update(['value' => $value]);  
                    
                        if( $result == TRUE ) {
                         
                            DB::commit();
                       
                        } else {

                            throw new Exception(tr('settings_save_error'), 101);
                        } 

                    }  
 
                }
            }

            Helper::settings_generate_json();

            return back()->with('flash_success', tr('settings_update_success'));
            
        } catch (Exception $e) {

            DB::rollback();

            return back()->with('flash_error', $e->getMessage());
        
        }
    }

    /**
     * @method env_settings_save()
     *
     * @uses To update the email details for .env file
     *
     * @created vithya R
     *
     * @updated
     *
     * @param Form data
     *
     * @return view page
     */

    public function env_settings_save(Request $request) {
        try {

            $env_values = EnvEditorHelper::getEnvValues();

            $env_settings = ['MAIL_MAILER' , 'MAIL_HOST' , 'MAIL_PORT' , 'MAIL_USERNAME' , 'MAIL_PASSWORD' , 'MAIL_ENCRYPTION' , 'MAILGUN_DOMAIN' , 'MAILGUN_SECRET' , 'FCM_SERVER_KEY', 'FCM_SENDER_ID' , 'FCM_PROTOCOL'];

            if($env_values) {

                foreach ($env_values as $key => $data) {

                    if($request->$key) { 

                       Enveditor::set($key, $request->$key);

                    }
                }
            }
          
            return redirect()->route('clear-cache')->with('flash_success', tr('settings_update_success'));  

        } catch(Exception $e) {

            return back()->withInput()->with('flash_error' , $e->getMessage());

        }  

    }


    /**
     * @method static_pages_index()
     *
     * @uses Used to list the static pages
     *
     * @created Arun
     *
     * @updated   
     *
     * @param -
     *
     * @return List of pages   
     */

    public function static_pages_index(Request $request) {

        $base_query = StaticPage::orderBy('updated_at' , 'desc');

        if($request->status!=''){
        
        $base_query->where('status',$request->status);
   
        }
   
        if($request->search_key){
   
        $base_query = $base_query->where('title','LIKE','%'.$request->search_key.'%')
                                  ->orWhere('type','LIKE','%'.$request->search_key.'%');

        }

        $static_pages = $base_query->paginate(10);

        return view('admin.static_pages.index')
                    ->with('page','pages')
                    ->with('sub_page','static_pages-view')
                    ->with('static_pages', $static_pages);
    
    }

    /**
     * @method static_pages_create()
     *
     * @uses To create static_page details
     *
     * @created Arun
     *
     * @updated    
     *
     * @param
     *
     * @return view page   
     *
     */
    public function static_pages_create() {

        $static_keys = ['about' , 'contact', 'privacy' , 'terms' , 'help' , 'faq' , 'refund', 'cancellation','others'];

        foreach ($static_keys as $key => $static_key) {

            // Check the record exists

            $check_page = StaticPage::where('type', $static_key)->first();

            if($check_page) {
                unset($static_keys[$key]);
            }
        }

        $static_keys[] = 'others';

        $static_page = new StaticPage;


        return view('admin.static_pages.create')
                ->with('page','pages')
                ->with('sub_page','static_pages-create')
                ->with('static_keys', $static_keys)
                ->with('static_page',$static_page);
   
    }

    /**
     * @method static_pages_save()
     *
     * @uses Used to create/update the page details 
     *
     * @created Arun
     *
     * @updated 
     *
     * @param
     *
     * @return index page    
     *
     */
    public function static_pages_save(Request $request) {

        try {
            
            DB::beginTransaction();

            $validator = Validator::make($request->all(), [              
                'description' => 'required',
                'type' => !$request->static_page_id ? 'required' : "",
                'title' => $request->static_page_id ? 'required|max:255|unique:static_pages,title,'.$request->static_page_id : 'required|max:255|unique:static_pages,title',

            ]);
            
            if($validator->fails()) {

                $error = implode(',', $validator->messages()->all());

                throw new Exception($error, 101);
                
            }
            if($request->static_page_id != '') {

                $static_page = StaticPage::find($request->static_page_id);

                $message = tr('static_page_updated_success');                    

            } else {

                $check_page = "";

                // Check the staic page already exists

                if($request->type != 'others') {

                    $check_page = StaticPage::where('type',$request->type)->first();

                    if($check_page) {

                        return back()->with('flash_error',tr('static_page_already_alert'));
                    }

                }

                $message = tr('static_page_created_success');

                $static_page = new StaticPage;

                $static_page->status = APPROVED;

            }

            $static_page->title = $request->title ?: $static_page->title;

            $static_page->description = $request->description ?: $static_page->description;

            $static_page->type = $request->type ?? $static_page->type;

            $static_page->section_type = $request->section_type ?: $static_page->section_type;

            $unique_id = $request->type ?? $static_page->type;

            if(!in_array($request->type ?? $static_page->type, ['about', 'contact', 'privacy', 'terms', 'help', 'faq', 'refund', 'cancellation', 'business'])) {

                $unique_id = routefreestring($request->title ?? rand());

                $unique_id = in_array($unique_id, ['about', 'contact', 'privacy', 'terms', 'help', 'faq', 'refund', 'cancellation', 'business']) ? $unique_id.rand() : $unique_id;

            }

            $static_page->unique_id = $unique_id ?? rand();

            if($static_page->save()) {

                DB::commit();
                
                Helper::settings_generate_json();

                return redirect()->route('admin.static_pages.view', ['static_page_id' => $static_page->id] )->with('flash_success', $message);

            } 

            throw new Exception(tr('static_page_save_failed'), 101);
                      
        } catch(Exception $e) {

            DB::rollback();

            return back()->withInput()->with('flash_error', $e->getMessage());

        }
    
    }

    /**
     * @method static_pages_edit()
     *
     * @uses To display and update static_page details based on the static_page id
     *
     * @created Arun
     *
     * @updated 
     *
     * @param object $request - static_page Id
     * 
     * @return redirect view page 
     *
     */
    public function static_pages_edit(Request $request) {

        try {

            $static_page = StaticPage::find($request->static_page_id);

            if(!$static_page) {

                throw new Exception(tr('static_page_not_found'), 101);
            }

            $static_keys = ['about', 'contact', 'privacy', 'terms', 'help', 'faq', 'refund', 'cancellation'];

            foreach ($static_keys as $key => $static_key) {

                // Check the record exists

                $check_page = StaticPage::where('type', $static_key)->first();

                if($check_page) {
                    unset($static_keys[$key]);
                }
            }

            $section_types = static_page_footers(0, $is_list = YES);

            $static_keys[] = 'others';

            $static_keys[] = $static_page->type;

            return view('admin.static_pages.edit')
                    ->with('page','pages')
                    ->with('sub_page','static_pages-create')
                    ->with('static_keys' , array_unique($static_keys))
                    ->with('static_page' , $static_page)
                    ->with('section_types',$section_types);
            
        } catch(Exception $e) {

            return redirect()->route('admin.static_pages.index')->with('flash_error' , $e->getMessage());

        }
    
    }   

    /**
     * @method static_pages_view()
     *
     * @uses view the static_pages details based on static_pages id
     *
     * @created Arun 
     *
     * @updated 
     *
     * @param object $request - static_page Id
     * 
     * @return View page
     *
     */
    public function static_pages_view(Request $request) {

        $static_page = StaticPage::find($request->static_page_id);

        if(!$static_page) {
           
            return redirect()->route('admin.static_pages.index')->with('flash_error',tr('static_page_not_found'));
        }

        return view('admin.static_pages.view')
                    ->with('page','pages')
                    ->with('sub_page','static_pages-view')
                    ->with('static_page', $static_page);
    
    }

    /**
     * @method static_pages_status_change()
     *
     * @uses To update static_page status as DECLINED/APPROVED based on static_page id
     *
     * @created Arun
     *
     * @updated 
     *
     * @param - integer static_page_id
     *
     * @return view page 
     */

    public function static_pages_status_change(Request $request) {

        try {

            DB::beginTransaction();

            $static_page = StaticPage::find($request->static_page_id);

            if(!$static_page) {

                throw new Exception(tr('static_page_not_found'), 101);
            }

            $static_page->status = $static_page->status == DECLINED ? APPROVED : DECLINED;

            $static_page->save();

            DB::commit();

            $message = $static_page->status == DECLINED ? tr('static_page_decline_success') : tr('static_page_approve_success');

            return redirect()->back()->with('flash_success', $message);

        } catch(Exception $e) {

            DB::rollback();

            return redirect()->back()->with('flash_error', $e->getMessage());

        }

    }    

    /**
     * @method static_pages_delete()
     *
     * Used to view file of the create the static page 
     *
     * @created Arun
     *
     * @updated 
     *
     * @param -
     *
     * @return view page   
     */

    public function static_pages_delete(Request $request) {

        try {

            DB::beginTransaction();

            $static_page = StaticPage::find($request->static_page_id);

            if(!$static_page) {

                throw new Exception(tr('static_page_not_found'), 101);
                
            }

            if($static_page->delete()) {

                DB::commit();

                return redirect()->route('admin.static_pages.index')->with('flash_success',tr('static_page_deleted_success')); 

            } 

            throw new Exception(tr('static_page_error'));

        } catch(Exception $e) {

            DB::rollback();

            return redirect()->route('admin.static_pages.index')->with('flash_error', $e->getMessage());

        }
    
    }


    /**
     * @method faqs_index()
     *
     * @uses To list out faq details 
     *
     * @created Ganesh
     *
     * @updated 
     *
     * @param 
     * 
     * @return return view page
     *
     */
    public function faqs_index(Request $request) {
       
        $base_query = Faq::orderBy('created_at','desc');

        if($request->search_key){
     
            $base_query = $base_query->where('question','LIKE','%'.$request->search_key.'%');

        }

        $faqs = $base_query->paginate($this->take);

        return view('admin.faqs.index')
                    ->with('main_page','faqs-crud')
                    ->with('page','faqs')
                    ->with('sub_page' , 'faqs-view')
                    ->with('faqs' , $faqs);
    }

    /**
     * @method faqs_create()
     *
     * @uses To create faq details
     *
     * @created  Ganesh
     *
     * @updated 
     *
     * @param 
     * 
     * @return return view page
     *
     */
    public function faqs_create() {

        $faq = new Faq;

        return view('admin.faqs.create')
                    ->with('main_page','faqs-crud')
                    ->with('page' , 'faqs')
                    ->with('sub_page','faqs-create')
                    ->with('faq', $faq);
                
    }

    /**
     * @method faqs_edit()
     *
     * @uses To display and update faqs details based on the faq id
     *
     * @created Ganesh
     *
     * @updated 
     *
     * @param object $request - Faq Id
     * 
     * @return redirect view page 
     *
     */
    public function faqs_edit(Request $request) {

        try {

            $faq = Faq::find($request->faq_id);

            if(!$faq) { 

                throw new Exception(tr('faq_not_found'), 101);

            }
           
            return view('admin.faqs.edit')
                    ->with('main_page','faqs-crud')
                    ->with('page' , 'faqs')
                    ->with('sub_page','faqs-view')
                    ->with('faq' , $faq); 
            
        } catch(Exception $e) {

            return redirect()->route('admin.faqs.index')->with('flash_error', $e->getMessage());
        }
    
    }

    /**
     * @method faqs_save()
     *
     * @uses To save the faqs details of new/existing Faq object based on details
     *
     * @created Ganesh
     *
     * @updated 
     *
     * @param object request - Faq Form Data
     *
     * @return success message
     *
     */
    public function faqs_save(Request $request) {

        try {

            DB::begintransaction();

            $rules = [
                'question' => 'required',
                'answer' => 'required',
            
            ];

            Helper::custom_validator($request->all(),$rules);

            $faq = $request->faq_id ? Faq::find($request->faq_id) : new Faq;

            if(!$faq) {

                throw new Exception(tr('faq_not_found'), 101);
            }

            $faq->question = $request->question;

            $faq->answer = $request->answer;

            $faq->status = APPROVED;

            if($faq->save() ) {

                DB::commit();

                $message = $request->faq_id ? tr('faq_update_success')  : tr('faq_create_success');

                return redirect()->route('admin.faqs.view', ['faq_id' => $faq->id])->with('flash_success', $message);
            } 

            throw new Exception(tr('faq_saved_error') , 101);

        } catch(Exception $e) {

            DB::rollback();

            return redirect()->back()->withInput()->with('flash_error', $e->getMessage());
        } 

    }

    /**
     * @method faqs_view()
     *
     * @uses view the faqs details based on faq id
     *
     * @created Ganesh 
     *
     * @updated 
     *
     * @param object $request - Faq Id
     * 
     * @return View page
     *
     */
    public function faqs_view(Request $request) {
       
        try {
      
            $faq = Faq::find($request->faq_id);
            
            if(!$faq) { 

                throw new Exception(tr('faq_not_found'), 101);                
            }

            return view('admin.faqs.view')
                        ->with('main_page','faqs-crud')
                        ->with('page', 'faqs') 
                        ->with('sub_page','faqs-view') 
                        ->with('faq' , $faq);
            
        } catch (Exception $e) {

            return redirect()->back()->with('flash_error', $e->getMessage());
        }
    
    }

    /**
     * @method faqs_delete()
     *
     * @uses delete the faq details based on faq id
     *
     * @created Ganesh 
     *
     * @updated  
     *
     * @param object $request - Faq Id
     * 
     * @return response of success/failure details with view page
     *
     */
    public function faqs_delete(Request $request) {

        try {

            DB::begintransaction();

            $faq = Faq::find($request->faq_id);
            
            if(!$faq) {

                throw new Exception(tr('faq_not_found'), 101);                
            }

            if($faq->delete()) {

                DB::commit();

                return redirect()->route('admin.faqs.index')->with('flash_success',tr('faq_deleted_success'));   

            } 
            
            throw new Exception(tr('faq_delete_failed'));
            
        } catch(Exception $e){

            DB::rollback();

            return redirect()->back()->with('flash_error', $e->getMessage());

        }       
         
    }

    /**
     * @method faqs_status
     *
     * @uses To update faq status as DECLINED/APPROVED based on faqs id
     *
     * @created Ganesh
     *
     * @updated 
     *
     * @param object $request - Faq Id
     * 
     * @return response success/failure message
     *
     **/
    public function faqs_status(Request $request) {

        try {

            DB::beginTransaction();

            $faq = Faq::find($request->faq_id);

            if(!$faq) {

                throw new Exception(tr('faq_not_found'), 101);
                
            }

            $faq->status = $faq->status ? DECLINED : APPROVED ;

            if($faq->save()) {

                DB::commit();

                $message = $faq->status ? tr('faq_approve_success') : tr('faq_decline_success');

                return redirect()->back()->with('flash_success', $message);
            }
            
            throw new Exception(tr('faq_status_change_failed'));

        } catch(Exception $e) {

            DB::rollback();

            return redirect()->route('admin.faqs.index')->with('flash_error', $e->getMessage());

        }

    }


    /**
     * @method support_contacts_index()
     *
     * @uses To list the support contacts
     *
     * @created Ganesh
     *
     * @updated   
     *
     * @param -
     *
     * @return List of pages   
     */

    public function support_contacts_index(Request $request) {

        $base_query = SupportContact::orderBy('created_at', 'desc');

        if($request->search_key) {

            $base_query = $base_query
                    ->orWhere('name','LIKE','%'.$request->search_key.'%')
                    ->orWhere('email','LIKE','%'.$request->search_key.'%')
                    ->orWhere('mobile','LIKE','%'.$request->search_key.'%');
        }


        $support_contacts = $base_query->paginate($this->paginate_count);

        return view('admin.support_contacts.index')
                    ->with('main_page','support_contacts-crud')
                    ->with('page','support_contacts')
                    ->with('sub_page',"support_contacts-view")
                    ->with('support_contacts',$support_contacts);
    
    }

    /**
     * @method support_contacts_view()
     *
     * @uses view the static_pages details based on static_pages id
     *
     * @created Ganesh 
     *
     * @updated 
     *
     * @param object $request - static_page Id
     * 
     * @return View page
     *
     */
    public function support_contacts_view(Request $request) {

        $support_contact = SupportContact::find($request->support_contact_id);

        if(!$support_contact) {
           
            return redirect()->route('admin.support_contacts.index')->with('flash_error', tr('support_contact_not_found'));

        }

        return view('admin.support_contacts.view')
                    ->with('main_page','support_contacts-crud')
                    ->with('page', 'support_contacts')
                    ->with('sub_page','support_contacts-view')
                    ->with('support_contact' , $support_contact);
    }



     

    
}
Back to Directory File Manager