PageRenderTime 22ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sale/general/helper.php

https://gitlab.com/alexprowars/bitrix
PHP | 469 lines | 355 code | 91 blank | 23 comment | 66 complexity | e93475a787b5d6338a7a5eb27abbb3e7 MD5 | raw file
  1. <?
  2. IncludeModuleLangFile(__FILE__);
  3. class CSaleHelper
  4. {
  5. public static function IsAssociativeArray($ar)
  6. {
  7. if (count($ar) <= 0)
  8. return false;
  9. $fl = false;
  10. $arKeys = array_keys($ar);
  11. $ind = -1;
  12. foreach ($arKeys as $key)
  13. {
  14. $ind++;
  15. if ($key."!" !== $ind."!" && "".$key !== "n".$ind)
  16. {
  17. $fl = true;
  18. break;
  19. }
  20. }
  21. return $fl;
  22. }
  23. /**
  24. * Writes to /bitrix/modules/sale.log
  25. *
  26. * @param string $text message to write
  27. * @param array $arVars array (varname => value) to print out variables
  28. * @param string $code log record tag
  29. */
  30. public static function WriteToLog($text, $arVars = array(), $code = "")
  31. {
  32. $filename = $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/sale.log";
  33. if ($f = fopen($filename, "a"))
  34. {
  35. fwrite($f, date("Y-m-d H:i:s")." - ".$code." - ".$text."\n");
  36. if (is_array($arVars))
  37. {
  38. foreach ($arVars as $varName => $varData)
  39. {
  40. fwrite($f, $varName.": ");
  41. fwrite($f, print_r($varData, true));
  42. fwrite($f, "\n");
  43. }
  44. }
  45. fwrite($f, "\n");
  46. fclose($f);
  47. }
  48. }
  49. /**
  50. * @param $fieldId
  51. * @param $arField
  52. * @param $fieldName
  53. * @param $formName
  54. * @return string
  55. * @deprecated
  56. */
  57. public static function getAdminHtml($fieldId, $arField, $fieldName, $formName)
  58. {
  59. $arField["VALUE"] = CSaleDeliveryHelper::getConfValue($arField);
  60. $resultHtml = '';
  61. $name = htmlspecialcharsbx($fieldName.($fieldId <> '' ? '['.$fieldId.']' : ''));
  62. if(isset($arField['PRE_TEXT']))
  63. $resultHtml = $arField['PRE_TEXT'].' ';
  64. if(isset($arField['BLOCK_HIDEABLE']))
  65. $resultHtml .= '<a href="javascript:void(0);" style="border-bottom: 1px dashed; text-decoration: none;">';
  66. switch ($arField["TYPE"])
  67. {
  68. case "TEXT_RO": //read only text
  69. $resultHtml .= htmlspecialcharsbx($arField["VALUE"]);
  70. break;
  71. case "CHECKBOX":
  72. $resultHtml .= '<input '.
  73. 'type="checkbox" '.
  74. 'name="'.$name.'" '.
  75. 'value="Y" '.
  76. ($arField["VALUE"] == "Y" ? "checked=\"checked\"" : "");
  77. if(isset($arField['HIDE_BY_NAMES']) && is_array($arField['HIDE_BY_NAMES']))
  78. $resultHtml .= 'onclick="hideFormElementsByNames(this, '.CUtil::PhpToJSObject($arField['HIDE_BY_NAMES']).');"';
  79. $resultHtml .= '/>';
  80. if(isset($arField['HIDE_BY_NAMES']) && is_array($arField['HIDE_BY_NAMES']))
  81. {
  82. $resultHtml .= '
  83. <script language="JavaScript">
  84. BX.ready(
  85. function(){
  86. var cbObj = document.forms["'.$formName.'"]["'.$name.'"];
  87. if(cbObj)
  88. hideFormElementsByNames(cbObj, '.CUtil::PhpToJSObject($arField['HIDE_BY_NAMES']).');
  89. }
  90. );
  91. </script>';
  92. }
  93. break;
  94. case "RADIO":
  95. foreach ($arField["VALUES"] as $value => $title)
  96. {
  97. $resultHtml .= '<input type="radio"
  98. id="hc_'.htmlspecialcharsbx($fieldId).'_'.htmlspecialcharsbx($value).'"'.
  99. 'name="'.$name.'" '.
  100. 'value="'.htmlspecialcharsbx($value).'"'.
  101. ($value == $arField["VALUE"] ? " checked=\"checked\"" : "").' />'.
  102. '<label for="hc_'.htmlspecialcharsbx($fieldId).'_'.htmlspecialcharsbx($value).'">'.
  103. htmlspecialcharsbx($title).'</label><br />';
  104. }
  105. break;
  106. case "PASSWORD":
  107. $resultHtml .= '<input '.
  108. 'type="password" '.
  109. 'name="'.$name.'" '.
  110. 'value="'.htmlspecialcharsbx($arField["VALUE"]).'" />';
  111. break;
  112. case "DROPDOWN":
  113. $resultHtml .= '<select name="'.$name.'" ';
  114. if(isset($arField['ONCHANGE']))
  115. $resultHtml .= ' onchange = "'.$arField['ONCHANGE'].'"';
  116. $resultHtml .='>';
  117. foreach ($arField["VALUES"] as $value => $title)
  118. {
  119. $resultHtml .= '<option '.
  120. 'value="'.htmlspecialcharsbx($value).'"'.
  121. ($value == $arField["VALUE"] ? " selected=\"selected\"" : "").'>'.
  122. htmlspecialcharsbx($title).
  123. '</option>';
  124. }
  125. $resultHtml .= '</select>';
  126. break;
  127. case "MULTISELECT":
  128. $resultHtml .= '<select name="'.$name.'" multiple="multiple">';
  129. foreach ($arField["VALUES"] as $value => $title)
  130. $resultHtml .= '<option '.
  131. 'value="'.htmlspecialcharsbx($value).'"'.
  132. (in_array($value, $arField["VALUE"]) ? " selected=\"selected\"" : "").'>'.
  133. htmlspecialcharsbx($title).
  134. '</option>';
  135. $resultHtml .= '</select>';
  136. break;
  137. case "SECTION":
  138. case "TEXT_CENTERED":
  139. case "MULTI_CONTROL_STRING":
  140. $resultHtml .= htmlspecialcharsbx($arField["TITLE"]);
  141. break;
  142. case "CUSTOM":
  143. $resultHtml .= $arField["VALUE"];
  144. break;
  145. default:
  146. $resultHtml .= '<input type="text"'.
  147. 'name="'.$name.'" '.
  148. 'value="'.htmlspecialcharsbx($arField["VALUE"]).'" '.
  149. (isset($arField["SIZE"]) ? 'size="'.$arField["SIZE"].'"' : '').
  150. '/>';
  151. }
  152. if(isset($arField['BLOCK_HIDEABLE']))
  153. $resultHtml .= '</a>';
  154. if(isset($arField['POST_TEXT'])):
  155. $resultHtml .= ' '.$arField['POST_TEXT'];
  156. endif;
  157. return $resultHtml;
  158. }
  159. public static function getAdminMultilineControl($arMultiControlQuery)
  160. {
  161. $resultHtml = '';
  162. if(is_array($arMultiControlQuery))
  163. {
  164. reset($arMultiControlQuery);
  165. $key = key($arMultiControlQuery);
  166. if(isset($arMultiControlQuery[$key]['ITEMS']) && isset($arMultiControlQuery[$key]['CONFIG']))
  167. {
  168. $multiHtml = implode(' ', $arMultiControlQuery[$key]['ITEMS']);
  169. $resultHtml = self::wrapAdminHtml($multiHtml, $arMultiControlQuery[$key]['CONFIG']);
  170. }
  171. }
  172. return $resultHtml;
  173. }
  174. public static function wrapAdminHtml($controlHtml, &$arConfig)
  175. {
  176. $wrapHtml = '';
  177. $tdStyle = isset($arConfig["TOP_LINE"]) && $arConfig["TOP_LINE"] == "Y" ? ' border-top: 1px solid #DDDDDD;' : '';
  178. switch ($arConfig["TYPE"])
  179. {
  180. case "SECTION":
  181. $wrapHtml .= '<tr class="heading"><td colspan="2">'.$controlHtml.'</td></tr>';
  182. break;
  183. case "TEXT_CENTERED":
  184. $wrapHtml .= '<tr';
  185. if(isset($arConfig["BLOCK_HIDEABLE"]))
  186. $wrapHtml .= ' onclick="BX.Sale.PaySystem.toggleNextSiblings(this,'.intval($arConfig["BLOCK_LENGTH"]).');" class="ps-admin-hide" ';
  187. $wrapHtml .= '><td style="text-align: center; font-weight: bold;'.$tdStyle.'" colspan="2">'.$controlHtml;
  188. if(isset($arConfig["BLOCK_DELETABLE"]))
  189. $wrapHtml .= '&nbsp;&nbsp;<a href="javascript:void(0);" onclick="BX.Sale.PaySystem.deleteObjectAndNextSiblings(this,'.intval($arConfig["BLOCK_LENGTH"]).',2);" style="border-bottom: 1px dashed; text-decoration: none;">'.GetMessage("SALE_HELPER_DELETE").'</a>';
  190. $wrapHtml .= '</td></tr>';
  191. break;
  192. default:
  193. $wrapHtml .= '<tr>'.
  194. '<td style="'.$tdStyle.'" class="field-name"'.(($arConfig["TYPE"] == "MULTISELECT") ? ' valign="top"' : '').' width="40%" align="right">'.htmlspecialcharsbx($arConfig["TITLE"]).':</td>'.
  195. '<td style="'.$tdStyle.'" valign="top" width="60%">'.
  196. $controlHtml.
  197. '</td>'.
  198. '</tr>';
  199. }
  200. return $wrapHtml;
  201. }
  202. public static function getOptionOrImportValues($optName, $importFuncName = false, $arFuncParams = array(), $siteId = "")
  203. {
  204. $arResult = array();
  205. if(trim($optName) !== '')
  206. {
  207. $optValue = COption::GetOptionString('sale', $optName, '', $siteId);
  208. $arOptValue = unserialize($optValue, ['allowed_classes' => false]);
  209. if(empty($arOptValue))
  210. {
  211. if($importFuncName !== false && is_callable($importFuncName))
  212. {
  213. $arResult = call_user_func_array($importFuncName, $arFuncParams);
  214. COption::SetOptionString('sale', $optName, serialize($arResult), false, $siteId);
  215. }
  216. }
  217. else
  218. {
  219. $arResult = $arOptValue;
  220. }
  221. }
  222. return $arResult;
  223. }
  224. private static function getShopLocationParams($siteId = false)
  225. {
  226. $loc_diff = COption::GetOptionString('sale', 'ADDRESS_different_set', 'N');
  227. if ($loc_diff == "Y" && ($siteId !== false || defined(SITE_ID)))
  228. {
  229. if($siteId === false)
  230. $siteId = SITE_ID;
  231. $locId = COption::GetOptionString('sale', 'location', '', $siteId);
  232. $locZip = COption::GetOptionString('sale', 'location_zip', '', $siteId);
  233. }
  234. else
  235. {
  236. $locId = COption::GetOptionString('sale', 'location', '');
  237. $locZip = COption::GetOptionString('sale', 'location_zip', '');
  238. if($locId == '')
  239. {
  240. static $defSite = null;
  241. if (!isset($defSite))
  242. $defSite = CSite::GetDefSite();
  243. if($defSite)
  244. {
  245. $locId = COption::GetOptionString('sale', 'location', '', $defSite);
  246. $locZip = COption::GetOptionString('sale', 'location_zip', '', $defSite);
  247. }
  248. }
  249. }
  250. if((string) $locId != '')
  251. {
  252. $location = self::getLocationByIdHitCached($locId);
  253. if(intval($location['ID']))
  254. $locId = $location['ID'];
  255. }
  256. return array(
  257. 'ID' => $locId,
  258. 'ZIP' => $locZip
  259. );
  260. }
  261. public static function getShopLocationId($siteId)
  262. {
  263. static $shopLocationId = array();
  264. if(!isset($shopLocationId[$siteId]))
  265. {
  266. $locParams = self::getShopLocationParams($siteId);
  267. if(isset($locParams['ID']) && $locParams['ID'] <> '')
  268. $shopLocationId[$siteId] = $locParams['ID'];
  269. }
  270. return $shopLocationId[$siteId];
  271. }
  272. public static function getShopLocationZIP()
  273. {
  274. static $shopLocationZip = '';
  275. if($shopLocationZip == '')
  276. {
  277. $locParams = self::getShopLocationParams();
  278. if(isset($locParams['ZIP']) && $locParams['ZIP'] <> '')
  279. $shopLocationZip = strval($locParams['ZIP']);
  280. }
  281. return $shopLocationZip;
  282. }
  283. public static function getShopLocation($siteId = false)
  284. {
  285. static $shopLocation = array();
  286. if(empty($shopLocation))
  287. {
  288. $shopLocationId = self::getShopLocationId($siteId);
  289. if(intval($shopLocationId) > 0)
  290. $shopLocation = CSaleLocation::GetByID($shopLocationId);
  291. }
  292. return $shopLocation;
  293. }
  294. public static function getCsvObject($filePath)
  295. {
  296. $csvFile = new CCSVData();
  297. $csvFile->LoadFile($filePath);
  298. $csvFile->SetFieldsType("R");
  299. $csvFile->SetFirstHeader(false);
  300. $csvFile->SetDelimiter(",");
  301. return $csvFile;
  302. }
  303. /**
  304. * Returns HTML code to show file (image or download link)
  305. * Similar to CFile::ShowFile but shows name of the file in the download link
  306. *
  307. * @param int $fileId - file id
  308. * @param array $arSize - width and height for image thumbnail
  309. * @return string
  310. */
  311. public static function getFileInfo($fileId, $arSize = array("WIDTH" => 90, "HEIGHT" => 90))
  312. {
  313. $resultHTML = "";
  314. $arFile = CFile::GetFileArray($fileId);
  315. if ($arFile)
  316. {
  317. $is_image = CFile::IsImage($arFile["FILE_NAME"], $arFile["CONTENT_TYPE"]);
  318. if ($is_image)
  319. $resultHTML = CFile::ShowImage($arFile["ID"], $arSize["WIDTH"], $arSize["HEIGHT"], "border=0", $arFile["SRC"], true);
  320. else
  321. $resultHTML = '<a href="'.$arFile["SRC"].'">'.$arFile["ORIGINAL_NAME"].'</a>';
  322. }
  323. return $resultHTML;
  324. }
  325. public static function getIblockPropInfo($value, $propData, $arSize = array("WIDTH" => 90, "HEIGHT" => 90))
  326. {
  327. $res = "";
  328. if ($propData["MULTIPLE"] == "Y")
  329. {
  330. $arVal = array();
  331. if (!is_array($value))
  332. {
  333. if (mb_strpos($value, ",") !== false)
  334. $arVal = explode(",", $value);
  335. else
  336. $arVal[] = $value;
  337. }
  338. else
  339. $arVal = $value;
  340. if (count($arVal) > 0)
  341. {
  342. foreach ($arVal as $key => $val)
  343. {
  344. if ($propData["PROPERTY_TYPE"] == "F")
  345. {
  346. if ($res <> '')
  347. $res .= "<br/> ".CSaleHelper::getFileInfo(trim($val), $arSize);
  348. else
  349. $res = CSaleHelper::getFileInfo(trim($val), $arSize);
  350. }
  351. else
  352. {
  353. if ($res <> '')
  354. $res .= ", ".$val;
  355. else
  356. $res = $val;
  357. }
  358. }
  359. }
  360. }
  361. else
  362. {
  363. if ($propData["PROPERTY_TYPE"] == "F")
  364. $res = CSaleHelper::getFileInfo($value, $arSize);
  365. else
  366. $res = $value;
  367. }
  368. return $res;
  369. }
  370. public static function getLocationByIdHitCached($id)
  371. {
  372. static $result = array();
  373. if(!isset($result[$id]))
  374. $result[$id] = CSaleLocation::GetByIDForLegacyDelivery($id);
  375. return $result[$id];
  376. }
  377. }