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

221 lines
5.5 KiB
Plaintext

/**
* 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::_('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', '[[[component]]]')) === null
|| $published != 1)
{
return [
'error' => Text::_('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::_('Tagged verse already exist, but could not be reactivated.')
];
}
$_tag->published = 1;
$_tag->success = Text::_('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::_('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, '[[[component]]]'))
{
// 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');
}