Your IP : 3.149.28.7


Current Path : /home/ncdcgo/public_html/wp-content/plugins/chart-builder/public/
Upload File :
Current File : /home/ncdcgo/public_html/wp-content/plugins/chart-builder/public/class-chart-builder-public.php

<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://ays-pro.com/
 * @since      1.0.0
 *
 * @package    Chart_Builder
 * @subpackage Chart_Builder/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Chart_Builder
 * @subpackage Chart_Builder/public
 * @author     Chart Builder Team <info@ays-pro.com>
 */
class Chart_Builder_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * @var string
	 */
	private $html_class_prefix = 'ays-chart-';

	/**
	 * @var string
	 */
	private $html_name_prefix = 'ays_chart_';

	/**
	 * @var string
	 */
	private $name_prefix = 'chart_';

	/**
	 * @var
	 */
	private $unique_id;

	/**
	 * @var Chart_Builder_DB_Actions
	 */
	private $db_object;

	/**
	 * @var array
	 */
	private $data;


	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;
		$this->db_object =  new Chart_Builder_DB_Actions( $this->plugin_name );

		add_shortcode( 'ays_chart', array( $this, 'ays_generate_chart_method' ) );
	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Chart_Builder_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Chart_Builder_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/chart-builder-public.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Chart_Builder_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Chart_Builder_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		$is_elementor_exists = Chart_Builder_DB_Actions::ays_chart_is_elementor();
        if( !$is_elementor_exists ) {
			wp_enqueue_script( $this->plugin_name . '-plugin', plugin_dir_url( __FILE__ ) . 'js/chart-builder-public-plugin.js', array( 'jquery' ), $this->version, false );
			wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/chart-builder-public.js', array( 'jquery' ), $this->version, false );
			wp_enqueue_script( $this->plugin_name . '-datatable-min', plugin_dir_url( __FILE__ ) . '/js/chart-builder-datatable.min.js', array('jquery'), $this->version, true);
			wp_enqueue_script( $this->plugin_name . "-db4.min.js", plugin_dir_url( __FILE__ ) . 'js/dataTables.bootstrap4.min.js', array( 'jquery' ), $this->version, true );
			wp_enqueue_script( $this->plugin_name . '-charts-google', plugin_dir_url(__FILE__) . 'js/google-chart.js', array('jquery'), $this->version, true);
		}

	}

	public function ays_generate_chart_method( $attr ) {
		$id = (isset($attr['id'])) ? absint(intval($attr['id'])) : null;

		if (is_null($id)) {
			return "<p class='wrong_shortcode_text' style='color:red;'>" . __( 'Wrong shortcode initialized', $this->plugin_name ) . "</p>";
		}

		$content = $this->show_chart( $id, $attr );

		$this->enqueue_styles();
		$this->enqueue_scripts();

		return str_replace( array( "\r\n", "\n", "\r" ), '', $content );
	}

	public function show_chart( $id, $attr ) {

		$chartData = CBActions()->get_chart_data( $id );
		if ( is_null( $chartData ) ) {
			return "<p class='wrong_shortcode_text' style='color:red;'>" . __('Wrong shortcode initialized', $this->plugin_name) . "</p>";
		}
		
		$source_type = $chartData['source_type'];
		$user_id = get_current_user_id();
		if ($source_type == 'quiz_maker' && $user_id == 0) {
			return "<p class='ays_chart_not_logged_text'>" . __('You are not logged in. Please log in to view this chart.', $this->plugin_name) . "</p>";
		}

		$chart = $chartData['chart'];
		$settings = $chartData['settings'];

		$unique_id = uniqid();
		$this->unique_id = $unique_id;

		$data = array();

		if ( is_null( $chart ) ) {
			return "<p class='wrong_shortcode_text' style='color:red;'>" . __('Wrong shortcode initialized', $this->plugin_name) . "</p>";
		}

		$status = isset( $chart['status'] ) && $chart['status'] != '' ? $chart['status'] : '';

		if ( $status != 'published' ) {
			return "<p class='wrong_shortcode_text' style='color:red;'>" . __('Wrong shortcode initialized', $this->plugin_name) . "</p>";
		}

		$chart_default_colors = array('#3366cc','#dc3912','#ff9900','#109618', '#990099','#0099c6','#dd4477','#66aa00', '#b82e2e','#316395','#994499','#22aa99', '#aaaa11','#6633cc','#e67300','#8b0707', '#651067','#329262','#5574a6','#3b3eac', '#b77322','#16d620','#b91383','#f4359e', '#9c5935','#a9c413','#2a778d','#668d1c', '#bea413','#0c5922','#743411');
		$combo_types = array(
			"line" => __("Line", "chart-builder"),
			"bars" => __("Bar", "chart-builder"),
			"area" => __("Area", "chart-builder"),
			"steppedArea" => __("Stepped Area", "chart-builder"),
		);

		$chart_title = (isset($chart['title']) && $chart['title'] != '') ? stripslashes ( sanitize_text_field( $chart['title'] ) ) : '';
		$settings['title_font_size'] = (isset( $settings['title_font_size'] ) && $settings['title_font_size'] != '') ? esc_attr( $settings['title_font_size'] ) : '30';
		$chart_description = (isset($chart['description']) && $chart['description'] != '') ? stripslashes ( sanitize_text_field( $chart['description'] ) ) : '';
		$settings['description_font_size'] = (isset( $settings['description_font_size'] ) && $settings['description_font_size'] != '') ? esc_attr( $settings['description_font_size'] ) : '16';
		$settings['description_color'] = isset( $settings['description_color'] ) && $settings['description_color'] != '' ? esc_attr( $settings['description_color'] ) : '#4c4c4c';
		$settings['title_position'] = isset( $settings['title_position'] ) && $settings['title_position'] != '' ? esc_attr( $settings['title_position'] ) : 'left';
		$show_title = isset( $settings['show_title'] ) && $settings['show_title'] != '' ? esc_attr( $settings['show_title'] ) : 'on';
		
		if ($chartData['source_chart_type'] == 'line_chart' || $chartData['source_chart_type'] == 'bar_chart' || $chartData['source_chart_type'] == 'pie_chart' || $chartData['source_chart_type'] == 'column_chart' || $chartData['source_chart_type'] == 'donut_chart' || $chartData['source_chart_type'] == 'area_chart' || $chartData['source_chart_type'] == 'combo_chart' || $chartData['source_chart_type'] == 'stepped_area_chart' || $chartData['source_chart_type'] == '3dpie_chart') {
			if ( !isset( $chartData['source'][0] ) ){
				if (count($chartData['source'][1]) > 2) {
					$titles = array();
					for ($i = 0; $i < count($chartData['source'][1]); $i++) {
						array_push($titles, __("Title", $this->plugin_name).$i);
					}
					$chartData['source'][0] = $titles;
				} else {
					$chartData['source'][0] = array(
						__("Country", $this->plugin_name),
						__("Population", $this->plugin_name),
					);
				}
	
				ksort($chartData['source']);
			}

			$count_slices = (isset($chartData['source']) && !is_null($chartData['source'])) ? count($chartData['source']) - 1 : 0;
        	$count_series = (isset($chartData['source'][0]) && !is_null($chartData['source'][0])) ? count($chartData['source'][0]) - 1 : 0;

			// Slices settings
			$settings['slice_colors_default'] = $chart_default_colors;
			$settings['slice_color'] = isset( $settings['slice_color'] ) && $settings['slice_color'] != '' ? json_decode($settings['slice_color'], true) : $chart_default_colors;
			$settings['slice_offset'] = isset( $settings['slice_offset'] ) && $settings['slice_offset'] != '' ? json_decode($settings['slice_offset'], true) : array_fill(0, $count_slices, 0);
			$settings['slice_text_color'] = isset( $settings['slice_text_color'] ) && $settings['slice_text_color'] != '' ? json_decode($settings['slice_text_color'], true) : array_fill(0, $count_slices, '#ffffff');
			
			// Series settings
			$settings['series_colors_default'] = $chart_default_colors;
			$settings['series_color'] = isset( $settings['series_color'] ) && $settings['series_color'] != '' ? json_decode($settings['series_color'], true) : $chart_default_colors;
			$settings['series_visible_in_legend'] = isset( $settings['series_visible_in_legend'] ) && $settings['series_visible_in_legend'] != '' ? json_decode($settings['series_visible_in_legend'], true) : array_fill(0, $count_series, 'on');
			$settings['combo_types'] = $combo_types;
			$settings['combo_type'] = isset( $settings['combo_type'] ) && $settings['combo_type'] != '' ? json_decode($settings['combo_type'], true) : array_fill(0, $count_series, 'line');
		}
		$data['chart_data_by_types'] = isset($chartData['source']) && !empty($chartData['source']) ? array() : CBActions()->get_chart_data_by_types();

		$data['chart_type'] = $chartData['source_chart_type'];
		// if( !isset( $chartData['source'][0] ) ){
		// 	$chartData['source'][0] = array(
		// 		__("Country", $this->plugin_name),
		// 		__("Population", $this->plugin_name),
		// 	);

		// 	ksort($chartData['source']);
		// }
		$data['source'] = $chartData['source'];

		$data['options'] = $settings;

		$content = array();

		$content[] = "<div class='" . $this->html_class_prefix . "container " . $this->html_class_prefix . "container-" . $id . "' id='" . $this->html_class_prefix . "container" . $unique_id . "' data-id='" . $unique_id . "'>";

		$content[] = "<div class='" . $this->html_class_prefix . "header-container'>";

		if ($show_title == 'on') {
			$content[] = "<p class='" . $this->html_class_prefix . "charts-title " . $this->html_class_prefix . "charts-title" . $unique_id . "'>";
			$content[] = $chart_title;
			$content[] = "</p>";
		}

		$content[] = "<p class='" . $this->html_class_prefix . "charts-description " . $this->html_class_prefix . "charts-description" . $unique_id . "'>";
		$content[] = $chart_description;
		$content[] = "</p>";
		$content[] = "</div>";

		$content[] = "<div class='" . $this->html_class_prefix . "charts-main-container " . $this->html_class_prefix . "charts-main-container" . $unique_id . "' id=" . $this->html_class_prefix . $chartData['source_chart_type'] . $unique_id . "></div>";

		$content[] = "</div>";

		$content[] = "<style>";

		$content[] = "#" . $this->html_class_prefix . "container" . $unique_id . " div." . $this->html_class_prefix . "charts-main-container" . $unique_id . " {
							width: " . $settings['width'] . "%;
							height: " . $settings['height'] . "px;
						}";
		
		if ($chartData['source_chart_type'] == 'word_tree') {
			$content[] = "#" . $this->html_class_prefix . "container" . $unique_id . " div." . $this->html_class_prefix . "charts-main-container" . $unique_id . " svg g:first-of-type rect:last-child {
								display: none !important;
							}";
		}

		$content[] = "#" . $this->html_class_prefix . "container" . $unique_id . " div." . $this->html_class_prefix . "header-container>p." . $this->html_class_prefix . "charts-title" . $unique_id . " {
							color: " . $settings['title_color'] . " !important;
							font-size: " . $settings['title_font_size'] . "px !important;
							text-align: " . $settings['title_position'] . ";
						}";

		$content[] = "#" . $this->html_class_prefix . "container" . $unique_id . " div." . $this->html_class_prefix . "header-container>p." . $this->html_class_prefix . "charts-description" . $unique_id . " {
							color: " . $settings['description_color'] . " !important;
							font-size: " . $settings['description_font_size'] . "px !important; 
						}";



		$content[] = "</style>";

		$this->data = $data;

		$content[] = $this->get_encoded_options();

		return implode( '', $content );
	}

	public function get_encoded_options () {
		$content = array();
		$data = $this->data;

		$content[] = '<script type="text/javascript">';

		$content[] = "
                if(typeof aysChartOptions === 'undefined'){
                    var aysChartOptions = [];
                }
                aysChartOptions['" . $this->unique_id . "']  = '" . base64_encode( json_encode( $data ) ) . "';";

		$content[] = '</script>';

		return implode( '', $content );
	}


}