Link: Add shortcuts for homepage links and validate urLs - refs #4767
parent
70178f39b2
commit
e039da98d6
@ -0,0 +1,49 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Controller\Api; |
||||
|
||||
use Chamilo\CourseBundle\Entity\CLink; |
||||
use Chamilo\CourseBundle\Repository\CShortcutRepository; |
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
use Symfony\Component\HttpFoundation\Response; |
||||
|
||||
class CLinkDetailsController extends AbstractController |
||||
{ |
||||
public function __invoke(CLink $link, CShortcutRepository $shortcutRepository): Response |
||||
{ |
||||
$shortcut = $shortcutRepository->getShortcutFromResource($link); |
||||
$isOnHomepage = null !== $shortcut; |
||||
|
||||
$parentResourceNodeId = null; |
||||
if ($link->getResourceNode() && $link->getResourceNode()->getParent()) { |
||||
$parentResourceNodeId = $link->getResourceNode()->getParent()->getId(); |
||||
} |
||||
|
||||
$resourceLinkList = []; |
||||
if ($link->getResourceLinkEntityList()) { |
||||
foreach ($link->getResourceLinkEntityList() as $resourceLink) { |
||||
$resourceLinkList[] = [ |
||||
'visibility' => $resourceLink->getVisibility(), |
||||
'cid' => $resourceLink->getCourse()->getId(), |
||||
'sid' => $resourceLink->getSession()->getId() |
||||
]; |
||||
} |
||||
} |
||||
|
||||
$details = [ |
||||
'url' => $link->getUrl(), |
||||
'title' => $link->getTitle(), |
||||
'description' => $link->getDescription(), |
||||
'onHomepage' => $isOnHomepage, |
||||
'target' => $link->getTarget(), |
||||
'parentResourceNodeId' => $parentResourceNodeId, |
||||
'resourceLinkList' => $resourceLinkList, |
||||
'category' => $link->getCategory()?->getIid(), |
||||
]; |
||||
|
||||
return $this->json($details, Response::HTTP_OK); |
||||
} |
||||
} |
@ -0,0 +1,66 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Controller\Api; |
||||
|
||||
use Chamilo\CoreBundle\Settings\SettingsManager; |
||||
use Chamilo\CourseBundle\Entity\CLink; |
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
use Symfony\Component\HttpFoundation\JsonResponse; |
||||
use Symfony\Component\HttpFoundation\Request; |
||||
|
||||
class CheckCLinkAction extends AbstractController |
||||
{ |
||||
public function __invoke(CLink $link, Request $request, SettingsManager $settingsManager): JsonResponse |
||||
{ |
||||
$url = $request->query->get('url'); |
||||
$result = $this->checkUrl($url, $settingsManager); |
||||
|
||||
return new JsonResponse(['isValid' => $result]); |
||||
} |
||||
|
||||
private function checkUrl(string $url, SettingsManager $settingsManager): bool |
||||
{ |
||||
// Check if curl is available. |
||||
if (!\in_array('curl', get_loaded_extensions())) { |
||||
return false; |
||||
} |
||||
|
||||
// set URL and other appropriate options |
||||
$defaults = [ |
||||
CURLOPT_URL => $url, |
||||
CURLOPT_FOLLOWLOCATION => true, // follow redirects |
||||
CURLOPT_HEADER => 0, |
||||
CURLOPT_RETURNTRANSFER => true, |
||||
CURLOPT_TIMEOUT => 4, |
||||
]; |
||||
|
||||
// Check for proxy settings in your application configuration |
||||
$proxySettings = $settingsManager->getSetting('platform.proxy_settings', true); |
||||
if ($proxySettings && isset($proxySettings['curl_setopt_array'])) { |
||||
$defaults[CURLOPT_PROXY] = $proxySettings['curl_setopt_array']['CURLOPT_PROXY']; |
||||
$defaults[CURLOPT_PROXYPORT] = $proxySettings['curl_setopt_array']['CURLOPT_PROXYPORT']; |
||||
} |
||||
|
||||
// Create a new cURL resource |
||||
$ch = curl_init(); |
||||
curl_setopt_array($ch, $defaults); |
||||
|
||||
// grab URL and pass it to the browser |
||||
ob_start(); |
||||
$result = curl_exec($ch); |
||||
ob_get_clean(); |
||||
|
||||
// close cURL resource, and free up system resources |
||||
curl_close($ch); |
||||
|
||||
// Check for any errors |
||||
if ($result === false || curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) { |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,72 @@ |
||||
<?php |
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||
|
||||
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||
use Chamilo\CourseBundle\Entity\CLink; |
||||
use Chamilo\CourseBundle\Repository\CLinkRepository; |
||||
use Chamilo\CourseBundle\Repository\CShortcutRepository; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
|
||||
class Version20240202122300 extends AbstractMigrationChamilo |
||||
{ |
||||
public function getDescription(): string |
||||
{ |
||||
return 'Create shortcuts for c_link entries with on_homepage = 1'; |
||||
} |
||||
|
||||
public function up(Schema $schema): void |
||||
{ |
||||
$container = $this->getContainer(); |
||||
$doctrine = $container->get('doctrine'); |
||||
|
||||
$em = $doctrine->getManager(); |
||||
$connection = $em->getConnection(); |
||||
|
||||
$admin = $this->getAdmin(); |
||||
|
||||
$linkRepo = $container->get(CLinkRepository::class); |
||||
$shortcutRepo = $container->get(CShortcutRepository::class); |
||||
|
||||
$sql = 'SELECT * FROM c_link WHERE on_homepage = 1'; |
||||
$stmt = $connection->prepare($sql); |
||||
$result = $stmt->executeQuery(); |
||||
|
||||
while ($row = $result->fetchAssociative()) { |
||||
$linkId = $row['iid']; |
||||
|
||||
/* @var CLink $link */ |
||||
$link = $linkRepo->find($linkId); |
||||
|
||||
if (!$link) { |
||||
error_log("Link with ID $linkId not found"); |
||||
continue; |
||||
} |
||||
|
||||
$course = $link->getFirstResourceLink()->getCourse(); |
||||
$session = $link->getFirstResourceLink()->getSession(); |
||||
|
||||
$shortcut = $shortcutRepo->getShortcutFromResource($link); |
||||
if (null === $shortcut) { |
||||
try { |
||||
$shortcutRepo->addShortCut($link, $admin, $course, $session); |
||||
error_log("Shortcut created for link ID $linkId"); |
||||
} catch (\Exception $e) { |
||||
error_log("Failed to create shortcut for link ID $linkId: " . $e->getMessage()); |
||||
} |
||||
} else { |
||||
error_log("Shortcut already exists for link ID $linkId"); |
||||
} |
||||
} |
||||
|
||||
$em->flush(); |
||||
} |
||||
|
||||
public function down(Schema $schema): void |
||||
{ |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue