PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/code/base/SilvercartNewsletter.php

https://bitbucket.org/silvercart/silvercart/
PHP | 244 lines | 95 code | 23 blank | 126 comment | 9 complexity | 497555c1d427a5622ce687e444ccdb74 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Base
  22. */
  23. /**
  24. * Bundles newsletter related functionality.
  25. *
  26. * @package Silvercart
  27. * @subpackage Base
  28. * @author Sascha Koehler <skoehler@pixeltricks.de>
  29. * @since 25.08.2011
  30. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  31. * @copyright 2011 pixeltricks GmbH
  32. */
  33. class SilvercartNewsletter extends DataObject {
  34. /**
  35. * Adds a member to the newsletter subscribers list.
  36. *
  37. * @param Member $member The member to subscribe
  38. *
  39. * @return boolean
  40. *
  41. * @author Sascha Koehler <skoehler@pixeltricks.de>
  42. * @since 25.08.2011
  43. */
  44. public static function subscribeRegisteredCustomer($member) {
  45. $subscribed = false;
  46. if ($member instanceof Member) {
  47. if ($member->NewsletterOptInStatus) {
  48. // Opt-in is done, so subscribe to newsletter
  49. $member->SubscribedToNewsletter = true;
  50. } else {
  51. // Opt-in has to be done first
  52. $confirmationHash = self::createConfirmationHash($member->Salutation, $member->FirstName, $member->Surname, $member->Email);
  53. $member->setField('NewsletterConfirmationHash', Convert::raw2sql($confirmationHash));
  54. self::sendOptInEmailTo($member->Salutation, $member->FirstName, $member->Surname, $member->Email, $confirmationHash);
  55. }
  56. $member->write();
  57. $subscribed = true;
  58. }
  59. return $subscribed;
  60. }
  61. /**
  62. * Removes a member from the newsletter subscribers list.
  63. *
  64. * @param Member $member The member to unsubscribe
  65. *
  66. * @return boolean
  67. *
  68. * @author Sascha Koehler <skoehler@pixeltricks.de>
  69. * @since 25.08.2011
  70. */
  71. public static function unSubscribeRegisteredCustomer($member) {
  72. $unSubscribed = false;
  73. if ($member instanceof Member) {
  74. $member->SubscribedToNewsletter = false;
  75. $member->NewsletterConfirmationHash = '';
  76. $member->NewsletterOptInStatus = false;
  77. $member->write();
  78. $unSubscribed = true;
  79. }
  80. return $unSubscribed;
  81. }
  82. /**
  83. * Adds an anonymous customer to the newsletter subscribers list.
  84. *
  85. * @param string $salutation The salutation to use
  86. * @param string $firstName The first name to use
  87. * @param string $surName The last name to use
  88. * @param string $email The email address to use
  89. *
  90. * @return void
  91. *
  92. * @author Sascha Koehler <skoehler@pixeltricks.de>
  93. * @since 25.08.2011
  94. */
  95. public static function subscribeAnonymousCustomer($salutation, $firstName, $surName, $email) {
  96. $doOptIn = true;
  97. if (SilvercartAnonymousNewsletterRecipient::doesExist($email) &&
  98. SilvercartAnonymousNewsletterRecipient::isOptInDoneFor($email)) {
  99. $doOptIn = false;
  100. }
  101. if ($doOptIn) {
  102. $confirmationHash = self::createConfirmationHash($salutation, $firstName, $surName, $email);
  103. SilvercartAnonymousNewsletterRecipient::add($salutation, $firstName, $surName, $email, false, Convert::raw2sql($confirmationHash));
  104. self::sendOptInEmailTo($salutation, $firstName, $surName, $email, $confirmationHash);
  105. } else {
  106. SilvercartAnonymousNewsletterRecipient::add($salutation, $firstName, $surName, $email, true);
  107. }
  108. }
  109. /**
  110. * Removes an anonymous customer from the newsletter subscribers list.
  111. *
  112. * @param string $email The email address whose entry should be removed from
  113. * the recipient list.
  114. *
  115. * @return void
  116. *
  117. * @author Sascha Koehler <skoehler@pixeltricks.de>
  118. * @since 25.08.2011
  119. */
  120. public static function unSubscribeAnonymousCustomer($email) {
  121. SilvercartAnonymousNewsletterRecipient::removeByEmailAddress($email);
  122. }
  123. /**
  124. * Checks if the given email address is allocated by a registered
  125. * regular customer.
  126. *
  127. * @param string $email The email address to check
  128. *
  129. * @return boolean
  130. *
  131. * @author Sascha Koehler <skoehler@pixeltricks.de>
  132. * @since 25.08.2011
  133. */
  134. public static function isEmailAllocatedByRegularCustomer($email) {
  135. $emailIsAllocated = false;
  136. $regularCustomer = DataObject::get_one(
  137. 'Member',
  138. sprintf(
  139. "Email = '%s'",
  140. $email
  141. )
  142. );
  143. if ( $regularCustomer &&
  144. ($regularCustomer->Groups()->find('Code', 'b2b') ||
  145. $regularCustomer->Groups()->find('Code', 'b2c'))
  146. ) {
  147. $emailIsAllocated = true;
  148. }
  149. return $emailIsAllocated;
  150. }
  151. /**
  152. * Checks if the given email address is allocated by an anonymous
  153. * newsletter subscriber.
  154. *
  155. * @param string $email The email address to check
  156. *
  157. * @return boolean
  158. *
  159. * @author Sascha Koehler <skoehler@pixeltricks.de>
  160. * @since 25.08.2011
  161. */
  162. public static function isEmailAllocatedByAnonymousRecipient($email) {
  163. $emailIsAllocated = false;
  164. $anonymousRecipient = SilvercartAnonymousNewsletterRecipient::getByEmailAddress($email);
  165. if ($anonymousRecipient) {
  166. $emailIsAllocated = true;
  167. }
  168. return $emailIsAllocated;
  169. }
  170. /**
  171. * Sends an email with opt-in link to the given address.
  172. *
  173. * @param string $salutation The salutation to use
  174. * @param string $firstName The first name to use
  175. * @param string $surName The last name to use
  176. * @param string $email The email address to use
  177. * @param string $confirmationHash The hash value to use for identification
  178. *
  179. * @return void
  180. *
  181. * @author Sascha Koehler <skoehler@pixeltricks.de>
  182. * @since 25.08.2011
  183. */
  184. public static function sendOptInEmailTo($salutation, $firstName, $surName, $email, $confirmationHash) {
  185. SilvercartShopEmail::send(
  186. 'NewsletterOptIn',
  187. $email,
  188. array(
  189. 'Salutation' => $salutation,
  190. 'FirstName' => $firstName,
  191. 'Surname' => $surName,
  192. 'Email' => $email,
  193. 'ConfirmationLink' => Director::absoluteURL(SilvercartPage_Controller::PageByIdentifierCode("SilvercartNewsletterOptInConfirmationPage")->Link()).'?h='.urlencode($confirmationHash)
  194. )
  195. );
  196. }
  197. /**
  198. * Creates a hash from the given parameters and returns it.
  199. *
  200. * @param string $salutation The salutation to use
  201. * @param string $firstName The first name to use
  202. * @param string $surName The last name to use
  203. * @param string $email The email address to use
  204. *
  205. * @return string The created hash
  206. *
  207. * @author Sascha Koehler <skoehler@pixeltricks.de>
  208. * @since 25.08.2011
  209. */
  210. public static function createConfirmationHash($salutation, $firstName, $surName, $email) {
  211. $confirmationHash = md5(
  212. mktime() .
  213. rand() .
  214. $salutation .
  215. $email .
  216. $firstName .
  217. $surName
  218. );
  219. return $confirmationHash;
  220. }
  221. }