4
0
api-powers/src/2315a1c5-bcf6-401a-8d11-60f4d6d12dbb/code.php
2023-07-22 05:43:37 +02:00

253 lines
6.2 KiB
PHP

<?php
/**
* @package GetBible
*
* @created 30th May, 2023
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git GetBible <https://git.vdm.dev/getBible>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VDM\Joomla\GetBible;
use Joomla\CMS\Language\Text;
use VDM\Joomla\GetBible\Database\Load;
use VDM\Joomla\GetBible\Database\Insert;
use VDM\Joomla\GetBible\Database\Update;
use VDM\Joomla\GetBible\Linker;
use VDM\Joomla\Utilities\GuidHelper;
/**
* The GetBible Tagged
*
* @since 2.0.1
*/
final class Tagged
{
/**
* The Load class
*
* @var Load
* @since 2.0.1
*/
protected Load $load;
/**
* The Insert class
*
* @var Insert
* @since 2.0.1
*/
protected Insert $insert;
/**
* The Update class
*
* @var Update
* @since 2.0.1
*/
protected Update $update;
/**
* The Linker class
*
* @var Linker
* @since 2.0.1
*/
protected Linker $linker;
/**
* Constructor
*
* @param Load $load The load object.
* @param Insert $insert The insert object.
* @param Update $update The update object.
* @param Linker $linker The linker object.
*
* @since 2.0.1
*/
public function __construct(
Load $load,
Insert $insert,
Update $update,
Linker $linker)
{
$this->load = $load;
$this->insert = $insert;
$this->update = $update;
$this->linker = $linker;
}
/**
* Set a tagged verse
*
* @param string $translation The translation in which the note is made
* @param int $book The book in which the note is made
* @param int $chapter The chapter in which the note is made
* @param int $verse The verse where the note is made
* @param string $tag The tag being added
*
* @return array|null Array of the tagged values on success
* @since 2.0.1
**/
public function set(
string $translation,
int $book,
int $chapter,
int $verse,
string $tag
): ?array
{
// make sure the linker has access
if (($linker = $this->linker->get()) === null)
{
return [
'error' => Text::_('COM_GETBIBLE_YOU_DO_NOT_HAVE_ACCESS_TO_PERFORM_THIS_ACTION'),
'access_required' => true
];
}
// make sure the tag is active
if (($published = GuidHelper::item($tag, 'tag', 'a.published', 'getbible')) === null
|| $published != 1)
{
return [
'error' => Text::_('COM_GETBIBLE_THE_TAG_SELECTED_IS_NOT_ACTIVE_PLEASE_SELECT_AN_ACTIVE_TAG')
];
}
// get tag if it exist
if (($_tag = $this->get($linker, $translation, $book, $chapter, $verse, $tag)) !== null)
{
// publish if not published
if ($_tag->published != 1 && !$this->update->value(1, 'published', $_tag->id, 'id', 'tagged_verse'))
{
return [
'error' => Text::_('COM_GETBIBLE_TAGGED_VERSE_ALREADY_EXIST_BUT_COULD_NOT_BE_REACTIVATED')
];
}
$_tag->published = 1;
$_tag->success = Text::_('COM_GETBIBLE_THE_VERSE_WAS_SUCCESSFULLY_TAGGED');
return (array) $_tag;
}
// create a new tag
elseif ($this->create($linker, $translation, $book, $chapter, $verse, $tag)
&& ($_tag = $this->get($linker, $translation, $book, $chapter, $verse, $tag)) !== null)
{
$_tag->success = Text::_('COM_GETBIBLE_THE_VERSE_WAS_SUCCESSFULLY_TAGGED');
return (array) $_tag;
}
return null;
}
/**
* Delete a tagged verse
*
* @param string $tag The tagged verse GUID value
*
* @return bool True on success
* @since 2.0.1
**/
public function delete(string $tag): bool
{
// make sure the linker has access
if (($linker = $this->linker->get()) === null)
{
return false;
}
// make sure the linker has access to delete this tag
if (($id = $this->load->value(['guid' => $tag, 'linker' => $linker], 'id', 'tagged_verse')) !== null && $id > 0)
{
return $this->update->value(-2, 'published', $id, 'id', 'tagged_verse');
}
return false;
}
/**
* Get a tagged verse
*
* @param string $linker The linker GUID value
* @param string $translation The translation in which the note is made
* @param int $book The book in which the note is made
* @param int $chapter The chapter in which the note is made
* @param int $verse The verse where the note is made
* @param string $tag The tag being added
*
* @return object|null Object of the tagged verse values on success
* @since 2.0.1
**/
private function get(
string $linker,
string $translation,
int $book,
int $chapter,
int $verse,
string $tag
): ?object
{
// get tag if it exist
if (($_tag = $this->load->item([
'linker' => $linker,
'translation' => $translation,
'book_nr' => $book,
'chapter' => $chapter,
'verse' => $verse,
'tag' => $tag
], 'tagged_verse')) !== null)
{
return $_tag;
}
return null;
}
/**
* Create a Tagged verse
*
* @param string $linker The linker GUID value
* @param string $translation The translation in which the note is made
* @param int $book The book in which the note is made
* @param int $chapter The chapter in which the note is made
* @param int $verse The verse where the note is made
* @param string $tag The tag being added
*
* @return bool True on success
* @since 2.0.1
**/
private function create(
string $linker,
string $translation,
int $book,
int $chapter,
int $verse,
string $tag
): bool
{
$guid = (string) GuidHelper::get();
while (!GuidHelper::valid($guid, 'tagged_verse', 0, 'getbible'))
{
// must always be set
$guid = (string) GuidHelper::get();
}
return $this->insert->row([
'tag' => $tag,
'access' => 0,
'linker' => $linker,
'translation' => $translation,
'book_nr' => $book,
'chapter' => $chapter,
'verse' => $verse,
'guid' => $guid
], 'tagged_verse');
}
}