helper.php
186 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments - Libraries |
| 4 | * @subpackage layout |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 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 | * Factory class to render common parts of the plugin. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | abstract class VikAppointmentsLayoutHelper |
| 20 | { |
| 21 | /** |
| 22 | * Renders the system messages. |
| 23 | * |
| 24 | * @param array $queue The messages queue. |
| 25 | * @param boolean $echo True to echo the layout, false to return it. |
| 26 | * |
| 27 | * @return mixed True when the layout is echoed, otherwise the layout string. |
| 28 | */ |
| 29 | public static function renderSystemMessages(?array $queue = null, $echo = true) |
| 30 | { |
| 31 | $app = JFactory::getApplication(); |
| 32 | |
| 33 | if (is_null($queue)) |
| 34 | { |
| 35 | // do not flush the messages queue if the |
| 36 | // application is going to do a JS redirect |
| 37 | if ($app->shouldRedirect()) |
| 38 | { |
| 39 | $queue = array(); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | $queue = $app->getMessagesQueue(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get setting from config to evaluate whether the messages should be grouped. |
| 49 | * This setting is not installed by default and can be added on a second time to |
| 50 | * turn off messages grouping. |
| 51 | * |
| 52 | * Here's the query to turn off this feature: |
| 53 | * INSERT INTO `#__vikappointments_config` (`param`, `setting`) VALUES |
| 54 | * ('groupsysmessages', 0) |
| 55 | */ |
| 56 | $should_be_grouped = VAPFactory::getConfig()->getBool('groupsysmessages', true); |
| 57 | |
| 58 | if ($should_be_grouped) |
| 59 | { |
| 60 | // manipulate queue to group contiguous messages that share the same type |
| 61 | $tmp = array(); |
| 62 | |
| 63 | for ($i = 0; $i < count($queue); $i++) |
| 64 | { |
| 65 | // make sure we have a valid message object |
| 66 | if (isset($queue[$i]->type)) |
| 67 | { |
| 68 | // extract last message from temporary list |
| 69 | $last = end($tmp); |
| 70 | |
| 71 | // in case the list is empty or the type of the current |
| 72 | // message doesn't match the previous one, push it |
| 73 | // within the temporary list as new element |
| 74 | if (empty($tmp) || $last->type != $queue[$i]->type) |
| 75 | { |
| 76 | $tmp[] = $queue[$i]; |
| 77 | } |
| 78 | // otherwise append this message to the previous one |
| 79 | else |
| 80 | { |
| 81 | $last->message = (array) $last->message; |
| 82 | $last->message[] = $queue[$i]->message; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // overwrite standard queue |
| 88 | $queue = $tmp; |
| 89 | } |
| 90 | |
| 91 | $layout = new JLayoutFile('html.system.messages', null, array('component' => 'com_vikappointments')); |
| 92 | $output = $layout->render(array('queue' => $queue)); |
| 93 | |
| 94 | if ($echo) |
| 95 | { |
| 96 | echo $output; |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return $output; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Renders the plugin toolbar. |
| 105 | * |
| 106 | * @param JToolbar $bar The toolbar to render. |
| 107 | * @param boolean $echo True to echo the layout, false to return it. |
| 108 | * |
| 109 | * @return mixed True when the layout is echoed, otherwise the layout string. |
| 110 | */ |
| 111 | public static function renderToolbar($bar = null, $echo = true) |
| 112 | { |
| 113 | if (is_null($bar) || !$bar instanceof JToolbar) |
| 114 | { |
| 115 | $bar = JToolbar::getInstance(); |
| 116 | } |
| 117 | |
| 118 | $output = ''; |
| 119 | |
| 120 | // render toolbar only if it contains at least a button or the title is set |
| 121 | if ($bar->hasButtons() || $bar->hasTitle()) |
| 122 | { |
| 123 | // open toolbar |
| 124 | $layout = new JLayoutFile('html.toolbar.open', null, array('component' => 'com_vikappointments')); |
| 125 | $output .= $layout->render(array('bar' => $bar)); |
| 126 | |
| 127 | // render contents |
| 128 | foreach ($bar->getButtons() as $button) |
| 129 | { |
| 130 | $layoutId = $button->getLayoutId(); |
| 131 | |
| 132 | if (!is_array($layoutId)) |
| 133 | { |
| 134 | $layoutId = array($layoutId); |
| 135 | } |
| 136 | |
| 137 | // specify base path (null) |
| 138 | $layoutId[] = null; |
| 139 | // force component |
| 140 | $layoutId[] = array('component' => 'com_vikappointments'); |
| 141 | |
| 142 | // use reflection to support multiple arguments |
| 143 | $reflect = new ReflectionClass('JLayoutFile'); |
| 144 | |
| 145 | // [0] layout id, [1] base path, [2] options |
| 146 | $layout = $reflect->newInstanceArgs($layoutId); |
| 147 | $output .= $layout->render($button->getDisplayData()); |
| 148 | } |
| 149 | |
| 150 | // close toolbar |
| 151 | $layout = new JLayoutFile('html.toolbar.close', null, array('component' => 'com_vikappointments')); |
| 152 | $output .= $layout->render(); |
| 153 | } |
| 154 | |
| 155 | if ($echo) |
| 156 | { |
| 157 | echo $output; |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | return $output; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Renders the current body page. |
| 166 | * |
| 167 | * @param string $html The HTML to print. |
| 168 | * @param boolean $echo True to echo the layout, false to return it. |
| 169 | * |
| 170 | * @return mixed True when the layout is echoed, otherwise the layout string. |
| 171 | */ |
| 172 | public static function renderBody($html, $echo = true) |
| 173 | { |
| 174 | $layout = new JLayoutFile('html.system.body', null, array('component' => 'com_vikappointments')); |
| 175 | $output = $layout->render(array('html' => $html)); |
| 176 | |
| 177 | if ($echo) |
| 178 | { |
| 179 | echo $output; |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | return $output; |
| 184 | } |
| 185 | } |
| 186 |