Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
enum.php
1<?php
2
4
7
8Loc::loadMessages(__FILE__);
9
10class Enum extends Base
11{
12 public function __construct($id, array $structure, $currency, $value = null, array $additionalParams = array())
13 {
14 $prices = !empty($structure["PARAMS"]["PRICES"]) && is_array($structure["PARAMS"]["PRICES"]) ? $structure["PARAMS"]["PRICES"] : array();
15 $structure["PARAMS"]["ONCHANGE"] = $this->createJSOnchange($id, $prices);
16 parent::__construct($id, $structure, $currency, $value);
17 $this->params["TYPE"] = "ENUM";
18 $this->params["OPTIONS"] = array();
19 }
20
21 public static function getClassTitle()
22 {
23 return Loc::getMessage("DELIVERY_EXTRA_SERVICE_ENUM_TITLE");
24 }
25
26 public function getCost()
27 {
28 if (
29 !isset($this->params["PRICES"])
30 || !is_array($this->params["PRICES"])
31 )
32 {
33 throw new SystemException("Service id: " . $this->id . " doesn't have field array PRICES");
34 }
35
36 if (isset($this->params["PRICES"][$this->value]["PRICE"]))
37 {
38 $result = $this->params["PRICES"][$this->value]["PRICE"];
39 }
40 else
41 {
42 $row = reset($this->params["PRICES"]);
43 $result =$row["PRICE"] ?? 0;
44 }
45
46 return $this->convertToOperatingCurrency($result);
47 }
48
49 public static function prepareParamsToSave(array $params)
50 {
51 if(!isset($params["PARAMS"]["PRICES"]) || !is_array($params["PARAMS"]["PRICES"]))
52 return $params;
53
54 foreach($params["PARAMS"]["PRICES"] as $id => $price)
55 if($price["TITLE"] == '')
56 unset($params["PARAMS"]["PRICES"][$id]);
57
58 return $params;
59 }
60
61 public static function getAdminParamsName()
62 {
63 return Loc::getMessage("DELIVERY_EXTRA_SERVICE_ENUM_LIST");
64 }
65
66 public static function getAdminParamsControl($name, array $params, $currency = "")
67 {
68 $result = '<div style="border: 1px solid #e0e8ea; padding: 10px; width: 500px;">';
69
70 if(isset($params["PARAMS"]["PRICES"]) && is_array($params["PARAMS"]["PRICES"]))
71 {
72 foreach($params["PARAMS"]["PRICES"] as $id => $price)
73 {
74 if(!isset($params["PARAMS"]["PRICES"][$id]))
75 $params["PARAMS"]["PRICES"][$id] = 0;
76
77 $result .= self::getValueHtml($name, $id, $price["TITLE"], $price["PRICE"] ?? 0, $currency)."<br><br>";
78 }
79 }
80
81 $i = strval(time());
82 $result .= self::getValueHtml($name, $i, "", "", $currency)."<br><br>".
83 '<input type="button" value="'.Loc::getMessage("DELIVERY_EXTRA_SERVICE_ENUM_ADD").
84 '" onclick=\'var d=new Date(); '.
85 'this.parentNode.insertBefore(BX.create("span",{html: this.nextElementSibling.innerHTML.replace(/\#ID\#/g, d.getTime())}), this);\'>'.
86 '<span style="display:none;">'.self::getValueHtml($name, '#ID#')."<br><br>".'</span><br><br></div>';
87
88 return $result;
89 }
90
91 protected static function getValueHtml($name, $id, $title = "", $price = "", $currency = "")
92 {
93 $price = roundEx((float)$price, SALE_VALUE_PRECISION);
94 $currency = htmlspecialcharsbx((string)$currency);
95
96 return Loc::getMessage("DELIVERY_EXTRA_SERVICE_ENUM_NAME").
97 ':&nbsp;<input name="'.$name.'[PARAMS][PRICES]['.$id.'][TITLE]" value="'.htmlspecialcharsbx($title).'">&nbsp;&nbsp;'.
98 Loc::getMessage("DELIVERY_EXTRA_SERVICE_ENUM_PRICE").
99 ':&nbsp;<input name="'.$name.'[PARAMS][PRICES]['.$id.'][PRICE]" value="'.$price.'">'.($currency <> '' ? " (".$currency.")" : "");
100 }
101
102 protected static function getJSPrice(array $prices)
103 {
104 if(empty($prices))
105 return "";
106
107 foreach($prices as $id => $price)
108 $prices[$id] = roundEx(floatval($price), SALE_VALUE_PRECISION);
109
110 return "(function(value){var prices=".\CUtil::PhpToJSObject($prices)."; return prices[value]['PRICE'];})(this.value)";
111 }
112
113 public function setOperatingCurrency($currency)
114 {
115 if(!empty($this->params["PRICES"]) && is_array($this->params["PRICES"]))
116 {
117 $prices = array();
118
119 foreach($this->params["PRICES"] as $id => $price)
120 $prices[$id] = $this->convertToOperatingCurrency($price);
121
122 $this->params["ONCHANGE"] = $this->createJSOnchange($this->id, $prices);
123 }
124
125 $this->createOptions();
126 parent::setOperatingCurrency($currency);
127 }
128
129 protected function createOptions()
130 {
131 $this->params["OPTIONS"] = [];
132
133 if (empty($this->params["PRICES"]) || !is_array($this->params["PRICES"]))
134 {
135 return;
136 }
137
138 foreach ($this->params["PRICES"] as $key => $price)
139 {
140 if (!is_array($price))
141 {
142 continue;
143 }
144 $priceTitle = trim((string)($price['TITLE'] ?? ''));
145 if ($priceTitle === '')
146 {
147 continue;
148 }
149
150 $priceVal = (float)($price['PRICE'] ?? 0);
151 $this->params['OPTIONS'][$key] =
152 htmlspecialcharsbx($price['TITLE'])
153 . ' ('
154 . strip_tags(
155 SaleFormatCurrency(
156 $this->convertToOperatingCurrency($priceVal),
157 $this->operatingCurrency,
158 false
159 )
160 )
161 . ')'
162 ;
163 }
164 }
165
166 public function getEditControl($prefix = "", $value = false)
167 {
168 $this->createOptions();
169 return parent::getEditControl($prefix, $value);
170 }
171
172 public function getViewControl()
173 {
174 $this->createOptions();
175 return parent::getViewControl();
176 }
177
178 protected function createJSOnchange($id, array $prices)
179 {
180 return "BX.onCustomEvent('onDeliveryExtraServiceValueChange', [{'id' : '".$id."', 'value': this.value, 'price': ".$this->getJSPrice($prices)."}]);";
181 }
182
186 public function getDisplayValue(): ?string
187 {
188 return isset($this->params['PRICES'][$this->value])
189 ? (string)$this->params['PRICES'][$this->value]['TITLE']
190 : null;
191 }
192}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
getEditControl($prefix="", $value=false)
Definition enum.php:166
static getValueHtml($name, $id, $title="", $price="", $currency="")
Definition enum.php:91
__construct($id, array $structure, $currency, $value=null, array $additionalParams=array())
Definition enum.php:12
static getJSPrice(array $prices)
Definition enum.php:102
static getAdminParamsControl($name, array $params, $currency="")
Definition enum.php:66
createJSOnchange($id, array $prices)
Definition enum.php:178
static prepareParamsToSave(array $params)
Definition enum.php:49