<?php

/**
 * @file
 * Installation information for the 'Webform Entity Print' module.
 */

use Drupal\Core\Serialization\Yaml;
use Drupal\system\Entity\Action;

// Webform Entity Print install helper functions.
include_once __DIR__ . '/includes/webform_entity_print.install.inc';

/**
 * Implements hook_install().
 */
function webform_entity_print_install() {
  /** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
  $third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');

  // Debug.
  if ($third_party_settings_manager->getThirdPartySetting('webform_entity_print', 'debug') === NULL) {
    $third_party_settings_manager->setThirdPartySetting('webform_entity_print', 'debug', FALSE);
  }

  // Template.
  if ($third_party_settings_manager->getThirdPartySetting('webform_entity_print', 'template') === NULL) {
    $template = [
      'header' => '<h1>[webform_submission:label]</h1>',
      'footer' => '',
      'css' => '/* Remove page margins and padding and rely on the PDF generator\'s default margins. */
body {
  margin: 0;
}

.page {
  padding: 0;
}',
    ];
    $third_party_settings_manager->setThirdPartySetting('webform_entity_print', 'template', $template);
  }

  // Export types. Only enabling PDF documents.
  if ($third_party_settings_manager->getThirdPartySetting('webform_entity_print', 'export_types') === NULL) {
    $export_types = [
      'pdf' => [
        'enabled' => TRUE,
        'link_text' => t('Download PDF'),
        'link_attributes' => ['class' => ['button']],
      ],
    ];
    $third_party_settings_manager->setThirdPartySetting('webform_entity_print', 'export_types', $export_types);
  }

  // Add new 'Download PDF' submission action.
  _webform_entity_print_update_add_new_download_pdf_submission_action();
}

/**
 * Issue #3328205: Add print download bulk action.
 */
function webform_entity_print_update_8001() {
  // Add new 'Download PDF' action.
  $data = Yaml::decode(file_get_contents(__DIR__ . '/config/install/system.action.webform_submission_print_download_action.yml'));
  $action = Action::create($data);
  $action->trustData()->save();

  // Add new 'Download PDF' submission action.
  _webform_entity_print_update_add_new_download_pdf_submission_action();
}
