4
0
api-powers/src/67f91b8b-7b48-4dbb-bbd5-7945c174622a/code.power
2023-07-28 17:15:46 +02:00

178 lines
3.7 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 tag
*
* @param string $name The tag name being created
*
* @return array|null Array of the tag values on success
* @since 2.0.1
**/
public function set(string $name): ?array
{
// make sure the linker has access
if (($linker = $this->linker->get()) === null)
{
return [
'error' => Text::_("Without selecting the correct favourite verse,<br />you can't perform the initial action."),
'access_required' => true
];
}
// get tag if it exist
if (($tag = $this->get($linker, $name)) !== null)
{
// publish if not published
if ($tag->published != 1 && !$this->update->value(1, 'published', $tag->id, 'id', 'tag'))
{
return [
'error' => Text::_('Tag already exist, but could not be reactivated.')
];
}
$tag->published = 1;
$tag->success = Text::_('The tag was successfully set.');
return (array) $tag;
}
// create a new tag
elseif ($this->create($linker, $name)
&& ($tag = $this->get($linker, $name)) !== null)
{
$tag->success = Text::_('The tag was successfully set.');
return (array) $tag;
}
return null;
}
/**
* Delete a tag
*
* @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', 'tag')) !== null && $id > 0)
{
return $this->update->value(-2, 'published', $id, 'id', 'tag');
}
return false;
}
/**
* Get a tag
*
* @param string $linker The linker GUID value
* @param string $name The tag name
*
* @return array|null Array of the tagged verse values on success
* @since 2.0.1
**/
private function get(
string $linker,
string $name
): ?array
{
// get tag if it exist
if (($tag = $this->load->item([
'linker' => $linker,
'name' => $name
], 'tag')) !== null)
{
return $tag;
}
return null;
}
/**
* Create a Tag
*
* @param string $linker The linker GUID value
* @param string $name The tag name
*
* @return bool True on success
* @since 2.0.1
**/
private function create(
string $linker,
string $name
): bool
{
$guid = (string) GuidHelper::get();
while (!GuidHelper::valid($guid, 'tag', 0, '[[[component]]]'))
{
// must always be set
$guid = (string) GuidHelper::get();
}
return $this->insert->row([
'access' => 0,
'linker' => $linker,
'name' => $name,
'guid' => $guid
], 'tag');
}