Przeglądaj źródła

Ticket #5604 - API: Add full support for Mass Mailer module.

AntonLV 2 miesięcy temu
rodzic
commit
73495c350e

+ 9 - 3
modules/boonex/massmailer/classes/BxMassMailerDb.php

@@ -151,11 +151,17 @@ class BxMassMailerDb extends BxBaseModGeneralDb
         $this->query("DELETE FROM `" . $CNF['TABLE_UNSUBSCRIBE'] . "` WHERE `" . $CNF['FIELD_CAMPAIGN_ID'] . "` = :campaign_id", $aBindings);
     }
     
-    public function getLettersByCampaignId ($iCampaignId)
+    public function getLettersByCampaignId ($iCampaignId, $iStart = false, $iPerPage = false)
     {
         $CNF = &$this->_oConfig->CNF;
-        $sQuery = $this->prepare ("SELECT * FROM `" . $CNF['TABLE_LETTERS'] . "` WHERE `" . $CNF['FIELD_CAMPAIGN_ID'] . "` = ?", $iCampaignId);
-        return $this->getAll($sQuery);
+
+        $sLimitClause = "";
+        if($iStart !== false && $iPerPage !== false)
+            $sLimitClause = " LIMIT " . (int)$iStart . ", " . (int)$iPerPage;
+
+        return $this->getAll("SELECT * FROM `" . $CNF['TABLE_LETTERS'] . "` WHERE `" . $CNF['FIELD_CAMPAIGN_ID'] . "` = :id" . $sLimitClause, [
+            'id' => $iCampaignId
+        ]);
     }
     
     public function getClicksByCampaignId ($iCampaignId)

+ 73 - 0
modules/boonex/massmailer/classes/BxMassMailerGridLetters.php

@@ -0,0 +1,73 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+* Copyright (c) UNA, Inc - https://una.io
+* MIT License - https://opensource.org/licenses/MIT
+*
+* @defgroup    MassMailer Mass Mailer
+* @ingroup     UnaModules
+* 
+* @{
+*/
+
+class BxMassMailerGridLetters extends BxTemplGrid
+{
+    protected $_sModule;
+    protected $_oModule;
+
+    protected $_iContentId;
+
+    public function __construct ($aOptions, $oTemplate = false)
+    {
+        $this->_sModule = 'bx_massmailer';
+        $this->_oModule = BxDolModule::getInstance($this->_sModule);
+
+        parent::__construct ($aOptions, $oTemplate);
+
+        $this->_iContentId = 0;
+        if(($iContentId = bx_get('content_id')) !== false) 
+            $this->setContentId($iContentId);
+    }
+
+    public function setContentId($iContentId)
+    {
+        $this->_iContentId = (int)$iContentId;
+        $this->_aQueryAppend['content_id'] = $this->_iContentId;
+    }
+
+    public function getFormCallBackUrlAPI($sAction, $iId = 0)
+    {
+         return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&content_id=' . $this->_iContentId . '&id=' . $iId;
+    }
+
+    protected function _getCellDateSent($mixedValue, $sKey, $aField, $aRow)
+    {
+        return $this->_getCellDate($mixedValue, $sKey, $aField, $aRow);
+    }
+
+    protected function _getCellDateSeen($mixedValue, $sKey, $aField, $aRow)
+    {
+        return $this->_getCellDate($mixedValue, $sKey, $aField, $aRow);
+    }
+
+    protected function _getCellDateClick($mixedValue, $sKey, $aField, $aRow)
+    {
+        return $this->_getCellDate($mixedValue, $sKey, $aField, $aRow);
+    }
+
+    protected function _getCellDate($mixedValue, $sKey, $aField, $aRow)
+    {
+        if($this->_bIsApi)
+            return ['type' => 'time', 'data' => $mixedValue];
+
+        return parent::_getCellDefault($mixedValue != '0' ? bx_time_js($mixedValue, BX_FORMAT_DATE, true) : _t('_bx_massmailer_txt_never_sent'), $sKey, $aField, $aRow);
+    }
+
+    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+    {
+        $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `campaign_id`=?", $this->_iContentId);
+
+        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
+    }
+}
+
+/** @} */

+ 38 - 13
modules/boonex/massmailer/classes/BxMassMailerModule.php

@@ -225,7 +225,9 @@ class BxMassMailerModule extends BxBaseModGeneralModule
 
     public function serviceGetSafeServices()
     {
-        return parent::serviceGetSafeServices();
+        return array_merge(parent::serviceGetSafeServices(), [
+            'CampagnSubscribers' => ''
+        ]);
     }
 
     /**
@@ -246,18 +248,30 @@ class BxMassMailerModule extends BxBaseModGeneralModule
     /** 
     * @ref bx_massmailer-campagn_subscribers "entity_view"
     */
-    public function serviceCampagnSubscribers ($iContentId = 0, $sDisplay = false)
+    public function serviceCampagnSubscribers ($iContentId = 0, $iStart = 0, $iPerPage = 20)
     {
         if(!$iContentId)
             $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
 
         if(!$iContentId)
             return false;
-              
-        $aData = $this->_oDb->getLettersByCampaignId($iContentId);
-        if (count($aData) > 0)
-            return $this->_oTemplate->getSubscribers($aData);
-        return $iContentId;
+        
+        $CNF = &$this->_oConfig->CNF;
+
+        $sGrid = $CNF['OBJECT_GRID_LETTERS'];
+        $oGrid = BxDolGrid::getObjectInstance($sGrid);
+        if(!$oGrid)
+            return $this->_bIsApi ? [] : '';
+
+        if($iContentId)
+            $oGrid->setContentId($iContentId);
+
+        if($this->_bIsApi)
+            return [
+                bx_api_get_block('grid', $oGrid->getCodeAPI())
+            ];
+
+        return $oGrid->getCode();
     }
     
     /**
@@ -278,7 +292,7 @@ class BxMassMailerModule extends BxBaseModGeneralModule
     /** 
      * @ref bx_massmailer-campagn_info "campagn_info"
      */
-    public function serviceCampagnInfo ($iContentId = 0, $sDisplay = false)
+    public function serviceCampagnInfo ($iContentId = 0)
     {
         if(!$iContentId)
             $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
@@ -286,7 +300,12 @@ class BxMassMailerModule extends BxBaseModGeneralModule
         if(!$iContentId)
             return false;
         
-        return $this->_oTemplate->getInfo($iContentId);
+        $mixedResult = $this->_oTemplate->getInfo($iContentId);
+
+        if($this->_bIsApi)
+            return [bx_api_get_block('simple_list',  $mixedResult)];
+
+        return $mixedResult;
     }
     
     /**
@@ -307,7 +326,7 @@ class BxMassMailerModule extends BxBaseModGeneralModule
     /** 
      * @ref bx_massmailer-campagn_links "campagn_links"
      */
-    public function serviceCampagnLinks ($iContentId = 0, $sDisplay = false)
+    public function serviceCampagnLinks ($iContentId = 0)
     {
         if(!$iContentId)
             $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
@@ -316,9 +335,15 @@ class BxMassMailerModule extends BxBaseModGeneralModule
             return false;
         
         $aData = $this->_oDb->getClicksByCampaignId($iContentId);
-        if (count($aData) > 0)
-            return $this->_oTemplate->getClicks($aData);
-        return $iContentId;
+        if(!$aData || !is_array($aData))
+            return false;
+
+        $mixedResult = $this->_oTemplate->getClicks($aData);
+
+        if($this->_bIsApi)
+            return [bx_api_get_block('table',  $mixedResult)];
+
+        return $mixedResult;
     }
     
     /**

+ 49 - 12
modules/boonex/massmailer/classes/BxMassMailerTemplate.php

@@ -18,11 +18,18 @@ class BxMassMailerTemplate extends BxBaseModGeneralTemplate
     
     function getSubscribers($aData)
     {
-        $aList = array();
-        foreach($aData as $aItem){
-            $aList[] = array('email' => $aItem['email'], 'date_sent' => $aItem['date_sent'] > 0 ? bx_time_js($aItem['date_sent']) : '', 'date_seen' => $aItem['date_seen'] > 0 ? bx_time_js($aItem['date_seen']) : '', 'date_click' => $aItem['date_click'] > 0 ? bx_time_js($aItem['date_click']) : '');
-        }
-        
+        $aList = [];
+        foreach($aData as $aItem)
+            $aList[] = [
+                'email' => $aItem['email'], 
+                'date_sent' => $this->_bIsApi ? $aItem['date_sent'] : ($aItem['date_sent'] > 0 ? bx_time_js($aItem['date_sent']) : ''), 
+                'date_seen' => $this->_bIsApi ? $aItem['date_seen'] : ($aItem['date_seen'] > 0 ? bx_time_js($aItem['date_seen']) : ''), 
+                'date_click' => $this->_bIsApi ? $aItem['date_click'] : ($aItem['date_click'] > 0 ? bx_time_js($aItem['date_click']) : '')
+            ];
+
+        if($this->_bIsApi)
+            return $aList;
+
         $this->addJs(array(BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/datatables/|datatables.min.js'));
         $this->addCss(array('main.css', BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/datatables/|datatables.min.css'));
         return $this->parseHtmlByName('campaign_subscribers.html', array(
@@ -36,13 +43,25 @@ class BxMassMailerTemplate extends BxBaseModGeneralTemplate
     
     function getClicks($aData)
     {
-        $aList = array();
-        foreach($aData as $aItem){
-            $aList[] = array('title' => $aItem['title'], 'link' => $aItem['link'], 'last_click' => $aItem['last_click'] > 0 ? bx_time_js($aItem['last_click']) : '', 'click_count' => $aItem['click_count']);
-        }
-        
-        $this->addJs(array(BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/datatables/|datatables.min.js'));
-        $this->addCss(array('main.css', BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/datatables/|datatables.min.css'));
+        $aList = [];
+        foreach($aData as $aItem)
+            $aList[] = [
+                'title' => $aItem['title'], 
+                'link' => $aItem['link'], 
+                'last_click' => $this->_bIsApi ? $aItem['last_click'] : ($aItem['last_click'] > 0 ? bx_time_js($aItem['last_click']) : ''),
+                'click_count' => $aItem['click_count']
+            ];
+
+        if($this->_bIsApi)
+            return array_merge([[
+                'title' => _t('_bx_massmailer_txt_title_title'),
+                'link' => _t('_bx_massmailer_txt_link_title'),
+                'last_click' => _t('_bx_massmailer_txt_last_click_title'),
+                'click_count' => _t('_bx_massmailer_txt_click_count_title'),
+            ]], $aList);
+
+        $this->addJs([BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/datatables/|datatables.min.js']);
+        $this->addCss(['main.css', BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/datatables/|datatables.min.css']);
         return $this->parseHtmlByName('campaign_clicks.html', array(
              'bx_repeat:items' => $aList,
              'title_title' => _t('_bx_massmailer_txt_title_title'),
@@ -64,6 +83,24 @@ class BxMassMailerTemplate extends BxBaseModGeneralTemplate
         $this->addJs(array('chart.min.js', 'chart.js', BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/daterangepicker/|daterangepicker.js'));
         $this->addCss(array('main.css', BX_DIRECTORY_PATH_MODULES . 'boonex/massmailer/plugins/daterangepicker/|daterangepicker.css'));
         
+        if($this->_bIsApi) {
+            $aItems = [
+                ['title' => '_bx_massmailer_txt_subject_title', 'value' => $aData[$CNF['FIELD_SUBJECT']]],
+                ['title' => '_bx_massmailer_txt_body_title', 'value' => $aData[$CNF['FIELD_BODY']]],
+                ['title' => '_bx_massmailer_txt_from_name_title', 'value' => $aData[$CNF['FIELD_FROM_NAME']]],
+                ['title' => '_bx_massmailer_txt_reply_title', 'value' => $aData[$CNF['FIELD_REPLY_TO']]],
+                ['title' => '_bx_massmailer_txt_created_title', 'value' => $aData[$CNF['FIELD_ADDED']]],
+                ['title' => '_bx_massmailer_txt_sent_to_title', 'value' => $aData[$CNF['FIELD_DATE_SENT']]],
+                ['title' => '_bx_massmailer_txt_sent_to_title', 'value' => $this->getModule()->getSegments($aData[$CNF['FIELD_SEGMENTS']])]
+            ];
+
+            array_walk($aItems, function(&$aItem) {
+                $aItem['title'] = trim(_t($aItem['title']), ":");
+            });
+
+            return $aItems;
+        }
+
         return $this->getJsCode('chart', array('sChartName' => 'CAMPAIGN_REPORT', 'sReportName' => $CampaignId)) . $this->parseHtmlByName('campaign_info.html', array(
             'created_title' => _t('_bx_massmailer_txt_created_title'),
             'subject_title' => _t('_bx_massmailer_txt_subject_title'),

+ 17 - 0
modules/boonex/massmailer/install/sql/enable-app.sql

@@ -0,0 +1,17 @@
+-- PAGES: config_api
+
+-- PAGES: active_api
+UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_massmailer_create_campaign' AND `module`='bx_massmailer' AND `title`='_bx_massmailer_page_block_title_create_campaign';
+UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_massmailer_edit_campaign' AND `module`='bx_massmailer' AND `title`='_bx_massmailer_page_block_title_edit_campaign';
+UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_massmailer_view_campaign' AND `module`='bx_massmailer' AND `title`='_bx_massmailer_page_block_title_view_campaign_info';
+UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_massmailer_view_campaign' AND `module`='bx_massmailer' AND `title`='_bx_massmailer_page_block_title_view_campaign_subscribers';
+UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_massmailer_view_campaign' AND `module`='bx_massmailer' AND `title`='_bx_massmailer_page_block_title_view_campaign_links';
+UPDATE `sys_pages_blocks` SET `active_api`=1 WHERE `object`='bx_massmailer_campaigns' AND `module`='bx_massmailer' AND `title_system`='_bx_massmailer_page_block_title_system_manage_campaigns' AND `title`='_bx_massmailer_page_block_title_manage_campaigns';
+
+-- MENUS:
+
+-- MENUS: config_api
+
+-- MENUS: active_api
+UPDATE `sys_menu_items` SET `active_api`=1 WHERE `set_name`='sys_account_dashboard' AND `module`='bx_massmailer' AND `name`='dashboard-massmailer';
+

+ 10 - 0
modules/boonex/massmailer/install/sql/enable.sql

@@ -92,6 +92,16 @@ INSERT INTO `sys_grid_actions` (`object`, `type`, `name`, `title`, `icon`, `icon
 ('bx_massmailer_campaigns', 'single', 'send_all', '_bx_massmailer_grid_action_title_adm_send_all', 'envelope', 1, 0, 7),
 ('bx_massmailer_campaigns', 'independent', 'add', '_bx_massmailer_grid_action_title_adm_add', '', 0, 0, 1);
 
+-- GRIDS: campaign subscribers
+INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `override_class_name`, `override_class_file`) VALUES
+('bx_massmailer_letters', 'Sql', 'SELECT * FROM `bx_massmailer_letters` WHERE 1 ', 'bx_massmailer_letters', 'id', 'date_sent', '', '', 20, NULL, 'start', '', 'email', '', 'like', 'email,date_sent,date_seen,date_click', '', 2147483647, 'BxMassMailerGridLetters', 'modules/boonex/massmailer/classes/BxMassMailerGridLetters.php');
+
+INSERT INTO `sys_grid_fields` (`object`, `name`, `title`, `width`, `translatable`, `chars_limit`, `params`, `order`) VALUES
+('bx_massmailer_letters', 'email', '_bx_massmailer_txt_title_email', '55%', 0, '48', '', 1),
+('bx_massmailer_letters', 'date_sent', '_bx_massmailer_txt_title_date_sent', '15%', 0, '0', '', 2),
+('bx_massmailer_letters', 'date_seen', '_bx_massmailer_txt_title_date_seen', '15%', 0, '0', '', 3),
+('bx_massmailer_letters', 'date_click', '_bx_massmailer_txt_title_date_click', '15%', 0, '0', '', 4);
+
 -- EMAIL TEMPLATES
 INSERT INTO `sys_email_templates` (`Module`, `NameSystem`, `Name`, `Subject`, `Body`) VALUES
 (@sName, '_bx_massmailer_email_name', 'bx_massmailer_email', '_bx_massmailer_email_subject', '_bx_massmailer_email_body');

+ 0 - 25
modules/boonex/massmailer/template/campaign_subscribers.html

@@ -1,25 +0,0 @@
-<div class="bx_massmailer_table">
-    <table  class="bx_massmailer_table_subscribers">
-        <thead>
-            <tr>
-                <th>__email_title__</th>
-                <th>__date_sent_title__</th>
-                <th>__date_seen_title__</th>
-                <th>__date_click_title__</th>
-            </tr>
-        </thead>
-        <bx_repeat:items>
-            <tr>
-                <td>__email__</td>
-                <td>__date_sent__</td>
-                <td>__date_seen__</td>
-                <td>__date_click__</td>
-            </tr>
-        </bx_repeat:items>
-    </table>
-</div>
-<script language="javascript">
-$(document).ready(function () {
-    $('.bx_massmailer_table table.bx_massmailer_table_subscribers').DataTable({ dom: '<"top"i>rt<"bottom"flp><"clear">', paging: false, searching: false, ordering: true, order: [] });
-});
-</script>