/www/shop/engine/Shopware/Plugins/Default/Frontend/Notification/Bootstrap.php

https://bitbucket.org/weberlars/sot-shopware · PHP · 307 lines · 191 code · 42 blank · 74 comment · 28 complexity · 1869d7e7fe94b7f18f2f9a49dde3e1fc MD5 · raw file

  1. <?php
  2. /**
  3. * Shopware 4.0
  4. * Copyright Š 2012 shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. *
  24. * @category Shopware
  25. * @package Shopware_Plugins
  26. * @subpackage Notification
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author Heiner Lohaus
  30. * @author $Author$
  31. */
  32. /**
  33. * Shopware Notification Plugin
  34. *
  35. * Allows customers to register to any product in shop that is not on stock.
  36. * Informing this customers automatically via email as soon as the product becomes available
  37. * Includes a backend module that display statistics about the usage of this feature
  38. */
  39. class Shopware_Plugins_Frontend_Notification_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  40. {
  41. /**
  42. * Installation of plugin
  43. * Create-Events to include custom code on product detail page
  44. * Creates new cronjob "notification"
  45. * @return bool
  46. */
  47. public function install()
  48. {
  49. $this->subscribeEvent(
  50. 'Enlight_Controller_Action_PostDispatch_Frontend_Detail',
  51. 'onPostDispatch'
  52. );
  53. $this->subscribeEvent(
  54. 'Enlight_Controller_Action_Frontend_Detail_Notify',
  55. 'onNotifyAction'
  56. );
  57. $this->subscribeEvent(
  58. 'Enlight_Controller_Action_Frontend_Detail_NotifyConfirm',
  59. 'onNotifyConfirmAction'
  60. );
  61. $this->subscribeEvent(
  62. 'Shopware_CronJob_Notification',
  63. 'onRunCronJob'
  64. );
  65. return true;
  66. }
  67. /**
  68. * Check if product is available (instock is greater then zero)
  69. * If not available display possibility to register for status updates
  70. * @static
  71. * @param Enlight_Event_EventArgs $args
  72. * @return
  73. */
  74. public static function onPostDispatch(Enlight_Event_EventArgs $args)
  75. {
  76. $request = $args->getSubject()->Request();
  77. $response = $args->getSubject()->Response();
  78. if (!$request->isDispatched() || $response->isException() || $request->getModuleName() != 'frontend') {
  79. return;
  80. }
  81. $id = (int)$args->getSubject()->Request()->sArticle;
  82. $view = $args->getSubject()->View();
  83. $notificationVariants = array();
  84. if (!empty(Shopware()->Session()->sNotificatedArticles)) {
  85. $sql = 'SELECT `ordernumber` FROM `s_articles_details` WHERE `articleID`=?';
  86. $ordernumbers = Shopware()->Db()->fetchCol($sql, $id);
  87. if (!empty($ordernumbers)) {
  88. foreach ($ordernumbers as $ordernumber) {
  89. if (in_array($ordernumber, Shopware()->Session()->sNotificatedArticles)) {
  90. $notificationVariants[] = $ordernumber;
  91. }
  92. }
  93. }
  94. }
  95. $view->NotifyHideBasket = Shopware()->Config()->sDEACTIVATEBASKETONNOTIFICATION;
  96. $view->setNoCache(true);
  97. $view->NotificationVariants = $notificationVariants;
  98. $view->NotifyEmailError = $view->NotifyEmailError;
  99. $view->NotifyValid = $view->NotifyValid;
  100. $view->NotifyInvalid = $view->NotifyInvalid;
  101. $view->ShowNotification = true;
  102. $view->NotifyAlreadyRegistered = $view->NotifyAlreadyRegistered;
  103. $view->setNoCache(false);
  104. }
  105. /**
  106. * Called on register for status updates
  107. * Check user email address and send double optin to confirm the email
  108. * @static
  109. * @param Enlight_Event_EventArgs $args
  110. * @return
  111. */
  112. public static function onNotifyAction(Enlight_Event_EventArgs $args)
  113. {
  114. $args->setProcessed(true);
  115. $action = $args->getSubject();
  116. $id = (int)$action->Request()->sArticle;
  117. $email = $action->Request()->sNotificationEmail;
  118. $sError = false;
  119. $action->View()->NotifyEmailError = false;
  120. if (!empty($action->Request()->notifyOrdernumber)) {
  121. $validator = new Zend_Validate_EmailAddress();
  122. if (empty($email) || !$validator->isValid($email)) {
  123. $sError = true;
  124. $action->View()->NotifyEmailError = true;
  125. } elseif (!empty($action->Request()->notifyOrdernumber)) {
  126. if (!empty(Shopware()->Session()->sNotificatedArticles)) {
  127. if (in_array($action->Request()->notifyOrdernumber, Shopware()->Session()->sNotificatedArticles)) {
  128. $sError = true;
  129. $action->View()->ShowNotification = false;
  130. $action->View()->NotifyAlreadyRegistered = true;
  131. } else {
  132. Shopware()->Session()->sNotificatedArticles[] = $action->Request()->notifyOrdernumber;
  133. }
  134. } else {
  135. Shopware()->Session()->sNotificatedArticles = array($action->Request()->notifyOrdernumber);
  136. }
  137. } else {
  138. $sError = true;
  139. }
  140. if (!$sError) {
  141. $AlreadyNotified = Shopware()->Db()->fetchRow('
  142. SELECT * FROM `s_articles_notification`
  143. WHERE `ordernumber`=?
  144. AND `mail` = ?
  145. AND send = 0
  146. ', array($action->Request()->notifyOrdernumber, $email));
  147. if (empty($AlreadyNotified)) {
  148. $action->View()->NotifyAlreadyRegistered = false;
  149. $hash = md5(uniqid(rand()));
  150. $link = $action->Front()->Router()->assemble(array(
  151. 'sViewport' => 'detail',
  152. 'sArticle' => $id,
  153. 'sNotificationConfirmation' => $hash,
  154. 'sNotify' => '1',
  155. 'action' => 'notifyConfirm'
  156. ));
  157. $name = Shopware()->Modules()->Articles()->sGetArticleNameByOrderNumber($action->Request()->notifyOrdernumber);
  158. Shopware()->System()->_POST['sLanguage'] = Shopware()->System()->sLanguageData[Shopware()->System()->sLanguage]['isocode'];
  159. Shopware()->System()->_POST['sShopPath'] = 'http://' . Shopware()->Config()->sBASEPATH . '/' . Shopware()->Config()->sBASEFILE;
  160. $sql = '
  161. INSERT INTO s_core_optin (datum, hash, data)
  162. VALUES (NOW(), ?, ?)
  163. ';
  164. Shopware()->Db()->query($sql, array($hash, serialize(Shopware()->System()->_POST)));
  165. $context = array(
  166. 'sConfirmLink' => $link,
  167. 'sArticleName' => $name,
  168. );
  169. $mail = Shopware()->TemplateMail()->createMail('sACCEPTNOTIFICATION', $context);
  170. $mail->addTo($email);
  171. $mail->send();
  172. } else {
  173. $action->View()->NotifyAlreadyRegistered = true;
  174. }
  175. }
  176. }
  177. return $action->forward('index');
  178. }
  179. /**
  180. * If confirmation link in email was clicked
  181. * Make entry in s_articles_notification table
  182. * @static
  183. * @param Enlight_Event_EventArgs $args
  184. * @return
  185. */
  186. public static function onNotifyConfirmAction(Enlight_Event_EventArgs $args)
  187. {
  188. $args->setProcessed(true);
  189. $action = $args->getSubject();
  190. $action->View()->NotifyValid = false;
  191. $action->View()->NotifyInvalid = false;
  192. if (!empty($action->Request()->sNotificationConfirmation) && !empty($action->Request()->sNotify)) {
  193. $getConfirmation = Shopware()->Db()->fetchRow('
  194. SELECT * FROM s_core_optin WHERE hash = ?
  195. ', array($action->Request()->sNotificationConfirmation));
  196. $notificationConfirmed = false;
  197. if (!empty($getConfirmation['hash'])) {
  198. $notificationConfirmed = true;
  199. $json_data = unserialize($getConfirmation['data']);
  200. Shopware()->Db()->query('DELETE FROM s_core_optin WHERE hash=?', array($action->Request()->sNotificationConfirmation));
  201. }
  202. if ($notificationConfirmed) {
  203. $sql = '
  204. INSERT INTO `s_articles_notification` (
  205. `ordernumber` ,
  206. `date` ,
  207. `mail` ,
  208. `language` ,
  209. `shopLink` ,
  210. `send`
  211. )
  212. VALUES (
  213. ?, NOW(), ?, ?, ?, 0
  214. );
  215. ';
  216. Shopware()->Db()->query($sql, array(
  217. $json_data['notifyOrdernumber'],
  218. $json_data['sNotificationEmail'],
  219. $json_data['sLanguage'],
  220. $json_data['sShopPath']
  221. ));
  222. $action->View()->NotifyValid = true;
  223. } else {
  224. $action->View()->NotifyInvalid = true;
  225. }
  226. }
  227. return $action->forward('index');
  228. }
  229. /**
  230. * Cronjob method
  231. * Check all products from s_articles_notification
  232. * Inform customer if any status update available
  233. * @static
  234. * @param Shopware_Components_Cron_CronJob $job
  235. * @return void
  236. */
  237. public static function onRunCronJob(Shopware_Components_Cron_CronJob $job)
  238. {
  239. $sql = "SELECT * FROM `s_articles_notification` WHERE send = 0";
  240. $getNotifications = Shopware()->Db()->fetchAll($sql);
  241. foreach ($getNotifications as $data) {
  242. $ordernumber = $data["ordernumber"];
  243. $sArticle = Shopware()->Db()->fetchRow("SELECT a.id as articleID, d.ordernumber, d.instock, a.active FROM s_articles_details d, s_articles a WHERE d.articleID=a.id AND d.ordernumber=?", array($ordernumber));
  244. $sArticleID = $sArticle["articleID"];
  245. if (empty($sArticleID)) continue;
  246. $instock = $sArticle["instock"];
  247. $sql = "SELECT notification from s_articles WHERE ID = ?";
  248. $notificationActive = Shopware()->Db()->fetchOne($sql, array($sArticleID));
  249. if (intval($instock) > 0 && $notificationActive == true && !empty($sArticle["active"])) {
  250. $context = array(
  251. 'sArticleLink' => $data["shopLink"] . "?sViewport=detail&sArticle=$sArticleID",
  252. 'sOrdernumber' => $ordernumber,
  253. 'sData' => $job["data"],
  254. );
  255. $mail = Shopware()->TemplateMail()->createMail('sARTICLEAVAILABLE', $context);
  256. $mail->addTo($data["mail"]);
  257. $mail->send();
  258. //set notification to already send
  259. $sql = "UPDATE `s_articles_notification` SET `send` = '1' WHERE `ordernumber` =?";
  260. Shopware()->Db()->query($sql, array($data["ordernumber"]));
  261. // doing update on s_articles_notification
  262. }
  263. }
  264. }
  265. }