/techne/expressionengine/libraries/Subscription.php

https://github.com/mondomon916/LYBC · PHP · 491 lines · 276 code · 85 blank · 130 comment · 43 complexity · 7a7a980ce043f4e40fc1a2d586da41f3 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Subscription Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Libraries
  19. * @category Subscription
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class EE_Subscription {
  24. var $hash;
  25. var $module; // module, also used as table name
  26. var $anonymous = FALSE; // allow anonymous subscriptions? if true, table must have email column
  27. var $publisher = array();
  28. var $table;
  29. /**
  30. * Constructor
  31. *
  32. * @access public
  33. */
  34. function __construct()
  35. {
  36. // Get EE superobject reference
  37. $this->EE =& get_instance();
  38. }
  39. // --------------------------------------------------------------------
  40. /**
  41. * init the library
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. function init($module, $publisher = array(), $anonymous = FALSE)
  47. {
  48. $this->module = $module;
  49. $this->publisher = $publisher;
  50. $this->anonymous = $anonymous;
  51. $this->table = $module.'_subscriptions';
  52. }
  53. // --------------------------------------------------------------------
  54. /**
  55. * Check if they're subscribed
  56. *
  57. * @access public
  58. * @param mixed identifiers
  59. * @return bool
  60. */
  61. function is_subscribed($identifiers = FALSE)
  62. {
  63. $user = $this->_prep($identifiers);
  64. if ( ! $user)
  65. {
  66. return FALSE;
  67. }
  68. list($member_ids, $emails) = $user;
  69. if ( ! count($member_ids) && ! count($emails))
  70. {
  71. return;
  72. }
  73. $func = 'where_in';
  74. if (count($member_ids))
  75. {
  76. $this->EE->db->where_in('member_id', $member_ids);
  77. $func = 'or_where_in';
  78. }
  79. if (count($emails))
  80. {
  81. $this->EE->db->$func('email', $emails);
  82. }
  83. $this->EE->db->select('member_id');
  84. $this->EE->db->where($this->publisher);
  85. $query = $this->EE->db->get($this->table);
  86. if ($query->num_rows() > 0)
  87. {
  88. return TRUE;
  89. }
  90. return FALSE;
  91. }
  92. // --------------------------------------------------------------------
  93. /**
  94. * Mark post as read
  95. *
  96. * @access public
  97. * @return void
  98. */
  99. function mark_as_read($identifiers = FALSE, $skip_prep = FALSE)
  100. {
  101. $this->_mark($identifiers, 'n', $skip_prep);
  102. }
  103. // --------------------------------------------------------------------
  104. /**
  105. * Mark post as unread
  106. *
  107. * @access public
  108. * @return void
  109. */
  110. function mark_as_unread($identifiers = FALSE, $skip_prep = FALSE)
  111. {
  112. $this->_mark($identifiers, 'y', $skip_prep);
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Add subscriptions for current post
  117. *
  118. * @access public
  119. * @return void
  120. */
  121. function subscribe($identifiers = FALSE, $mark_existing = TRUE)
  122. {
  123. $rand = '';
  124. $user = $this->_prep($identifiers);
  125. if ( ! $user)
  126. {
  127. return;
  128. }
  129. $existing_ids = array();
  130. $existing_emails = array();
  131. $subscriptions = $this->get_subscriptions();
  132. foreach($subscriptions as $row)
  133. {
  134. if ($row['member_id'])
  135. {
  136. $existing_ids[] = $row['member_id'];
  137. }
  138. else
  139. {
  140. $existing_emails[] = $row['email'];
  141. }
  142. }
  143. list($member_ids, $emails) = $user;
  144. // Handle duplicates
  145. $new_member_ids = array_diff($member_ids, $existing_ids);
  146. $new_emails = array_diff($emails, $existing_emails);
  147. if (count($new_member_ids) OR count($new_emails))
  148. {
  149. $data = array();
  150. $default = $this->publisher;
  151. // Add member ids
  152. foreach($new_member_ids as $id)
  153. {
  154. $rand = $id.$this->EE->functions->random('alnum', 8);
  155. $data[] = array_merge($default, array(
  156. 'hash' => $rand,
  157. 'member_id' => $id,
  158. 'email' => '',
  159. 'subscription_date' => $this->EE->localize->now
  160. ));
  161. }
  162. // Add emails
  163. foreach($new_emails as $email)
  164. {
  165. $rand = $this->EE->functions->random('alnum', 15);
  166. $data[] = array_merge($default, array(
  167. 'hash' => $rand,
  168. 'member_id' => 0,
  169. 'email' => $email,
  170. 'subscription_date' => $this->EE->localize->now
  171. ));
  172. }
  173. // Batch it in case there are lots of them
  174. $this->EE->db->insert_batch($this->table, $data);
  175. }
  176. // Refresh existing subscriptions if there were any
  177. // @todo update subscription date
  178. if ($mark_existing)
  179. {
  180. $member_ids = array_intersect($member_ids, $existing_ids);
  181. $emails = array_intersect($emails, $existing_emails);
  182. $dupes = array($member_ids, $emails);
  183. $this->mark_as_read($dupes, TRUE);
  184. }
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Remove subscriptions for current post
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. function unsubscribe($identifiers = FALSE, $hash = FALSE)
  194. {
  195. if ($hash != '')
  196. {
  197. $this->EE->db->where('hash', $hash);
  198. }
  199. else
  200. {
  201. $user = $this->_prep($identifiers);
  202. if ( ! $user)
  203. {
  204. return;
  205. }
  206. list($member_ids, $emails) = $user;
  207. if ( ! count($member_ids) && ! count($emails))
  208. {
  209. return;
  210. }
  211. $func = 'where_in';
  212. if (count($member_ids))
  213. {
  214. $this->EE->db->where_in('member_id', $member_ids);
  215. $func = 'or_where_in';
  216. }
  217. if (count($emails))
  218. {
  219. $this->EE->db->$func('email', $emails);
  220. }
  221. }
  222. $this->EE->db->where($this->publisher);
  223. $this->EE->db->delete($this->table);
  224. }
  225. // --------------------------------------------------------------------
  226. /**
  227. * Remove all subscriptions for a publisher
  228. *
  229. * Call this when removing posts to avoid cluttering up the subscription table
  230. *
  231. * @access public
  232. * @return void
  233. */
  234. function delete_subscriptions()
  235. {
  236. $this->EE->db->where($this->publisher);
  237. $this->EE->db->delete($this->table);
  238. }
  239. // --------------------------------------------------------------------
  240. /**
  241. * Get subscribers
  242. *
  243. * @access public
  244. * @param bool Return array with member ids instead of looking up their emails (used internally)
  245. * @return mixed Array of email addresses
  246. */
  247. function get_subscriptions($ignore = FALSE)
  248. {
  249. $emails = array();
  250. $member_ids = array();
  251. // Grab them all
  252. if ($this->anonymous)
  253. {
  254. $this->EE->db->select('email');
  255. }
  256. if ($ignore)
  257. {
  258. if (is_numeric($ignore) && $ignore != 0)
  259. {
  260. $this->EE->db->where('member_id !=', $ignore);
  261. }
  262. elseif ($this->anonymous)
  263. {
  264. $this->EE->db->where('email !=', $ignore);
  265. }
  266. }
  267. $this->EE->db->select('subscription_id, member_id, notification_sent, hash');
  268. $this->EE->db->where($this->publisher);
  269. $query = $this->EE->db->get($this->table);
  270. if ( ! $query->num_rows())
  271. {
  272. return array();
  273. }
  274. $return = array();
  275. foreach($query->result_array() as $subscription)
  276. {
  277. if ($subscription['member_id'] != 0)
  278. {
  279. $return[$subscription['subscription_id']] = $subscription;
  280. }
  281. elseif ($this->anonymous && $subscription['email'])
  282. {
  283. $return[$subscription['subscription_id']] = $subscription;
  284. }
  285. }
  286. return $return;
  287. }
  288. // --------------------------------------------------------------------
  289. /**
  290. * Prep user data
  291. *
  292. * Figure out the member ids and email addresses we're working with
  293. *
  294. * @access private
  295. * @param mixed Values to identify the subscriber(s)
  296. * @return mixed
  297. */
  298. function _prep($identifiers = FALSE)
  299. {
  300. static $current_user = '';
  301. $emails = array();
  302. $member_ids = array();
  303. // No user specified? Use the current one
  304. if ($identifiers == FALSE)
  305. {
  306. if ($current_user === '')
  307. {
  308. $current_user = $this->_get_current_user();
  309. }
  310. // get_current_user returns false if it can't
  311. // find an existing identifier
  312. if ($current_user === FALSE)
  313. {
  314. return FALSE;
  315. }
  316. $array = key($current_user).'s';
  317. ${$array}[] = current($current_user);
  318. }
  319. else
  320. {
  321. if ( ! is_array($identifiers))
  322. {
  323. $identifiers = array($identifiers);
  324. }
  325. foreach($identifiers as $email_or_id)
  326. {
  327. if ( ! is_numeric($email_or_id))
  328. {
  329. if ($this->anonymous == TRUE)
  330. {
  331. $emails[] = $email_or_id;
  332. }
  333. }
  334. else
  335. {
  336. $member_ids[] = $email_or_id;
  337. }
  338. }
  339. }
  340. return array($member_ids, $emails);
  341. }
  342. // --------------------------------------------------------------------
  343. /**
  344. * Mark a subscription as read / unread
  345. *
  346. * @access private
  347. * @param mixed Values to identify the subscriber(s)
  348. * @param string New subscription_sent status (y | n)
  349. * @param bool Skip call to _prep (used internally)
  350. * @return void
  351. */
  352. function _mark($identifiers, $new_state, $skip_prep = FALSE)
  353. {
  354. if ( ! $skip_prep)
  355. {
  356. $identifiers = $this->_prep($identifiers);
  357. if ( ! $identifiers)
  358. {
  359. return;
  360. }
  361. }
  362. list($member_ids, $emails) = $identifiers;
  363. if ( ! count($member_ids) && ! count($emails))
  364. {
  365. return;
  366. }
  367. $func = 'where_in';
  368. if (count($member_ids))
  369. {
  370. $this->EE->db->where_in('member_id', $member_ids);
  371. $func = 'or_where_in';
  372. }
  373. if (count($emails))
  374. {
  375. $this->EE->db->$func('email', $emails);
  376. }
  377. $this->EE->db->set('notification_sent', $new_state);
  378. $this->EE->db->where($this->publisher);
  379. $this->EE->db->update($this->table);
  380. }
  381. // --------------------------------------------------------------------
  382. /**
  383. * Identify the current user
  384. *
  385. * @access private
  386. * @return mixed
  387. */
  388. function _get_current_user()
  389. {
  390. // They're logged in!
  391. if ($this->EE->session->userdata('member_id') != 0)
  392. {
  393. return array('member_id' => $this->EE->session->userdata('member_id'));
  394. }
  395. // my_email cookie is set
  396. elseif ($this->EE->session->userdata('email'))
  397. {
  398. return array('email' => $this->EE->session->userdata('email'));
  399. }
  400. // anonymous
  401. return FALSE;
  402. }
  403. }
  404. // END Subscription class
  405. /* End of file Subscription.php */
  406. /* Location: ./system/expressionengine/libraries/Subscription.php */