PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminDashboardController.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 401 lines | 319 code | 48 blank | 34 comment | 54 complexity | 5ceecf6b5ae964c87e678f3ebb27dc0d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 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-2014 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 AdminDashboardControllerCore extends AdminController
  27. {
  28. public function __construct()
  29. {
  30. $this->bootstrap = true;
  31. $this->display = 'view';
  32. parent::__construct();
  33. if (Tools::isSubmit('profitability_conf') || Tools::isSubmit('submitOptionsconfiguration'))
  34. $this->fields_options = $this->getOptionFields();
  35. }
  36. public function setMedia()
  37. {
  38. parent::setMedia();
  39. $this->addJqueryUI('ui.datepicker');
  40. $this->addJS(array(
  41. _PS_JS_DIR_.'vendor/d3.v3.min.js',
  42. __PS_BASE_URI__.$this->admin_webpath.'/themes/'.$this->bo_theme.'/js/vendor/nv.d3.min.js',
  43. _PS_JS_DIR_.'/admin/dashboard.js',
  44. ));
  45. $this->addCSS(__PS_BASE_URI__.$this->admin_webpath.'/themes/'.$this->bo_theme.'/css/vendor/nv.d3.css');
  46. }
  47. public function initPageHeaderToolbar()
  48. {
  49. parent::initPageHeaderToolbar();
  50. $this->page_header_toolbar_title = $this->l('Dashboard');
  51. $this->page_header_toolbar_btn = array();
  52. }
  53. protected function getOptionFields()
  54. {
  55. $forms = array();
  56. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  57. $carriers = Carrier::getCarriers($this->context->language->id, true);
  58. $modules = Module::getModulesOnDisk(true);
  59. $forms = array(
  60. 'payment' => array('title' => $this->l('Average bank fees per payment method'), 'id' => 'payment'),
  61. 'carriers' => array('title' => $this->l('Average shipping fees per shipping method'), 'id' => 'carriers'),
  62. 'other' => array('title' => $this->l('Other settings'), 'id' => 'other')
  63. );
  64. foreach ($forms as &$form)
  65. {
  66. $form['icon'] = 'tab-preferences';
  67. $form['fields'] = array();
  68. $form['submit'] = array('title' => $this->l('Save'));
  69. }
  70. foreach ($modules as $module)
  71. if (isset($module->tab) && $module->tab == 'payments_gateways' && $module->id)
  72. {
  73. $moduleClass = Module::getInstanceByName($module->name);
  74. if (!$moduleClass->isEnabledForShopContext())
  75. continue;
  76. $forms['payment']['fields']['CONF_'.strtoupper($module->name).'_FIXED'] = array(
  77. 'title' => $module->displayName,
  78. 'desc' => sprintf($this->l('Choose a fixed fee for each order placed in %1$s with %2$s.'), $currency->iso_code, $module->displayName),
  79. 'validation' => 'isPrice',
  80. 'cast' => 'floatval',
  81. 'type' => 'text',
  82. 'defaultValue' => '0',
  83. 'suffix' => $currency->iso_code
  84. );
  85. $forms['payment']['fields']['CONF_'.strtoupper($module->name).'_VAR'] = array(
  86. 'title' => $module->displayName,
  87. 'desc' => sprintf($this->l('Choose a variable fee for each order placed in %1$s with %2$s. It will be applied on the total paid with taxes.'), $currency->iso_code, $module->displayName),
  88. 'validation' => 'isPercentage',
  89. 'cast' => 'floatval',
  90. 'type' => 'text',
  91. 'defaultValue' => '0',
  92. 'suffix' => '%'
  93. );
  94. if (Currency::isMultiCurrencyActivated())
  95. {
  96. $forms['payment']['fields']['CONF_'.strtoupper($module->name).'_FIXED_FOREIGN'] = array(
  97. 'title' => $module->displayName,
  98. 'desc' => sprintf($this->l('Choose a fixed fee for each order placed with a foreign currency with %s.'), $module->displayName),
  99. 'validation' => 'isPrice',
  100. 'cast' => 'floatval',
  101. 'type' => 'text',
  102. 'defaultValue' => '0',
  103. 'suffix' => $currency->iso_code
  104. );
  105. $forms['payment']['fields']['CONF_'.strtoupper($module->name).'_VAR_FOREIGN'] = array(
  106. 'title' => $module->displayName,
  107. 'desc' => sprintf($this->l('Choose a variable fee for each order placed with a foreign currency with %s. It will be applied on the total paid with taxes.'), $module->displayName),
  108. 'validation' => 'isPercentage',
  109. 'cast' => 'floatval',
  110. 'type' => 'text',
  111. 'defaultValue' => '0',
  112. 'suffix' => '%'
  113. );
  114. }
  115. }
  116. foreach ($carriers as $carrier)
  117. {
  118. $forms['carriers']['fields']['CONF_'.strtoupper($carrier['id_reference']).'_SHIP'] = array(
  119. 'title' => $carrier['name'],
  120. 'desc' => sprintf($this->l('For the carrier named %s, indicate the domestic delivery costs in percentage of the price charged to customers.'), $carrier['name']),
  121. 'validation' => 'isPercentage',
  122. 'cast' => 'floatval',
  123. 'type' => 'text',
  124. 'defaultValue' => '0',
  125. 'suffix' => '%'
  126. );
  127. $forms['carriers']['fields']['CONF_'.strtoupper($carrier['id_reference']).'_SHIP_OVERSEAS'] = array(
  128. 'title' => $carrier['name'],
  129. 'desc' => sprintf($this->l('For the carrier named %s, indicate the overseas delivery costs in percentage of the price charged to customers.'), $carrier['name']),
  130. 'validation' => 'isPercentage',
  131. 'cast' => 'floatval',
  132. 'type' => 'text',
  133. 'defaultValue' => '0',
  134. 'suffix' => '%'
  135. );
  136. }
  137. $forms['carriers']['description'] = $this->l('Method: Indicate the percentage of your carrier margin. For example, if you charge $10 of shipping fees to your customer for each shipment, but you really pay $4 to this carrier, then you should indicate "40" in the percentage field.');
  138. $forms['other']['fields']['CONF_AVERAGE_PRODUCT_MARGIN'] = array(
  139. 'title' => $this->l('Average gross margin'),
  140. 'desc' => $this->l('You should calculate this percentage as follows: ((total sales revenue) - (cost of goods sold)) / (total sales revenue) * 100. This value is only used to calculate the Dashboard approximate gross margin, if you do not specify the wholesale price for each product.'),
  141. 'validation' => 'isPercentage',
  142. 'cast' => 'intval',
  143. 'type' => 'text',
  144. 'defaultValue' => '0',
  145. 'suffix' => '%'
  146. );
  147. $forms['other']['fields']['CONF_ORDER_FIXED'] = array(
  148. 'title' => $this->l('Other fees per order'),
  149. 'desc' => $this->l('You should calculate this value by making the sum of all of your additional costs per order.'),
  150. 'validation' => 'isPrice',
  151. 'cast' => 'floatval',
  152. 'type' => 'text',
  153. 'defaultValue' => '0',
  154. 'suffix' => $currency->iso_code
  155. );
  156. Media::addJsDef(array(
  157. 'dashboard_ajax_url' => $this->context->link->getAdminLink('AdminDashboard'),
  158. 'read_more' => '',
  159. ));
  160. return $forms;
  161. }
  162. public function renderView()
  163. {
  164. if (Tools::isSubmit('profitability_conf'))
  165. return parent::renderOptions();
  166. // $translations = array(
  167. // 'Calendar' => $this->l('Calendar', 'AdminStatsTab'),
  168. // 'Day' => $this->l('Day', 'AdminStatsTab'),
  169. // 'Month' => $this->l('Month', 'AdminStatsTab'),
  170. // 'Year' => $this->l('Year', 'AdminStatsTab'),
  171. // 'From' => $this->l('From:', 'AdminStatsTab'),
  172. // 'To' => $this->l('To:', 'AdminStatsTab'),
  173. // 'Save' => $this->l('Save', 'AdminStatsTab')
  174. // );
  175. $calendar_helper = new HelperCalendar();
  176. $calendar_helper->setDateFrom(Tools::getValue('date_from', $this->context->employee->stats_date_from));
  177. $calendar_helper->setDateTo(Tools::getValue('date_to', $this->context->employee->stats_date_to));
  178. $stats_compare_from = $this->context->employee->stats_compare_from;
  179. $stats_compare_to = $this->context->employee->stats_compare_to;
  180. if (is_null($stats_compare_from) || $stats_compare_from == '0000-00-00')
  181. $stats_compare_from = null;
  182. if (is_null($stats_compare_to) || $stats_compare_to == '0000-00-00')
  183. $stats_compare_to = null;
  184. $calendar_helper->setCompareDateFrom($stats_compare_from);
  185. $calendar_helper->setCompareDateTo($stats_compare_to);
  186. $calendar_helper->setCompareOption(Tools::getValue('compare_date_option', $this->context->employee->stats_compare_option));
  187. $params = array(
  188. 'date_from' => $this->context->employee->stats_date_from,
  189. 'date_to' => $this->context->employee->stats_date_to
  190. );
  191. $this->tpl_view_vars = array(
  192. 'date_from' => $this->context->employee->stats_date_from,
  193. 'date_to' => $this->context->employee->stats_date_to,
  194. 'hookDashboardZoneOne' => Hook::exec('dashboardZoneOne', $params),
  195. 'hookDashboardZoneTwo' => Hook::exec('dashboardZoneTwo', $params),
  196. //'translations' => $translations,
  197. 'action' => '#',
  198. 'warning' => $this->getWarningDomainName(),
  199. 'new_version_url' => Tools::getCurrentUrlProtocolPrefix().'api.prestashop.com/version/check_version.php?v='._PS_VERSION_.'&lang='.$this->context->language->iso_code.'&autoupgrade='.(int)(Module::isInstalled('autoupgrade') && Module::isEnabled('autoupgrade')).'&hosted_mode='.(int)defined('_PS_HOST_MODE_'),
  200. 'dashboard_use_push' => Configuration::get('PS_DASHBOARD_USE_PUSH'),
  201. 'calendar' => $calendar_helper->generate(),
  202. 'PS_DASHBOARD_SIMULATION' => Configuration::get('PS_DASHBOARD_SIMULATION'),
  203. 'datepickerFrom' => Tools::getValue('datepickerFrom', $this->context->employee->stats_date_from),
  204. 'datepickerTo' => Tools::getValue('datepickerTo', $this->context->employee->stats_date_to),
  205. 'preselect_date_range' => Tools::getValue('preselectDateRange', $this->context->employee->preselect_date_range)
  206. );
  207. return parent::renderView();
  208. }
  209. public function postProcess()
  210. {
  211. if (Tools::isSubmit('submitDateRealTime'))
  212. {
  213. if ($use_realtime = (int)Tools::getValue('submitDateRealTime'))
  214. {
  215. $this->context->employee->stats_date_from = date('Y-m-d');
  216. $this->context->employee->stats_date_to = date('Y-m-d');
  217. $this->context->employee->stats_compare_option = HelperCalendar::DEFAULT_COMPARE_OPTION;
  218. $this->context->employee->stats_compare_from = null;
  219. $this->context->employee->stats_compare_to = null;
  220. $this->context->employee->update();
  221. }
  222. Configuration::updateValue('PS_DASHBOARD_USE_PUSH', $use_realtime);
  223. }
  224. if (Tools::isSubmit('submitDateRange'))
  225. {
  226. if (!Validate::isDate(Tools::getValue('date_from'))
  227. || !Validate::isDate(Tools::getValue('date_to')))
  228. $this->errors[] = Tools::displayError('The selected date range is not valid.');
  229. if (Tools::getValue('datepicker_compare'))
  230. if (!Validate::isDate(Tools::getValue('compare_date_from'))
  231. || !Validate::isDate(Tools::getValue('compare_date_to')))
  232. $this->errors[] = Tools::displayError('The selected date range is not valid.');
  233. if (!count($this->errors))
  234. {
  235. $this->context->employee->stats_date_from = Tools::getValue('date_from');
  236. $this->context->employee->stats_date_to = Tools::getValue('date_to');
  237. $this->context->employee->preselect_date_range = Tools::getValue('preselectDateRange');
  238. if (Tools::getValue('datepicker_compare'))
  239. {
  240. $this->context->employee->stats_compare_from = Tools::getValue('compare_date_from');
  241. $this->context->employee->stats_compare_to = Tools::getValue('compare_date_to');
  242. $this->context->employee->stats_compare_option = Tools::getValue('compare_date_option');
  243. }
  244. else
  245. {
  246. $this->context->employee->stats_compare_from = null;
  247. $this->context->employee->stats_compare_to = null;
  248. $this->context->employee->stats_compare_option = HelperCalendar::DEFAULT_COMPARE_OPTION;
  249. }
  250. $this->context->employee->update();
  251. }
  252. }
  253. parent::postProcess();
  254. }
  255. protected function getWarningDomainName()
  256. {
  257. $warning = false;
  258. if (Shop::isFeatureActive())
  259. return;
  260. $shop = Context::getContext()->shop;
  261. if ($_SERVER['HTTP_HOST'] != $shop->domain && $_SERVER['HTTP_HOST'] != $shop->domain_ssl && Tools::getValue('ajax') == false && !defined('_PS_HOST_MODE_'))
  262. {
  263. $warning = $this->l('You are currently connected under the following domain name:').' <span style="color: #CC0000;">'.$_SERVER['HTTP_HOST'].'</span><br />';
  264. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE'))
  265. $warning .= sprintf($this->l('This is different from the shop domain name set in the Multistore settings: "%s".'), $shop->domain).'
  266. '.preg_replace('@{link}(.*){/link}@', '<a href="index.php?controller=AdminShopUrl&id_shop_url='.(int)$shop->id.'&updateshop_url&token='.Tools::getAdminTokenLite('AdminShopUrl').'">$1</a>', $this->l('If this is your main domain, please {link}change it now{/link}.'));
  267. else
  268. $warning .= $this->l('This is different from the domain name set in the "SEO & URLs" tab.').'
  269. '.preg_replace('@{link}(.*){/link}@', '<a href="index.php?controller=AdminMeta&token='.Tools::getAdminTokenLite('AdminMeta').'#meta_fieldset_shop_url">$1</a>', $this->l('If this is your main domain, please {link}change it now{/link}.'));
  270. }
  271. return $warning;
  272. }
  273. public function ajaxProcessRefreshDashboard()
  274. {
  275. $id_module = null;
  276. if ($module = Tools::getValue('module'))
  277. {
  278. $module_obj = Module::getInstanceByName($module);
  279. if (Validate::isLoadedObject($module_obj))
  280. $id_module = $module_obj->id;
  281. }
  282. $params = array(
  283. 'date_from' => $this->context->employee->stats_date_from,
  284. 'date_to' => $this->context->employee->stats_date_to,
  285. 'compare_from' => $this->context->employee->stats_compare_from,
  286. 'compare_to' => $this->context->employee->stats_compare_to,
  287. 'dashboard_use_push' => (int)Tools::getValue('dashboard_use_push'),
  288. 'extra' => (int)Tools::getValue('extra')
  289. );
  290. die(Tools::jsonEncode(Hook::exec('dashboardData', $params, $id_module, true, true, (int)Tools::getValue('dashboard_use_push'))));
  291. }
  292. public function ajaxProcessSetSimulationMode()
  293. {
  294. Configuration::updateValue('PS_DASHBOARD_SIMULATION', (int)Tools::getValue('PS_DASHBOARD_SIMULATION'));
  295. die ('k'.Configuration::get('PS_DASHBOARD_SIMULATION').'k');
  296. }
  297. public function ajaxProcessGetBlogRss()
  298. {
  299. $return = array('has_errors' => false, 'rss' => array());
  300. if (!$this->isFresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', 86400))
  301. if (!$this->refresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', 'https://api.prestashop.com/rss/blog/blog-'.$this->context->language->iso_code.'.xml'))
  302. $return['has_errors'] = true;
  303. if (!$return['has_errors'])
  304. {
  305. $rss = simpleXML_load_file(_PS_ROOT_DIR_.'/config/xml/blog-'.$this->context->language->iso_code.'.xml');
  306. $articles_limit = 2;
  307. foreach ($rss->channel->item as $item)
  308. {
  309. if ($articles_limit > 0 && Validate::isCleanHtml((string)$item->title) && Validate::isCleanHtml((string)$item->description))
  310. $return['rss'][] = array(
  311. 'date' => Tools::displayDate(date('Y-m-d', strtotime((string)$item->pubDate))),
  312. 'title' => (string)Tools::htmlentitiesUTF8($item->title),
  313. 'short_desc' => Tools::truncateString(strip_tags((string)$item->description), 150),
  314. 'link' => (string)$item->link,
  315. );
  316. else
  317. break;
  318. $articles_limit --;
  319. }
  320. }
  321. die(Tools::jsonEncode($return));
  322. }
  323. public function ajaxProcessSaveDashConfig()
  324. {
  325. $return = array('has_errors' => false, 'errors' => array());
  326. $module = Tools::getValue('module');
  327. $hook = Tools::getValue('hook');
  328. $configs = Tools::getValue('configs');
  329. $params = array(
  330. 'date_from' => $this->context->employee->stats_date_from,
  331. 'date_to' => $this->context->employee->stats_date_to
  332. );
  333. if (Validate::isModuleName($module) && $module_obj = Module::getInstanceByName($module))
  334. {
  335. if (Validate::isLoadedObject($module_obj) && method_exists($module_obj, 'validateDashConfig'))
  336. $return['errors'] = $module_obj->validateDashConfig($configs);
  337. if (!count($return['errors']))
  338. {
  339. if (Validate::isLoadedObject($module_obj) && method_exists($module_obj, 'saveDashConfig'))
  340. $return['has_errors'] = $module_obj->saveDashConfig($configs);
  341. elseif (is_array($configs) && count($configs))
  342. foreach ($configs as $name => $value)
  343. if (Validate::isConfigName($name))
  344. Configuration::updateValue($name, $value);
  345. }
  346. else
  347. $return['has_errors'] = true;
  348. }
  349. if (Validate::isHookName($hook) && method_exists($module_obj, $hook))
  350. $return['widget_html'] = $module_obj->$hook($params);
  351. die(Tools::jsonEncode($return));
  352. }
  353. }