* @link https://www.fireplugins.com * @copyright Copyright © 2022 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core\Admin\Analytics; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } class Toolbar { /** * The default Toolbar Item * * @var string */ const default_toolbar_item = 'date'; /** * Configuration data * * @var array */ private $configuration; /** * Toolbar Items * * @var array */ private $settings; /** * Selected Toolbar Items * * @var array */ private $selectedToolbarItems; /** * Toolbar Items Classes Objects * * @var array */ private $toolbarItems; /** * Which toolbar item we currently edited * * @var string */ private $editing_toolbar_item; /** * DB * * @var DB */ private $db; /** * Toolbar Constructor * * @param array $toolbarItems * * @return void */ public function __construct($configuration = [], $db = null, $toolbarItems = null) { $this->configuration = $configuration; $this->db = $db; if (!$toolbarItems) { $toolbarItems = $this->initializeToolbarItems(); } $this->toolbarItems = $toolbarItems; $this->setUpAvailableToolbarItems(); } /** * Retrieves the currently selected Toolbar Items * * @return array */ public function setSelectedToolbarItems($selected_toolbar_items = []) { if (!$selected_toolbar_items) { $selected_toolbar_items = $this->getDefaultToolbarItems(); } // if the saved toolbar items are the ones we have already processed, then return them and do not do the process below again. if ($this->selectedToolbarItems == $selected_toolbar_items) { return $this->selectedToolbarItems; } // validate and set other toolbar items data foreach ($selected_toolbar_items as $key => &$value) { if (!$key) { continue; } if (empty($value)) { unset($selected_toolbar_items[$key]); continue; } // ensure only valid toolbar items are processed if (!isset($this->toolbarItems[$key])) { unset($selected_toolbar_items[$key]); continue; } $toolbarItem = $this->toolbarItems[$key]; // set popup title $value['popup_title'] = $toolbarItem->getPopupTitle(); $itemValue = isset($value['value']) ? $value['value'] : ''; // x axis labels $xAxisLabels = $toolbarItem->getXAxisLabels($value['method'], $value['options_key'], $itemValue); $value['xAxisLabel'] = $xAxisLabels['xAxisLabel']; $value['xAxisLabelPreviousPeriod'] = $xAxisLabels['xAxisLabelPreviousPeriod']; // unset toolbar item if no value is returned if (!$toolbarItemLabelValue = $toolbarItem->getToolbarItemLabelValue($value)) { unset($selected_toolbar_items[$key]); continue; } $value['title'] = $value['popup_title'] . ': ' . $toolbarItemLabelValue; } // add date if not exist $this->checkAndAddDateToolbarItem($selected_toolbar_items); $this->selectedToolbarItems = $selected_toolbar_items; } public function getSelectedToolbarItems() { return $this->selectedToolbarItems; } /** * Adds the date toolbar item at the front of the selected toolbar_items if it does not exist * * @param array $selected_toolbar_items * * @return void */ private function checkAndAddDateToolbarItem(&$selected_toolbar_items) { if (!isset($selected_toolbar_items[self::default_toolbar_item])) { $selected_toolbar_items = $this->toolbarItems[self::default_toolbar_item]->getDefaultToolbarItemData() + $selected_toolbar_items; } } /** * Default Selected Toolbar Items * * @return array */ private function getDefaultSelectedToolbarItems() { return [ 'toolbar_items' => $this->getDefaultToolbarItems(), 'editing_toolbar_item' => self::default_toolbar_item ]; } /** * Returns the default Toolbar Items * - Date - Filter : This Month * * @return array */ public function getDefaultToolbarItems() { return [ 'date' => [ 'method' => 'filter', 'options_key' => 'this_month', ] ]; } /** * Initialize all toolbar items * * @return array */ private function initializeToolbarItems() { // get all Toolbar Namespace Files if (!$files = \scandir(__DIR__ . '/Toolbar')) { return []; } $items = []; foreach ($files as $key => $file) { // ignore unimportant files if (in_array($file, ['index.php', '.', '..'])) { continue; } $file = basename($file, '.php'); // instantiate class $className = '\FireBox\Core\Admin\Analytics\Toolbar\\' . $file; if (!class_exists($className)) { continue; } $items[strtolower($file)] = new $className($this->db); } return $items; } /** * Set up each available toolbar item by giving the selected toolbar items to each toolbar item * * @return void */ public function setUpAvailableToolbarItems() { foreach ($this->toolbarItems as $key => $value) { $this->toolbarItems[$key]->setupVariables($this->configuration, $this->selectedToolbarItems); } } /** * Returns the Toolbar Items * * @return array */ public function getToolbarItems() { return $this->toolbarItems; } /** * Returns the toolbar settings * * @return array */ public function getSettings() { return $this->settings; } public function setSelectedToolbarItemsValue($value) { $this->selectedToolbarItems = $value; } public function setEditingToolbarItem($editing_toolbar_item) { $this->editing_toolbar_item = $editing_toolbar_item; } public function getConfiguration() { return $this->configuration; } }