<?php

/* This module created by @Tabvn (http://envato.tabvn.com), Can not copy or use without our permission!. */


/*
 * Implements of hook_menu()
 */

function parallax_breadcrumb_menu() {
  $items = array();

  $items['admin/structure/parallax-breadcrumb'] = array(
      'title' => 'Parallax breadcrumb',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('parallax_breadcrumb_form'),
      'access arguments' => array('administer parallax breadcrumb'),
  );

  return $items;
}

/**
 * Implements of hook_block_info()
 */
function parallax_breadcrumb_block_info() {

  $blocks['parallax_breadcrumb'] = array(
      'info' => t('[Tabvn] parallax breadcrumb'),
          // DRUPAL_CACHE_PER_ROLE will be assumed.
  );

  return $blocks;
}

/**
 * Implements of hook_block_view()
 */
function parallax_breadcrumb_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'parallax_breadcrumb':
      $breadcrumb = variable_get('parallax_breadcrumb', array());
      $current_path = current_path();
      $item = _parallax_breadcrumb_get_brreadcrumb_display($current_path, $breadcrumb);
      if (!empty($item)) {
        $module_path = drupal_get_path('module', 'parallax_breadcrumb');
        drupal_add_js($module_path . '/js/jquery.pureparallax.js');
        drupal_add_js($module_path . '/js/parallax_breadcrumb.js');
      }
      $block['subject'] = '';
      $block['content'] = theme('parallax_breadcrumb', array('item' => $item));

      break;
  }
  return $block;
}

/**
 * Implements of hook_theme()
 */
function parallax_breadcrumb_theme($existing, $type, $theme, $path) {
  return array(
      'parallax_breadcrumb' => array(
          'variables' => array('item' => NULL),
          'template' => 'parallax_breadcrumb',
      ),
  );
}

function _parallax_breadcrumb_get_brreadcrumb_display($current_path, $breadcrumb) {

  $path_alias = drupal_lookup_path('alias', $current_path);
  $return_value = FALSE;
  if (!empty($breadcrumb['items'])) {
    foreach ($breadcrumb['items'] as $item) {
      $patterns = $item['visibility'];
      if (drupal_match_path($current_path, $patterns) || drupal_match_path($path_alias, $patterns)) {
        $return_value = $item;
        return $return_value;
      }
    }
  }
  return $return_value;
}

/**
 * Implements of hook_permission()
 */
function parallax_breadcrumb_permission() {
  return array(
      'administer parallax breadcrumb' => array(
          'title' => t('Administer parallax breadcrumb'),
          'description' => t('Perform administration tasks for parallax breadcrumb.'),
      ),
  );
}

function parallax_breadcrumb_form($form, &$form_state) {
  $breadcrumbs = variable_get('parallax_breadcrumb');

  if (!empty($breadcrumbs['items'])) {
    foreach ($breadcrumbs['items'] as $key => $item) {
      $key_id = $item['id'];
      $form['parallax_breadcrumb']['items']['#tree'] = TRUE;
      $num = $key + 1;
      $form['parallax_breadcrumb']['items'][$key] = array(
          '#type' => 'fieldset',
          '#title' => '<small>#' . $num . '</small> ' . $item['title'],
          '#weight' => $key,
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#tree' => TRUE,
      );

      $form['parallax_breadcrumb']['items'][$key]['id'] = array(
          '#type' => 'value',
          '#default_value' => $key_id,
      );
      $form['parallax_breadcrumb']['items'][$key]['delete'] = array(
          '#type' => 'checkbox',
          '#title' => t('Delete'),
      );
      $form['parallax_breadcrumb']['items'][$key]['background'] = array(
          '#type' => 'managed_file',
          '#title' => t('Background'),
          '#description' => t('Allowed extensions: gif png jpg jpeg'),
          '#upload_location' => 'public://',
          '#default_value' => $item['background'],
          '#upload_validators' => array(
              'file_validate_extensions' => array('gif png jpg jpeg'),
              // Pass the maximum file size in bytes
              'file_validate_size' => array(10 * 3024 * 3024),
          ),
      );
      $form['parallax_breadcrumb']['items'][$key]['title'] = array(
          '#title' => t('Title'),
          '#type' => 'textfield',
          '#description' => t('Page title'),
          '#default_value' => $item['title'],
      );
      $form['parallax_breadcrumb']['items'][$key]['use_default_title'] = array(
          '#title' => t('Use default page title'),
          '#type' => 'checkbox',
          '#default_value' => $item['use_default_title'],
      );
      $form['parallax_breadcrumb']['items'][$key]['sub_title'] = array(
          '#title' => t('Sub title'),
          '#type' => 'textfield',
          '#description' => t('Sub page title'),
          '#default_value' => $item['sub_title'],
      );


      $form['parallax_breadcrumb']['items'][$key]['visibility'] = array(
          '#title' => t('Show breadcrumb on specific pages'),
          '#type' => 'textarea',
          '#default_value' => $item['visibility'],
          '#description' => t('Specify pages by using their paths. Enter one path per line. The \'*\' character is a wildcard. Example paths are '),
      );
    }
  }
  $max_id = !empty($breadcrumbs['items']) ? count($breadcrumbs['items']) : 0;

  $form['parallax_breadcrumb']['new'] = array(
      '#type' => 'fieldset',
      '#title' => t('Add new'),
      '#weight' => 3,
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
  );
  $form['parallax_breadcrumb']['new']['id'] = array(
      '#type' => 'value',
      '#default_value' => $max_id,
  );
  $form['parallax_breadcrumb']['new']['background'] = array(
      '#type' => 'managed_file',
      '#title' => t('Background'),
      '#description' => t('Allowed extensions: gif png jpg jpeg'),
      '#upload_location' => 'public://',
      '#upload_validators' => array(
          'file_validate_extensions' => array('gif png jpg jpeg'),
          // Pass the maximum file size in bytes
          'file_validate_size' => array(10 * 3024 * 3024),
      ),
  );
  $form['parallax_breadcrumb']['new']['title'] = array(
      '#title' => t('Title'),
      '#type' => 'textfield',
      '#description' => t('Page title'),
  );
  $form['parallax_breadcrumb']['new']['use_default_title'] = array(
      '#title' => t('Use default page title'),
      '#type' => 'checkbox',
  );
  $form['parallax_breadcrumb']['new']['sub_title'] = array(
      '#title' => t('Sub title'),
      '#type' => 'textfield',
      '#description' => t('Sub page title'),
  );


  $form['parallax_breadcrumb']['new']['visibility'] = array(
      '#title' => t('Show breadcrumb on specific pages'),
      '#type' => 'textarea',
      '#description' => t('Specify pages by using their paths. Enter one path per line. The \'*\' character is a wildcard. Example paths are '),
  );

  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
  );

  return $form;
}

function parallax_breadcrumb_form_validate($form, &$form_state) {

  $values = $form_state['values'];
  if (!empty($values['new']['background']) || !empty($values['new']['sub_title']) || !empty($values['new']['visibility'])) {
    if (empty($values['new']['title'])) {
      form_set_error('new][title', t('Title is required!'));
    }
  }
}

function parallax_breadcrumb_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  // update current items

  if (!empty($values['items'])) {
    $breadcrumb = variable_get('parallax_breadcrumb');
    foreach ($values['items'] as $key => $item) {
      $old_key = _parallax_breadcrumb_get_key_by_id($item['id']);
      $delete = $item['delete'];
      if ($delete) {
        if (!empty($breadcrumb['items'][$old_key]['background'])) {
          $fid = $breadcrumb['items'][$old_key]['background'];

          $file = file_load($fid);

          if ($file) {
            file_delete($file, TRUE);
          }
        }
        unset($breadcrumb['items'][$old_key]);

        variable_del('parallax_breadcrumb');
        variable_set('parallax_breadcrumb', $breadcrumb);
      }

      if (isset($breadcrumb['items'][$old_key]['id']) && ($breadcrumb['items'][$old_key]['id'] == $item['id'])) {

        _parallax_breadcrumb_update_item($old_key, $item);
      }
    }
  }
  // if new item, upload background image
  if (!empty($values['new']['background'])) {
    $file = file_load($values['new']['background']);

    if ($file) {
      // Change status to permanent.
      $file->status = FILE_STATUS_PERMANENT;
      // Save.
      file_save($file);
      // Record that the module (in this example, user module) is using the file.

      file_usage_add($file, 'parallax_breadcrumb', 'id', $values['new']['id']);
    }
  }

  $new_background = !empty($values['new']['background']) ? $values['new']['background'] : '';
  $new_title = $values['new']['title'];
  $new_use_default_title = $values['new']['use_default_title'];
  $new_sub_title = $values['new']['sub_title'];
  $new_visibility = $values['new']['visibility'];

  if ($new_background || $new_title || $new_sub_title) {
    $breadcrumb = variable_get('parallax_breadcrumb');
    $breadcrumb['items'][] = array(
        'id' => $values['new']['id'],
        'title' => $new_title,
        'use_default_title' => $new_use_default_title,
        'sub_title' => $new_sub_title,
        'background' => $new_background,
        'visibility' => $new_visibility,
    );

    variable_del('parallax_breadcrumb');
    variable_set('parallax_breadcrumb', $breadcrumb);
  }
}

function _parallax_breadcrumb_update_item($key, $item) {
  // check background if change, or add new

  $breadcrumb = variable_get('parallax_breadcrumb');
  if (!empty($breadcrumb['items'][$key]['background'])) {
    // background existing, now check it if update
    if ($item['background'] != $breadcrumb['items'][$key]['background']) {
      // background has change .. we need delete old background, then start upload new background
      $old_fid = $breadcrumb['items'][$key]['background'];
      $file = file_load($old_fid);
      if ($file) {
        file_delete($file, TRUE);
      }

      if (!empty($item['background'])) { // upload new background
        _parallax_breadcrumb_upload_file($item['background'], $item['id']);
      }
    }
  } else {
    if (!empty($item['background'])) {
      // upload new background
      _parallax_breadcrumb_upload_file($item['background'], $item['id']);
    }
  }
  unset($breadcrumb['items'][$key]);
  $breadcrumb['items'][$key] = $item;
  variable_del('parallax_breadcrumb');
  variable_set('parallax_breadcrumb', $breadcrumb);
}

function _parallax_breadcrumb_add_item($item) {

  $breadcrumb = variable_get('parallax_breadcrumb');
  $key = !empty($breadcrumb['items']) ? count($breadcrumb['items']) : 0;
  if (!empty($item['background'])) {

    _parallax_breadcrumb_upload_file($item['background'], $key);
  }

  $breadcrumb['items'][$key] = $item;
  variable_del('parallax_breadcrumb');
  variable_set('parallax_breadcrumb', $breadcrumb);
}

function _parallax_breadcrumb_get_key_by_id($id) {

  $return_value = FALSE;

  if (isset($id)) {
    $breadcrumb = variable_get('parallax_breadcrumb');

    if (!empty($breadcrumb['items'])) {
      foreach ($breadcrumb['items'] as $key => $item) {

        if ($item['id'] == $id) {
          $return_value = $key;
          return $return_value;
        }
      }
    }
  }

  return $return_value;
}

function _parallax_breadcrumb_upload_file($fid, $key) {
  if (!empty($fid)) {
    $file = file_load($fid);

    if ($file) {
      // Change status to permanent.
      $file->status = FILE_STATUS_PERMANENT;
      // Save.
      file_save($file);
      // Record that the module (in this example, user module) is using the file.

      file_usage_add($file, 'parallax_breadcrumb', 'id', $key);
    }
  }
}

function _parallax_breadcrumb_list_format() {
  $output = '';
  $breadcrumb = drupal_get_breadcrumb();

  if (!drupal_is_front_page() && !module_exists('custom_breadcrumbs')) {
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $nid = arg(1);
      $node = node_load($nid);
      if (($node->type == 'portfolio')) {
        $breadcrumb[] = t('Portfolio');
      }
      if (($node->type == 'blog')) {
        $breadcrumb[] = t('Blog');
      }
    }
    $breadcrumb[] = drupal_get_title();
  }

  if (!empty($breadcrumb)) {

    $output = '<ul>';

// Provide a navigational heading to give context for breadcrumb links to
// screen-reader users. Make the heading invisible with .element-invisible.
    foreach ($breadcrumb as $br) {
      $output .= '<li>' . $br . '</li>';
    }

    $output .= '</ul>';

    return $output;
  }
}
