<?php
/**
 * @file
 * Render any entity form into a block.
 */


/**
 * Implements hook_block_info().
 */
function entityform_block_block_info() {
  $blocks = array();
  $types = variable_get('entityform_block_types');
  if (!empty($types)) {
    foreach ($types as $key => $type) {
      $blocks[$type] = array(
          'info' => ucfirst($type),
          'cache' => DRUPAL_NO_CACHE,
      );
    }
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function entityform_block_block_view($delta = '') {
  $types = variable_get('entityform_block_types');
  // Hide existing blocks if disabled
  if (entityform_block_exists($types, $delta)) {
    $entity_form = entityform_block_get_entity($delta);
    if ($entity_form && entityform_access('submit', $entity_form)) {
      $subject = $entity_form->label;
      $block = array();
      module_load_include('inc', 'entityform', 'entityform.admin');
      $block['subject'] = $subject;
      $form = entityform_form_wrapper(entityform_empty_load($delta), 'submit', 'embedded');
      $block['content'] = drupal_render($form);
      return $block;
    }
  }
}

/**
 * Implements hook_form_alter().
 *
 * Adds a checkbox to enable a block to the entity form edit form.
 */
function entityform_block_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'entityform_type_form') {
    $types = array();
    $types = variable_get('entityform_block_types');
    $current_type = $form['#entityform_type']->type;

    $form['data']['block_set'] = array(
        '#type' => 'fieldset',
        '#title' => t('Entity block settings'),
        '#collapsible' => TRUE,
        '#group' => 'additional_settings',
        '#weight' => 100,
    );
    if (!empty($types) && isset($current_type)) {
      $exists = entityform_block_exists($types, $current_type);
      if ( $exists == TRUE) {
        $form['data']['block_set']['current_type'] = array(
            '#type' => 'hidden',
            '#value' => $current_type,
        );
      }
    }
    $form['data']['block_set']['enable_block'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable as a block'),
        '#description' => t('Create a block with this entity form?'),
        '#default_value' => !empty($exists) ? 1 : 0,

    );
    $form['#submit'][] = 'entityform_block_submit';

  }

}

/**
 * Custom submit function.
 */
function entityform_block_submit(&$form, &$form_state) {
  $types = array();
  $block_exists = FALSE;
  $types = variable_get('entityform_block_types', $types);
  $type = $form_state['values']['type'];
  $original_type = isset($form_state['values']['data']['current_type']) ?
  $form_state['values']['data']['current_type'] : FALSE;

  if (!empty($types)) {
    if (isset($original_type)) {
      if ($original_type !== $type) {
        $entity_name_has_changed = TRUE;
        $block_exists = entityform_block_exists($types, $original_type);
      }
      else {
        $block_exists = entityform_block_exists($types, $type);
      }
    }
  }
  if ($form_state['values']['data']['enable_block'] == 1) {
    $enabled = TRUE;
  }
  if (isset($enabled)) {
    // If the entityform block enabled but doesn't exist add to the array.
    if (!$block_exists) {
      $types[] = $type;
      drupal_set_message(t('Entityform block @title has been created', array('@title' => $type)));
    }
    // If the block exists, check if the entity form machine name has changed.
    else {
      if (isset($entity_name_has_changed)) {
        $key = array_search($original_type, $types);
        $types[$key] = $type;
        entityform_block_change_name($original_type, $type);
      }
    }
    // If it's not enabled but block exists.
  }
  elseif (!isset($enabled) && $block_exists) {
    $key = array_search($original_type, $types);
    unset($types[$key]);
    drupal_set_message(t('Entityform block @title has been disabled.', array('@title' => $type)));
  }
  variable_set('entityform_block_types', $types);
}

/**
 * Helper function to determine if the block is enabled
 * for a given entity form type.
 *
 * @param $types
 *   Array of enabled blocks for entityform types.
 *
 * @param $type
 *   Entityform type machine name used as a block delta. *
 *
 * @return
 *   True if the block is enabled for the given entityform type,
 *   False if not.
 */
function entityform_block_exists($types, $type) {

  if (in_array($type, $types)) {
    return TRUE;
  }
}
/**
 * Update block name if the entity form's machine readable name has changed.
 *
 * We're using block deltas instead of bid.
 */
function entityform_block_change_name($original_type, $new_type) {
  $query = db_update('block')->fields(array('delta' => $new_type))
          ->condition('delta', $original_type)
          ->execute();
}

/**
 * Using entity field query to retrieve the entity object.
 *
 * @param $type
 *   Entityform type.
 *
 * @return
 *   Returns entityform type entity object if true,
 *   FALSE if it doesn't exist.
 */
function entityform_block_get_entity($type){

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'entityform_type')
        ->propertyCondition('type', $type);
  $result = $query->execute();

  if (!empty($result)) {
    $id = key($result['entityform_type']);
    $entity_form = entity_load('entityform_type', array($id));
    return $entity_form[$id];
  }
  else {
    return FALSE;
  }
}
/**
 * Implements hook_entity_type_delete().
 *
 * Deletes blocks if the entity type is deleted.
 */
function entityform_block_entity_delete($entity, $entity_type) {
  $type = $entity->type;
  if ($entity_type == "entityform_type") {
    $types = variable_get('entityform_block_types');
    if (entityform_block_exists($types, $type)) {
      $key = array_search($type, $types);
      unset($types[$key]);
      variable_set('entityform_block_types', $types);
    }
  }
}

