PageRenderTime 679ms CodeModel.GetById 32ms RepoModel.GetById 10ms app.codeStats 0ms

/administrator/components/com_sm2emailmarketing/plugins/plug_sm2emcbplugin/sm2em_cb.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 575 lines | 378 code | 105 blank | 92 comment | 71 complexity | 457c8918abd44fa48435b90105689a60 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. // $Id: sm2em_cb.php,v 1.17 2008/04/21 03:35:09 sm2tony Exp $
  3. /**
  4. * sm2emailmarketing - Email Marketing Newsletter
  5. *
  6. * Joomla! Newsletter Component that supports multiple list mailing and message queueing
  7. *
  8. * @package Joomla!
  9. * @subpackage com_sm2emailmarketing
  10. * @copyright Copyright (C) 2006 SM2 / All Rights Reserved
  11. * @license commercial
  12. * @author SM2
  13. */
  14. /**
  15. * Makes sure this is included by a parent file
  16. */
  17. defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
  18. $_PLUGINS->registerFunction( 'onUserActive', 'userActivated','getSM2EMTab' );
  19. $_PLUGINS->registerFunction( 'onBeforeDeleteUser', 'userDeleted','getSM2EMTab' );
  20. global $mosConfig_absolute_path, $my;
  21. class getSM2EMTab extends cbTabHandler {
  22. var $receiveOptions = array(0=>'None',1=>'Text only',2=>'HTML');
  23. /**
  24. * Constructor
  25. */
  26. function getSM2EMTab() {
  27. $this->cbTabHandler();
  28. } // getSM2EMTab()
  29. function setReceiveOptions() {
  30. if (isset($this->params)) {
  31. if (is_object($this->params)) {
  32. $this->receiveOptions = array();
  33. $this->receiveOptions[0] = $this->params->get('receive_none','None');
  34. $this->receiveOptions[1] = $this->params->get('receive_text','Text only');
  35. $this->receiveOptions[2] = $this->params->get('receive_html','HTML');
  36. }
  37. }
  38. } // setReceiveOptions()
  39. function getSM2EMUser($id, $update=false) {
  40. global $mainframe, $mosConfig_lang;
  41. // load the sm2emailmarketingUser object
  42. require_once($mainframe->getPath('class', 'com_sm2emailmarketing'));
  43. if (file_exists($GLOBALS['mosConfig_absolute_path'].'/components/com_sm2emailmarketing/languages/'.$mosConfig_lang.'.php')) {
  44. include_once ($GLOBALS['mosConfig_absolute_path'].'/components/com_sm2emailmarketing/languages/'.$mosConfig_lang.'.php');
  45. } else {
  46. include_once ($GLOBALS['mosConfig_absolute_path'].'/components/com_sm2emailmarketing/languages/english.php');
  47. } // end if
  48. sm2emailmarketingConfig('com_sm2emailmarketing');
  49. $user = new sm2emailmarketingUser();
  50. $user->load((int)$id);
  51. if (!$update) {
  52. // validate the unsubscribe_date
  53. if ($user->unsubscribe_date===null) {
  54. $user->unsubscribe_date='0000-00-00 00:00:00';
  55. }
  56. // determine the status
  57. if ($user->unsubscribe_date!='0000-00-00 00:00:00' && $user->unsubscribe_reason==0) {
  58. $user->unsubscribe_reason=5;
  59. }
  60. if (empty($id)) {
  61. $user->unsubscribe_reason='NULL';
  62. }
  63. $statusVar = '_EMKTG_UNSUBSCRIBE_REASON_'.$user->unsubscribe_reason;
  64. if (defined($statusVar)) {
  65. $user->status = constant($statusVar);
  66. } else {
  67. $user->unsubscribe_reason = 5;
  68. $user->status = _EMKTG_UNSUBSCRIBE_REASON_5;
  69. }
  70. if (empty($id)) {
  71. $user->unsubscribe_reason=0;
  72. }
  73. }
  74. return $user;
  75. } // getSM2EMUser()
  76. function getDisplayTab($tab, $user, $ui) {
  77. global $my, $database;
  78. $this->setReceiveOptions();
  79. // check to see if this is a logged in user
  80. if (empty($my->id)) {
  81. mosNotAuth();
  82. return false;
  83. }
  84. if (empty($user->id)) {
  85. $userid = 0;
  86. } else {
  87. $userid = $user->id;
  88. }
  89. if(!$this->checkSM2EMInstalled()) {
  90. return _UE_NEWSLETTERNOTINSTALLED;
  91. }
  92. $params = $this->params;
  93. //$showReceive = $params->get('default_receive', 0);
  94. //$showReceive = (empty($showReceive) ? true : false);
  95. // determine this users receive_html option
  96. // load the sm2emailmarketingUser object
  97. $sm2emUser = $this->getSM2EMUser($userid);
  98. if ($sm2emUser->unsubscribe_date!='0000-00-00 00:00:00') {
  99. $sm2emUser->receive_html = 0;
  100. } else {
  101. $sm2emUser->receive_html++;
  102. }
  103. $emailreceived ='';
  104. if (!empty($userid)) {
  105. // determine the itemid
  106. $database->setQuery('SELECT `id`'
  107. .' FROM #__menu'
  108. .' WHERE `link` LIKE '.$database->Quote('%option=com_sm2emailmarketing%')
  109. .' AND `type`='.$database->Quote('components')
  110. .' ORDER BY `id` LIMIT 0, 1');
  111. $itemid = (int)$database->loadResult();
  112. $itemid = (empty($itemid) ? '' : '&Itemid='.$itemid);
  113. // get list of past emails sent
  114. $database->setQuery('SELECT sm.messageid, m.subject'
  115. .' FROM #__emktg_stats_message sm'
  116. .' LEFT JOIN #__emktg_message m on sm.messageid = m.messageid'
  117. .' WHERE sm.userid='.(int)$userid);
  118. $archivedemail = $database->loadAssocList();
  119. foreach($archivedemail as $email) {
  120. $url = 'index.php?option=com_sm2emailmarketing'
  121. .'&task=showarchivemessage'
  122. .'&id='.$email['messageid']
  123. .'&Itemid='.$itemid;
  124. if (function_exists('sefRelToAbs') && !defined('_JLEGACY')) {
  125. $url = sefRelToAbs($url);
  126. }
  127. $emailreceived .= '<a href="'.$url.'">'
  128. .$email['subject'].'</a><br />';
  129. }
  130. }
  131. $lists = '';
  132. $database->setQuery('SELECT list_name FROM #__emktg_list l'
  133. .' INNER JOIN #__emktg_list_user lu ON l.listid=lu.listid'
  134. .' WHERE lu.id='.(int)$userid
  135. .' ORDER BY list_name');
  136. $listNames = $database->loadResultArray();
  137. if (is_array($listNames)) {
  138. $lists = implode('<br />', $listNames);
  139. }
  140. unset($listNames);
  141. ob_start();
  142. ?>
  143. <table style="width:100%">
  144. <tr>
  145. <td width="25%" valign="top"><?php echo _EMKTG_UNSUBSCRIBE_STATUS; ?></td>
  146. <td width="75%"><?php echo $sm2emUser->status; ?></td>
  147. </tr>
  148. <tr>
  149. <td colspan="2" class="titleCell" valign="top" align="left"><?php echo $this->params->get('receive', 'Communication formats'); ?>:</td>
  150. </tr>
  151. <tr>
  152. <td width="25%">&nbsp;</td>
  153. <td width="75%">
  154. <?php echo $this->receiveOptions[$sm2emUser->receive_html]; ?>
  155. </td>
  156. </tr>
  157. <?php if (!empty($lists)) { ?>
  158. <tr>
  159. <td colspan="2" class="titleCell" valign="top" align="left"><?php echo $this->params->get('list_subscribed', 'Lists you have subscribed to'); ?>:</td>
  160. </tr>
  161. <tr>
  162. <td width="25%">&nbsp;</td>
  163. <td width="75%">
  164. <?php echo $lists; ?>
  165. </td>
  166. </tr>
  167. <?php } ?>
  168. <?php if (!empty($emailreceived)) { ?>
  169. <tr>
  170. <td colspan="2" class="titleCell" valign="top" align="left"><?php echo $this->params->get('archive_emails', 'Emails that you have been sent, click to view'); ?>:</td>
  171. </tr>
  172. <tr>
  173. <td width="25%">&nbsp;</td>
  174. <td width="75%">
  175. <?php echo $emailreceived; ?>
  176. </td>
  177. </tr>
  178. <?php } ?>
  179. </table>
  180. <?php
  181. $return = ob_get_contents();
  182. ob_end_clean();
  183. return $return;
  184. }
  185. /**
  186. * Generates the HTML to display the user edit tab
  187. * @param object tab reflecting the tab database entry
  188. * @param object mosUser reflecting the user being displayed
  189. * @param int 1 for front-end, 2 for back-end
  190. * @returns mixed : either string HTML for tab content, or false if ErrorMSG generated
  191. */
  192. function getEditTab($tab, $user, $ui) {
  193. global $my, $database;
  194. $this->setReceiveOptions();
  195. // check to see if this is a logged in user
  196. if (empty($my->id) && !empty($user)) {
  197. mosNotAuth();
  198. return false;
  199. }
  200. if (empty($user->id)) {
  201. $userid = 0;
  202. } else {
  203. $userid = $user->id;
  204. }
  205. if (!$this->checkSM2EMInstalled()) {
  206. return _UE_NEWSLETTERNOTINSTALLED;
  207. }
  208. $params = $this->params;
  209. $defaultReceive = $params->get('default_receive', 0);
  210. $lists = $this->findLists($userid, $ui);
  211. // load the sm2emailmarketingUser object
  212. $sm2emUser = $this->getSM2EMUser($userid);
  213. $receive_html = 2;
  214. if ($sm2emUser->unsubscribe_date!='0000-00-00 00:00:00') {
  215. // they are unsubscribed
  216. $receive_html = 0;
  217. } else {
  218. if ($sm2emUser->receive_html!==null) {
  219. $receive_html = $sm2emUser->receive_html + 1;
  220. }
  221. }
  222. if (!empty($defaultReceive)) {
  223. if ($defaultReceive==1) {
  224. unset($this->receiveOptions[2]);
  225. } else {
  226. unset($this->receiveOptions[1]);
  227. }
  228. }
  229. // get the htmlemail list
  230. $htmlemail = array();
  231. foreach ($this->receiveOptions as $index=>$value) {
  232. $htmlemail[] = mosHTML::makeOption($index,$value);
  233. }
  234. // determine the extra attributes needed by the receive_htmlDisplay
  235. $attributes = 'class="inputbox" size="1"';
  236. $onChange = '';
  237. if ($sm2emUser->unsubscribe_reason > 0) {
  238. // determine the resubscribe message
  239. $msgVar = '_EMKTG_RESUBSCRIBE_MSG_'.$sm2emUser->unsubscribe_reason;
  240. if (defined($msgVar)) {
  241. $onChange = ' onchange="confirmSubscribe(\''.constant($msgVar).'\');"';
  242. }
  243. }
  244. // determine what javascript processing to do on confirmSubscription
  245. $confirmJs = 'if (!confirm(msg)) {
  246. form.sm2em_receive_html.selectedIndex=0;
  247. }';
  248. $formJs = 'document.adminForm;';
  249. if ($ui==1) {
  250. //$formJs = 'document.EditUser;';
  251. // determine the confirmSubscription javascript based on the unsubscribe reason
  252. if ($sm2emUser->unsubscribe_reason > 1) {
  253. $confirmJs = 'form.sm2em_receive_html.selectedIndex=0;
  254. form.sm2em_receive_html.disabled = true;
  255. form.sm2em_receive_html.readonly = true;
  256. alert(\''.constant('_EMKTG_RESUBSCRIBE_ERROR_'.$sm2emUser->unsubscribe_reason).'\');';
  257. } else {
  258. $onChange = '';
  259. }
  260. }
  261. if ($ui==3) {
  262. $onChange = '';
  263. $sm2emUser->status = _EMKTG_UNSUBSCRIBE_REASON_NULL;
  264. }
  265. $receive_htmlDisplay = mosHTML::selectList($htmlemail, 'sm2em_receive_html', $attributes.$onChange, 'value', 'text', $receive_html);
  266. unset($htmlemail);
  267. ob_start();
  268. ?>
  269. <tr>
  270. <td colspan="2" class="titleCell" valign="top" align="left"><?php echo _EMKTG_UNSUBSCRIBE_STATUS; ?></td>
  271. </tr>
  272. <tr>
  273. <td width="25%">&nbsp;</td>
  274. <td width="75%">
  275. <script language="javascript" type="text/javascript">
  276. function confirmSubscribe(msg) {
  277. if (msg=='') return;
  278. var form = <?php echo $formJs; ?>;
  279. if (form.sm2em_receive_html.selectedIndex!=0) {
  280. <?php echo $confirmJs; ?>
  281. }
  282. }
  283. </script>
  284. <?php echo $sm2emUser->status; ?>
  285. </td>
  286. </tr>
  287. <tr>
  288. <td colspan="2" class="titleCell" valign="top" align="left"><?php echo $this->params->get('receive', 'Communication formats'); ?>:</td>
  289. </tr>
  290. <tr>
  291. <td width="25%">&nbsp;</td>
  292. <td width="75%">
  293. <?php echo $receive_htmlDisplay; ?>
  294. </td>
  295. </tr>
  296. <?php if ($lists) { ?>
  297. <tr>
  298. <td colspan="2" class="titleCell" valign="top" align="left"><?php echo $this->params->get('list_desc', 'Lists available to you'); ?>:</td>
  299. </tr>
  300. <tr>
  301. <td width="25%">&nbsp;</td>
  302. <td width="75%">
  303. <?php echo $lists; ?>
  304. </td>
  305. </tr>
  306. <?php } ?>
  307. <?php
  308. $return = ob_get_contents();
  309. ob_end_clean();
  310. return $return;
  311. } // getEditTab()
  312. /**
  313. * Saves the user edit tab postdata into the tab's permanent storage
  314. * @param object tab reflecting the tab database entry
  315. * @param object mosUser reflecting the user being displayed
  316. * @param int 1 for front-end, 2 for back-end
  317. * @param array _POST data for saving edited tab content as generated with getEditTab
  318. * @returns mixed : either string HTML for tab content, or false if ErrorMSG generated
  319. */
  320. function saveEditTab($tab, &$user, $ui, $postdata) {
  321. $receive_html = mosGetParam($_POST, 'sm2em_receive_html', 0);
  322. $lists = mosGetParam($_POST, 'sm2em_lists', array());
  323. // update the table as to whether they receive html or not
  324. // load the sm2emailmarketingUser object
  325. $sm2emUser = $this->getSM2EMUser($user->id, true);
  326. $forceInsert = false;
  327. $params = $this->params;
  328. $defaultReceive = $params->get('default_receive', 0);
  329. if (!empty($defaultReceive) && !empty($receive_html)) $receive_html = $defaultReceive;
  330. // check for non existing user
  331. if (empty($sm2emUser->id)) {
  332. $sm2emUser->id = $user->id;
  333. $sm2emUser->subscribe_date = date('Y-m-d H:i:s');
  334. $forceInsert = true;
  335. }
  336. if (empty($receive_html)) {
  337. $sm2emUser->unsubscribe_date = date('Y-m-d H:i:s');
  338. if (empty($sm2emUser->unsubscribe_reason)) {
  339. if ($ui==2) {
  340. $sm2emUser->unsubscribe_reason = 5;
  341. } else {
  342. $sm2emUser->unsubscribe_reason = 1;
  343. }
  344. if (!$forceInsert) {
  345. $msgSQL = 'select u.name, u.email, '.$sm2emUser->unsubscribe_reason.' as unsubscribe_reason'
  346. .' from #__users u inner join #__emktg_user s on (u.id=s.id) '
  347. .' where u.id='.(int)$user->id;
  348. }
  349. }
  350. } else {
  351. $sm2emUser->receive_html = $receive_html-1;
  352. $sm2emUser->unsubscribe_date = '0000-00-00 00:00:00';
  353. $sm2emUser->unsubscribe_reason = 0;
  354. }
  355. if (!$sm2emUser->store(false, $forceInsert)) {
  356. echo "<script> alert('".$user->_db->getErrorMsg()."'); window.history.go(-1); </script>\n";
  357. exit();
  358. }
  359. //update lists (what lists are they subscribed to)
  360. $sm2emUser->subscribe($lists);
  361. if (!empty($msgSQL)) {
  362. sm2emAdminUnsubscribeEmailFromQuery($msgSQL);
  363. }
  364. return true;
  365. } // saveEditTab()
  366. /**
  367. * Generates the HTML to display the registration tab/area
  368. * @param object tab reflecting the tab database entry
  369. * @param object mosUser reflecting the user being displayed (here null)
  370. * @param int 1 for front-end, 2 for back-end
  371. * @return mixed : either string HTML for tab content, or false if ErrorMSG generated
  372. */
  373. function getDisplayRegistration($tab, $user, $ui) {
  374. return $this->getEditTab($tab, $user, $ui);
  375. } // getDisplayRegistration()
  376. /**
  377. * Saves the registration tab/area postdata into the tab's permanent storage
  378. * @param object tab reflecting the tab database entry
  379. * @param object mosUser reflecting the user being displayed (here null)
  380. * @param int 1 for front-end, 2 for back-end
  381. * @param array _POST data for saving edited tab content as generated with getEditTab
  382. * @return boolean : either string HTML for tab content, or false if ErrorMSG generated
  383. */
  384. function saveRegistrationTab($tab, &$user, $ui, $postdata) {
  385. return $this->saveEditTab($tab, $user, $ui, $postdata);
  386. } // saveRegistrationTab()
  387. /**
  388. * Called once ConfirmationCode verified and user found and approved by moderator if needed
  389. * @param object tab reflecting the tab database entry
  390. * @param object mosUser reflecting the user being displayed
  391. * @param int 1 for front-end, 2 for back-end
  392. * @returns mixed : either string HTML for tab content, or false if ErrorMSG generated
  393. */
  394. function userActivated($user, $success) {
  395. if (empty($user->id)) return false;
  396. if ($success) {
  397. // update the table as to whether they confirmed
  398. // load the sm2emailmarketingUser object
  399. $sm2emuser = $this->getSM2EMUser($user->id, true);
  400. $sm2emuser->confirmed = $success;
  401. if (!$sm2emuser->store()) {
  402. return false;
  403. }
  404. }
  405. return true;
  406. } // userActivated()
  407. function userDeleted($user) {
  408. global $database;
  409. if (empty($user->id)) return false;
  410. // delete from queue if not yet processed
  411. $database->setQuery('DELETE FROM #__emktg_stats_message'
  412. .' WHERE userid='.(int)$user->id
  413. .' AND send_date=0');
  414. if (!$database->query()) return false;
  415. // delete from the lists
  416. $database->setQuery('DELETE FROM #__emktg_list_user'
  417. .' WHERE id='.(int)$user->id);
  418. if (!$database->query()) return false;
  419. // delete from the lists
  420. $database->setQuery('DELETE FROM #__emktg_user '
  421. .' WHERE id='.(int)$user->id);
  422. if (!$database->query()) return false;
  423. return true;
  424. } // userDeleted()
  425. /**
  426. * Check if the SM2 Email marketing component is installed
  427. */
  428. function checkSM2EMInstalled() {
  429. global $mosConfig_absolute_path;
  430. if (file_exists($mosConfig_absolute_path.'/components/com_sm2emailmarketing')) {
  431. return true;
  432. } else {
  433. return false;
  434. }
  435. } // checkSM2EMInstalled
  436. function findLists($userid, $ui=1){
  437. global $database, $acl;
  438. //get list of mailing lists available (public access only)
  439. //only if lists plugin is turned on
  440. $database->setQuery('SELECT enabled FROM #__emktg_plugin WHERE filename = '.$database->Quote('list.plugin.php'));
  441. $showlists = $database->loadResult();
  442. if (empty($showlists)) return;
  443. $database->setQuery('SELECT listid AS value FROM #__emktg_list_user WHERE id='.(int)$userid);
  444. $selected = $database->loadObjectList();
  445. $lists = array();
  446. if ($ui==2) {
  447. //admin - show all lists
  448. $database->setQuery('SELECT listid AS value, list_name AS text FROM #__emktg_list WHERE published=1');
  449. //also show lists they are subscribed to (selected)
  450. } else {
  451. //registering - show lists only avaialable to public and registered users
  452. $grp = $acl->getAroGroup($userid);
  453. $gid = 1;
  454. if ($grp !== null) {
  455. if ($acl->is_group_child_of( $grp->name, 'Registered', 'ARO' ) || $acl->is_group_child_of( $grp->name, 'Public Backend', 'ARO' )) {
  456. // fudge Authors, Editors, Publishers and Super Administrators into the Special Group
  457. $gid = 2;
  458. }
  459. }
  460. $database->setQuery('SELECT listid AS value, list_name AS text FROM #__emktg_list WHERE access<='.(int)$gid.' AND published=1');
  461. }
  462. $lists = $database->loadObjectList();
  463. if (!$lists) {
  464. return;
  465. }
  466. $size = min(count($lists), 10);
  467. return mosHTML::selectList($lists, 'sm2em_lists[]', 'class="inputbox" multiple size="'.$size.'"', 'value', 'text', $selected);
  468. }
  469. } // class getSM2EMTab
  470. ?>