/admin/model/catalog/information.php

https://gitlab.com/reclamare/mao · PHP · 199 lines · 143 code · 56 blank · 0 comment · 20 complexity · 057c5374b4aade871088e27eebe2672a MD5 · raw file

  1. <?php
  2. class ModelCatalogInformation extends Model {
  3. public function addInformation($data) {
  4. $this->event->trigger('pre.admin.information.add', $data);
  5. $this->db->query("INSERT INTO " . DB_PREFIX . "information SET sort_order = '" . (int)$data['sort_order'] . "', bottom = '" . (isset($data['bottom']) ? (int)$data['bottom'] : 0) . "', status = '" . (int)$data['status'] . "'");
  6. $information_id = $this->db->getLastId();
  7. foreach ($data['information_description'] as $language_id => $value) {
  8. $this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
  9. }
  10. if (isset($data['information_store'])) {
  11. foreach ($data['information_store'] as $store_id) {
  12. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_store SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "'");
  13. }
  14. }
  15. if (isset($data['information_layout'])) {
  16. foreach ($data['information_layout'] as $store_id => $layout_id) {
  17. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_layout SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout_id . "'");
  18. }
  19. }
  20. if (isset($data['keyword'])) {
  21. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  22. }
  23. $this->cache->delete('information');
  24. $this->event->trigger('post.admin.information.add', $information_id);
  25. return $information_id;
  26. }
  27. public function editInformation($information_id, $data) {
  28. $this->event->trigger('pre.admin.information.edit', $data);
  29. $this->db->query("UPDATE " . DB_PREFIX . "information SET sort_order = '" . (int)$data['sort_order'] . "', bottom = '" . (isset($data['bottom']) ? (int)$data['bottom'] : 0) . "', status = '" . (int)$data['status'] . "' WHERE information_id = '" . (int)$information_id . "'");
  30. $this->db->query("DELETE FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  31. foreach ($data['information_description'] as $language_id => $value) {
  32. $this->db->query("INSERT INTO " . DB_PREFIX . "information_description SET information_id = '" . (int)$information_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', meta_title = '" . $this->db->escape($value['meta_title']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . "'");
  33. }
  34. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  35. if (isset($data['information_store'])) {
  36. foreach ($data['information_store'] as $store_id) {
  37. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_store SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "'");
  38. }
  39. }
  40. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  41. if (isset($data['information_layout'])) {
  42. foreach ($data['information_layout'] as $store_id => $layout_id) {
  43. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_layout SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout_id . "'");
  44. }
  45. }
  46. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$information_id . "'");
  47. if ($data['keyword']) {
  48. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  49. }
  50. $this->cache->delete('information');
  51. $this->event->trigger('post.admin.information.edit', $information_id);
  52. }
  53. public function deleteInformation($information_id) {
  54. $this->event->trigger('pre.admin.information.delete', $information_id);
  55. $this->db->query("DELETE FROM " . DB_PREFIX . "information WHERE information_id = '" . (int)$information_id . "'");
  56. $this->db->query("DELETE FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  57. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  58. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  59. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$information_id . "'");
  60. $this->cache->delete('information');
  61. $this->event->trigger('post.admin.information.delete', $information_id);
  62. }
  63. public function getInformation($information_id) {
  64. $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$information_id . "') AS keyword FROM " . DB_PREFIX . "information WHERE information_id = '" . (int)$information_id . "'");
  65. return $query->row;
  66. }
  67. public function getInformations($data = array()) {
  68. if ($data) {
  69. $sql = "SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "'";
  70. $sort_data = array(
  71. 'id.title',
  72. 'i.sort_order'
  73. );
  74. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  75. $sql .= " ORDER BY " . $data['sort'];
  76. } else {
  77. $sql .= " ORDER BY id.title";
  78. }
  79. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  80. $sql .= " DESC";
  81. } else {
  82. $sql .= " ASC";
  83. }
  84. if (isset($data['start']) || isset($data['limit'])) {
  85. if ($data['start'] < 0) {
  86. $data['start'] = 0;
  87. }
  88. if ($data['limit'] < 1) {
  89. $data['limit'] = 20;
  90. }
  91. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  92. }
  93. $query = $this->db->query($sql);
  94. return $query->rows;
  95. } else {
  96. $information_data = $this->cache->get('information.' . (int)$this->config->get('config_language_id'));
  97. if (!$information_data) {
  98. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information i LEFT JOIN " . DB_PREFIX . "information_description id ON (i.information_id = id.information_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY id.title");
  99. $information_data = $query->rows;
  100. $this->cache->set('information.' . (int)$this->config->get('config_language_id'), $information_data);
  101. }
  102. return $information_data;
  103. }
  104. }
  105. public function getInformationDescriptions($information_id) {
  106. $information_description_data = array();
  107. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  108. foreach ($query->rows as $result) {
  109. $information_description_data[$result['language_id']] = array(
  110. 'title' => $result['title'],
  111. 'description' => $result['description'],
  112. 'meta_title' => $result['meta_title'],
  113. 'meta_description' => $result['meta_description'],
  114. 'meta_keyword' => $result['meta_keyword']
  115. );
  116. }
  117. return $information_description_data;
  118. }
  119. public function getInformationStores($information_id) {
  120. $information_store_data = array();
  121. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  122. foreach ($query->rows as $result) {
  123. $information_store_data[] = $result['store_id'];
  124. }
  125. return $information_store_data;
  126. }
  127. public function getInformationLayouts($information_id) {
  128. $information_layout_data = array();
  129. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  130. foreach ($query->rows as $result) {
  131. $information_layout_data[$result['store_id']] = $result['layout_id'];
  132. }
  133. return $information_layout_data;
  134. }
  135. public function getTotalInformations() {
  136. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information");
  137. return $query->row['total'];
  138. }
  139. public function getTotalInformationsByLayoutId($layout_id) {
  140. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information_to_layout WHERE layout_id = '" . (int)$layout_id . "'");
  141. return $query->row['total'];
  142. }
  143. }