closing_periods_fixer.php
1 month ago
custom_fields_mapper.php
1 month ago
define_default_tax.php
1 month ago
generate_random_colors.php
1 month ago
index.html
1 month ago
invoice_settings_adapter.php
1 month ago
option_var_translations.php
1 month ago
payments_icons_upgrade.php
1 month ago
register_invoices_data.php
1 month ago
service_employee_overrides.php
1 month ago
status_codes_mapper.php
1 month ago
timestamp_datetime_converter.php
1 month ago
user_notes_migrator.php
1 month ago
working_days_adapter.php
1 month ago
custom_fields_mapper.php
151 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 | * Custom fields update adapter rule for 1.7 version. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | class VAPUpdateRuleCustomFieldsMapper1_7 extends VAPUpdateRule |
| 20 | { |
| 21 | /** |
| 22 | * Method run during update process. |
| 23 | * |
| 24 | * @param mixed $parent The parent that calls this method. |
| 25 | * |
| 26 | * @return boolean True on success, otherwise false to stop the flow. |
| 27 | */ |
| 28 | protected function run($parent) |
| 29 | { |
| 30 | $this->mapRules(); |
| 31 | $this->encodeSelectOptions(); |
| 32 | |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Since the rules are now extendable, we had to refactor the column used to identify the rules, by |
| 38 | * switching the integer ID into the related file name. |
| 39 | * |
| 40 | * During the installation of the update, we need to map the existing custom fields. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | private function mapRules() |
| 45 | { |
| 46 | $dbo = JFactory::getDbo(); |
| 47 | |
| 48 | // create lookup to assign the correct rule |
| 49 | $lookup = array( |
| 50 | 1 => 'nominative', |
| 51 | 2 => 'email', |
| 52 | 3 => 'phone', |
| 53 | 4 => 'state', |
| 54 | 5 => 'city', |
| 55 | 6 => 'address', |
| 56 | 7 => 'zip', |
| 57 | 8 => 'company', |
| 58 | 9 => 'vatnum', |
| 59 | ); |
| 60 | |
| 61 | // fetch all the custom fields |
| 62 | $q = $dbo->getQuery(true) |
| 63 | ->select($dbo->qn(array('id', 'rule'))) |
| 64 | ->from($dbo->qn('#__vikappointments_custfields')); |
| 65 | |
| 66 | $dbo->setQuery($q); |
| 67 | $dbo->execute(); |
| 68 | |
| 69 | if ($dbo->getNumRows()) |
| 70 | { |
| 71 | foreach ($dbo->loadObjectList() as $f) |
| 72 | { |
| 73 | // make sure the rule is supported |
| 74 | if (!isset($lookup[$f->rule])) |
| 75 | { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | // assign the new rule alias |
| 80 | $f->rule = $lookup[$f->rule]; |
| 81 | // finalise the update |
| 82 | $dbo->updateObject('#__vikappointments_custfields', $f, 'id'); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The options of a select are now encoded within a JSON string, so that we can track the relations |
| 89 | * between the options and their translations. For this reason, we must iterate all the existing custom |
| 90 | * fields, fetch the options, JSON-encode them and save the records. |
| 91 | * |
| 92 | * NOTE: the values of the custom fields stored within the database will lose the relation with the |
| 93 | * existing options. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | private function encodeSelectOptions() |
| 98 | { |
| 99 | $dbo = JFactory::getDbo(); |
| 100 | |
| 101 | $ids = array(); |
| 102 | |
| 103 | // fetch all the "select" custom fields |
| 104 | $q = $dbo->getQuery(true) |
| 105 | ->select($dbo->qn(array('id', 'choose'))) |
| 106 | ->from($dbo->qn('#__vikappointments_custfields')) |
| 107 | ->where($dbo->qn('type') . ' = ' . $dbo->q('select')); |
| 108 | |
| 109 | $dbo->setQuery($q); |
| 110 | $dbo->execute(); |
| 111 | |
| 112 | if ($dbo->getNumRows()) |
| 113 | { |
| 114 | foreach ($dbo->loadObjectList() as $f) |
| 115 | { |
| 116 | // JSON encode options |
| 117 | $f->choose = json_encode(explode(';;__;;', $f->choose)); |
| 118 | |
| 119 | // finalise the update |
| 120 | $dbo->updateObject('#__vikappointments_custfields', $f, 'id'); |
| 121 | |
| 122 | $ids[] = $f->id; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if ($ids) |
| 127 | { |
| 128 | // do the same for the translations of the custom fields |
| 129 | $q = $dbo->getQuery(true) |
| 130 | ->select($dbo->qn(array('id', 'choose'))) |
| 131 | ->from($dbo->qn('#__vikappointments_lang_customf')) |
| 132 | ->where($dbo->qn('id_customf') . ' IN (' . implode(',', $ids) . ')'); |
| 133 | |
| 134 | $dbo->setQuery($q); |
| 135 | $dbo->execute(); |
| 136 | |
| 137 | if ($dbo->getNumRows()) |
| 138 | { |
| 139 | foreach ($dbo->loadObjectList() as $f) |
| 140 | { |
| 141 | // JSON encode options |
| 142 | $f->choose = json_encode(explode(';;__;;', $f->choose)); |
| 143 | |
| 144 | // finalise the update |
| 145 | $dbo->updateObject('#__vikappointments_lang_customf', $f, 'id'); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 |