PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.7
VikBooking Hotel Booking Engine & PMS v1.6.7
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / models / shortcodes.php

shortcodes.php in VikBooking Hotel Booking Engine & PMS 1.6.7, at admin/models/shortcodes.php

191 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author E4J srl
6 * @copyright Copyright (C) e4j - Extensionsforjoomla.com. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
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 * VikBooking plugin Shortcodes model.
18 *
19 * @since 1.0
20 * @see JModelForm
21 */
22 class VikBookingModelShortcodes extends JModelForm
23 {
24 /**
25 * Grabs all shortcode records and custom related information.
26 *
27 * @param string $columns optional list of columns to fetch.
28 * @param bool $full whether the column to fetch should not be empty.
29 *
30 * @return array list of records fetched, if any.
31 *
32 * @since 1.5.10 added second argument $full to filter empty post_id records.
33 */
34 public function all($columns = '*', $full = false)
35 {
36 $dbo = JFactory::getDbo();
37
38 $records = [];
39 $fetch = $columns;
40
41 if ($columns != '*')
42 {
43 if (is_string($columns))
44 {
45 $columns = $dbo->qn($columns);
46 }
47 else
48 {
49 $columns = implode(', ', array_map(array($dbo, 'qn'), $columns));
50 }
51 }
52
53 $q = "SELECT {$columns} FROM `#__vikbooking_wpshortcodes` ORDER BY `id` ASC";
54
55 $dbo->setQuery($q);
56 $dbo->execute();
57
58 if (!$dbo->getNumRows())
59 {
60 return [];
61 }
62
63 $records = $dbo->loadObjectList();
64
65 if ($full && is_string($fetch) && $fetch != '*' && strpos($fetch, ',') === false)
66 {
67 // filter the records by non-empty column
68 $records = array_filter($records, function($record) use ($fetch)
69 {
70 return isset($record->{$fetch}) && !empty($record->{$fetch});
71 });
72
73 // make sure to reset the array keys
74 $records = array_values($records);
75 }
76
77 return $records;
78 }
79
80 /**
81 * Finds the best post_id for the passed view(s).
82 * Rather than using all() to get the first post_id,
83 * with this method we can get the exact post_id for
84 * the type of shortcode passed along with the views.
85 *
86 * @param mixed $views string or array of strings
87 * @param string $lang the language attribute to look for
88 *
89 * @return mixed integer for the post_id, null otherwise
90 *
91 * @uses all()
92 * @since 1.0.14
93 */
94 public function best($views, $lang = null)
95 {
96 if (empty($views))
97 {
98 return null;
99 }
100
101 if (is_scalar($views))
102 {
103 // always convert the views to look for into an array
104 $views = array($views);
105 }
106
107 // get all shortcodes of any type
108 $shortcodes = $this->all();
109
110 // current language
111 $lang = is_null($lang) ? JFactory::getLanguage()->getTag() : $lang;
112
113 if (!count($shortcodes))
114 {
115 return null;
116 }
117
118 // loop through the shortcodes and look for the requested views
119 do
120 {
121 // the view to look for
122 $reqview = array_shift($views);
123
124 // highest score counter
125 $last_count = 0;
126
127 foreach ($shortcodes as $k => $v)
128 {
129 // ignore shortcodes not linked to a valid page
130 if (empty($v->post_id))
131 {
132 continue;
133 }
134
135 // start the score counter
136 $count = 0;
137
138 if (!strcasecmp($v->type, $reqview))
139 {
140 // view found, increment best score
141 $count++;
142
143 if ($lang == $v->lang) {
144 // same language, increment best score
145 $count++;
146 }
147 }
148
149 if ($count > $last_count)
150 {
151 // update counters
152 $last_count = $count;
153 $code_index = $k;
154 }
155 }
156
157 if ($last_count > 0)
158 {
159 // best view found, return this post_id
160 return $shortcodes[$code_index]->post_id;
161 }
162
163 } while (count($views));
164
165 // return the first post_id available in the shortcodes
166 return $shortcodes[0]->post_id;
167 }
168
169 /**
170 * Method to get a table object.
171 *
172 * @param string $name The table name.
173 * @param string $prefix The class prefix.
174 * @param array $options Configuration array for table.
175 *
176 * @return JTable A table object.
177 *
178 * @since 1.4.4
179 */
180 public function getTable($name = '', $prefix = 'JTable', $options = array())
181 {
182 if (!$name)
183 {
184 $name = 'shortcode';
185 $prefix = 'VBOTable';
186 }
187
188 return parent::getTable($name, $prefix, $options);
189 }
190 }
191