/** * 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; } }