PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/iblock/lib/template/entity/elementproperty.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 402 lines | 317 code | 19 blank | 66 comment | 50 complexity | aae81f6e2a4c11ce66d712dcb82d920e MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage iblock
  6. */
  7. namespace Bitrix\Iblock\Template\Entity;
  8. class ElementProperty extends Base
  9. {
  10. protected $iblockId = 0;
  11. protected $properties = array();
  12. protected $elementLinkProperties = array();
  13. protected $sectionLinkProperties = array();
  14. /**
  15. * @param integer $id Iblock element identifier.
  16. */
  17. public function __construct($id)
  18. {
  19. parent::__construct($id);
  20. }
  21. /**
  22. * Set the iblock of the element.
  23. *
  24. * @param integer $iblockId Iblock identifier.
  25. *
  26. * @return void
  27. */
  28. public function setIblockId($iblockId)
  29. {
  30. $this->iblockId = intval($iblockId);
  31. }
  32. /**
  33. * Used to find entity for template processing.
  34. *
  35. * @param string $entity What to find.
  36. *
  37. * @return \Bitrix\Iblock\Template\Entity\Base
  38. */
  39. public function resolve($entity)
  40. {
  41. if ($this->loadFromDatabase())
  42. {
  43. if (isset($this->elementLinkProperties[$entity]))
  44. {
  45. if (!is_object($this->elementLinkProperties[$entity]))
  46. $this->elementLinkProperties[$entity] = new Element($this->elementLinkProperties[$entity]);
  47. return $this->elementLinkProperties[$entity];
  48. }
  49. elseif (isset($this->sectionLinkProperties[$entity]))
  50. {
  51. if (!is_object($this->sectionLinkProperties[$entity]))
  52. $this->sectionLinkProperties[$entity] = new Element($this->sectionLinkProperties[$entity]);
  53. return $this->sectionLinkProperties[$entity];
  54. }
  55. }
  56. return parent::resolve($entity);
  57. }
  58. /**
  59. * Used to initialize entity fields from some external source.
  60. *
  61. * @param array $fields Entity fields.
  62. *
  63. * @return void
  64. */
  65. public function setFields(array $fields)
  66. {
  67. parent::setFields($fields);
  68. if (
  69. is_array($this->fields)
  70. && $this->iblockId > 0
  71. )
  72. {
  73. $properties = array();
  74. $propertyList = \Bitrix\Iblock\PropertyTable::getList(array(
  75. "select" => array("*"),
  76. "filter" => array("=IBLOCK_ID" => $this->iblockId),
  77. ));
  78. while ($row = $propertyList->fetch())
  79. {
  80. if ($row["USER_TYPE_SETTINGS"])
  81. $row["USER_TYPE_SETTINGS"] = unserialize($row["USER_TYPE_SETTINGS"]);
  82. $properties[$row["ID"]] = $row;
  83. if ($row["CODE"] != "")
  84. $properties[$row["CODE"]] = &$properties[$row["ID"]];
  85. }
  86. foreach ($fields as $propertyCode => $propertyValues)
  87. {
  88. if (is_array($propertyValues))
  89. {
  90. foreach ($propertyValues as $i => $propertyValue)
  91. {
  92. if (is_array($propertyValue) && array_key_exists("VALUE", $propertyValue))
  93. {
  94. if ($propertyValue["VALUE"] != "")
  95. $propertyValues[$i] = $propertyValue["VALUE"];
  96. else
  97. unset($propertyValues[$i]);
  98. }
  99. }
  100. }
  101. if (isset($properties[$propertyCode]))
  102. {
  103. $property = $properties[$propertyCode];
  104. $fieldCode = strtolower($propertyCode);
  105. if ($property["PROPERTY_TYPE"] === "L")
  106. {
  107. if (is_numeric($propertyValues))
  108. {
  109. $value = new ElementPropertyEnum($propertyValues);
  110. }
  111. elseif (is_array($propertyValues))
  112. {
  113. $value = array();
  114. foreach ($propertyValues as $propertyValue)
  115. {
  116. if (is_numeric($propertyValue))
  117. $value[] = new ElementPropertyEnum($propertyValue);
  118. }
  119. }
  120. else
  121. {
  122. $value = $propertyValues;
  123. }
  124. }
  125. elseif ($property["PROPERTY_TYPE"] === "E")
  126. {
  127. if ($propertyValues instanceof Element)
  128. {
  129. $this->elementLinkProperties[$fieldCode] = $propertyValues;
  130. $value = $propertyValues->getField("name");
  131. }
  132. elseif (is_numeric($propertyValues))
  133. {
  134. $this->elementLinkProperties[$fieldCode] = $propertyValues;
  135. $value = new ElementPropertyElement($propertyValues);
  136. }
  137. elseif (is_array($propertyValues))
  138. {
  139. $value = array();
  140. foreach ($propertyValues as $propertyValue)
  141. {
  142. if (is_numeric($propertyValue))
  143. $value[] = new ElementPropertyElement($propertyValue);
  144. }
  145. }
  146. else
  147. {
  148. $value = $propertyValues;
  149. }
  150. }
  151. elseif ($property["PROPERTY_TYPE"] === "G")
  152. {
  153. if ($propertyValues instanceof Section)
  154. {
  155. $this->sectionLinkProperties[$fieldCode] = $propertyValues;
  156. $value = $propertyValues->getField("name");
  157. }
  158. elseif (is_numeric($propertyValues))
  159. {
  160. $this->sectionLinkProperties[$fieldCode] = $propertyValues;
  161. $value = new ElementPropertySection($propertyValues);
  162. }
  163. elseif (is_array($propertyValues))
  164. {
  165. $value = array();
  166. foreach ($propertyValues as $propertyValue)
  167. {
  168. if (is_numeric($propertyValue))
  169. $value[] = new ElementPropertySection($propertyValue);
  170. }
  171. }
  172. else
  173. {
  174. $value = $propertyValues;
  175. }
  176. }
  177. else
  178. {
  179. if(strlen($property["USER_TYPE"]))
  180. {
  181. $value = new ElementPropertyUserField($propertyValues, $property);
  182. }
  183. else
  184. {
  185. $value = $propertyValues;
  186. }
  187. }
  188. $this->fieldMap[$fieldCode] = $property["ID"];
  189. $this->fieldMap[$property["ID"]] = $property["ID"];
  190. if ($property["CODE"] != "")
  191. $this->fieldMap[strtolower($property["CODE"])] = $property["ID"];
  192. $this->fields[$property["ID"]] = $value;
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * Loads values from database.
  199. * Returns true on success.
  200. *
  201. * @return boolean
  202. */
  203. protected function loadFromDatabase()
  204. {
  205. if (!isset($this->fields) && $this->iblockId > 0)
  206. {
  207. $this->fields = array();
  208. $this->fieldMap = array();
  209. $propertyList = \CIBlockElement::getProperty(
  210. $this->iblockId,
  211. $this->id,
  212. array("sort" => "asc"),
  213. array("EMPTY" => "N")
  214. );
  215. while ($property = $propertyList->fetch())
  216. {
  217. if ($property["VALUE_ENUM"] != "")
  218. {
  219. $value = $property["VALUE_ENUM"];
  220. }
  221. elseif ($property["PROPERTY_TYPE"] === "E")
  222. {
  223. $this->elementLinkProperties[$property["ID"]] = $property["VALUE"];
  224. if ($property["CODE"] != "")
  225. $this->elementLinkProperties[strtolower($property["CODE"])] = $property["VALUE"];
  226. $value = new ElementPropertyElement($property["VALUE"]);
  227. }
  228. elseif ($property["PROPERTY_TYPE"] === "G")
  229. {
  230. $this->sectionLinkProperties[$property["ID"]] = $property["VALUE"];
  231. if ($property["CODE"] != "")
  232. $this->sectionLinkProperties[strtolower($property["CODE"])] = $property["VALUE"];
  233. $value = new ElementPropertySection($property["VALUE"]);
  234. }
  235. else
  236. {
  237. if(strlen($property["USER_TYPE"]))
  238. {
  239. $value = new ElementPropertyUserField($property["VALUE"], $property);
  240. }
  241. else
  242. {
  243. $value = $property["VALUE"];
  244. }
  245. }
  246. $this->fieldMap[$property["ID"]] = $property["ID"];
  247. if ($property["CODE"] != "")
  248. $this->fieldMap[strtolower($property["CODE"])] = $property["ID"];
  249. if ($property["MULTIPLE"] == "Y")
  250. $this->fields[$property["ID"]][] = $value;
  251. else
  252. $this->fields[$property["ID"]] = $value;
  253. }
  254. }
  255. return is_array($this->fields);
  256. }
  257. }
  258. class ElementPropertyUserField extends LazyValueLoader
  259. {
  260. /** @var array */
  261. private $property = null;
  262. /**
  263. * @param integer $key Iblock element identifier.
  264. * @param array|mixed $property Iblock property array.
  265. */
  266. function __construct($key, $property)
  267. {
  268. parent::__construct($key);
  269. if (is_array(($property)))
  270. {
  271. $this->property = $property;
  272. }
  273. }
  274. /**
  275. * Actual work method which have to retrieve data from the DB.
  276. *
  277. * @return mixed
  278. */
  279. protected function load()
  280. {
  281. $propertyFormatFunction = $this->getFormatFunction();
  282. if ($propertyFormatFunction)
  283. {
  284. return call_user_func_array($propertyFormatFunction,
  285. array(
  286. $this->property,
  287. array("VALUE" => $this->key),
  288. array("MODE" => "ELEMENT_TEMPLATE"),
  289. )
  290. );
  291. }
  292. else
  293. {
  294. return $this->key;
  295. }
  296. }
  297. /**
  298. * Retruns GetPublicViewHTML handler function for $this->property.
  299. * Returns false if no handler defined.
  300. *
  301. * @return callable|false
  302. */
  303. protected function getFormatFunction()
  304. {
  305. static $propertyFormatFunction = null;
  306. if (!isset($propertyFormatFunction))
  307. {
  308. $propertyFormatFunction = false;
  309. if ($this->property && strlen($this->property["USER_TYPE"]))
  310. {
  311. $propertyUserType = \CIBlockProperty::getUserType($this->property["USER_TYPE"]);
  312. if(
  313. array_key_exists("GetPublicViewHTML", $propertyUserType)
  314. && is_callable($propertyUserType["GetPublicViewHTML"])
  315. )
  316. {
  317. $propertyFormatFunction = $propertyUserType["GetPublicViewHTML"];
  318. }
  319. }
  320. }
  321. return $propertyFormatFunction;
  322. }
  323. }
  324. class ElementPropertyEnum extends LazyValueLoader
  325. {
  326. /**
  327. * Actual work method which have to retrieve data from the DB.
  328. *
  329. * @return mixed
  330. */
  331. protected function load()
  332. {
  333. $enumList = \Bitrix\Iblock\PropertyEnumerationTable::getList(array(
  334. "select" => array("VALUE"),
  335. "filter" => array("=ID" => $this->key),
  336. ));
  337. $enum = $enumList->fetch();
  338. if ($enum)
  339. return $enum["VALUE"];
  340. else
  341. return "";
  342. }
  343. }
  344. class ElementPropertyElement extends LazyValueLoader
  345. {
  346. /**
  347. * Actual work method which have to retrieve data from the DB.
  348. *
  349. * @return mixed
  350. */
  351. protected function load()
  352. {
  353. $elementList = \Bitrix\Iblock\ElementTable::getList(array(
  354. "select" => array("NAME"),
  355. "filter" => array("=ID" => $this->key),
  356. ));
  357. $element = $elementList->fetch();
  358. if ($element)
  359. return $element["NAME"];
  360. else
  361. return "";
  362. }
  363. }
  364. class ElementPropertySection extends LazyValueLoader
  365. {
  366. /**
  367. * Actual work method which have to retrieve data from the DB.
  368. *
  369. * @return mixed
  370. */
  371. protected function load()
  372. {
  373. $sectionList = \Bitrix\Iblock\SectionTable::getList(array(
  374. "select" => array("NAME"),
  375. "filter" => array("=ID" => $this->key),
  376. ));
  377. $section = $sectionList->fetch();
  378. if ($section)
  379. return $section["NAME"];
  380. else
  381. return "";
  382. }
  383. }