Viewing File: /home/ubuntu/vedadeals-backend-base/app/Http/Controllers/Admin/StaticPageController.php

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Helpers\Helper;

use DB, Exception, Setting;

use App\Models\{ StaticPage };

use App\Http\Requests\Admin\StaticPage\{ StaticPagePostRequest, StaticPageGetRequest };

class StaticPageController 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);
    }

    /**
     * @method static_pages_index()
     *
     * @uses to list the static_pages
     *
     * @created Karthick
     *
     * @updated  
     *
     * @param
     *
     * @return to static_pages index page
     */

    public function static_pages_index(Request $request) {

        $base_query = StaticPage::orderBy('created_at','DESC');

        if($request->filled('search_key')) {

            $base_query = $base_query->where(function ($query) use ($request) {
                                $query->where('title', "LIKE", "%" . $request->search_key . "%");
                            });
        }

        if($request->filled('status')) {

            $base_query = $base_query->where('status', $request->status);
            
        } 

        $static_pages = $base_query->paginate($this->take);
        
        return view('admin.static_pages.index')
                    ->with('page', 'static_pages')
                    ->with('sub_page', 'static_pages-view')
                    ->with('static_pages', $static_pages);
    
    }

    /**
     * @method static_pages_create()
     *
     * @uses To create a new static page
     *
     * @created  Karthick
     *
     * @updated 
     *
     * @param create a new object
     * 
     * @return to create static page page
     *
     */
    public function static_pages_create() {

        try {

        $static_page = new StaticPage;

        $page_types = Helper::get_static_page_types();

        return view('admin.static_pages.create')
                    ->with('page', 'static_pages')
                    ->with('sub_page', 'static_pages-create')
                    ->with('page_types', array_unique($page_types))
                    ->with('static_page', $static_page);     

        } catch(Exception $e) {

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

    /**
     * @method static_pages_edit()
     *
     * @uses to edit static page details based on the static_page_id
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param object $request - static_page_id
     * 
     * @return to edit static page page 
     *
     */
    public function static_pages_edit(StaticPageGetRequest $request) {

        try {

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

            $page_types = Helper::get_static_page_types();

            $page_types[] = $static_page->type;

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

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

    /**
     * @method static_pages_save()
     *
     * @uses to save the static page details of new/existing object based on static_page_id
     *
     * @created Karthick
     *
     * @updated
     *
     * @param object $request - static_page_id
     *
     * @return view static page page
     *
     */
    public function static_pages_save(StaticPagePostRequest $request) {
            
        try {

            DB::begintransaction();

            $static_page = StaticPage::updateOrCreate(['id' => $request->static_page_id], $request->validated());

            if($static_page) {

                DB::commit();

                $message = $request->static_page_id ? tr('static_page_updated_success') : tr('static_page_created_success');

                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'));
            
        } catch(Exception $e){ 

            DB::rollback();

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

        } 

    }

    /**
     * @method static_pages_view()
     *
     * @uses to view the static page details based on static_page_id
     *
     * @created Karthick 
     *
     * @updated 
     *
     * @param object $request - static_page_id
     * 
     * @return View page
     *
     */
    public function static_pages_view(StaticPageGetRequest $request) {
       
        try {
      
            $static_page = StaticPage::find($request->static_page_id);   

            return view('admin.static_pages.view')
                        ->with('page', 'static_pages')
                        ->with('sub_page', 'static_pages-view')
                        ->with('static_page',$static_page);
            
        } catch (Exception $e) {

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


    /**
     * @method static_pages_status
     *
     * @uses to change static page status as DECLINED/APPROVED based on static_page_id
     *
     * @created Karthick
     *
     * @updated 
     *
     * @param object $request - static_page_id
     * 
     * @return response success/failure message
     *
     **/
    public function static_pages_status(StaticPageGetRequest $request) {

        try {

            DB::beginTransaction();

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

            $result = $static_page->update(['status' => $static_page->status ? DECLINED : APPROVED]);

            if($result) {

                DB::commit();

                Helper::settings_generate_json();

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

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

        } catch(Exception $e) {

            DB::rollback();

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

        }

    }

    /**
     * @method static_pages_delete()
     *
     * @uses to delete the static page details based on static_page_id
     *
     * @created Karthick 
     *
     * @updated  
     *
     * @param object $request - static_page_id
     * 
     * @return response of success/failure details with view page
     *
     */
    public function static_pages_delete(StaticPageGetRequest $request) {

        try {

            DB::begintransaction();

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

            if($static_page->delete()) {

                DB::commit();

                Helper::settings_generate_json();

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

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

            DB::rollback();

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

        }       
         
    }
}
Back to Directory File Manager