router
2 days ago
factory.php
2 days ago
helper.php
2 days ago
index.html
2 days ago
router.php
2 days ago
helper.php
180 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 | /** |
| 15 | * Helper class used to manipulate the alias of the records. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | class VAPSefHelper |
| 20 | { |
| 21 | /** |
| 22 | * Method used to make URL-safe any strings. |
| 23 | * |
| 24 | * @param string $src The string to convert. |
| 25 | * |
| 26 | * @return string The safe string. |
| 27 | */ |
| 28 | public static function stringToAlias($src) |
| 29 | { |
| 30 | return JFilterOutput::stringURLSafe($src); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns a valid and unique alias for SEO usages. |
| 35 | * |
| 36 | * @param string $src The source name. |
| 37 | * @param string $type The type name. |
| 38 | * @param integer $id The ID of record to exclude while serching other aliases. |
| 39 | * @param mixed $parent An optional parent. |
| 40 | * |
| 41 | * @return string The resulting alias. |
| 42 | */ |
| 43 | public static function getUniqueAlias($src, $type = '', $id = null, $parent = null) |
| 44 | { |
| 45 | $src = static::stringToAlias($src); |
| 46 | |
| 47 | $alias = $src; |
| 48 | $cont = 1; |
| 49 | |
| 50 | do |
| 51 | { |
| 52 | if ($cont > 1) |
| 53 | { |
| 54 | $alias = $src . "-" . $cont; |
| 55 | } |
| 56 | |
| 57 | $cont++; |
| 58 | |
| 59 | // check if the fetched alias is already assigned |
| 60 | // to a different record (repeat again in that case) |
| 61 | $_id = static::getRecordWithAlias($alias, $type, null, $parent); |
| 62 | } while ($_id > 0 && $_id != $id); |
| 63 | |
| 64 | return $alias; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the record that owns the specified alias. |
| 69 | * |
| 70 | * @param string $alias The alias to find. |
| 71 | * @param string $type The type name. |
| 72 | * @param mixed $lang The language tag. |
| 73 | * @param mixed $parent An optional parent. |
| 74 | * |
| 75 | * @return mixed The ID of the record found on success, null otherwise. |
| 76 | */ |
| 77 | public static function getRecordWithAlias($alias, $type, $lang = null, $parent = null) |
| 78 | { |
| 79 | $dbo = JFactory::getDbo(); |
| 80 | |
| 81 | $translator = VAPFactory::getTranslator(); |
| 82 | |
| 83 | // obtain translation table |
| 84 | $table = $translator->getTable($type); |
| 85 | |
| 86 | // search for a translation first |
| 87 | $q = $dbo->getQuery(true) |
| 88 | ->select($dbo->qn('l.' . $table->getForeignKey())) |
| 89 | ->from($dbo->qn($table->getTableName(), 'l')) |
| 90 | ->where($dbo->qn('l.alias') . ' = ' . $dbo->q($alias)); |
| 91 | |
| 92 | if ($lang) |
| 93 | { |
| 94 | // filter by language only if provided |
| 95 | $q->where($dbo->qn('l.' . $table->getLangColumn()) . ' = ' . $dbo->q($lang)); |
| 96 | } |
| 97 | |
| 98 | if ($parent) |
| 99 | { |
| 100 | // get parent table |
| 101 | $parentTable = $table->getParentTable(); |
| 102 | |
| 103 | // make sure the table supports a parent |
| 104 | if ($parentTable) |
| 105 | { |
| 106 | // get table instance |
| 107 | $parentTable = $translator->getTable($parentTable); |
| 108 | |
| 109 | // join query to parent |
| 110 | $q->leftjoin($dbo->qn($parentTable->getTableName(), 'p') . ' ON ' . |
| 111 | $dbo->qn('p.' . $parentTable->getPrimaryKey()) . ' = ' . $dbo->qn('l.' . $table->getParentKey())); |
| 112 | |
| 113 | // filter by parent ID |
| 114 | $q->where($dbo->qn('p.' . $parentTable->getForeignKey()) . ' = ' . (int) $parent); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | $dbo->setQuery($q, 0, 1); |
| 119 | |
| 120 | if ($tmp = $dbo->loadResult()) |
| 121 | { |
| 122 | // return only the record ID |
| 123 | return (int) $tmp; |
| 124 | } |
| 125 | |
| 126 | // translation not found, try with the original table |
| 127 | $q = $dbo->getQuery(true) |
| 128 | ->select($dbo->qn($table->getLinkedPrimaryKey())) |
| 129 | ->from($dbo->qn($table->getLinkedTable())) |
| 130 | ->where($dbo->qn('alias') . ' = ' . $dbo->q($alias)); |
| 131 | |
| 132 | if ($parent) |
| 133 | { |
| 134 | // get parent column |
| 135 | $parentCol = $table->getLinkedParentKey(); |
| 136 | |
| 137 | // make sure the table supports a parent column |
| 138 | if ($parentCol) |
| 139 | { |
| 140 | $q->where($dbo->qn($parentCol) . ' = ' . (int) $parent); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | $dbo->setQuery($q, 0, 1); |
| 145 | |
| 146 | if ($tmp = $dbo->loadResult()) |
| 147 | { |
| 148 | // return only the record ID |
| 149 | return (int) $tmp; |
| 150 | } |
| 151 | |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the alias used by the specified record. |
| 157 | * |
| 158 | * @param integer $id The ID of the record. |
| 159 | * @param string $type The type name. |
| 160 | * @param mixed $lang The current language for translated aliases. |
| 161 | * |
| 162 | * @return mixed The alias of the record found, null otherwise. |
| 163 | */ |
| 164 | public static function getRecordAlias($id, $type, $lang = null) |
| 165 | { |
| 166 | $translator = VAPFactory::getTranslator(); |
| 167 | |
| 168 | // translate specified record |
| 169 | $tx = $translator->translate($type, $id, $lang); |
| 170 | |
| 171 | if ($tx) |
| 172 | { |
| 173 | // record found, return correct alias |
| 174 | return $tx->alias; |
| 175 | } |
| 176 | |
| 177 | return null; |
| 178 | } |
| 179 | } |
| 180 |