You are here

party_activity.module in Party 7

Same filename and directory in other branches
  1. 8.2 modules/party_activity/party_activity.module

Functions and important hooks for the party_activity module

File

modules/party_activity/party_activity.module
View source
<?php

/**
 * @file Functions and important hooks for the party_activity module
 */

/**
 * Implements hook_entity_info().
 */
function party_activity_entity_info() {
  $info['party_activity'] = array(
    'label' => t('Activity'),
    'plural label' => t('Activities'),
    'entity class' => 'PartyActivity',
    'controller class' => 'PartyActivityController',
    'base table' => 'party_activity',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'label' => 'title',
      'bundle' => 'type',
    ),
    'bundles' => array(),
    'bundle keys' => array(
      'bundle' => 'type',
    ),
    'uri callback' => 'entity_class_uri',
    'creation callback' => 'party_activity_create',
    'access callback' => 'party_activity_access',
    'module' => 'party_activity',
    'admin ui' => array(
      'path' => 'admin/community/activities',
      'file' => 'party_activity.admin.inc',
      'controller class' => 'PartyActivityUIController',
      'menu wildcard' => '%party_activity',
    ),
  );
  $info['party_activity_type'] = array(
    'label' => 'Activity Type',
    'entity class' => 'PartyActivityType',
    'controller class' => 'PartyActivityTypeController',
    'base table' => 'party_activity_type',
    'fieldable' => FALSE,
    'bundle of' => 'party_activity',
    'exportable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'name' => 'type',
      'label' => 'label',
    ),
    'access callback' => 'party_activity_type_access',
    'module' => 'party_activity',
    'admin ui' => array(
      'path' => 'admin/structure/activity_types',
      'file' => 'party_activity_type.admin.inc',
      'controller class' => 'PartyActivityTypeUIController',
    ),
  );
  return $info;
}

/**
 * Implements hook_entity_info_alter().
 */
function party_activity_entity_info_alter(&$entity_info) {
  $types = db_select('party_activity_type', 'at')
    ->fields('at', array(
    'type',
    'label',
  ))
    ->execute()
    ->fetchAllKeyed();
  foreach ($types as $type => $label) {
    $entity_info['party_activity']['bundles'][$type] = array(
      'label' => $label,
      'admin' => array(
        'path' => 'admin/structure/activity_types/manage/%party_activity_type',
        'real path' => 'admin/structure/activity_types/manage/' . $type,
        'bundle argument' => 4,
        'access arguments' => array(
          'administer activity types',
        ),
      ),
    );
  }
}

/**
 * Implements hook_field_extra_fields().
 */
function party_activity_field_extra_fields() {
  $extra = array();
  $types = db_select('party_activity_type', 'at')
    ->fields('at', array(
    'type',
    'label',
  ))
    ->execute()
    ->fetchAllKeyed();
  foreach ($types as $type => $label) {
    $extra['party_activity'][$type] = array(
      'form' => array(
        'title' => array(
          'label' => t('Title'),
          'description' => '',
          'weight' => -5,
        ),
      ),
    );
  }
  return $extra;
}

/**
 * Implements hook_permission().
 */
function party_activity_permission() {
  $permissions = array(
    'administer activity types' => array(
      'title' => t('Administer Activity types'),
      'description' => t('Create and delete fields for activity types, and set there permissions'),
    ),
    'administer activities' => array(
      'title' => t('Administer Activities'),
      'description' => t('Edit and delete all activities'),
    ),
  );

  // General permissions per activity type
  foreach (party_activity_get_types() as $type) {
    $type_name = check_plain($type->type);
    $permissions += array(
      "edit any {$type_name} activity" => array(
        'title' => t('%type_name: Edit any activity', array(
          '%type_name' => $type->label,
        )),
      ),
      "view any {$type_name} activity" => array(
        'title' => t('%type_name: View any activity', array(
          '%type_name' => $type->label,
        )),
      ),
    );
  }
  return $permissions;
}

/**
 * Determines whether the given user has access to an activity
 *
 * @param $op
 *  The operation being performed. One of 'view', 'update', 'create', 'delete'
 *  or just 'edit' (being the same as 'create' or 'update').
 * @param $activity
 *   Optionally an activity or an activity type to check access for. If nothing is
 *   given, access for all models is determined.
 * @param $account
 *   The user to check for. Leave it to NULL to check for the global user.
 * @return boolean
 *   Whether access is allowed or not.
 */
function party_activity_access($op, $party_activity = NULL, $account = NULL) {
  if (user_access('administer activities', $account)) {
    return TRUE;
  }
  if (isset($party_activity)) {
    $type_name = is_object($party_activity) ? $party_activity->type : $party_activity;
    if ($type_name) {
      $op = $op == 'view' ? 'view' : 'edit';
      if (user_access("{$op} any {$type_name} activity", $account)) {
        return TRUE;
      }
    }
  }
  return FALSE;
}

/**
 * Access callback for the entity API
 */
function party_activity_type_access($op, $type = NULL, $account = NULL) {
  return user_access('administer activity types', $account);
}

/**
 * Get an array of all activity types, keyed by the type name.
 *
 * @param $type_name
 * @return PartyActivityType[]
 */
function party_activity_get_types($type_name = NULL) {
  $types = entity_load_multiple_by_name('party_activity_type', isset($type_name) ? array(
    $type_name,
  ) : FALSE);
  return isset($type_name) ? reset($types) : $types;
}

/**
 * Menu argument loader; Load an activity type
 */
function party_activity_type_load($type) {
  return party_activity_get_types($type);
}

/**
 * Fetch a party activity object
 *
 * @param $activity_id
 *  Intefer specifying the party activity id.
 * @param $reset
 *  A boolean indicating whether the internal cache should be reset
 * @return
 *  A fully loaded PartyActivity object or false.
 */
function party_activity_load($id, $reset = FALSE) {
  $party_activities = party_activity_load_multiple(array(
    $id,
  ), array(), $reset);
  return reset($party_activities);
}

/**
 * Load multiple activities
 *
 * @see entity_load()
 */
function party_activity_load_multiple($activity_ids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('party_activity', $activity_ids, $conditions, $reset);
}

/**
 * Delete an Activity
 */
function party_activity_delete(PartyActivity $party_activity) {
  $party_activity
    ->delete();
}

/**
 * Delete multiple activities
 */
function party_activity_delete_multiple(array $ids) {
  entity_get_controller('party_activity')
    ->delete($ids);
}

/**
 * Create a PartyActivity object
 */
function party_activity_create($values = array()) {
  return entity_get_controller('party_activity')
    ->create($values);
}

/**
 * Save an activity to the database
 */
function party_activity_save(PartyActivity $activity) {
  $activity
    ->save();
}

/**
 * Save an activity type to the database
 */
function party_activity_type_save(PartyActivityType $type) {
  $type
    ->save();
}

/**
 * Delete a party activity type
 */
function party_activity_type_delete(PartyActivityType $type) {
  $type
    ->delete();
}

/**
 * URI Callback for party activities
 */
function party_activity_uri(PartyActivity $activity) {
  return array(
    'path' => 'activity/' . $activity->id,
  );
}

/**
 * Menu title callback for activity pages
 */
function party_activity_page_title(PartyActivity $activity, $action = NULL) {
  $title = '';
  if ($action == 'edit') {
    $title = 'Edit Activity: ';
  }
  return $title . $activity->title;
}

/**
 * Sets up the content to show an individual party activity
 */
function party_activity_page_view($activity, $view_mode = 'full') {
  $controller = entity_get_controller('party_activity');
  $content = $controller
    ->view(array(
    $activity->id => $activity,
  ));
  return $content;
}

/**
 * Implements hook_views_api().
 */
function party_activity_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'party_activity') . '/views',
  );
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function party_activity_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'panelizer' && $plugin_type == 'entity') {
    return "plugins/{$plugin_type}";
  }
}

/**
 * Implements hook_theme().
 */
function party_activity_theme() {
  return array(
    'party_activity' => array(
      'render element' => 'elements',
      'template' => 'party_activity',
    ),
    'party_activity_add_list' => array(
      'variables' => array(
        'content' => array(),
      ),
      'file' => 'party_activity.admin.inc',
    ),
  );
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function party_activity_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  if ($root_path == 'admin/community/activities') {
    $item = menu_get_item('admin/community/activities/add');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
}

/**
 * Implements hook_party_activity_type_insert().
 */
function party_activity_party_activity_type_insert($activity_type) {

  // Date field, this might change to be used
  if (!field_info_field('activity_date')) {

    //create field
    $field = array(
      'field_name' => 'activity_date',
      'type' => 'datetime',
      'settings' => array(
        'granularity' => drupal_map_assoc(array(
          'year',
          'month',
          'day',
          'hour',
          'minute',
        )),
        'timezone_db' => 'UTC',
        'tz_handling' => 'date',
        'todate' => 'required',
      ),
    );
    field_create_field($field);
  }
  if (!field_info_instance('party_activity', 'activity_date', $activity_type->type)) {

    //create instance
    $instance = array(
      'field_name' => 'activity_date',
      'entity_type' => 'party_activity',
      'bundle' => $activity_type->type,
      'label' => 'Date',
      'required' => TRUE,
      'description' => "The date and time of the {$activity_type->label}",
      'settings' => array(
        'default_value' => 'blank',
      ),
      'widget' => array(
        'type' => 'date_popup',
        'settings' => array(
          'year_range' => '-2:+5',
          'input_format' => 'j M Y - g:i:sa',
          'increment' => 15,
        ),
      ),
      'display' => array(
        'default' => array(
          'type' => 'date_default',
          'label' => 'above',
        ),
        'teaser' => array(
          'type' => 'date_default',
          'label' => 'inline',
        ),
      ),
    );
    field_create_instance($instance);
  }

  // Participants Field
  if (!field_info_field('activity_participants')) {

    //create field
    $field = array(
      'field_name' => 'activity_participants',
      'type' => 'entityreference',
      'cardinality' => -1,
      'settings' => array(
        'target_type' => 'party',
        'handler' => 'base',
        'handler_settings' => array(
          'sort' => array(
            'type' => 'none',
          ),
        ),
      ),
    );
    field_create_field($field);
  }
  if (!field_info_instance('party_activity', 'activity_participants', $activity_type->type)) {

    //create instance
    $instance = array(
      'field_name' => 'activity_participants',
      'entity_type' => 'party_activity',
      'bundle' => $activity_type->type,
      'label' => 'Participants',
      'required' => TRUE,
      'description' => "Those involved in the activity",
      'settings' => array(
        'default_value' => '',
      ),
      'widget' => array(
        'type' => 'entityreference_autocomplete',
        'settings' => array(
          'size' => '60',
          'match_operator' => 'CONTAINS',
        ),
      ),
      'display' => array(
        'default' => array(
          'type' => 'entityreference_label',
          'label' => 'above',
          'settings' => array(
            'link' => TRUE,
          ),
        ),
        'teaser' => array(
          'type' => 'entityreference_label',
          'label' => 'inline',
          'settings' => array(
            'link' => TRUE,
          ),
        ),
      ),
    );
    field_create_instance($instance);
  }

  // Assigned To Field
  if (!field_info_field('activity_assigned_to')) {

    //create field
    $field = array(
      'field_name' => 'activity_assigned_to',
      'type' => 'entityreference',
      'cardinality' => 1,
      'settings' => array(
        'target_type' => 'party',
        'handler' => 'base',
        'handler_settings' => array(
          'sort' => array(
            'type' => 'none',
          ),
        ),
      ),
    );
    field_create_field($field);
  }
  if (!field_info_instance('party_activity', 'activity_assigned_to', $activity_type->type)) {

    //create instance
    $instance = array(
      'field_name' => 'activity_assigned_to',
      'entity_type' => 'party_activity',
      'bundle' => $activity_type->type,
      'label' => 'Assigned To',
      'required' => TRUE,
      'description' => "The party to whom the activity is assigned",
      'settings' => array(
        'default_value' => '',
      ),
      'widget' => array(
        'type' => 'entityreference_autocomplete',
        'settings' => array(
          'size' => '60',
          'match_operator' => 'CONTAINS',
        ),
      ),
      'display' => array(
        'default' => array(
          'type' => 'entityreference_label',
          'label' => 'above',
          'settings' => array(
            'link' => TRUE,
          ),
        ),
        'teaser' => array(
          'type' => 'entityreference_label',
          'label' => 'inline',
          'settings' => array(
            'link' => TRUE,
          ),
        ),
      ),
    );
    field_create_instance($instance);
  }

  // Activity Details Field
  if (!field_info_field('activity_details')) {
    $field = array(
      'field_name' => 'activity_details',
      'type' => 'text_long',
    );
    $field = field_create_field($field);
  }
  if (!field_info_instance('party_activity', 'activity_details', $activity_type->type)) {
    $instance = array(
      'field_name' => 'activity_details',
      'entity_type' => 'party_activity',
      'bundle' => $activity_type->type,
      'label' => 'Details',
      'description' => t('(e.g. key information about the @type)', array(
        '@type' => $activity_type->label,
      )),
      'widget' => array(
        'type' => 'text_textarea',
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'text_default',
        ),
        'teaser' => array(
          'label' => 'hidden',
          'type' => 'text_default',
        ),
      ),
    );
    $instance = field_create_instance($instance);
  }
}

/**
 * Implements hook_fullcalendar_editable()
 */
function party_activity_fullcalendar_editable($entity, $view) {
  return TRUE;
}

/**
 * Implements hook_preprocess_fullcalendar
 */
function party_activity_preprocess_fullcalendar(&$variables) {
  $controller = entity_ui_controller('party_activity');

  // Add ctools modal js.
  ctools_include('modal');
  ctools_modal_add_js();

  // Add settings.
  $tz = new DateTimeZone(date_default_timezone());
  $date_time = new DateTime('now', $tz);
  $settings = array(
    'party_activity_popup' => array(
      'add_path' => $controller
        ->getModalAddPath(),
    ),
    'party_activity_timezone' => array(
      'timezone' => date_default_timezone(),
      'offset' => $tz
        ->getOffset($date_time),
    ),
  );
  drupal_add_js($settings, 'setting');
  drupal_add_js(drupal_get_path('module', 'party_activity') . '/js/party_activity.fullcalendar.js');
  foreach ($variables['element']['content']['events'] as &$events) {
    foreach ($events['#event'] as &$event) {
      $event['#path'] = $controller
        ->getModalEditPath($event['#options']['attributes']['eid']);
      $event['#options']['query']['fc'] = 1;
      $event['#options']['query']['views_view'] = $variables['view']->name;
      $event['#options']['query']['views_display'] = $variables['view']->current_display;
      $event['#options']['attributes']['cn'] .= ' ctools-use-modal';
    }
  }

  // Add view name and display to the full calendar div.
  $variables['element']['fullcalendar']['#attributes']['views_view'] = $variables['view']->name;
  $variables['element']['fullcalendar']['#attributes']['views_display'] = $variables['view']->current_display;
}

/**
 * Implements hook_party_activity_fullcalendar_event_command_alter().
 */
function party_activity_party_activity_fullcalendar_event_command_alter(&$command, $activity, $view) {
  $controller = entity_ui_controller('party_activity');
  $command['href'] = base_path() . $controller
    ->getModalEditPath($activity->id);
  $command['href'] .= "?fc=1&views_view={$view->name}&views_display={$view->current_display}";
  $command['cn'] .= ' ctools-use-modal';
}

Functions

Namesort descending Description
party_activity_access Determines whether the given user has access to an activity
party_activity_create Create a PartyActivity object
party_activity_ctools_plugin_directory Implements hook_ctools_plugin_directory().
party_activity_delete Delete an Activity
party_activity_delete_multiple Delete multiple activities
party_activity_entity_info Implements hook_entity_info().
party_activity_entity_info_alter Implements hook_entity_info_alter().
party_activity_field_extra_fields Implements hook_field_extra_fields().
party_activity_fullcalendar_editable Implements hook_fullcalendar_editable()
party_activity_get_types Get an array of all activity types, keyed by the type name.
party_activity_load Fetch a party activity object
party_activity_load_multiple Load multiple activities
party_activity_menu_local_tasks_alter Implements hook_menu_local_tasks_alter().
party_activity_page_title Menu title callback for activity pages
party_activity_page_view Sets up the content to show an individual party activity
party_activity_party_activity_fullcalendar_event_command_alter Implements hook_party_activity_fullcalendar_event_command_alter().
party_activity_party_activity_type_insert Implements hook_party_activity_type_insert().
party_activity_permission Implements hook_permission().
party_activity_preprocess_fullcalendar Implements hook_preprocess_fullcalendar
party_activity_save Save an activity to the database
party_activity_theme Implements hook_theme().
party_activity_type_access Access callback for the entity API
party_activity_type_delete Delete a party activity type
party_activity_type_load Menu argument loader; Load an activity type
party_activity_type_save Save an activity type to the database
party_activity_uri URI Callback for party activities
party_activity_views_api Implements hook_views_api().