You are here

party_activity.module in Party 8.2

Same filename and directory in other branches
  1. 7 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'),
    'entity class' => 'PartyActivity',
    'controller class' => 'PartyActivityController',
    'base table' => 'party_activity',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'party_activity_id',
      'bundle' => 'type',
    ),
    'bundles' => array(),
    'bundle keys' => array(
      'bundle' => 'type',
    ),
    'label callback' => 'entity_class_label',
    '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) {
  foreach (party_activity_get_types() as $type => $info) {
    $entity_info['party_activity']['bundles'][$type] = array(
      'label' => $info->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_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 = $party_activity->type)) {
    $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($party_activity_id, $reset = FALSE) {
  $party_activities = party_activity_load_multiple(array(
    $party_activity_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->party_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->party_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_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',
        )),
        'tz_handling' => 'site',
      ),
    );
    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);
  }
}

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_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_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_type_insert Implements hook_party_activity_type_insert().
party_activity_permission Implements hook_permission().
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().