/admin/model/catalog/information.php

https://gitlab.com/AndreyLes/top-tap · PHP · 290 lines · 223 code · 67 blank · 0 comment · 37 complexity · 5fd3ffea8bdd6a5f5fe70386149ad490 MD5 · raw file

  1. <?php
  2. class ModelCatalogInformation extends Model {
  3. public function addInformation($data) {
  4. $this->db->query("INSERT INTO " . DB_PREFIX . "information SET sort_order = '" . (int)$data['sort_order'] . "', top = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', bottom = '" . (isset($data['bottom']) ? (int)$data['bottom'] : 0) . "', status = '" . (int)$data['status'] . "'");
  5. $information_id = $this->db->getLastId();
  6. $informationTitle = '';
  7. foreach ($data['information_description'] as $language_id => $value) {
  8. if ($language_id == $this->config->get('config_language_id')){
  9. $informationTitle = $value['title'];
  10. }
  11. $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_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', seo_title = '" . $this->db->escape($value['seo_title']) . "', seo_h1 = '" . $this->db->escape($value['seo_h1']) . "'");
  12. }
  13. if (isset($data['information_store'])) {
  14. foreach ($data['information_store'] as $store_id) {
  15. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_store SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "'");
  16. }
  17. }
  18. if (isset($data['information_layout'])) {
  19. foreach ($data['information_layout'] as $store_id => $layout) {
  20. if ($layout) {
  21. $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['layout_id'] . "'");
  22. }
  23. }
  24. }
  25. if ($data['keyword']) {
  26. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  27. } else {
  28. $this->load->model('tool/seo_manager');
  29. if ($informationTitle) {
  30. $this->model_tool_seo_manager->generateInformation($information_id, $informationTitle, $this->config->get('config_template_seo_url_informations'), $this->config->get('config_language'));
  31. }
  32. }
  33. $this->cache->delete('information');
  34. $this->cache->delete('seo_pro');
  35. $this->cache->delete('seo_url');
  36. return $information_id;
  37. }
  38. public function editInformation($information_id, $data) {
  39. $this->db->query("UPDATE " . DB_PREFIX . "information SET sort_order = '" . (int)$data['sort_order'] . "', top = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', bottom = '" . (isset($data['bottom']) ? (int)$data['bottom'] : 0) . "', status = '" . (int)$data['status'] . "' WHERE information_id = '" . (int)$information_id . "'");
  40. $this->db->query("DELETE FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  41. $informationTitle = '';
  42. foreach ($data['information_description'] as $language_id => $value) {
  43. if ($language_id == $this->config->get('config_language_id')){
  44. $informationTitle = $value['title'];
  45. }
  46. $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_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', seo_title = '" . $this->db->escape($value['seo_title']) . "', seo_h1 = '" . $this->db->escape($value['seo_h1']) . "'");
  47. }
  48. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  49. if (isset($data['information_store'])) {
  50. foreach ($data['information_store'] as $store_id) {
  51. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_store SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "'");
  52. }
  53. }
  54. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  55. if (isset($data['information_layout'])) {
  56. foreach ($data['information_layout'] as $store_id => $layout) {
  57. if ($layout['layout_id']) {
  58. $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['layout_id'] . "'");
  59. }
  60. }
  61. }
  62. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$information_id. "'");
  63. if ($data['keyword']) {
  64. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  65. } else {
  66. $this->load->model('tool/seo_manager');
  67. if ($informationTitle) {
  68. $this->model_tool_seo_manager->generateInformation($information_id, $informationTitle, $this->config->get('config_template_seo_url_informations'), $this->config->get('config_language'));
  69. }
  70. }
  71. $this->cache->delete('information');
  72. $this->cache->delete('seo_pro');
  73. $this->cache->delete('seo_url');
  74. return $information_id;
  75. }
  76. public function deleteInformation($information_id) {
  77. $this->db->query("DELETE FROM " . DB_PREFIX . "information WHERE information_id = '" . (int)$information_id . "'");
  78. $this->db->query("DELETE FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  79. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  80. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  81. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$information_id . "'");
  82. $this->cache->delete('information');
  83. }
  84. public function getInformation($information_id) {
  85. $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 . "'");
  86. return $query->row;
  87. }
  88. public function getInformations($data = array()) {
  89. if ($data) {
  90. $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') . "'";
  91. $sort_data = array(
  92. 'id.title',
  93. 'i.sort_order'
  94. );
  95. if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
  96. $sql .= " ORDER BY " . $data['sort'];
  97. } else {
  98. $sql .= " ORDER BY id.title";
  99. }
  100. if (isset($data['order']) && ($data['order'] == 'DESC')) {
  101. $sql .= " DESC";
  102. } else {
  103. $sql .= " ASC";
  104. }
  105. if (isset($data['start']) || isset($data['limit'])) {
  106. if ($data['start'] < 0) {
  107. $data['start'] = 0;
  108. }
  109. if ($data['limit'] < 1) {
  110. $data['limit'] = 20;
  111. }
  112. $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
  113. }
  114. $query = $this->db->query($sql);
  115. return $query->rows;
  116. } else {
  117. $information_data = $this->cache->get('information.' . (int)$this->config->get('config_language_id'));
  118. if (!$information_data) {
  119. $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");
  120. $information_data = $query->rows;
  121. $this->cache->set('information.' . (int)$this->config->get('config_language_id'), $information_data);
  122. }
  123. return $information_data;
  124. }
  125. }
  126. public function getInformationDescriptions($information_id) {
  127. $information_description_data = array();
  128. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  129. foreach ($query->rows as $result) {
  130. $information_description_data[$result['language_id']] = array(
  131. 'title' => $result['title'],
  132. 'seo_title' => $result['seo_title'],
  133. 'seo_h1' => $result['seo_h1'],
  134. 'meta_keyword' => $result['meta_keyword'],
  135. 'meta_description' => $result['meta_description'],
  136. 'description' => $result['description']
  137. );
  138. }
  139. return $information_description_data;
  140. }
  141. public function getInformationStores($information_id) {
  142. $information_store_data = array();
  143. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  144. foreach ($query->rows as $result) {
  145. $information_store_data[] = $result['store_id'];
  146. }
  147. return $information_store_data;
  148. }
  149. public function getInformationLayouts($information_id) {
  150. $information_layout_data = array();
  151. $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  152. foreach ($query->rows as $result) {
  153. $information_layout_data[$result['store_id']] = $result['layout_id'];
  154. }
  155. return $information_layout_data;
  156. }
  157. public function getTotalInformations() {
  158. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information");
  159. return $query->row['total'];
  160. }
  161. public function getTotalInformationsByLayoutId($layout_id) {
  162. $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "information_to_layout WHERE layout_id = '" . (int)$layout_id . "'");
  163. return $query->row['total'];
  164. }
  165. public function getInformationTitle($information_id) {
  166. $selected_language = (int)$this->config->get('config_language_id');
  167. $row = $this->db->query("SELECT title FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int) $information_id . "' AND language_id = '" . $selected_language . "'");
  168. if(isset($row->row['title'])){
  169. return $row->row['title'];
  170. }
  171. }
  172. public function changeTitle($information_id, $title){
  173. $selected_language = (int)$this->config->get('config_language_id');
  174. if ($this->user->hasPermission('modify', 'catalog/information_quick')) {
  175. $query = $this->db->query("UPDATE " . DB_PREFIX . "information_description SET title= '" .$this->db->escape($title) . "' WHERE information_id = '" . (int)$information_id . "' AND language_id='" . $selected_language . "'");
  176. }
  177. $query = $this->db->query("SELECT title FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "' AND language_id= '" . $selected_language . "'");
  178. $this->cache->delete('information');
  179. return $query->row['title'];
  180. }
  181. public function changeSortOrder($information_id, $sort_order) {
  182. if ($this->user->hasPermission('modify', 'catalog/information_quick')) {
  183. $this->db->query("UPDATE " . DB_PREFIX . "information SET sort_order = '" . (int)$sort_order . "' WHERE information_id = '" . (int)$information_id . "'");
  184. }
  185. $query = $this->db->query("SELECT sort_order FROM " . DB_PREFIX . "information WHERE information_id = '". $information_id . "'");
  186. $row = $query->row;
  187. $this->cache->delete('information');
  188. return $row['sort_order'];
  189. }
  190. public function changeStatus($information_id, $column_name, $value){
  191. $this->db->query("UPDATE " . DB_PREFIX . "information SET " . $column_name . " = '" . (int)$value . "' WHERE information_id = '" . (int)$information_id . "'");
  192. }
  193. public function changeDescriptions($information_id, $data){
  194. $this->db->query("DELETE FROM " . DB_PREFIX . "information_description WHERE information_id = '" . (int)$information_id . "'");
  195. $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'information_id=" . (int)$information_id. "'");
  196. foreach ($data['information_description'] as $language_id => $value) {
  197. $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_keyword = '" . $this->db->escape($value['meta_keyword']) . "', meta_description = '" . $this->db->escape($value['meta_description']) . "', seo_title = '" . $this->db->escape($value['seo_title']) . "', seo_h1 = '" . $this->db->escape($value['seo_h1']) . "'");
  198. }
  199. if ($data['keyword']) {
  200. $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'information_id=" . (int)$information_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
  201. }
  202. $this->cache->delete('information');
  203. }
  204. public function changeInformationData($information_id, $data){
  205. $this->db->query("UPDATE " . DB_PREFIX . "information SET top = '" . (isset($data['top']) ? (int)$data['top'] : 0) . "', bottom = '" . (isset($data['bottom']) ? (int)$data['bottom'] : 0) . "' WHERE information_id = '" . (int)$information_id . "'");
  206. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_store WHERE information_id = '" . (int)$information_id . "'");
  207. if (isset($data['information_store'])) {
  208. foreach ($data['information_store'] as $store_id) {
  209. $this->db->query("INSERT INTO " . DB_PREFIX . "information_to_store SET information_id = '" . (int)$information_id . "', store_id = '" . (int)$store_id . "'");
  210. }
  211. }
  212. $this->db->query("DELETE FROM " . DB_PREFIX . "information_to_layout WHERE information_id = '" . (int)$information_id . "'");
  213. if (isset($data['information_layout'])) {
  214. foreach ($data['information_layout'] as $store_id => $layout) {
  215. if ($layout['layout_id']) {
  216. $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['layout_id'] . "'");
  217. }
  218. }
  219. }
  220. $this->cache->delete('information');
  221. }
  222. }
  223. ?>