You are here

function uc_catalog_uc_store_status in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_catalog/uc_catalog.module \uc_catalog_uc_store_status()

Implements hook_uc_store_status().

Provides status information about the "Product Catalog" and products not listed in the catalog.

File

uc_catalog/uc_catalog.module, line 103
Ubercart Catalog module.

Code

function uc_catalog_uc_store_status() {
  $connection = \Drupal::database();
  $field = FieldStorageConfig::loadByName('node', 'taxonomy_catalog');
  if (!$field) {
    return [
      [
        'status' => 'error',
        'title' => t('Catalog field'),
        'desc' => t('The catalog taxonomy reference field is missing. <a href=":url">Click here to create it</a>.', [
          ':url' => Url::fromRoute('uc_catalog.repair')
            ->toString(),
        ]),
      ],
    ];
  }
  $statuses = [];
  $cat_id = \Drupal::config('uc_catalog.settings')
    ->get('vocabulary');
  $catalog = Vocabulary::load($cat_id);
  if ($catalog) {

    // Don't display a status if the taxonomy_index table has no data.
    if (\Drupal::config('taxonomy.settings')
      ->get('maintain_index_table')) {
      $statuses[] = [
        'status' => 'ok',
        'title' => t('Catalog vocabulary'),
        'desc' => t('Vocabulary @name has been identified as the Ubercart catalog.', [
          '@name' => Link::createFromRoute($catalog
            ->label(), 'entity.taxonomy_vocabulary.edit_form', [
            'taxonomy_vocabulary' => $catalog
              ->id(),
          ])
            ->toString(),
        ]),
      ];
      $product_types = uc_product_types();
      $types = array_intersect($product_types, $field
        ->getBundles());
      $result = $connection
        ->query("SELECT COUNT(DISTINCT n.nid) FROM {node} n LEFT JOIN {taxonomy_index} ti ON n.nid = ti.nid LEFT JOIN {taxonomy_term_data} td ON ti.tid = td.tid WHERE n.type IN (:types[]) AND ti.tid IS NULL AND td.vid = :vid", [
        ':types[]' => $types,
        ':vid' => $catalog
          ->id(),
      ]);
      if ($excluded = $result
        ->fetchField()) {
        $description = \Drupal::translation()
          ->formatPlural($excluded, 'There is 1 product not listed in the catalog.', 'There are @count products not listed in the catalog.') . t('Products are listed by assigning a category from the <a href=":cat_url">Product Catalog</a> vocabulary to them.', [
          ':cat_url' => Url::fromRoute('entity.taxonomy_vocabulary.edit_form', [
            'taxonomy_vocabulary' => $catalog->machine_name,
          ])
            ->toString(),
        ]);
        $terms = $connection
          ->query("SELECT COUNT(1) FROM {taxonomy_term_data} WHERE vid = :vid", [
          ':vid' => $catalog
            ->id(),
        ])
          ->fetchField();
        if ($terms) {
          $description .= ' ' . Link::createFromRoute(t('Find orphaned products here.'), 'uc_product.orphans')
            ->toString();
        }
        else {
          $description .= ' ' . Link::createFromRoute(t('Add terms for the products to inhabit.'), 'entity.taxonomy_vocabulary.add_form', [
            'taxonomy_vocabulary' => $catalog
              ->id(),
          ])
            ->toString();
        }
        $statuses[] = [
          'status' => 'warning',
          'title' => t('Unlisted products'),
          'desc' => $description,
        ];
      }
    }
  }
  else {
    $statuses[] = [
      'status' => 'error',
      'title' => t('Catalog vocabulary'),
      'desc' => t('No vocabulary has been recognized as the Ubercart catalog. Choose one on <a href=":admin_catalog">this page</a> or add one on the <a href=":admin_vocab">taxonomy admin page</a> first, if needed.', [
        ':admin_catalog' => Url::fromRoute('uc_catalog.settings')
          ->toString(),
        ':admin_vocab' => Url::fromRoute('entity.taxonomy_vocabulary.collection')
          ->toString(),
      ]),
    ];
  }
  return $statuses;
}