You are here

calendar_systems_date_properties.module in Calendar Systems 7

Contains hook implementations and helpers.

File

calendar_systems_date_properties/calendar_systems_date_properties.module
View source
<?php

/**
 * @file
 * Contains hook implementations and helpers.
 */

/**
 * Implements hook_help().
 */
function calendar_systems_date_properties_help($path, $arg) {
  switch ($path) {
    case 'admin/config/regionals/calendar-systems/date_properties':
      $output = '<p>' . t('Configure Date Properties.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_perm().
 */
function calendar_systems_date_properties_permission() {
  return array(
    'administer calendar_systems_date_properties' => array(
      'title' => t('Administer calendar systems date properties'),
      'description' => t('Administer calendar systems date properties configurations.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function calendar_systems_date_properties_menu() {
  $items = array();

  // Main configuration page:
  $items['admin/config/regional/calendar-systems/date_properties'] = array(
    'title' => 'Calendar systems date properties',
    'description' => 'Administer calendar systems date properties.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'calendar_systems_date_properties_settings_form',
    ),
    'access arguments' => array(
      'administer calendar_systems_date_properties',
    ),
    'file' => 'calendar_systems_date_properties.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_views_api().
 */
function calendar_systems_date_properties_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'calendar_systems_date_properties'),
  );
}

/**
 * hook_entity_presave
 * @param $entity
 * @param $type
 */
function calendar_systems_date_properties_entity_presave($entity, $type) {
  $entity_types = _calendar_systems_date_properties_entity_types();
  $entity_type_info = @$entity_types[$type];
  if (isset($entity_type_info['integrated']) && $entity_type_info['integrated']) {
    $calendar_systems = _calendar_systems_date_properties_get_active_calendar_systems();
    foreach ($entity_type_info['selected_properties'] as $property_name => $property) {
      foreach ($calendar_systems as $calendar_system) {
        $calendar = calendar_systems_get_calendar_instance($calendar_system['name']);
        $time = $entity->{$property_name};
        if ($time > 0) {
          $date = $calendar
            ->timestampToInfoArray($time);
          foreach ($calendar_system['parts'] as $calendar_system_part_name => $calendar_system_part) {
            $new_property_name = $property_name . '_' . $calendar_system['name'] . '_' . $calendar_system_part_name;
            $entity->{$new_property_name} = $date[$calendar_system_part_name];
          }
        }
      }
    }
  }
}

/**
 * Implements hook_schema_alter().
 *
 * @param $schema
 *   The system-wide schema
 */
function calendar_systems_date_properties_schema_alter(&$schema) {
  $entity_types = _calendar_systems_date_properties_entity_types();
  foreach ($entity_types as $entity_type => $entity_type_info) {
    if (isset($entity_type_info['integrated']) && $entity_type_info['integrated']) {

      //dsm($entity_type);

      //dsm($entity_type_info);
      foreach ($entity_type_info['properties'] as $property_name => $property) {
        if (isset($entity_type_info['selected_properties'][$property_name])) {
          $calendar_systems = _calendar_systems_date_properties_get_active_calendar_systems();
          foreach ($calendar_systems as $calendar_system) {
            foreach ($calendar_system['parts'] as $calendar_system_part_name => $calendar_system_part) {
              $new_property_label = t($entity_type_info['properties'][$property_name]['label']) . ' ' . t($calendar_system['name']) . ' ' . t($calendar_system_part['label']);
              $new_property_name = $property_name . '_' . $calendar_system['name'] . '_' . $calendar_system_part_name;
              $schema[$entity_type_info['base table']]['fields'][$new_property_name] = array(
                'description' => $new_property_label,
                'type' => 'int',
                'not null' => FALSE,
                'default' => NULL,
              );
            }
          }
        }
      }
    }
  }
}

/**
 * hook_module_implements_alter
 * @param $implementations
 * @param $hook
 */
function calendar_systems_date_properties_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'entity_property_info_alter' || $hook == 'schema_alter' || $hook == 'schema_alter' || $hook == 'entity_presave') {
    $var = $implementations['calendar_systems_date_properties'];
    unset($implementations['calendar_systems_date_properties']);
    $implementations['calendar_systems_date_properties'] = $var;
  }
}

/**
 * @param $info
 */
function calendar_systems_date_properties_entity_property_info_alter(&$info) {
  $entity_types = _calendar_systems_date_properties_entity_types();
  foreach ($entity_types as $entity_type => $entity_type_info) {
    if (isset($entity_type_info['integrated']) && $entity_type_info['integrated']) {
      $properties =& $info[$entity_type]['properties'];
      foreach ($entity_type_info['properties'] as $property_name => $property) {
        if (isset($entity_type_info['selected_properties'][$property_name])) {
          $calendar_systems = _calendar_systems_date_properties_get_active_calendar_systems();
          foreach ($calendar_systems as $calendar_system) {
            foreach ($calendar_system['parts'] as $calendar_system_part_name => $calendar_system_part) {
              $new_property_label = t($entity_type_info['properties'][$property_name]['label']) . ' ' . t($calendar_system['name']) . ' ' . t($calendar_system_part['label']);
              $new_property_name = $property_name . '_' . $calendar_system['name'] . '_' . $calendar_system_part_name;

              //dsm($new_property_name);
              $properties[$new_property_name] = array(
                'label' => $new_property_label,
                'type' => 'integer',
                //'description' => 'The use whom deleted the content.',

                // This is the schema field you made.
                'schema field' => $new_property_name,
                'getter callback' => 'entity_property_verbatim_get',
                'setter callback' => 'entity_property_verbatim_set',
              );
            }
          }
        }
      }
    }
  }
}

/**
 * @return array
 */
function _calendar_systems_date_properties_entity_types() {

  //static $entity_types;
  $entity_types = NULL;
  if (is_null($entity_types)) {
    $selected_bundles = variable_get('calendar_systems_date_properties_entity_bundles_settings');
    $bundles = field_info_bundles();
    return array();
    $entity_types = array();
    foreach ($bundles as $entity_type => $_bundles) {
      $entity_type_info = entity_get_info($entity_type);
      $entity_types[$entity_type] = $entity_type_info;
      $entity_types[$entity_type]['bundles'] = $_bundles;
      $integrated = FALSE;
      $selected_properties = array();
      if (!empty($_bundles)) {
        foreach ($_bundles as $bundle => $bundle_info) {
          $entity_types[$entity_type]['bundles'][$bundle]['integrated'] = FALSE;
          if (isset($selected_bundles[$entity_type . '.' . $bundle]) && !empty($bundle_info)) {
            if ($selected_bundles[$entity_type . '.' . $bundle]['enabled'] == TRUE) {
              $entity_types[$entity_type]['bundles'][$bundle]['integrated'] = TRUE;
              $integrated = TRUE;
              foreach ($selected_bundles[$entity_type . '.' . $bundle]['properties'] as $property_name => $property) {
                if ($property == 1) {
                  $selected_properties[$property_name] = $property_name;
                }
              }
            }
          }
        }
      }
      $properties = entity_get_property_info($entity_type);
      $entity_types[$entity_type]['properties'] = $properties['properties'];
      $entity_types[$entity_type]['integrated'] = $integrated;
      $entity_types[$entity_type]['selected_properties'] = $selected_properties;

      //if ($integrated) { dsm($entity_type);dsm($entity_types[$entity_type]);dsm(debug_backtrace()); }
    }
  }
  return $entity_types;
}

/**
 *
 */
function _calendar_systems_date_properties_schema_apply_change($remove = FALSE) {
  $entity_types = _calendar_systems_date_properties_entity_types();
  foreach ($entity_types as $entity_type => $entity_type_info) {
    if (isset($entity_type_info['integrated']) && $entity_type_info['integrated']) {
      foreach ($entity_type_info['properties'] as $property_name => $property) {
        $calendar_systems = _calendar_systems_date_properties_get_active_calendar_systems();
        foreach ($calendar_systems as $calendar_system) {
          foreach ($calendar_system['parts'] as $calendar_system_part_name => $calendar_system_part) {
            $new_property_name = $property_name . '_' . $calendar_system['name'] . '_' . $calendar_system_part_name;
            if (!$remove && $entity_type_info['integrated'] && isset($entity_type_info['selected_properties'][$property_name])) {
              if (!db_field_exists($entity_type_info['base table'], $new_property_name)) {
                db_add_field($entity_type_info['base table'], $new_property_name, array(
                  'type' => 'int',
                  'length' => 1,
                  'not null' => FALSE,
                  'default' => NULL,
                ));
              }
            }
            else {
              if (db_field_exists($entity_type_info['base table'], $new_property_name)) {
                db_drop_field($entity_type_info['base table'], $new_property_name);
              }
            }
          }
        }
      }
    }
  }
}

/**
 *
 */
function _calendar_systems_date_properties_get_active_calendar_systems() {
  static $calendar_systems;
  if (!$calendar_systems) {
    if (!function_exists('_calendar_systems_profiles')) {
      module_load_include('inc', 'calendar_systems', 'calendar_systems.helpers');
    }
    $profiles = _calendar_systems_profiles();
    $calendar_systems = array();
    foreach ($profiles as $profile) {
      if ($profile->calendar_system != 'gregorian') {
        $calendar_systems[$profile->calendar_system] = array(
          'name' => $profile->calendar_system,
          'label' => $profile->calendar_system,
          'parts' => array(
            'year' => array(
              'label' => 'Year',
            ),
            'month' => array(
              'label' => 'Month',
            ),
            'day' => array(
              'label' => 'Day',
            ),
          ),
        );
      }
    }
  }
  return $calendar_systems;
}