PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/shop_export/classes/model/shopexport.php

https://bitbucket.org/seyar/kinda.local
PHP | 141 lines | 81 code | 20 blank | 40 comment | 11 complexity | e0dd7ebff9076b94407007daf5d6b743 MD5 | raw file
  1. <?php
  2. defined('SYSPATH') OR die('No direct access allowed.');
  3. /**
  4. * @version $Id: v 0.1 May 16, 2011 - 10:44:48 AM Exp $
  5. *
  6. * Project: microshop.local
  7. * File: shopexport.php *
  8. *
  9. * This library is commercial distributed software; you can't
  10. * redistribute it and/or modify it without owner (or author) approval.
  11. *
  12. * @link http://bestartdesign.com
  13. * @Best IT Solutions (C) 2010
  14. *
  15. * @author Seyar Chapuh seyarchapuh@gmail.com
  16. */
  17. class Model_Shopexport extends Model
  18. {
  19. const EXT = '.xml';
  20. protected $domainInfo;
  21. protected $fileFolderUrl;
  22. function __construct()
  23. {
  24. $this->domainInfo = Model_Content::load_domain_info();
  25. $this->fileFolderUrl = DOCROOT.Kohana::config('shopexport.exportFolder').'/';
  26. }
  27. /**
  28. * Function return categories id => Array('id','parent_id','name')
  29. *
  30. * @return array
  31. */
  32. static function getListCategories()
  33. {
  34. $deprecated = Kohana::config('shopexport.deprecatedCategories');
  35. $query = DB::select('id','parent_id','name', 'seo_url')
  36. ->from('shop_categories')
  37. ->where('status' ,'=','verified')
  38. ->and_where('id', 'NOT IN', DB::expr('('.implode(',',$deprecated).')') )
  39. ;
  40. $result = $query->execute();
  41. if( $result->count() == 0) return array();
  42. else
  43. return $result->as_array('id');
  44. }
  45. /**
  46. * get list available goods
  47. *
  48. * @param array $categories
  49. * @return array goods
  50. */
  51. static function getListGoods( $categories = NULL )
  52. {
  53. if( !isset($categories) || empty($categories) ) $categories = self::getListCategories();
  54. $price_category = 1; // розница
  55. $query = DB::select('id','name','alter_name', 'seo_url', 'categories_id', 'scu', 'short_description','default_photo')
  56. ->from('shop_goods')
  57. ->where('categories_id','IN', DB::expr('('.implode(',',array_keys($categories)).')') )
  58. ;
  59. // if( !IN_PRODUCTION )
  60. // $query->limit(20);
  61. $query->and_where_open();
  62. foreach( Kohana::config('shop.frontendStatuses') as $item )
  63. {
  64. $query->or_where( "status", '=', $item );
  65. }
  66. $query->or_where( "status", '=', 'out_of_store' );
  67. $query->and_where_close();
  68. $result = $query->execute();
  69. if( $result->count() == 0 ) return array();
  70. else
  71. {
  72. $return = array();
  73. foreach( $result->as_array('id') as $key => $item )
  74. {
  75. $query = Db::select('p.price_categories_id', 'p.currencies_id', 'p.price')
  76. ->from(array('shop_prices', 'p'))
  77. ->where('p.good_id' , '=', $item['id'])
  78. ->order_by('p.currencies_id')
  79. ;
  80. if (is_numeric($price_category))
  81. $query->where('p.price_categories_id', '=', (int)$price_category);
  82. $prices = array();
  83. $rows = $query->execute()->as_array();
  84. $oPrices = new Model_Frontend_Currencies();
  85. $item['price'] = $oPrices->getPrice($rows);
  86. $item['price'] = number_format($item['price'][$price_category][Kohana::config( 'shop' )->frontend_currency],2,'.','');
  87. $return[$key] = $item;
  88. }
  89. return $return;
  90. }
  91. }
  92. /**
  93. * Function return string url to the export file /export/yandex.xml
  94. *
  95. * @param string $target target method
  96. * @return string url
  97. */
  98. function getExportFileURL( $target )
  99. {
  100. if( !file_exists($this->fileFolderUrl.$target.self::EXT) )
  101. return false;
  102. return '/'.Kohana::config('shopexport.exportFolder').'/'.$target.self::EXT;
  103. }
  104. /**
  105. * Function save data to file
  106. *
  107. * @param string $target file path to save
  108. * @param string $data date to save
  109. * @return bool
  110. */
  111. function saveExportFile( $target,$data )
  112. {
  113. if( !file_exists($this->fileFolderUrl) )
  114. mkdir( $this->fileFolderUrl, 0755, TRUE );
  115. $file = $this->fileFolderUrl.$target.self::EXT;
  116. if(!$handle = fopen($file, 'w')) return "some error";
  117. if(!fwrite($handle, $data."\r\n")) return "writing error";
  118. fclose($handle);
  119. return;
  120. }
  121. }