Server : LiteSpeed System : Linux server 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 User : alsaif ( 1057) PHP Version : 7.4.33 Disable Function : show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/alsaif/public_html/plugins/system/nrframework/NRFramework/Library/ |
<?php
/**
* @author Tassos Marinos <info@tassos.gr>
* @link https://www.tassos.gr
* @copyright Copyright © 2024 Tassos All Rights Reserved
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/
namespace NRFramework\Library;
defined('_JEXEC') or die;
class Favorites
{
/**
* Library
*
* @var Library
*/
protected $library = [];
public function __construct($library = [])
{
$this->library = $library;
}
/**
* Handles AJAX Library favorite toggle
*
* @return string
*/
public function tf_library_ajax_favorites_toggle()
{
$template_id = $this->library->getLibrarySetting('template_id');
if (empty($template_id))
{
return false;
}
$this->addOrRemoveFavorite($template_id);
return $this->getFavorites();
}
/**
* Add or remove favorites
*
* @param int $template_id
*
* @return void
*/
private function addOrRemoveFavorite($template_id)
{
$favorites = $this->getFavorites();
if (array_key_exists($template_id, $favorites))
{
$this->removeFromFavorites($template_id);
return;
}
$this->addToFavorites($template_id);
}
/**
* Add to favorites
*
* @param int $template_id
*
* @return void
*/
private function addToFavorites($template_id)
{
$favorites = $this->getFavorites();
if (array_key_exists($template_id, $favorites))
{
return;
}
$favorites[$template_id] = true;
$this->saveFavorites($favorites);
}
/**
* Save favorites to file
*
* @param string $content
*
* @return void
*/
private function saveFavorites($content)
{
// Create directory if not exist
if (!is_dir($this->library->getTemplatesPath()))
{
\NRFramework\File::createDirs($this->library->getTemplatesPath());
}
$file = $this->library->getTemplatesPath() . 'favorites.json';
return file_put_contents($file, json_encode($content));
}
/**
* Remove from favorites
*
* @param int $template_id
*
* @return void
*/
private function removeFromFavorites($template_id)
{
$favorites = $this->getFavorites();
unset($favorites[$template_id]);
$this->saveFavorites($favorites);
}
/**
* Get favorites
*
* @return array
*/
public function getFavorites()
{
$file = $this->library->getTemplatesPath() . 'favorites.json';
if (!file_exists($file))
{
return [];
}
return (array) json_decode(file_get_contents($file), true);
}
}