/www/application/modules/shop/models/build/classes/Shop/SProductsQuery.php

https://github.com/kelios/imshop · PHP · 140 lines · 97 code · 25 blank · 18 comment · 11 complexity · ffd2531adebcb0842ba6656a12aed2df MD5 · raw file

  1. <?php
  2. /**
  3. * Skeleton subclass for performing query and update operations on the 'shop_products' table.
  4. *
  5. *
  6. *
  7. * You should add additional methods to this class to meet the
  8. * application requirements. This class will only be generated as
  9. * long as it does not already exist in the output directory.
  10. *
  11. * @package propel.generator.Shop
  12. */
  13. class SProductsQuery extends BaseSProductsQuery {
  14. public function combinator(array $data)
  15. {
  16. $n=0;
  17. foreach ($data as $key=>$values)
  18. {
  19. $combiners=array();
  20. foreach ($values as $searchText)
  21. {
  22. $alias = 'C'.$n;
  23. $c1 = $alias.$n.'c1';
  24. $c2 = $alias.$n.'c2';
  25. $combineName = 'Combine'.$n;
  26. $this->join('SProductPropertiesData '. $alias, Criteria::LEFT_JOIN);
  27. $this->condition($c1, $alias.'.PropertyId = ?', $key);
  28. $this->condition($c2, $alias.'.Value = ?', $searchText);
  29. $this->combine(array($c1,$c2), 'and', $combineName);
  30. $combiners[]=$combineName;
  31. $n++;
  32. }
  33. $this->combine($combiners,'or','Combiner'.$n);
  34. $allCombiners[] = 'Combiner'.$n;
  35. }
  36. $this->where($allCombiners, 'and');
  37. $this->distinct();
  38. return $this;
  39. }
  40. /**
  41. * Apply custom fields query.
  42. *
  43. * @param $fieldsDataArray
  44. * @return SProductsQuery
  45. */
  46. public function applyCustomFieldsQuery($fieldsDataArray)
  47. {
  48. $filterData = array();
  49. if (sizeof($fieldsDataArray) > 0 && is_array($fieldsDataArray))
  50. {
  51. foreach ($fieldsDataArray as $fieldId=>$vals)
  52. {
  53. if (count($vals) > 0 && is_array($vals))
  54. {
  55. // Load field
  56. $field = SPropertiesQuery::create()
  57. ->filterByActive(true)
  58. ->findPk($fieldId);
  59. if ($field !== null)
  60. {
  61. $fieldValues = $field->asArray();
  62. foreach ($vals as $needVal)
  63. {
  64. if (isset($fieldValues, $needVal) && !empty($needVal) OR $needVal == '0')
  65. {
  66. if (!is_array($filterData[$field->getId()]))
  67. $filterData[$field->getId()] = array();
  68. array_push($filterData[$field->getId()],$fieldValues[$needVal]);
  69. $filterData[$field->getId()] = array_unique($filterData[$field->getId()]);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. if (sizeof($filterData) > 0)
  77. return $this->combinator($filterData);
  78. else
  79. return $this;
  80. }
  81. public static function getFilterQueryString()
  82. {
  83. $data = array();
  84. $need = array('f','lp','rp','brand','order');
  85. foreach($need as $key=>$value)
  86. {
  87. if (isset(ShopCore::$_GET[$value]))
  88. {
  89. $data[$value] = ShopCore::$_GET[$value];
  90. }
  91. }
  92. return '?'.http_build_query($data);
  93. }
  94. public function mostViewed($limit = 6)
  95. {
  96. $this->orderByViews(Criteria::DESC)
  97. ->filterByActive(true)
  98. ->limit($limit);
  99. return $this;
  100. }
  101. public function getSimilarProducts(SProducts $model)
  102. {
  103. $properties = SProductPropertiesDataQuery::create()
  104. ->findByProductId($model->getId());
  105. foreach ($properties as $property) {
  106. $data[][$property->getPropertyId()] = array($property->getValue());
  107. }
  108. shuffle($data);
  109. return $this
  110. ->leftJoin('ProductVariant')
  111. ->where('SProducts.Id != ?', $model->getId())
  112. ->filterByActive(true)
  113. ->combinator($data[0]);
  114. }
  115. } // SProductsQuery