BxMassMailerGridCampaigns.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup MassMailer Mass Mailer
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxMassMailerGridCampaigns extends BxTemplGrid
  12. {
  13. protected $MODULE;
  14. protected $_oModule;
  15. public function __construct ($aOptions, $oTemplate = false)
  16. {
  17. $this->MODULE = 'bx_massmailer';
  18. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  19. parent::__construct ($aOptions, $oTemplate);
  20. $this->_sDefaultSortingOrder = 'DESC';
  21. }
  22. public function getFormBlockTitleAPI($sAction, $iId = 0)
  23. {
  24. $sResult = '';
  25. switch($sAction) {
  26. case 'send_test':
  27. $sResult = _t('_bx_massmailer_campaign_form_send_test_title');
  28. break;
  29. case 'send_all':
  30. $sResult = _t('_bx_massmailer_campaign_form_send_all_title');
  31. break;
  32. }
  33. return $sResult;
  34. }
  35. public function getFormCallBackUrlAPI($sAction, $iId = 0)
  36. {
  37. return '/api.php?r=system/perfom_action_api/TemplServiceGrid/&params[]=&o=' . $this->_sObject . '&a=' . $sAction . '&context_pid=' . $this->_iContextPid . '&id=' . $iId;
  38. }
  39. public function performActionDelete()
  40. {
  41. $aIds = bx_get('ids');
  42. foreach ($aIds as $iId){
  43. $this->_oModule->_oDb->deleteCampaignData($iId);
  44. }
  45. parent::performActionDelete();
  46. }
  47. public function performActionSendTest()
  48. {
  49. $sAction = 'send_test';
  50. $aIds = bx_get('ids');
  51. $iId = $aIds[0];
  52. $oForm = BxDolForm::getObjectInstance('bx_massmailer', 'bx_massmailer_campaign_send_test');
  53. if (!$oForm)
  54. return $this->_getActionResult([]);
  55. $oForm->setId('bx_massmailer_campaign_send_test');
  56. $oForm->setAction(BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('_r')));
  57. $aContentInfo = array('email' => BxDolProfile::getInstance()->getAccountObject()->getEmail());
  58. $oForm->initChecker($aContentInfo, array());
  59. if($oForm->isSubmittedAndValid()) {
  60. $mixedResult = $this->_oModule->sendTest($oForm->aInputs['email']['value'], $iId);
  61. if($mixedResult !== false)
  62. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $mixedResult]);
  63. else
  64. return ($sMsg = _t('_bx_massmailer_txt_send_test_error')) && $this->_bIsApi ? [bx_api_get_msg($sMsg)] : echoJson(['msg' => $sMsg]);
  65. }
  66. else {
  67. if($this->_bIsApi)
  68. return $this->getFormBlockAPI($oForm, $sAction, $iTrackId);
  69. $sContent = BxTemplFunctions::getInstance()->popupBox('bx_massmailer_campaign_send_test', _t('_bx_massmailer_campaign_form_send_test_title'), $this->_oModule->_oTemplate->parseHtmlByName('manage_item.html', array(
  70. 'form_id' => $oForm->id,
  71. 'form' => $oForm->getCode(true),
  72. 'object' => $this->_sObject,
  73. 'action' => $sAction
  74. )));
  75. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  76. }
  77. }
  78. public function performActionSendAll()
  79. {
  80. $sAction = 'send_all';
  81. $aIds = bx_get('ids');
  82. $iId = $aIds[0];
  83. $oForm = BxDolForm::getObjectInstance('bx_massmailer', 'bx_massmailer_campaign_send_all');
  84. if (!$oForm)
  85. return $this->_getActionResult([]);
  86. $oForm->setId('bx_massmailer_campaign_send_all');
  87. $oForm->setAction(BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('_r')));
  88. $oForm->initChecker(array(), array());
  89. $oForm->aInputs['campaign_info']['value'] = _t($oForm->aInputs['campaign_info']['value'], $this->_oModule->getEmailCountInSegment($iId));
  90. if($oForm->isSubmittedAndValid()) {
  91. $mixedResult = $this->_oModule->sendAll($iId);
  92. if($mixedResult !== false)
  93. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $mixedResult]);
  94. else
  95. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  96. }
  97. else {
  98. if($this->_bIsApi)
  99. return $this->getFormBlockAPI($oForm, $sAction, $iTrackId);
  100. $sContent = BxTemplFunctions::getInstance()->popupBox('bx_massmailer_campaign_send_all', _t('_bx_massmailer_campaign_form_send_all_title'), $this->_oModule->_oTemplate->parseHtmlByName('manage_item.html', array(
  101. 'form_id' => $oForm->id,
  102. 'form' => $oForm->getCode(true),
  103. 'object' => $this->_sObject,
  104. 'action' => $sAction
  105. )));
  106. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  107. }
  108. }
  109. public function performActionCopy()
  110. {
  111. $aIds = bx_get('ids');
  112. $iId = $aIds[0];
  113. $mixedResult = $this->_oModule->_oDb->copyCampaign($iId);
  114. if($mixedResult !== false)
  115. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $mixedResult]);
  116. else
  117. return $this->_bIsApi ? [bx_api_get_msg($mixedResult)] : echoJson(['msg' => $mixedResult]);
  118. }
  119. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  120. {
  121. if($this->_bIsApi)
  122. return ['type' => 'time', 'data' => $mixedValue];
  123. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  124. }
  125. protected function _getCellSegments($mixedValue, $sKey, $aField, $aRow)
  126. {
  127. return parent::_getCellDefault($this->_oModule->getSegments($mixedValue), $sKey, $aField, $aRow);
  128. }
  129. protected function _getCellDateSent($mixedValue, $sKey, $aField, $aRow)
  130. {
  131. if($this->_bIsApi)
  132. return ['type' => 'time', 'data' => $mixedValue];
  133. return parent::_getCellDefault($mixedValue != '0' ? bx_time_js($mixedValue) : _t('_bx_massmailer_txt_never_sent'), $sKey, $aField, $aRow);
  134. }
  135. protected function _getCellIsOnePerAccount($mixedValue, $sKey, $aField, $aRow)
  136. {
  137. return parent::_getCellDefault(_t('_bx_massmailer_grid_column_title_adm_is_one_per_account_' . ($mixedValue == '1' ? 'yes' : 'no')), $sKey, $aField, $aRow);
  138. }
  139. protected function _getActionCopy($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  140. {
  141. if(isset($aRow["date_sent"]) && $aRow["date_sent"] == '0')
  142. return $this->_bIsApi ? [] : '';
  143. if($this->_bIsApi)
  144. return array_merge($a, [
  145. 'name' => $sKey,
  146. 'type' => 'callback',
  147. 'on_callback' => 'refresh'
  148. ]);
  149. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  150. }
  151. protected function _getActionSendTest($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  152. {
  153. if(isset($aRow["date_sent"]) && $aRow["date_sent"] != '0')
  154. return $this->_bIsApi ? [] : '';
  155. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  156. }
  157. protected function _getActionSendAll($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  158. {
  159. if(isset($aRow["date_sent"]) && $aRow["date_sent"] != '0')
  160. return $this->_bIsApi ? [] : '';
  161. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  162. }
  163. protected function _getActionAdd ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  164. {
  165. $CNF = &$this->_oModule->_oConfig->CNF;
  166. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_ADD_CAMPAIGN']));
  167. if($this->_bIsApi)
  168. return array_merge($a, [
  169. 'name' => $sKey,
  170. 'type' => 'modal',
  171. 'on_callback' => 'refresh',
  172. 'link' => bx_api_get_relative_url($sUrl)
  173. ]);
  174. unset($a['attr']['bx_grid_action_independent']);
  175. $a['attr'] = array_merge($a['attr'], array(
  176. "onclick" => "window.open('" . $sUrl . "','_self');"
  177. ));
  178. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  179. }
  180. protected function _getActionEdit ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  181. {
  182. if(isset($aRow["date_sent"]) && $aRow["date_sent"] != '0')
  183. return $this->_bIsApi ? [] : '';
  184. $CNF = &$this->_oModule->_oConfig->CNF;
  185. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_EDIT_CAMPAIGN'] . '&id=' . $aRow[$CNF['FIELD_ID']]));
  186. if($this->_bIsApi)
  187. return array_merge($a, [
  188. 'name' => $sKey,
  189. 'type' => 'modal',
  190. 'on_callback' => 'refresh',
  191. 'link' => bx_api_get_relative_url($sUrl)
  192. ]);
  193. $a['attr'] = array_merge($a['attr'], array(
  194. "onclick" => "window.open('" . $sUrl . "','_self');"
  195. ));
  196. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  197. }
  198. protected function _getActionView ($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  199. {
  200. if(isset($aRow["date_sent"]) && $aRow["date_sent"] == '0')
  201. return $this->_bIsApi ? [] : '';
  202. $CNF = &$this->_oModule->_oConfig->CNF;
  203. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_CAMPAIGN'] . '&id=' . $aRow[$CNF['FIELD_ID']]));
  204. if($this->_bIsApi)
  205. return array_merge($a, [
  206. 'name' => $sKey,
  207. 'type' => 'callback',
  208. 'on_callback' => 'redirect',
  209. 'redirect_url' => bx_api_get_relative_url($sUrl)
  210. ]);
  211. unset($a['attr']['bx_grid_action_independent']);
  212. $a['attr'] = array_merge($a['attr'], array(
  213. "onclick" => "window.open('" . $sUrl . "','_self');"
  214. ));
  215. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  216. }
  217. protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow)
  218. {
  219. $oProfile = BxDolProfile::getInstance($aRow['author']);
  220. if (!$oProfile)
  221. $oProfile = BxDolProfileUndefined::getInstance();
  222. return parent::_getCellDefault($oProfile->getDisplayName(), $sKey, $aField, $aRow);
  223. }
  224. protected function _isVisibleGrid ($a)
  225. {
  226. if (isAdmin() || !isset($a['visible_for_levels']))
  227. return true;
  228. return $this->_oModule->checkAllowed() === CHECK_ACTION_RESULT_ALLOWED ? true : false;
  229. }
  230. }
  231. /** @} */