You are here

class KanbanStatisticService in Content Planner 8

Class KanbanStatisticService.

Hierarchy

Expanded class hierarchy of KanbanStatisticService

1 string reference to 'KanbanStatisticService'
content_kanban.services.yml in modules/content_kanban/content_kanban.services.yml
modules/content_kanban/content_kanban.services.yml
1 service uses KanbanStatisticService
content_kanban.kanban_statistic_service in modules/content_kanban/content_kanban.services.yml
Drupal\content_kanban\KanbanStatisticService

File

modules/content_kanban/src/KanbanStatisticService.php, line 11

Namespace

Drupal\content_kanban
View source
class KanbanStatisticService {

  /**
   * The database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a new NewsService object.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * Get content counts from a given Workflow.
   *
   * @param \Drupal\workflows\Entity\Workflow $workflow
   *   The workflow object.
   *
   * @return array
   *   Returns an array with the workflow state content counts.
   */
  public function getWorkflowStateContentCounts(Workflow $workflow) {

    // Get all workflow states form a given workflow.
    $workflow_states = KanbanWorkflowService::getWorkflowStates($workflow);
    $data = [];
    foreach ($workflow_states as $state_id => $state_label) {
      $count = $this
        ->getWorkflowStateContentCount($workflow
        ->id(), $state_id);
      $data[$state_id] = [
        'id' => $state_id,
        'label' => $state_label,
        'count' => $count,
      ];
    }
    return $data;
  }

  /**
   * Get the content count of a given workflow state.
   *
   * @param string $workflow_id
   *   The workflow ID.
   * @param string $state_id
   *   The state ID.
   *
   * @return mixed
   *   Returns the workflow state content count.
   */
  public function getWorkflowStateContentCount($workflow_id, $state_id) {
    $query = $this->database
      ->select('content_moderation_state_field_data', 'c');
    $query
      ->addField('c', 'id');
    $query
      ->condition('c.workflow', $workflow_id);
    $query
      ->condition('c.moderation_state', $state_id);
    $count_query = $query
      ->countQuery();
    $result = $count_query
      ->execute()
      ->fetchObject();
    return $result->expression;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KanbanStatisticService::$database protected property The database service.
KanbanStatisticService::getWorkflowStateContentCount public function Get the content count of a given workflow state.
KanbanStatisticService::getWorkflowStateContentCounts public function Get content counts from a given Workflow.
KanbanStatisticService::__construct public function Constructs a new NewsService object.