PageRenderTime 103ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/controller/common/home.php

https://bitbucket.org/vsepessarii/vsepessarii
PHP | 406 lines | 298 code | 101 blank | 7 comment | 58 complexity | 2a10d6ffae5dddaec2ce35d016e8ca0b MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. class ControllerCommonHome extends Controller {
  3. public function index() {
  4. $this->load->language('common/home');
  5. $this->document->setTitle($this->language->get('heading_title'));
  6. $this->data['heading_title'] = $this->language->get('heading_title');
  7. $this->data['text_overview'] = $this->language->get('text_overview');
  8. $this->data['text_statistics'] = $this->language->get('text_statistics');
  9. //$this->data['text_latest_10_orders'] = $this->language->get('text_latest_10_orders');
  10. $this->data['text_latest_10_orders'] = 'Последние 20 заказов';
  11. $this->data['text_total_sale'] = $this->language->get('text_total_sale');
  12. $this->data['text_total_sale_year'] = $this->language->get('text_total_sale_year');
  13. $this->data['text_total_order'] = $this->language->get('text_total_order');
  14. $this->data['text_total_customer'] = $this->language->get('text_total_customer');
  15. $this->data['text_total_customer_approval'] = $this->language->get('text_total_customer_approval');
  16. $this->data['text_total_review_approval'] = $this->language->get('text_total_review_approval');
  17. $this->data['text_total_affiliate'] = $this->language->get('text_total_affiliate');
  18. $this->data['text_total_affiliate_approval'] = $this->language->get('text_total_affiliate_approval');
  19. $this->data['text_day'] = $this->language->get('text_day');
  20. $this->data['text_week'] = $this->language->get('text_week');
  21. $this->data['text_month'] = $this->language->get('text_month');
  22. $this->data['text_year'] = $this->language->get('text_year');
  23. $this->data['text_no_results'] = $this->language->get('text_no_results');
  24. $this->data['column_order'] = $this->language->get('column_order');
  25. $this->data['column_customer'] = $this->language->get('column_customer');
  26. $this->data['column_status'] = $this->language->get('column_status');
  27. $this->data['column_date_added'] = $this->language->get('column_date_added');
  28. $this->data['column_total'] = $this->language->get('column_total');
  29. $this->data['column_firstname'] = $this->language->get('column_firstname');
  30. $this->data['column_lastname'] = $this->language->get('column_lastname');
  31. $this->data['column_action'] = $this->language->get('column_action');
  32. $this->data['entry_range'] = $this->language->get('entry_range');
  33. // Check install directory exists
  34. if (is_dir(dirname(DIR_APPLICATION) . '/install')) {
  35. $this->data['error_install'] = $this->language->get('error_install');
  36. } else {
  37. $this->data['error_install'] = '';
  38. }
  39. // Check image directory is writable
  40. $file = DIR_IMAGE . 'test';
  41. $handle = fopen($file, 'a+');
  42. fwrite($handle, '');
  43. fclose($handle);
  44. if (!file_exists($file)) {
  45. $this->data['error_image'] = sprintf($this->language->get('error_image'). DIR_IMAGE);
  46. } else {
  47. $this->data['error_image'] = '';
  48. unlink($file);
  49. }
  50. // Check image cache directory is writable
  51. $file = DIR_IMAGE . 'cache/test';
  52. $handle = fopen($file, 'a+');
  53. fwrite($handle, '');
  54. fclose($handle);
  55. if (!file_exists($file)) {
  56. $this->data['error_image_cache'] = sprintf($this->language->get('error_image_cache'). DIR_IMAGE . 'cache/');
  57. } else {
  58. $this->data['error_image_cache'] = '';
  59. unlink($file);
  60. }
  61. // Check cache directory is writable
  62. $file = DIR_CACHE . 'test';
  63. $handle = fopen($file, 'a+');
  64. fwrite($handle, '');
  65. fclose($handle);
  66. if (!file_exists($file)) {
  67. $this->data['error_cache'] = sprintf($this->language->get('error_image_cache'). DIR_CACHE);
  68. } else {
  69. $this->data['error_cache'] = '';
  70. unlink($file);
  71. }
  72. // Check download directory is writable
  73. $file = DIR_DOWNLOAD . 'test';
  74. $handle = fopen($file, 'a+');
  75. fwrite($handle, '');
  76. fclose($handle);
  77. if (!file_exists($file)) {
  78. $this->data['error_download'] = sprintf($this->language->get('error_download'). DIR_DOWNLOAD);
  79. } else {
  80. $this->data['error_download'] = '';
  81. unlink($file);
  82. }
  83. // Check logs directory is writable
  84. $file = DIR_LOGS . 'test';
  85. $handle = fopen($file, 'a+');
  86. fwrite($handle, '');
  87. fclose($handle);
  88. if (!file_exists($file)) {
  89. $this->data['errorlogs'] = sprintf($this->language->get('error_logs'). DIR_LOGS);
  90. } else {
  91. $this->data['error_logs'] = '';
  92. unlink($file);
  93. }
  94. $this->data['breadcrumbs'] = array();
  95. $this->data['breadcrumbs'][] = array(
  96. 'text' => $this->language->get('text_home'),
  97. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  98. 'separator' => false
  99. );
  100. $this->data['token'] = $this->session->data['token'];
  101. $this->load->model('sale/order');
  102. $this->data['total_sale'] = $this->currency->format($this->model_sale_order->getTotalSales(), $this->config->get('config_currency'));
  103. $this->data['total_sale_year'] = $this->currency->format($this->model_sale_order->getTotalSalesByYear(date('Y')), $this->config->get('config_currency'));
  104. $this->data['total_order'] = $this->model_sale_order->getTotalOrders();
  105. $this->load->model('sale/customer');
  106. $this->data['total_customer'] = $this->model_sale_customer->getTotalCustomers();
  107. $this->data['total_customer_approval'] = $this->model_sale_customer->getTotalCustomersAwaitingApproval();
  108. $this->load->model('catalog/review');
  109. $this->data['total_review'] = $this->model_catalog_review->getTotalReviews();
  110. $this->data['total_review_approval'] = $this->model_catalog_review->getTotalReviewsAwaitingApproval();
  111. $this->load->model('sale/affiliate');
  112. $this->data['total_affiliate'] = $this->model_sale_affiliate->getTotalAffiliates();
  113. $this->data['total_affiliate_approval'] = $this->model_sale_affiliate->getTotalAffiliatesAwaitingApproval();
  114. $this->data['orders'] = array();
  115. $data = array(
  116. 'sort' => 'o.date_added',
  117. 'order' => 'DESC',
  118. 'start' => 0,
  119. 'limit' => 20
  120. );
  121. $results = $this->model_sale_order->getOrders($data);
  122. foreach ($results as $result) {
  123. $action = array();
  124. $action[] = array(
  125. 'text' => $this->language->get('text_view'),
  126. 'href' => $this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'], 'SSL')
  127. );
  128. $this->data['orders'][] = array(
  129. 'order_id' => $result['order_id'],
  130. 'customer' => $result['customer'],
  131. 'status' => $result['status'],
  132. 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
  133. 'time_added' => date($this->language->get('H:i:s'), strtotime($result['date_added'])),
  134. 'total' => $this->currency->format($result['total'], $result['currency_code'], $result['currency_value']),
  135. 'action' => $action
  136. );
  137. }
  138. if ($this->config->get('config_currency_auto')) {
  139. $this->load->model('localisation/currency');
  140. $this->model_localisation_currency->updateCurrencies();
  141. }
  142. $this->template = 'common/home.tpl';
  143. $this->children = array(
  144. 'common/header',
  145. 'common/footer'
  146. );
  147. $this->response->setOutput($this->render());
  148. }
  149. public function chart() {
  150. $this->load->language('common/home');
  151. $data = array();
  152. $data['order'] = array();
  153. $data['customer'] = array();
  154. $data['xaxis'] = array();
  155. $data['order']['label'] = $this->language->get('text_order');
  156. $data['customer']['label'] = $this->language->get('text_customer');
  157. if (isset($this->request->get['range'])) {
  158. $range = $this->request->get['range'];
  159. } else {
  160. $range = 'month';
  161. }
  162. switch ($range) {
  163. case 'day':
  164. for ($i = 0; $i < 24; $i++) {
  165. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND (DATE(date_added) = DATE(NOW()) AND HOUR(date_added) = '" . (int)$i . "') GROUP BY HOUR(date_added) ORDER BY date_added ASC");
  166. if ($query->num_rows) {
  167. $data['order']['data'][] = array($i, (int)$query->row['total']);
  168. } else {
  169. $data['order']['data'][] = array($i, 0);
  170. }
  171. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE DATE(date_added) = DATE(NOW()) AND HOUR(date_added) = '" . (int)$i . "' GROUP BY HOUR(date_added) ORDER BY date_added ASC");
  172. if ($query->num_rows) {
  173. $data['customer']['data'][] = array($i, (int)$query->row['total']);
  174. } else {
  175. $data['customer']['data'][] = array($i, 0);
  176. }
  177. $data['xaxis'][] = array($i, date('H', mktime($i, 0, 0, date('n'), date('j'), date('Y'))));
  178. }
  179. break;
  180. case 'week':
  181. $date_start = strtotime('-' . date('w') . ' days');
  182. for ($i = 0; $i < 7; $i++) {
  183. $date = date('Y-m-d', $date_start + ($i * 86400));
  184. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND DATE(date_added) = '" . $this->db->escape($date) . "' GROUP BY DATE(date_added)");
  185. if ($query->num_rows) {
  186. $data['order']['data'][] = array($i, (int)$query->row['total']);
  187. } else {
  188. $data['order']['data'][] = array($i, 0);
  189. }
  190. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "customer` WHERE DATE(date_added) = '" . $this->db->escape($date) . "' GROUP BY DATE(date_added)");
  191. if ($query->num_rows) {
  192. $data['customer']['data'][] = array($i, (int)$query->row['total']);
  193. } else {
  194. $data['customer']['data'][] = array($i, 0);
  195. }
  196. $data['xaxis'][] = array($i, date('D', strtotime($date)));
  197. }
  198. break;
  199. default:
  200. case 'month':
  201. for ($i = 1; $i <= date('t'); $i++) {
  202. $date = date('Y') . '-' . date('m') . '-' . $i;
  203. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND (DATE(date_added) = '" . $this->db->escape($date) . "') GROUP BY DAY(date_added)");
  204. if ($query->num_rows) {
  205. $data['order']['data'][] = array($i, (int)$query->row['total']);
  206. } else {
  207. $data['order']['data'][] = array($i, 0);
  208. }
  209. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE DATE(date_added) = '" . $this->db->escape($date) . "' GROUP BY DAY(date_added)");
  210. if ($query->num_rows) {
  211. $data['customer']['data'][] = array($i, (int)$query->row['total']);
  212. } else {
  213. $data['customer']['data'][] = array($i, 0);
  214. }
  215. $data['xaxis'][] = array($i, date('j', strtotime($date)));
  216. }
  217. break;
  218. case 'year':
  219. for ($i = 1; $i <= 12; $i++) {
  220. $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE order_status_id > '0' AND YEAR(date_added) = '" . date('Y') . "' AND MONTH(date_added) = '" . $i . "' GROUP BY MONTH(date_added)");
  221. if ($query->num_rows) {
  222. $data['order']['data'][] = array($i, (int)$query->row['total']);
  223. } else {
  224. $data['order']['data'][] = array($i, 0);
  225. }
  226. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE YEAR(date_added) = '" . date('Y') . "' AND MONTH(date_added) = '" . $i . "' GROUP BY MONTH(date_added)");
  227. if ($query->num_rows) {
  228. $data['customer']['data'][] = array($i, (int)$query->row['total']);
  229. } else {
  230. $data['customer']['data'][] = array($i, 0);
  231. }
  232. $data['xaxis'][] = array($i, date('M', mktime(0, 0, 0, $i, 1, date('Y'))));
  233. }
  234. break;
  235. }
  236. $this->response->setOutput(json_encode($data));
  237. }
  238. public function login() {
  239. $route = '';
  240. if (isset($this->request->get['route'])) {
  241. $part = explode('/', $this->request->get['route']);
  242. if (isset($part[0])) {
  243. $route .= $part[0];
  244. }
  245. if (isset($part[1])) {
  246. $route .= '/' . $part[1];
  247. }
  248. }
  249. $ignore = array(
  250. 'common/login',
  251. 'common/forgotten',
  252. 'common/reset'
  253. );
  254. if (!$this->user->isLogged() && !in_array($route, $ignore)) {
  255. return $this->forward('common/login');
  256. }
  257. if (isset($this->request->get['route'])) {
  258. $ignore = array(
  259. 'common/login',
  260. 'common/logout',
  261. 'common/forgotten',
  262. 'common/reset',
  263. 'error/not_found',
  264. 'error/permission'
  265. );
  266. $config_ignore = array();
  267. if ($this->config->get('config_token_ignore')) {
  268. $config_ignore = unserialize($this->config->get('config_token_ignore'));
  269. }
  270. $ignore = array_merge($ignore, $config_ignore);
  271. if (!in_array($route, $ignore) && (!isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token']))) {
  272. return $this->forward('common/login');
  273. }
  274. } else {
  275. if (!isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token'])) {
  276. return $this->forward('common/login');
  277. }
  278. }
  279. }
  280. public function permission() {
  281. if (isset($this->request->get['route'])) {
  282. $route = '';
  283. $part = explode('/', $this->request->get['route']);
  284. if (isset($part[0])) {
  285. $route .= $part[0];
  286. }
  287. if (isset($part[1])) {
  288. $route .= '/' . $part[1];
  289. }
  290. $ignore = array(
  291. 'common/home',
  292. 'common/login',
  293. 'common/logout',
  294. 'common/forgotten',
  295. 'common/reset',
  296. 'error/not_found',
  297. 'error/permission'
  298. );
  299. if (!in_array($route, $ignore) && !$this->user->hasPermission('access', $route)) {
  300. return $this->forward('error/permission');
  301. }
  302. }
  303. }
  304. }
  305. ?>