PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/seyar/ari100krat.local
PHP | 314 lines | 243 code | 56 blank | 15 comment | 26 complexity | 3e066c80c6767e468b434e2737944093 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. class Model_Frontend_Goods extends Model
  3. {
  4. static public function images($id = NULL)
  5. {
  6. }
  7. static public function properties($id = NULL)
  8. {
  9. }
  10. static public function review($id = NULL)
  11. {
  12. }
  13. static public function info($id = NULL, $price_categoty = NULL)
  14. {
  15. $query = Db::select('g.*'
  16. , array('c.name','category_name')
  17. , array('c.seo_url','category_url')
  18. )
  19. ->from(array('shop_goods', 'g'))
  20. ->join(array('shop_categories', 'c'))
  21. ->on('g.categories_id', '=', 'c.id')
  22. ->where('g.id', '=', $id)
  23. ->and_where_open()
  24. ->where('c.status' , '=', 'verified')
  25. ->or_where('c.id' , '=', Kohana::config('shop.archiveID') )
  26. ->and_where_close()
  27. ->limit(1)
  28. ;
  29. $query->and_where_open();
  30. foreach( Kohana::config('shop.frontendStatuses') as $key => $item )
  31. {
  32. $query->or_where( "g.status", '=', $item );
  33. }
  34. $query->or_where( 'g.status', '=', 'out_of_store' );
  35. $query->or_where( 'g.status', '=', 'from1c' );
  36. $query->and_where_close();
  37. //Kohana_log::instance()->add('d', print_r($query->__toString(),1) );
  38. $result = $query->cached(60)->execute();
  39. if ($result->count() == 0)
  40. {
  41. echo Request::factory('error/404')->execute();
  42. die();
  43. }
  44. else
  45. {
  46. $return =array();
  47. $return = $result->current();
  48. if($return['rating'] > 0)
  49. $return['rating'] = intval($return['stars'] / $return['rating']);
  50. $query = Db::select('p.price_categories_id', 'p.currencies_id', 'p.price')
  51. ->from(array('shop_prices', 'p'))
  52. ->where('p.good_id' , '=', $return['id'])
  53. ->order_by('p.currencies_id')
  54. ;
  55. if (is_numeric($price_categoty))
  56. $query->where('p.price_categories_id', '=', (int)$price_categoty);
  57. $prices = array();
  58. $rows = $query->cached(60)->execute()->as_array();
  59. $curr = new Model_Frontend_Currencies();
  60. $return['prices'] =$curr->getPrice($rows);
  61. $return['seo_url'] = $return['seo_url'] ? str_replace( Model_Frontend_Categories::$url_search,Model_Frontend_Categories::$url_replace,urlencode($return['seo_url'])) : str_replace( Model_Frontend_Categories::$url_search, Model_Frontend_Categories::$url_replace, Controller_Admin::to_url($return['name']));
  62. $return['seo_url'] = str_replace( '__', '_', $return['seo_url'] );
  63. $return['seo_url'] = str_replace( '__', '_', $return['seo_url'] );
  64. $return['category_url'] = $return['category_url'] ? str_replace(Model_Frontend_Categories::$url_search,Model_Frontend_Categories::$url_replace,urlencode($return['category_url'])) : str_replace( Model_Frontend_Categories::$url_search, Model_Frontend_Categories::$url_replace, Controller_Admin::to_url($return['category_name']));
  65. $return['category_url'] = str_replace( '__', '_', $return['category_url'] );
  66. $return['category_url'] = str_replace( '__', '_', $return['category_url'] );
  67. $add_fields = DB::select('ser_addFields')
  68. ->from('shop_categories')
  69. ->where('id', '=', $return['categories_id'])
  70. ->execute()
  71. ->current()
  72. ;
  73. $return['ser_addFields'] = unserialize($add_fields['ser_addFields']);
  74. // если из груп то не эксплодим. в противном случае эксплод с влож-ю 2.
  75. $return['add_fields'] = DB::query(Database::SELECT,
  76. "SELECT `f`.`id` AS `f_id`, `f`.`name`, `f`.`type`, `f`.`desc`, v.*, f.is_group, f.alias "
  77. ." FROM `shop_goods_properties` AS `f` "
  78. ." LEFT OUTER JOIN "
  79. . "(SELECT * FROM `shop_goods_properties_values` AS v1 WHERE v1.good_id = {$id} ) "
  80. . " AS `v` ON (`f`.`id`=`v`.`properties_category_id`) "
  81. . " WHERE f.category_id = {$return['categories_id']}
  82. order by f.alias"
  83. )
  84. ->cached(60)
  85. ->execute()
  86. ->as_array('alias')
  87. ;
  88. $return['add_groups'] = array();
  89. foreach( $return['add_fields'] as $item )
  90. {
  91. if( $item['is_group'] == 1 )
  92. {
  93. $return['add_groups'][$item['alias']] = $item;
  94. // $res[] = explode('_', $item, 2);
  95. }
  96. else
  97. {
  98. $key_group = substr($item['alias'], 0, strpos($item['alias'],'_') );
  99. $key_group = empty($key_group) ? -1 : $key_group;
  100. $return['add_groups'][$key_group][$item['alias']] = $item;
  101. }
  102. }
  103. return $return;
  104. }
  105. }
  106. static function getPriceCategories($arr = array(), $price_category = NULL)
  107. {
  108. $query = Db::select('p.price_categories_id', 'p.currencies_id', 'p.price','p.good_id')
  109. ->from(array('shop_prices', 'p'))
  110. ->where('p.good_id' , 'IN', DB::expr( '('.implode(',', $arr).')') )
  111. ->order_by('p.currencies_id')
  112. ->cached(120)
  113. ;
  114. if( is_numeric( $price_category ) )
  115. $query->where( 'p.price_categories_id', '=', (int) $price_category );
  116. $return = array();
  117. $rows = $query->execute()->as_array();
  118. foreach($rows as $item)
  119. {
  120. $return[$item['good_id']][] = $item;
  121. }
  122. return $return;
  123. }
  124. static public function thisCategoryOtherGoods( $goodId, $catid, $price_category = NULL, $limit = NULL, $rand = FALSE )
  125. {
  126. $query = Db::select('g.*'
  127. , array('c.name','category_name')
  128. , array('c.seo_url','category_url')
  129. )
  130. ->from(array('shop_goods', 'g'))
  131. ->join(array('shop_categories', 'c'))
  132. ->on('g.categories_id', '=', 'c.id')
  133. ->where('c.status' , '=', 'verified')
  134. ->and_where('c.id' , '=', $catid)
  135. ->and_where('g.id' , '<>', $goodId)
  136. ->order_by( 'id', 'desc' )
  137. ;
  138. $query->and_where_open();
  139. foreach( Kohana::config('shop.frontendStatuses') as $key => $item )
  140. {
  141. $query->or_where( "g.status", '=', $item );
  142. }
  143. $query->and_where_close();
  144. // if( $limit )
  145. // $query->limit($limit);
  146. $result = $query->cached(30)->execute();
  147. if ($result->count() == 0) return array();
  148. else
  149. {
  150. $return =array();
  151. $curr = new Model_Frontend_Currencies();
  152. $tmpkeys = $result->as_array('id');
  153. $rows = self::getPriceCategories( array_keys($tmpkeys) );
  154. foreach ($tmpkeys as $key=>$val)
  155. {
  156. $ids[] = $key;
  157. // if (is_numeric($price_categoty))
  158. // $query->where('p.price_categories_id', '=', (int)$price_categoty);
  159. $prices = array();
  160. $val['prices'] =$curr->getPrice($rows[$key]);
  161. $val['seo_url'] = $val['seo_url'] ? $val['seo_url'] : Controller_Admin::to_url($val['name']);
  162. $val['category_url'] = $val['category_url'] ? $val['category_url'] : Controller_Admin::to_url($val['category_name']);
  163. $return[$key] = $val;
  164. }
  165. if( $rand && $limit && $result->count() > $limit)
  166. {
  167. $ids = array_rand($return,$limit);
  168. foreach( $ids as $item)
  169. $return2[] = $return[$item];
  170. $return = $return2;
  171. }
  172. return $return;
  173. }
  174. }
  175. static public function vote($goodId, $stars, $userId)
  176. {
  177. if(self::isVoted($goodId, $userId)) throw new Exception('You have already voted');
  178. DB::update('shop_goods')
  179. ->set(
  180. array(
  181. 'rating' => DB::expr("rating+1"),
  182. 'stars' => DB::expr("stars+".$stars),
  183. )
  184. )
  185. ->where('id', '=', $goodId)
  186. ->execute();
  187. self::setVoted($goodId, $userId);
  188. }
  189. static public function isVoted($goodId, $userId)
  190. {
  191. $res = DB::select()
  192. ->from('shop_goods_votes')
  193. ->where('good_id', '=', $goodId)
  194. ->and_where('user_id', '=', $userId)
  195. ->execute()
  196. ->as_array('id')
  197. ;
  198. return !empty($res);
  199. }
  200. static public function setVoted($goodId, $userId)
  201. {
  202. DB::insert('shop_goods_votes', array('good_id', 'user_id'))
  203. ->values(array($goodId, $userId))
  204. ->execute();
  205. }
  206. static function getKeywords( $id = NULL,$catid = NULL, $type = 'keywords' )
  207. {
  208. if( !isset($id) && !isset($catid) ) return '';
  209. $return = $type == 'keywords' ? Kohana::config('shopkeywords.keywordSource') : Kohana::config('shopkeywords.descriptionSource');
  210. // get good
  211. $goodInfo = DB::select()
  212. ->from('shop_goods')
  213. ->where('id','=',$id)
  214. ->cached(120)
  215. ->limit(1)
  216. ->execute()
  217. ->current()
  218. ;
  219. if( count($goodInfo) == 0 && isset($id) ) return '';
  220. $catid = isset($id) ? $goodInfo['categories_id'] : $catid;
  221. //get cat
  222. $catInfo = DB::select()
  223. ->from('shop_categories')
  224. ->where('id','=', $catid)
  225. ->cached(120)
  226. ->limit(1)
  227. ->execute()
  228. ->current()
  229. ;
  230. $goodname = isset($goodInfo['name']) && !empty ($goodInfo['name']) ? $goodInfo['name'] : $goodInfo['alter_name'];
  231. $catname = isset($catInfo['page_title']) && !empty ($catInfo['page_title']) ? $catInfo['page_title'] : $catInfo['name'];
  232. // $configKeys = Kohana::config('shopkeywords.keywords');
  233. // $keywords = array();
  234. // foreach($configKeys as $k => $value)
  235. // $keywords[strtolower($k)] = $value;
  236. // $goodname = str_replace( array_keys($keywords),$keywords, strtolower($goodname) );
  237. // $catname = str_replace( array_keys($keywords),$keywords, strtolower($catInfo['name']) );
  238. $search = array( '%category%', '%good%' );
  239. $replace = array( $catname, $goodname );
  240. $return = ucwords( str_replace( $search, $replace, $return ));
  241. return $return;
  242. }
  243. static function getApproximateDate( $storeDate = NULL )
  244. {
  245. $return = i18n::get('Expected').' ';
  246. $dateTexts = Kohana::config('shop.stpreApproximateText');
  247. $seconds = 60*60*24;
  248. $dateDiff = (int)(strtotime($storeDate, time())/ $seconds) - (int)(time()/ $seconds) + 1;
  249. if( $dateDiff < 0 || !$storeDate ) return $return.'доставка';
  250. $dateText = '';
  251. foreach( $dateTexts as $key => $item )
  252. {
  253. if( $dateDiff == $key ) $dateText .= i18n::get($item);
  254. }
  255. if( !empty($dateText) ) return $return.$dateText;
  256. return $return.date('d.m.Y', strtotime($storeDate));
  257. }
  258. }