21 parent::__construct();
24 $this->_sPrefix =
'sys_sbs_';
29 if(!isset($aParams[
'user_id']) || (
int)$aParams[
'user_id'] == 0) {
30 $aParams[
'user_id'] = 0;
31 if(!
empty($aParams[
'name']) && !
empty($aParams[
'email']))
32 $aParams[
'user_id'] = (int)(
int)$this->
getOne(
"SELECT `id` FROM `" . $this->_sPrefix .
"users` WHERE `name`='" . $aParams[
'name'] .
"' AND `email`='" . $aParams[
'email'] .
"' LIMIT 1");
39 FROM `" . $this->_sPrefix .
"entries` AS `tse`
40 LEFT JOIN `" . $this->_sPrefix .
"types` AS `tst` ON `tse`.`subscription_id`=`tst`.`id`
42 `tst`.`unit`='" . $aParams[
'unit'] .
"' AND
43 " . (!
empty($aParams[
'action']) ?
"`tst`.`action`='" . $aParams[
'action'] .
"' AND " :
"") .
"
44 `tse`.`subscriber_id`='" . $aParams[
'user_id'] .
"' AND
45 `tse`.`subscriber_type`='" . $iType .
"'" .
46 ((
int)$aParams[
'object_id'] != 0 ?
" AND `tse`.`object_id`='" . $aParams[
'object_id'] .
"'" :
"");
47 return !
empty($aParams[
'user_id']) && (int)$this->
getOne($sSql) > 0;
55 `template` AS `template`,
57 FROM `" . $this->_sPrefix .
"types`
58 WHERE `unit`= ? AND `action`= ?
68 `template` AS `template`,
70 FROM `" . $this->_sPrefix .
"types`
72 return $this->
getAll($sSql, [$sUnit]);
78 `tt`.`unit` AS `unit`,
79 `tt`.`action` AS `action`,
80 `tt`.`params` AS `params`,
82 GROUP_CONCAT(`ste`.`id` ORDER BY `ste`.`id`)
83 FROM `" . $this->_sPrefix .
"entries` AS `ste`
84 LEFT JOIN `" . $this->_sPrefix .
"types` AS `stt` ON `ste`.`subscription_id`=`stt`.`id`
85 WHERE `stt`.`unit`=`tt`.`unit` AND `ste`.`object_id`=`te`.`object_id`
86 GROUP BY `stt`.`unit`, `ste`.`object_id`) AS `entry_id`,
87 `te`.`object_id` AS `object_id`
88 FROM `" . $this->_sPrefix .
"entries` AS `te`
89 LEFT JOIN `" . $this->_sPrefix .
"types` AS `tt` ON `te`.`subscription_id`=`tt`.`id`
90 WHERE `tt`.`action`='' AND `te`.`subscriber_id`= ? AND `te`.`subscriber_type`='" .
CH_WSB_SBS_TYPE_MEMBER .
"'
91 ORDER BY `tt`.`unit`, `te`.`object_id`";
92 return $this->
getAll($sSql, [$iUserId]);
96 switch($aParams[
'type']) {
100 if(
empty($sUserName) ||
empty($sUserEmail) || !(
bool)preg_match(
'/^([a-z0-9\+\_\-\.]+)@([a-z0-9\+\_\-\.]+)$/i', $sUserEmail))
101 return array(
'code' => 4,
'message' =>
_t(
'_sys_txt_sbs_empty_name_email'));
103 $iUserId = (int)$this->
getOne(
"SELECT `id` FROM `" . $this->_sPrefix .
"users` WHERE `email`='" . $sUserEmail .
"' LIMIT 1");
107 $mixedResult = $this->
query(
"INSERT INTO `" . $this->_sPrefix .
"users`(`name`, `email`, `date`) VALUES('" . $sUserName .
"', '" . $sUserEmail .
"', UNIX_TIMESTAMP())");
108 if($mixedResult ===
false)
109 return array(
'code' => 1,
'message' =>
_t(
'_sys_txt_sbs_cannot_save_visitor'));
111 $iUserId = (int)$this->
lastId();
122 $aSubscriptions = $this->
getSubscriptions($aParams[
'unit'], $aParams[
'action']);
123 if(!is_array($aSubscriptions) ||
empty($aSubscriptions))
124 return array(
'code' => 2,
'message' =>
_t(
'_sys_txt_sbs_cannot_find_subscription'));
126 $aTemplateParams = array();
128 foreach($aSubscriptions
as $aSubscription) {
129 if($aSubscription[
'action'] == $aParams[
'action'] && !
empty($aSubscription[
'params'])) {
130 $oFunction =
function($arg1, $arg2, $arg3)
use ($aSubscription) {
131 return eval($aSubscription[
'params']);
134 $aUnitParams = $oFunction($aParams[
'unit'], $aParams[
'action'], $aParams[
'object_id']);
142 $iEntryId = (int)$this->
getOne(
"SELECT `id` FROM `" . $this->_sPrefix .
"entries` WHERE `subscriber_id`='" . $iUserId .
"' AND `subscriber_type`='" . $aParams[
'type'] .
"' AND `subscription_id`='" . $aSubscription[
'id'] .
"' AND `object_id`='" . (
int)$aParams[
'object_id'] .
"' LIMIT 1");
143 if(!
empty($iEntryId))
144 return array(
'code' => 3,
'message' =>
_t(
'_sys_txt_sbs_already_subscribed'));
146 $iResult = (int)$this->
query(
"INSERT INTO `" . $this->_sPrefix .
"entries`(`subscriber_id`, `subscriber_type`, `subscription_id`, `object_id`) VALUES('" . $iUserId .
"', '" . $aParams[
'type'] .
"', '" . $aSubscription[
'id'] .
"', '" . (
int)$aParams[
'object_id'] .
"')");
153 $aTemplateParams = array (
154 'RealName' => $sUserName,
155 'SysUnsubscribeLink' => $this->_oSubscription->_getUnsubscribeLink(
$aResults)
157 if(isset($aUnitParams[
'template']))
158 $aTemplateParams = array_merge($aTemplateParams, $aUnitParams[
'template']);
160 $aMail = $oEmailTemplate->parseTemplate(
't_Subscription', $aTemplateParams);
161 sendMail($sUserEmail, $aMail[
'subject'], $aMail[
'body']);
163 $aResult = array(
'code' => 0,
'message' =>
_t(
'_sys_txt_sbs_success_subscribe'));
165 $aResult = array(
'code' => 5,
'message' =>
_t(
'_sys_txt_sbs_error_occured'));
171 switch($aParams[
'type']) {
173 if(isset($aParams[
'user_id']))
174 $iUserId = (int)$aParams[
'user_id'];
175 else if(isset($aParams[
'user_name']) && isset($aParams[
'user_email']))
181 $iUserId = (int)$aParams[
'user_id'];
188 if(isset($aParams[
'unit']) && isset($aParams[
'action'])) {
189 $aSubscriptions = $this->
getSubscriptions($aParams[
'unit'], $aParams[
'action']);
190 if(!is_array($aSubscriptions) ||
empty($aSubscriptions))
191 return array(
'code' => 2,
'message' =>
_t(
'_sys_txt_sbs_cannot_find_subscription'));
193 foreach($aSubscriptions
as $aSubscription)
194 $iResult += (int)$this->
query(
"DELETE FROM `" . $this->_sPrefix .
"entries` WHERE `subscriber_id`='" . $iUserId .
"' AND `subscriber_type`='" . $iUserType .
"' AND `subscription_id`='" . $aSubscription[
'id'] .
"'" . ((
int)$aParams[
'object_id'] != 0 ?
" AND `object_id`='" . (
int)$aParams[
'object_id'] .
"'" :
""));
197 else if(isset($aParams[
'unit']) && isset($aParams[
'object_id'])) {
199 if(is_array($aSubscriptions) && !
empty($aSubscriptions)) {
200 foreach($aSubscriptions
as $aSubscription)
201 $aIds[] = $aSubscription[
'id'];
203 $iResult = (int)$this->
query(
"DELETE FROM `" . $this->_sPrefix .
"entries` WHERE `subscription_id` IN ('" . implode(
"','", $aIds) .
"') AND `object_id`='" . (int)$aParams[
'object_id'] .
"'");
208 else if(isset($aParams[
'sid'])) {
209 $aIds = explode(
",", base64_decode(urldecode($aParams[
'sid'])));
210 if(is_array($aIds) && !
empty($aIds)) {
211 foreach ($aIds
as $k => $v)
214 list($iUserId, $iUserType) = $this->
getRow(
"SELECT `subscriber_id`, `subscriber_type` FROM `" . $this->_sPrefix .
"entries` WHERE `id`= ? LIMIT 1", [$aIds[0]], PDO::FETCH_NUM);
216 $iResult = (int)$this->
query(
"DELETE FROM `" . $this->_sPrefix .
"entries` WHERE `id` IN ('" . implode(
"','", $aIds) .
"')");
221 $iResult = (int)$this->
query(
"DELETE FROM `" . $this->_sPrefix .
"entries` WHERE `subscriber_id`='" . $iUserId .
"' AND `subscriber_type`='" . $iUserType .
"'");
224 $iSbsEntries = (int)$this->
getOne(
"SELECT COUNT(`id`) FROM `" . $this->_sPrefix .
"entries` WHERE `subscriber_id`='" . $iUserId .
"' AND `subscriber_type`='" .
CH_WSB_SBS_TYPE_VISITOR .
"' LIMIT 1");
225 if($iSbsEntries == 0)
226 $this->
query(
"DELETE FROM `" . $this->_sPrefix .
"users` WHERE `id`='" . $iUserId .
"' LIMIT 1");
229 return $iResult > 0 ? array(
'code' => 0,
'message' =>
_t(
'_sys_txt_sbs_success_unsubscribe')) : array(
'code' => 4,
'message' =>
_t(
'_sys_txt_sbs_already_unsubscribed'));
236 $aSubscription = $this->
getSubscription($aParams[
'unit'], $aParams[
'action']);
238 if(!
empty($aSubscription[
'params'])) {
239 $oFunction =
function($arg1, $arg2, $arg3)
use ($aSubscription) {
240 return eval($aSubscription[
'params']);
243 $aUnitParams = $oFunction($aParams[
'unit'], $aParams[
'action'], $aParams[
'object_id']);
246 if(isset($aUnitParams[
'skip']) && $aUnitParams[
'skip'] ===
true)
249 $aSubscribers = $this->
getAll(
"SELECT `id` AS `subscription_id`, `subscriber_id` AS `id`, `subscriber_type` AS `type` FROM `" . $this->_sPrefix .
"entries` WHERE `subscription_id`='" . (
empty($aSubscription[
'id']) ? 0 : $aSubscription[
'id']) .
"'" . ((
int)$aParams[
'object_id'] != 0 ?
" AND `object_id`='" . $aParams[
'object_id'] .
"'" :
""));
250 foreach($aSubscribers
as $aSubscriber) {
251 switch($aSubscriber[
'type']) {
253 $sSql =
"SELECT '0' AS `id`, `name`, `email` FROM `" . $this->_sPrefix .
"users` WHERE `id`= ? LIMIT 1";
256 $sSql =
"SELECT `ID` AS `id`, `NickName` AS `name`, `Email` AS `email` FROM `Profiles` WHERE `ID`= ? LIMIT 1";
264 FROM `" . $this->_sPrefix .
"entries` AS `tse`
265 LEFT JOIN `" . $this->_sPrefix .
"types` AS `tst` ON `tse`.`subscription_id`=`tst`.`id` AND `tst`.`unit`='" . $aParams[
'unit'] .
"' AND `tst`.`action`<>''
266 WHERE `tse`.`subscriber_id`='" . $aSubscriber[
'id'] .
"' AND `tse`.`subscriber_type`='" . $aSubscriber[
'type'] .
"'" . ((int)$aParams[
'object_id'] != 0 ?
" AND `object_id`='" . $aParams[
'object_id'] .
"'" :
"");
269 $aTemplateParams = array(
271 'Email' =>
$aUser[
'email'],
272 'ObjectId' => $aParams[
'object_id'],
273 'UnsubscribeLink' => $this->_oSubscription->_getUnsubscribeLink((
int)$aSubscriber[
'subscription_id']),
274 'UnsubscribeAllLink' => $this->_oSubscription->_getUnsubscribeLink($aEntries),
276 if(isset($aUnitParams[
'template']))
277 $aTemplateParams = array_merge($aTemplateParams, $aUnitParams[
'template']);
279 $aMail = $oEmailTemplates->parseTemplate($aSubscription[
'template'], $aTemplateParams, (
int)
$aUser[
'id']);
291 COUNT(DISTINCT `tsu`.`id`) AS `count`
292 FROM `" . $this->_sPrefix .
"users` AS `tsu`
293 INNER JOIN `" . $this->_sPrefix .
"entries` AS `tse` ON `tsu`.`id`=`tse`.`subscriber_id` AND `tse`.`subscriber_type`='" .
CH_WSB_SBS_TYPE_VISITOR .
"'
299 COUNT(DISTINCT `tsu`.`ID`) AS `count`
300 FROM `Profiles` AS `tsu`
301 INNER JOIN `" . $this->_sPrefix .
"entries` AS `tse` ON `tsu`.`ID`=`tse`.`subscriber_id` AND `tse`.`subscriber_type`='" .
CH_WSB_SBS_TYPE_MEMBER .
"'
306 return (
int)$this->
getOne($sSql);
314 `tsu`.`name` AS `name`,
315 `tsu`.`email` AS `email`
316 FROM `" . $this->_sPrefix .
"users` AS `tsu`
317 INNER JOIN `" . $this->_sPrefix .
"entries` AS `tse` ON `tsu`.`id`=`tse`.`subscriber_id` AND `tse`.`subscriber_type`='" .
CH_WSB_SBS_TYPE_VISITOR .
"'
320 LIMIT " . $iStart .
"," . $iCount;
325 `tsu`.`NickName` AS `name`,
326 `tsu`.`Email` AS `email`
327 FROM `Profiles` AS `tsu`
328 INNER JOIN `" . $this->_sPrefix .
"entries` AS `tse` ON `tsu`.`ID`=`tse`.`subscriber_id` AND `tse`.`subscriber_type`='" .
CH_WSB_SBS_TYPE_MEMBER .
"'
331 LIMIT " . $iStart .
"," . $iCount;
334 return $this->
getAll($sSql);