/** * It handles the case in which a user that already used a free trial, * try to get another free trial (that is not allowed) * * It also set the pc_user.has_requested_free_trial field * * @param PcUser $user * @param PcSubscriptionType $subscriptionType * @param bool $isGift (=false) * @param bool $isAutomatic (=false) * @param string $paypalTransactionId (='') * @return bool - false if a user who requested a free trial tries to get it again, true otherwise */ public static function createOrExtendSupporterAccount(PcUser $user, PcSubscriptionType $subscriptionType, $isGift = false, $isAutomatic = false, $paypalTransactionId = '') { if ($subscriptionType->getId() == PcSubscriptionTypePeer::FREE_TRIAL) { if ($user->getHasRequestedFreeTrial()) { return false; } else { $user->setHasRequestedFreeTrial(1)->save(); } } // 3 situations can happen: // 1) the user is not an supporter -> we add the record // 2) the user is still a supporter -> we extend the subscription from the last day of the current subscription // 3) the user used to be a supporter (the record is still in the table) but the subscription has expired -> we // start a new subscription from today $startDate = null; $today = date("Y-m-d"); $c = new Criteria(); $c->add(PcSupporterPeer::USER_ID, $user->getId()); $supporterAccount = PcSupporterPeer::doSelectOne($c); if (!$supporterAccount) { $supporterAccount = new PcSupporter(); $supporterAccount->setUserId($user->getId()); $startDate = $today; } else { $supporterAccountExpiryDate = $supporterAccount->getExpiryDate(); if ($today > $supporterAccountExpiryDate) { $startDate = $today; } else { $startDate = $supporterAccountExpiryDate; } } $newExpiryDateTimestamp = $supporterAccount->getNewExpiryDateAfterSubscription($subscriptionType, $startDate); $supporterAccount->setExpiryDate(date("Y-m-d", $newExpiryDateTimestamp))->save(); // recording the subscription $subscription = new PcSubscription(); $subscription->setUserId($user->getId())->setSubscriptionTypeId($subscriptionType->getId())->setWasGift($isGift)->setWasAutomatic($isAutomatic)->setPaypalTransactionId($paypalTransactionId)->save(); // sending email $email = $user->getEmail(); $from = sfConfig::get('app_emailAddress_contact'); $subject = sfConfig::get('app_subscriptionSuccess_emailSubject'); $body = sfConfig::get('app_subscriptionSuccess_emailBody'); $replyTo = sfConfig::get('app_emailAddress_director'); PcUtils::sendEmail($email, $subject, $body, $from, $replyTo); // creating task in the Inbox $user->addToInbox(__('ACCOUNT_SUBSCRIPTION_INBOX_MESSAGE') . ' ' . $supporterAccount->getExpiryDate('j F Y') . '.'); return true; }
/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases -- especially when you override doSelect*() * methods in your stub classes -- you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by doSelect*() * and retrieveByPK*() calls. * * @param PcSubscriptionType $value A PcSubscriptionType object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(PcSubscriptionType $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getId(); } // if key === null self::$instances[$key] = $obj; } }
/** * Declares an association between this object and a PcSubscriptionType object. * * @param PcSubscriptionType $v * @return PcSubscription The current object (for fluent API support) * @throws PropelException */ public function setPcSubscriptionType(PcSubscriptionType $v = null) { if ($v === null) { $this->setSubscriptionTypeId(NULL); } else { $this->setSubscriptionTypeId($v->getId()); } $this->aPcSubscriptionType = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the PcSubscriptionType object, it will not be re-added. if ($v !== null) { $v->addPcSubscription($this); } return $this; }