PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
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 1.7.3 1.7.4 All 35 releases
vikbooking / admin / models / shortcodes.php

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

195 lines 4.2 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 /**
111 * Premature filtering of shortcodes not assigned to any page.
112 *
113 * @since 1.8.6
114 */
115 $shortcodes = array_values(array_filter($shortcodes, function($shortcode)
116 {
117 return !empty($shortcode->post_id);
118 }));
119
120 // current language
121 $lang = is_null($lang) ? JFactory::getLanguage()->getTag() : $lang;
122
123 if (!count($shortcodes))
124 {
125 return null;
126 }
127
128 // loop through the shortcodes and look for the requested views
129 do
130 {
131 // the view to look for
132 $reqview = array_shift($views);
133
134 // highest score counter
135 $last_count = 0;
136
137 foreach ($shortcodes as $k => $v)
138 {
139 // start the score counter
140 $count = 0;
141
142 if (!strcasecmp($v->type, $reqview))
143 {
144 // view found, increment best score
145 $count++;
146
147 if ($lang == $v->lang) {
148 // same language, increment best score
149 $count++;
150 }
151 }
152
153 if ($count > $last_count)
154 {
155 // update counters
156 $last_count = $count;
157 $code_index = $k;
158 }
159 }
160
161 if ($last_count > 0)
162 {
163 // best view found, return this post_id
164 return $shortcodes[$code_index]->post_id;
165 }
166
167 } while (count($views));
168
169 // return the first post_id available in the shortcodes
170 return $shortcodes[0]->post_id;
171 }
172
173 /**
174 * Method to get a table object.
175 *
176 * @param string $name The table name.
177 * @param string $prefix The class prefix.
178 * @param array $options Configuration array for table.
179 *
180 * @return JTable A table object.
181 *
182 * @since 1.4.4
183 */
184 public function getTable($name = '', $prefix = 'JTable', $options = array())
185 {
186 if (!$name)
187 {
188 $name = 'shortcode';
189 $prefix = 'VBOTable';
190 }
191
192 return parent::getTable($name, $prefix, $options);
193 }
194 }
195