/**
  * 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      PcSubscription $value A PcSubscription object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(PcSubscription $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = (string) $obj->getId();
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }