/modules/shop/classes/model/goods.php

https://bitbucket.org/seyar/kinda.local · PHP · 1153 lines · 947 code · 148 blank · 58 comment · 108 complexity · c6a68bafab38e86c5ab10fdb19010839 MD5 · raw file

  1. <?php
  2. defined( 'SYSPATH' ) OR die( 'No direct access allowed.' );
  3. class Model_Goods extends Model
  4. {
  5. public $images_folder = '';
  6. static function statuses()
  7. {
  8. $statuses = array( '0' => I18n::get( 'Any' ) );
  9. foreach( Controller_Goods::$goods_status_list as $value )
  10. $statuses[$value] = I18n::get( ucfirst( str_replace( '_', ' ', $value ) ) );
  11. return $statuses;
  12. }
  13. static function getFilter( $lang_id = 1 )
  14. {
  15. $filter = (array) json_decode( Cookie::get( 'filter_' . MODULE_NAME, '' ), TRUE );
  16. if( isset( $_POST['cat'] ) )
  17. $_POST['filter']['cat'] = $_POST['cat']; // because custom select not working with []
  18. if( isset( $_POST['filter_price_categories_id']) )
  19. $_POST['filter']['price_categories_id'] = $_POST['filter_price_categories_id']; // because custom select not working with []
  20. if( isset( $_POST['status']) )
  21. $_POST['filter']['status'] = $_POST['status']; // because custom select not working with []
  22. if( isset( $_POST['filter'] ) )
  23. {
  24. $filter = $_POST['filter'];
  25. Cookie::set( 'filter_' . MODULE_NAME, json_encode( $_POST['filter'] ) );
  26. }
  27. $where = array( );
  28. if( count( $filter ) > 0 ):
  29. foreach( $filter as $key => $item )
  30. {
  31. if( !empty( $item ) )
  32. {
  33. switch( $key )
  34. {
  35. case 'cat':
  36. $cat = (int) $filter['cat'];
  37. $cats = Model_Categories::findChildrenRecurse( $lang_id, $cat );
  38. if( count( $cats ) > 0 )
  39. {
  40. $where[] = array( 'categories_id', 'IN', DB::expr( '(' . implode( ', ', $cats ) . ')' ) );
  41. }else
  42. {
  43. $where[] = array( 'categories_id', '=', $cat );
  44. }
  45. /**/
  46. break;
  47. case 'shop_articul':
  48. $where[] = array( 'scu', 'LIKE', "%$item%" );
  49. break;
  50. case 'name':
  51. $where[] = array( 'shop_goods.name', 'LIKE', "%$item%" );
  52. break;
  53. case 'shop_col_ot':
  54. $where[] = array( 'quantity', '>=', $item );
  55. break;
  56. case 'shop_col_do':
  57. $where[] = array( 'shop_prices', '<=', $item );
  58. break;
  59. case 'shop_price_ot':
  60. $where[] = array( 'shop_prices', '>=', $item );
  61. break;
  62. case 'shop_price_do':
  63. $where[] = array( 'shop_prices', '<=', $item );
  64. break;
  65. case 'default_photo':
  66. $where[] = array( 'default_photo', '=', '' );
  67. // $where[] = array( 'default_photo', 'IS', DB::expr('NULL') );
  68. break;
  69. case 'id':
  70. $where[] = array( 'shop_goods.id', '=', $item );
  71. break;
  72. case 'status':
  73. $where[] = array( 'shop_goods.status', '=', $item );
  74. break;
  75. default:
  76. $where[] = array( $key, '=', $item );
  77. }
  78. }
  79. }
  80. endif;
  81. return array( $where, $filter );
  82. }
  83. static public function product_create()
  84. {
  85. $post = $_POST;
  86. if( !isset( $_POST['is_bestseller'] ) )
  87. {
  88. $post['is_bestseller'] = 0;
  89. }
  90. if( !isset( $_POST['is_new'] ) )
  91. {
  92. $post['is_new'] = 0;
  93. }
  94. if( !isset( $_POST['show_on_startpage'] ) )
  95. {
  96. $post['show_on_startpage'] = 0;
  97. }
  98. //echo (Kohana::debug($post));
  99. list($id, $total_rows) = DB::insert( 'shop_goods', array(
  100. 'categories_id',
  101. 'name', 'alter_name',
  102. 'scu', 'short_description',
  103. 'full_description', 'rating', 'weight',
  104. 'is_bestseller', 'is_new',
  105. 'status', 'show_on_startpage'
  106. )
  107. )
  108. ->values(
  109. array(
  110. $post['categories_id'],
  111. $post['name'], $post['alter_name'],
  112. $post['scu'], $post['short_description'],
  113. $post['full_description'], $post['rating'],
  114. $post['weight'], $post['is_bestseller'],
  115. $post['is_new'], $post['status'],
  116. $post['show_on_startpage']
  117. )
  118. )
  119. ->execute()
  120. ;
  121. //echo($id);
  122. return $id;
  123. }
  124. static public function product_load( $id = NULL )
  125. {
  126. $query = DB::select( 'id', 'alter_name', 'name', 'short_description', 'full_description', 'is_bestseller', 'is_new', 'show_on_startpage', 'scu', 'weight', 'rating', 'status', 'brand_id', 'categories_id', 'seo_url', 'seo_description', 'seo_keywords'
  127. )
  128. ->where( 'id', '=', $id )
  129. ->from( 'shop_goods' );
  130. $result = $query->execute()->as_array();
  131. $query_b = DB::select( 'id', 'name' )
  132. ->from( 'shop_brands' );
  133. $result_b = $query_b
  134. ->execute()
  135. ->as_array();
  136. $query_c = DB::select( 'id', 'name' )->from( 'shop_categories' );
  137. $result_c = $query_c
  138. ->execute()
  139. ->as_array();
  140. return array( $result, $result_b, $result_c );
  141. }
  142. public function show( $languages_id = NULL )
  143. {
  144. if( is_numeric( $languages_id ) )
  145. {
  146. // Set and save rows_per_page value
  147. $rows_per_page = ((int) $_POST['rows_num']) ? ((int) $_POST['rows_num']) : (int) cookie::get( 'rows_per_page_' . MODULE_NAME . '_' . MODULE_SUBCONTROLLER, 10 );
  148. cookie::set( 'rows_per_page_' . MODULE_NAME . '_' . MODULE_SUBCONTROLLER, $rows_per_page );
  149. $_POST['rows_num'] = $rows_per_page;
  150. $query_c = DB::select( DB::expr( 'COUNT(*) AS mycount' ) )
  151. ->from( array( 'shop_goods', 'goods' ) )
  152. ->join( array( 'shop_prices', 'prices' ), 'LEFT' )
  153. ->on( 'goods.id', '=', 'prices.good_id' )
  154. ->join( array( 'shop_categories', 'c' ) )
  155. ->on( 'goods.categories_id', '=', 'c.id' )
  156. ->where( 'prices.price_categories_id', '=', Kohana_Config::instance()->load( 'shop' )->default_category )
  157. ->and_where( 'prices.currencies_id', '=', Kohana_Config::instance()->load( 'shop' )->default_currency )
  158. ->and_where( 'c.languages_id', '=', $languages_id )
  159. ;
  160. $query = DB::select( 'goods.*', array( 'categories.name', 'category_name' ), array( 'prices.price', 'price' ) )
  161. ->from( array( 'shop_goods', 'goods' ) )
  162. ->join( array( 'shop_categories', 'categories' ), 'LEFT' )
  163. ->on( 'categories.id', '=', 'goods.categories_id' )
  164. ->join( array( 'shop_prices', 'prices' ), 'LEFT' )
  165. ->on( 'goods.id', '=', 'prices.good_id' )
  166. ->where( 'prices.price_categories_id', '=', Kohana_Config::instance()->load( 'shop' )->default_category )
  167. ->and_where( 'prices.currencies_id', '=', Kohana_Config::instance()->load( 'shop' )->default_currency )
  168. ->and_where( 'categories.languages_id', '=', $languages_id )
  169. ;
  170. ////////////////////////////
  171. // begin filters
  172. ////////////////////////////
  173. // Set and save SCU
  174. $good_scu = (isset( $_POST['search_good_scu'] ) ? $_POST['search_good_scu'] : Session::instance()->get( 'search_good_scu', '' ) );
  175. Session::instance()->set( 'search_good_scu', $good_scu );
  176. $_POST['search_good_scu'] = $good_scu;
  177. if( $_POST['search_good_scu'] )
  178. {
  179. $query_c->and_where( 'goods.scu', '=', $_POST['search_good_scu'] );
  180. $query->and_where( 'goods.scu', '=', $_POST['search_good_scu'] );
  181. }
  182. // Set and save Name
  183. $good_name = (isset( $_POST['search_good_name'] ) ? $_POST['search_good_name'] : Session::instance()->get( 'search_good_name', '' ) );
  184. Session::instance()->set( 'search_good_name', $good_name );
  185. $_POST['search_good_name'] = $good_name;
  186. if( $_POST['search_good_name'] )
  187. {
  188. $query_c->where_open()
  189. ->where( Db::expr( 'LCASE(goods.name)' ), 'LIKE', "%" . mb_strtolower( $_POST['search_good_name'], 'UTF-8' ) . "%" )
  190. ->or_where( Db::expr( 'LCASE(goods.alter_name)' ), 'LIKE', "%" . mb_strtolower( $_POST['search_good_name'], 'UTF-8' ) . "%" )
  191. ->where_close();
  192. $query->where_open()
  193. ->where( Db::expr( 'LCASE(goods.name)' ), 'LIKE', "%" . mb_strtolower( $_POST['search_good_name'], 'UTF-8' ) . "%" )
  194. ->or_where( Db::expr( 'LCASE(goods.alter_name)' ), 'LIKE', "%" . mb_strtolower( $_POST['search_good_name'], 'UTF-8' ) . "%" )
  195. ->where_close();
  196. }
  197. // Set and save filter for cat ID
  198. $good_cat = (isset( $_POST['search_good_cat'] ) ? $_POST['search_good_cat'] : Session::instance()->get( 'search_good_cat', Kohana::config( 'shop' )->root_category_id ) );
  199. Session::instance()->set( 'search_good_cat', $good_cat );
  200. $_POST['search_good_cat'] = $good_cat;
  201. if( $_POST['search_good_cat'] && $_POST['search_good_cat'] != Kohana::config( 'shop' )->root_category_id )
  202. {
  203. // Set and save filter for cat ID
  204. $all_cats = Model_Categories::findChildrenRecurse( $languages_id, $_POST['search_good_cat'], $result );
  205. $all_cats = count( $all_cats ) != 0 ? $all_cats : array( $_POST['search_good_cat'] );
  206. $in = '(' . implode( ',', $all_cats ) . ')';
  207. $query_c->and_where( 'categories_id', 'IN', DB::expr( $in ) );
  208. $query->and_where( 'goods.categories_id', 'IN', DB::expr( $in ) );
  209. }
  210. // Set and save Price filters
  211. $good_price_from = (isset( $_POST['search_good_price_from'] ) ? (float) $_POST['search_good_price_from'] : Session::instance()->get( 'search_good_price_from', '' ) );
  212. $good_price_from = $good_price_from ? $good_price_from : '';
  213. Session::instance()->set( 'search_good_price_from', $good_price_from );
  214. $_POST['search_good_price_from'] = $good_price_from;
  215. if( $_POST['search_good_price_from'] )
  216. {
  217. $query_c->and_where( 'prices.price', '>=', $_POST['search_good_price_from'] );
  218. $query->and_where( 'prices.price', '>=', $_POST['search_good_price_from'] );
  219. }
  220. $good_price_to = (isset( $_POST['search_good_price_to'] ) ? (float) $_POST['search_good_price_to'] : Session::instance()->get( 'search_good_price_to', '' ) );
  221. $good_price_to = $good_price_to ? $good_price_to : '';
  222. Session::instance()->set( 'search_good_price_to', $good_price_to );
  223. $_POST['search_good_price_to'] = $good_price_to;
  224. if( $_POST['search_good_price_to'] )
  225. {
  226. $query_c->and_where( 'prices.price', '<=', $_POST['search_good_price_to'] );
  227. $query->and_where( 'prices.price', '<=', $_POST['search_good_price_to'] );
  228. }
  229. $good_price_currency = (isset( $_POST['search_good_price_currency'] ) ? (int) $_POST['search_good_price_currency'] : Session::instance()->get( 'search_good_price_currency', Controller_Shop::$default_currency ) );
  230. Session::instance()->set( 'search_good_price_currency', $good_price_currency );
  231. $_POST['search_good_price_currency'] = $good_price_currency;
  232. if( $_POST['search_good_price_currency'] && ($good_price_from || $good_price_to) )
  233. {
  234. $query_c->and_where( 'prices.currencies_id', '=', $_POST['search_good_price_currency'] );
  235. $query->and_where( 'prices.currencies_id', '=', $_POST['search_good_price_currency'] );
  236. }else if( $good_price_from || $good_price_to )
  237. {
  238. $query_c->where( 'prices.currencies_id', '=', Kohana::config( 'shop' )->default_currency );
  239. $query->where( 'prices.currencies_id', '=', Kohana::config( 'shop' )->default_currency );
  240. }
  241. // Set and save Quantity
  242. $good_quantity_from = (isset( $_POST['search_good_quantity_from'] ) ? (int) $_POST['search_good_quantity_from'] : Session::instance()->get( 'search_good_quantity_from', '' ) );
  243. $good_quantity_from = $good_quantity_from ? $good_quantity_from : '';
  244. Session::instance()->set( 'search_good_quantity_from', $good_quantity_from );
  245. $_POST['search_good_quantity_from'] = $good_quantity_from;
  246. if( $_POST['search_good_quantity_from'] )
  247. {
  248. $query_c->and_where( 'goods.quantity', '>=', $_POST['search_good_quantity_from'] );
  249. $query->and_where( 'goods.quantity', '>=', $_POST['search_good_quantity_from'] );
  250. }
  251. $good_quantity_to = (isset( $_POST['search_good_quantity_to'] ) ? (int) $_POST['search_good_quantity_to'] : Session::instance()->get( 'search_good_quantity_to', '' ) );
  252. $good_quantity_to = $good_quantity_to ? $good_quantity_to : '';
  253. Session::instance()->set( 'search_good_quantity_to', $good_quantity_to );
  254. $_POST['search_good_quantity_to'] = $good_quantity_to;
  255. if( $_POST['search_good_quantity_to'] )
  256. {
  257. $query_c->and_where( 'goods.quantity', '<=', $_POST['search_good_quantity_to'] );
  258. $query->and_where( 'goods.quantity', '<=', $_POST['search_good_quantity_to'] );
  259. }
  260. // Set and save Status
  261. $good_status = (isset( $_POST['search_good_status'] ) ? $_POST['search_good_status'] : Session::instance()->get( 'search_good_status', '' ) );
  262. Session::instance()->set( 'search_good_status', $good_status );
  263. $_POST['search_good_status'] = $good_status;
  264. if( $_POST['search_good_status'] )
  265. {
  266. $query_c->and_where( 'goods.status', '=', $_POST['search_good_status'] );
  267. $query->and_where( 'goods.status', '=', $_POST['search_good_status'] );
  268. }
  269. // Set and save default photo filter
  270. $good_default_photo = (isset( $_POST['search_default_photo'] ) ? $_POST['search_default_photo'] : Session::instance()->get( 'search_default_photo', '' ) );
  271. Session::instance()->set( 'search_default_photo', $good_default_photo );
  272. $_POST['search_default_photo'] = $good_default_photo;
  273. if( $_POST['search_default_photo'] == 1 )
  274. {
  275. $query_c->and_where( DB::expr( 'TRIM(goods.default_photo)' ), '!=', '' );
  276. $query->and_where( DB::expr( 'TRIM(goods.default_photo)' ), '!=', '' );
  277. }elseif( $_POST['search_default_photo'] == 0 )
  278. {
  279. $query_c->and_where( DB::expr( 'TRIM(goods.default_photo)' ), '=', '' );
  280. $query->and_where( DB::expr( 'TRIM(goods.default_photo)' ), '=', '' );
  281. }
  282. ////////////////////////////
  283. // end filters
  284. ////////////////////////////
  285. $count = $query_c
  286. ->execute()
  287. ->get( 'mycount' );
  288. if( isset( $_POST['page'] ) )
  289. $_GET['page'] = $_POST['page'];
  290. $pagination = Pagination::factory( array(
  291. 'total_items' => $count,
  292. 'items_per_page' => $rows_per_page,
  293. ) );
  294. $result = $query
  295. ->order_by( 'id', 'ASC' )
  296. ->limit( $pagination->items_per_page )
  297. ->offset( $pagination->offset )
  298. ->execute()
  299. ;
  300. $page_links_str = '';
  301. if( $result->count() == 0 )
  302. return array( array( ), '', array( ) );
  303. else
  304. {
  305. $result = $result->as_array( 'id' );
  306. return array( $result, $page_links_str, $pagination->total_pages );
  307. }
  308. }
  309. }
  310. static public function load( $id = NULL )
  311. {
  312. if( (int) $id )
  313. {
  314. $query = DB::select()
  315. ->from( 'shop_goods' )
  316. ->limit( 1 );
  317. $query->where( 'id', '=', $id );
  318. $result = $query->execute();
  319. if( $result->count() == 0 )
  320. return array( );
  321. else
  322. {
  323. $return = $result->current();
  324. $prices = DB::select()
  325. ->from( array( 'shop_prices', 'prices' ) )
  326. ->where( 'good_id', '=', $return['id'] )
  327. ->order_by( 'currencies_id', 'ASC' )
  328. ->cached( 10 )
  329. ->execute()
  330. // ->as_array('currencies_id')
  331. ->as_array()
  332. ;
  333. $curr = new Model_Frontend_Currencies();
  334. $return['prices'] = $curr->getPrice( $prices );
  335. $return['oldprices'] = $curr->getPrice( $prices, 'oldprice' );
  336. $add_fields = DB::select( 'ser_addFields' )
  337. ->from( 'shop_categories' )
  338. ->where( 'id', '=', $return['categories_id'] )
  339. ->cached( 30 )
  340. ->execute()
  341. ->current()
  342. ;
  343. $return['ser_addFields'] = unserialize( $add_fields['ser_addFields'] );
  344. $return['add_fields'] = DB::query( Database::SELECT, "SELECT `f`.`id` AS `f_id`, `f`.`alias` AS `f_alias`, `f`.`is_group` AS `f_group`, `f`.`name`, `f`.`type`, `f`.`desc`, `f`.`defaults`, v.* "
  345. . " FROM `shop_goods_properties` AS `f` "
  346. . " LEFT OUTER JOIN "
  347. . "(SELECT * FROM `shop_goods_properties_values` AS v1 WHERE v1.good_id = {$return['id']} ) "
  348. . " AS `v` ON (`f`.`id`=`v`.`properties_category_id`) "
  349. . " WHERE f.category_id = {$return['categories_id']}"
  350. . " ORDER BY `f_alias` "
  351. )
  352. // ->cached( 30 )
  353. ->execute()
  354. ->as_array()
  355. ;
  356. return $return;
  357. }
  358. }else
  359. {
  360. return array( 'parent_id' => Session::instance()->get( 'search_good_cat' ) );
  361. }
  362. }
  363. public function save( $lang, $id = NULL )
  364. {
  365. if( $post = Model_Goods::validate() )
  366. {
  367. $search = array( '%23', '%3F', '%2F', '%26', '.', ',', 'quot','amp', '/','"',"'",'%27',';','%2C','%28','%29','%3B','&','%2B','(',')', );
  368. // $replace = array('#','?','/','&', "'",'/', '"');
  369. $replace = array( '', '', '', '', '', '','_','_','_', '', '','','','','(',')','','','','','' );
  370. $post['seo_url'] = $post['seo_url'] ? str_replace( $search, $replace, urlencode($post['seo_url']) ) : str_replace( $search, $replace, Controller_Admin::to_url( $post['name'] ));
  371. $post['seo_url'] = str_replace( '__', '_', $post['seo_url'] );
  372. $seourl = strtolower( str_replace( '__', '_', $post['seo_url'] ));
  373. if( (int)$id )
  374. {
  375. // update
  376. $id = (int)$id;
  377. DB::update( 'shop_goods' )
  378. ->set(
  379. array(
  380. 'categories_id' => $post['categories_id'],
  381. 'scu' => $post['scu'],
  382. 'name' => $post['name'],
  383. 'brand_id' => $post['brand_id'],
  384. 'alter_name' => $post['alter_name'],
  385. 'short_description' => $post['short_description'],
  386. 'full_description' => $post['full_description'],
  387. 'is_bestseller' => $post['is_bestseller'],
  388. 'is_new' => $post['is_new'],
  389. 'rating' => $post['rating'],
  390. 'show_on_startpage' => $post['show_on_startpage'],
  391. 'cost' => $post['cost'],
  392. 'weight' => $post['weight'],
  393. 'quantity' => $post['quantity'],
  394. 'guaranties_period' => $post['guaranties_period'],
  395. 'store_approximate_date' => (strtotime( $post['store_approximate_date'] ) ? $post['store_approximate_date'] : NULL),
  396. 'seo_keywords' => $post['seo_keywords'],
  397. 'seo_description' => $post['seo_description'],
  398. 'seo_url' => $seourl,
  399. 'status' => $post['status'],
  400. 'parsed' => $post['parsed'],
  401. 'format' => $post['format'],
  402. )
  403. )
  404. ->where( 'id', '=', $id )
  405. ->execute()
  406. ;
  407. }else
  408. { // create
  409. $id = $total_rows = 0;
  410. list($id, $total_rows) = DB::insert( 'shop_goods', array(
  411. 'categories_id',
  412. 'scu',
  413. 'name',
  414. 'brand_id',
  415. 'alter_name',
  416. 'short_description',
  417. 'full_description',
  418. 'is_bestseller',
  419. 'is_new',
  420. 'rating',
  421. 'show_on_startpage',
  422. 'cost',
  423. 'weight',
  424. 'quantity',
  425. 'guaranties_period',
  426. 'store_approximate_date',
  427. 'seo_keywords',
  428. 'seo_description',
  429. 'seo_url',
  430. 'status',
  431. 'format',
  432. )
  433. )
  434. ->values(
  435. array(
  436. $post['categories_id'],
  437. $post['scu'],
  438. $post['name'],
  439. $post['brand_id'],
  440. Model_Content::arrGet($post, 'alter_name', $post['name']),
  441. $post['short_description'],
  442. $post['full_description'],
  443. $post['is_bestseller'],
  444. $post['is_new'],
  445. $post['rating'],
  446. $post['show_on_startpage'],
  447. $post['cost'],
  448. $post['weight'],
  449. $post['quantity'],
  450. $post['guaranties_period'],
  451. (strtotime( $post['store_approximate_date'] ) ? $post['store_approximate_date'] : NULL),
  452. $post['seo_keywords'],
  453. $post['seo_description'],
  454. $seourl,
  455. $post['status'],
  456. $post['format'],
  457. )
  458. )
  459. ->execute();
  460. }
  461. // Prices update
  462. if( method_exists( 'Model_Currencies', 'prices_update' ) )
  463. {
  464. Model_Currencies::prices_update( $id, 'price', $post );
  465. Model_Currencies::prices_update( $id, 'oldprice', $post );
  466. }
  467. Model_Goods::save_add_filelds( $lang, $id, $post );
  468. // save rec goods
  469. $addGoodsModel = Kohana::config( 'shop' )->addGoodsModel;
  470. $addGoodsMethod = Kohana::config( 'shop' )->addGoodsMethSave;
  471. if( isset( $addGoodsModel ) && !empty( $addGoodsModel ) && method_exists( $addGoodsModel, $addGoodsMethod ) )
  472. call_user_func( array( $addGoodsModel, $addGoodsMethod ), $id, $post, 'goodId' );
  473. if( isset($post['goodgroups_scu']) && count($post['goodgroups_scu']) > 0 )
  474. {
  475. $this->saveGoodGroups($post, $id);
  476. }
  477. return $id;
  478. }
  479. else
  480. {
  481. return false;
  482. }
  483. }
  484. static public function save_add_filelds( $lang = NULL, $id = NULL, $post = array( ) )
  485. {
  486. if( !$lang OR !$id )
  487. return false;
  488. $result = array( );
  489. $rows = DB::select()
  490. ->from( 'shop_goods_properties' )
  491. ->where( 'category_id', '=', $post['categories_id'] )
  492. ->execute()
  493. ->as_array()
  494. ;
  495. foreach( $rows as $value )
  496. {
  497. if( isset($_POST['add_fields'][$value['id']]) )
  498. {
  499. $result[$value['id']] = array(
  500. 'name' => $value['name'],
  501. 'type' => $value['type'],
  502. );
  503. }else
  504. {
  505. $query = Db::delete( 'shop_goods_properties_values' )
  506. ->where( 'good_id', '=', $id )
  507. ->and_where( 'properties_category_id', '=', (int)@$_POST['add_fields_id'][$value['id']] )
  508. ;
  509. $query->execute();
  510. }
  511. }
  512. foreach( $result as $key => $value )
  513. {
  514. $insert = array( );
  515. $null = DB::expr( 'NULL' );
  516. if( !$_POST['add_fields_id'][$key] )
  517. {
  518. $query = Db::query( Database::INSERT
  519. , " INSERT INTO shop_goods_properties_values "
  520. . " SET "
  521. . " good_id = $id "
  522. . " , properties_category_id = :add_fld_id "
  523. . " , value_int = :value_int "
  524. . " , value_float = :value_float "
  525. . " , value_varchar = :value_varchar "
  526. . " , value_text = :value_text "
  527. . " , value_date = :value_date "
  528. )
  529. ->bind( ':add_fld_id', $key )
  530. ->bind( ':value_int', $null )
  531. ->bind( ':value_float', $null )
  532. ->bind( ':value_varchar', $null )
  533. ->bind( ':value_text', $null )
  534. ->bind( ':value_date', $null )
  535. ;
  536. }
  537. else
  538. $query = Db::query( Database::UPDATE
  539. , " UPDATE shop_goods_properties_values "
  540. . " SET "
  541. . " good_id = $id "
  542. . " , properties_category_id = :add_fld_id "
  543. . " , value_int = :value_int "
  544. . " , value_float = :value_float "
  545. . " , value_varchar = :value_varchar "
  546. . " , value_text = :value_text "
  547. . " , value_date = :value_date "
  548. . " WHERE id = " . $_POST['add_fields_id'][$key]
  549. )
  550. ->bind( ':add_fld_id', $key )
  551. ->bind( ':value_int', $null )
  552. ->bind( ':value_float', $null )
  553. ->bind( ':value_varchar', $null )
  554. ->bind( ':value_text', $null )
  555. ->bind( ':value_date', $null )
  556. ;
  557. switch( $value['type'] )
  558. {
  559. case 'int':
  560. $query->bind( ':value_int', $_POST['add_fields'][$key] );
  561. break;
  562. case 'boolean':
  563. if(is_array($_POST['add_fields'][$key]))
  564. $query->bind( ':value_text', implode('|',$_POST['add_fields'][$key]) );
  565. break;
  566. case 'float':
  567. $query->bind( ':value_float', $_POST['add_fields'][$key] );
  568. break;
  569. case 'varchar':
  570. $query->bind( ':value_varchar', $_POST['add_fields'][$key] );
  571. break;
  572. case 'text':
  573. case 'select':
  574. $query->bind( ':value_text', $_POST['add_fields'][$key] );
  575. break;
  576. case 'date':
  577. $query->bind( ':value_date', $_POST['add_fields'][$key] );
  578. break;
  579. default:
  580. break;
  581. }
  582. $query->execute();
  583. }
  584. return true;
  585. }
  586. public function saveGoodGroups( $post = array(), $id = NULL )
  587. {
  588. if( !isset($post['goodgroups_scu']) || count($post['goodgroups_scu']) == 0 && !$id ) return array();
  589. if($post['goodgroups_scu']):
  590. foreach($post['goodgroups_scu'] as $key => $item)
  591. {
  592. if( !empty($item) )
  593. {
  594. if( !isset($post['goodgroups'][$key]) )
  595. {// товар новый
  596. list($insertId, $totalRows) = $this->copyGood($id);
  597. DB::insert('shop_goodgroups', array('goodId','mainGoodId') )->values(array($insertId, $id))->execute();
  598. // обновление табл гудс другим артикулом.
  599. // и цен
  600. DB::update('shop_goods')->set( array('scu' => $item, 'format' => $post['goodgroups_format'][$key]) )->where('id','=', $insertId)->execute();
  601. DB::insert('shop_prices', array('price','good_id',
  602. 'price_categories_id','currencies_id') )->values(
  603. array(number_format($post['goodgroups_price'][$key],2,'.',''),$insertId,1,1) )
  604. ->execute();
  605. }
  606. else
  607. {
  608. $this->updateHiddenGood( $id, $key );
  609. DB::update('shop_goods')->set( array('scu' => $item,'format' => $post['goodgroups_format'][$key],) )->where('id','=', $key)->execute();
  610. DB::update('shop_prices')->set(
  611. array('price' => number_format($post['goodgroups_price'][$key],2,'.',''),
  612. 'price_categories_id' => 1,
  613. 'currencies_id' => 1))
  614. ->where('good_id','=',$key)
  615. ->execute();
  616. }
  617. }
  618. }
  619. endif;
  620. }
  621. static function getGoodsGroups( $id = NULL )
  622. {
  623. if( !$id ) return false;
  624. $result = DB::select('goodId',array('g.scu','scu'),array('g.format','format'), array('p.price','price') )
  625. ->from(array('shop_goodgroups','gg'))
  626. ->join(array('shop_goods','g'))
  627. ->on('gg.goodId','=','g.id')
  628. ->join(array('shop_prices','p'))
  629. ->on('gg.goodId','=','p.good_id')
  630. ->where('mainGoodId','=',$id)
  631. ->execute()
  632. ;
  633. if( $result->count() == 0 ) return array();
  634. else
  635. return $result->as_array('goodId');
  636. }
  637. public function copyGood( $from = NULL )
  638. {
  639. $columns = array('scu','format', 'categories_id', 'brand_id','alter_name','name',
  640. 'short_description','full_description','is_bestseller',
  641. 'is_new','rating','show_on_startpage','default_photo',
  642. 'start_date','edit_date','date','weight',
  643. 'quantity','guaranties_period','store_approximate_date',
  644. 'seo_title','seo_keywords','seo_description','seo_url',
  645. 'parsed');
  646. $select = DB::select( DB::expr( implode(',',$columns).', "archive"' ) )
  647. ->from('shop_goods')
  648. ->where('id','=',$from)
  649. ;
  650. $sql = "INSERT INTO shop_goods(".implode(',',$columns).", status) ".$select->__toString();
  651. $data = DB::query(Database::INSERT, $sql)->execute();
  652. $folder = MEDIAPATH.'shop/';
  653. if( !file_exists($folder.$data[0].'/') ) mkdir( $folder.$data[0].'/', 0755,true );
  654. Helper_Directory::fullCopy( $folder.$from.'/', $folder.$data[0].'/');
  655. return $data;
  656. }
  657. public function updateHiddenGood( $from = NULL, $to = NULL )
  658. {
  659. if( !$from || !$to ) return false;
  660. $columns = array('categories_id', 'brand_id','alter_name','name',
  661. 'short_description','full_description','is_bestseller',
  662. 'is_new','rating','show_on_startpage','default_photo',
  663. 'start_date','edit_date','date','weight',
  664. 'quantity','guaranties_period','store_approximate_date',
  665. 'seo_title','seo_keywords','seo_description','seo_url',
  666. 'parsed');
  667. $select = DB::select( DB::expr( implode(',',$columns)) )
  668. ->from('shop_goods')
  669. ->where('id','=',$from)
  670. ->execute()
  671. ;
  672. if( $select->count() == 0 ) return false;
  673. $data = array_combine($columns, $select->current() );
  674. DB::update('shop_goods')
  675. ->set($data)
  676. ->where('id','=', $to)
  677. ->execute()
  678. ;
  679. return array($from, $to);
  680. }
  681. public function validate()
  682. {
  683. $keys = array(
  684. 'categories_id',
  685. 'scu',
  686. 'name',
  687. 'brand_id',
  688. 'alter_name',
  689. 'short_description',
  690. 'full_description',
  691. 'is_bestseller',
  692. 'is_new',
  693. 'show_on_startpage',
  694. 'rating',
  695. 'cost',
  696. 'weight',
  697. 'quantity',
  698. 'guaranties_period',
  699. 'store_approximate_date',
  700. 'seo_keywords',
  701. 'seo_description',
  702. 'seo_url',
  703. 'status',
  704. 'price',
  705. 'oldprice',
  706. 'add_fields_id',
  707. 'add_fields',
  708. 'recgoodid',
  709. 'parsed',
  710. 'goodgroups',
  711. 'goodgroups_scu',
  712. 'goodgroups_price',
  713. 'goodgroups_format',
  714. 'format',
  715. );
  716. $params = Arr::extract( $_POST, $keys, 0 );
  717. $params['weight'] = str_replace( ",", ".", $params['weight'] );
  718. $params['cost'] = str_replace( ",", ".", $params['cost'] );
  719. $post = Validate::factory( $params )
  720. ->rule( 'categories_id', 'not_empty' )
  721. ->rule( 'categories_id', 'digit' )
  722. // ->rule('scu', 'not_empty')
  723. ->rule( 'name', 'not_empty' )
  724. ->rule( 'brand_id', 'digit' )
  725. // ->rule( 'alter_name', 'not_empty' )
  726. // ->rule('short_description', 'not_empty')
  727. ->rule( 'full_description', 'not_empty' )
  728. ->rule( 'is_bestseller', 'digit' )
  729. ->rule( 'is_new', 'digit' )
  730. ->rule( 'show_on_startpage', 'digit' )
  731. // ->rule('rating', 'decimal', array(2))
  732. // ->rule('cost', 'numeric')
  733. ->rule( 'cost', 'regex', array( "/^[0-9]+((\.|,)[0-9]{1,2})?$/" ) )
  734. ->rule( 'weight', 'regex', array( "/^[0-9]+((\.|,)[0-9]{1,3})?$/" ) )
  735. ->rule( 'quantity', 'digit' )
  736. ->rule( 'guaranties_period', 'digit' )
  737. // ->rule( 'price', 'numeric' )
  738. ->rule( 'store_approximate_date', 'date' )
  739. ->rule( 'status', 'not_empty' )
  740. ;
  741. if( $post->check() )
  742. {
  743. return $params;
  744. }else
  745. {
  746. Messages::add( $post->errors( 'validate' ) );
  747. }
  748. }
  749. public function delete( $id = NULL )
  750. {
  751. $query = DB::delete( 'shop_goods' );
  752. if( is_numeric( $id ) )
  753. {
  754. $query->where( 'id', '=', $id ); // ->limit(1)
  755. $total_rows = $query->execute();
  756. }
  757. }
  758. public function delete_list( array $array )
  759. {
  760. $query = DB::delete( 'shop_goods' );
  761. if( count( $array ) )
  762. {
  763. $query->where( 'id', 'in', array_flip( $array ) );
  764. $total_rows = $query->execute();
  765. }
  766. }
  767. // Images functions
  768. public function images_default( $file, $id = NULL )
  769. {
  770. if( is_file( $file ) )
  771. {
  772. $id = defined( 'MODULE_ID' ) ? MODULE_ID : $id;
  773. $query = DB::update( 'shop_goods' )
  774. ->set(
  775. array(
  776. 'default_photo' => basename( $_GET['name'] ),
  777. )
  778. )
  779. ->where( 'id', '=', $id );
  780. $query->execute();
  781. echo '1';
  782. }
  783. }
  784. public function images_delete( $folder, $id = NULL )
  785. {
  786. if( $id == NULL ) return false;
  787. if( isset($_GET['name']) && is_array($_GET['name']) )
  788. {
  789. foreach( $_GET['name'] as $item )
  790. {
  791. $obj_image = MEDIAPATH . "$folder/" . $id . '/' . basename( $item );
  792. if( is_file( $obj_image ) )
  793. if( unlink( $obj_image ) )
  794. {
  795. $obj_image = MEDIAPATH . "$folder/" . $id . '/prev_' . basename( $item );
  796. if( is_file( $obj_image ) )
  797. {
  798. if( unlink( $obj_image ) )
  799. echo '1';
  800. }
  801. else
  802. echo '1';
  803. }
  804. }
  805. }
  806. else
  807. {
  808. $obj_image = MEDIAPATH . "$folder/" . $id . '/' . basename( $_GET['name'] );
  809. if( is_file( $obj_image ) )
  810. if( unlink( $obj_image ) )
  811. {
  812. $obj_image = MEDIAPATH . "$folder/" . $id . '/prev_' . basename( $_GET['name'] );
  813. if( is_file( $obj_image ) )
  814. {
  815. if( unlink( $obj_image ) )
  816. echo '1';
  817. }
  818. else
  819. echo '1';
  820. }
  821. }
  822. }
  823. static public function images_show( $folder, $path='', $webpath='/', $id = NULL )
  824. {
  825. $obj = array( 'image' => '' );
  826. $id = defined( 'MODULE_ID' ) ? MODULE_ID : $id;
  827. $obj = DB::select( 'default_photo' )
  828. ->from( 'shop_goods' )
  829. ->where( 'id', '=', $id )
  830. ->limit( 1 )
  831. ->execute()
  832. ->current()
  833. ;
  834. $filesArray = array( 'path' => $path
  835. , 'webpath' => $webpath
  836. , 'files' => array( )
  837. , 'default_img' => $obj['default_photo'] );
  838. if( !file_exists( MEDIAPATH . "$folder/$id/" ) )
  839. mkdir( MEDIAPATH . "$folder/$id/", 0755, true );
  840. $obj_images_dir = dir( MEDIAPATH . "$folder/$id/" );
  841. if( $obj_images_dir )
  842. {
  843. while( false !== ($entry = $obj_images_dir->read()) )
  844. {
  845. if( $entry != '.' and $entry != '..' and !strstr( $entry, 'prev_' ) )
  846. $filesArray['files'][] = $entry;
  847. }
  848. }
  849. // set default image if it empty and there is images
  850. if( !$obj['default_photo'] AND count( $filesArray['files'] ) )
  851. {
  852. DB::update( 'shop_goods' )
  853. ->set( array( 'default_photo' => $filesArray['files'][0] ) )
  854. ->where( 'id', '=', $id )
  855. ->execute()
  856. ;
  857. $filesArray['default_img'] = $filesArray['files'][0];
  858. }
  859. return $filesArray;
  860. }
  861. public function images_upload( $folder, $width = NULL, $height = NULL, $t_width = NULL, $t_height = NULL, $id = NULL, $watermark = NULL )
  862. {
  863. if( !empty( $_FILES ) )
  864. {
  865. $tempFile = $_FILES['Filedata']['tmp_name'];
  866. $targetPath = MEDIAPATH . "$folder/$id/";
  867. $targetFile = str_replace( '//', '/', $targetPath ) . strtolower( Controller_Admin::to_url( $_FILES['Filedata']['name'] ) );
  868. if( !file_exists( MEDIAPATH . "$folder/$id" ) )
  869. @mkdir( str_replace( '//', '/', $targetPath ), 0755, true ); move_uploaded_file( $tempFile, $targetFile );
  870. $this->image = Image::factory( $targetFile );
  871. // Resize Image
  872. if( $width || $height )
  873. {
  874. if( $width < $this->image->width || $height < $this->image->height )
  875. {
  876. $this->image->resize( $width, $height, Image::AUTO )
  877. //->crop($width, $height)
  878. ;
  879. } $this->image->save( $targetFile, 90 );
  880. }
  881. // Generate Thumbnail
  882. if( $width || $height )
  883. {
  884. $this->image->resize( $t_width, $t_height, Image::AUTO )->crop( $t_width, $t_height );
  885. $this->image->save( dirname( $targetFile ) . '/prev_' . basename( $targetFile ), 90 );
  886. }
  887. if( $watermark !== null && file_exists( $watermark ) )
  888. {
  889. $this->image = Image::factory( $targetFile );
  890. $this->image->watermark( Image::factory( $watermark ), true, true )->save( $targetFile, 90 );
  891. }
  892. echo "1";
  893. } die();
  894. }
  895. public function images_check( $folder, $id = NULL )
  896. {
  897. $fileArray = array( );
  898. if( !file_exists( MEDIAPATH . "$folder/$id/" ) )
  899. return array( );
  900. foreach( $_POST as $key => $value )
  901. {
  902. if( $key != 'folder' && $key != '_' )
  903. {
  904. if( file_exists( MEDIAPATH . "$folder/" . $id . "/$value" ) )
  905. {
  906. $fileArray[$key] = $value;
  907. }
  908. }
  909. }
  910. echo json_encode( $fileArray );
  911. die();
  912. }
  913. function reprice()
  914. {
  915. $goods = DB::select( 'id' )
  916. ->from( 'shop_goods' )
  917. // ->limit(100)
  918. ->execute()
  919. ;
  920. $goods = $goods->as_array( 'id' );
  921. $prices = DB::select( array( 'good_id', 'id' ) )
  922. ->from( 'shop_prices' )
  923. // ->limit(100)
  924. ->execute()
  925. ;
  926. $prices = $prices->as_array( 'id' );
  927. $query = DB::insert( 'shop_prices', array( 'price_categories_id', 'currencies_id', 'price', 'good_id' ) );
  928. $arr = array_diff_assoc( $goods, $prices );
  929. if( count( $arr ) == 0 )
  930. die( 'нетУ' );
  931. foreach( $arr as $item )
  932. {
  933. $query->values( array( 1, 1, 0, $item['id'] ) );
  934. $query->values( array( 2, 1, 0, $item['id'] ) );
  935. }
  936. $query->execute();
  937. die();
  938. }
  939. static public function publ_types( $count_pubs = FALSE, $languages_id = NULL )
  940. {
  941. $query = DB::select( 'publicationstypes.id', 'publicationstypes.name', 'publicationstypes.parents_path', 'publicationstypes.parent_id', 'publicationstypes.parent_url', 'publicationstypes.url'
  942. )
  943. ->from( 'publicationstypes' )
  944. ->order_by( 'publicationstypes.name' )
  945. ->cached( 30 )
  946. ;
  947. if( $count_pubs )
  948. {
  949. $query = DB::select( 'publicationstypes.id', 'publicationstypes.name', 'publicationstypes.parents_path', 'publicationstypes.parent_id', 'publicationstypes.parent_url', 'publicationstypes.url', Db::expr( 'COUNT(publications.id) AS count' )
  950. )
  951. ->from( 'publicationstypes' )
  952. ->join( 'publications', 'left' )
  953. ->on( 'publicationstypes.id', '=', Db::expr( 'publications.publicationstypes_id AND publications.languages_id = ' . $languages_id )
  954. )
  955. ->order_by( 'publicationstypes.name' )
  956. ->group_by( 'publicationstypes.id' )
  957. ->cached( 30 )
  958. ;
  959. }
  960. $result = $query->execute();
  961. if( $result->count() == 0 )
  962. return array( );
  963. else
  964. return $result->as_array( 'id' );
  965. }
  966. static public function cat_types( $count_pubs = FALSE, $languages_id = NULL )
  967. {
  968. $query = DB::select( 'id', 'name', 'parent_id' )
  969. ->from( 'shop_categories' )
  970. //->order_by( 'publicationstypes.name' )
  971. ;
  972. // if( $count_pubs )
  973. // {
  974. // $query = DB::select( 'publicationstypes.id', 'publicationstypes.name', 'publicationstypes.parents_path', 'publicationstypes.parent_id', 'publicationstypes.parent_url', 'publicationstypes.url', Db::expr( 'COUNT(publications.id) AS count' )
  975. // )
  976. // ->from( 'publicationstypes' )
  977. // ->join( 'publications', 'left' )
  978. // ->on( 'publicationstypes.id', '=', Db::expr( 'publications.publicationstypes_id AND publications.languages_id = ' . $languages_id )
  979. // )
  980. // ->order_by( 'publicationstypes.name' )
  981. // ->group_by( 'publicationstypes.id' )
  982. //
  983. // ;
  984. // }
  985. $result = $query->cached( 30 )->execute();
  986. if( $result->count() == 0 )
  987. return array( );
  988. else
  989. return $result->as_array( 'id' );
  990. }
  991. static public function toParentsMode( $lang_id = 1 )
  992. {
  993. $query = DB::select( 'id', 'parents_serialized' )
  994. ->from( array( 'shop_categories', 'categories' ) )
  995. ->where( 'parents_path', '=', '1,' )
  996. ->execute()
  997. ;
  998. $return = array( );
  999. foreach( $query->as_array() as $item )
  1000. {
  1001. // $parents = Kohana::config( 'shop.root_category_id' ) . ',';
  1002. // $parents_serialized = unserialize( $item['parents_serialized'] );
  1003. // if( is_array( $parents_serialized ) && count( $parents_serialized ) )
  1004. // {
  1005. //