| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Cost Tracker Clients Widget |
| 4 |
* |
| 5 |
* Displays the Clients's Cost Tracker Info. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
* @version 4.6 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace MainWP\Dashboard\Module\CostTracker; |
| 12 |
|
| 13 |
use MainWP\Dashboard\MainWP_DB_Client; |
| 14 |
use MainWP\Dashboard\MainWP_Utility; |
| 15 |
use MainWP\Dashboard\MainWP_UI; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Cost_Tracker_Clients_Widget |
| 24 |
*/ |
| 25 |
class Cost_Tracker_Clients_Widget { |
| 26 |
|
| 27 |
/** |
| 28 |
* Public static variable to hold the single instance of class. |
| 29 |
* |
| 30 |
* @var mixed Default null |
| 31 |
*/ |
| 32 |
public static $instance = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Public static variable to hold the clients costs. |
| 36 |
* |
| 37 |
* @var mixed Default array(). |
| 38 |
*/ |
| 39 |
public $clients_sites = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Method instance(). |
| 43 |
* |
| 44 |
* Get instance class. |
| 45 |
*/ |
| 46 |
public static function instance() { |
| 47 |
if ( null === static::$instance ) { |
| 48 |
static::$instance = new self(); |
| 49 |
} |
| 50 |
return static::$instance; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructor class. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function __construct() { |
| 59 |
// construct. |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Method callback_render_tasks_client_page_widget(). |
| 65 |
* |
| 66 |
* Handle callback render tasks client page widget. |
| 67 |
*/ |
| 68 |
public function callback_render_costs_widget() { |
| 69 |
if ( ! isset( $_GET['page'] ) || 'ManageClients' !== $_GET['page'] || empty( $_GET['client_id'] ) ) { //phpcs:ignore -- ok. |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
?> |
| 74 |
<div class="ui grid mainwp-widget-header"> |
| 75 |
<div class="twelve wide column"> |
| 76 |
<h2 class="ui header handle-drag"> |
| 77 |
<?php esc_html_e( 'Cost Tracker', 'mainwp' ); ?> |
| 78 |
<div class="sub header"><?php esc_html_e( 'Manage and monitor your expenses.', 'mainwp' ); ?></div> |
| 79 |
</h2> |
| 80 |
</div> |
| 81 |
</div> |
| 82 |
<div class="mainwp-scrolly-overflow"> |
| 83 |
<?php $this->render_costs_tracker_widget_content(); ?> |
| 84 |
</div> |
| 85 |
<div class="ui two columns grid mainwp-widget-footer"> |
| 86 |
<div class="left aligned column"> |
| 87 |
<a href="admin.php?page=ManageCostTracker" class="ui basic mini button"><?php esc_html_e( 'Cost Tracker Dashboard', 'mainwp' ); ?></a> |
| 88 |
</div> |
| 89 |
</div> |
| 90 |
<?php |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Method render_tasks_client_page_widget_content(). |
| 96 |
*/ |
| 97 |
public function render_costs_tracker_widget_content() { // phpcs:ignore -- NOSONAR - complex method. |
| 98 |
$client_id = intval( $_GET['client_id'] ); //phpcs:ignore -- ok. |
| 99 |
|
| 100 |
$this->clients_sites = array(); |
| 101 |
// get all sites of the $client_id. |
| 102 |
$cls_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( array( $client_id ) ); |
| 103 |
if ( is_array( $cls_sites ) ) { |
| 104 |
foreach ( $cls_sites as $cls_site ) { |
| 105 |
if ( ! empty( $cls_site->client_id ) && ! isset( $this->clients_sites[ $cls_site->client_id ] ) ) { |
| 106 |
$this->clients_sites[ $cls_site->client_id ] = array(); |
| 107 |
} |
| 108 |
$this->clients_sites[ $cls_site->client_id ][] = $cls_site->id; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$client_costs = Cost_Tracker_DB::get_instance()->get_all_cost_trackers_by_clients( array( $client_id ), array( 'get_cost_sites' => true ) ); |
| 113 |
|
| 114 |
if ( is_array( $client_costs ) ) { |
| 115 |
$client_costs = current( $client_costs ); // for current client. |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! is_array( $client_costs ) ) { |
| 119 |
$client_costs = array(); |
| 120 |
} |
| 121 |
|
| 122 |
?> |
| 123 |
<?php if ( empty( $client_costs ) ) : ?> |
| 124 |
<?php MainWP_UI::render_empty_element_placeholder( __( 'No Costs Added', 'mainwp' ), __( 'Add your costs to track upcoming payments.', 'mainwp' ), '<em data-emoji=":calendar_spiral:" class="medium"></em>' ); ?> |
| 125 |
<?php else : ?> |
| 126 |
<table class="ui table" id="mainwp-module-cost-tracker-costs-widget-table"> |
| 127 |
<thead> |
| 128 |
<tr> |
| 129 |
<th scope="col" ><?php esc_html_e( 'Product', 'mainwp' ); ?></th> <?php //phpcs:ignore -- to fix WordPress word. ?> |
| 130 |
<th scope="col" class="collapsing center aligned"><?php esc_html_e( 'Client Sites', 'mainwp' ); ?></th> |
| 131 |
<th scope="col" class="collapsing right aligned"><?php esc_html_e( 'Cost', 'mainwp' ); ?></th> |
| 132 |
<th scope="col" class="no-sort collapsing"></th> |
| 133 |
</tr> |
| 134 |
</thead> |
| 135 |
<tbody> |
| 136 |
<?php |
| 137 |
if ( ! empty( $client_costs ) ) { |
| 138 |
$columns = array( |
| 139 |
'name', |
| 140 |
'number_sites', |
| 141 |
'price', |
| 142 |
'actions', |
| 143 |
); |
| 144 |
foreach ( $client_costs as $cost ) { |
| 145 |
$item = $cost; |
| 146 |
?> |
| 147 |
<tr> |
| 148 |
<?php |
| 149 |
foreach ( $columns as $col ) { |
| 150 |
$row = $this->column_default( $item, $col, $client_id ); |
| 151 |
echo $row; // phpcs:ignore -- ok. |
| 152 |
} |
| 153 |
?> |
| 154 |
</tr> |
| 155 |
<?php |
| 156 |
} |
| 157 |
} |
| 158 |
?> |
| 159 |
</tbody> |
| 160 |
</table> |
| 161 |
<script type="text/javascript"> |
| 162 |
jQuery( document ).ready( function () { |
| 163 |
jQuery( '#mainwp-module-cost-tracker-costs-widget-table' ).DataTable( { |
| 164 |
"lengthMenu": [ [5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, "All"] ], |
| 165 |
"stateSave" : true, |
| 166 |
"info": false, |
| 167 |
"layout": { |
| 168 |
"bottom": 'paging', |
| 169 |
"bottomStart": null, |
| 170 |
"bottomEnd": null |
| 171 |
}, |
| 172 |
"order" : [ [1, 'asc'] ], |
| 173 |
"columnDefs": [ { |
| 174 |
"targets": 'no-sort', |
| 175 |
"orderable": false |
| 176 |
} ], |
| 177 |
"drawCallback": function () { |
| 178 |
mainwp_datatable_fix_menu_overflow('#mainwp-module-cost-tracker-costs-widget-table', -60, 5); |
| 179 |
jQuery( '#mainwp-module-cost-tracker-costs-widget-table .ui.dropdown' ).dropdown(); |
| 180 |
}, |
| 181 |
stateSaveParams: function (settings, data) { |
| 182 |
data._mwpv = mainwpParams.mainwpVersion || 'dev'; |
| 183 |
}, |
| 184 |
stateLoadParams: function (settings, data) { |
| 185 |
if ((mainwpParams.mainwpVersion || 'dev') !== data._mwpv) return false; |
| 186 |
}, |
| 187 |
search: { regex: false, smart: false }, |
| 188 |
orderMulti: false, |
| 189 |
searchDelay: 350 |
| 190 |
} ); |
| 191 |
// to prevent events conflict. |
| 192 |
setTimeout( function () { |
| 193 |
mainwp_datatable_fix_menu_overflow('#mainwp-module-cost-tracker-costs-widget-table', -60, 5); |
| 194 |
}, 1000 ); |
| 195 |
} ); |
| 196 |
</script> |
| 197 |
<?php endif; ?> |
| 198 |
<?php |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/** |
| 203 |
* Returns the column content for the provided item and column. |
| 204 |
* |
| 205 |
* @param array $item Record data. |
| 206 |
* @param string $column_name Column name. |
| 207 |
* @param int $client_id Client id. |
| 208 |
* |
| 209 |
* @return string $out Output. |
| 210 |
*/ |
| 211 |
public function column_default( $item, $column_name, $client_id ) { //phpcs:ignore -- NOSONAR - complex. |
| 212 |
$out = ''; |
| 213 |
|
| 214 |
$order_val = 0; |
| 215 |
switch ( $column_name ) { |
| 216 |
case 'name': |
| 217 |
$out = esc_html( $item->name ); |
| 218 |
break; |
| 219 |
case 'number_sites': // for client widget. |
| 220 |
$out = property_exists( $item, 'number_client_costs_sites' ) && is_array( $item->number_client_costs_sites ) ? count( $item->number_client_costs_sites ) : 0; |
| 221 |
break; |
| 222 |
case 'price': // for client widget. |
| 223 |
$array_costs = array( |
| 224 |
'weekly' => 0, |
| 225 |
'monthly' => 0, |
| 226 |
'quarterly' => 0, |
| 227 |
'yearly' => 0, |
| 228 |
'lifetime' => 0, |
| 229 |
); |
| 230 |
|
| 231 |
if ( 'active' === $item->cost_status && is_array( $item->cost_sites_ids ) ) { |
| 232 |
foreach ( $item->cost_sites_ids as $ct_siteid ) { |
| 233 |
// if site of cost tracker in the client's sites then calculate the site's cost/price. |
| 234 |
if ( is_array( $this->clients_sites[ $client_id ] ) && in_array( $ct_siteid, $this->clients_sites[ $client_id ] ) ) { |
| 235 |
$cost_val = 0; |
| 236 |
if ( 'single_site' === $item->license_type ) { |
| 237 |
$cost_val = $item->price; |
| 238 |
} elseif ( 'multi_site' === $item->license_type && ! empty( $item->count_sites ) ) { |
| 239 |
$cost_val = $item->price / $item->count_sites; |
| 240 |
} |
| 241 |
if ( 'lifetime' === $item->type ) { |
| 242 |
$array_costs['lifetime'] += $cost_val; |
| 243 |
} elseif ( isset( $array_costs[ $item->renewal_type ] ) ) { |
| 244 |
$array_costs[ $item->renewal_type ] += $cost_val; |
| 245 |
} |
| 246 |
$order_val += $cost_val; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
$out = Cost_Tracker_Utility::get_separated_costs_price( $array_costs ); |
| 251 |
break; |
| 252 |
case 'actions': |
| 253 |
ob_start(); |
| 254 |
?> |
| 255 |
<div class="ui right pointing dropdown not-auto-init" style="z-index: 99;"> |
| 256 |
<i class="ellipsis vertical icon"></i> |
| 257 |
<div class="menu"> |
| 258 |
<a class="item widget-row-cost-tracker-edit-cost" href="admin.php?page=CostTrackerAdd&id=<?php echo intval( $item->id ); ?>"><?php esc_html_e( 'Edit', 'mainwp' ); ?></a> |
| 259 |
</div> |
| 260 |
</div> |
| 261 |
<?php |
| 262 |
$out = ob_get_clean(); |
| 263 |
break; |
| 264 |
default: |
| 265 |
} |
| 266 |
|
| 267 |
if ( 'price' === $column_name ) { |
| 268 |
$out = '<td data-order="' . esc_attr( $order_val ) . '">' . $out . '</td>'; |
| 269 |
} else { |
| 270 |
$out = '<td>' . $out . '</td>'; |
| 271 |
} |
| 272 |
return $out; |
| 273 |
} |
| 274 |
} |
| 275 |
|