public static function sendNotifications($memberships, $cid = null, $resend = false) { // Get custom fields $db = JFactory::getDBO(); $query = $db->getQuery(true); $date = JFactory::getDate(); $fields = RSMembership::getCustomFields(); $config = RSMembershipConfig::getInstance(); $update_ids = array(); foreach ($memberships as $membership) { $date = JFactory::getDate(); $interval = $membership->expire_notify_interval; $date->modify("+{$interval} days"); // Select all the subscriptions that match (about to expire) $query->clear(); $query->select($db->qn('u.id', 'user_id'))->select($db->qn('u.email', 'user_email'))->select($db->qn('u.name', 'user_name'))->select($db->qn('u.username', 'user_username'))->select($db->qn('mu.id', 'muid'))->select($db->qn('mu.extras'))->select($db->qn('mu.membership_end'))->select($db->qn('mu.from_transaction_id'))->from($db->qn('#__rsmembership_membership_subscribers', 'mu'))->join('left', $db->qn('#__users', 'u') . ' ON ' . $db->qn('mu.user_id') . ' = ' . $db->qn('u.id'))->where($db->qn('mu.status') . ' = ' . $db->q(MEMBERSHIP_STATUS_ACTIVE))->where($db->qn('mu.published') . ' = ' . $db->q(1)); if (!$resend) { $query->where($db->qn('mu.notified') . ' = ' . $db->q($db->getNullDate())); } $query->where($db->qn('mu.membership_end') . ' != ' . $db->q($db->getNulldate()))->where($db->qn('mu.membership_end') . ' < ' . $db->q($date->toSql()))->where($db->qn('mu.membership_id') . ' = ' . $db->q($membership->id)); if ($cid != null && is_array($cid) && count($cid) > 0) { $ids = implode($db->q(','), $cid); $query->where($db->qn('mu.id') . ' IN (' . $ids . ')'); } $db->setQuery($query, 0, $config->get('expire_emails')); $results = $db->loadObjectList(); // No results, next membership if (empty($results)) { continue; } $now = JFactory::getDate()->toUnix(); $sentToUser = false; $sentToAdmin = false; foreach ($results as $result) { $extras = ''; // Performance check if ($result->extras && (strpos($membership->user_email_expire_text . $membership->user_email_expire_subject, '{extras}') !== false || strpos($membership->admin_email_expire_text . $membership->admin_email_expire_subject, '{extras}') !== false)) { $extras = RSMembershipHelper::getExtrasNames($result->extras); } $expireDate = JFactory::getDate($result->membership_end); $expireIn = ceil(($expireDate->toUnix() - $now) / 86400); $placeholders = array('{membership}' => $membership->name, '{membership_end}' => RSMembershipHelper::showDate($result->membership_end), '{extras}' => $extras, '{email}' => $result->user_email, '{name}' => $result->user_name, '{username}' => $result->user_username, '{interval}' => $expireIn); $replace = array_keys($placeholders); $with = array_values($placeholders); $query->clear(); $query->select('*')->from($db->qn('#__rsmembership_subscribers'))->where($db->qn('user_id') . ' = ' . $db->q($result->user_id)); $db->setQuery($query); $user_data_tmp = $db->loadObject(); $user_data = array(); foreach ($fields as $field) { $field_id = 'f' . $field->id; $user_data[$field->name] = isset($user_data_tmp->{$field_id}) ? $user_data_tmp->{$field_id} : ''; } unset($user_data_tmp); foreach ($fields as $field) { $name = $field->name; $replace[] = '{' . $name . '}'; if (isset($user_data[$name])) { $with[] = is_array($user_data[$name]) ? implode("\n", $user_data[$name]) : $user_data[$name]; } else { $with[] = ''; } } $membership_fields = RSMembership::getCustomMembershipFields($membership->id); $transaction_user_data = RSMembership::getUserData($result->from_transaction_id); foreach ($membership_fields as $field) { $name = $field->name; $replace[] = '{' . $name . '}'; if (isset($transaction_user_data->membership_fields[$name])) { $with[] = is_array($transaction_user_data->membership_fields[$name]) ? implode("\n", $transaction_user_data->membership_fields[$name]) : $transaction_user_data->membership_fields[$name]; } else { $with[] = ''; } } $jconfig = JFactory::getConfig(); if ($membership->user_email_expire_subject) { $message = str_replace($replace, $with, $membership->user_email_expire_text); // from address $from = $membership->user_email_use_global ? $jconfig->get('mailfrom') : $membership->user_email_from_addr; // from name $fromName = $membership->user_email_use_global ? $jconfig->get('fromname') : $membership->user_email_from; // recipient $recipient = $result->user_email; // user email // subject $subject = str_replace($replace, $with, $membership->user_email_expire_subject); // body $body = $message; // mode $mode = $membership->user_email_mode; // cc $cc = null; // bcc $bcc = null; // attachments $query->clear(); $query->select($db->qn('path'))->from($db->qn('#__rsmembership_membership_attachments'))->where($db->qn('membership_id') . ' = ' . $db->q($membership->id))->where($db->qn('email_type') . ' = ' . $db->q('user_email_expire'))->where($db->qn('published') . '=' . $db->q('1'))->order($db->qn('ordering') . ' ASC'); $db->setQuery($query); $attachment = $db->loadColumn(); // reply to $replyto = $from; // reply to name $replytoname = $fromName; // send to user RSMembershipHelper::sendMail($from, $fromName, $recipient, $subject, $body, $mode, $cc, $bcc, $attachment, $replyto, $replytoname); $sentToUser = true; self::syslog('expiry-notification', "Membership: " . $membership->name . " (" . $membership->id . ") | Email sent to {$recipient} (Subject: {$subject})"); } // admin emails if ($membership->admin_email_expire_subject) { $message = str_replace($replace, $with, $membership->admin_email_expire_text); // from address $from = trim($membership->admin_email_from_addr) != '' ? $membership->admin_email_from_addr : $result->user_email; // from name $fromName = $result->user_name; // recipient $recipient = $membership->admin_email_to_addr; // subject $subject = str_replace($replace, $with, $membership->admin_email_expire_subject); // body $body = $message; // mode $mode = $membership->admin_email_mode; // cc $cc = null; // bcc $bcc = null; // attachments $attachment = null; // reply to $replyto = $from; // reply to name $replytoname = $fromName; // send to admin if ($subject != '') { RSMembershipHelper::sendMail($from, $fromName, $recipient, $subject, $body, $mode, $cc, $bcc, $attachment, $replyto, $replytoname); $sentToAdmin = true; self::syslog('expiry-notification', "Membership: " . $membership->name . " (" . $membership->id . ") | Admin email sent to {$recipient} (Subject: {$subject})"); } } if ($sentToUser || $sentToAdmin) { $update_ids[] = $result->muid; } } } if (!empty($update_ids)) { $query->clear(); $query->update($db->qn('#__rsmembership_membership_subscribers'))->set($db->qn('notified') . ' = ' . $db->q($date->toSql()))->where($db->qn('id') . ' IN (\'' . implode($db->q(','), $update_ids) . '\')'); $db->setQuery($query); $db->execute(); return true; } else { return false; } }
function approve($transaction_id, $force = false) { $db =& JFactory::getDBO(); $db->setQuery("SELECT * FROM #__rsmembership_transactions WHERE `id`='" . (int) $transaction_id . "'" . ($force ? "" : " AND `status`!='completed'")); $transaction = $db->loadObject(); if (empty($transaction->id)) { return false; } $params = RSMembershipHelper::parseParams($transaction->params); JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_rsmembership' . DS . 'tables'); $row =& JTable::getInstance('RSMembership_Membership_Users', 'Table'); $row->user_id = $transaction->user_id; if (!RSMembershipHelper::getConfig('create_user_instantly') || $row->user_id == 0) { $row->user_id = RSMembership::createUser($transaction->user_email, unserialize($transaction->user_data)); $db->setQuery("UPDATE #__rsmembership_transactions SET `user_id`='" . $row->user_id . "' WHERE `id`='" . $transaction->id . "'"); $db->query(); } $row->price = $transaction->price; $row->currency = $transaction->currency; $idev_enabled = RSMembershipHelper::getConfig('idev_enable'); $idev_track_renewals = RSMembershipHelper::getConfig('idev_track_renewals'); $update_gid = false; $update_user = false; $update_idev = false; $update_rsmail = false; $date = JFactory::getDate(); switch ($transaction->type) { case 'new': $row->membership_id = $params['membership_id']; $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . (int) $row->membership_id . "'"); $membership = $db->loadObject(); if (empty($membership)) { JError::raiseWarning(500, JText::_('RSM_COULD_NOT_APPROVE_TRANSACTION')); return false; } if ($membership->gid_enable) { $update_gid = true; } if ($membership->disable_expired_account) { $update_user = true; } $row->membership_start = $date->toUnix(); if ($membership->use_trial_period) { $membership->period = $membership->trial_period; $membership->period_type = $membership->trial_period_type; } if ($membership->fixed_expiry) { $row->membership_end = RSMembershipHelper::calculateFixedDate($membership); } else { if ($membership->period > 0) { switch ($membership->period_type) { case 'h': $offset = $membership->period * 3600; break; case 'd': $offset = $membership->period * 86400; break; case 'm': $offset = strtotime('+' . $membership->period . ' months', $row->membership_start) - $row->membership_start; break; case 'y': $offset = strtotime('+' . $membership->period . ' years', $row->membership_start) - $row->membership_start; break; } $row->membership_end = $date->toUnix() + $offset; } else { $row->membership_end = 0; } } if (!empty($params['extras'])) { $row->extras = implode(',', $params['extras']); } $row->status = 0; $row->from_transaction_id = $transaction->id; $row->last_transaction_id = $transaction->id; $row->store(); $return = $row->id; // iDev Integration if ($idev_enabled) { $update_idev = true; } $update_rsmail = $membership->id; break; case 'addextra': $db->setQuery("SELECT `extras` FROM #__rsmembership_membership_users WHERE `id`='" . (int) $params['id'] . "'"); $extras = $db->loadResult(); $extras = explode(',', $extras); if (empty($extras[0])) { $extras = $params['extras']; } else { $extras = array_merge($extras, $params['extras']); } $db->setQuery("UPDATE #__rsmembership_membership_users SET `extras`='" . implode(',', $extras) . "' WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); $return = $params['id']; break; case 'upgrade': // Get the upgraded membership $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . (int) $params['to_id'] . "'"); $membership = $db->loadObject(); // Get the current membership $db->setQuery("SELECT * FROM #__rsmembership_membership_users WHERE `id`='" . (int) $params['id'] . "'"); $current = $db->loadObject(); $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . $current->membership_id . "'"); $old_membership = $db->loadObject(); $db->setQuery("UPDATE #__rsmembership_membership_users SET `membership_id`='" . (int) $params['to_id'] . "' WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); $new_price = ''; $db->setQuery("SELECT price FROM #__rsmembership_membership_upgrades WHERE `membership_from_id`='" . (int) $old_membership->id . "' AND `membership_to_id`='" . (int) $membership->id . "' AND `published`='1'"); $upgrade = $db->loadResult(); if ($upgrade) { $new_price = ", `price`='" . $db->getEscaped($current->price + $upgrade) . "'"; } if ($membership->fixed_expiry) { $membership_end = RSMembershipHelper::calculateFixedDate($membership); $status = ''; if ($membership_end > $date->toUnix()) { $status = ", `status`='0', `notified`='0'"; } $db->setQuery("UPDATE #__rsmembership_membership_users SET membership_end = '" . $membership_end . "' {$status} {$new_price} WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); } else { if ($membership->period == 0) { $db->setQuery("UPDATE #__rsmembership_membership_users SET `membership_end`='0', `status`='0', `notified`='0' {$new_price} WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); } elseif ($membership->period > 0) { switch ($membership->period_type) { case 'h': $offset = $membership->period * 3600; break; case 'd': $offset = $membership->period * 86400; break; case 'm': $offset = strtotime('+' . $membership->period . ' months', $current->membership_start) - $current->membership_start; break; case 'y': $offset = strtotime('+' . $membership->period . ' years', $current->membership_start) - $current->membership_start; break; } // $membership_end = $current->membership_start + $offset; // FDS replaced the above line with the one below. This sets the membership expiration date to now + offset. $membership_end = $date->toUnix() + $offset; $status = ''; if ($membership_end > $date->toUnix()) { $status = ", `status`='0', `notified`='0'"; } $db->setQuery("UPDATE #__rsmembership_membership_users SET membership_end = '" . $membership_end . "' {$status} {$new_price} WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); } } // the last transaction $db->setQuery("UPDATE #__rsmembership_membership_users SET `last_transaction_id`='" . $transaction->id . "' WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); if ($membership->gid_enable) { $update_gid = true; } if ($membership->disable_expired_account) { $update_user = true; } $update_rsmail = $membership->id; $return = $params['id']; break; case 'renew': $row->membership_id = $params['membership_id']; $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . (int) $row->membership_id . "'"); $membership = $db->loadObject(); $membership_start = $date->toUnix(); if ($membership->fixed_expiry) { $membership_end = RSMembershipHelper::calculateFixedDate($membership); } else { // Renew when not expired ? $db->setQuery("SELECT * FROM #__rsmembership_membership_users WHERE `id`='" . (int) $params['id'] . "'"); $current = $db->loadObject(); if ($current->status == 0) { $membership_start = $current->membership_end; } if ($membership->period > 0) { switch ($membership->period_type) { case 'h': $offset = $membership->period * 3600; break; case 'd': $offset = $membership->period * 86400; break; case 'm': $offset = strtotime('+' . $membership->period . ' months', $membership_start) - $membership_start; break; case 'y': $offset = strtotime('+' . $membership->period . ' years', $membership_start) - $membership_start; break; } $membership_end = $membership_start + $offset; } else { $membership_end = 0; } if ($current->status == 0) { $membership_start = $current->membership_start; } } $db->setQuery("UPDATE #__rsmembership_membership_users SET `membership_start`='" . $membership_start . "', `membership_end`='" . $membership_end . "',`price`='" . $db->getEscaped($row->price) . "', `status`='0', `notified`='0' WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); // the last transaction $db->setQuery("UPDATE #__rsmembership_membership_users SET `last_transaction_id`='" . $transaction->id . "' WHERE `id`='" . (int) $params['id'] . "'"); $db->query(); if ($membership->gid_enable) { $update_gid = true; } if ($membership->disable_expired_account) { $update_user = true; } $return = $params['id']; // iDev Integration if ($idev_enabled && $idev_track_renewals) { $update_idev = true; } break; } if ($update_gid) { RSMembership::updateGid($row->user_id, $membership->gid_subscribe, true); } if ($update_user) { RSMembership::enableUser($row->user_id); } $db->setQuery("UPDATE #__rsmembership_transactions SET `status`='completed' WHERE `id`='" . $transaction->id . "'"); $db->query(); $user_data = unserialize($transaction->user_data); $user_email = $transaction->user_email; $replace = array('{membership}', '{email}', '{username}', '{name}'); $with = array($membership->name, $user_email, isset($user_data->username) ? $user_data->username : '', $user_data->name); $db->setQuery("SELECT * FROM #__rsmembership_fields WHERE published='1'"); $fields = $db->loadObjectList(); foreach ($fields as $field) { $name = $field->name; $replace[] = '{' . $name . '}'; if (isset($user_data->fields[$name])) { $with[] = is_array($user_data->fields[$name]) ? implode("\n", $user_data->fields[$name]) : $user_data->fields[$name]; } else { $with[] = ''; } } if ($update_rsmail) { RSMembership::addToRSMail($update_rsmail, $row->user_id, $user_email, $user_data); } $userEmail = array('from' => '', 'fromName' => '', 'recipient' => '', 'subject' => '', 'body' => '', 'mode' => '', 'cc' => '', 'bcc' => '', 'attachments' => '', 'replyto' => '', 'replytoname' => ''); $adminEmail = array('from' => '', 'fromName' => '', 'recipient' => '', 'subject' => '', 'body' => '', 'mode' => '', 'cc' => '', 'bcc' => '', 'attachments' => '', 'replyto' => '', 'replytoname' => ''); $jconfig = JFactory::getConfig(); $membership->user_email_from_addr = $membership->user_email_use_global ? $jconfig->getValue('config.mailfrom') : $membership->user_email_from_addr; $membership->user_email_from = $membership->user_email_use_global ? $jconfig->getValue('config.fromname') : $membership->user_email_from; if (!empty($membership->user_email_from_addr) && $membership->user_email_approved_subject != '') { // start sending emails // from address $userEmail['from'] = $membership->user_email_from_addr; // from name $userEmail['fromName'] = $membership->user_email_from; // recipient $userEmail['recipient'] = $user_email; // user email // subject $userEmail['subject'] = str_replace($replace, $with, $membership->user_email_approved_subject); // body $userEmail['body'] = str_replace($replace, $with, $membership->user_email_approved_text); // mode $userEmail['mode'] = $membership->user_email_mode; // cc $userEmail['cc'] = null; // bcc $userEmail['bcc'] = null; // attachments $db->setQuery("SELECT `path` FROM #__rsmembership_membership_attachments WHERE `membership_id`='" . $membership->id . "' AND `email_type`='user_email_approved' AND `published`='1' ORDER BY `ordering`"); $userEmail['attachments'] = $db->loadResultArray(); // reply to $userEmail['replyto'] = $userEmail['from']; // reply to name $userEmail['replytoname'] = $userEmail['fromName']; } // admin emails if (!empty($membership->admin_email_to_addr) && $membership->admin_email_approved_subject != '') { // from address $adminEmail['from'] = $user_email; // from name $adminEmail['fromName'] = $user_data->name; // recipient $adminEmail['recipient'] = $membership->admin_email_to_addr; // subject $adminEmail['subject'] = str_replace($replace, $with, $membership->admin_email_approved_subject); // body $adminEmail['body'] = str_replace($replace, $with, $membership->admin_email_approved_text); // mode $adminEmail['mode'] = $membership->admin_email_mode; // cc $adminEmail['cc'] = null; // bcc $adminEmail['bcc'] = null; // attachments $adminEmail['attachments'] = null; // reply to $adminEmail['replyto'] = $adminEmail['from']; // reply to name $adminEmail['replytoname'] = $adminEmail['fromName']; } // run php code eval($membership->custom_code_transaction); // send to user if (!empty($membership->user_email_from_addr) && $membership->user_email_approved_subject != '') { RSMembershipHelper::sendMail($userEmail['from'], $userEmail['fromName'], $userEmail['recipient'], $userEmail['subject'], $userEmail['body'], $userEmail['mode'], $userEmail['cc'], $userEmail['bcc'], $userEmail['attachments'], $userEmail['replyto'], $userEmail['replytoname']); } // send to admin if (!empty($membership->admin_email_to_addr) && !empty($membership->admin_email_approved_subject)) { RSMembershipHelper::sendMail($adminEmail['from'], $adminEmail['fromName'], $adminEmail['recipient'], $adminEmail['subject'], $adminEmail['body'], $adminEmail['mode'], $adminEmail['cc'], $adminEmail['bcc'], $adminEmail['attachments'], $adminEmail['replyto'], $adminEmail['replytoname']); } // process stock if ($membership->stock > 0) { // decrease stock if ($membership->stock > 1) { $db->setQuery("UPDATE #__rsmembership_memberships SET `stock`=`stock`-1 WHERE `id`='" . $membership->id . "'"); } else { $db->setQuery("UPDATE #__rsmembership_memberships SET `stock`='-1' WHERE `id`='" . $membership->id . "'"); } $db->query(); } if ($update_idev) { RSMembership::updateIdev(array('idev_saleamt' => $transaction->price, 'idev_ordernum' => $transaction->id, 'ip_address' => $transaction->ip)); } // should return the newly created/updated membership id return $return; }