BxSitesGridOverview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 Sites Sites
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  12. class BxSitesGridOverview extends BxTemplGrid
  13. {
  14. protected $_oModule;
  15. protected $_aAccount;
  16. public function __construct ($aOptions, $oTemplate = false)
  17. {
  18. parent::__construct ($aOptions, $oTemplate);
  19. $this->_oModule = BxDolModule::getInstance('bx_sites');
  20. }
  21. public function setAccount(&$aAccount)
  22. {
  23. $this->_aAccount = $aAccount;
  24. $this->_aQueryAppend['account'] = $aAccount['id'];
  25. }
  26. public function performActionUnconfirmed()
  27. {
  28. $sAction = 'unconfirmed';
  29. $oForm = BxDolForm::getObjectInstance('bx_sites', 'bx_sites_site_confirm');
  30. if(!$oForm) {
  31. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  32. return;
  33. }
  34. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&_r=' . time();
  35. $oForm->aParams['db']['submit_name'] = 'do_confirm';
  36. $oPermalinks = BxDolPermalinks::getInstance();
  37. $sUrl = BX_DOL_URL_ROOT . $oPermalinks->permalink('page.php?i=site-delete&id=' . (int)bx_get('account'));
  38. foreach($oForm->aInputs['confirm_block'] as $iKey => $aInput)
  39. if(is_int($iKey) && !empty($aInput) && $aInput['name'] == 'do_delete')
  40. $oForm->aInputs['confirm_block'][$iKey]['attrs']['onclick'] = 'javascript:window.open(\'' . $sUrl . '\',\'_self\');';
  41. $aAccount = $this->_performAction($oForm, $sAction);
  42. if(!empty($aAccount) && is_array($aAccount))
  43. $this->_startSubscription($aAccount);
  44. }
  45. public function performActionPending()
  46. {
  47. $sAction = 'pending';
  48. $oForm = BxDolForm::getObjectInstance('bx_sites', 'bx_sites_site_pending');
  49. if(!$oForm) {
  50. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  51. return;
  52. }
  53. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&_r=' . time();
  54. $this->_performAction($oForm, $sAction, false);
  55. //Note: Place onSubmit code here if it's needed.
  56. }
  57. public function performActionActive()
  58. {
  59. $sAction = 'active';
  60. $oForm = BxDolForm::getObjectInstance('bx_sites', 'bx_sites_site_cancel');
  61. if(!$oForm) {
  62. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  63. return;
  64. }
  65. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&_r=' . time();
  66. $oForm->aFormAttrs['onsubmit'] = 'return ' . $this->_oModule->_oConfig->getJsObject() . '.onCancelSubscription(this);';
  67. $oForm->aParams['db']['submit_name'] = 'do_cancel';
  68. $aAccount = $this->_performAction($oForm, $sAction);
  69. if(empty($aAccount) || !is_array($aAccount))
  70. return;
  71. $bResult = $this->_oModule->cancelSubscription($aAccount['pd_profile_id']);
  72. if(!$bResult) {
  73. echoJson(array('msg' => _t('_bx_sites_txt_err_cannot_perform')));
  74. return;
  75. }
  76. $oPermalinks = BxDolPermalinks::getInstance();
  77. $sUrl = BX_DOL_URL_ROOT . $oPermalinks->permalink('page.php?i=site-view&id=' . $aAccount['id']);
  78. echoJson(array('eval' => 'window.open(\'' . $sUrl . '\',\'_self\');'));
  79. }
  80. public function performActionCanceled()
  81. {
  82. $sAction = 'canceled';
  83. $oForm = BxDolForm::getObjectInstance('bx_sites', 'bx_sites_site_reactivate');
  84. if(!$oForm) {
  85. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  86. return;
  87. }
  88. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&_r=' . time();
  89. $oForm->aParams['db']['submit_name'] = 'do_reactivate';
  90. $aAccount = $this->_performAction($oForm, $sAction);
  91. if(!empty($aAccount) && is_array($aAccount))
  92. $this->_startSubscription($aAccount);
  93. }
  94. public function performActionSuspended()
  95. {
  96. $sAction = 'suspended';
  97. $oForm = BxDolForm::getObjectInstance('bx_sites', 'bx_sites_site_suspended');
  98. if(!$oForm) {
  99. echoJson(array('msg' => _t('_sys_txt_error_occured')));
  100. return;
  101. }
  102. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&_r=' . time();
  103. $this->_performAction($oForm, $sAction, false);
  104. //Note: Place onSubmit code here if it's needed.
  105. }
  106. public function getCode($isDisplayHeader = true)
  107. {
  108. if(in_array($this->_aAccount['status'], array(BX_SITES_ACCOUNT_STATUS_UNCONFIRMED, BX_SITES_ACCOUNT_STATUS_PENDING, BX_SITES_ACCOUNT_STATUS_CANCELED, BX_SITES_ACCOUNT_STATUS_SUSPENDED)))
  109. $this->_oTemplate = $this->_oModule->_oTemplate;
  110. return parent::getCode($isDisplayHeader);
  111. }
  112. protected function _addJsCss()
  113. {
  114. parent::_addJsCss();
  115. $this->_oTemplate->addJs(array('jquery.form.min.js'));
  116. $this->_oTemplate->addJsTranslation(array('_bx_sites_form_site_input_do_cancel_confirm'));
  117. $oForm = new BxTemplFormView(array());
  118. $oForm->addCssJs();
  119. }
  120. protected function _getDataArray($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  121. {
  122. $this->_aOptions['source'] = array();
  123. $sCurrency = $this->_oModule->_oConfig->getCurrencyCode();
  124. $iNextPayment = $this->_aAccount['paid'];
  125. $aPayment = $this->_oModule->_oDb->getPaymentHistory(array('type' => 'account_id_last', 'value' => $this->_aAccount['id']));
  126. if(!empty($aPayment) && is_array($aPayment))
  127. $this->_aOptions['source'][] = array(
  128. 'id' => 'last',
  129. 'title' => '_bx_sites_grid_overview_txt_payment_last',
  130. 'type' => '_bx_sites_txt_payment_type_' . $aPayment['type'],
  131. 'transaction' => $aPayment['transaction'],
  132. 'when' => bx_time_js($aPayment['when']),
  133. 'amount' => number_format($aPayment['amount'], 2) . ' ' .$sCurrency
  134. );
  135. if(!empty($iNextPayment)) {
  136. $aAmount = 0;
  137. $fAmountTrial = $this->_oModule->_oConfig->getPaymentPrice(BX_SITES_PP_PERIOD_TRIAL);
  138. $fAmountRegular = $this->_oModule->_oConfig->getPaymentPrice(BX_SITES_PP_PERIOD_REGULAR);
  139. switch($this->_aAccount['status']) {
  140. case BX_SITES_ACCOUNT_STATUS_PENDING:
  141. $aAmount = $fAmountTrial != 0 ? $fAmountTrial : $fAmountRegular;
  142. break;
  143. case BX_SITES_ACCOUNT_STATUS_TRIAL:
  144. $aAmount = $fAmountRegular;
  145. break;
  146. case BX_SITES_ACCOUNT_STATUS_ACTIVE:
  147. $aAmount = $fAmountRegular;
  148. break;
  149. }
  150. $this->_aOptions['source'][] = array(
  151. 'id' => 'next',
  152. 'title' => '_bx_sites_grid_overview_txt_payment_next',
  153. 'type' => '',
  154. 'transaction' => '',
  155. 'when' => bx_time_js($iNextPayment),
  156. 'amount' => number_format($aAmount, 2) . ' ' .$sCurrency
  157. );
  158. }
  159. return parent::_getDataArray($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  160. }
  161. protected function _getFilterControls ()
  162. {
  163. $sAction = $this->_aAccount['status'];
  164. if($sAction == BX_SITES_ACCOUNT_STATUS_TRIAL)
  165. $sAction = BX_SITES_ACCOUNT_STATUS_ACTIVE;
  166. $sStatus = _t('_bx_sites_txt_status_' . $this->_aAccount['status']);
  167. if(in_array($sAction, array(BX_SITES_ACCOUNT_STATUS_UNCONFIRMED, BX_SITES_ACCOUNT_STATUS_PENDING, BX_SITES_ACCOUNT_STATUS_ACTIVE, BX_SITES_ACCOUNT_STATUS_CANCELED, BX_SITES_ACCOUNT_STATUS_SUSPENDED))) {
  168. $sStatus = $this->_oModule->_oTemplate->parseHtmlByName('bx_a.html', array(
  169. 'href' => 'javascript:void(0)',
  170. 'title' => _t('_bx_sites_grid_overview_btn_' . $sAction),
  171. 'bx_repeat:attrs' => array(
  172. array('key' => 'bx_grid_action_independent', 'value' => $sAction),
  173. array('key' => 'bx_grid_action_data', 'value' => $this->_aAccount['id'])
  174. ),
  175. 'content' => $sStatus,
  176. ));
  177. }
  178. return $this->_oModule->_oTemplate->parseHtmlByName('block_overview_status.html', array(
  179. 'status' => $sStatus,
  180. 'bx_if:show_reactivate' => array(
  181. 'condition' => false,
  182. 'content' => array()
  183. )
  184. ));
  185. }
  186. protected function _getActionUnconfirmed($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  187. {
  188. return '';
  189. }
  190. protected function _getActionPending($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  191. {
  192. return '';
  193. }
  194. protected function _getActionActive($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  195. {
  196. return '';
  197. }
  198. protected function _getActionCanceled($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  199. {
  200. return '';
  201. }
  202. protected function _getActionSuspended($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  203. {
  204. return '';
  205. }
  206. protected function _performAction(&$oForm, $sAction, $bReturnAccount = true)
  207. {
  208. $iAccountId = (int)bx_get('account');
  209. if($iAccountId !== false)
  210. $oForm->aInputs['id']['value'] = $iAccountId;
  211. $oForm->aInputs['info']['content'] = _t('_bx_sites_form_site_input_info_' . $sAction);
  212. $oForm->initChecker();
  213. if(!$oForm->isSubmittedAndValid()) {
  214. $sContent = BxTemplFunctions::getInstance()->popupBox('bx-sites-site-' . $sAction . '-popup', _t('_bx_sites_grid_overview_popup_' . $sAction), $this->_oModule->_oTemplate->parseHtmlByName('block_' . $sAction . '.html', array(
  215. 'form_id' => $oForm->aFormAttrs['id'],
  216. 'form' => $oForm->getCode(true),
  217. 'object' => $this->_sObject,
  218. 'action' => $sAction
  219. )));
  220. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  221. return;
  222. }
  223. if(!$bReturnAccount)
  224. return;
  225. $iAccount = $oForm->getCleanValue('id');
  226. $aAccount = $this->_oModule->_oDb->getAccount(array('type' => 'id', 'value' => $iAccount));
  227. if(empty($aAccount) || !is_array($aAccount)) {
  228. echoJson(array('msg' => _t('_bx_sites_txt_err_site_is_not_defined')));
  229. return;
  230. }
  231. return $aAccount;
  232. }
  233. protected function _startSubscription($aAccount)
  234. {
  235. $sUrl = $this->_oModule->startSubscription($aAccount['id']);
  236. if(empty($sUrl)) {
  237. echoJson(array('msg' => _t('_bx_sites_txt_err_cannot_perform')));
  238. return;
  239. }
  240. echoJson(array('eval' => 'window.open(\'' . $sUrl . '\', \'_self\');', 'popup_not_hide' => 1));
  241. }
  242. }
  243. /** @} */