source: trunk/inc/classes/BxDolSubscription.php

Last change on this file was 2, checked in by george, 14 years ago
  • Přidáno: Trunk revize 13719.
File size: 13.2 KB
Line 
1<?
2/***************************************************************************
3* Dolphin Smart Community Builder
4* -------------------
5* begin : Mon Mar 23 2006
6* copyright : (C) 2007 BoonEx Group
7* website : http://www.boonex.com
8* This file is part of Dolphin - Smart Community Builder
9*
10* Dolphin is free software; you can redistribute it and/or modify it under
11* the terms of the GNU General Public License as published by the
12* Free Software Foundation; either version 2 of the
13* License, or any later version.
14*
15* Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17* See the GNU General Public License for more details.
18* You should have received a copy of the GNU General Public License along with Dolphin,
19* see license.txt file; if not, write to marketing@boonex.com
20***************************************************************************/
21
22bx_import('BxDolMistake');
23bx_import('BxDolSubscriptionQuery');
24bx_import('BxDolEmailTemplates');
25
26define('BX_DOL_SBS_TYPE_VISITOR', 0);
27define('BX_DOL_SBS_TYPE_MEMBER', 1);
28
29/*
30 * Subscriptions for any content changes.
31 *
32 * Integration of the content with subscriptions engine allows
33 * site member and visitors to subscribe to any content changes.
34 *
35 * Related classes:
36 * BxDolSubscriptionQuery - database queries.
37 *
38 * Example of usage:
39 * 1. Register all your subscriptions in `sys_sbs_types` database table.
40 * 2. Add necessary email templates in the `sys_email_templates` table.
41 * 3. Add necessary HTML/JavaScript data on the page where the 'Subscribe'
42 * button would be displayed. Use the following code
43 *
44 * $oSubscription = new BxDolSubscription();
45 * $oSubscription->getData();
46 *
47 * 4. Add Subscribe/Unsubscribe button using the following code.
48 *
49 * $oSubscription = new BxDolSubscription();
50 * $oSubscription->getButton($iUserId, $sUnit, $sAction, $iObjectId);
51 *
52 * @see an example of integration in the default Dolphin's modules(feedback, news, etc)
53 *
54 *
55 * Memberships/ACL:
56 * Doesn't depend on user's membership.
57 *
58 *
59 * Alerts:
60 * no alerts available
61 *
62 */
63class BxDolSubscription extends BxDolMistake {
64 var $_oDb;
65 var $_sJsObject;
66 var $_sActionUrl;
67 var $_sVisitorPopup;
68
69 /**
70 * constructor
71 */
72 function BxDolSubscription() {
73 parent::BxDolMistake();
74
75 $this->_oDb = new BxDolSubscriptionQuery($this);
76 $this->_sJsObject = 'oBxDolSubscription';
77 $this->_sActionUrl = $GLOBALS['site']['url'] . 'subscription.php';
78 $this->_sVisitorPopup = 'sbs_visitor_popup';
79 }
80 function getMySubscriptions() {
81 $aUserInfo = getProfileInfo();
82
83 $aSubscriptions = $this->_oDb->getSubscriptionsByUser((int)$aUserInfo['ID']);
84 if(empty($aSubscriptions))
85 return MsgBox(_t('_Empty'));
86
87 $aForm = array(
88 'form_attrs' => array(
89 'id' => 'sbs-subscriptions-form',
90 'name' => 'sbs-subscriptions-form',
91 'action' => $_SERVER['PHP_SELF'],
92 'method' => 'post',
93 'enctype' => 'multipart/form-data'
94 ),
95 'params' => array(),
96 'inputs' => array()
97 );
98 $sUnit = '';
99 $bCollapsed = true;
100 foreach($aSubscriptions as $aSubscription) {
101 if($sUnit != $aSubscription['unit']) {
102 if(!empty($sUnit))
103 $aForm['inputs'][$sUnit . '_end'] = array(
104 'type' => 'block_end'
105 );
106 $aForm['inputs'][$aSubscription['unit'] . '_begin'] = array(
107 'type' => 'block_header',
108 'caption' => _t('_sbs_txt_title_' . $aSubscription['unit']),
109 'collapsable' => true,
110 'collapsed' => $bCollapsed
111 );
112
113 $sUnit = $aSubscription['unit'];
114 $bCollapsed = true;
115 }
116
117 $oFunction = create_function('$arg1, $arg2, $arg3', $aSubscription['params']);
118 $aParams = $oFunction($aSubscription['unit'], $aSubscription['action'], $aSubscription['object_id']);
119
120 $sName = 'sbs-subscription_' . $aSubscription['entry_id'];
121 $aForm['inputs'][$sName] = array(
122 'type' => 'custom',
123 'name' => $sName,
124 'content' => '<a href="' . $aParams['template']['ViewLink'] . '">' . $aParams['template']['Subscription'] . '</a> <a href="javascript:void(0)" onclick="return unsubscribeConfirm(\'' . $this->_getUnsubscribeLink((int)$aSubscription['entry_id']) . '\');">' . _t('_sys_btn_sbs_unsubscribe') . '</a>',
125 'colspan' => true
126 );
127 }
128 //'' . . ''
129 $aForm['inputs'][$sUnit . '_end'] = array(
130 'type' => 'block_end'
131 );
132
133 $oForm = new BxTemplFormView($aForm);
134 $sContent = $oForm->getCode();
135
136 $GLOBALS['oSysTemplate']->addJsTranslation('_sbs_wrn_unsubscribe');
137 ob_start();
138 ?>
139 <script language="javascript" type="text/javascript">
140 <!--
141 function unsubscribeConfirm(sUrl){
142 if(confirm(_t('_sbs_wrn_unsubscribe'))) {
143 $.get(
144 sUrl + '&js=1',
145 {},
146 function(oData){
147 alert(oData.message);
148
149 if(oData.code == 0)
150 window.location.href = window.location.href;
151 },
152 'json'
153 );
154
155 return true;
156 }
157 else
158 return false;
159 }
160 -->
161 </script>
162 <?
163 $sContent .= ob_get_clean();
164
165 return $sContent;
166 }
167 function getData() {
168 ob_start();
169 ?>
170 <script language="javascript" type="text/javascript">
171 <!--
172 var <?=$this->_sJsObject; ?> = new BxDolSubscription({
173 sActionUrl: '<?=$this->_sActionUrl; ?>',
174 sObjName: '<?=$this->_sJsObject; ?>',
175 sVisitorPopup: '<?=$this->_sVisitorPopup; ?>'
176 });
177 -->
178 </script>
179 <?
180 $sContent = ob_get_clean();
181
182 $aForm = array(
183 'form_attrs' => array(
184 'id' => 'sbs_form',
185 'name' => 'sbs_form',
186 'action' => $this->_sActionUrl,
187 'method' => 'post',
188 'enctype' => 'multipart/form-data',
189 'onSubmit' => 'javascript: return ' . $this->_sJsObject . '.send(this);'
190
191 ),
192 'inputs' => array (
193 'direction' => array (
194 'type' => 'hidden',
195 'name' => 'direction',
196 'value' => ''
197 ),
198 'unit' => array (
199 'type' => 'hidden',
200 'name' => 'unit',
201 'value' => ''
202 ),
203 'action' => array (
204 'type' => 'hidden',
205 'name' => 'action',
206 'value' => ''
207 ),
208 'object_id' => array (
209 'type' => 'hidden',
210 'name' => 'object_id',
211 'value' => ''
212 ),
213 'user_name' => array (
214 'type' => 'text',
215 'name' => 'user_name',
216 'caption' => _t('_sys_txt_sbs_name'),
217 'value' => '',
218 'attrs' => array (
219 'id' => 'sbs_name'
220 )
221 ),
222 'user_email' => array (
223 'type' => 'text',
224 'name' => 'user_email',
225 'caption' => _t('_sys_txt_sbs_email'),
226 'value' => '',
227 'attrs' => array (
228 'id' => 'sbs_email'
229 )
230 ),
231 'sbs_controls' => array (
232 'type' => 'input_set',
233 array (
234 'type' => 'submit',
235 'name' => 'sbs_subscribe',
236 'value' => _t('_sys_btn_sbs_subscribe'),
237 'attrs' => array(
238 'onClick' => 'javascript:$("#' . $this->_sVisitorPopup . ' [name=\'direction\']").val(\'subscribe\')',
239 )
240 ),
241 array (
242 'type' => 'submit',
243 'name' => 'sbs_unsubscribe',
244 'value' => _t('_sys_btn_sbs_unsubscribe'),
245 'attrs' => array(
246 'onClick' => 'javascript:$("#' . $this->_sVisitorPopup . ' [name=\'direction\']").val(\'unsubscribe\')',
247 )
248 ),
249 )
250
251 )
252 );
253 $oForm = new BxTemplFormView($aForm);
254 $sContent .= PopupBox($this->_sVisitorPopup, _t('_sys_bcpt_subscribe'), $oForm->getCode());
255
256 $GLOBALS['oSysTemplate']->addCss('subscription.css');
257 $GLOBALS['oSysTemplate']->addJs('BxDolSubscription.js');
258 return $sContent;
259 }
260 function getButton($iUserId, $sUnit, $sAction = '', $iObjectId = 0) {
261 if($this->_oDb->isSubscribed(array('user_id' => $iUserId, 'unit' => $sUnit, 'action' => $sAction, 'object_id' => $iObjectId)))
262 $aResult = array(
263 'title' => _t('_sys_btn_sbs_unsubscribe'),
264 'script' => $this->_sJsObject . ".unsubscribe(" . $iUserId . ", '" . $sUnit . "', '" . $sAction . "', " . $iObjectId . ")"
265 );
266 else
267 $aResult = array(
268 'title' => _t('_sys_btn_sbs_subscribe'),
269 'script' => $this->_sJsObject . ".subscribe(" . $iUserId . ", '" . $sUnit . "', '" . $sAction . "', " . $iObjectId . ")"
270 );
271
272 return $aResult;
273 }
274
275 function subscribeVisitor($sUserName, $sUserEmail, $sUnit, $sAction, $iObjectId = 0) {
276 $aResult = $this->_processVisitor('add', $sUserName, $sUserEmail, $sUnit, $sAction, $iObjectId);
277 return $aResult;
278 }
279 function unsubscribeVisitor($sUserName, $sUserEmail, $sUnit, $sAction, $iObjectId = 0) {
280 return $this->_processVisitor('delete', $sUserName, $sUserEmail, $sUnit, $sAction, $iObjectId);
281 }
282 function subscribeMember($iUserId, $sUnit, $sAction, $iObjectId = 0) {
283 return $this->_processMember('add', $iUserId, $sUnit, $sAction, $iObjectId);
284 }
285 function unsubscribeMember($iUserId, $sUnit, $sAction, $iObjectId = 0) {
286 return $this->_processMember('delete', $iUserId, $sUnit, $sAction, $iObjectId);
287 }
288 function unsubscribe($aParams) {
289 $aRequest = array();
290
291 switch($aParams['type']) {
292 case 'sid';
293 $aRequest = array('sid' => $aParams['sid']);
294 break;
295 case 'object_id';
296 $aRequest = array('unit' => $aParams['unit'], 'object_id' => $aParams['object_id']);
297 break;
298 case 'visitor':
299 $aRequest = array(
300 'type' => BX_DOL_SBS_TYPE_VISITOR,
301 'user_id' => $aParams['id']
302 );
303 break;
304 case 'member':
305 $aRequest = array(
306 'type' => BX_DOL_SBS_TYPE_MEMBER,
307 'user_id' => $aParams['id']
308 );
309 break;
310 }
311 return $this->_oDb->deleteSubscription($aRequest);
312 }
313 function send($sUnit, $sAction, $iObjectId = 0, $aExtras = array()) {
314 return $this->_oDb->sendDelivery(array(
315 'unit' => $sUnit,
316 'action' => $sAction,
317 'object_id' => $iObjectId
318 ));
319 }
320 function getSubscribersCount($iType = BX_DOL_SBS_TYPE_VISITOR) {
321 return $this->_oDb->getSubscribersCount($iType);
322 }
323 function getSubscribers($iType = BX_DOL_SBS_TYPE_VISITOR, $iStart = 0, $iCount = 1) {
324 return $this->_oDb->getSubscribers($iType, $iStart, $iCount);
325 }
326
327 function _processMember($sDirection, $iUserId, $sUnit, $sAction, $iObjectId) {
328 $sMethodName = $sDirection . 'Subscription';
329 return $this->_oDb->$sMethodName(array(
330 'type' => BX_DOL_SBS_TYPE_MEMBER,
331 'user_id' => $iUserId,
332 'unit' => $sUnit,
333 'action' => $sAction,
334 'object_id' => $iObjectId
335 ));
336 }
337 function _processVisitor($sDirection, $sUserName, $sUserEmail, $sUnit, $sAction, $iObjectId) {
338 $sMethodName = $sDirection . 'Subscription';
339 return $this->_oDb->$sMethodName(array(
340 'type' => BX_DOL_SBS_TYPE_VISITOR,
341 'user_name' => $sUserName,
342 'user_email' => $sUserEmail,
343 'unit' => $sUnit,
344 'action' => $sAction,
345 'object_id' => $iObjectId
346 ));
347 }
348
349 function _getUnsubscribeLink($mixedIds) {
350 $aIds = array();
351 if(is_int($mixedIds))
352 $aIds = array($mixedIds);
353 else if(is_string($mixedIds))
354 $aIds = explode(",", $mixedIds);
355 else if(is_array($mixedIds))
356 $aIds = $mixedIds;
357
358 return !empty($aIds) ? $this->_sActionUrl . '?sid=' . urlencode(base64_encode(implode(",", $aIds))) : '';
359 }
360}
Note: See TracBrowser for help on using the repository browser.