-
Notifications
You must be signed in to change notification settings - Fork 45
Issue#242165 Feat: Development of Prefix and Postfix to input group #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: j4x
Are you sure you want to change the base?
Changes from all commits
69a3357
e0d592b
7f099aa
37de023
2132437
913f945
ab9ffd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
| /** | ||
| * @package TJ-Fields | ||
| * | ||
| * @author Techjoomla <extensions@techjoomla.com> | ||
| * @copyright Copyright (c) 2009-2021 TechJoomla. All rights reserved. | ||
| * @license GNU General Public License version 2 or later. | ||
| */ | ||
|
|
||
| // No direct access | ||
| defined('_JEXEC') or die('Restricted access'); | ||
|
|
||
| use Joomla\CMS\Table\Table; | ||
|
|
||
| /** | ||
| * Migration file for TJ-Fields | ||
| * | ||
| * @since 1.4.6 | ||
| */ | ||
| class TjHouseKeepingTjVendorsColumn extends TjModelHouseKeeping | ||
| { | ||
| public $title = "Country, Region, and City table fix for com_tjvendors"; | ||
|
|
||
| public $description = "Add com_tjvendors column in Country, Region, and City table"; | ||
|
|
||
| /** | ||
| * Add com_tjvendors column in Country, Region, and City table if not exists | ||
| * | ||
| * @return void | ||
| * | ||
| * @since 1.0 | ||
| */ | ||
| public function migrate() | ||
| { | ||
| $result = array(); | ||
|
|
||
| try | ||
| { | ||
| $db = JFactory::getDbo(); | ||
| $query = $db->getQuery(true); | ||
| $query = "SHOW COLUMNS FROM `#__tj_city`"; | ||
| $db->setQuery($query); | ||
| $columns = $db->loadAssoclist(); | ||
|
|
||
| $columns = array_column($columns, "Field"); | ||
|
|
||
| if (!in_array('com_tjvendors', $columns)) | ||
| { | ||
| $query = "ALTER TABLE `#__tj_city` ADD COLUMN `com_tjvendors` tinyint(1) NOT NULL DEFAULT '1'"; | ||
| $db->setQuery($query); | ||
|
|
||
| if (!$db->execute()) | ||
| { | ||
| $result['status'] = false; | ||
| $result['message'] = $db->getErrorMsg(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Replace deprecated getErrorMsg() method.
- $result['message'] = $db->getErrorMsg();
+ $result['message'] = 'Database error: ' . $db->getErrorNum() . ' - ' . $db->getErrorMsg();Alternatively, consider letting database exceptions bubble up and handling them in the catch block. Also applies to: 76-76, 97-97 🤖 Prompt for AI Agents |
||
|
|
||
| return $result; | ||
| } | ||
| } | ||
|
|
||
| $query = $db->getQuery(true); | ||
| $query = "SHOW COLUMNS FROM `#__tj_region`"; | ||
| $db->setQuery($query); | ||
| $columns = $db->loadobjectlist(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix inconsistent method calls.
- $columns = $db->loadobjectlist();
+ $columns = $db->loadObjectList();Apply the same fix on line 85: - $columns = $db->loadobjectlist();
+ $columns = $db->loadObjectList();Also applies to: 85-85 🤖 Prompt for AI Agents |
||
|
|
||
| $columns = array_column($columns, "Field"); | ||
|
|
||
| if (!in_array('com_tjvendors', $columns)) | ||
| { | ||
| $query = "ALTER TABLE `#__tj_region` ADD COLUMN `com_tjvendors` tinyint(1) NOT NULL DEFAULT '1'"; | ||
| $db->setQuery($query); | ||
|
|
||
| if (!$db->execute()) | ||
| { | ||
| $result['status'] = false; | ||
| $result['message'] = $db->getErrorMsg(); | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
|
|
||
| $query = $db->getQuery(true); | ||
| $query = "SHOW COLUMNS FROM `#__tj_country`"; | ||
| $db->setQuery($query); | ||
| $columns = $db->loadobjectlist(); | ||
|
|
||
| $columns = array_column($columns, "Field"); | ||
|
|
||
| if (!in_array('com_tjvendors', $columns)) | ||
| { | ||
| $query = "ALTER TABLE `#__tj_country` ADD COLUMN `com_tjvendors` tinyint(1) NOT NULL DEFAULT '1'"; | ||
| $db->setQuery($query); | ||
|
|
||
| if (!$db->execute()) | ||
| { | ||
| $result['status'] = false; | ||
| $result['message'] = $db->getErrorMsg(); | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Reduce code duplication with helper method. The same logic is repeated three times for different tables. Extract this into a reusable method to follow DRY principles. + /**
+ * Add com_tjvendors column to a table if it doesn't exist
+ *
+ * @param string $tableName The table name
+ * @param object $db Database object
+ *
+ * @return array Result array with status and message
+ *
+ * @since 1.4.6
+ */
+ private function addColumnToTable($tableName, $db)
+ {
+ $query = "SHOW COLUMNS FROM `#__tj_{$tableName}`";
+ $db->setQuery($query);
+ $columns = $db->loadObjectList();
+ $columns = array_column($columns, "Field");
+
+ if (!in_array('com_tjvendors', $columns))
+ {
+ $query = "ALTER TABLE `#__tj_{$tableName}` ADD COLUMN `com_tjvendors` tinyint(1) NOT NULL DEFAULT '1'";
+ $db->setQuery($query);
+
+ if (!$db->execute())
+ {
+ return array(
+ 'status' => false,
+ 'message' => 'Database error: ' . $db->getErrorNum() . ' - ' . $db->getErrorMsg()
+ );
+ }
+ }
+
+ return array('status' => true);
+ }Then simplify the migrate method: public function migrate()
{
- $result = array();
-
try
{
$db = Factory::getDbo();
- $query = $db->getQuery(true);
- $query = "SHOW COLUMNS FROM `#__tj_city`";
- $db->setQuery($query);
- $columns = $db->loadAssoclist();
-
- $columns = array_column($columns, "Field");
-
- if (!in_array('com_tjvendors', $columns))
- {
- $query = "ALTER TABLE `#__tj_city` ADD COLUMN `com_tjvendors` tinyint(1) NOT NULL DEFAULT '1'";
- $db->setQuery($query);
-
- if (!$db->execute())
- {
- $result['status'] = false;
- $result['message'] = $db->getErrorMsg();
-
- return $result;
- }
- }
-
- // Similar code blocks for region and country...
+
+ $tables = ['city', 'region', 'country'];
+
+ foreach ($tables as $table)
+ {
+ $result = $this->addColumnToTable($table, $db);
+
+ if (!$result['status'])
+ {
+ return $result;
+ }
+ }
- $result['status'] = true;
- $result['message'] = "Migration successful";
+ return array(
+ 'status' => true,
+ 'message' => 'Migration successful'
+ );
}
catch (Exception $e)
{
- $result['err_code'] = '';
- $result['status'] = false;
- $result['message'] = $e->getMessage();
+ return array(
+ 'err_code' => '',
+ 'status' => false,
+ 'message' => $e->getMessage()
+ );
}
-
- return $result;
}
🤖 Prompt for AI Agents |
||
|
|
||
| $result['status'] = true; | ||
| $result['message'] = "Migration successful"; | ||
| } | ||
| catch (Exception $e) | ||
| { | ||
| $result['err_code'] = ''; | ||
| $result['status'] = false; | ||
| $result['message'] = $e->getMessage(); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Replace deprecated JFactory with Factory.
JFactory::getDbo()is deprecated in Joomla 4+. Use the modern Factory class instead.🤖 Prompt for AI Agents