From 03e1ef7e80050e91027ee691f61c2f9c868a3f6e Mon Sep 17 00:00:00 2001
From: Kevin Kaland <kevin@wizonesolutions.com>
Date: Wed, 22 Oct 2014 00:25:14 +0200
Subject: [PATCH] Convert admin settings form.

---
 config/install/fillpdf.settings.yml  |   1 +
 fillpdf.admin.inc                    |  80 --------------------
 fillpdf.css                          |   4 +
 fillpdf.libraries.yml                |  10 +++
 fillpdf.links.menu.yml               |   5 ++
 fillpdf.module                       |  50 ++-----------
 fillpdf.permissions.yml              |  11 +++
 fillpdf.routing.yml                  |   7 ++
 src/Component/Utility/FillPdf.php    |  24 ++++++
 src/Controller/DefaultController.php |  15 ++++
 src/Form/FillpdfSettingsForm.php     | 107 +++++++++++++++++++++++++++
 11 files changed, 192 insertions(+), 122 deletions(-)
 create mode 100644 config/install/fillpdf.settings.yml
 create mode 100644 fillpdf.css
 create mode 100644 fillpdf.libraries.yml
 create mode 100644 fillpdf.links.menu.yml
 create mode 100644 fillpdf.permissions.yml
 create mode 100644 fillpdf.routing.yml
 create mode 100644 src/Component/Utility/FillPdf.php
 create mode 100644 src/Controller/DefaultController.php
 create mode 100644 src/Form/FillpdfSettingsForm.php

diff --git a/config/install/fillpdf.settings.yml b/config/install/fillpdf.settings.yml
new file mode 100644
index 0000000..99356e0
--- /dev/null
+++ b/config/install/fillpdf.settings.yml
@@ -0,0 +1 @@
+fillpdf_remote_protocol: https
diff --git a/fillpdf.admin.inc b/fillpdf.admin.inc
index 342ff73..e65dbf4 100644
--- a/fillpdf.admin.inc
+++ b/fillpdf.admin.inc
@@ -15,87 +15,7 @@ define('FILLPDF_REPLACEMENTS_DESCRIPTION', t("<p>Tokens, such as those from CCK,
   been used. <p>Note that omitting the <em>replacement value</em> will replace <em>original value</em>
   with a blank, essentially erasing it.</p>"));
 
-/* ---------------- Configuration --------------------*/
-
-/**
- * Settings form for user to place API Key
- */
-function fillpdf_settings($form, &$form_state) {
-  $fillpdf_service = variable_get('fillpdf_service');
-
-  // Assemble service options. Warning messages will be added next as needed.
-  $options = array(
-    'pdftk' => t('Use locally-installed pdftk: You will need a VPS or a dedicated server so you can install pdftk: (!see_documentation).', array('!see_documentation' => l(t('see documentation'),  'http://drupal.org/documentation/modules/fillpdf'))),
-    'local' => t('Use locally-installed PHP/JavaBridge: You will need a VPS or dedicated server so you can deploy PHP/JavaBridge on Apache Tomcat: (!see_documentation).', array('!see_documentation' => l(t('see documentation'),  'http://drupal.org/documentation/modules/fillpdf'))),
-    'remote' => t('Use FillPDF Service: Sign up for <a href="https://fillpdf-service.com">FillPDF Service</a>.'),
-  );
-
-  // Check for JavaBridge.
-  if (!(file_exists(drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc'))) {
-    $options['local'] .= '<div class="messages warning">' . t('JavaBridge is not installed locally.') . '</div>';
-  }
-
-  // Check for pdftk.
-  $status = fillpdf_pdftk_check(fillpdf_pdftk_path());
-  if ($status === FALSE) {
-    $options['pdftk'] .= '<div class="messages warning">' . t('pdftk is not properly installed.') . '</div>';
-  }
-
-  $form['fillpdf_service'] = array(
-    '#type' => 'radios',
-    '#title' => t('PDF-filling service'),
-    '#description' => t('This module requires the use of one of several external PDF manipulation tools. Choose the service you would like to use.'),
-    '#default_value' => $fillpdf_service,
-    '#options' => $options,
-  );
-  $form['remote'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Configure FillPDF Service'),
-    '#collapsible' => TRUE,
-    '#collapsed' => $fillpdf_service !== 'remote',
-  );
-  $form['remote']['fillpdf_api_key'] = array(
-    '#type' => 'textfield',
-    '#title' => t('API Key'),
-    '#default_value' => variable_get('fillpdf_api_key', ''),
-    '#description' => t('You need to sign up for an API key at <a href="https://fillpdf-service.com">FillPDF Service</a>'),
-  );
-  $form['remote']['fillpdf_remote_protocol'] = array(
-    '#type' => 'radios',
-    '#title' => t('Use HTTPS?'),
-    '#description' => t('It is recommended to select <em>Use HTTPS</em> for this option. Doing so will help prevent
-      sensitive information in your PDFs from being intercepted in transit between your server and the remote service.'),
-    '#default_value' => variable_get('fillpdf_remote_protocol', 'https'),
-    '#options' => array(
-      'https' => t('Use HTTPS'),
-      'http' => t('Do not use HTTPS'),
-      ),
-  );
-  $form['fillpdf_pdftk_path'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Configure path to pdftk'),
-    '#description' => t("If FillPDF is not detecting your pdftk installation, you can specify the full path to the program here. Include the program name as well. For example, <em>/usr/bin/pdftk</em> is a valid value. You can almost always leave this field blank. If you should set it, you'll probably know."),
-    '#default_value' => variable_get('fillpdf_pdftk_path'),
-  );
-
-  drupal_add_js(drupal_get_path('module', 'fillpdf') . '/fillpdf.js');
-
-  return system_settings_form($form);
-}
-
-function fillpdf_settings_validate($form, &$form_state) {
-  if ($form_state['values']['fillpdf_pdftk_path']) {
-    $status = fillpdf_pdftk_check($form_state['values']['fillpdf_pdftk_path']);
-    if ($status === FALSE) {
-      form_set_error('fillpdf_pdftk_path', t('The path you have entered for
-      <em>pdftk</em> is invalid. Please enter a valid path.'));
-    }
-  }
-}
-
-
 /* ---------------- Form Create --------------------*/
-
 /**
  * Manage your existing forms, or upload a new one
  */
diff --git a/fillpdf.css b/fillpdf.css
new file mode 100644
index 0000000..1ec7dd4
--- /dev/null
+++ b/fillpdf.css
@@ -0,0 +1,4 @@
+/* @todo: Do this without nested selector */
+fieldset.collapsed {
+  display: none;
+}
diff --git a/fillpdf.libraries.yml b/fillpdf.libraries.yml
new file mode 100644
index 0000000..c160d7f
--- /dev/null
+++ b/fillpdf.libraries.yml
@@ -0,0 +1,10 @@
+fillpdf.admin.settings:
+  version: VERSION
+  js:
+    fillpdf.js: {}
+  css:
+    component:
+      fillpdf.css: {}
+  dependencies:
+    - core/jquery
+    - core/drupal
diff --git a/fillpdf.links.menu.yml b/fillpdf.links.menu.yml
new file mode 100644
index 0000000..94086f5
--- /dev/null
+++ b/fillpdf.links.menu.yml
@@ -0,0 +1,5 @@
+fillpdf.settings:
+  title: FillPDF settings
+  description: "Configure tool to use with FillPDF."
+  route_name: fillpdf.settings
+  parent: system.admin_config_media
\ No newline at end of file
diff --git a/fillpdf.module b/fillpdf.module
index ebb21de..3fd1c64 100644
--- a/fillpdf.module
+++ b/fillpdf.module
@@ -5,7 +5,9 @@
  * Allows mappings of PDFs to site content
  */
 
-define("DEFAULT_SERVLET_URL", variable_get('fillpdf_remote_protocol', 'http') . "://" . variable_get('fillpdf_remote_endpoint', "fillpdf-service.com/xmlrpc.php"));
+// @todo: Convert this after settings page
+//define("DEFAULT_SERVLET_URL", variable_get('fillpdf_remote_protocol', 'http') . "://" . variable_get('fillpdf_remote_endpoint', "fillpdf-service.com/xmlrpc.php"));
+define("DEFAULT_SERVLET_URL", 'https://fillpdf-service.com/xmlrpc.rpc'); // TODO: temporary
 module_load_include('inc', 'fillpdf', 'fillpdf.admin');
 
 /**
@@ -31,6 +33,8 @@ function fillpdf_help($path, $arg) {
 
 /**
  * Implements hook_menu().
+ *
+ * @todo: Remove once converted.
  */
 function fillpdf_menu() {
   $access = array('administer pdfs');
@@ -103,27 +107,6 @@ function fillpdf_menu() {
   return $items;
 }
 
-/**
- * Implements hook_permission().
- */
-function fillpdf_permission() {
-  return array(
-    'administer pdfs' => array(
-      'title' => t('Administer PDFs'),
-      'description' => t('Allows usage of the FillPDF administration screen.'),
-    ),
-    'publish own pdfs' => array(
-      'title' => t('Publish Own PDFs'),
-      'description' => t("Allows filling in and downloading PDFs with one's own site content."),
-    ),
-    'publish all pdfs' => array(
-      'title' => t('Publish All PDFs'),
-      'description' => t('Allows filling in and downloading PDFs with any site content.'),
-    ),
-  );
-}
-
-
 /**
  * Gets a link to the prinable PDF, merged with the passed-in data
  * @param array/int $nids or $nid, if you pass in one value it will merge with that node.
@@ -1013,27 +996,10 @@ function fillpdf_load($fid, $reset = FALSE) {
   }
 }
 
-function fillpdf_pdftk_check($pdftk_path = 'pdftk') {
-  // An empty value means we should leave it to the PATH.
-  if (empty($pdftk_path)) {
-    $pdftk_path = 'pdftk';
-  }
-  $output = array();
-  $status = NULL;
-  exec($pdftk_path, $output, $status);
-  if (in_array($status, array(126, 127))) {
-    return FALSE;
-  }
-  return TRUE;
-}
-
 // This is a convenience wrapper around variable_get(). It lets us **also**
 // return "pdftk" if the variable is empty, not just unset.
 function fillpdf_pdftk_path() {
-  $path_to_pdftk = variable_get('fillpdf_pdftk_path', 'pdftk');
-  if ($path_to_pdftk) {
-    return $path_to_pdftk;
-  }
-  return 'pdftk';
+  // @todo: Move this to \Drupal\fillpdf\Component\Utility\FillPdf or somewhere depending on how I wind up using it in the main code. I wasn't sure at first, so I just left it procedural, but I would prefer, WORST-CASE, to pass in the $config object.
+  $path_to_pdftk = \Drupal::config('fillpdf.settings')->get('fillpdf_pdftk_path');
+  return $path_to_pdftk;
 }
-
diff --git a/fillpdf.permissions.yml b/fillpdf.permissions.yml
new file mode 100644
index 0000000..2ae4ebc
--- /dev/null
+++ b/fillpdf.permissions.yml
@@ -0,0 +1,11 @@
+administer pdfs:
+  title: 'Administer PDFs'
+  description: 'Allows usage of FillPDF administration screens.'
+
+publish own pdfs:
+  title: 'Publish own PDFs'
+  description: "Allows filling in and downloading PDFs with one's own site content."
+
+publish all pdfs:
+  title: 'Publish all PDFs'
+  description: 'Allows filling in and downloading PDFs with any site content.'
\ No newline at end of file
diff --git a/fillpdf.routing.yml b/fillpdf.routing.yml
new file mode 100644
index 0000000..85eccfa
--- /dev/null
+++ b/fillpdf.routing.yml
@@ -0,0 +1,7 @@
+fillpdf.settings:
+  path: '/admin/config/media/fillpdf'
+  defaults:
+    _form: '\Drupal\fillpdf\Form\FillpdfSettingsForm'
+    _title: 'FillPDF settings'
+  requirements:
+    _permission: 'administer pdfs'
diff --git a/src/Component/Utility/FillPdf.php b/src/Component/Utility/FillPdf.php
new file mode 100644
index 0000000..bf7d588
--- /dev/null
+++ b/src/Component/Utility/FillPdf.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\fillpdf\Component\Utility\Fillpdf.
+ */
+namespace Drupal\fillpdf\Component\Utility;
+use \Symfony\Component\Process\Process;
+
+class FillPdf {
+  public static function checkPdftkPath($pdftk_path = '') {
+    // An empty value means we should leave it to the PATH.
+    if (empty($pdftk_path)) {
+      $pdftk_path = 'pdftk';
+    }
+    $status = NULL;
+    $process = new Process($pdftk_path);
+    $process->run();
+
+    if (in_array($process->getExitCode(), array(126, 127))) {
+      return FALSE;
+    }
+    return TRUE;
+  }
+}
\ No newline at end of file
diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php
new file mode 100644
index 0000000..1190979
--- /dev/null
+++ b/src/Controller/DefaultController.php
@@ -0,0 +1,15 @@
+<?php /**
+ * @file
+ * Contains \Drupal\fillpdf\Controller\DefaultController.
+ */
+
+namespace Drupal\fillpdf\Controller;
+
+use Drupal\Core\Controller\ControllerBase;
+
+/**
+ * Default controller for the fillpdf module.
+ */
+class DefaultController extends ControllerBase {
+
+}
diff --git a/src/Form/FillpdfSettingsForm.php b/src/Form/FillpdfSettingsForm.php
new file mode 100644
index 0000000..7c23d3c
--- /dev/null
+++ b/src/Form/FillpdfSettingsForm.php
@@ -0,0 +1,107 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\fillpdf\Form\FillpdfSettingsForm.
+ */
+namespace Drupal\fillpdf\Form;
+use Drupal\Core\Form\ConfigFormBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Url;
+
+use Drupal\fillpdf\Component\Utility\Fillpdf;
+
+class FillpdfSettingsForm extends ConfigFormBase {
+  public function getFormId() {
+    return 'fillpdf_settings';
+  }
+
+  public function buildForm(array $form, FormStateInterface $form_state) {
+    $config = $this->config('fillpdf.settings');
+    $fillpdf_service = $config->get('fillpdf_service');
+
+    // Assemble service options. Warning messages will be added next as needed.
+    $options = array(
+      'pdftk' => $this->t('Use locally-installed pdftk: You will need a VPS or a dedicated server so you can install pdftk: (!see_documentation).', array('!see_documentation' => $this->l($this->t('see documentation'),  Url::fromUri('http://drupal.org/documentation/modules/fillpdf')))),
+      'local' => $this->t('Use locally-installed PHP/JavaBridge: You will need a VPS or dedicated server so you can deploy PHP/JavaBridge on Apache Tomcat: (!see_documentation).', array('!see_documentation' => $this->l($this->t('see documentation'),  Url::fromUri('http://drupal.org/documentation/modules/fillpdf')))),
+      'remote' => $this->t('Use FillPDF Service: Sign up for <a href="https://fillpdf-service.com">FillPDF Service</a>.'),
+    );
+
+    // Check for JavaBridge.
+    if (!(file_exists(drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc'))) {
+      $options['local'] .= '<div class="messages warning">' . $this->t('JavaBridge is not installed locally.') . '</div>';
+    }
+
+    // Check for pdftk.
+    $status = FillPdf::checkPdftkPath(fillpdf_pdftk_path());
+    if ($status === FALSE) {
+      $options['pdftk'] .= '<div class="messages warning">' . $this->t('pdftk is not properly installed.') . '</div>';
+    }
+
+    $form['fillpdf_service'] = array(
+      '#type' => 'radios',
+      '#title' => $this->t('PDF-filling service'),
+      '#description' => $this->t('This module requires the use of one of several external PDF manipulation tools. Choose the service you would like to use.'),
+      '#default_value' => $fillpdf_service,
+      '#options' => $options,
+    );
+    $form['remote'] = array(
+      '#type' => 'fieldset',
+      '#title' => $this->t('Configure FillPDF Service'),
+      '#collapsible' => TRUE,
+      '#collapsed' => $fillpdf_service !== 'remote',
+    );
+    $form['remote']['fillpdf_api_key'] = array(
+      '#type' => 'textfield',
+      '#title' => $this->t('API Key'),
+      '#default_value' => $config->get('fillpdf_api_key', ''),
+      '#description' => $this->t('You need to sign up for an API key at <a href="https://fillpdf-service.com">FillPDF Service</a>'),
+    );
+    $form['remote']['fillpdf_remote_protocol'] = array(
+      '#type' => 'radios',
+      '#title' => $this->t('Use HTTPS?'),
+      '#description' => $this->t('It is recommended to select <em>Use HTTPS</em> for this option. Doing so will help prevent
+      sensitive information in your PDFs from being intercepted in transit between your server and the remote service.'),
+      '#default_value' => $config->get('fillpdf_remote_protocol'),
+      '#options' => array(
+        'https' => $this->t('Use HTTPS'),
+        'http' => $this->t('Do not use HTTPS'),
+      ),
+    );
+    $form['fillpdf_pdftk_path'] = array(
+      '#type' => 'textfield',
+      '#title' => $this->t('Configure path to pdftk'),
+      '#description' => $this->t("If FillPDF is not detecting your pdftk installation, you can specify the full path to the program here. Include the program name as well. For example, <em>/usr/bin/pdftk</em> is a valid value. You can almost always leave this field blank. If you should set it, you'll probably know."),
+      '#default_value' => $config->get('fillpdf_pdftk_path'),
+    );
+
+    $form['#attached'] = array('library' => array('fillpdf/fillpdf.admin.settings'));
+
+    return parent::buildForm($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function validateForm(array &$form, FormStateInterface $form_state) {
+    if ($form_state->getValue('fillpdf_pdftk_path')) {
+      $status = FillPdf::checkPdftkPath($form_state->getValue('fillpdf_pdftk_path'));
+      if ($status === FALSE) {
+        $form_state->setErrorByName('fillpdf_pdftk_path', $this->t('The path you have entered for
+      <em>pdftk</em> is invalid. Please enter a valid path.'));
+      }
+    }
+  }
+
+  public function submitForm(array &$form, FormStateInterface $form_state) {
+    // Save form values.
+    $this->config('fillpdf.settings')
+      ->set('fillpdf_service', $form_state->getValue('fillpdf_service'))
+      ->set('fillpdf_api_key', $form_state->getValue('fillpdf_api_key'))
+      ->set('fillpdf_remote_protocol', $form_state->getValue('fillpdf_remote_protocol'))
+      ->set('fillpdf_pdftk_path', $form_state->getValue('fillpdf_pdftk_path'))
+      ->save();
+
+    parent::submitForm($form, $form_state);
+  }
+}
-- 
GitLab