Skip to content

Commit

Permalink
feat: introduce tool to change main.referenceUrl in records (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
modelrailroader committed May 20, 2024
1 parent 008a73a commit ead7442
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpmyfaq/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}

// Check permission to add new faqs
if (-1 !== $user->getUserId() && !$user->perm->hasPermission($user->getUserId(), PermissionType::FAQ_ADD)) {
if (-1 !== $user->getUserId() && !$user->perm->hasPermission($user->getUserId(), PermissionType::FAQ_ADD->value)) {
$response = new RedirectResponse($faqSystem->getSystemUri($faqConfig));
$response->send();
}
Expand Down
39 changes: 39 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,43 @@ public function update(array $newConfigs): bool

return true;
}

/**
* Updates main.referenceUrl in media objects in faqs.
*
* @param string[] $oldUrl Old main.referenceUrl
* @paran string[] $newUrl New main.referenceUrl
* @return bool true|false
*/
public function replaceMainReferenceUrl(string $oldUrl, string $newUrl)
{
$query = sprintf(
"SELECT content FROM %sfaqdata",
Database::getTablePrefix()
);
$response = $this->getDb()->query($query);
$contentItems = $this->getDb()->fetchAll($response);
$newContentItems = [];

foreach ($contentItems as $item) {
if (strpos($item->content, $oldUrl) !== false) {
$newContentItems[] = str_replace($oldUrl, $newUrl, $item->content);
} else {
$newContentItems[] = $item->content;
}
}

$count = 0;
foreach ($newContentItems as $newItem) {
$query = sprintf(
"UPDATE %sfaqdata SET content='%s' WHERE content='%s'",
Database::getTablePrefix(),
$this->getDb()->escape($newItem),
$this->getDb()->escape($contentItems[$count]->content)
);
$count++;
$this->getDb()->query($query);
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public function save(Request $request): JsonResponse
}
}

// Replace main.referenceUrl in faqs
if ($oldConfigurationData['main.referenceURL'] !== $newConfigValues['main.referenceURL']) {
$this->configuration->replaceMainReferenceUrl(
$oldConfigurationData['main.referenceURL'],
$newConfigValues['main.referenceURL']
);
}

var_dump($oldConfigurationData['main.referenceURL']);
var_dump($newConfigValues['main.referenceURL']);

$this->configuration->update($newConfigValues);

return $this->json(['success' => Translation::get('ad_config_saved')], Response::HTTP_OK);
Expand Down

0 comments on commit ead7442

Please sign in to comment.