/** * Pre-processes the message text in $text, replacing merge tags with those * fetched based on subscription $sub * * @param string $text The message to process * @param AkeebasubsTableSubscription $sub A subscription object * * @return string The processed string */ public static function processSubscriptionTags($text, $sub, $extras = array()) { // Get the user object for this subscription $user = JFactory::getUser($sub->user_id); // Get the extra user parameters object for the subscription $kuser = F0FModel::getTmpInstance('Users', 'AkeebasubsModel')->user_id($sub->user_id)->getFirstItem(); // Get the subscription level $level = F0FModel::getTmpInstance('Levels', 'AkeebasubsModel')->getItem($sub->akeebasubs_level_id); // Merge the user objects $userdata = array_merge((array) $user, (array) $kuser->getData()); // Create and replace merge tags for subscriptions. Format [SUB:KEYNAME] if ($sub instanceof AkeebasubsTableSubscription) { $subData = (array) $sub->getData(); } else { $subData = (array) $sub; } foreach ($subData as $k => $v) { if (is_array($v) || is_object($v)) { continue; } if (substr($k, 0, 1) == '_') { continue; } if ($k == 'akeebasubs_subscription_id') { $k = 'id'; } $tag = '[SUB:' . strtoupper($k) . ']'; if (in_array($k, array('net_amount', 'gross_amount', 'tax_amount', 'prediscount_amount', 'discount_amount', 'affiliate_comission'))) { $v = sprintf('%.2f', $v); } $text = str_replace($tag, $v, $text); } // Create and replace merge tags for the subscription level. Format [LEVEL:KEYNAME] $levelData = (array) $level->getData(); foreach ($levelData as $k => $v) { if (is_array($v) || is_object($v)) { continue; } if (substr($k, 0, 1) == '_') { continue; } if ($k == 'akeebasubs_level_id') { $k = 'id'; } $tag = '[LEVEL:' . strtoupper($k) . ']'; $text = str_replace($tag, $v, $text); } // Create and replace merge tags for custom per-subscription data. Format [SUBCUSTOM:KEYNAME] if (array_key_exists('params', $subData)) { if (is_string($subData['params'])) { $custom = json_decode($subData['params'], true); } elseif (is_array($subData['params'])) { $custom = $subData['params']; } elseif (is_object($subData['params'])) { $custom = (array) $subData['params']; } else { $custom = array(); } // Extra check for subcustom params: if you save a subscription form the backend, // custom fields are inside an array named subcustom if (is_array($custom) && isset($custom['subcustom'])) { $custom = $custom['subcustom']; } if (!empty($custom)) { foreach ($custom as $k => $v) { if (is_object($v)) { continue; } if (substr($k, 0, 1) == '_') { continue; } $tag = '[SUBCUSTOM:' . strtoupper($k) . ']'; if (is_array($v)) { continue; } $text = str_replace($tag, $v, $text); } } } // Create and replace merge tags for user data. Format [USER:KEYNAME] foreach ($userdata as $k => $v) { if (is_object($v) || is_array($v)) { continue; } if (substr($k, 0, 1) == '_') { continue; } if ($k == 'akeebasubs_subscription_id') { $k = 'id'; } $tag = '[USER:'******']'; $text = str_replace($tag, $v, $text); } // Create and replace merge tags for custom fields data. Format [CUSTOM:KEYNAME] if (array_key_exists('params', $userdata)) { if (is_string($userdata['params'])) { $custom = json_decode($userdata['params']); } elseif (is_array($userdata['params'])) { $custom = $userdata['params']; } elseif (is_object($userdata['params'])) { $custom = (array) $userdata['params']; } else { $custom = array(); } if (!empty($custom)) { foreach ($custom as $k => $v) { if (substr($k, 0, 1) == '_') { continue; } $tag = '[CUSTOM:' . strtoupper($k) . ']'; if (is_array($v)) { $v = implode(', ', $v); } $text = str_replace($tag, $v, $text); } } } // Extra variables replacement // -- Coupon code $couponcode = ''; if ($sub->akeebasubs_coupon_id) { $couponData = F0FModel::getTmpInstance('Coupons', 'AkeebasubsModel')->savestate(0)->getItem($sub->akeebasubs_coupon_id); $couponcode = $couponData->coupon; } // -- Get the site name $config = JFactory::getConfig(); if (version_compare(JVERSION, '3.0', 'ge')) { $sitename = $config->get('sitename'); } else { $sitename = $config->getValue('config.sitename'); } // -- First/last name $fullname = $user->name; $nameParts = explode(' ', $fullname, 2); $firstname = array_shift($nameParts); $lastname = !empty($nameParts) ? array_shift($nameParts) : ''; // -- Get the subscription level $level = F0FModel::getTmpInstance('Levels', 'AkeebasubsModel')->setId($sub->akeebasubs_level_id)->getItem(); // -- Site URL list($isCli, $isAdmin) = F0FDispatcher::isCliAdmin(); if ($isCli) { JLoader::import('joomla.application.component.helper'); $baseURL = JComponentHelper::getParams('com_akeebasubs')->get('siteurl', 'http://www.example.com'); $temp = str_replace('http://', '', $baseURL); $temp = str_replace('https://', '', $temp); $parts = explode($temp, '/', 2); $subpathURL = count($parts) > 1 ? $parts[1] : ''; } else { $baseURL = JURI::base(); $subpathURL = JURI::base(true); } $baseURL = str_replace('/administrator', '', $baseURL); $subpathURL = str_replace('/administrator', '', $subpathURL); // -- My Subscriptions URL if ($isAdmin || $isCli) { $url = 'index.php?option=com_akeebasubs&view=subscriptions&layout=default'; } else { $url = str_replace('&', '&', JRoute::_('index.php?option=com_akeebasubs&view=subscriptions&layout=default')); } $url = ltrim($url, '/'); $subpathURL = ltrim($subpathURL, '/'); if (substr($url, 0, strlen($subpathURL) + 1) == "{$subpathURL}/") { $url = substr($url, strlen($subpathURL) + 1); } $mysubsurl = rtrim($baseURL, '/') . '/' . ltrim($url, '/'); $currency = ''; if (!class_exists('AkeebasubsHelperCparams')) { @(include_once JPATH_ADMINISTRATOR . '/components/com_akeebasubs/helpers/cparams.php'); } if (class_exists('AkeebasubsHelperCparams')) { $currency = AkeebasubsHelperCparams::getParam('currencysymbol', '€'); } // Dates JLoader::import('joomla.utilities.date'); $jFrom = new JDate($sub->publish_up); $jTo = new JDate($sub->publish_down); // Download ID $dlid = md5($user->id . $user->username . $user->password); // User's state, human readable $formatted_state = ''; $state = $kuser->state; if (!empty($state)) { if (!class_exists('AkeebasubsHelperSelect')) { require_once JPATH_ADMINISTRATOR . '/components/com_akeebasubs/helpers/select.php'; } $formatted_state = AkeebasubsHelperSelect::formatState($state); } // User's country, human readable $formatted_country = ''; $country = $kuser->country; if (!empty($country)) { if (!class_exists('AkeebasubsHelperSelect')) { require_once JPATH_ADMINISTRATOR . '/components/com_akeebasubs/helpers/select.php'; } $formatted_country = AkeebasubsHelperSelect::formatCountry($country); } // -- The actual replacement $extras = array_merge(array("\\n" => "\n", '[SITENAME]' => $sitename, '[SITEURL]' => $baseURL, '[FULLNAME]' => $fullname, '[FIRSTNAME]' => $firstname, '[LASTNAME]' => $lastname, '[USERNAME]' => $user->username, '[USEREMAIL]' => $user->email, '[LEVEL]' => $level->title, '[ENABLED]' => JText::_('COM_AKEEBASUBS_SUBSCRIPTION_COMMON_' . ($sub->enabled ? 'ENABLED' : 'DISABLED')), '[PAYSTATE]' => JText::_('COM_AKEEBASUBS_SUBSCRIPTION_STATE_' . $sub->state), '[PUBLISH_UP]' => $jFrom->format(JText::_('DATE_FORMAT_LC2'), true), '[PUBLISH_UP_EU]' => $jFrom->format('d/m/Y H:i:s', true), '[PUBLISH_UP_USA]' => $jFrom->format('m/d/Y h:i:s a', true), '[PUBLISH_UP_JAPAN]' => $jFrom->format('Y/m/d H:i:s', true), '[PUBLISH_DOWN]' => $jTo->format(JText::_('DATE_FORMAT_LC2'), true), '[PUBLISH_DOWN_EU]' => $jTo->format('d/m/Y H:i:s', true), '[PUBLISH_DOWN_USA]' => $jTo->format('m/d/Y h:i:s a', true), '[PUBLISH_DOWN_JAPAN]' => $jTo->format('Y/m/d H:i:s', true), '[MYSUBSURL]' => $mysubsurl, '[URL]' => $mysubsurl, '[CURRENCY]' => $currency, '[$]' => $currency, '[DLID]' => $dlid, '[COUPONCODE]' => $couponcode, '[USER:STATE_FORMATTED]' => $formatted_state, '[USER:COUNTRY_FORMATTED]' => $formatted_country, '[NAME]' => $firstname, '[STATE]' => JText::_('COM_AKEEBASUBS_SUBSCRIPTION_STATE_' . $sub->state), '[FROM]' => $jFrom->format(JText::_('DATE_FORMAT_LC2'), true), '[TO]' => $jTo->format(JText::_('DATE_FORMAT_LC2'), true)), $extras); foreach ($extras as $key => $value) { $text = str_replace($key, $value, $text); } return $text; }
/** * This is called whenever a new subscription is created or an existing * subscription is modified. We are using it to create slave subscriptions * where necessary and "mirror" the parameters of the master subscription * to the slave subscriptions when slave subscriptions already exist. * * @param AkeebasubsTableSubscription $row The subscription which we act upon * @param array $info Update information */ public function onAKSubscriptionChange($row, $info) { if (self::$dontFire) { return; } // Get the parameters of the row $params = $row->params; // No params? No need to check anything else! if (empty($params)) { return; } if (!is_object($params) && !is_array($params)) { $params = json_decode($params, true); } else { $params = (array) $params; } // Nothing in the params array? No need to check anything else! if (empty($params)) { return; } // Create new slave subscriptions if the subscription level allows us to if (!isset($this->maxSlaves[$row->akeebasubs_level_id])) { $this->maxSlaves[$row->akeebasubs_level_id] = 0; } // Do we have slave users at all? if (!array_key_exists('slaveusers', $params)) { return; } JLoader::import('joomla.user.helper'); $slavesubs_ids = array(); $data = $row instanceof F0FTable ? $row->getData() : (array) $row; // Let's look inside modified fields, is this a new slave, a removed one or I'm just renewing his subscription? // Simply create new subscription, the user specified slaves while creating his subscription if ($info['status'] == 'new') { $slaveusers = $params['slaveusers']; // Do we have at least one slave user? if (empty($slaveusers)) { return; } $newParams = $params; if (isset($newParams['slavesubs_ids'])) { unset($newParams['slavesubs_ids']); } unset($newParams['slaveusers']); // Create new slave subscriptions JLoader::import('joomla.user.helper'); $slavesubs_ids = array(); if ($row instanceof F0FTable) { $data = $row->getData(); } else { $data = (array) $row; } foreach ($slaveusers as $slaveUsername) { if (empty($slaveUsername)) { continue; } $user_id = JUserHelper::getUserId($slaveUsername); if ($user_id <= 0) { continue; } $newdata = array_merge($data, array('akeebasubs_subscription_id' => 0, 'user_id' => $user_id, 'net_amount' => 0, 'tax_amount' => 0, 'gross_amount' => 0, 'tax_percent' => 0, 'params' => $newParams, 'akeebasubs_coupon_id' => 0, 'akeebasubs_upgrade_id' => 0, 'akeebasubs_affiliate_id' => 0, 'affiliate_comission' => 0, 'prediscount_amount' => 0, 'discount_amount' => 0, 'contact_flag' => 0)); // Save the new subscription record $db = JFactory::getDbo(); $tableName = '#__akeebasubs_subscriptions'; $tableKey = 'akeebasubs_subscription_id'; $table = new AkeebasubsTableSubscription($tableName, $tableKey, $db); $table->reset(); self::$dontFire = true; $table->save($newdata); self::$dontFire = false; $slavesubs_ids[] = $table->akeebasubs_subscription_id; } $params['slavesubs_ids'] = $slavesubs_ids; $newdata = array_merge($data, array('params' => json_encode($params), '_dontNotify' => true)); $db = JFactory::getDbo(); $tableName = '#__akeebasubs_subscriptions'; $tableKey = 'akeebasubs_subscription_id'; $table = new AkeebasubsTableSubscription($tableName, $tableKey, $db); $table->reset(); self::$dontFire = true; $table->save($newdata); self::$dontFire = false; } else { $current = $params; $previous = json_decode($info['previous']->params, true); if (!isset($previous['slaveusers']) || empty($previous['slaveusers'])) { $previous['slaveusers'] = array(); } // Let's get the full list of involved people $list = array_merge($current['slaveusers'], $previous['slaveusers']); $i = 0; $dirty = false; foreach ($list as $slave) { if (empty($slave)) { continue; } $result = false; if (in_array($slave, $current['slaveusers']) && in_array($slave, $previous['slaveusers'])) { // Slave is still here, just check if his subscription is expired, if so extend it $index = array_search($slave, $current['slaveusers']); if (isset($current['slavesubs_ids'][$index])) { $table = F0FModel::getTmpInstance('Subscriptions', 'AkeebasubsModel')->getItem($current['slavesubs_ids'][$index]); if (!$table->enabled) { $table->save(array('publish_down' => $row->publish_down)); $dirty = true; } $result = $table->akeebasubs_subscription_id; } } elseif (in_array($slave, $current['slaveusers']) && !in_array($slave, $previous['slaveusers'])) { // Added user, create a new subscription for him $result = $this->createSlaveSub($slave, $data, $params); $dirty = true; } elseif (!in_array($slave, $current['slaveusers']) && in_array($slave, $previous['slaveusers'])) { // Before he was active, now it's no more; let's fire this slave (aka expire his subscription) $index = array_search($slave, $previous['slaveusers']); if (isset($previous['slavesubs_ids'][$index])) { $this->expireSlaveSub($previous['slavesubs_ids'][$index]); $dirty = true; } } if ($result) { $slavesubs_ids[] = $result; } } // Do not try to save the subscription unless we made a change in slave subscribers if (!$dirty) { //return; } $params['slavesubs_ids'] = $slavesubs_ids; $newdata = array_merge($data, array('params' => json_encode($params), '_dontNotify' => true)); $table = F0FModel::getTmpInstance('Subscriptions', 'AkeebasubsModel')->getTable(); $table->reset(); self::$dontFire = true; $table->save($newdata); self::$dontFire = false; } }