View source
<?php
function profile2_entity_info() {
$return = array(
'profile2' => array(
'label' => t('Profile'),
'plural label' => t('Profiles'),
'description' => t('Profile2 user profiles.'),
'entity class' => 'Profile',
'controller class' => 'Profile2Controller',
'base table' => 'profile',
'revision table' => 'profile_revision',
'fieldable' => TRUE,
'view modes' => array(
'account' => array(
'label' => t('User account'),
'custom settings' => FALSE,
),
'revision' => array(
'label' => t('Revision display'),
'custom settings' => FALSE,
),
),
'entity keys' => array(
'id' => 'pid',
'revision' => 'vid',
'bundle' => 'type',
'label' => 'label',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'access callback' => 'profile2_access',
'module' => 'profile2',
'metadata controller class' => 'Profile2MetadataController',
),
);
$types = db_select('profile_type', 'p')
->fields('p')
->execute()
->fetchAllAssoc('type');
foreach ($types as $type => $info) {
$return['profile2']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/profiles/manage/%profile2_type',
'real path' => 'admin/structure/profiles/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array(
'administer profiles',
),
),
);
}
if (module_exists('entitycache')) {
$return['profile2']['field cache'] = FALSE;
$return['profile2']['entity cache'] = TRUE;
}
$return['profile2_type'] = array(
'label' => t('Profile type'),
'plural label' => t('Profile types'),
'description' => t('Profiles types of Profile2 user profiles.'),
'entity class' => 'ProfileType',
'controller class' => 'EntityAPIControllerExportable',
'base table' => 'profile_type',
'fieldable' => FALSE,
'bundle of' => 'profile2',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'access callback' => 'profile2_type_access',
'module' => 'profile2',
'admin ui' => array(
'path' => 'admin/structure/profiles',
'file' => 'profile2.admin.inc',
'controller class' => 'Profile2TypeUIController',
),
);
return $return;
}
function profile2_type_load($type) {
return profile2_get_types($type);
}
function profile2_menu() {
$items = array();
if (module_exists('devel_generate')) {
$items['admin/config/development/generate/profile2'] = array(
'title' => 'Generate profiles',
'description' => 'Generate a given number of profiles for users. Optionally override current user profiles.',
'access arguments' => array(
'administer profiles',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'profile2_generate_form',
),
'file' => 'profile2.devel.inc',
);
}
$items['user/%profile2_by_uid/%/delete'] = array(
'title' => 'Delete',
'description' => 'Delete Profile of User.',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'profile2_delete_confirm_form',
1,
),
'load arguments' => array(
2,
),
'access callback' => 'profile2_access',
'access arguments' => array(
'delete',
1,
),
'file' => 'profile2.delete.inc',
);
foreach (profile2_get_types() as $type_name => $type) {
if (empty($type->data['use_page'])) {
if (module_exists('profile2_diff')) {
$page_callback = 'profile2_diff_diffs_overview_user';
$path = drupal_get_path('module', 'profile2_diff');
$file = 'profile2_diff.pages.inc';
}
else {
$page_callback = 'profile2_revisions_view_for_user';
$path = NULL;
$file = 'profile2.revisions.inc';
}
$items['user/%user/revisions/' . $type_name] = array(
'page callback' => $page_callback,
'page arguments' => array(
$type_name,
1,
),
'access callback' => '_profile2_revision_tab_access',
'access arguments' => array(
$type_name,
1,
array(
'view own profile revisions',
'view any profile revisions',
),
),
'title callback' => 'check_plain',
'title arguments' => array(
$type
->getTranslation('label'),
),
'type' => MENU_LOCAL_TASK,
'file path' => $path,
'file' => $file,
);
$items['user/%user/revisions/' . $type_name . '/view'] = array(
'title' => 'View revision',
'page callback' => 'profile2_tab_revision_view',
'page arguments' => array(
$type_name,
1,
5,
),
'access callback' => '_profile2_revision_tab_access',
'access arguments' => array(
$type_name,
1,
array(
'view own profile revisions',
'view any profile revisions',
),
),
'type' => MENU_LOCAL_TASK,
'file' => 'profile2.revisions.inc',
);
$items['user/%user/revisions/' . $type_name . '/%/revert'] = array(
'title' => 'Revert to earlier revision',
'page callback' => 'profile2_tab_get_revision_revert',
'page arguments' => array(
$type_name,
1,
4,
),
'access callback' => '_profile2_revision_tab_access',
'access arguments' => array(
$type_name,
1,
array(
'revert own profile revisions',
'revert any profile revisions',
),
),
'type' => MENU_CALLBACK,
'file' => 'profile2.revisions.inc',
);
$items['user/%user/revisions/' . $type_name . '/%/delete'] = array(
'title' => 'Delete earlier revision',
'page callback' => 'profile2_tab_get_revision_delete',
'page arguments' => array(
$type_name,
1,
4,
),
'access callback' => '_profile2_revision_tab_access',
'access arguments' => array(
$type_name,
1,
array(
'delete own profile revisions',
'delete any profile revisions',
),
),
'type' => MENU_CALLBACK,
'file' => 'profile2.revisions.inc',
);
}
}
return $items;
}
function profile2_menu_alter(&$items) {
foreach (profile2_get_types() as $type) {
if (empty($type->data['use_page'])) {
if (!isset($items['user/%user/revisions'])) {
$items['user/%user/revisions'] = array(
'title' => 'Revisions',
'page callback' => '_profile2_user_revisions',
'access callback' => '_profile2_any_revision_access',
'access arguments' => array(
1,
),
'weight' => 2,
'type' => MENU_LOCAL_TASK,
);
}
else {
$items['user/%user/revisions']['page arguments'][] = $items['user/%user/revisions']['page callback'];
$items['user/%user/revisions']['page callback'] = '_profile2_user_revisions';
$items['user/%user/revisions']['access callback'] = '_profile2_any_revision_access';
$items['user/%user/revisions']['access arguments'] = array(
1,
TRUE,
);
}
unset($items['user/%user/revisions/list']);
$items['user/%user/revisions/account'] = array(
'title' => 'Account',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
break;
}
}
}
function _profile2_user_revisions($account = NULL, $overview_function = NULL) {
if ($account && _user_revision_access($account, array(
'view user revisions',
'view own user revisions',
))) {
return $overview_function($account);
}
else {
return array(
'no_user_revision' => array(
'#type' => 'markup',
'#markup' => t('No User Account revisions available'),
'#prefix' => '<h3>',
'#suffix' => '</h3>',
),
);
}
}
function profile2_admin_paths() {
$paths = array();
foreach (profile2_get_types() as $type_name => $type) {
if (!empty($type->data['revision_admin'])) {
if (empty($type->data['use_page'])) {
$paths["user/*/revisions/{$type_name}"] = TRUE;
$paths["user/*/revisions/{$type_name}/*"] = TRUE;
}
else {
$paths["profile-{$type_name}/*/revisions"] = TRUE;
$paths["profile-{$type_name}/*/revisions/*"] = TRUE;
}
}
}
return $paths;
}
function profile2_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/structure/profiles') {
$item = menu_get_item('admin/people/permissions');
if ($item['access']) {
$item['title'] = t('Manage permissions');
if (module_exists('filter_perms')) {
$item['href'] .= '/module-profile2';
}
else {
$item['localized_options']['fragment'] = 'module-profile2';
}
$item['weight'] = 3;
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function profile2_permission() {
$permissions = array(
'administer profile types' => array(
'title' => t('Administer profile types'),
'description' => t('Create and delete fields on user profiles, and set their permissions.'),
),
'administer profiles' => array(
'title' => t('Administer profiles'),
'description' => t('Edit and view all user profiles.'),
),
'view own profile revisions' => array(
'title' => t('View own profile revisions'),
),
'revert own profile revisions' => array(
'title' => t('Revert own profile revisions'),
),
'delete own profile revisions' => array(
'title' => t('Delete own profile revisions'),
),
'view any profile revisions' => array(
'title' => t('View any profile revisions'),
),
'revert any profile revisions' => array(
'title' => t('Revert any profile revisions'),
),
'delete any profile revisions' => array(
'title' => t('Delete any profile revisions'),
),
'allow to choose profile2 revision creation' => array(
'title' => t('Allow to choose profile2 revision creation'),
),
);
foreach (profile2_get_types() as $type) {
$type_name = check_plain($type->type);
$permissions += array(
"edit own {$type_name} profile" => array(
'title' => t('%type_name: Edit own profile', array(
'%type_name' => $type
->getTranslation('label'),
)),
),
"edit any {$type_name} profile" => array(
'title' => t('%type_name: Edit any profile', array(
'%type_name' => $type
->getTranslation('label'),
)),
),
"view own {$type_name} profile" => array(
'title' => t('%type_name: View own profile', array(
'%type_name' => $type
->getTranslation('label'),
)),
),
"view any {$type_name} profile" => array(
'title' => t('%type_name: View any profile', array(
'%type_name' => $type
->getTranslation('label'),
)),
),
"delete own {$type_name} profile" => array(
'title' => t('%type_name: Delete own profile', array(
'%type_name' => $type
->getTranslation('label'),
)),
),
);
}
return $permissions;
}
function profile2_get_types($type_name = NULL) {
$types = entity_load_multiple_by_name('profile2_type', isset($type_name) ? array(
$type_name,
) : FALSE);
return isset($type_name) ? reset($types) : $types;
}
function profile2_load($pid, $vid = NULL, $reset = FALSE) {
$pids = isset($pid) ? array(
$pid,
) : array();
$conditions = isset($vid) ? array(
'vid' => $vid,
) : array();
$profiles = profile2_load_multiple($pids, $conditions, $reset);
return reset($profiles);
}
function profile2_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('profile2', $pids, $conditions, $reset);
}
function profile2_load_by_user($account, $type_name = NULL) {
$cache =& drupal_static(__FUNCTION__, array());
$uid = is_object($account) ? $account->uid : $account;
if (!isset($cache[$uid])) {
$cache[$uid] = db_select('profile', 'p')
->fields('p', array(
'type',
'pid',
))
->condition('uid', $uid)
->execute()
->fetchAllKeyed();
}
if (isset($type_name)) {
return isset($cache[$uid][$type_name]) ? profile2_load($cache[$uid][$type_name]) : FALSE;
}
return $cache[$uid] ? array_combine(array_keys($cache[$uid]), profile2_load_multiple($cache[$uid])) : $cache[$uid];
}
function profile2_profile2_delete($profile) {
$cache =& drupal_static('profile2_load_by_user', array());
unset($cache[$profile->uid][$profile->type]);
}
function profile2_delete(Profile $profile) {
$profile
->delete();
}
function profile2_delete_multiple(array $pids) {
entity_get_controller('profile2')
->delete($pids);
}
function profile2_user_delete($account) {
foreach (profile2_load_by_user($account) as $profile) {
profile2_delete($profile);
}
}
function profile2_create(array $values) {
return new Profile($values);
}
function profile_create(array $values) {
return new Profile($values);
}
function profile2_save(Profile $profile) {
return $profile
->save();
}
function profile2_type_save(ProfileType $type) {
$type
->save();
}
function profile2_type_delete(ProfileType $type) {
$type
->delete();
}
function profile2_profile2_type_insert(ProfileType $type) {
variable_set('menu_rebuild_needed', TRUE);
}
function profile2_profile2_type_update(ProfileType $type) {
variable_set('menu_rebuild_needed', TRUE);
}
function profile2_profile2_type_delete(ProfileType $type) {
if (!$type
->hasStatus(ENTITY_IN_CODE)) {
$pids = array_keys(profile2_load_multiple(FALSE, array(
'type' => $type->type,
)));
if ($pids) {
profile2_delete_multiple($pids);
}
variable_set('menu_rebuild_needed', TRUE);
}
}
function profile2_user_view($account, $view_mode, $langcode) {
foreach (profile2_get_types() as $type => $profile_type) {
if ($profile_type->userView && ($profile = profile2_load_by_user($account, $type))) {
if (profile2_access('view', $profile)) {
$account->content['profile_' . $type] = array(
'#type' => 'user_profile_category',
'#title' => $profile_type
->getTranslation('label'),
'#prefix' => '<a id="profile-' . $profile->type . '"></a>',
);
$account->content['profile_' . $type]['view'] = $profile
->view($view_mode);
}
}
}
}
function profile2_form_user_profile_form_alter(&$form, &$form_state) {
global $user;
if (($type = profile2_get_types($form['#user_category'])) && $type->userCategory) {
if (empty($form_state['profiles'])) {
$profile = profile2_load_by_user($form['#user'], $form['#user_category']);
if (empty($profile)) {
$profile = profile2_create(array(
'type' => $form['#user_category'],
'uid' => $form['#user']->uid,
));
$profile->is_new = TRUE;
}
$form_state['profiles'][$profile->type] = $profile;
}
if (user_access('administer profiles') || $user->uid === $profile->uid && user_access("delete own {$profile->type} profile")) {
$str_button_value = t('Delete profile');
}
if (empty($profile->is_new) && !empty($str_button_value)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => $str_button_value,
'#weight' => 45,
'#limit_validation_errors' => array(),
'#submit' => array(
'profile2_form_submit_own_delete',
),
);
}
if (($key = array_search('user_profile_form_validate', $form['#validate'])) !== FALSE) {
unset($form['#validate'][$key]);
}
if (($key = array_search('user_profile_form_submit', $form['#submit'])) !== FALSE) {
unset($form['#submit'][$key]);
}
profile2_attach_form($form, $form_state);
}
}
function profile2_form_submit_own_delete($form, &$form_state) {
$profile = $form_state['profiles'][$form['#user_category']];
if (isset($profile) && is_object($profile)) {
$form_state['redirect'] = 'user/' . $profile->uid . '/' . $form['#user_category'] . '/delete';
}
}
function profile2_form_user_register_form_alter(&$form, &$form_state) {
$profile_types = profile2_get_types();
if (!empty($profile_types)) {
foreach ($profile_types as $type_name => $profile_type) {
if (!empty($profile_type->data['registration'])) {
if (empty($form_state['profiles'][$type_name])) {
$form_state['profiles'][$type_name] = profile2_create(array(
'type' => $type_name,
));
}
}
}
}
if (!empty($form_state['profiles'])) {
profile2_attach_form($form, $form_state);
foreach ($form_state['profiles'] as $type_name => $profile) {
$form['profile_' . $type_name] += array(
'#type' => 'fieldset',
'#title' => check_plain($profile_types[$type_name]
->getTranslation('label')),
);
}
}
}
function profile2_attach_form(&$form, &$form_state) {
$allowed = user_access('allow to choose profile2 revision creation') || user_access('administer profiles');
foreach ($form_state['profiles'] as $type => $profile) {
$profile_type = profile2_get_types($type);
if ($allowed || $profile_type->data['revisions']) {
$form['profile_' . $profile->type]['revision_information'] = array(
'#type' => 'fieldset',
'#title' => t('Revision information'),
'#collapsible' => FALSE,
'#attributes' => array(
'class' => array(
'user-profile-form-revision-information',
),
),
'#weight' => 30,
);
$form['profile_' . $profile->type]['revision_information']['revision'] = array(
'#type' => $allowed ? 'checkbox' : 'value',
'#title' => t('Create new revision'),
'#default_value' => $profile_type->data['revisions'],
);
$checkbox_name = "profile_{$profile->type}[revision_information][revision]";
$form['profile_' . $profile->type]['revision_information']['log'] = array(
'#type' => 'textarea',
'#title' => t('Revision log message'),
'#rows' => 4,
'#description' => t('Provide an explanation of the changes you are making. This will help other authors understand your motivations.'),
'#states' => array(
'invisible' => array(
'input[name="' . $checkbox_name . '"]' => array(
'checked' => FALSE,
),
),
),
);
}
if (is_null($profile->pid)) {
$form['profile_' . $profile->type]['revision_information']['revision']['#default_value'] = 1;
$form['profile_' . $profile->type]['revision_information']['log']['#value'] = t('Initial revision.');
$form['profile_' . $profile->type]['revision_information']['#access'] = FALSE;
}
$form['profile_' . $profile->type]['#tree'] = TRUE;
$form['profile_' . $profile->type]['#parents'] = array(
'profile_' . $profile->type,
);
field_attach_form('profile2', $profile, $form['profile_' . $profile->type], $form_state);
if (user_access('administer profile types')) {
if (count(field_info_instances('profile2', $profile->type)) == 0) {
$form['profile_' . $profile->type]['message'] = array(
'#markup' => t('No fields have been associated with this profile type. Go to the <a href="!url">Profile types</a> page to add some fields.', array(
'!url' => url('admin/structure/profiles'),
)),
);
}
}
$form['profile_' . $profile->type]['#pre_render'] = array_unique($form['profile_' . $profile->type]['#pre_render']);
if (!isset($form_state['profile2_skip_hook'])) {
$hooks[] = 'form_profile2_edit_' . $type . '_form';
$hooks[] = 'form_profile2_form';
drupal_alter($hooks, $form, $form_state);
}
}
$form['#validate'][] = 'profile2_form_validate_handler';
$register_submit_callback = 'user_register_submit';
if (module_exists('logintoboggan')) {
$register_submit_callback = 'logintoboggan_user_register_submit';
}
if (!empty($form['#submit']) && is_array($form['#submit'])) {
$submit_key = array_search($register_submit_callback, $form['#submit']);
}
if (isset($submit_key) && $submit_key !== FALSE && !in_array('profile2_form_before_user_register_submit_handler', $form['#submit'])) {
array_splice($form['#submit'], $submit_key, 0, array(
'profile2_form_before_user_register_submit_handler',
));
array_splice($form['#submit'], $submit_key + 2, 0, array(
'profile2_form_submit_handler',
));
array_splice($form['#submit'], $submit_key + 3, 0, array(
'profile2_form_after_user_register_submit_handler',
));
}
else {
$form['#submit'][] = 'profile2_form_submit_handler';
}
}
function profile2_form_validate_handler(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
if (isset($form_state['values']['profile_' . $profile->type])) {
$pseudo_entity = (object) $form_state['values']['profile_' . $profile->type];
$pseudo_entity->type = $type;
field_attach_form_validate('profile2', $pseudo_entity, $form['profile_' . $profile->type], $form_state);
}
}
}
function profile2_form_before_user_register_submit_handler(&$form, &$form_state) {
global $conf;
$register_ops = array(
'register_admin_created',
'register_no_approval_required',
'register_pending_approval',
);
$changed_ops =& drupal_static('profile2_register_changed_operations', array());
foreach ($register_ops as $op) {
if (isset($conf['user_mail_' . $op . '_notify'])) {
$changed_ops['user_mail_' . $op . '_notify'] = $conf['user_mail_' . $op . '_notify'];
}
$conf['user_mail_' . $op . '_notify'] = FALSE;
}
}
function profile2_form_after_user_register_submit_handler(&$form, &$form_state) {
global $conf;
$changed_ops =& drupal_static('profile2_register_changed_operations', array());
$register_ops = array(
'register_admin_created',
'register_no_approval_required',
'register_pending_approval',
);
foreach ($register_ops as $op) {
if (isset($changed_ops['user_mail_' . $op . '_notify'])) {
$conf['user_mail_' . $op . '_notify'] = $changed_ops['user_mail_' . $op . '_notify'];
}
else {
unset($conf['user_mail_' . $op . '_notify']);
}
}
$admin = !empty($form_state['values']['administer_users']);
$account = $form_state['user'];
$notify = !empty($form_state['values']['notify']);
if ($admin && !$notify) {
}
elseif (!$admin && !variable_get('user_email_verification', TRUE) && $account->status) {
_user_mail_notify('register_no_approval_required', $account);
}
elseif ($account->status || $notify) {
$op = $notify ? 'register_admin_created' : 'register_no_approval_required';
_user_mail_notify($op, $account);
}
elseif (!$admin) {
_user_mail_notify('register_pending_approval', $account);
}
}
function profile2_form_submit_handler(&$form, &$form_state) {
profile2_form_submit_build_profile($form, $form_state);
profile2_form_submit_cleanup($form, $form_state);
foreach ($form_state['profiles'] as $type => $profile) {
if (empty($profile->uid) && isset($form_state['user']->uid)) {
$profile->uid = $form_state['user']->uid;
}
else {
drupal_set_message(t('The changes have been saved.'));
}
profile2_save($profile);
}
}
function profile2_form_submit_build_profile(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
if (isset($form['profile_' . $type]['#entity_builders'])) {
foreach ($form['profile_' . $type]['#entity_builders'] as $function) {
$function('profile2', $profile, $form['profile_' . $type], $form_state);
}
}
field_attach_submit('profile2', $profile, $form['profile_' . $type], $form_state);
if (!empty($form_state['values']['profile_' . $type]['revision_information']['revision'])) {
global $user;
$profile->authorid = $user->uid;
$profile->timestamp = REQUEST_TIME;
$profile->log = empty($form_state['values']['profile_' . $type]['revision_information']['log']) ? '' : $form_state['values']['profile_' . $type]['revision_information']['log'];
$profile->is_new_revision = TRUE;
}
elseif ($profile->vid) {
$profile_orig = profile2_revision_list($profile)[$profile->vid];
$profile->log = $profile_orig->log;
}
}
}
function profile2_form_submit_cleanup(&$form, &$form_state) {
foreach ($form_state['profiles'] as $type => $profile) {
unset($form_state['values']['profile_' . $type]);
}
}
function profile2_user_categories() {
$data = array();
foreach (profile2_get_types() as $type => $info) {
if ($info->userCategory) {
$data[] = array(
'name' => $type,
'title' => $info
->getTranslation('label'),
'weight' => $info->weight + 3,
'access callback' => 'profile2_category_access',
'access arguments' => array(
1,
$type,
),
);
}
}
return $data;
}
function profile2_category_access($account, $type_name) {
$profile = profile2_load_by_user($account, $type_name);
if (empty($profile)) {
$profile = profile2_create(array(
'type' => $type_name,
'uid' => $account->uid,
));
}
return $account->uid > 0 && $profile
->type()->userCategory && profile2_access('edit', $profile);
}
function profile2_role_access($profile) {
if (isset($profile->type)) {
$profile_type = profile2_type_load($profile->type);
if (!empty($profile_type) && !empty($profile_type->data['roles']) && isset($profile->uid)) {
$profile_user = user_load($profile->uid);
$profile_roles = array_keys($profile_type->data['roles']);
$user_roles = array_keys($profile_user->roles);
$matches = array_intersect($profile_roles, $user_roles);
if (empty($matches)) {
return FALSE;
}
}
}
return TRUE;
}
function profile2_access($op, $profile = NULL, $account = NULL) {
if (isset($profile) && !profile2_role_access($profile)) {
return FALSE;
}
if (user_access('administer profiles', $account)) {
return TRUE;
}
if ($op == 'create' || $op == 'update') {
$op = 'edit';
}
$access = module_invoke_all('profile2_access', $op, $profile, $account);
if (in_array(FALSE, $access, TRUE)) {
return FALSE;
}
elseif (in_array(TRUE, $access, TRUE)) {
return TRUE;
}
return FALSE;
}
function profile2_profile2_access($op, $profile = NULL, $account = NULL) {
if (isset($profile) && ($type_name = $profile->type)) {
if (user_access("{$op} any {$type_name} profile", $account)) {
return TRUE;
}
$account = isset($account) ? $account : $GLOBALS['user'];
if (isset($profile->uid) && $profile->uid == $account->uid && user_access("{$op} own {$type_name} profile", $account)) {
return TRUE;
}
}
}
function profile2_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer profile types', $account);
}
function profile2_theme() {
return array(
'profile2' => array(
'render element' => 'elements',
'template' => 'profile2',
),
);
}
class Profile extends Entity {
public $pid;
public $vid;
public $type;
public $label;
public $uid;
public $created;
public $changed;
public function __construct($values = array()) {
if (isset($values['user'])) {
$this
->setUser($values['user']);
unset($values['user']);
}
if (isset($values['type']) && is_object($values['type'])) {
$values['type'] = $values['type']->type;
}
if (!isset($values['label']) && isset($values['type']) && ($type = profile2_get_types($values['type']))) {
$values['label'] = $type->label;
}
if (!isset($values['log'])) {
$values['log'] = '';
}
parent::__construct($values, 'profile2');
}
public function user() {
return user_load($this->uid);
}
public function setUser($account) {
$this->uid = is_object($account) ? $account->uid : $account;
}
public function type() {
return profile2_get_types($this->type);
}
public function url() {
$uri = $this
->uri();
return url($uri['path'], $uri);
}
public function path() {
$uri = $this
->uri();
return $uri['path'];
}
public function defaultUri() {
return array(
'path' => 'user/' . $this->uid,
'options' => array(
'fragment' => 'profile-' . $this->type,
),
);
}
public function defaultLabel() {
$type = profile2_get_types($this->type);
if (!empty($type->data['edit_label'])) {
return $this->label;
}
else {
return t('@type profile for @user', array(
'@type' => $type
->getTranslation('label'),
'@user' => format_username($this
->user()),
));
}
}
public function buildContent($view_mode = 'full', $langcode = NULL) {
$content = array();
if (!empty($this->is_new)) {
$content['empty']['#markup'] = '<em class="profile2-no-data">' . t('There is no profile data yet.') . '</em>';
}
return entity_get_controller($this->entityType)
->buildContent($this, $view_mode, $langcode, $content);
}
public function save() {
$existing_profile = profile2_load_by_user($this->uid, $this->type);
if (empty($this->pid) && !empty($existing_profile)) {
watchdog('profile2_create', '@type profile already exists for user @uid', array(
'@uid' => $this->uid,
'@type' => $this->type,
), WATCHDOG_WARNING);
return;
}
if (empty($this->created) && (!empty($this->is_new) || !$this->pid)) {
$this->created = REQUEST_TIME;
}
$this->changed = REQUEST_TIME;
$cache =& drupal_static('profile2_load_by_user', array());
unset($cache[$this->uid]);
return parent::save();
}
public function revisionDelete($revision_id) {
entity_get_controller($this->entityType)
->revisionDelete($revision_id);
}
}
class Profile2Controller extends EntityAPIController {
function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
$fields =& $query
->getFields();
unset($fields['timestamp']);
$query
->addField('revision', 'timestamp', 'revision_timestamp');
$query
->addField('revision', 'authorid', 'revision_uid');
return $query;
}
protected function saveRevision($entity) {
if (!isset($entity->timestamp)) {
$entity->timestamp = REQUEST_TIME;
}
if (!isset($entity->revision_timestamp)) {
$entity->revision_timestamp = REQUEST_TIME;
}
parent::saveRevision($entity);
}
}
class ProfileType extends Entity {
public $userCategory = TRUE;
public $userView = TRUE;
public $type;
public $label;
public $weight = 0;
public function __construct($values = array()) {
parent::__construct($values, 'profile2_type');
}
public function isLocked() {
return isset($this->status) && empty($this->is_new) && ($this->status & ENTITY_IN_CODE || $this->status & ENTITY_FIXED);
}
public function getTranslation($property, $langcode = NULL) {
if (module_exists('profile2_i18n')) {
return parent::getTranslation($property, $langcode);
}
return $this->{$property};
}
public function save() {
parent::save();
field_info_cache_clear();
}
}
function profile2_view($profile, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$account = user_load($profile->uid);
drupal_set_title(t('Revision of %profile from %date', array(
'%profile' => $profile
->label(),
'%date' => format_date($profile->revision_timestamp),
)), PASS_THROUGH);
return $profile
->view($view_mode, $langcode, $page);
}
function profile2_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
if (!empty($form['instance']['entity_type']['#value']) && $form['instance']['entity_type']['#value'] == 'profile2') {
$form['field']['settings']['profile2_private'] = array(
'#type' => 'checkbox',
'#title' => t('Make the content of this field private.'),
'#default_value' => !empty($form['#field']['settings']['profile2_private']),
'#description' => t('If checked, the content of this field is only shown to the profile owner and administrators.'),
);
}
else {
$form['field']['settings']['profile2_private'] = array(
'#type' => 'value',
'#value' => !empty($form['#field']['settings']['profile2_private']),
);
}
}
function profile2_field_access($op, $field, $entity_type, $profile = NULL, $account = NULL) {
if ($entity_type == 'profile2' && $op == 'view' && isset($profile)) {
if (!profile2_role_access($profile)) {
return FALSE;
}
if (!empty($field['settings']['profile2_private']) && !user_access('administer profiles', $account)) {
$account = isset($account) ? $account : $GLOBALS['user'];
if ($account->uid != $profile->uid) {
return FALSE;
}
}
}
}
function profile2_field_extra_fields() {
$extra = array();
foreach (profile2_get_types() as $type_name => $type) {
if (!empty($type->userView)) {
$extra['user']['user']['display']['profile_' . $type_name] = array(
'label' => t('Profile: @profile', array(
'@profile' => $type->label,
)),
'weight' => $type->weight,
);
}
if (!empty($type->data['registration'])) {
$extra['user']['user']['form']['profile_' . $type_name] = array(
'label' => t('Profile: @profile', array(
'@profile' => $type->label,
)),
'description' => t('Appears during registration only.'),
'weight' => $type->weight,
);
}
}
return $extra;
}
function profile2_user_get_properties($account, array $options, $name) {
$profile = profile2_load_by_user($account, substr($name, 8));
return $profile ? $profile : NULL;
}
function profile2_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && !empty($plugin_type)) {
return 'plugins/' . $plugin_type;
}
}
function profile2_preprocess_ctools_context_item_form(&$vars) {
unset($vars['form']['buttons']['relationship']['item']['#options']['entity_from_schema:uid-user-profile2']);
}
function profile2_delete_access($uid, $type_name) {
$profile = profile2_by_uid_load($uid, $type_name);
return is_object($profile) ? profile2_access('edit', $profile) : FALSE;
}
function profile2_by_uid_load($uid, $type_name) {
if ($uid && is_numeric($uid) && ($account = user_load($uid))) {
$profile = profile2_load_by_user($account, $type_name);
if (!$profile) {
$profile = profile2_create(array(
'type' => $type_name,
));
$profile
->setUser($account);
$profile->is_new = TRUE;
}
return $profile;
}
return FALSE;
}
function profile2_preprocess_page(&$vars) {
if (!empty($vars['page']['content']['system_main']['#user_category'])) {
$ptype = $vars['page']['content']['system_main']['#user_category'];
if (!empty($vars['page']['content']['system_main']["profile_{$ptype}"])) {
$item = $vars['page']['content']['system_main']["profile_{$ptype}"];
if (!empty($item['#entity'])) {
$vars['title'] = $item['#entity']
->label();
}
}
}
}
function profile2_revision_list($profile) {
$revisions = array();
$result = db_query('SELECT r.vid, r.log, r.authorid AS uid, p.vid AS current_vid, r.timestamp, u.name FROM {profile_revision} r LEFT JOIN {profile} p ON p.vid = r.vid INNER JOIN {users} u ON u.uid = r.authorid WHERE r.pid = :pid ORDER BY r.vid DESC', array(
':pid' => $profile->pid,
));
foreach ($result as $revision) {
$revisions[$revision->vid] = $revision;
}
return $revisions;
}
function _profile2_revision_base_path($profile, $ops = FALSE) {
return empty($profile
->type()->data['use_page']) ? "user/{$profile->uid}/revisions/{$profile->type}" : "profile-{$profile->type}/{$profile->uid}/revisions" . ($ops ? '/ops' : '');
}
function profile2_revision_overview($profile) {
$account = user_load($profile->uid);
drupal_set_title(t('Revisions for %profile_type profile of %user', array(
'%profile_type' => $profile->label,
'%user' => $account->name,
)), PASS_THROUGH);
$header = array(
t('Revision'),
array(
'data' => t('Operations'),
'colspan' => 2,
),
);
$data = profile2_revision_list_build($profile);
$rows = array();
foreach ($data as $item) {
$rows[] = array_merge($item['row'], $item['operations']);
}
$build['profile2_revisions_table'] = array(
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
);
return $build;
}
function profile2_revision_list_build($profile) {
$revisions = profile2_revision_list($profile);
$data = array();
foreach ($revisions as $revision) {
$row = array();
$operations = array();
$link_base = _profile2_revision_base_path($profile, TRUE);
$revert_permission = user_access('administer profiles') || profile2_access('update', $profile) && _profile2_revision_access($profile, array(
'revert own profile revisions',
'revert any profile revisions',
));
$delete_permission = user_access('administer profiles') || profile2_access('update', $profile) && _profile2_revision_access($profile, array(
'delete own profile revisions',
'delete any profile revisions',
));
$vid = $revision->vid;
$row[$vid] = array(
'data' => t('!date by !username', array(
'!date' => l(format_date($revision->timestamp), "{$link_base}/view/{$vid}"),
'!username' => theme('username', array(
'account' => $revision,
)),
)) . ($revision->log != '' ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : ''),
'revision' => $revision,
);
if ($revision->current_vid > 0) {
$operations[] = array(
'data' => drupal_placeholder(t('current revision')),
'class' => array(
'revision-current',
),
'colspan' => 2,
);
}
else {
$operations[] = $revert_permission ? l(t('revert'), "{$link_base}/{$vid}/revert") : '';
$operations[] = $delete_permission ? l(t('delete'), "{$link_base}/{$vid}/delete") : '';
}
$data[] = array(
'row' => $row,
'operations' => $operations,
);
}
return $data;
}
function profile2_revision_revert_confirm($form, $form_state, $profile_revision) {
$form['#profile_revision'] = $profile_revision;
$cancel_path = _profile2_revision_base_path($profile_revision);
return confirm_form($form, t('Are you sure you want to revert to the revision from %date?', array(
'%date' => format_date($profile_revision->revision_timestamp),
)), $cancel_path, '', t('Revert'), t('Cancel'));
}
function profile2_revision_revert_confirm_submit($form, &$form_state) {
$profile_revision = $form['#profile_revision'];
$profile_revision->revision = 1;
$profile_revision->log = t('Copy of the revision from %date.', array(
'%date' => format_date($profile_revision->revision_timestamp),
));
$profile_revision->changed = REQUEST_TIME;
$profile_revision->timestamp = REQUEST_TIME;
entity_revision_set_default('profile2', $profile_revision);
entity_save('profile2', $profile_revision);
drupal_set_message(t('@type profile has been reverted back to the revision from %date.', array(
'@type' => $profile_revision->type,
'%date' => format_date($profile_revision->revision_timestamp),
)));
$redirect = _profile2_revision_base_path($profile_revision);
$form_state['redirect'] = $redirect;
}
function profile2_revision_delete_confirm($form, $form_state, $profile_revision) {
$form['#profile_revision'] = $profile_revision;
$cancel_path = _profile2_revision_base_path($profile_revision);
return confirm_form($form, t('Are you sure you want to delete the revision from %date?', array(
'%date' => format_date($profile_revision->revision_timestamp),
)), $cancel_path, t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function profile2_revision_delete_confirm_submit($form, &$form_state) {
$profile_revision = $form['#profile_revision'];
$deleted = entity_revision_delete('profile2', $profile_revision->vid);
if ($deleted) {
drupal_set_message(t('@type profile revision from %date has been deleted.', array(
'@type' => $profile_revision->type,
'%date' => format_date($profile_revision->revision_timestamp),
)));
}
else {
drupal_set_message(t('Error occured while deleting revision.'), 'error');
}
$redirect = _profile2_revision_base_path($profile_revision);
if (db_select('profile_revision', 'pr')
->condition('pr.pid', $profile_revision->pid)
->countQuery()
->execute()
->fetchField() < 2) {
$redirect = str_replace('/revisions', '', $redirect);
}
$form_state['redirect'] = $redirect;
}
function profile2_views_api() {
return array(
'api' => '3.0',
'path' => drupal_get_path('module', 'profile2') . '/views',
);
}
function _profile2_revision_access($profile, $perm) {
if (empty($profile)) {
return FALSE;
}
global $user;
if (!is_array($perm)) {
$perm = array(
$perm,
);
}
$access = FALSE;
foreach ($perm as $permission) {
if ($profile->uid == $user->uid) {
$access = user_access($permission) || $access;
}
elseif (strpos($permission, 'own') === FALSE) {
$access = user_access($permission) || $access;
}
}
$count =& drupal_static(__FUNCTION__, array());
if (!isset($count[$profile->pid])) {
$count[$profile->pid] = db_select('profile_revision', 'pr')
->condition('pr.pid', $profile->pid)
->countQuery()
->execute()
->fetchField();
}
return $access && $count[$profile->pid] > 1;
}
function _profile2_revision_tab_access($type, $account, $permissions) {
$profile = profile2_load_by_user($account, $type);
return _profile2_revision_access($profile, $permissions);
}
function _profile2_any_revision_access($account, $check_user = FALSE) {
foreach (profile2_get_types() as $type_name => $type) {
if (empty($type->data['use_page'])) {
$profile = profile2_load_by_user($account, $type_name);
if (_profile2_revision_access($profile, array(
'view own profile revisions',
'view any profile revisions',
))) {
return TRUE;
}
}
}
if ($check_user) {
return _user_revision_access($account, array(
'view user revisions',
'view own user revisions',
));
}
return FALSE;
}