Last active
June 30, 2021 06:56
-
-
Save sukhikh18/99feb839bd0d36da045660400f68deb6 to your computer and use it in GitHub Desktop.
Класс для получения / изменения формы редактирования элементов / разделов инфоблока пользователем #Bitrix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$UserOptionFormNews = new UserOptionForm(1); | |
if ($arOptions = $UserOptionFormNews->fetchAll()) { | |
$arOptions['edit1']['NAME'] = 'Новость'; | |
$arOptions['edit1']['PROPS']['PROPERTY_1'] = 'Изображение'; | |
} | |
if ($UserOptionFormNews->save($arOptions, true)) { | |
echo "Success message"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserOptionForm | |
{ | |
const TYPE_ELEMENT = 'element'; | |
const TYPE_SECTION = 'section'; | |
private $name; | |
private $userId; | |
private function getAllowedTypes() | |
{ | |
return [self::TYPE_ELEMENT, self::TYPE_SECTION]; | |
} | |
public function __construct(int $iblockId, $type = '', int $userId = 0) | |
{ | |
// @todo throw exception | |
$allowedTypes = $this->getAllowedTypes(); | |
if (!in_array($type, $allowedTypes)) $type = current($allowedTypes); | |
$this->name = 'form_' . (in_array($type, $allowedTypes) ? $type : current($type)) . '_' . $iblockId; | |
$this->userId = $userId; | |
} | |
public function fetchAll() { | |
$arFormTabs = []; | |
$option = CUserOptions::GetOption('form', $this->name, ['tabs' => ''], $this->userId); | |
$arTabs = explode('--;--', $option['tabs']); | |
if (!empty($arTabs)): | |
foreach ($arTabs as $sTab) | |
{ | |
$arFields = explode('--,--', $sTab); | |
list($sTabId, $sTabName) = explode('--#--', array_shift($arFields)); | |
if (!$sTabId) continue; | |
$arFormTabs[ $sTabId ] = ['NAME' => $sTabName, 'PROPS' => []]; | |
foreach ($arFields as $sField) | |
{ | |
list($propId, $propLabel) = explode('--#--', $sField); | |
$arFormTabs[ $sTabId ]['PROPS'][ $propId ] = $propLabel; | |
} | |
} | |
endif; | |
return $arFormTabs; | |
} | |
private function encode(array $arFormTabs) | |
{ | |
$arTabs = []; | |
foreach ($arFormTabs as $sFormTabCode => $arFormTab) | |
{ | |
$arTab = [$sFormTabCode.'--#--'.$arFormTab['NAME']]; | |
foreach($arFormTab['PROPS'] as $propId => $propLabel) $arTab[] = $propId.'--#--'.$propLabel; | |
$arTabs[] = implode('--,--', $arTab); | |
} | |
return count($arTabs) > 0 ? ['tabs' => implode('--;--', $arTabs) . '--;--'] : []; | |
} | |
public function save(array $_arFormTabs, $clearCurrent = false) | |
{ | |
$arFormTabs = $this->encode($_arFormTabs); | |
if (count($arFormTabs) <= 0) return false; | |
if ($clearCurrent) { | |
// clear for current user | |
CUserOptions::DeleteOption("form", $this->name, $bCommon = false, $user_id = false); | |
} | |
// Save for all | |
return CUserOptions::SetOption("form", $this->name, $arFormTabs, 0 === $this->userId, $this->userId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment