diff --git a/lib/fn/btr_user.php b/lib/fn/btr_user.php index 98ff818..d4aa7f0 100644 --- a/lib/fn/btr_user.php +++ b/lib/fn/btr_user.php @@ -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']); +} diff --git a/project/callback/dashboard.inc b/project/callback/dashboard.inc index f190f27..ce59fb5 100644 --- a/project/callback/dashboard.inc +++ b/project/callback/dashboard.inc @@ -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' => '
', bcl::get_block('btrProject', 'statistics'), @@ -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(); +} diff --git a/services/authenticated.inc b/services/authenticated.inc index c948b9f..6168473 100644 --- a/services/authenticated.inc +++ b/services/authenticated.inc @@ -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',