PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/seyar/ari100krat.local
PHP | 1066 lines | 863 code | 135 blank | 68 comment | 91 complexity | 4b7b83ba63b36c6bf16f2660d9b183b8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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( '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. )
  402. )
  403. ->where( 'id', '=', $id )
  404. ->execute()
  405. ;
  406. }else
  407. { // create
  408. $id = $total_rows = 0;
  409. list($id, $total_rows) = DB::insert( 'shop_goods', array(
  410. 'categories_id',
  411. 'scu',
  412. 'name',
  413. 'brand_id',
  414. 'alter_name',
  415. 'short_description',
  416. 'full_description',
  417. 'is_bestseller',
  418. 'is_new',
  419. 'rating',
  420. 'show_on_startpage',
  421. 'cost',
  422. 'weight',
  423. 'quantity',
  424. 'guaranties_period',
  425. 'store_approximate_date',
  426. 'seo_keywords',
  427. 'seo_description',
  428. 'seo_url',
  429. 'status',
  430. )
  431. )
  432. ->values(
  433. array(
  434. $post['categories_id'],
  435. $post['scu'],
  436. $post['name'],
  437. $post['brand_id'],
  438. Model_Content::arrGet($post, 'alter_name', $post['name']),
  439. $post['short_description'],
  440. $post['full_description'],
  441. $post['is_bestseller'],
  442. $post['is_new'],
  443. $post['rating'],
  444. $post['show_on_startpage'],
  445. $post['cost'],
  446. $post['weight'],
  447. $post['quantity'],
  448. $post['guaranties_period'],
  449. (strtotime( $post['store_approximate_date'] ) ? $post['store_approximate_date'] : NULL),
  450. $post['seo_keywords'],
  451. $post['seo_description'],
  452. $seourl,
  453. $post['status'],
  454. )
  455. )
  456. ->execute();
  457. }
  458. // Prices update
  459. if( method_exists( 'Model_Currencies', 'prices_update' ) )
  460. {
  461. Model_Currencies::prices_update( $id, 'price', $post );
  462. Model_Currencies::prices_update( $id, 'oldprice', $post );
  463. }
  464. Model_Goods::save_add_filelds( $lang, $id, $post );
  465. // save rec goods
  466. $addGoodsModel = Kohana::config( 'shop' )->addGoodsModel;
  467. $addGoodsMethod = Kohana::config( 'shop' )->addGoodsMethSave;
  468. if( isset( $addGoodsModel ) && !empty( $addGoodsModel ) && method_exists( $addGoodsModel, $addGoodsMethod ) )
  469. call_user_func( array( $addGoodsModel, $addGoodsMethod ), $id, $post, 'goodId' );
  470. return $id;
  471. }
  472. else
  473. {
  474. return false;
  475. }
  476. }
  477. static public function save_add_filelds( $lang = NULL, $id = NULL, $post = array( ) )
  478. {
  479. if( !$lang OR !$id )
  480. return false;
  481. $result = array( );
  482. $rows = DB::select()
  483. ->from( 'shop_goods_properties' )
  484. ->where( 'category_id', '=', $post['categories_id'] )
  485. ->execute()
  486. ->as_array()
  487. ;
  488. foreach( $rows as $value )
  489. {
  490. if( isset($_POST['add_fields'][$value['id']]) )
  491. {
  492. $result[$value['id']] = array(
  493. 'name' => $value['name'],
  494. 'type' => $value['type'],
  495. );
  496. }else
  497. {
  498. $query = Db::delete( 'shop_goods_properties_values' )
  499. ->where( 'good_id', '=', $id )
  500. ->and_where( 'properties_category_id', '=', (int)@$_POST['add_fields_id'][$value['id']] )
  501. ;
  502. $query->execute();
  503. }
  504. }
  505. foreach( $result as $key => $value )
  506. {
  507. $insert = array( );
  508. $null = DB::expr( 'NULL' );
  509. if( !$_POST['add_fields_id'][$key] )
  510. {
  511. $query = Db::query( Database::INSERT
  512. , " INSERT INTO shop_goods_properties_values "
  513. . " SET "
  514. . " good_id = $id "
  515. . " , properties_category_id = :add_fld_id "
  516. . " , value_int = :value_int "
  517. . " , value_float = :value_float "
  518. . " , value_varchar = :value_varchar "
  519. . " , value_text = :value_text "
  520. . " , value_date = :value_date "
  521. )
  522. ->bind( ':add_fld_id', $key )
  523. ->bind( ':value_int', $null )
  524. ->bind( ':value_float', $null )
  525. ->bind( ':value_varchar', $null )
  526. ->bind( ':value_text', $null )
  527. ->bind( ':value_date', $null )
  528. ;
  529. }
  530. else
  531. $query = Db::query( Database::UPDATE
  532. , " UPDATE shop_goods_properties_values "
  533. . " SET "
  534. . " good_id = $id "
  535. . " , properties_category_id = :add_fld_id "
  536. . " , value_int = :value_int "
  537. . " , value_float = :value_float "
  538. . " , value_varchar = :value_varchar "
  539. . " , value_text = :value_text "
  540. . " , value_date = :value_date "
  541. . " WHERE id = " . $_POST['add_fields_id'][$key]
  542. )
  543. ->bind( ':add_fld_id', $key )
  544. ->bind( ':value_int', $null )
  545. ->bind( ':value_float', $null )
  546. ->bind( ':value_varchar', $null )
  547. ->bind( ':value_text', $null )
  548. ->bind( ':value_date', $null )
  549. ;
  550. switch( $value['type'] )
  551. {
  552. case 'int':
  553. $query->bind( ':value_int', $_POST['add_fields'][$key] );
  554. break;
  555. case 'boolean':
  556. if(is_array($_POST['add_fields'][$key]))
  557. $query->bind( ':value_text', implode('|',$_POST['add_fields'][$key]) );
  558. break;
  559. case 'float':
  560. $query->bind( ':value_float', $_POST['add_fields'][$key] );
  561. break;
  562. case 'varchar':
  563. $query->bind( ':value_varchar', $_POST['add_fields'][$key] );
  564. break;
  565. case 'text':
  566. case 'select':
  567. $query->bind( ':value_text', $_POST['add_fields'][$key] );
  568. break;
  569. case 'date':
  570. $query->bind( ':value_date', $_POST['add_fields'][$key] );
  571. break;
  572. default:
  573. break;
  574. }
  575. $query->execute();
  576. }
  577. return true;
  578. }
  579. public function validate()
  580. {
  581. $keys = array(
  582. 'categories_id',
  583. 'scu',
  584. 'name',
  585. 'brand_id',
  586. 'alter_name',
  587. 'short_description',
  588. 'full_description',
  589. 'is_bestseller',
  590. 'is_new',
  591. 'show_on_startpage',
  592. 'rating',
  593. 'cost',
  594. 'weight',
  595. 'quantity',
  596. 'guaranties_period',
  597. 'store_approximate_date',
  598. 'seo_keywords',
  599. 'seo_description',
  600. 'seo_url',
  601. 'status',
  602. 'price',
  603. 'oldprice',
  604. 'add_fields_id',
  605. 'add_fields',
  606. 'recgoodid',
  607. 'parsed',
  608. );
  609. $params = Arr::extract( $_POST, $keys, 0 );
  610. $params['weight'] = str_replace( ",", ".", $params['weight'] );
  611. $params['cost'] = str_replace( ",", ".", $params['cost'] );
  612. $post = Validate::factory( $params )
  613. ->rule( 'categories_id', 'not_empty' )
  614. ->rule( 'categories_id', 'digit' )
  615. // ->rule('scu', 'not_empty')
  616. ->rule( 'name', 'not_empty' )
  617. ->rule( 'brand_id', 'digit' )
  618. // ->rule( 'alter_name', 'not_empty' )
  619. // ->rule('short_description', 'not_empty')
  620. ->rule( 'full_description', 'not_empty' )
  621. ->rule( 'is_bestseller', 'digit' )
  622. ->rule( 'is_new', 'digit' )
  623. ->rule( 'show_on_startpage', 'digit' )
  624. // ->rule('rating', 'decimal', array(2))
  625. // ->rule('cost', 'numeric')
  626. ->rule( 'cost', 'regex', array( "/^[0-9]+((\.|,)[0-9]{1,2})?$/" ) )
  627. ->rule( 'weight', 'regex', array( "/^[0-9]+((\.|,)[0-9]{1,3})?$/" ) )
  628. ->rule( 'quantity', 'digit' )
  629. ->rule( 'guaranties_period', 'digit' )
  630. // ->rule( 'price', 'numeric' )
  631. ->rule( 'store_approximate_date', 'date' )
  632. ->rule( 'status', 'not_empty' )
  633. ;
  634. if( $post->check() )
  635. {
  636. return $params;
  637. }else
  638. {
  639. Messages::add( $post->errors( 'validate' ) );
  640. }
  641. }
  642. public function delete( $id = NULL )
  643. {
  644. $query = DB::delete( 'shop_goods' );
  645. if( is_numeric( $id ) )
  646. {
  647. $query->where( 'id', '=', $id ); // ->limit(1)
  648. $total_rows = $query->execute();
  649. }
  650. }
  651. public function delete_list( array $array )
  652. {
  653. $query = DB::delete( 'shop_goods' );
  654. if( count( $array ) )
  655. {
  656. $query->where( 'id', 'in', array_flip( $array ) );
  657. $total_rows = $query->execute();
  658. }
  659. }
  660. // Images functions
  661. public function images_default( $file, $id = NULL )
  662. {
  663. if( is_file( $file ) )
  664. {
  665. $id = defined( 'MODULE_ID' ) ? MODULE_ID : $id;
  666. $query = DB::update( 'shop_goods' )
  667. ->set(
  668. array(
  669. 'default_photo' => basename( $_GET['name'] ),
  670. )
  671. )
  672. ->where( 'id', '=', $id );
  673. $query->execute();
  674. echo '1';
  675. }
  676. }
  677. public function images_delete( $folder, $id = NULL )
  678. {
  679. if( $id == NULL ) return false;
  680. if( isset($_GET['name']) && is_array($_GET['name']) )
  681. {
  682. foreach( $_GET['name'] as $item )
  683. {
  684. $obj_image = MEDIAPATH . "$folder/" . $id . '/' . basename( $item );
  685. if( is_file( $obj_image ) )
  686. if( unlink( $obj_image ) )
  687. {
  688. $obj_image = MEDIAPATH . "$folder/" . $id . '/prev_' . basename( $item );
  689. if( is_file( $obj_image ) )
  690. {
  691. if( unlink( $obj_image ) )
  692. echo '1';
  693. }
  694. else
  695. echo '1';
  696. }
  697. }
  698. }
  699. else
  700. {
  701. $obj_image = MEDIAPATH . "$folder/" . $id . '/' . basename( $_GET['name'] );
  702. if( is_file( $obj_image ) )
  703. if( unlink( $obj_image ) )
  704. {
  705. $obj_image = MEDIAPATH . "$folder/" . $id . '/prev_' . basename( $_GET['name'] );
  706. if( is_file( $obj_image ) )
  707. {
  708. if( unlink( $obj_image ) )
  709. echo '1';
  710. }
  711. else
  712. echo '1';
  713. }
  714. }
  715. }
  716. static public function images_show( $folder, $path='', $webpath='/', $id = NULL )
  717. {
  718. $obj = array( 'image' => '' );
  719. $id = defined( 'MODULE_ID' ) ? MODULE_ID : $id;
  720. $obj = DB::select( 'default_photo' )
  721. ->from( 'shop_goods' )
  722. ->where( 'id', '=', $id )
  723. ->limit( 1 )
  724. ->execute()
  725. ->current()
  726. ;
  727. $filesArray = array( 'path' => $path
  728. , 'webpath' => $webpath
  729. , 'files' => array( )
  730. , 'default_img' => $obj['default_photo'] );
  731. if( !file_exists( MEDIAPATH . "$folder/$id/" ) )
  732. mkdir( MEDIAPATH . "$folder/$id/", 0755, true );
  733. $obj_images_dir = dir( MEDIAPATH . "$folder/$id/" );
  734. if( $obj_images_dir )
  735. {
  736. while( false !== ($entry = $obj_images_dir->read()) )
  737. {
  738. if( $entry != '.' and $entry != '..' and !strstr( $entry, 'prev_' ) )
  739. $filesArray['files'][] = $entry;
  740. }
  741. }
  742. // set default image if it empty and there is images
  743. if( !$obj['default_photo'] AND count( $filesArray['files'] ) )
  744. {
  745. DB::update( 'shop_goods' )
  746. ->set( array( 'default_photo' => $filesArray['files'][0] ) )
  747. ->where( 'id', '=', $id )
  748. ->execute()
  749. ;
  750. $filesArray['default_img'] = $filesArray['files'][0];
  751. }
  752. return $filesArray;
  753. }
  754. public function images_upload( $folder, $width = NULL, $height = NULL, $t_width = NULL, $t_height = NULL, $id = NULL, $watermark = NULL )
  755. {
  756. if( !empty( $_FILES ) )
  757. {
  758. $tempFile = $_FILES['Filedata']['tmp_name'];
  759. $targetPath = MEDIAPATH . "$folder/$id/";
  760. $targetFile = str_replace( '//', '/', $targetPath ) . strtolower( Controller_Admin::to_url( $_FILES['Filedata']['name'] ) );
  761. if( !file_exists( MEDIAPATH . "$folder/$id" ) )
  762. @mkdir( str_replace( '//', '/', $targetPath ), 0755, true ); move_uploaded_file( $tempFile, $targetFile );
  763. $this->image = Image::factory( $targetFile );
  764. // Resize Image
  765. if( $width || $height )
  766. {
  767. if( $width > $this->image->width || $height > $this->image->height )
  768. {
  769. $this->image->resize( $width, $height, Image::AUTO )
  770. //->crop($width, $height)
  771. ;
  772. } $this->image->save( $targetFile, 90 );
  773. }
  774. // Generate Thumbnail
  775. if( $width || $height )
  776. {
  777. $this->image->resize( $t_width, $t_height, Image::AUTO )->crop( $t_width, $t_height );
  778. $this->image->save( dirname( $targetFile ) . '/prev_' . basename( $targetFile ), 90 );
  779. }
  780. if( $watermark !== null && file_exists( $watermark ) )
  781. {
  782. $this->image = Image::factory( $targetFile );
  783. $this->image->watermark( Image::factory( $watermark ), TRUE, TRUE )->save( $targetFile, 90 );
  784. }
  785. echo "1";
  786. } die();
  787. }
  788. public function images_check( $folder, $id = NULL )
  789. {
  790. $fileArray = array( );
  791. if( !file_exists( MEDIAPATH . "$folder/$id/" ) )
  792. return array( );
  793. foreach( $_POST as $key => $value )
  794. {
  795. if( $key != 'folder' && $key != '_' )
  796. {
  797. if( file_exists( MEDIAPATH . "$folder/" . $id . "/$value" ) )
  798. {
  799. $fileArray[$key] = $value;
  800. }
  801. }
  802. }
  803. echo json_encode( $fileArray );
  804. die();
  805. }
  806. function reprice()
  807. {
  808. $goods = DB::select( 'id' )
  809. ->from( 'shop_goods' )
  810. // ->limit(100)
  811. ->execute()
  812. ;
  813. $goods = $goods->as_array( 'id' );
  814. $prices = DB::select( array( 'good_id', 'id' ) )
  815. ->from( 'shop_prices' )
  816. // ->limit(100)
  817. ->execute()
  818. ;
  819. $prices = $prices->as_array( 'id' );
  820. $query = DB::insert( 'shop_prices', array( 'price_categories_id', 'currencies_id', 'price', 'good_id' ) );
  821. $arr = array_diff_assoc( $goods, $prices );
  822. if( count( $arr ) == 0 )
  823. die( 'нетУ' );
  824. foreach( $arr as $item )
  825. {
  826. $query->values( array( 1, 1, 0, $item['id'] ) );
  827. $query->values( array( 2, 1, 0, $item['id'] ) );
  828. }
  829. $query->execute();
  830. die();
  831. }
  832. static public function publ_types( $count_pubs = FALSE, $languages_id = NULL )
  833. {
  834. $query = DB::select( 'publicationstypes.id', 'publicationstypes.name', 'publicationstypes.parents_path', 'publicationstypes.parent_id', 'publicationstypes.parent_url', 'publicationstypes.url'
  835. )
  836. ->from( 'publicationstypes' )
  837. ->order_by( 'publicationstypes.name' )
  838. ->cached( 30 )
  839. ;
  840. if( $count_pubs )
  841. {
  842. $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' )
  843. )
  844. ->from( 'publicationstypes' )
  845. ->join( 'publications', 'left' )
  846. ->on( 'publicationstypes.id', '=', Db::expr( 'publications.publicationstypes_id AND publications.languages_id = ' . $languages_id )
  847. )
  848. ->order_by( 'publicationstypes.name' )
  849. ->group_by( 'publicationstypes.id' )
  850. ->cached( 30 )
  851. ;
  852. }
  853. $result = $query->execute();
  854. if( $result->count() == 0 )
  855. return array( );
  856. else
  857. return $result->as_array( 'id' );
  858. }
  859. static public function cat_types( $count_pubs = FALSE, $languages_id = NULL )
  860. {
  861. $query = DB::select( 'id', 'name', 'parent_id' )
  862. ->from( 'shop_categories' )
  863. //->order_by( 'publicationstypes.name' )
  864. ;
  865. // if( $count_pubs )
  866. // {
  867. // $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' )
  868. // )
  869. // ->from( 'publicationstypes' )
  870. // ->join( 'publications', 'left' )
  871. // ->on( 'publicationstypes.id', '=', Db::expr( 'publications.publicationstypes_id AND publications.languages_id = ' . $languages_id )
  872. // )
  873. // ->order_by( 'publicationstypes.name' )
  874. // ->group_by( 'publicationstypes.id' )
  875. //
  876. // ;
  877. // }
  878. $result = $query->cached( 30 )->execute();
  879. if( $result->count() == 0 )
  880. return array( );
  881. else
  882. return $result->as_array( 'id' );
  883. }
  884. static public function toParentsMode( $lang_id = 1 )
  885. {
  886. $query = DB::select( 'id', 'parents_serialized' )
  887. ->from( array( 'shop_categories', 'categories' ) )
  888. ->where( 'parents_path', '=', '1,' )
  889. ->execute()
  890. ;
  891. $return = array( );
  892. foreach( $query->as_array() as $item )
  893. {
  894. // $parents = Kohana::config( 'shop.root_category_id' ) . ',';
  895. // $parents_serialized = unserialize( $item['parents_serialized'] );
  896. // if( is_array( $parents_serialized ) && count( $parents_serialized ) )
  897. // {
  898. // foreach( $parents_serialized as $iitem )
  899. // {
  900. // $parents .= $iitem['id'] . ',';
  901. // }
  902. // }
  903. $query = DB::select( 'id', 'parents_path' )
  904. ->from( array( 'shop_categories', 'categories' ) )
  905. ->where( 'id', '=', $item['parent_id'] )
  906. ->execute()
  907. ->current()
  908. ;
  909. DB::update( 'shop_categories' )->set( array( 'parents_path' => $query['parents_path'] . $item['id'] . ',' ) )->where( 'id', '=', $item['id'] )->execute();
  910. // $item['parents_path'] = $parents;
  911. // $return[] = $item;
  912. }
  913. return true;
  914. }
  915. /**
  916. * function update table shop_good from current id
  917. *
  918. * @param int $id
  919. * @param array $array array update data
  920. */
  921. static function update( $id, $update = array() )
  922. {
  923. $totalrows = 0;
  924. $totalrows = DB::update('shop_goods')
  925. ->set($update)
  926. ->where('id','=',$id)
  927. ->execute()
  928. ;
  929. return $totalrows;
  930. }
  931. }