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

/shop/models/shop_items_m.php

https://github.com/ekoisa/pyrocms-shop
PHP | 297 lines | 226 code | 40 blank | 31 comment | 48 complexity | e48772c40822111c985f8c3210aea943 MD5 | raw file
  1. <?php defined('BASEPATH') or exit('No direct script access allowed');
  2. /**
  3. * @author Anatoly Khelmer
  4. modified by Eko Muhammad Isa
  5. */
  6. class Shop_Items_m extends MY_Model {
  7. public function __construct() {
  8. parent::__construct();
  9. }
  10. public function get_all($base_where = array())
  11. {
  12. $where = '';
  13. if (!empty($base_where)) {
  14. $where = 'where ';
  15. $i = 0;
  16. foreach($base_where as $name => $value) {
  17. $where .= ($i) ? ' and ' : '';
  18. if ($name == 'status') {
  19. if ($value == 'draft') $value = 0;
  20. else $value = 1;
  21. }
  22. // Need to use "like" if it is a name
  23. if ($name == 'name') {
  24. $value = '%'. $value. '%';
  25. $where .= "{$name} like {$this->db->escape($value)}";
  26. }
  27. else {
  28. $where .= "{$name} = {$this->db->escape($value)}";
  29. $i++;
  30. }
  31. }
  32. }
  33. $query = "select * from `".$this->db->dbprefix('shop_items')."` $where;";
  34. $sql = $this->db->query($query);
  35. return $sql;
  36. }
  37. public function get_all_in_cat($cat_id)
  38. {
  39. $query = "select a.`id`, a.`name`, a.`manufacturer`, a.`category`, a.`description`, a.`price`, a.`options`, a.`status`, a.`postdate`, b.`image_name` from `".$this->db->dbprefix('shop_items')."` as a
  40. left outer join `".$this->db->dbprefix('shop_images')."` as b on a.`id` = b.`id_item` and b.`is_default` = 1
  41. where a.category={$this->db->escape($cat_id)};";
  42. $sql = $this->db->query($query);
  43. return $sql;
  44. }
  45. public function get_lastest($rowreturn = 5)
  46. {
  47. $query = "select a.`id`, a.`name`, a.`manufacturer`, a.`category`, a.`description`, a.`price`, a.`options`, a.`status`, a.`postdate`, b.`image_name`, c.`name` as cat_name from `".$this->db->dbprefix('shop_items')."` as a
  48. left outer join `".$this->db->dbprefix('shop_images')."` as b on a.`id` = b.`id_item` and b.`is_default` = 1
  49. left outer join `".$this->db->dbprefix('shop_categories')."` as c on a.`category` = c.`id`
  50. where a.`status` = 1 order by postdate desc limit 0, ".$rowreturn." ";
  51. $sql = $this->db->query($query);
  52. if($sql->num_rows() > 0){
  53. return $sql->result();
  54. }else{
  55. return array();
  56. }
  57. }
  58. public function get($id)
  59. {
  60. $query = "select a.`id`, a.`name`, a.`manufacturer`, a.`category`, a.`description`, a.`price`, a.`options`, a.`status`, a.`postdate`, c.`name` as cat_name from `".$this->db->dbprefix('shop_items')."` as a
  61. left outer join `".$this->db->dbprefix('shop_categories')."` as c on a.`category` = c.`id`
  62. where a.id={$this->db->escape($id)};";
  63. $sql = $this->db->query($query);
  64. $row = $sql->row();
  65. return $row;
  66. }
  67. public function get_options($id=0)
  68. {
  69. $query = "select * from `".$this->db->dbprefix('shop_item_options')."` where item_id={$this->db->escape($id)};";
  70. $sql = $this->db->query($query);
  71. return $sql;
  72. }
  73. /**
  74. * Get all values for option with id specified
  75. *
  76. * @param int $option_id
  77. * @return sql query result
  78. */
  79. public function get_option_values($option_id=0)
  80. {
  81. $query = "select * from `".$this->db->dbprefix('shop_item_option_values')."` where option_id={$this->db->escape($option_id)};";
  82. $sql = $this->db->query($query);
  83. return $sql;
  84. }
  85. public function get_option_value($value_id=0)
  86. {
  87. $query = "select * from `".$this->db->dbprefix('shop_item_option_values')."` where id={$this->db->escape($value_id)};";
  88. $sql = $this->db->query($query);
  89. $row = $sql->row();
  90. return $row;
  91. }
  92. public function search($word)
  93. {
  94. $word = '%'.$word.'%';
  95. $query = "select * from `".$this->db->dbprefix('shop_items')."` where name like {$this->db->escape($word)};";
  96. $sql = $this->db->query($query);
  97. return $sql;
  98. }
  99. public function create($params)
  100. {
  101. if ($params['status'] == 'draft') $status = 0;
  102. else $status = 1;
  103. $name = $this->db->escape($params['title']);
  104. $price = $this->db->escape($params['price']);
  105. $category = $this->db->escape($params['category']);
  106. $gallery = $this->db->escape($params['gallery']);
  107. $status = $this->db->escape($status);
  108. $description = $this->db->escape($params['description']);
  109. $manufacturer = $this->db->escape($params['manufacturer']);
  110. // Let's start from items
  111. $query = "insert into `".$this->db->dbprefix('shop_items')."` (name, price, category, gallery, status, description, manufacturer)
  112. values ($name, $price, $category, $gallery, $status, $description, $manufacturer);";
  113. $sql = $this->db->query($query);
  114. if ($sql == false) return false;
  115. $item_id = $this->db->insert_id();
  116. // Now options if we have
  117. // Option name first of all
  118. if (isset($params['option_name']) && count($params['option_name']) != 0) {
  119. // Here we need loop over all options we have
  120. foreach ($params['option_name'] as $id=>$option_name) {
  121. $query = "insert into `".$this->db->dbprefix('shop_item_options')."` (name, item_id) values ({$this->db->escape($option_name)}, {$this->db->escape($item_id)});";
  122. $sql = $this->db->query($query);
  123. $item_option_id = $this->db->insert_id();
  124. if ($sql == false) return false;
  125. // And option values
  126. if (isset($params['option' .$id. '_value'])) {
  127. foreach ($params['option' .$id. '_value'] as $option_value_id => $value) {
  128. $query = "insert into `".$this->db->dbprefix('shop_item_option_values')."` (option_id, value) values ({$this->db->escape($item_option_id)}, {$this->db->escape($value)});";
  129. $sql = $this->db->query($query);
  130. if ($sql == false) return false;
  131. }
  132. }
  133. }
  134. }
  135. return TRUE;
  136. }
  137. public function createnew($params)
  138. {
  139. if ($params['status'] == 'draft') $status = 0;
  140. else $status = 1;
  141. $name = $params['title'];
  142. $price = $params['price'];
  143. $category = $params['category'];
  144. $status = $status;
  145. $description = $params['description'];
  146. $manufacturer = $params['manufacturer'];
  147. $hourpost = (intval($params['hourpost']) > 23) ? 23 : intval($params['hourpost']);
  148. $minutepost = (intval($params['minutepost']) > 59) ? 59 : intval($params['minutepost']);
  149. $datepost = $params['datepost'] . ' ' . $hourpost.':'.$minutepost.':00';
  150. if(strlen($params['datepost']) < 8){
  151. $datepost = date("YYYY-mm-dd H:i:s");
  152. }
  153. // Let's start from items
  154. $arPrm = array($name, $price, $category, $status, $description, $manufacturer, $datepost);
  155. $query = "insert into `".$this->db->dbprefix('shop_items')."` (name, price, category, status, description, manufacturer, postdate)
  156. values (?, ?, ?, ?, ?, ?, ?);";
  157. $sql = $this->db->query($query, $arPrm);
  158. if ($sql == false) return false;
  159. $item_id = $this->db->insert_id();
  160. return $item_id;
  161. }
  162. public function delete($id)
  163. {
  164. $query = "delete from `".$this->db->dbprefix('shop_items')."` where id={$this->db->escape($id)};";
  165. $sql = $this->db->query($query);
  166. return $sql;
  167. }
  168. /**
  169. * Edit Shop Item
  170. *
  171. * @param int $id - the id of the item we need to edit
  172. * @param array $params - all item parameters
  173. * @return boolean
  174. */
  175. public function edit($id, $params)
  176. {
  177. if ($params['status'] == 'draft') $status = 0;
  178. else $status = 1;
  179. $name = $this->db->escape($params['title']);
  180. $price = $this->db->escape($params['price']);
  181. $category = $this->db->escape($params['category']);
  182. $gallery = $this->db->escape($params['gallery']);
  183. $status = $this->db->escape($status);
  184. $description = $this->db->escape($params['description']);
  185. $manufacturer = $this->db->escape($params['manufacturer']);
  186. // Let's start from items
  187. $query = "update `".$this->db->dbprefix('shop_items')."` set name = $name,
  188. price = $price,
  189. category = $category,
  190. gallery = $gallery,
  191. status = $status,
  192. manufacturer = $manufacturer,
  193. description = ? where id={$this->db->escape($id)};";
  194. $sql = $this->db->query($query, array($description));
  195. if ($sql == false) return false;
  196. // Delete all options and write new ones
  197. $options = $this->get_options($id);
  198. foreach ($options->result() as $option) {
  199. $query = "delete from `".$this->db->dbprefix('shop_item_options')."` where id = {$this->db->escape($option->id)};";
  200. $sql = $this->db->query($query);
  201. }
  202. $item_id = $id;
  203. // Now options if we have
  204. // Option name first of all
  205. if (isset($params['option_name']) && count($params['option_name']) != 0) {
  206. // Here we need loop over all options we have
  207. foreach ($params['option_name'] as $id =>$option_name) {
  208. $id++;
  209. $query = "insert into `".$this->db->dbprefix('shop_item_options')."` (name, item_id) values ({$this->db->escape($option_name)},
  210. {$this->db->escape($item_id)} );";
  211. $sql = $this->db->query($query);
  212. if ($sql == false) return false;
  213. $item_option_id = $this->db->insert_id();
  214. // And option values (option1_value[])
  215. if (isset($params['option' .$id. '_value'])) {
  216. foreach ($params['option' .$id. '_value'] as $option_value_id => $value) {
  217. $query = "insert into `".$this->db->dbprefix('shop_item_option_values')."` (option_id, value) values ({$this->db->escape($item_option_id)},
  218. {$this->db->escape($value)});";
  219. $sql = $this->db->query($query);
  220. if ($sql == false) return false;
  221. }
  222. }
  223. }
  224. }
  225. return TRUE;
  226. }
  227. public function edit_ax_data($id, $params)
  228. {
  229. if ($params['status'] == 'draft') $status = 0;
  230. else $status = 1;
  231. $name = $this->db->escape($params['title']);
  232. $price = $this->db->escape($params['price']);
  233. $category = $this->db->escape($params['category']);
  234. $status = $this->db->escape($status);
  235. $description = $this->db->escape($params['description']);
  236. $manufacturer = $this->db->escape($params['manufacturer']);
  237. $hourpost = (intval($params['hourpost']) > 23) ? 23 : intval($params['hourpost']);
  238. $minutepost = (intval($params['minutepost']) > 59) ? 59 : intval($params['minutepost']);
  239. $datepost = $params['datepost'] . ' ' . $hourpost.':'.$minutepost.':00';
  240. if(strlen($params['datepost']) < 8){
  241. $datepost = date("YYYY-mm-dd H:i:s");
  242. }
  243. $datepost = $this->db->escape($datepost);
  244. // Let's start from items
  245. $query = "update `".$this->db->dbprefix('shop_items')."` set name = $name,
  246. price = $price,
  247. category = $category,
  248. status = $status,
  249. manufacturer = $manufacturer,
  250. description = $description,
  251. postdate = $datepost
  252. where id={$this->db->escape($id)};";
  253. $sql = $this->db->query($query);
  254. if ($sql == false) return false;
  255. return TRUE;
  256. }
  257. }