Untitled

mail@pastecode.io avatar
unknown
php
3 years ago
23 kB
2
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Helpers\FlashMsg;
use App\Mail\BasicMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use App\Language;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Spatie\Sitemap\SitemapGenerator;

class GeneralSettingsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');

        $this->middleware('permission:general-settings-site-identity',['only',['site_identity','update_site_identity']]);
        $this->middleware('permission:general-settings-basic-settings',['only',['basic_settings','update_basic_settings']]);
        $this->middleware('permission:general-settings-color-settings',['only',['color_settings','update_color_settings']]);
        $this->middleware('permission:general-settings-typography-settings',['only',['typography_settings','get_single_font_variant','update_typography_settings']]);
        $this->middleware('permission:general-settings-seo-settings',['only',['seo_settings','update_seo_settings']]);
        $this->middleware('permission:general-settings-third-party-script',['only',['update_scripts_settings','scripts_settings']]);
        $this->middleware('permission:general-settings-email-template',['only',['email_template_settings','update_email_template_settings']]);
        $this->middleware('permission:general-settings-smtp-settings',['only',['smtp_settings','update_smtp_settings','test_smtp_settings']]);
        $this->middleware('permission:general-settings-page-settings',['only',['page_settings','update_page_settings']]);;
        $this->middleware('permission:general-settings-custom-css',['only',['custom_css_settings','update_custom_css_settings']]);
        $this->middleware('permission:general-settings-custom-js',['only',['custom_js_settings','update_custom_js_settings']]);
        $this->middleware('permission:general-settings-license-settings',['only',['license_settings','update_license_settings']]);
        $this->middleware('permission:general-settings-cache-settings',['only',['cache_settings','update_cache_settings']]);
    }

    public function site_identity()
    {
        return view('backend.general-settings.site-identity');
    }

    public function update_site_identity(Request $request)
    {
        $this->validate($request, [
            'site_logo' => 'nullable|string',
            'site_purple_logo' => 'nullable|string',
            'site_favicon' => 'nullable|string',
            'site_purple_favicon' => 'nullable|string',
            'site_breadcrumb_image' => 'nullable|string',

        ]);
        $fields = [
            'site_logo',
            'site_favicon',
            'site_purple_logo',
            'site_purple_favicon',
            'site_breadcrumb_image'
        ];
        foreach ($fields as $field) {
            if ($request->has($field)) {
                update_static_option($field, $request->$field);
            }
        }
        return redirect()->back()->with(FlashMsg::settings_update());
    }

    public function basic_settings()
    {
        $all_languages = Language::all();
        return view('backend.general-settings.basic')->with(['all_languages' => $all_languages]);
    }

    public function update_basic_settings(Request $request)
    {
        $this->validate($request, [
            'language_select_option' => 'nullable|string',
            'disable_user_email_verify' => 'nullable|string',
            'site_maintenance_mode' => 'nullable|string',
            'admin_loader_animation' => 'nullable|string',
            'site_loader_animation' => 'nullable|string',
            'site_force_ssl_redirection' => 'nullable|string',
            'admin_panel_rtl_status' => 'nullable|string',
            'site_google_captcha_enable' => 'nullable|string',
        ]);
        $all_language = Language::all();
        foreach ($all_language as $lang) {
            $this->validate($request, [
                'site_' . $lang->slug . '_title' => 'nullable|string',
                'site_' . $lang->slug . '_tag_line' => 'nullable|string',
                'site_' . $lang->slug . '_footer_copyright' => 'nullable|string',
            ]);
            $_title = 'site_' . $lang->slug . '_title';
            $_tag_line = 'site_' . $lang->slug . '_tag_line';
            $_footer_copyright = 'site_' . $lang->slug . '_footer_copyright';
            update_static_option($_title, $request->$_title);
            update_static_option($_tag_line, $request->$_tag_line);
            update_static_option($_footer_copyright, $request->$_footer_copyright);
        }
        $all_fields = [
            'language_select_option',
            'disable_user_email_verify',
            'site_maintenance_mode',
            'admin_loader_animation',
            'site_loader_animation',
            'site_force_ssl_redirection',
            'site_google_captcha_enable',
        ];
        foreach ($all_fields as $field) {
            update_static_option($field, $request->$field);
        }
        return redirect()->back()->with(FlashMsg::settings_update());
    }

    public function color_settings()
    {
        return view('backend.general-settings.color-settings');
    }

    public function update_color_settings(Request $request)
    {
        $this->validate($request, [
            'site_main_color_one' => 'nullable|string',
            'site_main_color_two' => 'nullable|string',
        ]);

        $all_fields = [
          'site_main_color_one',
          'site_main_color_two',
          'site_heading_color',
          'site_paragraph_color',
        ];

        foreach ($all_fields as $field) {
            update_static_option($field, $request->$field);
        }
        return redirect()->back()->with(FlashMsg::settings_update());
    }

    public function seo_settings()
    {
        $all_languages = Language::all();
        return view('backend.general-settings.seo')->with(['all_languages' => $all_languages]);
    }
    public function update_seo_settings(Request $request)
    {
        $all_languages = Language::all();
        foreach ($all_languages as $lang) {
            $this->validate($request, [
                'site_meta_' . $lang->slug . '_tags' => 'nullable|string',
                'site_meta_' . $lang->slug . '_description' => 'nullable|string',
                'og_meta_' . $lang->slug . '_title' => 'nullable|string',
                'og_meta_' . $lang->slug . '_description' => 'nullable|string',
                'og_meta_' . $lang->slug . '_site_name' => 'nullable|string',
                'og_meta_' . $lang->slug . '_url' => 'nullable|string',
                'og_meta_' . $lang->slug . '_image' => 'nullable|string',
            ]);
            $fields = [
                'site_meta_' . $lang->slug . '_tags',
                'site_meta_' . $lang->slug . '_description',
                'og_meta_' . $lang->slug . '_title',
                'og_meta_' . $lang->slug . '_description',
                'og_meta_' . $lang->slug . '_site_name',
                'og_meta_' . $lang->slug . '_url',
                'og_meta_' . $lang->slug . '_image'
            ];
            foreach ($fields as $field) {
                if ($request->has($field)) {
                    update_static_option($field, $request->$field);
                }
            }
        }
        return redirect()->back()->with(FlashMsg::settings_update());
    }
    public function scripts_settings()
    {
        return view('backend.general-settings.thid-party');
    }
    public function update_scripts_settings(Request $request)
    {

        $this->validate($request, [
            'site_disqus_key' => 'nullable|string',
            'tawk_api_key' => 'nullable|string',
            'site_third_party_tracking_code' => 'nullable|string',
            'site_google_analytics' => 'nullable|string',
            'site_google_captcha_v3_site_key' => 'nullable|string',
            'site_google_captcha_v3_secret_key' => 'nullable|string',
        ]);
        $fields = [
            'site_disqus_key',
            'site_google_analytics',
            'tawk_api_key',
            'site_google_captcha_v3_site_key',
            'site_google_captcha_v3_secret_key',
            'site_third_party_tracking_code',
            'enable_google_login',
           'enable_facebook_login',
           'google_client_id',
           'google_client_secret',
           'facebook_client_id',
           'facebook_client_secret',
           'envato_api_token',
           'site_third_party_tracking_code_body_start',
           'enable_envato_login',
           'envato_client_id',
           'envato_client_secret',
        ];
        foreach ($fields as $field) {
            if ($request->has($field)) {
                update_static_option($field, $request->$field);
            }
        }
        setEnvValue([
           'FACEBOOK_CLIENT_ID' => $request->facebook_client_id,
           'FACEBOOK_CLIENT_SECRET' => $request->facebook_client_secret,
           'FACEBOOK_CALLBACK_URL' => route('facebook.callback'),
           'GOOGLE_CLIENT_ID' => $request->google_client_id,
           'GOOGLE_CLIENT_SECRET' => $request->google_client_secret,
           'GOOGLE_CALLBACK_URL' => route('google.callback'),
       ]);
        return redirect()->back()->with(FlashMsg::settings_update());
    }
    public function email_template_settings()
    {
        return view('backend.general-settings.email-template');
    }
    public function update_email_template_settings(Request $request)
    {

        $this->validate($request, [
            'site_global_email' => 'required|string',
            'site_global_email_template' => 'required|string',
        ]);

        $save_data = [
            'site_global_email',
            'site_global_email_template'
        ];
        foreach ($save_data as $item) {
            if (empty($request->$item)) {
                continue;
            }
            update_static_option($item, $request->$item);
        }

        return redirect()->back()->with(FlashMsg::settings_update());
    }
    public function cache_settings()
    {
        return view('backend.general-settings.cache-settings');
    }

    public function update_cache_settings(Request $request)
    {

        $this->validate($request, [
            'cache_type' => 'required|string'
        ]);

        Artisan::call($request->cache_type . ':clear');

        return redirect()->back()->with(['msg' => __('Cache Cleaned'), 'type' => 'success']);
    }
    public function typography_settings()
    {
        $all_google_fonts = file_get_contents('assets/frontend/webfonts/google-fonts.json');
        return view('backend.general-settings.typograhpy')->with(['google_fonts' => json_decode($all_google_fonts)]);
    }
    public function get_single_font_variant(Request $request)
    {
        $all_google_fonts = file_get_contents('assets/frontend/webfonts/google-fonts.json');
        $decoded_fonts = json_decode($all_google_fonts, true);
        return response()->json($decoded_fonts[$request->font_family]);
    }
    public function update_typography_settings(Request $request)
    {
        $this->validate($request, [
            'body_font_family' => 'required|string|max:191',
            'body_font_variant' => 'required',
            'heading_font' => 'nullable|string',
            'heading_font_family' => 'nullable|string|max:191',
            'heading_font_variant' => 'nullable',
        ]);

        $save_data = [
            'body_font_family',
            'heading_font_family',
        ];
        foreach ($save_data as $item) {
            update_static_option($item, $request->$item);
        }
        $body_font_variant = !empty($request->body_font_variant) ?  $request->body_font_variant : ['regular'];
        $heading_font_variant = !empty($request->heading_font_variant) ?  $request->heading_font_variant : ['regular'];

        update_static_option('heading_font', $request->heading_font);
        update_static_option('body_font_variant', serialize($body_font_variant));
        update_static_option('heading_font_variant', serialize($heading_font_variant));

        return redirect()->back()->with(['msg' => __('Typography Settings Updated..'), 'type' => 'success']);
    }
    public function smtp_settings()
    {
        return view('backend.general-settings.smtp-settings');
    }
    public function update_smtp_settings(Request $request)
    {

        $this->validate($request, [
            'site_smtp_mail_host' => 'required|string',
            'site_smtp_mail_mailer' => 'required|string',
            'site_smtp_mail_port' => 'required|string',
            'site_smtp_mail_username' => 'required|string',
            'site_smtp_mail_password' => 'required|string',
            'site_smtp_mail_encryption' => 'required|string'
        ]);

        $fields = [
            'site_smtp_mail_mailer',
            'site_smtp_mail_host',
            'site_smtp_mail_port',
            'site_smtp_mail_username',
            'site_smtp_mail_password',
            'site_smtp_mail_encryption',
        ];
        foreach ($fields as $field) {
            update_static_option($field, $request->$field);
        }
        $env_val['MAIL_MAILER'] = !empty($request->site_smtp_mail_mailer) ? $request->site_smtp_mail_mailer : 'smtp';
        $env_val['MAIL_HOST'] = !empty($request->site_smtp_mail_host) ? $request->site_smtp_mail_host : 'YOUR_SMTP_MAIL_HOST';
        $env_val['MAIL_PORT'] = !empty($request->site_smtp_mail_port) ? $request->site_smtp_mail_port : 'YOUR_SMTP_MAIL_POST';
        $env_val['MAIL_USERNAME'] = !empty($request->site_smtp_mail_username) ? $request->site_smtp_mail_username : 'YOUR_SMTP_MAIL_USERNAME';
        $env_val['MAIL_PASSWORD'] = !empty($request->site_smtp_mail_password) ? $request->site_smtp_mail_password : 'YOUR_SMTP_MAIL_USERNAME_PASSWORD';
        $env_val['MAIL_ENCRYPTION'] = !empty($request->site_smtp_mail_encryption) ? $request->site_smtp_mail_encryption : 'YOUR_SMTP_MAIL_ENCRYPTION';

        setEnvValue($env_val);

        return redirect()->back()->with(FlashMsg::settings_update());
    }


    public function test_smtp_settings(Request $request){
      $this->validate($request,[
         'subject' => 'required|string|max:191',
         'email' => 'required|email|max:191',
         'message' => 'required|string',
      ]);
      $res_data = [
          'msg' => __('Mail Send Success'),
          'type' => 'success'
      ];
        try {
            Mail::to($request->email)->send(new BasicMail([
                'subject' => $request->subject,
                'message' => $request->message
            ]));
        }catch (\Exception $e){
            return  redirect()->back()->with([
                'type' => 'danger',
                'msg' => $e->getMessage()
            ]);
        }

      return redirect()->back()->with($res_data);
  }

    public function page_settings()
    {
        $all_languages = Language::all();
        return view('backend.general-settings.page-settings')->with(['all_languages' => $all_languages]);
    }

    public function update_page_settings(Request $request)
    {
        $all_languages = Language::all();
        $this->validate($request, [
            'knowledgebase_page_slug'=> 'nullable|string|max:191',
            'blog_page_slug'=> 'nullable|string|max:191',
            'contact_page_slug'=> 'nullable|string|max:191',
            'faq'=> 'nullable|string|max:191',
            'ticket'=> 'nullable|string|max:191',

        ]);
        $pages_list = ['knowledgebase','blog','contact','faq','support_ticket'];

        foreach ($pages_list as $slug_field) {
            $field = $slug_field.'_page_slug';
            update_static_option($field, Str::slug($request->$field));
        }
        foreach ($all_languages as $lang) {
            $this->validate($request, [
                'knowledgebase_page_' . $lang->slug . '_name' => 'nullable|string',
                'knowledgebase_page_' . $lang->slug . '_meta_tags' => 'nullable|string',
                'knowledgebase_page_' . $lang->slug . '_meta_description' => 'nullable|string',
                'contact_page_' . $lang->slug . '_name' => 'nullable|string',
                'contact_page_' . $lang->slug . '_meta_tags' => 'nullable|string',
                'contact_page_' . $lang->slug . '_meta_description' => 'nullable|string',
                'blog_page_' . $lang->slug . '_name' => 'nullable|string',
                'blog_page_' . $lang->slug . '_meta_tags' => 'nullable|string',
                'blog_page_' . $lang->slug . '_meta_description' => 'nullable|string',

                'faq_page_' . $lang->slug . '_name' => 'nullable|string',
                'faq_page_' . $lang->slug . '_meta_tags' => 'nullable|string',
                'faq_page_' . $lang->slug . '_meta_description' => 'nullable|string',

                'support_ticket_page_' . $lang->slug . '_name' => 'nullable|string',
                'support_ticket_page_' . $lang->slug . '_meta_tags' => 'nullable|string',
                'support_ticket_page_' . $lang->slug . '_meta_description' => 'nullable|string',
            ]);

            foreach ($pages_list as $field) {
                $field_name = $field.'_page_'. $lang->slug.'_name';
                $field_meta_tags = $field.'_page_'. $lang->slug.'_meta_tags';
                $field_meta_meta_description = $field.'_page_'. $lang->slug.'_meta_description';
                update_static_option($field_name, $request->$field_name);
                update_static_option($field_meta_tags, $request->$field_meta_tags);
                update_static_option($field_meta_meta_description, $request->$field_meta_meta_description);
            }
        }
        return redirect()->back()->with(FlashMsg::settings_update());
    }
    public function payment_settings()
    {
        return view('backend.general-settings.payment-gateway');
    }


    public function custom_css_settings()
    {
        $custom_css = '/* Write Custom Css Here */';
        if (file_exists('assets/frontend/css/dynamic-style.css')) {
            $custom_css = file_get_contents('assets/frontend/css/dynamic-style.css');
        }
        return view('backend.general-settings.custom-css')->with(['custom_css' => $custom_css]);
    }

    public function update_custom_css_settings(Request $request)
    {
        file_put_contents('assets/frontend/css/dynamic-style.css', $request->custom_css_area);

        return redirect()->back()->with(['msg' => __('Custom Style Successfully Added...'), 'type' => 'success']);
    }

    public function custom_js_settings()
    {
        $custom_js = '/* Write Custom js Here */';
        if (file_exists('assets/frontend/js/dynamic-script.js')) {
            $custom_js = file_get_contents('assets/frontend/js/dynamic-script.js');
        }
        return view('backend.general-settings.custom-js')->with(['custom_js' => $custom_js]);
    }

    public function update_custom_js_settings(Request $request)
    {
        file_put_contents('assets/frontend/js/dynamic-script.js', $request->custom_js_area);

        return redirect()->back()->with(['msg' => __('Custom Script Successfully Added...'), 'type' => 'success']);
    }
    public function gdpr_settings()
    {
        $all_languages = Language::all();
        return view('backend.general-settings.gdpr')->with(['all_languages' => $all_languages]);
    }

    public function update_gdpr_cookie_settings(Request $request)
    {

        $this->validate($request, [
            'site_gdpr_cookie_enabled' => 'nullable|string|max:191',
            'site_gdpr_cookie_expire' => 'required|string|max:191',
            'site_gdpr_cookie_delay' => 'required|string|max:191',
        ]);

        $all_language = Language::all();
        foreach ($all_language as $lang) {
            $this->validate($request, [
                "site_gdpr_cookie_" . $lang->slug . "_title" => 'nullable|string',
                "site_gdpr_cookie_" . $lang->slug . "_message" => 'nullable|string',
                "site_gdpr_cookie_" . $lang->slug . "_more_info_label" => 'nullable|string',
                "site_gdpr_cookie_" . $lang->slug . "_more_info_link" => 'nullable|string',
                "site_gdpr_cookie_" . $lang->slug . "_accept_button_label" => 'nullable|string',
                "site_gdpr_cookie_" . $lang->slug . "_decline_button_label" => 'nullable|string',
            ]);
            $_title = "site_gdpr_cookie_" . $lang->slug . "_title";
            $_message = "site_gdpr_cookie_" . $lang->slug . "_message";
            $_more_info_label = "site_gdpr_cookie_" . $lang->slug . "_more_info_label";
            $_more_info_link = "site_gdpr_cookie_" . $lang->slug . "_more_info_link";
            $_accept_button_label = "site_gdpr_cookie_" . $lang->slug . "_accept_button_label";
            $decline_button_label = "site_gdpr_cookie_" . $lang->slug . "_decline_button_label";

            $fields = [
                $_title,
                $_message,
                $_more_info_label,
                $_more_info_link,
                $_accept_button_label,
                $decline_button_label,
            ];
            foreach ($fields as $field) {
                if ($request->has($field)) {
                    update_static_option($field, $request->$field);
                }
            }
        }
        $fields = [
            'site_gdpr_cookie_delay',
            'site_gdpr_cookie_enabled',
            'site_gdpr_cookie_expire',
        ];
        foreach ($fields as $field) {
            update_static_option($field, $request->$field);
        }
        return redirect()->back()->with(FlashMsg::settings_update());
    }


    public function license_settings()
    {
        return view('backend.general-settings.license-settings');
    }

    public function update_license_settings(Request $request)
    {
        
    }

    public function sitemap_settings()
    {
        $all_sitemap = glob('sitemap/*');
        return view('backend.general-settings.sitemap-settings')->with(['all_sitemap' => $all_sitemap]);
    }

    public function update_sitemap_settings(Request $request)
    {
        $this->validate($request, [
            'site_url' => 'required|url',
            'title' => 'nullable|string',
        ]);
        $title = $request->title ? $request->title : time();
        SitemapGenerator::create(strip_tags($request->site_url))->writeToFile('sitemap/sitemap-' . $title . '.xml');
        return redirect()->back()->with([
            'msg' => __('Sitemap Generated..'),
            'type' => 'success'
        ]);
    }

    public function delete_sitemap_settings(Request $request)
    {
        if (file_exists($request->sitemap_name)) {
            @unlink($request->sitemap_name);
        }
        return redirect()->back()->with(['msg' => __('Sitemap Deleted...'), 'type' => 'danger']);
    }



}