Skip to content
This repository was archived by the owner on Mar 4, 2019. It is now read-only.

Add a subscribe button to the dashboard of a project #13

Merged
merged 3 commits into from
Aug 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/fn/btr_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ function btr_user_set($data) {

$_SESSION['btrClient']['btr_user'] = $btr_user;
}

/**
* Remove btr_user from the session.
*/
function btr_user_expire() {
unset($_SESSION['btrClient']['btr_user']);
}
81 changes: 80 additions & 1 deletion project/callback/dashboard.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* Menu callback for 'btr/project/%/%/%/dashboard'
*/
function btrProject_dashboard($origin, $project, $lng) {
$output = [
$output[] = drupal_get_form('btrProject_subscribe_form', $origin, $project);

$output[] = [
'statistics' => [
'#prefix' => '<div class="col-sm-4">',
bcl::get_block('btrProject', 'statistics'),
Expand All @@ -28,3 +30,80 @@ function btrProject_dashboard($origin, $project, $lng) {

return $output;
}

/**
* Form callback for: btrProject_subscribe_form
*/
function btrProject_subscribe_form($form, &$form_state, $origin, $project) {
$subscribed = _btrProject_user_subscribed($origin, $project);
$form = [
'origin' => [
'#type' => 'hidden',
'#value' => $origin,
],
'project' => [
'#type' => 'hidden',
'#value' => $project,
],
'subscribe' => [
'#value' => t('Subscribe'),
'#type' => 'submit',
'#attributes' => ['class' => ['btn-primary']],
'#access' => !$subscribed,
],
'unsubscribe' => [
'#value' => t('Unsubscribe'),
'#type' => 'submit',
'#attributes' => ['class' => ['btn-primary']],
'#access' => $subscribed,
],
];

return $form;
}

/**
* Return TRUE if the user is subscribed to the given project.
*/
function _btrProject_user_subscribed($origin, $project) {
if (!bcl::user_is_authenticated()) return FALSE;

// Check the given project on the list of preferred_projects.
$btr_user = bcl::btr_user_get();
if (!isset($btr_user['preferred_projects'])) return FALSE;
return in_array("$origin/$project", $btr_user['preferred_projects']);
}

/**
* Submit callback for the form: btrProject_subscribe_form
*/
function btrProject_subscribe_form_submit($form, &$form_state) {
// If user is not authenticated, redirect to login first.
if (!bcl::user_is_authenticated()) {
bcl::user_authenticate($form_state, $redirection = ($form===NULL));
return;
}

// Get the parameters.
$values = $form_state['values'];
$operation = $values['op'];
$origin = $values['origin'];
$project = $values['project'];

$btr = wsclient_service_load('btr');
if ($operation == t('Subscribe')) {
// Subscribe to the project.
$btr->subscribe(['origin' => $origin, 'project' => $project]);
$msg = t("Subscribed to project '!project'.", ['!project' => "$origin/$project"]);
}
elseif ($operation == t('Unsubscribe')) {
// Unsubscribe from the project.
$btr->unsubscribe(['origin' => $origin, 'project' => $project]);
$msg = t("Unsubscribed from project '!project'.", ['!project' => "$origin/$project"]);
}
drupal_set_message($msg);

// Expire from cache the btr_user, so that it can be
// refreshed with the latest values.
bcl::btr_user_expire();
}
3 changes: 3 additions & 0 deletions services/authenticated.inc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ function _btrClientWS_authenticated_translations() {
*/
function _btrClientWS_authenticated_projects() {
$post_operations = array(
'subscribe' => 'project/subscribe',
'unsubscribe' => 'project/unsubscribe',
'subscriptions' => 'project/subscriptions',
'export_projects' => 'project/export',
'add_string' => 'project/add_string',
'del_string' => 'project/del_string',
Expand Down