acl.php
1 day ago
license.php
1 day ago
override.php
1 day ago
overrides.php
1 day ago
shortcode.php
1 day ago
shortcodes.php
1 day ago
shortcodes.php
202 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 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 | JLoader::import('adapter.mvc.models.form'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments plugin Shortcodes model. |
| 18 | * |
| 19 | * @since 1.0 |
| 20 | * @see JModelForm |
| 21 | */ |
| 22 | class VikAppointmentsModelShortcodes extends JModelForm |
| 23 | { |
| 24 | /** |
| 25 | * Returns a list of shortcodes. |
| 26 | * |
| 27 | * @param mixed $columns Either a column or a list of columns to select. |
| 28 | * Use '*' to load all the columns (default). |
| 29 | * |
| 30 | * @return array An array of records. |
| 31 | */ |
| 32 | public function all($columns = '*') |
| 33 | { |
| 34 | $dbo = JFactory::getDbo(); |
| 35 | |
| 36 | if ($columns != '*') |
| 37 | { |
| 38 | if (is_string($columns)) |
| 39 | { |
| 40 | $columns = $dbo->qn($columns); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | $columns = implode(', ', array_map(array($dbo, 'qn'), $columns)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | $q = "SELECT {$columns} FROM `#__vikappointments_wpshortcodes` ORDER BY `id` ASC"; |
| 49 | |
| 50 | $dbo->setQuery($q); |
| 51 | $dbo->execute(); |
| 52 | |
| 53 | if ($dbo->getNumRows()) |
| 54 | { |
| 55 | return $dbo->loadObjectList(); |
| 56 | } |
| 57 | |
| 58 | return array(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Finds the best post_id for the passed view(s). |
| 63 | * Rather than using all() to get the first post_id, |
| 64 | * with this method we can get the exact post_id for |
| 65 | * the type of shortcode passed along with the views. |
| 66 | * |
| 67 | * @param mixed $views string or array of strings |
| 68 | * @param string $lang the language attribute to look for |
| 69 | * |
| 70 | * @return mixed integer for the post_id, null otherwise |
| 71 | * |
| 72 | * @uses all() |
| 73 | * @since 1.0.14 |
| 74 | */ |
| 75 | public function best($views, $lang = null) |
| 76 | { |
| 77 | if (empty($views)) |
| 78 | { |
| 79 | /** |
| 80 | * Use the default services view to properly display the output |
| 81 | * generated by controller tasks. |
| 82 | * |
| 83 | * @since 1.2.14 |
| 84 | */ |
| 85 | $views = 'serviceslist'; |
| 86 | } |
| 87 | |
| 88 | if (is_scalar($views)) |
| 89 | { |
| 90 | // always convert the views to look for into an array |
| 91 | $views = array($views); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Attempt to take the "All Orders" view in case the system doesn't use |
| 96 | * a shortcode for the "Order" view. |
| 97 | * |
| 98 | * @since 1.2.18 |
| 99 | */ |
| 100 | if (in_array('order', $views)) |
| 101 | { |
| 102 | $views[] = 'allorders'; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Properly route the packages order as this view doesn't own a XML file. |
| 107 | * |
| 108 | * @since 1.2.12 |
| 109 | */ |
| 110 | if (in_array('packagesorder', $views)) |
| 111 | { |
| 112 | $views[] = 'packages'; |
| 113 | } |
| 114 | |
| 115 | // get all shortcodes of any type |
| 116 | $shortcodes = $this->all(); |
| 117 | |
| 118 | /** |
| 119 | * Exclude all the shortcodes that haven't been assigned to a WordPress page. |
| 120 | * |
| 121 | * @since 1.2.18 |
| 122 | */ |
| 123 | $shortcodes = array_values(array_filter($shortcodes, function($s) { |
| 124 | return $s->post_id > 0; |
| 125 | })); |
| 126 | |
| 127 | if (!count($shortcodes)) |
| 128 | { |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | // current language |
| 133 | $lang = is_null($lang) ? JFactory::getLanguage()->getTag() : $lang; |
| 134 | |
| 135 | // loop through the shortcodes and look for the requested views |
| 136 | do |
| 137 | { |
| 138 | // the view to look for |
| 139 | $reqview = array_shift($views); |
| 140 | |
| 141 | // highest score counter |
| 142 | $last_count = 0; |
| 143 | |
| 144 | foreach ($shortcodes as $k => $v) |
| 145 | { |
| 146 | // start the score counter |
| 147 | $count = 0; |
| 148 | |
| 149 | if (!strcasecmp($v->type, $reqview)) |
| 150 | { |
| 151 | // view found, increment best score |
| 152 | $count++; |
| 153 | |
| 154 | if ($lang == $v->lang) { |
| 155 | // same language, increment best score |
| 156 | $count++; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if ($count > $last_count) |
| 161 | { |
| 162 | // update counters |
| 163 | $last_count = $count; |
| 164 | $code_index = $k; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if ($last_count > 0) |
| 169 | { |
| 170 | // best view found, return this post_id |
| 171 | return $shortcodes[$code_index]->post_id; |
| 172 | } |
| 173 | |
| 174 | } while (count($views)); |
| 175 | |
| 176 | // return the first post_id available in the shortcodes |
| 177 | return $shortcodes[0]->post_id; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Method to get a table object. |
| 182 | * |
| 183 | * @param string $name The table name. |
| 184 | * @param string $prefix The class prefix. |
| 185 | * @param array $options Configuration array for table. |
| 186 | * |
| 187 | * @return JTable A table object. |
| 188 | * |
| 189 | * @since 1.2 |
| 190 | */ |
| 191 | public function getTable($name = '', $prefix = 'JTable', $options = array()) |
| 192 | { |
| 193 | if (!$name) |
| 194 | { |
| 195 | $name = 'shortcode'; |
| 196 | $prefix = 'VAPTable'; |
| 197 | } |
| 198 | |
| 199 | return parent::getTable($name, $prefix, $options); |
| 200 | } |
| 201 | } |
| 202 |