You are here

epsacrop.install in EPSA Crop - Image Cropping 7.2

Same filename and directory in other branches
  1. 8.2 epsacrop.install
  2. 6.2 epsacrop.install
  3. 6 epsacrop.install

install file for epsacrop.module

File

epsacrop.install
View source
<?php

/**
 * @file
 * install file for epsacrop.module
*/

/**
 * Implementation of hook_schema
 * 
 * @access public
 * @return array
 */
function epsacrop_schema() {
  $schema['epsacrop_files'] = array(
    'description' => 'TODO: please describe this table!',
    'fields' => array(
      'fid' => array(
        'description' => 'TODO: please describe this field!',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'coords' => array(
        'description' => 'TODO: please describe this field!',
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'fid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_requirements.
 *
 * @access public
 * @param string $phase
 * @return array
 */
function epsacrop_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if ($phase == 'install') {

    // The _epsacrop_jcrop_get_version() function is in the .module file, which isn't
    // loaded yet.
    include_once dirname(__FILE__) . '/epsacrop.module';
  }
  if (!function_exists('libraries_get_path')) {
    include_once drupal_get_path('module', 'libraries') . '/libraries.module';
  }
  $requirements['epsacrop_jcrop']['title'] = $t('EPSA Crop : Jcrop');
  $requirements['epsacrop_json2']['title'] = $t('EPSA Crop : json2.js');
  if ($version = _epsacrop_jcrop_get_version() || _epsacrop_is_json2_exists(FALSE)) {
    $version = _epsacrop_jcrop_get_version();

    // Everything looks good; display the current Jcrop version.
    if ($version) {
      $requirements['epsacrop_jcrop']['value'] = 'Jcrop version : ' . $version;
      $requirements['epsacrop_jcrop']['severity'] = REQUIREMENT_OK;
    }
    if (_epsacrop_is_json2_exists(FALSE)) {
      $requirements['epsacrop_json2']['value'] = 'JSON file (json2.js) OK';
      $requirements['epsacrop_json2']['severity'] = REQUIREMENT_OK;
    }
  }
  else {

    // Required libraies wasn't found. Abort installation.
    if (!_epsacrop_jcrop_get_version()) {
      $requirements['epsacrop_jcrop']['value'] = $t('Not found');
      $requirements['epsacrop_jcrop']['description'] = $t('The <a href="@jcrop">Jcrop</a> plugin is missing. <a href="@download">Download</a> and extract it in your <em>sites/all/libraries</em> directory.', array(
        '@jcrop' => 'http://deepliquid.com/content/Jcrop.html',
        '@download' => 'https://github.com/tapmodo/Jcrop/archive/master.zip',
      ));
      $requirements['epsacrop_jcrop']['severity'] = REQUIREMENT_ERROR;
    }
    if (!_epsacrop_is_json2_exists(FALSE)) {
      $requirements['epsacrop_json2']['value'] = $t('Not found');
      $requirements['epsacrop_json2']['description'] = $t("The <a href='@json2'>json2.js</a> file is missing, you can download it <a href='@downjson2'>here</a> and put it in your <em>sites/all/libraries</em> directory. Don't forget rename the downloaded folder into json2. At the end, you should have something like this sites/all/libraries/json2", array(
        '@json2' => 'http://www.json.org/js.html',
        '@downjson2' => 'https://github.com/douglascrockford/JSON-js',
      ));
      $requirements['epsacrop_json2']['severity'] = REQUIREMENT_ERROR;
    }
  }
  return $requirements;
}

/**
 * Implementation of hook_install.
 * 
 * @access public
 * @return void
 */
function epsacrop_install() {
  db_update('system')
    ->fields(array(
    'weight' => 10,
  ))
    ->condition('name', 'epsacrop')
    ->execute();
}

/**
 * Implementation of hook_uninstall.
 * 
 * @access public
 * @return void
 */
function epsacrop_uninstall() {
}

/**
 * Converts all the epsacrop coordinates to a new machine_name format.
 * Issue related: http://drupal.org/node/1396500
 */
function epsacrop_update_7201() {

  // Get all the EPSACROP files
  $result = db_select('epsacrop_files', 'ef')
    ->fields('ef', array())
    ->execute()
    ->fetchAllKeyed();

  // Get all the EPSACROP image styles
  $epsacrop_styles = _epsacrop_load_styles();
  foreach ($result as $fid => $coords) {

    // Old coordenates
    $coords_old = drupal_json_decode(unserialize($coords));

    // New Coordenates
    $coords_new = array();

    // Find the right coords
    foreach ($coords_old as $key => $item) {
      if (!empty($item)) {

        // Iterates each crop coords by image style
        foreach ($item as $name_old => $data) {

          // Extract the ieid and the isid
          preg_match("/epsacrop\\-([0-9]*)\\-([0-9]*)/", $name_old, $matches);
          $ieid = $matches[1];
          $isid = $matches[2];

          // Find the machine name of the image style by the isid
          $style_name = '';
          foreach ($epsacrop_styles as $style) {
            if ($style['isid'] == $isid) {
              $style_name = $style['name'];
            }
          }

          // Rename the object name with the new format
          if (!empty($style_name)) {
            $coords_new[$key]['epsacrop-' . $style_name] = $data;
          }
        }
      }
      else {
        $coords_new[$key] = NULL;
      }
    }
    if (!empty($coords_new)) {

      // Encode the new coordenates
      $coords_new = serialize(drupal_json_encode($coords_new));

      // Save the changes
      $record = array(
        'fid' => $fid,
        'coords' => $coords_new,
      );
      drupal_write_record('epsacrop_files', $record, 'fid');
    }
    else {

      // No coordinates were found, probably because the image styles couldn't
      // be found based on the id. Remove the coordinates data from the
      // database.
      db_delete('epsacrop_files')
        ->condition('fid', $fid)
        ->execute();
    }
  }
}

/**
 * Clean the database with all the NULL entries
 * Issue related https://www.drupal.org/node/1823940
 */
function epsacrop_update_7202() {
  $result = db_query("SELECT * FROM {epsacrop_files}");
  if ($result) {
    while ($row = $result
      ->fetchAssoc()) {
      $flag = FALSE;
      $coords = unserialize($row['coords']);
      $coords = json_decode($coords);
      foreach ($coords as $k => $coord) {
        if ($coord == NULL) {
          unset($coords[$k]);
          $flag = TRUE;
        }
      }
      if ($flag) {
        $coords = json_encode($coords);
        $data = serialize($coords);
        db_update('epsacrop_files')
          ->fields(array(
          'coords' => $data,
        ))
          ->condition('fid', $row['fid'], '=')
          ->execute();
      }
    }
  }
}

Functions

Namesort descending Description
epsacrop_install Implementation of hook_install.
epsacrop_requirements Implementation of hook_requirements.
epsacrop_schema Implementation of hook_schema
epsacrop_uninstall Implementation of hook_uninstall.
epsacrop_update_7201 Converts all the epsacrop coordinates to a new machine_name format. Issue related: http://drupal.org/node/1396500
epsacrop_update_7202 Clean the database with all the NULL entries Issue related https://www.drupal.org/node/1823940