PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/func.php

https://gitlab.com/Oasisnejeb/yml_helper_for_cs-cart
PHP | 132 lines | 119 code | 8 blank | 5 comment | 32 complexity | 4682560d7d7944f0cf014e06168e6972 MD5 | raw file
  1. <?php
  2. if ( !defined('AREA') ) { die('Access denied'); }
  3. use Tygh\Registry;
  4. function fn_yml_helper_update_product ($product_id,$values) {
  5. db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", $values, $product_id);
  6. }
  7. function fn_yml_helper_check_products () {
  8. $start = microtime(true);
  9. $options = Registry::get('addons.yml_helper');
  10. $yml_pickup = $options['yml_helper_yml_pickup'];
  11. $yml_delivery = $options['yml_helper_yml_delivery'];
  12. $yml_store = $options['yml_helper_yml_store'];
  13. $disable_disabled = ($options['yml_helper_disable_disabled'] == 'Y')?true:false;
  14. $generate_delivery = ($options['yml_helper_generate_delivery_options'] == 'Y')?true:false;
  15. $add_avail = ($options['yml_helper_add_avail_since'] == 'Y')?true:false;
  16. $delivery_cost = (!empty($options['yml_helper_local_delivery_cost']))?intval($options['yml_helper_local_delivery_cost']):0;
  17. $delivery_period = (!empty($options['yml_helper_local_delivery_period']))?intval($options['yml_helper_local_delivery_period']):0;
  18. $delivery_time = (!empty($options['yml_helper_local_delivery_time']))?intval($options['yml_helper_local_delivery_time']):13;
  19. $product_exempts = explode(",", $options['yml_helper_product_regexps']);
  20. $check_products = (($options['yml_helper_check_products'] == 'Y')&&(!empty($product_exempts)))?true:false;
  21. $check_descriptions = (($options['yml_helper_check_descriptions'] == 'Y')&&(!empty($product_exempts)))?true:false;
  22. $features_exempts = explode(",", $options['yml_helper_features_regexps']);
  23. $check_features = (($options['yml_helper_check_features'] == 'Y')&&(!empty($features_exempts)))?true:false;
  24. $generate_notes = ($options['yml_helper_generate_yml_notes'] == 'Y')?true:false;
  25. $add_reward_points = ($options['yml_helper_add_reward_points'] == 'Y')?true:false;
  26. $notes_text_available = $options['yml_helper_yml_notes_text_available'];
  27. $notes_text_for_order = $options['yml_helper_yml_notes_text_for_order'];
  28. $analyze_images = ($options['yml_helper_analyze_images'] == 'Y')?true:false;
  29. $analyze_json_images = (($options['yml_helper_analyze_json_images'] == 'Y')&&(Registry::get('addons.exim_json.status') == 'A'))?true:false;
  30. $fields = array('p.product_id','p.amount','p.avail_since');
  31. if ($analyze_json_images) $fields[] = 'p.exim_json_images_q';
  32. if ($check_products) $fields[] = 'pd.product';
  33. if ($check_descriptions) $fields = array_merge($fields,array('pd.full_description','pd.short_description'));
  34. if ($generate_notes&&$add_reward_points) $fields[] = 'pp.price';
  35. $fields = implode(",", $fields);
  36. $allow_product = array(
  37. 'yml_pickup' => $yml_pickup,
  38. 'yml_delivery' => $yml_delivery,
  39. 'yml_store' => $yml_store,
  40. 'yml2_pickup' => $yml_pickup,
  41. 'yml2_delivery' => $yml_delivery,
  42. 'yml2_store' => $yml_store,
  43. 'yml_export_yes' => 'Y',
  44. 'yml2_export_yes' => 'Y'
  45. );
  46. $deny_product = array('yml_export_yes' => 'N', 'yml2_export_yes' => 'N');
  47. $bad_prod_ids = array();
  48. // check product feature variants that contain bad words from list
  49. if ($check_features) {
  50. $bad_variants = array();
  51. $features_where = "";
  52. if (!empty(YML_FEATURE_IDS)) {
  53. $features_where = " WHERE v.feature_id IN (" . implode(",", YML_FEATURE_IDS) . ")";
  54. }
  55. $all_variants = db_get_hash_array("SELECT v.variant_id, vd.variant FROM ?:product_feature_variants AS v
  56. INNER JOIN ?:product_feature_variant_descriptions AS vd ON v.variant_id = vd.variant_id " . $features_where, 'variant_id');
  57. foreach ($all_variants as $variant_id => $options) {
  58. foreach ($features_exempts as $fexempt) {
  59. if (strripos($options['variant'], $fexempt) !== false) {
  60. $bad_variants[] = $variant_id;
  61. break;
  62. }
  63. }
  64. }
  65. if (!empty($bad_variants)) {
  66. $bad_prod_ids = db_get_fields("SELECT DISTINCT product_id FROM ?:product_features_values WHERE variant_id IN (" . implode(",", $bad_variants) . ")");
  67. if (!empty($bad_prod_ids)) {
  68. db_query("UPDATE ?:products SET ?u WHERE product_id IN (" . implode(",", $bad_prod_ids) . ")", $deny_product);
  69. }
  70. }
  71. }
  72. $now = time();
  73. $product_where = " WHERE status != ?s";
  74. $product_join = " INNER JOIN ?:product_descriptions as pd ON p.product_id = pd.product_id";
  75. if (!empty($bad_prod_ids)) {
  76. $product_where .= " AND p.product_id NOT IN (" . implode(",", $bad_prod_ids) . ")";
  77. }
  78. //modify SQL to add reward points to product_notes field
  79. if ($generate_notes&&$add_reward_points) {
  80. $product_join .= " LEFT JOIN ?:product_prices AS pp ON p.product_id = pp.product_id ";
  81. $product_where .= " AND pp.usergroup_id = " . strval(PRICE_USERGROUP_ID);
  82. }
  83. $products = db_get_hash_array("SELECT " . $fields . " FROM ?:products AS p" . $product_join . $product_where, 'product_id', 'D');
  84. foreach ($products as $product_id => $options) {
  85. if ((($options['amount'] < 1)&&(($avail_period = ($options['avail_since'] - $now)) < 0))||($analyze_json_images&&(!intval($options['exim_json_images_q'])))) {
  86. fn_yml_helper_update_product($product_id,$deny_product);
  87. continue;
  88. }
  89. // check products' names and descriptions for bad words and disable them if need
  90. if ($check_products||$check_descriptions) {
  91. $text = $options['product'] . $options['full_description'] . $options['short_description'];
  92. $bad_text = false;
  93. foreach ($product_exempts as $exempt) {
  94. if (strripos($text,$exempt) !== false) {
  95. $bad_text = true;
  96. break;
  97. }
  98. }
  99. if ($bad_text) {
  100. fn_yml_helper_update_product($product_id,$deny_product);
  101. continue;
  102. }
  103. }
  104. $product_data = array();
  105. if ($generate_notes) {
  106. //add reward points
  107. $product_data['yml2_sales_notes'] = ($options['amount'] > 0)?$notes_text_available:$notes_text_for_order;
  108. if ($add_reward_points&&$options['price']) $product_data['yml2_sales_notes'] .= "," . ceil($options['price']*REWARD_POINTS_COEF) . " руб на бонусный счет";
  109. }
  110. if ($generate_delivery) {
  111. // add delivery options
  112. $delivery = ($add_avail&&($options['amount'] < 1))?ceil($avail_period/86400)+$delivery_period:$delivery_period;
  113. $product_data['yml2_delivery_options'] = serialize(array(0 => array('cost' => $delivery_cost, 'days' => strval($delivery), 'order_before' => $delivery_time)));;
  114. }
  115. db_query("UPDATE ?:products SET ?u WHERE product_id = ?i", array_merge($allow_product,$product_data), $product_id);
  116. }
  117. if ($disable_disabled) db_query("UPDATE ?:products SET ?u WHERE status = ?s", $deny_product, 'D');
  118. fn_print_r('Время выполнения : '.(microtime(true) - $start).' сек.');
  119. }
  120. function fn_yml_helper_init_secure_controllers(&$controllers)
  121. {
  122. $controllers['yml_helper'] = 'passive';
  123. }
  124. ?>