PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / modules / cost-tracker / widgets / widget-cost-tracker-category-totals.php

widget-cost-tracker-category-totals.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.1, at modules/cost-tracker/widgets/widget-cost-tracker-category-totals.php

238 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Upcoming Category Totals Widget
4 *
5 * Displays the Category Totals.
6 *
7 * @package MainWP/Dashboard
8 * @version 4.6
9 */
10
11 namespace MainWP\Dashboard\Module\CostTracker;
12
13 use MainWP\Dashboard\MainWP_UI;
14 use MainWP\Dashboard\MainWP_Utility;
15
16 // Exit if accessed directly.
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * Class Cost_Tracker_Category_Totals
23 *
24 * Displays the Category Totals.
25 */
26 class Cost_Tracker_Category_Totals {
27
28 /**
29 * Protected static variable to hold the single instance of the class.
30 *
31 * @var mixed Default null
32 */
33 protected static $instance = null;
34
35 /**
36 * Return the single instance of the class.
37 *
38 * @return mixed $instance The single instance of the class.
39 */
40 public static function instance() {
41 if ( is_null( static::$instance ) ) {
42 static::$instance = new self();
43 }
44 return static::$instance;
45 }
46
47 /**
48 * Handle render costs widget.
49 */
50 public function render() {
51 $this->render_widget();
52 }
53
54 /**
55 * Render widget.
56 */
57 public function render_widget() {
58 ?>
59 <div class="mainwp-widget-header">
60 <h2 class="ui header handle-drag">
61 <?php esc_html_e( 'Annual Expense Distribution by Category', 'mainwp' ); ?>
62 <div class="sub header">
63 <?php esc_html_e( 'Visualize your yearly spending proportions across different product categories.', 'mainwp' ); ?>
64 </div>
65 </h2>
66 </div>
67
68 <div class="mainwp-scrolly-overflow">
69 <?php
70 /**
71 * Action: mainwp_module_cost_tracker_widget_top
72 *
73 * Fires at the top of the widget.
74 *
75 * @since 5.0.2
76 */
77 do_action( 'mainwp_module_cost_tracker_widget_top', 'category-totals' );
78 ?>
79 <div id="mainwp-message-zone" style="display:none;" class="ui message"></div>
80 <?php
81 $this->render_widget_content();
82 ?>
83 <?php
84 /**
85 * Action: mainwp_module_cost_tracker_widget_bottom
86 *
87 * Fires at the bottom of the widget.
88 *
89 * @since 5.0.2
90 */
91 do_action( 'mainwp_module_cost_tracker_widget_bottom', 'category-totals' );
92 ?>
93 </div>
94 <?php
95 }
96
97 /**
98 * Get widget costs data.
99 *
100 * @param array $cost_data Cost data.
101 */
102 public static function get_costs_widgets_data( $cost_data ) { //phpcs:ignore -- NOSONAR - complex.
103
104 $categories = array();
105 $series_products_price = array();
106 $colors = array();
107 $product_colors = Cost_Tracker_Admin::get_product_colors();
108 $product_types = Cost_Tracker_Admin::get_product_types();
109
110 if ( is_array( $cost_data ) ) {
111 $current_time = time();
112 $upcoming1 = strtotime( gmdate( 'Y-m-d 00:00:00', $current_time ) );
113 $upcoming2 = strtotime( gmdate( 'Y', $current_time ) . '+1 year' );
114 foreach ( $cost_data as $cost ) {
115 if ( ! isset( $series_products_price[ $cost->product_type ] ) ) {
116 $series_products_price[ $cost->product_type ] = 0;
117 $colors[ $cost->product_type ] = isset( $product_colors[ $cost->product_type ] ) ? $product_colors[ $cost->product_type ] : '';
118 $categories[ $cost->product_type ] = isset( $product_types[ $cost->product_type ] ) ? esc_js( $product_types[ $cost->product_type ] ) : 'N/A';
119 }
120
121 $next_rl = $cost->next_renewal;
122
123 if ( $next_rl <= $upcoming1 ) {
124 $next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false );
125 }
126
127 $next_price = 0;
128 while ( $next_rl <= $upcoming2 ) {
129 if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) {
130 $next_price = $cost->price;
131 }
132 $series_products_price[ $cost->product_type ] += $next_price;
133 $next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false );
134 $next_price = 0;
135 }
136 }
137 }
138
139 $dec = Cost_Tracker_Utility::cost_tracker_format_price( 0, true, array( 'get_decimals' => true ) );
140 foreach ( $series_products_price as $product_type => $price ) {
141 $series_products_price[ $product_type ] = round( $price, $dec );
142 }
143
144 $cur_format = Cost_Tracker_Utility::cost_tracker_format_price( 0, true, array( 'get_currency_format' => true ) );
145 return array(
146 'series' => array_values( $series_products_price ),
147 'categories' => array_values( $categories ),
148 'colors' => array_values( $colors ),
149 'currency_format' => $cur_format['format'],
150 'decimals' => $cur_format['decimals'],
151 );
152 }
153
154 /**
155 * Method render_widget_content()
156 */
157 public function render_widget_content() {
158 $costs_data = Cost_Tracker_DB::get_instance()->get_summary_data( array( 'sum_data' => 'all' ) );
159 $chart_data = static::get_costs_widgets_data( $costs_data );
160 ?>
161 <?php if ( ! empty( $costs_data ) ) : ?>
162 <div id="mainwp-module-cost-tracker-category-totals-wrapper"></div>
163 <script type="text/javascript">
164 jQuery( document ).ready( function() {
165
166 let cost_chart_colors = <?php echo wp_json_encode($chart_data['colors'], true ); //phpcs:ignore -- ok. ?>;
167 let cost_chart_currency_format = '<?php echo esc_js($chart_data['currency_format']); //phpcs:ignore -- ok. ?>';
168
169 let options = {
170 series: <?php echo wp_json_encode($chart_data['series'], true ); //phpcs:ignore -- ok. ?>,
171 chart: {
172 height: '95%',
173 type: 'donut',
174 },
175 labels: <?php echo wp_json_encode($chart_data['categories'], true ); //phpcs:ignore -- ok. ?>,
176 legend: {
177 show: true,
178 labels: {
179 colors: '#999999',
180 useSeriesColors: false
181 },
182 },
183 responsive: [{
184 breakpoint: 480,
185 options: {
186 chart: {
187 width: 200
188 },
189 legend: {
190 show: false
191 }
192 }
193 }],
194 colors: [
195 function ( { value, seriesIndex, dataPointIndex, w } ) {
196 if (cost_chart_colors[seriesIndex] !== undefined) {
197 return cost_chart_colors[seriesIndex];
198 } else {
199 return "#5ec130";
200 }
201 }
202 ],
203 xaxis: {
204 labels: {
205 style: {
206 colors: '#999999',
207 }
208 }
209 },
210 yaxis:{
211 type: 'string',
212 labels: {
213 formatter: function (value) {
214 return __(cost_chart_currency_format, value );
215 },
216 style: {
217 colors: '#999999',
218 }
219 }
220 },
221 stroke: {
222 width: 0
223 },
224 };
225
226 let cost_chart = new ApexCharts(document.querySelector("#mainwp-module-cost-tracker-category-totals-wrapper"), options);
227 setTimeout(() => {
228 cost_chart.render();
229 }, 1000);
230 } );
231 </script>
232 <?php else : ?>
233 <?php MainWP_UI::render_empty_element_placeholder( __( 'No Annual Data Yet', 'mainwp' ), '<a href="admin.php?page=CostTrackerAdd">' . __( 'Add cost tracker data to visualize your yearly spending.', 'mainwp' ) . '</a>', '<em data-emoji=":bar_chart:" class="medium"></em>' ); ?>
234 <?php endif; ?>
235 <?php
236 }
237 }
238