AntonLV 7 vuotta sitten
vanhempi
sitoutus
dc187c7c18

+ 11 - 1
inc/classes/BxDolAccount.php

@@ -390,10 +390,20 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
         return $aAccountInfo['email'];
     }
 
+    /**
+     * Get account email
+     */
+    public function getLanguageId($iAccountId = false)
+    {
+        $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
+        $aAccountInfo = $this->getInfo($iAccountId);
+        return $aAccountInfo['lang_id'];
+    }
+
     /**
      * Is account online
      */
-	public function isOnline($iAccountId = false)
+    public function isOnline($iAccountId = false)
     {
         $iAccountId = (int)$iAccountId ? (int)$iAccountId : $this->_iAccountID;
         return $this->_oQuery->isOnline($iAccountId);

+ 3 - 2
inc/classes/BxDolEmailTemplates.php

@@ -107,8 +107,9 @@ class BxDolEmailTemplates extends BxDolFactory implements iBxDolSingleton
         if ($iAccountId) {
             $oAccount = BxDolAccount::getInstance($iAccountId);
             if ($oAccount) {
-                $aAccountInfo = $oAccount->getInfo();
-                $iUseLang = $aAccountInfo['lang_id'] ? $aAccountInfo['lang_id'] : $this->iDefaultLangId;
+                $iUseLang = $oAccount->getLanguageId();
+                if(empty($iUseLang))
+                    $iUseLang = $this->iDefaultLangId;
             }
         }
 

+ 29 - 9
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -780,7 +780,7 @@ class BxBaseModGeneralModule extends BxDolModule
      */
     public function serviceGetNotificationsPost($aEvent)
     {
-		$CNF = &$this->_oConfig->CNF;
+        $CNF = &$this->_oConfig->CNF;
 
         $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
         if(empty($aContentInfo) || !is_array($aContentInfo))
@@ -789,14 +789,34 @@ class BxBaseModGeneralModule extends BxDolModule
         $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
         $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
 
-		return array(
-			'entry_sample' => $CNF['T']['txt_sample_single'],
-			'entry_url' => $sEntryUrl,
-			'entry_caption' => $sEntryCaption,
-			'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
-			'entry_privacy' => '', //may be empty or not specified. In this case Public privacy will be used.
-			'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
-		);
+        return array(
+            'entry_sample' => $CNF['T']['txt_sample_single'],
+            'entry_url' => $sEntryUrl,
+            'entry_caption' => $sEntryCaption,
+            'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
+            'entry_privacy' => '', //may be empty or not specified. In this case Public privacy will be used.
+            'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
+            'settings' => array(
+                'email' => array(
+                    'subject' => 'My Custom Subject for ' . $sEntryCaption
+                ),
+            )
+            /*
+             * Custom settings for email and/or push notifications can be provided here. 
+             * Only necessary parts of 'settings' array can be used.
+             * 
+            'settings' => array(
+                'email' => array(
+                    'template' => '',   //--- custom email template
+                    'markers' => '',    //--- markers to parse email parts (subject, body) with
+                    'subject' => ''     //--- custom email subject
+                ),
+                'push' => array(
+                    'subject' => ''     //--- custom push notification subject
+                )
+            )
+             */
+        );
     }
 
 	/**

+ 18 - 0
modules/boonex/notifications/classes/BxNtfsModule.php

@@ -27,6 +27,24 @@ define('BX_NTFS_STYPE_FOLLOW_MEMBER', 'follow_member');
 define('BX_NTFS_STYPE_FOLLOW_CONTEXT', 'follow_context');
 define('BX_NTFS_STYPE_OTHER', 'other');
 
+/**
+ * SLTMODE - Silent mode:
+ * It is needed for alert sending module to tell that the alert should be ignored 
+ * with Notifications module completely or partially. Available values: 
+ * 1. disabled (value = 0) - all notifications are available;
+ * 2. absolute (value = 1) - alert isn't registered which means that there is no notifications at all;
+ * 3. on-site only (value = 2) - alert is registered. It means that on-site notification is available while the others are disabled.
+ * 4. on-site + email (value = 3) - alert is registered (on-site) and notification via 'email' is enabled.
+ * 5. on-site + push (value = 4) - alert is registered (on-site) and notification via 'push' is enabled.
+ * 
+ * @see BxNtfsResponse::response - 'silent_mode' parameter in Alerts Extras array.
+ */
+define('BX_NTFS_SLTMODE_DISABLED', 0);
+define('BX_NTFS_SLTMODE_ABSOLUTE', 1);
+define('BX_NTFS_SLTMODE_SITE', 2);
+define('BX_NTFS_SLTMODE_SITE_EMAIL', 3);
+define('BX_NTFS_SLTMODE_SITE_PUSH', 4);
+
 class BxNtfsModule extends BxBaseModNotificationsModule
 {
     /**

+ 111 - 54
modules/boonex/notifications/classes/BxNtfsResponse.php

@@ -33,12 +33,8 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
         if(empty($aHandler) || !is_array($aHandler))
             return;
 
-        /**
-         * silent_mode is needed for alert sending module to tell that this alert
-         * should be ignored with Notifications module completely or partially.
-         * Currently Complete ignore is used only.
-         */
-        if(isset($oAlert->aExtras['silent_mode']) && (int)$oAlert->aExtras['silent_mode'] != 0)
+        $iSilentMode = $this->_getSilentMode($oAlert->aExtras);
+        if($iSilentMode == BX_NTFS_SLTMODE_ABSOLUTE)
             return;
 
         switch($aHandler['type']) {
@@ -51,7 +47,8 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 foreach($aDataItems as $aDataItem) {
                     $iId = $this->_oModule->_oDb->insertEvent($aDataItem);
                     if(!empty($iId)) {
-                        $this->sendNotifications($iId, $oAlert, $aHandler);
+                        if($iSilentMode != BX_NTFS_SLTMODE_SITE)
+                            $this->sendNotifications($iId, $oAlert, $aHandler);
 
                         $this->_oModule->onPost($iId);
                     }
@@ -63,16 +60,16 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 break;
 
             case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
-        		if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
-        			$this->_oModule->_oDb->deleteEvent(array('owner_id' => $oAlert->iObject));
+                if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
+                    $this->_oModule->_oDb->deleteEvent(array('owner_id' => $oAlert->iObject));
 
-        			$this->_oModule->_oDb->deleteEvent(array('action' => 'connection_added', 'object_id' => $oAlert->iObject));
-					break;
+                    $this->_oModule->_oDb->deleteEvent(array('action' => 'connection_added', 'object_id' => $oAlert->iObject));
+                        break;
             	}
 
             	$sMethod = 'getDeleteData' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);           	
             	if(!method_exists($this, $sMethod))
-            		$sMethod = 'getDeleteData';
+                    $sMethod = 'getDeleteData';
 
                 $aDataItems = $this->$sMethod($oAlert, $aHandler);
                 foreach($aDataItems as $aDataItem)
@@ -156,30 +153,30 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
 
     	return array(
     	    array(
-    			'owner_id' => $oAlert->aExtras['initiator'],
-    			'type' => $oAlert->sUnit,
-    			'action' => $oAlert->sAction,
-    			'object_id' => $oAlert->aExtras['content'],
-    			'object_owner_id' => $oAlert->aExtras['content'],
-    			'object_privacy_view' => $iObjectPrivacyView,
-    			'subobject_id' => 0,
-    			'content' => '',
-        		'allow_view_event_to' => $iPrivacyView,
-    			'processed' => 0
+                'owner_id' => $oAlert->aExtras['initiator'],
+                'type' => $oAlert->sUnit,
+                'action' => $oAlert->sAction,
+                'object_id' => $oAlert->aExtras['content'],
+                'object_owner_id' => $oAlert->aExtras['content'],
+                'object_privacy_view' => $iObjectPrivacyView,
+                'subobject_id' => 0,
+                'content' => '',
+                'allow_view_event_to' => $iPrivacyView,
+                'processed' => 0
     	    ),
     	    array(
-    			'owner_id' => $oAlert->aExtras['content'],
-    			'type' => $oAlert->sUnit,
-    			'action' => $oAlert->sAction,
-    			'object_id' => $oAlert->aExtras['initiator'],
-    			'object_owner_id' => $oAlert->aExtras['initiator'],
-    			'object_privacy_view' => $iObjectPrivacyView,
-    			'subobject_id' => 0,
-    			'content' => '',
-        		'allow_view_event_to' => $iPrivacyView,
-    			'processed' => 0
+                'owner_id' => $oAlert->aExtras['content'],
+                'type' => $oAlert->sUnit,
+                'action' => $oAlert->sAction,
+                'object_id' => $oAlert->aExtras['initiator'],
+                'object_owner_id' => $oAlert->aExtras['initiator'],
+                'object_privacy_view' => $iObjectPrivacyView,
+                'subobject_id' => 0,
+                'content' => '',
+                'allow_view_event_to' => $iPrivacyView,
+                'processed' => 0
     	    ),
-		);
+        );
     }
     
     protected function getDeleteDataSysProfilesFriendsConnectionRemoved(&$oAlert, &$aHandler)
@@ -210,18 +207,18 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
     {
     	return array(
     	    array(
-    			'owner_id' => $oAlert->aExtras['initiator'],
-    			'type' => $oAlert->sUnit,
-    			'action' => $oAlert->sAction,
-    			'object_id' => $oAlert->aExtras['content'],
-    			'object_owner_id' => $oAlert->aExtras['content'],
-    			'object_privacy_view' => $this->_getObjectPrivacyView($oAlert->aExtras),
-    			'subobject_id' => 0,
-    			'content' => '',
-        		'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
-    			'processed' => 0
+                'owner_id' => $oAlert->aExtras['initiator'],
+                'type' => $oAlert->sUnit,
+                'action' => $oAlert->sAction,
+                'object_id' => $oAlert->aExtras['content'],
+                'object_owner_id' => $oAlert->aExtras['content'],
+                'object_privacy_view' => $this->_getObjectPrivacyView($oAlert->aExtras),
+                'subobject_id' => 0,
+                'content' => '',
+                'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
+                'processed' => 0
     	    )
-		);
+        );
     }
 
     protected function getDeleteDataSysProfilesSubscriptionsConnectionRemoved(&$oAlert, &$aHandler)
@@ -240,12 +237,31 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
 
     protected function sendNotifications($iId, &$oAlert, &$aHandler)
     {
+        $aDeliveryTypes = array();
+
+        $iSilentMode = $this->_getSilentMode($oAlert->aExtras);
+        switch($iSilentMode) {
+            case BX_NTFS_SLTMODE_ABSOLUTE:
+            case BX_NTFS_SLTMODE_SITE:
+                return;
+
+            case BX_NTFS_SLTMODE_SITE_EMAIL:
+                $aDeliveryTypes[] = BX_BASE_MOD_NTFS_DTYPE_EMAIL;
+                break;
+
+            case BX_NTFS_SLTMODE_SITE_PUSH:
+                $aDeliveryTypes[] = BX_BASE_MOD_NTFS_DTYPE_PUSH;
+                break;
+
+            default:
+                $aDeliveryTypes = array(BX_BASE_MOD_NTFS_DTYPE_EMAIL, BX_BASE_MOD_NTFS_DTYPE_PUSH);
+        }
+
         $aEvent = $this->_oModule->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
         if(empty($aEvent) || !is_array($aEvent))
             return;
 
         $aSendUsing = array();
-        $aDeliveryTypes = array(BX_BASE_MOD_NTFS_DTYPE_EMAIL, BX_BASE_MOD_NTFS_DTYPE_PUSH);
         foreach($aDeliveryTypes as $sDeliveryType) {
             $aHidden = $this->_oModule->_oConfig->getHandlersHidden($sDeliveryType);
             if(in_array($aHandler['id'], $aHidden))
@@ -257,13 +273,13 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
             if(!$this->_oModule->_oTemplate->isMethodExists($sMethodGet) || !method_exists($this, $sMethodSend))
                 continue;
 
-            $mixedContent = $this->_oModule->_oTemplate->$sMethodGet($aEvent);
-            if($mixedContent === false)
+            $mixedNotification = $this->_oModule->_oTemplate->$sMethodGet($aEvent);
+            if($mixedNotification === false)
                 continue;
 
             $aSendUsing[$sDeliveryType] = array(
             	'method' => $sMethodSend,
-                'content' => $mixedContent
+                'notification' => $mixedNotification
             );
         }
 
@@ -315,27 +331,60 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                     if((int)$aSetting['active_adm'] == 0 || (int)$aSetting['active_pnl'] == 0)
                         continue;
 
-                    if($this->{$aDeliveryType['method']}($oProfile, $aDeliveryType['content']) !== false)
+                    if($this->{$aDeliveryType['method']}($oProfile, $aDeliveryType['notification']) !== false)
                         break;
                 }
         }
     }
 
-    protected function sendNotificationEmail($oProfile, $sContent)
+    protected function sendNotificationEmail($oProfile, $aNotification)
     {
-        return sendMailTemplate('bx_notifications_new_event', $oProfile->getAccountId(), $oProfile->id(), array('content' => $sContent), BX_EMAIL_NOTIFY, true);
+        if(!$oProfile)
+            return false;
+
+        $oAccount = $oProfile->getAccountObject();
+        if(!$oAccount)
+            return false;
+
+        $aSettings = &$aNotification['settings'];
+
+        $sTemplate = !empty($aSettings['template']) ? $aSettings['template'] : 'bx_notifications_new_event';
+        $aTemplateMarkers = array('content' => $aNotification['content']);
+        if(!empty($aSettings['markers']) && is_array($aSettings['markers']))
+            $aTemplateMarkers = array_merge($aTemplateMarkers, $aSettings['markers']);              
+
+        $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sTemplate, $aTemplateMarkers, $oAccount->id(), $oProfile->id());
+        if(!$aTemplate)
+            return false;
+
+        $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : $aTemplate['Subject'];
+        return sendMail($oAccount->getEmail(), $sSubject, $aTemplate['Body'], 0, array(), BX_EMAIL_NOTIFY, 'html', false, array(), true);
     }
 
-    protected function sendNotificationPush($oProfile, $aContent)
+    protected function sendNotificationPush($oProfile, $aNotification)
     {
-        $sLanguage = BxDolStudioLanguagesUtils::getInstance()->getCurrentLangName(false);
+        $oAccount = $oProfile->getAccountObject();
+        if(!$oAccount)
+            return false;
+
+        $oLanguage = BxDolLanguages::getInstance();
+
+        $iLanguage = $oAccount->getLanguageId();
+        if(empty($iLanguage))
+            $iLanguage = $oLanguage->getCurrentLangId();
+
+        $sLanguage = $oLanguage->getLangName($iLanguage);
+
+        $aContent = &$aNotification['content'];
+        $aSettings = &$aNotification['settings'];
 
+        $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : _t('_bx_ntfs_push_new_event_subject', getParam('site_title'));
         return BxDolPush::getInstance()->send($oProfile->id(), array(
             'contents' => array(
                 $sLanguage => $aContent['message']
             ),
             'headings' => array(
-                $sLanguage => _t('_bx_ntfs_push_new_event_subject', getParam('site_title'))
+                $sLanguage => $sSubject
             ),
             'url' => $aContent['url'],
             'icon' => $aContent['icon']
@@ -354,6 +403,14 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
         return 0;
     }
 
+    protected function _getSilentMode($aExtras)
+    {
+        if(isset($aExtras['silent_mode']))
+            return (int)$aExtras['silent_mode'];
+
+        return BX_NTFS_SLTMODE_DISABLED;
+    }
+
     protected function _addRecipient($iUser, $sSettingType, &$aRecipients)
     {
         if(!isset($aRecipients[$iUser]))

+ 18 - 10
modules/boonex/notifications/classes/BxNtfsTemplate.php

@@ -198,12 +198,16 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
         if(empty($sEvent) || empty($aEvent['content_parsed']))
             return false;
 
-        return $this->parseHtmlByName('et_new_event.html', array(
-        	'icon_url' => !empty($aEvent['content']['owner_icon']) ? $aEvent['content']['owner_icon'] : $this->getIconUrl('std-icon.svg'),
-            'content_url' => $this->_getContentLink($aEvent),
-            'content' => $aEvent['content_parsed'],
-            'date' => bx_process_output($aEvent['date'], BX_DATA_DATETIME_TS),
-        ));
+        $aContent = &$aEvent['content'];
+        return array(
+            'content' => $this->parseHtmlByName('et_new_event.html', array(
+                'icon_url' => !empty($aContent['owner_icon']) ? $aContent['owner_icon'] : $this->getIconUrl('std-icon.svg'),
+                'content_url' => $this->_getContentLink($aEvent),
+                'content' => $aEvent['content_parsed'],
+                'date' => bx_process_output($aEvent['date'], BX_DATA_DATETIME_TS),
+            )),
+            'settings' => !empty($aContent['settings']['email']) ? $aContent['settings']['email'] : array()
+        );
     }
 
     public function getNotificationPush(&$aEvent)
@@ -213,16 +217,20 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
             return false;
 
         $sMessage = preg_replace('/<\/?[a-zA-Z0-9=\'":;\(\)\s_-]+>/i', '"', $aEvent['content_parsed']);
-		if($sMessage)
+        if($sMessage)
             $sMessage = BxTemplFunctions::getInstance()->getStringWithLimitedLength(html_entity_decode($sMessage), $this->_oConfig->getPushMaxLen());
 
         if(empty($sMessage))
             return false;
 
+        $aContent = &$aEvent['content'];
         return array(
-            'url' => $this->_getContentLink($aEvent),
-            'message' => $sMessage,
-            'icon' => !empty($aEvent['content']['owner_icon']) ? $aEvent['content']['owner_icon'] : ''
+            'content' => array(
+                'url' => $this->_getContentLink($aEvent),
+                'message' => $sMessage,
+                'icon' => !empty($aContent['owner_icon']) ? $aContent['owner_icon'] : ''
+            ),
+            'settings' => !empty($aContent['settings']['push']) ? $aContent['settings']['push'] : array()
         );
     }