PageRenderTime 72ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminPaymentController.php

https://gitlab.com/staging06/myproject
PHP | 313 lines | 246 code | 37 blank | 30 comment | 49 complexity | 4c064dfcc74b18c2d365050f33f44e09 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class AdminPaymentControllerCore extends AdminController
  27. {
  28. public $payment_modules = array();
  29. public function __construct()
  30. {
  31. $this->bootstrap = true;
  32. parent::__construct();
  33. $shop_id = Context::getContext()->shop->id;
  34. /* Get all modules then select only payment ones */
  35. $modules = Module::getModulesOnDisk(true);
  36. foreach ($modules as $module) {
  37. if ($module->tab == 'payments_gateways') {
  38. if ($module->id) {
  39. if (!get_class($module) == 'SimpleXMLElement') {
  40. $module->country = array();
  41. }
  42. $countries = DB::getInstance()->executeS('
  43. SELECT id_country
  44. FROM '._DB_PREFIX_.'module_country
  45. WHERE id_module = '.(int)$module->id.' AND `id_shop`='.(int)$shop_id
  46. );
  47. foreach ($countries as $country) {
  48. $module->country[] = $country['id_country'];
  49. }
  50. if (!get_class($module) == 'SimpleXMLElement') {
  51. $module->currency = array();
  52. }
  53. $currencies = DB::getInstance()->executeS('
  54. SELECT id_currency
  55. FROM '._DB_PREFIX_.'module_currency
  56. WHERE id_module = '.(int)$module->id.' AND `id_shop`='.(int)$shop_id
  57. );
  58. foreach ($currencies as $currency) {
  59. $module->currency[] = $currency['id_currency'];
  60. }
  61. if (!get_class($module) == 'SimpleXMLElement') {
  62. $module->group = array();
  63. }
  64. $groups = DB::getInstance()->executeS('
  65. SELECT id_group
  66. FROM '._DB_PREFIX_.'module_group
  67. WHERE id_module = '.(int)$module->id.' AND `id_shop`='.(int)$shop_id
  68. );
  69. foreach ($groups as $group) {
  70. $module->group[] = $group['id_group'];
  71. }
  72. } else {
  73. $module->country = null;
  74. $module->currency = null;
  75. $module->group = null;
  76. }
  77. $this->payment_modules[] = $module;
  78. }
  79. }
  80. }
  81. public function initToolbarTitle()
  82. {
  83. $this->toolbar_title = array_unique($this->breadcrumbs);
  84. }
  85. public function initPageHeaderToolbar()
  86. {
  87. parent::initPageHeaderToolbar();
  88. $this->page_header_toolbar_btn = array();
  89. }
  90. public function postProcess()
  91. {
  92. if (Tools::getValue('action') == 'GetModuleQuickView' && Tools::getValue('ajax') == '1') {
  93. $this->ajaxProcessGetModuleQuickView();
  94. }
  95. if ($this->action) {
  96. $this->saveRestrictions($this->action);
  97. }
  98. }
  99. public function initProcess()
  100. {
  101. if ($this->tabAccess['edit'] === '1') {
  102. if (Tools::isSubmit('submitModulecountry')) {
  103. $this->action = 'country';
  104. } elseif (Tools::isSubmit('submitModulecurrency')) {
  105. $this->action = 'currency';
  106. } elseif (Tools::isSubmit('submitModulegroup')) {
  107. $this->action = 'group';
  108. }
  109. } else {
  110. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  111. }
  112. }
  113. protected function saveRestrictions($type)
  114. {
  115. // Delete type restrictions for active module.
  116. $modules = array();
  117. foreach ($this->payment_modules as $module) {
  118. if ($module->active) {
  119. $modules[] = (int)$module->id;
  120. }
  121. }
  122. Db::getInstance()->execute('
  123. DELETE FROM `'._DB_PREFIX_.'module_'.bqSQL($type).'`
  124. WHERE id_shop = '.Context::getContext()->shop->id.'
  125. AND `id_module` IN ('.implode(', ', $modules).')'
  126. );
  127. // Fill the new restriction selection for active module.
  128. $values = array();
  129. foreach ($this->payment_modules as $module) {
  130. if ($module->active && isset($_POST[$module->name.'_'.$type.''])) {
  131. foreach ($_POST[$module->name.'_'.$type.''] as $selected) {
  132. $values[] = '('.(int)$module->id.', '.(int)Context::getContext()->shop->id.', '.(int)$selected.')';
  133. }
  134. }
  135. }
  136. if (count($values)) {
  137. Db::getInstance()->execute('
  138. INSERT INTO `'._DB_PREFIX_.'module_'.bqSQL($type).'`
  139. (`id_module`, `id_shop`, `id_'.bqSQL($type).'`)
  140. VALUES '.implode(',', $values));
  141. }
  142. Tools::redirectAdmin(self::$currentIndex.'&conf=4'.'&token='.$this->token);
  143. }
  144. public function initContent()
  145. {
  146. $this->display = 'view';
  147. return parent::initContent();
  148. }
  149. public function setMedia()
  150. {
  151. parent::setMedia();
  152. $this->addJqueryPlugin('fancybox');
  153. }
  154. public function renderView()
  155. {
  156. $this->toolbar_title = $this->l('Payment');
  157. unset($this->toolbar_btn['back']);
  158. $shop_context = (!Shop::isFeatureActive() || Shop::getContext() == Shop::CONTEXT_SHOP);
  159. if (!$shop_context) {
  160. $this->tpl_view_vars = array('shop_context' => $shop_context);
  161. return parent::renderView();
  162. }
  163. // link to modules page
  164. if (isset($this->payment_modules[0])) {
  165. $token_modules = Tools::getAdminToken('AdminModules'.(int)Tab::getIdFromClassName('AdminModules').(int)$this->context->employee->id);
  166. }
  167. $display_restrictions = false;
  168. foreach ($this->payment_modules as $module) {
  169. if ($module->active) {
  170. $display_restrictions = true;
  171. break;
  172. }
  173. }
  174. $lists = array(
  175. array('items' => Currency::getCurrencies(),
  176. 'title' => $this->l('Currency restrictions'),
  177. 'desc' => $this->l('Please mark each checkbox for the currency, or currencies, in which you want the payment module(s) to be available.'),
  178. 'name_id' => 'currency',
  179. 'identifier' => 'id_currency',
  180. 'icon' => 'icon-money',
  181. ),
  182. array('items' => Group::getGroups($this->context->language->id),
  183. 'title' => $this->l('Group restrictions'),
  184. 'desc' => $this->l('Please mark each checkbox for the customer group(s), in which you want the payment module(s) to be available.'),
  185. 'name_id' => 'group',
  186. 'identifier' => 'id_group',
  187. 'icon' => 'icon-group',
  188. ),
  189. array('items' =>Country::getCountries($this->context->language->id),
  190. 'title' => $this->l('Country restrictions'),
  191. 'desc' => $this->l('Please mark each checkbox for the country, or countries, in which you want the payment module(s) to be available.'),
  192. 'name_id' => 'country',
  193. 'identifier' => 'id_country',
  194. 'icon' => 'icon-globe',
  195. )
  196. );
  197. foreach ($lists as $key_list => $list) {
  198. $list['check_list'] = array();
  199. foreach ($list['items'] as $key_item => $item) {
  200. $name_id = $list['name_id'];
  201. if ($name_id === 'currency'
  202. && Tools::strpos($list['items'][$key_item]['name'], '('.$list['items'][$key_item]['iso_code'].')') === false) {
  203. $list['items'][$key_item]['name'] = sprintf($this->l('%1$s (%2$s)'), $list['items'][$key_item]['name'],
  204. $list['items'][$key_item]['iso_code']);
  205. }
  206. foreach ($this->payment_modules as $key_module => $module) {
  207. if (isset($module->$name_id) && in_array($item['id_'.$name_id], $module->$name_id)) {
  208. $list['items'][$key_item]['check_list'][$key_module] = 'checked';
  209. } else {
  210. $list['items'][$key_item]['check_list'][$key_module] = 'unchecked';
  211. }
  212. if (!isset($module->$name_id)) {
  213. $module->$name_id = array();
  214. }
  215. if (!isset($module->currencies_mode)) {
  216. $module->currencies_mode = '';
  217. }
  218. if (!isset($module->currencies)) {
  219. $module->currencies = '';
  220. }
  221. // If is a country list and the country is limited, remove it from list
  222. if ($name_id == 'country'
  223. && isset($module->limited_countries)
  224. && !empty($module->limited_countries)
  225. && is_array($module->limited_countries)
  226. && !(in_array(strtoupper($item['iso_code']), array_map('strtoupper', $module->limited_countries)))) {
  227. $list['items'][$key_item]['check_list'][$key_module] = null;
  228. }
  229. }
  230. }
  231. // update list
  232. $lists[$key_list] = $list;
  233. }
  234. $this->tpl_view_vars = array(
  235. 'modules_list' => $this->renderModulesList(),
  236. 'display_restrictions' => $display_restrictions,
  237. 'lists' => $lists,
  238. 'ps_base_uri' => __PS_BASE_URI__,
  239. 'payment_modules' => $this->payment_modules,
  240. 'url_submit' => self::$currentIndex.'&token='.$this->token,
  241. 'shop_context' => $shop_context
  242. );
  243. return parent::renderView();
  244. }
  245. public function renderModulesList()
  246. {
  247. if ($this->getModulesList($this->filter_modules_list)) {
  248. $active_list = array();
  249. foreach ($this->modules_list as $key => $module) {
  250. if (in_array($module->name, $this->list_partners_modules)) {
  251. $this->modules_list[$key]->type = 'addonsPartner';
  252. }
  253. if (isset($module->description_full) && trim($module->description_full) != '') {
  254. $module->show_quick_view = true;
  255. }
  256. if ($module->active) {
  257. $active_list[] = $module;
  258. } else {
  259. $unactive_list[] = $module;
  260. }
  261. }
  262. $helper = new Helper();
  263. $fetch = '';
  264. if (isset($active_list)) {
  265. $this->context->smarty->assign('panel_title', $this->l('Active payment'));
  266. $fetch = $helper->renderModulesList($active_list);
  267. }
  268. $this->context->smarty->assign(array(
  269. 'panel_title' => $this->l('Recommended payment gateways'),
  270. 'view_all' => true
  271. ));
  272. $fetch .= $helper->renderModulesList($unactive_list);
  273. return $fetch;
  274. }
  275. }
  276. }