PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/admin/controller/common/home.php

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