PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminDashboardController.php

https://github.com/netplayer/PrestaShop
PHP | 384 lines | 305 code | 45 blank | 34 comment | 48 complexity | 18c118fc0465699c58b35f3028ee8e86 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, 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. return $forms;
  157. }
  158. public function renderView()
  159. {
  160. if (Tools::isSubmit('profitability_conf'))
  161. return parent::renderOptions();
  162. // $translations = array(
  163. // 'Calendar' => $this->l('Calendar', 'AdminStatsTab'),
  164. // 'Day' => $this->l('Day', 'AdminStatsTab'),
  165. // 'Month' => $this->l('Month', 'AdminStatsTab'),
  166. // 'Year' => $this->l('Year', 'AdminStatsTab'),
  167. // 'From' => $this->l('From:', 'AdminStatsTab'),
  168. // 'To' => $this->l('To:', 'AdminStatsTab'),
  169. // 'Save' => $this->l('Save', 'AdminStatsTab')
  170. // );
  171. $calendar_helper = new HelperCalendar();
  172. $calendar_helper->setDateFrom(Tools::getValue('date_from', $this->context->employee->stats_date_from));
  173. $calendar_helper->setDateTo(Tools::getValue('date_to', $this->context->employee->stats_date_to));
  174. $stats_compare_from = $this->context->employee->stats_compare_from;
  175. $stats_compare_to = $this->context->employee->stats_compare_to;
  176. if (is_null($stats_compare_from) || $stats_compare_from == '0000-00-00')
  177. $stats_compare_from = null;
  178. if (is_null($stats_compare_to) || $stats_compare_to == '0000-00-00')
  179. $stats_compare_to = null;
  180. $calendar_helper->setCompareDateFrom($stats_compare_from);
  181. $calendar_helper->setCompareDateTo($stats_compare_to);
  182. $calendar_helper->setCompareOption(Tools::getValue('compare_date_option', $this->context->employee->stats_compare_option));
  183. $params = array(
  184. 'date_from' => $this->context->employee->stats_date_from,
  185. 'date_to' => $this->context->employee->stats_date_to
  186. );
  187. $this->tpl_view_vars = array(
  188. 'date_from' => $this->context->employee->stats_date_from,
  189. 'date_to' => $this->context->employee->stats_date_to,
  190. 'hookDashboardZoneOne' => Hook::exec('dashboardZoneOne', $params),
  191. 'hookDashboardZoneTwo' => Hook::exec('dashboardZoneTwo', $params),
  192. //'translations' => $translations,
  193. 'action' => '#',
  194. 'warning' => $this->getWarningDomainName(),
  195. 'new_version_url' => Tools::getCurrentUrlProtocolPrefix().'api.prestashop.com/version/check_version.php?v='._PS_VERSION_.'&lang='.$this->context->language->iso_code,
  196. 'dashboard_use_push' => Configuration::get('PS_DASHBOARD_USE_PUSH'),
  197. 'calendar' => $calendar_helper->generate(),
  198. 'PS_DASHBOARD_SIMULATION' => Configuration::get('PS_DASHBOARD_SIMULATION'),
  199. 'datepickerFrom' => Tools::getValue('datepickerFrom', $this->context->employee->stats_date_from),
  200. 'datepickerTo' => Tools::getValue('datepickerTo', $this->context->employee->stats_date_to),
  201. 'preselect_date_range' => Tools::getValue('preselectDateRange', $this->context->employee->preselect_date_range)
  202. );
  203. return parent::renderView();
  204. }
  205. public function postProcess()
  206. {
  207. if (Tools::isSubmit('submitDateRealTime'))
  208. {
  209. if ($use_realtime = (int)Tools::getValue('submitDateRealTime'))
  210. {
  211. $this->context->employee->stats_date_from = date('Y-m-d');
  212. $this->context->employee->stats_date_to = date('Y-m-d');
  213. $this->context->employee->stats_compare_option = HelperCalendar::DEFAULT_COMPARE_OPTION;
  214. $this->context->employee->stats_compare_from = null;
  215. $this->context->employee->stats_compare_to = null;
  216. $this->context->employee->update();
  217. }
  218. Configuration::updateValue('PS_DASHBOARD_USE_PUSH', $use_realtime);
  219. }
  220. if (Tools::isSubmit('submitDateRange'))
  221. {
  222. $this->context->employee->stats_date_from = Tools::getValue('date_from');
  223. $this->context->employee->stats_date_to = Tools::getValue('date_to');
  224. $this->context->employee->preselect_date_range = Tools::getValue('preselectDateRange');
  225. if (Tools::getValue('datepicker_compare'))
  226. {
  227. $this->context->employee->stats_compare_from = Tools::getValue('compare_date_from');
  228. $this->context->employee->stats_compare_to = Tools::getValue('compare_date_to');
  229. $this->context->employee->stats_compare_option = Tools::getValue('compare_date_option');
  230. }
  231. else
  232. {
  233. $this->context->employee->stats_compare_from = null;
  234. $this->context->employee->stats_compare_to = null;
  235. $this->context->employee->stats_compare_option = HelperCalendar::DEFAULT_COMPARE_OPTION;
  236. }
  237. $this->context->employee->update();
  238. }
  239. parent::postProcess();
  240. }
  241. protected function getWarningDomainName()
  242. {
  243. $warning = false;
  244. if (Shop::isFeatureActive())
  245. return;
  246. $shop = Context::getContext()->shop;
  247. if ($_SERVER['HTTP_HOST'] != $shop->domain && $_SERVER['HTTP_HOST'] != $shop->domain_ssl && Tools::getValue('ajax') == false)
  248. {
  249. $warning = $this->l('You are currently connected under the following domain name:').' <span style="color: #CC0000;">'.$_SERVER['HTTP_HOST'].'</span><br />';
  250. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE'))
  251. $warning .= sprintf($this->l('This is different from the shop domain name set in the Multistore settings: "%s".'), $shop->domain).'
  252. '.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}.'));
  253. else
  254. $warning .= $this->l('This is different from the domain name set in the "SEO & URLs" tab.').'
  255. '.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}.'));
  256. }
  257. return $warning;
  258. }
  259. public function ajaxProcessRefreshDashboard()
  260. {
  261. $id_module = null;
  262. if ($module = Tools::getValue('module'))
  263. {
  264. $module_obj = Module::getInstanceByName($module);
  265. if (Validate::isLoadedObject($module_obj))
  266. $id_module = $module_obj->id;
  267. }
  268. $params = array(
  269. 'date_from' => $this->context->employee->stats_date_from,
  270. 'date_to' => $this->context->employee->stats_date_to,
  271. 'compare_from' => $this->context->employee->stats_compare_from,
  272. 'compare_to' => $this->context->employee->stats_compare_to,
  273. 'dashboard_use_push' => (int)Tools::getValue('dashboard_use_push'),
  274. 'extra' => (int)Tools::getValue('extra')
  275. );
  276. die(Tools::jsonEncode(Hook::exec('dashboardData', $params, $id_module, true, true, (int)Tools::getValue('dashboard_use_push'))));
  277. }
  278. public function ajaxProcessSetSimulationMode()
  279. {
  280. Configuration::updateValue('PS_DASHBOARD_SIMULATION', (int)Tools::getValue('PS_DASHBOARD_SIMULATION'));
  281. die ('k'.Configuration::get('PS_DASHBOARD_SIMULATION').'k');
  282. }
  283. public function ajaxProcessGetBlogRss()
  284. {
  285. $return = array('has_errors' => false, 'rss' => array());
  286. if (!$this->isFresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', 86400))
  287. 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'))
  288. $return['has_errors'] = true;
  289. if (!$return['has_errors'])
  290. {
  291. $rss = simpleXML_load_file(_PS_ROOT_DIR_.'/config/xml/blog-'.$this->context->language->iso_code.'.xml');
  292. $articles_limit = 2;
  293. foreach ($rss->channel->item as $item)
  294. {
  295. if ($articles_limit > 0 && Validate::isCleanHtml((string)$item->title) && Validate::isCleanHtml((string)$item->description))
  296. $return['rss'][] = array(
  297. 'date' => Tools::displayDate(date('Y-m-d', strtotime((string)$item->pubDate))),
  298. 'title' => (string)Tools::htmlentitiesUTF8($item->title),
  299. 'short_desc' => Tools::truncateString(strip_tags((string)$item->description), 150),
  300. 'link' => (string)$item->link,
  301. );
  302. else
  303. break;
  304. $articles_limit --;
  305. }
  306. }
  307. die(Tools::jsonEncode($return));
  308. }
  309. public function ajaxProcessSaveDashConfig()
  310. {
  311. $return = array('has_errors' => false, 'errors' => array());
  312. $module = Tools::getValue('module');
  313. $hook = Tools::getValue('hook');
  314. $configs = Tools::getValue('configs');
  315. $params = array(
  316. 'date_from' => $this->context->employee->stats_date_from,
  317. 'date_to' => $this->context->employee->stats_date_to
  318. );
  319. if (Validate::isModuleName($module) && $module_obj = Module::getInstanceByName($module))
  320. {
  321. if (Validate::isLoadedObject($module_obj) && method_exists($module_obj, 'validateDashConfig'))
  322. $return['errors'] = $module_obj->validateDashConfig($configs);
  323. if (!count($return['errors']))
  324. {
  325. if (Validate::isLoadedObject($module_obj) && method_exists($module_obj, 'saveDashConfig'))
  326. $return['has_errors'] = $module_obj->saveDashConfig($configs);
  327. else if (is_array($configs) && count($configs))
  328. foreach ($configs as $name => $value)
  329. if (Validate::isConfigName($name))
  330. Configuration::updateValue($name, $value);
  331. }
  332. else
  333. $return['has_errors'] = true;
  334. }
  335. if (Validate::isHookName($hook) && method_exists($module_obj, $hook))
  336. $return['widget_html'] = $module_obj->$hook($params);
  337. die(Tools::jsonEncode($return));
  338. }
  339. }