contrib_todo_list-1.1.1/src/Service/TodoManagerService.php
src/Service/TodoManagerService.php
<?php
namespace Drupal\contrib_todo_list\Service;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\contrib_todo_list\data\TodoState;
use Drupal\contrib_todo_list\Entity\Todo;
use \Drupal\Core\Logger\LoggerChannelFactoryInterface;
/**
* Service for managing todos.
*/
class TodoManagerService {
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* The logger factory service.
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected AccountProxyInterface $currentUser,
protected LanguageManagerInterface $languageManager,
protected LoggerChannelFactoryInterface $loggerFactory
)
{
}
/**
* Gets todos for a specific node.
*
* @param NodeInterface $node
* The node entity.
*
* @return Todo[]
* Array of todo entities by node.
* @throws InvalidPluginDefinitionException
* @throws PluginNotFoundException
*/
public function getTodosByNode(NodeInterface $node): array {
$query = $this->entityTypeManager->getStorage('todo')->getQuery()
->condition('nid', $node->id())
->condition('langcode', $this->languageManager->getCurrentLanguage()->getId())
->accessCheck(false);
$orGroup = $query->orConditionGroup()
->condition('uid', $this->currentUser->id())
->condition('share', TRUE);
$query->condition($orGroup);
$ids = $query->execute();
if (empty($ids)) {
return [];
}
return $this->entityTypeManager->getStorage('todo')->loadMultiple($ids);
}
/**
* Gets todos for current user.
*
* @param string|null $state
* The state todo.
* @param string|null $share
* The share status to set.
*
* @return Todo[]
* Array of user todo entities.
*
* @throws InvalidPluginDefinitionException
* @throws PluginNotFoundException
*/
public function getUserTodos(?string $state, ?string $share): array {
$query = $this->entityTypeManager->getStorage('todo')
->getQuery()
->condition('uid', $this->currentUser->id())
->accessCheck(false);
if ($state) {
$query->condition('state', $state);
}
if ($share) {
$query->condition('share', $share);
}
$ids = $query->execute();
return $this->entityTypeManager->getStorage('todo')->loadMultiple($ids);
}
/**
* Updates share status for a todo.
*
* @param Todo $todo
* The todo entity.
* @param bool $share
* The share status to set.
*
* @return bool
* TRUE if update successful, FALSE otherwise.
* @throws EntityStorageException
*/
public function updateTodoShare(Todo $todo, bool $share): bool {
$todo->set('share', $share);
return (bool)$todo->save();
}
/**
* Updates state for a todo.
*
* @param Todo $todo
* The todo entity.
* @param string $state
* The state to set (pending, in_progress, completed).
*
* @return bool
* TRUE if update successful, FALSE otherwise.
* @throws EntityStorageException
*/
public function updateTodoState(Todo $todo, string $state): bool {
if (!in_array($state, TodoState::getStateKeys())) {
return false;
}
$todo->set('state', TodoState::getStateFromKey($state));
return (bool)$todo->save();
}
/**
* Creates a new todo.
*
* @param NodeInterface $node
* The node entity.
* @param string $todoText
* The todo text content.
*
* @return Todo|null
* The created todo entity or null if creation failed.
*/
public function createTodo(NodeInterface $node, string $todoText): ?Todo {
try {
/** @var Todo $todo */
$todo = $this->entityTypeManager->getStorage('todo')->create([
'nid' => $node->id(),
'uid' => $this->currentUser->id(),
'langcode' => $this->languageManager->getCurrentLanguage()->getId(),
'todo' => $todoText,
'share' => FALSE,
'state' => TodoState::getStateFromKey(TodoState::PENDING),
]);
$todo->save();
return $todo;
}
catch (\Exception $e) {
$this->loggerFactory->get('contrib_todo_list')->error('Failed to create todo: @message', ['@message' => $e->getMessage()]);
}
return null;
}
/**
* Deletes a todo entity.
*
* @param Todo $todo
* The todo entity to delete.
*
* @return bool
* TRUE if deletion was successful, FALSE otherwise.
*/
public function deleteTodo(Todo $todo): bool {
try {
if ($todo->get('uid')->target_id !== $this->currentUser->id()) {
return false;
}
$todo->delete();
return true;
}
catch (\Exception $e) {
$this->loggerFactory->get('contrib_todo_list')->error('Failed to delete todo: @message', ['@message' => $e->getMessage()]);
}
return false;
}
/**
* Add a pin to a todo entity.
*
* @param Todo $todo
* The todo entity.
* @param string $pin
* The element selector.
*
* @return bool
* TRUE if add a pin was successful, FALSE otherwise.
* @throws EntityStorageException
*/
public function addPin(Todo $todo, string $pin): bool {
$todo->set('pin', $pin);
return (bool)$todo->save();
}
/**
* Get the node url by todo
*
* @param Todo $todo
* The todo entity.
*
* @return string
* The node url
*/
public function getNodeUrlByTodo(Todo $todo): string {
$node = $todo->get('nid')->entity;
if (!$node) {
return '';
}
$currentLanguage = $this->languageManager->getCurrentLanguage()->getId();
if ($node->hasTranslation($currentLanguage)) {
$node = $node->getTranslation($currentLanguage);
}
return $node->toUrl()
->setAbsolute()
->toString();
}
}
