View source
<?php
module_load_include('inc', 'redhen_org', 'includes/redhen_org.forms');
function redhen_org_entity_info() {
return array(
'redhen_org' => array(
'label' => t('Organization'),
'plural label' => t('Organizations'),
'controller class' => 'RedhenOrgEntityController',
'rules controller class' => 'EntityDefaultRulesController',
'metadata controller class' => 'RedhenOrgMetadataController',
'views controller class' => 'RedhenOrgViewsController',
'entity class' => 'RedhenOrg',
'inline entity form' => array(
'controller' => 'RedHenOrgInlineEntityFormController',
),
'base table' => 'redhen_org',
'revision table' => 'redhen_org_revision',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'org_id',
'revision' => 'revision_id',
'bundle' => 'type',
'label' => 'label',
),
'bundle keys' => array(
'bundle' => 'name',
),
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full organization'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => TRUE,
),
),
'uri callback' => 'entity_class_uri',
'token type' => 'redhen_org',
'module' => 'redhen_org',
'label callback' => 'entity_class_label',
'access callback' => 'redhen_org_access',
),
'redhen_org_type' => array(
'label' => t('Organization type'),
'entity class' => 'RedhenOrgType',
'controller class' => 'RedhenOrgTypeController',
'base table' => 'redhen_org_type',
'fieldable' => FALSE,
'bundle of' => 'redhen_org',
'exportable' => TRUE,
'token type' => 'redhen_org_type',
'entity keys' => array(
'id' => 'org_type_id',
'name' => 'name',
'label' => 'label',
),
'access callback' => 'redhen_org_type_access',
'module' => 'redhen_org',
'admin ui' => array(
'path' => 'admin/structure/redhen/org_types',
'file' => 'redhen_org_type.admin.inc',
'file path' => drupal_get_path('module', 'redhen_org') . '/includes',
'controller class' => 'RedhenOrgTypeUIController',
),
),
);
}
function redhen_org_entity_info_alter(&$entity_info) {
if (drupal_get_schema('redhen_org')) {
foreach (redhen_org_get_types() as $type => $info) {
$entity_info['redhen_org']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/redhen/org_types/manage/%redhen_org_type',
'real path' => 'admin/structure/redhen/org_types/manage/' . $type,
'bundle argument' => 5,
'access arguments' => array(
'administer redhen_org types',
),
),
);
}
}
}
function redhen_org_menu() {
$items = array();
$items['redhen/org'] = array(
'title' => 'Organizations',
'description' => 'View RedHen CRM Organizations.',
'page callback' => 'redhen_org_page',
'file' => 'includes/redhen_org.pages.inc',
'access callback' => 'redhen_org_page_access',
);
$items['redhen/org/add'] = array(
'title' => 'Add an organization',
'description' => 'Add a new organization.',
'page callback' => 'redhen_org_types_list_page',
'access callback' => 'redhen_org_access',
'access arguments' => array(
'create',
),
'file' => 'includes/redhen_org.pages.inc',
'type' => MENU_NORMAL_ITEM,
);
$entity_info = entity_get_info('redhen_org');
foreach ($entity_info['bundles'] as $key => $bundle) {
$items['redhen/org/add/' . $key] = array(
'title' => $bundle['label'],
'title callback' => 'check_plain',
'description' => isset($bundle['description']) ? $bundle['description'] : '',
'page callback' => 'redhen_org_add_page',
'page arguments' => array(
$key,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'create',
$key,
),
'file' => 'includes/redhen_org.pages.inc',
);
}
$items['redhen/org/%redhen_org'] = array(
'title callback' => 'redhen_org_org_title',
'title arguments' => array(
2,
),
'page callback' => 'redhen_org_view',
'page arguments' => array(
2,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'view',
2,
),
);
$items['redhen/org/%redhen_org/view'] = array(
'title' => 'Summary',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -20,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['redhen/org/%redhen_org/view/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
$items['redhen/org/%redhen_org/view/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_org_form',
2,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'edit',
2,
),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['redhen/org/%redhen_org/view/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_org_delete_form',
2,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'delete',
2,
),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
$items['redhen/org/%redhen_org/view/archive'] = array(
'title' => 'Archive',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_org_archive_form',
2,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'archive',
2,
),
'type' => MENU_LOCAL_TASK,
);
$items['redhen/org/%redhen_org/view/unarchive'] = array(
'title' => 'Unarchive',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_org_unarchive_form',
2,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'unarchive',
2,
),
'type' => MENU_LOCAL_TASK,
'weight' => 4,
);
$items['redhen/org/%redhen_org/revisions'] = array(
'title' => 'Revisions',
'type' => MENU_LOCAL_TASK,
'page callback' => 'redhen_org_revision_list',
'page arguments' => array(
2,
),
'access callback' => 'user_access',
'access arguments' => array(
'access redhen org revisions',
),
'file' => 'includes/redhen_org.pages.inc',
'weight' => 4,
);
$items['redhen/org/%redhen_org/revisions/%redhen_org_revision'] = array(
'title callback' => 'redhen_org_org_title',
'title arguments' => array(
4,
),
'page callback' => 'redhen_org_view',
'page arguments' => array(
4,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'view',
4,
),
);
$items['redhen/org/%redhen_org/revisions/%redhen_org_revision/view'] = array(
'title' => 'Summary',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['redhen/org/%redhen_org/revisions/%redhen_org_revision/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_org_form',
4,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'edit',
4,
),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['redhen/org/%redhen_org/revisions/%redhen_org_revision/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_org_delete_form',
4,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'delete',
4,
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
$items['redhen/org/%redhen_org/primary/%redhen_contact'] = array(
'title callback' => 'Change primary contact',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'redhen_org_set_primary_form',
2,
4,
),
'access callback' => 'redhen_org_access',
'access arguments' => array(
'edit',
2,
),
'type' => MENU_CALLBACK,
);
$items['redhen/org/autocomplete'] = array(
'title' => 'Autocomplete for RedHen Organizations',
'page callback' => '_redhen_org_autocomplete',
'access callback' => 'redhen_org_access',
'access arguments' => array(
'edit',
),
'type' => MENU_CALLBACK,
'file' => 'includes/redhen_org.pages.inc',
);
if (module_exists('devel')) {
$items['redhen/org/%redhen_org/devel'] = array(
'title' => 'Devel',
'page callback' => 'redhen_devel_load_object',
'page arguments' => array(
'redhen_org',
2,
),
'access arguments' => array(
'access devel information',
),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
$items['redhen/org/%redhen_org/devel/load'] = array(
'title' => 'Load',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['redhen/org/%redhen_org/devel/render'] = array(
'title' => 'Render',
'page callback' => 'redhen_devel_render_object',
'page arguments' => array(
'redhen_org',
2,
),
'access arguments' => array(
'access devel information',
),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
}
return $items;
}
function redhen_org_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'redhen/org') {
$item = menu_get_item('redhen/org/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function redhen_org_theme() {
return array(
'redhen_org_add_list' => array(
'variables' => array(
'content' => NULL,
),
'file' => 'includes/redhen_org.forms.inc',
),
'redhen_org_list' => array(
'variables' => array(
'orgs' => NULL,
'header' => NULL,
'rows' => NULL,
),
),
);
}
function redhen_org_load($org_id) {
if (empty($org_id)) {
return FALSE;
}
$orgs = redhen_org_load_multiple(array(
$org_id,
), array());
return $orgs ? reset($orgs) : FALSE;
}
function redhen_org_load_multiple($org_ids = array(), $conditions = array(), $reset = FALSE) {
if (empty($org_ids) && empty($conditions)) {
return array();
}
return entity_load_multiple_by_name('redhen_org', $org_ids, $conditions, $reset);
}
function redhen_org_revision_load($revision_id) {
if (empty($revision_id)) {
return FALSE;
}
return entity_revision_load('redhen_org', $revision_id);
}
function redhen_org_delete_multiple($org_ids) {
$ret = entity_get_controller('redhen_org')
->delete($org_ids);
return $ret;
}
function redhen_org_create(array $values = array()) {
return entity_get_controller('redhen_org')
->create($values);
}
function redhen_org_save(RedhenOrg $org) {
$org
->save();
return $org;
}
function redhen_org_access($op, $org = NULL, $account = NULL) {
$op = $op == 'update' ? 'edit' : $op;
global $user;
$account = isset($account) ? $account : $user;
$redhen_relation_role_permissions = module_exists('redhen_relation') ? redhen_relation_role_get_permissions($user) : array();
if ($op == 'archive' && $org->redhen_state == REDHEN_STATE_ARCHIVED) {
return FALSE;
}
if ($op == 'unarchive' && $org->redhen_state == REDHEN_STATE_ACTIVE) {
return FALSE;
}
if (user_access('administer redhen orgs', $account)) {
return TRUE;
}
if (is_object($org) && $org
->isDefaultRevision()) {
$default_revision = TRUE;
}
else {
$default_revision = FALSE;
}
switch ($op) {
case 'view':
if ($default_revision) {
if (user_access('access redhen orgs', $account)) {
return TRUE;
}
if (isset($redhen_relation_role_permissions[$org->org_id])) {
return TRUE;
}
}
else {
if (user_access('access redhen org revisions', $account)) {
return TRUE;
}
}
break;
case 'archive':
case 'unarchive':
if (user_access('manage redhen orgs', $account)) {
return TRUE;
}
break;
case 'edit':
if ($default_revision) {
if (user_access('manage redhen orgs', $account) && $org->redhen_state != REDHEN_STATE_ARCHIVED) {
return TRUE;
}
if (!empty($redhen_relation_role_permissions[$org->org_id]['edit_org'])) {
return TRUE;
}
}
else {
if (user_access('manage redhen org revisions', $account) && $org->redhen_state != REDHEN_STATE_ARCHIVED) {
return TRUE;
}
}
break;
case 'delete':
if ($default_revision) {
if (user_access('manage redhen orgs', $account)) {
return TRUE;
}
if (!empty($redhen_relation_role_permissions[$org->org_id]['delete_org'])) {
return TRUE;
}
}
else {
if (user_access('manage redhen org revisions', $account)) {
return TRUE;
}
}
break;
case 'create':
if (user_access('manage redhen orgs', $account)) {
return TRUE;
}
if (isset($org) && is_string($org)) {
if (user_access('create ' . $org . ' orgs', $account)) {
return TRUE;
}
}
break;
}
return FALSE;
}
function redhen_org_page_access() {
global $user;
$redhen_relation_role_permissions = module_exists('redhen_relation') ? redhen_relation_role_get_permissions($user) : array();
if (user_access('access redhen orgs', $user)) {
return TRUE;
}
if (is_array($redhen_relation_role_permissions) && count($redhen_relation_role_permissions) > 0) {
return TRUE;
}
}
function redhen_org_permission() {
$permissions = array(
'administer redhen_org types' => array(
'title' => t('Administer RedHen Organization Types'),
'description' => t('Manage RedHen organization types and their structure.'),
),
'administer redhen orgs' => array(
'title' => t('Administer RedHen Organizations'),
'description' => t('Perform administration tasks for RedHen Organizations.'),
),
'manage redhen orgs' => array(
'title' => t('Manage RedHen Organizations'),
'description' => t('Create, update or delete RedHen Organizations.'),
),
'manage redhen org revisions' => array(
'title' => t('Manage RedHen Organization Revisions'),
'description' => t('Update or delete RedHen Organization revisions.'),
),
'access redhen orgs' => array(
'title' => t('Access Redhen Organizations'),
'description' => t('View for Redhen Organizations.'),
),
'access redhen org revisions' => array(
'title' => t('Access Redhen Organization Revisions'),
'description' => t('View for Redhen Organizations revisions'),
),
);
foreach (redhen_org_get_types() as $type => $info) {
$create_org_of_type_permission = 'create ' . $type . ' orgs';
$permissions[$create_org_of_type_permission] = array(
'title' => t('Create @organization_type Organizations', array(
'@organization_type' => $info->label,
)),
'description' => t('Create an organization of the type @organization_type.', array(
'@organization_type' => $info->label,
)),
);
}
return $permissions;
}
function redhen_org_view(RedhenOrg $org, $view_mode = 'full', $langcode = NULL, $page = NULL) {
return entity_view('redhen_org', array(
$org,
), $view_mode, $langcode, $page);
}
function redhen_org_org_title(RedhenOrg $org) {
return $org
->label();
}
function redhen_org_field_extra_fields() {
$extra = array();
foreach (redhen_org_get_types() as $type => $org_type) {
$extra['redhen_org'][$type] = array(
'form' => array(
'name' => array(
'label' => t('Organization name'),
'description' => t('Organization name'),
'weight' => 0,
),
),
'display' => array(
'name' => array(
'label' => t('Name'),
'description' => t('Organization name.'),
'weight' => 0,
),
'redhen_state' => array(
'label' => t('State'),
'description' => t('Organization state'),
'weight' => 0,
),
'primary_contact' => array(
'label' => t('Primary Contact'),
'description' => t('Primary contact display'),
'weight' => 0,
),
),
);
}
return $extra;
}
function redhen_org_get_types($name = NULL) {
$types = entity_load_multiple_by_name('redhen_org_type', isset($name) ? array(
$name,
) : FALSE);
return isset($name) ? reset($types) : $types;
}
function redhen_org_type_options_list() {
$options = array();
foreach (redhen_org_get_types() as $type) {
$options[$type->name] = $type
->label();
}
return $options;
}
function redhen_org_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer redhen_org types', $account);
}
function redhen_org_type_load($type) {
return redhen_org_get_types($type);
}
function redhen_org_type_save(RedhenOrgType $type) {
$type
->save();
}
function redhen_org_type_delete(RedhenOrgType $type) {
$type
->delete();
}
function theme_redhen_org_add_list($variables) {
$content = $variables['content'];
$output = '';
if ($content) {
$output = '<dl class="node-type-list">';
foreach ($content as $item) {
$output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
$output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
}
$output .= '</dl>';
}
else {
$output = '<p>' . t('You have not created any organization types yet. Go to the <a href="@link">organization type creation page</a> to add a new RedHen organization type.', array(
'@link' => url('admin/structure/redhen/org_types'),
)) . '</p>';
}
return $output;
}
function theme_redhen_org_list($variables) {
$header = $variables['header'];
$rows = $variables['rows'];
$render['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No organizations available.'),
);
$render['pager'] = array(
'#theme' => 'pager',
);
return render($render);
}
function redhen_org_redhen_relation_role_permissions() {
$roles = array(
'edit_org' => array(
'label' => 'Edit Organization',
),
'delete_org' => array(
'label' => 'Delete Organization',
),
);
return $roles;
}