| @@ -1,0 +1,131 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.toolbar | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Abstract base toolbar button. | |
| 16 | + * | |
| 17 | + * @since 10.0 | |
| 18 | + */ | |
| 19 | +abstract class JToolbarButtonBase | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * The name/type of the button. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + protected $_name; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * The layout id for the rendering of the button. | |
| 30 | + * | |
| 31 | + * @var string | |
| 32 | + */ | |
| 33 | + protected $_layoutId; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Class constructor. | |
| 37 | + * This method accepts an undefined number of arguments. | |
| 38 | + * | |
| 39 | + * @uses setup() | |
| 40 | + */ | |
| 41 | + public function __construct() | |
| 42 | + { | |
| 43 | + $args = func_get_args(); | |
| 44 | + | |
| 45 | + if (count($args)) | |
| 46 | + { | |
| 47 | + $this->_name = array_shift($args); | |
| 48 | + } | |
| 49 | + | |
| 50 | + call_user_func_array(array($this, 'setup'), $args); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Provides a new instance of the specified button. | |
| 55 | + * This method accepts an undefined number of arguments. | |
| 56 | + * | |
| 57 | + * @return self A new button instance. | |
| 58 | + */ | |
| 59 | + public static function getInstance() | |
| 60 | + { | |
| 61 | + $args = func_get_args(); | |
| 62 | + | |
| 63 | + if (!count($args)) | |
| 64 | + { | |
| 65 | + return null; | |
| 66 | + } | |
| 67 | + | |
| 68 | + // In case the first argument is an array it means that | |
| 69 | + // this method has been called directly. We need to replace | |
| 70 | + // the args list with the first element in the array. | |
| 71 | + if (is_array($args[0])) | |
| 72 | + { | |
| 73 | + $args = $args[0]; | |
| 74 | + } | |
| 75 | + | |
| 76 | + $name = strtolower($args[0]); | |
| 77 | + | |
| 78 | + if (!JLoader::import('adapter.toolbar.button.' . $name)) | |
| 79 | + { | |
| 80 | + return null; | |
| 81 | + } | |
| 82 | + | |
| 83 | + $classname = 'JToolbarButton' . ucwords($name); | |
| 84 | + | |
| 85 | + if (!class_exists($classname)) | |
| 86 | + { | |
| 87 | + return null; | |
| 88 | + } | |
| 89 | + | |
| 90 | + $reflect = new ReflectionClass($classname); | |
| 91 | + | |
| 92 | + $button = $reflect->newInstanceArgs($args); | |
| 93 | + | |
| 94 | + return ($button instanceof JToolbarButtonBase ? $button : null); | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Returns the name-identifier of the button. | |
| 99 | + * | |
| 100 | + * @return string The button name-id. | |
| 101 | + */ | |
| 102 | + public function getName() | |
| 103 | + { | |
| 104 | + return $this->_name; | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Returns the layout ID for the rendering of the button. | |
| 109 | + * | |
| 110 | + * @return string The layout ID. | |
| 111 | + */ | |
| 112 | + public function getLayoutId() | |
| 113 | + { | |
| 114 | + return $this->_layoutId; | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * Abstract method to setup the button. | |
| 119 | + * This method accepts an undefined number of arguments. | |
| 120 | + * | |
| 121 | + * @return void | |
| 122 | + */ | |
| 123 | + abstract protected function setup(); | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Returns an array containing the data to use for the button rendering. | |
| 127 | + * | |
| 128 | + * @return array Display data array. | |
| 129 | + */ | |
| 130 | + abstract public function getDisplayData(); | |
| 131 | +} | |