shortcodes.php
273 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments - Libraries |
| 4 | * @subpackage wizard |
| 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 | * Implement the wizard step used to setup the shortcodes |
| 16 | * to display VikAppointments within the front-end. |
| 17 | * |
| 18 | * @since 1.2.3 |
| 19 | */ |
| 20 | class VAPWizardStepShortcodes extends VAPWizardStep |
| 21 | { |
| 22 | /** |
| 23 | * Returns the step title. |
| 24 | * Used as a very-short description. |
| 25 | * |
| 26 | * @return string The step title. |
| 27 | */ |
| 28 | public function getTitle() |
| 29 | { |
| 30 | return __('Shortcodes', 'vikappointments'); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns the step description. |
| 35 | * |
| 36 | * @return string The step description. |
| 37 | */ |
| 38 | public function getDescription() |
| 39 | { |
| 40 | return __('<p>The shortcodes are used to display the plugin within the front-end. You need to create at least a shortcode, which should then be included within an apposite post/page.</p><p>After creating a shortcode, go back to the list and click the <i class="fas fa-plus-square"></i> button, under the <b>Post</b> column, to automatically create a page. The created page will include the shortcode previously generated.</p>', 'vikappointments'); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns an optional step icon. |
| 45 | * |
| 46 | * @return string The step icon. |
| 47 | */ |
| 48 | public function getIcon() |
| 49 | { |
| 50 | return '<i class="fas fa-quote-left"></i>'; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Return the group to which the step belongs. |
| 55 | * |
| 56 | * @return string The group name. |
| 57 | */ |
| 58 | public function getGroup() |
| 59 | { |
| 60 | // belongs to GLOBAL group |
| 61 | return JText::translate('VAPMENUTITLEHEADER3'); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the completion progress in percentage. |
| 66 | * |
| 67 | * @return integer The percentage progress (always rounded). |
| 68 | */ |
| 69 | public function getProgress() |
| 70 | { |
| 71 | $progress = []; |
| 72 | |
| 73 | foreach (['appointments', 'packages', 'subscriptions'] as $group) |
| 74 | { |
| 75 | // make sure the group is configurable |
| 76 | if ($this->groupExists($group)) |
| 77 | { |
| 78 | // check whether the shortcode should be created |
| 79 | if ($this->needShortcode($group)) |
| 80 | { |
| 81 | // not yet created |
| 82 | $progress[] = 0; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | // already created |
| 87 | $progress[] = 100; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // calculate the progress average |
| 93 | $avg = $progress ? array_sum($progress) / count($progress) : 100; |
| 94 | |
| 95 | // ignore decimals |
| 96 | return round($avg, 0); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Checks whether the step has been completed. |
| 101 | * |
| 102 | * @return boolean True if completed, false otherwise. |
| 103 | */ |
| 104 | public function isCompleted() |
| 105 | { |
| 106 | // look for 100% completion progress |
| 107 | return $this->getProgress() == 100; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the button used to process the step. |
| 112 | * |
| 113 | * @return string The HTML of the button. |
| 114 | */ |
| 115 | public function getExecuteButton() |
| 116 | { |
| 117 | // point to the controller to create a new shortcode |
| 118 | return '<a href="admin.php?page=vikappointments&task=shortcodes.add" class="btn btn-success">' . JText::translate('VAPNEW') . '</a>'; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the HTML to display description and actions |
| 123 | * needed to complete the step. |
| 124 | * |
| 125 | * @return string The HTML of the step. |
| 126 | */ |
| 127 | public function display() |
| 128 | { |
| 129 | // always try to search for a layout related to this step |
| 130 | return JLayoutHelper::render('html.wizard.shortcodes', array('step' => $this)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks whether the specified step can be skipped. |
| 135 | * By default, all the steps are mandatory. |
| 136 | * |
| 137 | * @return boolean True if skippable, false otherwise. |
| 138 | */ |
| 139 | public function canIgnore() |
| 140 | { |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns a list of created shortcodes. |
| 146 | * |
| 147 | * @return array A list of shortcodes. |
| 148 | */ |
| 149 | public function getShortcodes() |
| 150 | { |
| 151 | static $shortcodes = null; |
| 152 | |
| 153 | // get shortcodes only once |
| 154 | if (is_null($shortcodes)) |
| 155 | { |
| 156 | $dbo = JFactory::getDbo(); |
| 157 | |
| 158 | $q = $dbo->getQuery(true) |
| 159 | ->select($dbo->qn(array('type', 'title', 'name'))) |
| 160 | ->from($dbo->qn('#__vikappointments_wpshortcodes')) |
| 161 | ->order($dbo->qn('id') . ' ASC'); |
| 162 | |
| 163 | $dbo->setQuery($q); |
| 164 | $dbo->execute(); |
| 165 | |
| 166 | if ($dbo->getNumRows()) |
| 167 | { |
| 168 | $shortcodes = $dbo->loadObjectList(); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | $shortcodes = array(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return $shortcodes; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Checks whether the specified group should be considered |
| 181 | * while fetching the overall progress. |
| 182 | * |
| 183 | * @param string $group The group to look for. |
| 184 | * |
| 185 | * @return boolean True if observable, false otherwise. |
| 186 | */ |
| 187 | public function groupExists($group) |
| 188 | { |
| 189 | if ($group === 'appointments') |
| 190 | { |
| 191 | $exists = true; |
| 192 | } |
| 193 | else if ($group === 'packages') |
| 194 | { |
| 195 | // get packages step dependency |
| 196 | $section = $this->getDependency('syspack'); |
| 197 | |
| 198 | // the step must exists and should NOT be ignored |
| 199 | $exists = $section && !$section->isIgnored(); |
| 200 | } |
| 201 | else if ($group === 'subscriptions') |
| 202 | { |
| 203 | // get subscriptions step dependency |
| 204 | $section = $this->getDependency('syssubscr'); |
| 205 | |
| 206 | // the step must exists and should NOT be ignored |
| 207 | $exists = $section && !$section->isIgnored(); |
| 208 | } |
| 209 | |
| 210 | return $exists; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Checks whether the specified group needs the |
| 215 | * creation of a shortcode. |
| 216 | * |
| 217 | * @param string $group The group to look for. |
| 218 | * |
| 219 | * @return boolean True if a shortcode is needed, false otherwise. |
| 220 | */ |
| 221 | public function needShortcode($group) |
| 222 | { |
| 223 | // the step is completed after creating at least a shortcode |
| 224 | // for each active section |
| 225 | $types = array_map(function($shortcode) |
| 226 | { |
| 227 | return $shortcode->type; |
| 228 | }, $this->getShortcodes()); |
| 229 | |
| 230 | // check if the group is enabled |
| 231 | if ($group == 'appointments') |
| 232 | { |
| 233 | // appointments always enabled |
| 234 | $enabled = true; |
| 235 | |
| 236 | // define list of supported views |
| 237 | $lookup = ['serviceslist', 'employeeslist', 'servicesearch', 'employeesearch']; |
| 238 | } |
| 239 | else if ($group == 'packages') |
| 240 | { |
| 241 | // get packages step dependency |
| 242 | $section = $this->getDependency('syspack'); |
| 243 | // Check whether the packages section is enabled. |
| 244 | // Always consider has enabled in case the step was not yet completed. |
| 245 | $enabled = $section && ($section->isEnabled() || !$section->isCompleted()); |
| 246 | |
| 247 | // define list of supported views |
| 248 | $lookup = ['packages']; |
| 249 | } |
| 250 | else if ($group == 'subscriptions') |
| 251 | { |
| 252 | // get subscriptions step dependency |
| 253 | $section = $this->getDependency('syssubscr'); |
| 254 | // Check whether the subscriptions section is enabled. |
| 255 | // Always consider has enabled in case the step was not yet completed. |
| 256 | $enabled = $section && ($section->isEnabled() || !$section->isCompleted()); |
| 257 | |
| 258 | // define list of supported views |
| 259 | $lookup = ['subscriptions']; |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | // unknown group, not allowed |
| 264 | $enabled = false; |
| 265 | } |
| 266 | |
| 267 | // in case the group is active, check whether the list |
| 268 | // of created types intersects at least one element of |
| 269 | // the fetched lookup |
| 270 | return $enabled && !array_intersect($types, $lookup); |
| 271 | } |
| 272 | } |
| 273 |