<?php

/**
 * @file
 * Install, update and uninstall functions for the uc_catalog module.
 */

/**
 * Implements hook_install().
 */
function uc_catalog_install() {
  foreach (uc_product_types() as $type) {
    uc_catalog_add_node_type($type);
  }
  uc_catalog_add_image_field();
}

/**
 * Implements hook_uninstall().
 */
function uc_catalog_uninstall() {
  if ($vid = \Drupal::config('uc_catalog.settings')->get('vocabulary')) {
    $vocab = \Drupal\taxonomy\Entity\Vocabulary::load($vid);
    drupal_set_message(t('The Ubercart %catalog vocabulary has not been deleted. If you need to delete it, <a href=":url">please do so manually</a>.', ['%catalog' => $vocab->label(), ':url' => \Drupal\Core\Url::fromRoute('entity.taxonomy_vocabulary.edit_form', ['taxonomy_vocabulary' => $vocab->id()])->toString()]), 'warning');
  }

  $styles = \Drupal\image\Entity\ImageStyle::loadMultiple(['uc_category', 'uc_product_list']);
  if (!empty($styles)) {
    $style_names = array();
    foreach ($styles as $style) {
      $style_names[] = $style->label();
    }
    drupal_set_message(\Drupal::translation()->formatPlural(count($style_names),
      'The image style %style has not been deleted. If you need to delete it, <a href=":url">please do so manually</a>.',
      'The image styles %styles have not been deleted. If you need to delete them, <a href=":url">please do so manually</a>.',
      array(
        '%style' => $style_names[0],
        '%styles' => implode(', ', $style_names),
        ':url' => \Drupal\Core\Url::fromRoute('entity.image_style.collection')->toString(),
      )
    ), 'warning');
  }
}
