You are here

media_watermark.install in Media watermark 7

Same filename and directory in other branches
  1. 8 media_watermark.install

Media watermark module install file.

Add watermark database and add image style media_watermark.

File

media_watermark.install
View source
<?php

/**
 * @file
 * Media watermark module install file.
 *
 * Add watermark database and add image style media_watermark.
 */

/**
 * Implements hook_schema().
 */
function media_watermark_schema() {
  $schema['media_watermark'] = array(
    'description' => 'Watermark elements',
    'fields' => array(
      'wid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'size' => 'normal',
        'not null' => TRUE,
        'description' => 'Watermark id',
      ),
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'normal',
        'not null' => TRUE,
        'description' => 'Watermark file id',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Watermark Name',
      ),
      'hor_position' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'description' => 'Horizontal position',
      ),
      'ver_position' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'description' => 'Vertical position',
      ),
      'hor_margin' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'description' => 'Horizontal margin',
      ),
      'ver_margin' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => TRUE,
        'description' => 'Vertical margin',
      ),
    ),
    'primary key' => array(
      'wid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function media_watermark_install() {
  $style = image_style_save(array(
    'name' => 'media_watermark',
  ));
  $effect = array(
    'name' => 'image_scale',
    'data' => array(
      'width' => 200,
      'height' => NULL,
      'upscale' => FALSE,
    ),
    'isid' => $style['isid'],
  );
  image_effect_save($effect);

  // Need to prepare default watermarks.
  $data = array(
    'linux' => array(
      'file' => 'linux.png',
      'name' => 'png watermark',
    ),
    'drupal' => array(
      'file' => 'drupal-watermark.gif',
      'name' => 'gif watermark',
    ),
  );
  foreach ($data as $value) {
    $path = drupal_get_path('module', 'media_watermark') . '/includes/img/' . $value['file'];
    $file_temp = file_get_contents($path);
    $file_temp = file_save_data($file_temp, 'public://watermark' . $value['file'], FILE_EXISTS_RENAME);
    global $user;
    file_usage_add($file_temp, 'media_watermark', 'image', $user->uid);
    db_insert('media_watermark')
      ->fields(array(
      'fid' => $file_temp->fid,
      'name' => $value['name'],
      'hor_position' => 'left',
      'ver_position' => 'bottom',
      'hor_margin' => '0',
      'ver_margin' => '0',
    ))
      ->execute();
  }
}

/**
 * Implements hook_uninstall().
 */
function media_watermark_uninstall() {
  image_style_delete(image_style_load('media_watermark'));

  // Check for files in use by media_watermark.
  $result = db_query("SELECT fid FROM {file_usage} WHERE module = 'media_watermark'");
  foreach ($result as $record) {
    $file = file_load($record->fid);
    if ($file) {

      // Remove all usage for this file by my_module_name.
      file_usage_delete($file, 'media_watermark', NULL, NULL, 0);

      // Should only delete if file not in use by another module.
      file_delete($file);
    }
  }
}