示例#1
0
/** set or update the user's subscription
 *
 * @param $uid: user's id
 * @param $eid: event's id
 * @param $subs: user's new subscription
 *
 */
function subscribe($uid, $eid, $subs = array())
{
    global $globals;
    // get items
    $items = get_event_items($eid);
    // get previous subscription
    $old_subs = get_event_subscription($eid, $uid);
    $participate = false;
    $updated = false;
    // TODO : change the way to deal with manual payment
    $paid = 0;
    foreach ($old_subs as $item_id => $s) {
        $paid += $s['paid'];
    }
    $paid_updated = false;
    // for each item of the event
    foreach ($items as $item_id => $details) {
        // check if there is an old subscription
        if (array_key_exists($item_id, $old_subs)) {
            // compares new and old subscription
            if ($old_subs[$item_id]['nb'] != $subs[$item_id]) {
                if ($subs[$item_id] != 0) {
                    XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
                                       VALUES ({?}, {?}, {?}, {?}, {?}, {?})
                      ON DUPLICATE KEY UPDATE nb = VALUES(nb), flags = VALUES(flags), paid = VALUES(paid)', $eid, $uid, $item_id, $subs[$item_id], Env::has('notify_payment') ? 'notify_payment' : 0, !$paid_updated ? $paid : 0);
                    $participate = true;
                    $paid_updated = true;
                } else {
                    // we do not store non-subscription to event items
                    XDB::execute('DELETE FROM group_event_participants
                                        WHERE eid = {?} AND uid = {?} AND item_id = {?}', $eid, $uid, $item_id);
                }
                $updated = true;
            }
        } else {
            // if no old subscription
            if ($subs[$item_id] != 0) {
                XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
                                   VALUES ({?}, {?}, {?}, {?}, {?}, {?})', $eid, $uid, $item_id, $subs[$item_id], '', 0);
                $participate = true;
                $updated = true;
            }
        }
    }
    // item 0 stores whether the user participates globally or not, if he has to be notified when payment is created and his manual payment
    /*
    if (array_key_exists(0, $old_subs)) {
        XDB::execute('UPDATE group_event_participants
                         SET nb = {?}
                       WHERE eid = {?}, uid = {?}, item_id = 0',
                             ($participate ? 1 : 0), $eid, $uid);
    } else {
        XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
                           VALUES ({?}, {?}, {?}, {?}, {?}, {?})',
                                  $eid, $uid, 0, ($participate ? 1 : 0), (Env::has('notify_payment') ? 'notify_payment' : ''), 0);
    }
    */
    // if subscription is updated, we have to update the event aliases
    if ($updated) {
        $short_name = get_event_detail($eid)['short_name'];
        subscribe_lists_event($uid, $short_name, $participate ? 1 : -1, 0);
    }
    return $updated;
}
示例#2
0
 function handler_sub($page, $eid = null)
 {
     $this->load('xnetevents.inc.php');
     $page->changeTpl('xnetevents/subscribe.tpl');
     $evt = get_event($eid);
     if (is_null($evt)) {
         return PL_NOT_FOUND;
     }
     global $globals;
     if (!$evt['inscr_open']) {
         $page->kill('Les inscriptions pour cet événement sont closes');
     }
     if (!$evt['accept_nonmembre'] && !is_member() && !may_update()) {
         $url = $globals->asso('sub_url');
         if (empty($url)) {
             $url = $platal->ns . $globals->asso('diminutif') . "/" . 'subscribe';
         }
         $page->kill('Cet événement est réservé aux membres du groupe ' . $globals->asso('nom') . '. Pour devenir membre, rends-toi sur la page de <a href="' . $url . '">demande d\'inscripton</a>.');
     }
     $res = XDB::query("SELECT  stamp\n                             FROM  requests\n                            WHERE  type = 'paiements' AND data LIKE {?}", PayReq::same_event($eid, $globals->asso('id')));
     $page->assign('validation', $res->numRows());
     $page->assign('eid', $eid);
     $page->assign('event', $evt);
     $items = get_event_items($eid);
     $subs = get_event_subscription($eid, S::v('uid'));
     if (Post::has('submit')) {
         S::assert_xsrf_token();
         $moments = Post::v('moment', array());
         $pers = Post::v('personnes', array());
         $old_subs = $subs;
         $subs = array();
         foreach ($moments as $j => $v) {
             $subs[$j] = intval($v);
             // retrieve other field when more than one person
             if ($subs[$j] == 2) {
                 if (!isset($pers[$j]) || !is_numeric($pers[$j]) || $pers[$j] < 0) {
                     $page->trigError("Tu dois choisir un nombre d'invités correct&nbsp;!");
                     return;
                 }
                 $subs[$j] = $pers[$j];
             }
         }
         // count what the user must pay, and what he manually paid
         $manual_paid = 0;
         foreach ($items as $item_id => $item) {
             if (array_key_exists($item_id, $old_subs)) {
                 $manual_paid += $old_subs[$item_id]['paid'];
             }
         }
         // impossible to unsubscribe if you already paid sthing
         if (!array_sum($subs) && $manual_paid != 0) {
             $page->trigError("Impossible de te désinscrire complètement " . "parce que tu as fait un paiement par " . "chèque ou par liquide. Contacte un " . "administrateur du groupe si tu es sûr de " . "ne pas venir.");
             $updated = false;
         } else {
             // update actual inscriptions
             $updated = subscribe(S::v('uid'), $eid, $subs);
         }
         if ($updated) {
             $evt = get_event_detail($eid);
             if ($evt['topay'] > 0) {
                 $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès, tu peux payer ta participation en cliquant ci-dessous');
             } else {
                 $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès.');
             }
             if ($evt['subscription_notification'] != 'nobody') {
                 $mailer = new PlMailer('xnetevents/subscription-notif.mail.tpl');
                 if ($evt['subscription_notification'] != 'creator') {
                     $admins = $globals->asso()->iterAdmins();
                     while ($admin = $admins->next()) {
                         $mailer->addTo($admin);
                     }
                 }
                 if ($evt['subscription_notification'] != 'animator') {
                     $mailer->addTo($evt['organizer']);
                 }
                 $mailer->assign('group', $globals->asso('nom'));
                 $mailer->assign('event', $evt['intitule']);
                 $mailer->assign('subs', $subs);
                 $mailer->assign('moments', $evt['moments']);
                 $mailer->assign('name', S::user()->fullName('promo'));
                 $mailer->send();
             }
         }
     }
     $subs = get_event_subscription($eid, S::v('uid'));
     // count what the user must pay
     $topay = 0;
     $manually_paid = 0;
     foreach ($items as $item_id => $item) {
         if (array_key_exists($item_id, $subs)) {
             $topay += $item['montant'] * $subs[$item_id]['nb'];
             $manually_paid += $subs[$item_id]['paid'];
         }
     }
     $paid = $manually_paid + get_event_telepaid($eid, S::v('uid'));
     $page->assign('moments', $items);
     $page->assign('subs', $subs);
     $page->assign('topay', $topay);
     $page->assign('paid', $paid);
 }