You are here

pdf_using_mpdf.install in PDF using mPDF 7.2

Removing all PDF set variables and checks for mPDF library existence.

File

pdf_using_mpdf.install
View source
<?php

/**
 * @file
 * Removing all PDF set variables and checks for mPDF library existence.
 */

/**
 * Implements hook_install().
 */
function pdf_using_mpdf_install() {

  // Default value of some custom varibles.
  variable_set('pdf_using_mpdf_pdf_page_size', 'A4');
  variable_set('pdf_using_mpdf_pdf_font_size', 10);
  variable_set('pdf_using_mpdf_pdf_default_font', 'DejaVuSerif');
  variable_set('pdf_using_mpdf_pdf_filename', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
  variable_get('pdf_using_mpdf', 'pdf_using_mpdf');
  $node_types = node_type_get_names();
  foreach ($node_types as $type_name => $node_type_name) {
    variable_set('pdf_using_mpdf_type_' . $type_name, 1);
  }
  drupal_load('module', 'pdf_using_mpdf');
}

/*
 * Implements hook_enable().
 */
function pdf_using_mpdf_enable() {
  pdf_using_mpdf_create_folder();
}

/**
 * Implements hook_uninstall().
 */
function pdf_using_mpdf_uninstall() {

  // Removing all custom varible.
  db_query("DELETE FROM {variable} WHERE name LIKE 'pdf_using_mpdf_%'");
  pdf_using_mpdf_clear_folder(NULL, TRUE);
}

/**
 * Implements hook_requirements().
 */
function pdf_using_mpdf_requirements($phase) {
  $requirements = array();

  // Ensure translations do not break at install time.
  $t = get_t();
  if (module_exists('libraries') == TRUE) {

    // Search for mpdf tool first.
    $pattern = '/^mpdf.php$/';

    // Libraries module to detect mPDF library in case of multisite installation.
    $tools = array_keys(file_scan_directory(libraries_get_path('mpdf'), $pattern));

    // mPDF library looked for in the module directory itself.
    $tools = array_merge($tools, array_keys(file_scan_directory(drupal_get_path('module', 'pdf_using_mpdf'), $pattern)));
  }
  if (isset($tools[0])) {
    $requirements['pdf_using_mpdf_library_exist'] = array(
      'title' => $t('PDF using mPDF Library'),
      'value' => $t('Installed'),
      'severity' => REQUIREMENT_OK,
    );
  }
  else {
    $requirements['pdf_using_mpdf_library_exist'] = array(
      'title' => $t('PDF using mPDF Library'),
      'value' => $t('Not found !'),
      'severity' => REQUIREMENT_WARNING,
      'description' => $t('<p>No mPDF Library Found in "sites/all/libraries" or "!default_module_path". Please download the PHP mPDF Library API from <a href="http://www.mpdf1.com/">mPDF1.com</a> </p>', array(
        '!default_module_path' => drupal_get_path('module', 'pdf_using_mpdf'),
      )),
    );
  }
  return $requirements;
}

/**
 * Removes all pdf file and delete the folder.
 *
 * @param class $pdfmpdf
 *   A variable holding standard class.
 *
 * @param boolean #delete
 *   Variable indicating whether to delete folder permanently.
 */
function pdf_using_mpdf_clear_folder(stdClass $pdfmpdf = NULL, $delete = FALSE) {
  $folder = variable_get('pdf_using_mpdf', 'pdf_using_mpdf');
  $folder = file_build_uri($folder);
  return pdf_using_mpdf_delete_recursive($folder, $delete);
}

/**
 * Deletes all files and directories in the specified filepath recursively.
 *
 * If the specified path is a file then it will be passed to
 * file_unmanaged_delete().
 *
 * Note that this only deletes visible files with write permission.
 *
 * @param string $folder_pth
 *   A string containing either an URI or a file or directory path.
 *
 * @param boolean $delete_permanently
 *   A boolean value indicating wheteher to delete folder permanently or not.
 */
function pdf_using_mpdf_delete_recursive($folder_pth, $delete_permanently = FALSE) {
  $folder_pth = drupal_realpath($folder_pth);
  if (is_dir($folder_pth)) {
    $dir = dir($folder_pth);
    while (($cs_file = $dir
      ->read()) !== FALSE) {

      // condition to avoid deleting of peer folders
      if ($cs_file != '.' && $cs_file != '..') {
        $cs_file_path = $folder_pth . '/' . $cs_file;
        file_unmanaged_delete_recursive($cs_file_path, TRUE);
      }
    }
    $dir
      ->close();
    return $delete_permanently ? drupal_rmdir($folder_pth) : TRUE;
  }
}

Functions

Namesort descending Description
pdf_using_mpdf_clear_folder Removes all pdf file and delete the folder.
pdf_using_mpdf_delete_recursive Deletes all files and directories in the specified filepath recursively.
pdf_using_mpdf_enable
pdf_using_mpdf_install Implements hook_install().
pdf_using_mpdf_requirements Implements hook_requirements().
pdf_using_mpdf_uninstall Implements hook_uninstall().