@git Get Bible @github Get Bible @support Get Bible @copyright Copyright (C) 2015. All Rights Reserved @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html /------------------------------------------------------------------------------------------------------*/ // No direct access to this file defined('_JEXEC') or die('Restricted access'); use Joomla\CMS\MVC\Model\ListModel; use Joomla\Utilities\ArrayHelper; use Joomla\Registry\Registry; use Joomla\CMS\Language\Text; use VDM\Joomla\Utilities\FileHelper; use VDM\Joomla\Gitea\Factory; /** * Getbible Ajax List Model */ class GetbibleModelAjax extends ListModel { protected $app_params; public function __construct() { parent::__construct(); // get params $this->app_params = JComponentHelper::getParams('com_getbible'); } // Used in translation /** * Check and if a notice is new (per/user) * * @param string|null $notice The current notice * * @return bool true if is new * @since 2.0.0 */ public function isNew(?string $notice): bool { // first get the file path $path_filename = FileHelper::getPath('path', 'usernotice', 'md', JFactory::getUser()->username, JPATH_COMPONENT_ADMINISTRATOR); // check if the file is set if (($content = FileHelper::getContent($path_filename, FALSE)) !== FALSE) { if ($notice == $content) { return false; } } return true; } /** * Check if a notice has been read (per/user) * * @param string|null $notice The current notice * * @return bool true if is read * @since 2.0.0 */ public function isRead(?string $notice): bool { // first get the file path $path_filename = FileHelper::getPath('path', 'usernotice', 'md', JFactory::getUser()->username, JPATH_COMPONENT_ADMINISTRATOR); // set as read if not already set if (($content = FileHelper::getContent($path_filename, FALSE)) !== FALSE) { if ($notice == $content) { return true; } } return FileHelper::write($path_filename, $notice); } /** * get Current Version * * @param string|null $message The error messages if any. * * @return array The array of the notice or error message * @since 2.3.0 */ public function getVersion($version = null) { // get the token if set $token = $this->app_params->get('gitea_token'); // only add if token is set if ($token) { try { // load the API details Factory::_('Gitea.Repository.Tags')->load_('https://git.vdm.dev', $token); // get the repository tags $tags = Factory::_('Gitea.Repository.Tags')->list('getBible', 'joomla-component'); } catch (DomainException $e) { return $this->getTokenForVersion($e->getMessage()); } catch (InvalidArgumentException $e) { return $this->getTokenForVersion($e->getMessage()); } catch (Exception $e) { return $this->getTokenForVersion($e->getMessage()); } // do we have tags returned if (isset($tags[0]) && isset($tags[0]->name)) { // get the version $manifest = GetbibleHelper::manifest(); $local_version = (string) $manifest->version; $current_version = trim($tags[0]->name, 'vV'); // now check if this version is out dated if ($current_version === $local_version) { return ['notice' => '' . Text::_('COM_GETBIBLE_UP_TO_DATE') . '']; } else { // check if this is beta version $current_array = array_map(function ($v) { return (int) $v; }, (array) explode('.', $current_version)); $local_array = array_map(function ($v) { return (int) $v; }, (array) explode('.', $local_version)); if (($local_array[0] > $current_array[0]) || ($local_array[0] == $current_array[0] && $local_array[1] > $current_array[1]) || ($local_array[0] == $current_array[0] && $local_array[1] == $current_array[1] && $local_array[2] > $current_array[2])) { return ['notice' => '' . Text::_('COM_GETBIBLE_BETA_RELEASE') . '']; } else { // download link of the latest version $download = "https://git.vdm.dev/api/v1/repos/getBible/joomla-component/archive/" . $tags[0]->name . ".zip?access_token=" . $token; return ['notice' => '' . Text::_('COM_GETBIBLE_OUT_OF_DATE') . '! ' . Text::_('COM_GETBIBLE_DOWNLOAD_UPDATE') . '!']; } } } } return $this->getTokenForVersion(); } /** * Instructions to get Token for version * * @param string|null $message The error messages if any. * * @return array The array of the error message * @since 2.3.0 */ protected function getTokenForVersion(?string $message = null): array { // the URL $url = 'https://git.vdm.dev/user/settings/applications'; // create link $a = ''; $_a = ''; if ($message) { return ['error' => $a . $message . $a_ . Text::_('COM_GETBIBLE_GET_TOKEN') . $_a]; } return ['error' => $a . Text::_('COM_GETBIBLE_GET_TOKEN_FROM_VDM_TO_GET_UPDATE_NOTICE_AND_ADD_IT_TO_YOUR_GLOBAL_OPTIONS') . $a_ . Text::_('COM_GETBIBLE_GET_TOKEN') . $_a]; } /** * get Wiki Page * * @param string|null $message The error messages if any. * * @return array The array of the page or error message * @since 2.3.0 */ public function getWiki(string $name = 'Home'): array { try { // get the token if set $token = $this->app_params->get('gitea_token'); // load the API details Factory::_('Gitea.Repository.Wiki')->load_('https://git.vdm.dev', $token); // get the gitea wiki page im markdown $wiki = Factory::_('Gitea.Repository.Wiki')->get('getBible', 'support', $name); // now render the page in HTML $page = Factory::_('Gitea.Miscellaneous.Markdown')->render($wiki->content, true); } catch (DomainException $e) { return $this->getTokenForWiki($e->getMessage()); } catch (InvalidArgumentException $e) { return $this->getTokenForWiki($e->getMessage()); } catch (Exception $e) { return $this->getTokenForWiki($e->getMessage()); } // get the html if (isset($page)) { return ['page' => $page]; } return $this->getTokenForWiki(); } /** * Instructions to get Token for wiki * * @param string|null $message The error messages if any. * * @return array The array of the error message * @since 2.3.0 */ protected function getTokenForWiki(?string $message = null): array { if ($message) { return ['error' => $message]; } return ['error' => Text::_('COM_GETBIBLE_THE_WIKI_CAN_ONLY_BE_LOADED_WHEN_YOUR_JCB_SYSTEM_HAS_INTERNET_CONNECTION')]; } }