You are here

animate_any.module in Animate Any 7

Same filename and directory in other branches
  1. 8 animate_any.module

Add CSS3 cross-browser animation to any Drupal site.

File

animate_any.module
View source
<?php

/**
 * @file
 * Add CSS3 cross-browser animation to any Drupal site.
 */

/**
 * Implements hook_menu().
 */
function animate_any_menu() {
  $items = array();
  $items['admin/config/animate_any'] = array(
    'title' => 'Animate any settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'animate_any_settings',
    ),
    'access arguments' => array(
      'access animate any',
    ),
    'weight' => 1,
  );
  $items['admin/config/animate_any/add'] = array(
    'title' => 'Add',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 2,
  );
  $items['admin/config/animate_any/list'] = array(
    'title' => 'List',
    'page callback' => 'animate_any_get_animate_list',
    'access arguments' => array(
      'access animate any',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 3,
  );
  $items['admin/config/animate_any/edit/%'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'animate_any_edit_animate_block',
      4,
    ),
    'access arguments' => array(
      'access animate any',
    ),
    'weight' => 5,
  );
  $items['admin/config/animate_any/delete/%'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'animate_any_delete_animate_block',
      4,
    ),
    'access arguments' => array(
      'access animate any',
    ),
    'weight' => 6,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function animate_any_permission() {
  return array(
    'access animate any' => array(
      'title' => t('Animate any setting page'),
      'description' => t('To access setting and add animation'),
    ),
  );
}

/**
 * Callback for animate_any_settings.
 */
function animate_any_settings($form, &$form_state) {

  // fetch animate.css from library
  $animate_css = libraries_get_path('animate.css') . '/animate.css';

  // Check animate.css file exists
  if (!file_exists($animate_css)) {
    drupal_set_message(t('animate.css library is missing.'), 'warning');
  }

  // building add more form element to add animation
  $form['#attached'] = array(
    'js' => array(
      drupal_get_path('module', 'animate_any') . '/js/form.js',
    ),
    'css' => array(
      $animate_css,
    ),
  );
  $form['parent_class'] = array(
    '#title' => 'Add Parent Class / ID',
    '#description' => t('You can add body class like <em>body.front (for front page)</em> OR class with dot(.) prefix and Id with hash(#) prefix.'),
    '#type' => 'textfield',
  );
  $form['#tree'] = TRUE;
  $form['animate_fieldset'] = array(
    '#prefix' => '<div id="item-fieldset-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'table',
    '#header' => array(),
    '#rows' => array(),
    '#attributes' => array(
      'class' => 'animation',
    ),
  );
  $form_state['field_deltas'] = isset($form_state['field_deltas']) ? $form_state['field_deltas'] : NULL;
  $field_count = $form_state['field_deltas'];
  if (isset($form_state['field_deltas'])) {
    foreach ($field_count as $delta) {
      $section_identity = array(
        '#title' => 'Add section class/Id',
        '#description' => t('Add class with dot(.) prefix and Id with hash(#) prefix.'),
        '#type' => 'textfield',
        '#size' => 20,
      );
      $section_animation = array(
        '#title' => 'Select animation',
        '#type' => 'select',
        '#options' => animate_any_options(),
        '#attributes' => array(
          'class' => array(
            'select_animate',
          ),
        ),
      );
      $animation = array(
        '#markup' => 'ANIMATE ANY',
        '#prefix' => '<h1 id="animate" class="" style="font-size: 30px;">',
        '#suffix' => '</h1>',
      );
      $remove = array(
        '#type' => 'submit',
        '#value' => t('Remove'),
        '#submit' => array(
          'animate_any_custom_add_more_remove_one',
        ),
        '#ajax' => array(
          'callback' => 'animate_any_custom_remove_callback',
          'wrapper' => 'item-fieldset-wrapper',
        ),
        '#name' => 'remove_name_' . $delta,
      );
      $form['animate_fieldset'][$delta] = array(
        'section_identity' => &$section_identity,
        'section_animation' => &$section_animation,
        'animation' => &$animation,
        'remove' => &$remove,
      );
      $form['animate_fieldset']['#rows'][$delta] = array(
        array(
          'data' => &$section_identity,
        ),
        array(
          'data' => &$section_animation,
        ),
        array(
          'data' => &$animation,
        ),
        array(
          'data' => &$remove,
        ),
      );
      unset($section_identity);
      unset($section_animation);
      unset($animation);
      unset($remove);
    }
  }
  $form['instruction'] = array(
    '#markup' => '<strong>Click on <i>Add item</i> button to add animation section.</strong>',
    '#prefix' => '<div class="form-item">',
    '#suffix' => '</div>',
  );

  // add more button and callback
  $form['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add Item'),
    '#submit' => array(
      'animate_any_custom_add_more_add_one',
    ),
    '#ajax' => array(
      'callback' => 'animate_any_custom_add_more_callback',
      'wrapper' => 'item-fieldset-wrapper',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
  );
  return $form;
}

/**
 * Submit handler for the "Remove" button.
 */
function animate_any_custom_add_more_remove_one($form, &$form_state) {
  $delta_remove = $form_state['triggering_element']['#parents'][1];
  $k = array_search($delta_remove, $form_state['field_deltas']);
  unset($form_state['field_deltas'][$k]);
  $form_state['rebuild'] = TRUE;
}
function animate_any_custom_remove_callback($form, &$form_state) {
  return $form['animate_fieldset'];
}

/**
 * Submit handler for the "Add More" button.
 */
function animate_any_custom_add_more_add_one($form, &$form_state) {
  $form_state['field_deltas'][] = count($form_state['field_deltas']) > 0 ? max($form_state['field_deltas']) + 1 : 0;
  $form_state['rebuild'] = TRUE;
}
function animate_any_custom_add_more_callback($form, $form_state) {
  return $form['animate_fieldset'];
}

/**
 * Validate the animation form.
 */
function animate_any_settings_validate($form, &$form_state) {
  if ($form_state['values']['op'] == 'Save Settings') {
    $parent = $form_state['values']['parent_class'];
    if (empty($parent)) {
      $form_state['rebuild'] = TRUE;
      form_set_error("parent_class", t("Please select parent class"));
    }

    // check all the section validation
    if (!empty($form_state['values']['animate_fieldset'])) {
      foreach ($form_state['values']['animate_fieldset'] as $key => $value) {
        if (empty($value['section_identity'])) {
          $form_state['rebuild'] = TRUE;
          form_set_error("animate_fieldset][{$key}][section_identity", t("Please select section identity for row @key", array(
            '@key' => $key,
          )));
        }
        if ($value['section_animation'] == 'none') {
          $form_state['rebuild'] = TRUE;
          form_set_error("animate_fieldset][{$key}][section_animation", t("Please select section animation for row @key", array(
            '@key' => $key,
          )));
        }
      }
    }
    else {
      $form_state['rebuild'] = TRUE;
      form_set_error("", t("Please add some section for animation"));
    }
  }
}

/**
 * Submit for animate_any_settings.
 */
function animate_any_settings_submit($form, &$form_state) {

  // encode data in json to store in db
  if ($form_state['values']['op'] == 'Save Settings') {
    $identifiers = json_encode($form_state['values']['animate_fieldset']);
    $parent = $form_state['values']['parent_class'];
    $data = db_insert('animate_any');
    $data
      ->fields(array(
      'parent' => $parent,
      'identifier' => $identifiers,
    ));
    $data
      ->execute();
    if ($data) {
      drupal_set_message(t('Animation added for @parent.', array(
        '@parent' => $parent,
      )));
    }
  }
}

/**
 * Get list of all animations.
 */
function animate_any_get_animate_list() {
  $header = array();
  $header[] = array(
    'data' => t('ID'),
  );
  $header[] = array(
    'data' => t('Parent element'),
  );
  $header[] = array(
    'data' => t('Identifiers'),
  );
  $header[] = array(
    'data' => t('Operation'),
  );

  // Fetch Animate Data.
  $fetch = db_select("animate_any", "a");
  $fetch
    ->fields('a');
  $fetch
    ->orderBy('aid', 'DESC');
  $fetch = $fetch
    ->extend('TableSort')
    ->orderByHeader($header);
  $fetch = $fetch
    ->extend('PagerDefault')
    ->limit(10);
  $fetch_results = $fetch
    ->execute()
    ->fetchAll();
  foreach ($fetch_results as $items) {
    $mini_header = array();
    $mini_header[] = array(
      'data' => t('Section'),
    );
    $mini_header[] = array(
      'data' => t('Animation'),
    );
    $mini_rows = array();
    $data = json_decode($items->identifier);
    foreach ($data as $value) {
      $mini_rows[] = array(
        $value->section_identity,
        $value->section_animation,
      );
    }
    $mini_output = array();
    $mini_output['mini_list'] = array(
      '#theme' => 'table',
      '#header' => $mini_header,
      '#rows' => $mini_rows,
    );
    $identifiers = drupal_render($mini_output);
    $edit = l(t('edit'), 'admin/config/animate_any/edit/' . $items->aid, array(
      'query' => array(
        'destination' => 'admin/config/animate_any/list',
      ),
    ));
    $delete = l(t('delete'), 'admin/config/animate_any/delete/' . $items->aid);
    $rows[] = array(
      $items->aid,
      $items->parent,
      $identifiers,
      $edit . ' / ' . $delete,
    );
  }
  $add = l(t('Add Animation'), 'admin/config/animate_any/add', array(
    'attributes' => array(
      'class' => array(
        'form-submit',
      ),
    ),
  ));
  $add_link = '<ul class="action-links"><li>' . $add . '</li></ul>';
  $output['animate_list'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => 'No record found.',
    '#prefix' => $add_link,
    '#suffix' => theme('pager'),
  );
  return drupal_render($output);
}

/**
 * Edit animation element.
 */
function animate_any_edit_animate_block($form, &$form_state, $element) {

  // fetch selected element data
  $fetch = db_select("animate_any", "a");
  $fetch
    ->fields('a');
  $fetch
    ->condition('a.aid', $element);
  $fetch_results = $fetch
    ->execute()
    ->fetchAssoc();
  $form = array();
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'animate_any') . '/js/form.js',
  );
  $form['#tree'] = TRUE;
  $form['parent_class'] = array(
    '#title' => 'Add Parent Class',
    '#description' => t('You can add body class like <em>body.front (for front page)</em> OR class with dot(.) prefix and Id with hash(#) prefix.'),
    '#type' => 'textfield',
    '#default_value' => $fetch_results['parent'],
  );
  $form['aid'] = array(
    '#type' => 'hidden',
    '#default_value' => $element,
  );
  $form['animate_fieldset'] = array(
    '#prefix' => '<div id="item-fieldset-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'table',
    '#header' => array(),
    '#rows' => array(),
    '#attributes' => array(
      'class' => 'animation',
    ),
  );

  // json decode to get json to array
  $data = json_decode($fetch_results['identifier']);
  foreach ($data as $key => $value) {
    $section_identity = array(
      '#type' => 'textfield',
      '#title' => t('Section identity'),
      '#default_value' => $value->section_identity,
      '#description' => t("Add class with dot(.) prefix and Id with hash(#) prefix."),
    );
    $section_animation = array(
      '#type' => 'select',
      '#options' => animate_any_options(),
      '#title' => t('Section Animation'),
      '#default_value' => $value->section_animation,
      '#attributes' => array(
        'class' => array(
          'select_animate',
        ),
      ),
    );
    $animation = array(
      '#markup' => 'ANIMATE ANY',
      '#prefix' => '<h1 id="animate" class="" style="font-size: 30px;">',
      '#suffix' => '</h1>',
    );
    $form['animate_fieldset'][$key] = array(
      'section_identity' => &$section_identity,
      'section_animation' => &$section_animation,
      'animation' => &$animation,
    );
    $form['animate_fieldset']['#rows'][$key] = array(
      array(
        'data' => &$section_identity,
      ),
      array(
        'data' => &$section_animation,
      ),
      array(
        'data' => &$animation,
      ),
    );
    unset($section_identity);
    unset($section_animation);
    unset($animation);
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update Settings'),
  );
  return $form;
}

/**
 * Edit Animate Block Submit Function.
 */
function animate_any_edit_animate_block_submit($form, &$form_state) {

  // update the data for current element
  $parent = $form_state['values']['parent_class'];
  $aid = $form_state['values']['aid'];
  $identifiers = json_encode($form_state['values']['animate_fieldset']);
  $data = db_merge('animate_any');
  $data
    ->key(array(
    'aid' => $aid,
  ));
  $data
    ->fields(array(
    'parent' => $parent,
    'identifier' => $identifiers,
  ));
  $valid = $data
    ->execute();
  if ($valid) {
    drupal_set_message(t('Animation settings updated.'));
  }
}

/**
 * Function to confirm animation item delete
 */
function animate_any_delete_animate_block($form, &$form_state, $aid) {
  $form['animate_aid'] = array(
    '#type' => 'hidden',
    '#value' => $aid,
  );
  return confirm_form($form, "Do you really want to delete?", 'admin/config/animate_any/list', "This action cannot be undone.", "Delete");
}

/**
 * Implements Delete Animate Submit.
 */
function animate_any_delete_animate_block_submit($form, &$form_state) {
  $animate_aid = $form_state['values']['animate_aid'];
  if (is_numeric($animate_aid)) {
    $delete = db_delete('animate_any')
      ->condition('aid', $animate_aid)
      ->execute();
    if ($delete) {
      drupal_set_message(t('Record deleted successfully.'));
    }
    drupal_goto('admin/config/animate_any/list');
  }
}

/**
 * This hook calls every where in drupal @ every page refresh
 * Implements hook_page_build().
 */
function animate_any_page_build(&$page) {

  // call every time when page loaded
  $path = drupal_get_path('module', 'animate_any');
  $animate_css = libraries_get_path('animate.css') . '/animate.css';
  if (!file_exists($animate_css)) {
    drupal_set_message(t('animate.css library is missing.'), 'warning');
  }

  // fetch all animation data
  $fetch = db_select("animate_any", "a");
  $fetch
    ->fields('a');
  $fetch_results = $fetch
    ->execute()
    ->fetchAll();
  $json_data = json_encode($fetch_results);

  // add js and css to execute animations
  drupal_add_css($animate_css);
  drupal_add_js($path . '/js/animate_any.js');

  // pass all animate data to animate_any.js in json format
  drupal_add_js(array(
    'animate_any' => array(
      'animation_data' => $json_data,
    ),
  ), array(
    'type' => 'setting',
  ));
}

/**
 * List of all Animation option provided by Animate.css.
 */
function animate_any_options() {
  return array(
    'none' => t('-- No animation --'),
    'bounce' => t('Bounce'),
    'flash' => t('Flash'),
    'pulse' => t('Pulse'),
    'rubberBand' => t('Rubber Band'),
    'shake' => t('Shake'),
    'swing' => t('Swing'),
    'tada' => t('Tada'),
    'wobble' => t('Wobble'),
    'bounceIn' => t('Bounce In'),
    'bounceInDown' => t('Bounce In Down'),
    'bounceInLeft' => t('Bounce In Left'),
    'bounceInRight' => t('Bounce In Right'),
    'bounceInUp' => t('Bounce In Up'),
    'bounceOut' => t('Bounce Out'),
    'bounceOutDown' => t('Bounce Out Down'),
    'bounceOutLeft' => t('Bounce Out Left'),
    'bounceOutRight' => t('Bounce Out Right'),
    'bounceOutUp' => t('Bounce Out Up'),
    'fadeIn' => t('Fade In'),
    'fadeInDown' => t('Fade In Down'),
    'fadeInDownBig' => t('Fade In Down Big'),
    'fadeInLeft' => t('Fade In Left'),
    'fadeInLeftBig' => t('Fade In Left Big'),
    'fadeInRight' => t('Fade In Right'),
    'fadeInRightBig' => t('Fade In Right Big'),
    'fadeInUp' => t('Fade In Up'),
    'fadeInUpBig' => t('Fade In Up Big'),
    'fadeOut' => t('Fade Out'),
    'fadeOutDown' => t('Fade Out Down'),
    'fadeOutDownBig' => t('Fade Out Down Big'),
    'fadeOutLeft' => t('Fade Out Left'),
    'fadeOutLeftBig' => t('Fade Out Left Big'),
    'fadeOutRight' => t('Fade Out Right'),
    'fadeOutRightBig' => t('Fade Out Right Big'),
    'fadeOutUp' => t('Fade Out Up'),
    'fadeOutUpBig' => t('Fade Out Up Big'),
    'flipInX' => t('Flip In X'),
    'flipInY' => t('Flip In Y'),
    'flipOutX' => t('Flip Out X'),
    'flipOutY' => t('Flip Out Y'),
    'lightSpeedIn' => t('Light Speed In'),
    'lightSpeedOut' => t('Light Speed Out'),
    'rotateIn' => t('Rotate In'),
    'rotateInDownLeft' => t('Rotate In Down Left'),
    'rotateInDownRight' => t('Rotate In Down Right'),
    'rotateInUpLeft' => t('Rotate In Up Left'),
    'rotateInUpRight' => t('Rotate In Up Right'),
    'rotateOut' => t('Rotate Out'),
    'rotateOutDownLeft' => t('Rotate Out Down Left'),
    'rotateOutDownRight' => t('Rotate Out Down Right'),
    'rotateOutUpLeft' => t('Rotate Out Up Left'),
    'rotateOutUpRight' => t('Rotate Out Up Right'),
    'hinge' => t('Hinge'),
    'rollIn' => t('Roll In'),
    'rollOut' => t('Roll Out'),
    'zoomIn' => t('Zoon In'),
    'zoomInDown' => t('Zoom In Down'),
    'zoomInLeft' => t('Zoom In Left'),
    'zoomInRight' => t('Zoom In Right'),
    'zoomInUp' => t('Zoom In Up'),
    'zoomOut' => t('Zoom Out'),
    'zoomOutDown' => t('Zoom Out Down'),
    'zoomOutLeft' => t('Zoom Out Left'),
    'zoomOutRight' => t('Zoom Out Right'),
    'zoomOutUp' => t('Zoom Out Up'),
    'slideInDown' => t('Slide In Down'),
    'slideInLeft' => t('Slide In Left'),
    'slideInRight' => t('Slide In Right'),
    'slideInUp' => t('Slide In Up'),
    'slideOutDown' => t('Slide Out Down'),
    'slideOutLeft' => t('Slide Out Left'),
    'slideOutRight' => t('Slide Out Right'),
    'slideOutUp' => t('Slide Out Up'),
  );
}

Functions

Namesort descending Description
animate_any_custom_add_more_add_one Submit handler for the "Add More" button.
animate_any_custom_add_more_callback
animate_any_custom_add_more_remove_one Submit handler for the "Remove" button.
animate_any_custom_remove_callback
animate_any_delete_animate_block Function to confirm animation item delete
animate_any_delete_animate_block_submit Implements Delete Animate Submit.
animate_any_edit_animate_block Edit animation element.
animate_any_edit_animate_block_submit Edit Animate Block Submit Function.
animate_any_get_animate_list Get list of all animations.
animate_any_menu Implements hook_menu().
animate_any_options List of all Animation option provided by Animate.css.
animate_any_page_build This hook calls every where in drupal @ every page refresh Implements hook_page_build().
animate_any_permission Implements hook_permission().
animate_any_settings Callback for animate_any_settings.
animate_any_settings_submit Submit for animate_any_settings.
animate_any_settings_validate Validate the animation form.