262 lines
6.6 KiB
Plaintext
262 lines
6.6 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;
|
|
}
|
|
|
|
/**
|
|
* Create a tag
|
|
*
|
|
* @param string $name The tag name being created
|
|
* @param string|null $description The tag description being created
|
|
*
|
|
* @return array|null Array of the tag values on success
|
|
* @since 2.0.1
|
|
**/
|
|
public function create(string $name, ?string $description): ?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
|
|
$name = trim($name);
|
|
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.')
|
|
];
|
|
}
|
|
|
|
// update the description if it does not match
|
|
$description = $description ?? '';
|
|
$description = trim($description);
|
|
if ($tag->description !== $description && $this->update->value($description, 'description', $tag->id, 'id', 'tag'))
|
|
{
|
|
$tag->description = $description;
|
|
}
|
|
|
|
$tag->published = 1;
|
|
$tag->success = Text::_('The tag was successfully reactivated.');
|
|
return (array) $tag;
|
|
}
|
|
// create a new tag
|
|
elseif (strlen($name) >= 2 && $this->createTag($linker, $name, $description)
|
|
&& ($tag = $this->get($linker, $name)) !== null)
|
|
{
|
|
$tag->success = Text::_('The tag was successfully created.');
|
|
return (array) $tag;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Update a tag
|
|
*
|
|
* @param string $tag The tag GUID value
|
|
* @param string $name The tag name being created
|
|
* @param string|null $description The tag description being created
|
|
*
|
|
* @return array|null Array of the tag values on success
|
|
* @since 2.0.1
|
|
**/
|
|
public function update(string $tag, string $name, ?string $description): ?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
|
|
$name = trim($name);
|
|
$tag = trim($tag);
|
|
if (($_tag = $this->load->item(['linker' => $linker, 'guid' => $tag], 'tag')) !== null)
|
|
{
|
|
// publish if not published
|
|
if ($_tag->published != 1 && !$this->update->value(1, 'published', $_tag->id, 'id', 'tag'))
|
|
{
|
|
return [
|
|
'error' => Text::_('Tag found, but could not be reactivated.')
|
|
];
|
|
}
|
|
|
|
// update the description if it does not match
|
|
$description = $description ?? '';
|
|
$description = trim($description);
|
|
$_tag->description = $_tag->description ?? '';
|
|
if ($_tag->description !== $description && $this->update->value($description, 'description', $_tag->id, 'id', 'tag'))
|
|
{
|
|
$_tag->description = $description;
|
|
}
|
|
|
|
// update the name if it does not match
|
|
if (strlen($name) >= 2 && $_tag->name !== $name && $this->update->value($name, 'name', $_tag->id, 'id', 'tag'))
|
|
{
|
|
$_tag->name = $name;
|
|
}
|
|
|
|
$_tag->published = 1;
|
|
$_tag->success = Text::_('The tag was successfully updated.');
|
|
return (array) $_tag;
|
|
}
|
|
//elseif (($_tag = $this->load->item(['guid' => $tag], 'tag')) !== null)
|
|
//{
|
|
// we may need to add this
|
|
//}
|
|
|
|
return [
|
|
'error' => Text::_("This tag doesn't belong to you, thus you cannot edit it.")
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Delete a tag
|
|
*
|
|
* @param string $tag The tagged verse GUID value
|
|
*
|
|
* @return array|null Array of the message on success
|
|
* @since 2.0.1
|
|
**/
|
|
public function delete(string $tag): ?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
|
|
];
|
|
}
|
|
|
|
// make sure the linker has access to delete this tag
|
|
if (($id = $this->load->value(['guid' => $tag, 'linker' => $linker], 'id', 'tag')) !== null && $id > 0
|
|
&& $this->update->value(-2, 'published', $id, 'id', 'tag'))
|
|
{
|
|
return [
|
|
'success' => Text::_('Tag successfully deleted.')
|
|
];
|
|
}
|
|
|
|
return [
|
|
'error' => Text::_("This tag doesn't belong to you, thus you cannot delete it.")
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get a tag
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
* @param string $name The tag name
|
|
*
|
|
* @return object|null Array of the tagged verse values on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function get(
|
|
string $linker,
|
|
string $name
|
|
): ?object
|
|
{
|
|
// 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
|
|
* @param string|null $description The tag description being created
|
|
*
|
|
* @return bool True on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function createTag(
|
|
string $linker,
|
|
string $name,
|
|
?string $description
|
|
): 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,
|
|
'description' => $description ?? '',
|
|
'guid' => $guid
|
|
], 'tag');
|
|
} |