Advertisement
Trigub_Ilia

Получение списка полей реквизитов

Jun 1st, 2021
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.40 KB | None | 0 0
  1. use \Bitrix\Crm;
  2.  
  3. \Bitrix\Main\Loader::includeModule('crm');
  4.  
  5. $result = array();
  6.  
  7. $preset = \Bitrix\Crm\EntityPreset::getSingleInstance();
  8.  
  9. $entityTypeId = \Bitrix\Crm\EntityPreset::Requisite;
  10.  
  11. $requisite = \Bitrix\Crm\EntityRequisite::getSingleInstance();
  12.  
  13. $entityFieldsTitles = $requisite->getFieldsTitles(1);
  14.  
  15. $countryList = array();
  16. foreach (\Bitrix\Crm\EntityPreset::getCountryList() as $k => $v)
  17.     $countryList[$k] = $v;
  18.  
  19.  
  20. if ($preset->checkEntityType($entityTypeId))
  21. {
  22.     $fieldsAllowed = array();
  23.     foreach (array_merge($requisite->getRqFields(), $requisite->getUserFields()) as $fieldName)
  24.     {
  25.         $fieldsAllowed[$fieldName] = true;
  26.     }
  27.     $presetList = $requisite->getFixedPresetList();
  28.  
  29.     $res = $preset->getList(array(
  30.         'order' => array('SORT' => 'ASC', 'ID' => 'ASC'),
  31.         'filter' => array('=ENTITY_TYPE_ID' => $entityTypeId, '=ACTIVE' => 'Y', '!ID' => 1),
  32.         'select' => array('ID', 'NAME', 'COUNTRY_ID', 'SETTINGS')
  33.     ));
  34.     while ($row = $res->fetch())
  35.     {
  36.         $presetTitle = trim(strval($row['NAME']));
  37.         if (empty($presetTitle))
  38.             $row['NAME'] = '['.$row['ID'].'] - '.GetMessage('CRM_REQUISITE_PRESET_NAME_EMPTY');
  39.         $presetList[] = $row;
  40.     }
  41.  
  42.     $topItems = array(array('id' => '', 'title' => GetMessage('CRM_PRESET_EDIT_SELECT_FIELDS_NONE')));
  43.     $items = array();
  44.     $otherItems = array();
  45.     $usedFields = array();
  46.     foreach ($presetList as $row)
  47.     {
  48.         if (1 === intval($row['COUNTRY_ID']) && is_array($row['SETTINGS']))
  49.         {
  50.             $fields = $preset->settingsGetFields($row['SETTINGS']);
  51.             if (!empty($fields))
  52.             {
  53.                 $countryId = isset($row['COUNTRY_ID']) ? (int)$row['COUNTRY_ID'] : 0;
  54.                 $countryPostfix = isset($countryList[$countryId]) ?
  55.                     " ({$countryList[$countryId]})" : '';
  56.                 $g = $i = 0;
  57.                 foreach ($fields as $fieldInfo)
  58.                 {
  59.                     if (isset($fieldsAllowed[$fieldInfo['FIELD_NAME']]))
  60.                     {
  61.                         if ($g === 0)
  62.                         {
  63.                             $groupItem = array('type' => 'group', 'title' => $row['NAME'].$countryPostfix);
  64.                             if ($countryId === 1)
  65.                             {
  66.                                 $topItems[] = $groupItem;
  67.                             }
  68.                             else
  69.                             {
  70.                                 $items[] = $groupItem;
  71.                             }
  72.                             $g++;
  73.                         }
  74.                         $title = $entityFieldsTitles[$fieldInfo['FIELD_NAME']];
  75.                         if (!empty($title))
  76.                         {
  77.                             $item = array(
  78.                                 'id' => $fieldInfo['FIELD_NAME'],
  79.                                 'title' => $title
  80.                             );
  81.                             if ($countryId === 1)
  82.                             {
  83.                                 $topItems[] = $item;
  84.                             }
  85.                             else
  86.                             {
  87.                                 $items[] = $item;
  88.                             }
  89.                             $usedFields[$fieldInfo['FIELD_NAME']] = true;
  90.                             $i++;
  91.                         }
  92.                     }
  93.                 }
  94.                 if ($i === 0 && $g === 1)
  95.                 {
  96.                     if ($countryId === 1)
  97.                     {
  98.                         unset($topItems[key($topItems)]);
  99.                     }
  100.                     else
  101.                     {
  102.                         unset($items[key($items)]);
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     $g = $i = 0;
  109.     foreach (array_keys($fieldsAllowed) as $fieldName)
  110.     {
  111.         if (!isset($usedFields[$fieldName]))
  112.         {
  113.             if ($g === 0)
  114.             {
  115.                 $otherItems[] = array(
  116.                     'type' => 'group', 'title' => GetMessage('CRM_PRESET_EDIT_SELECT_OTHER_FIELDS')
  117.                 );
  118.                 $g++;
  119.             }
  120.             $title = $entityFieldsTitles[$fieldName];
  121.             if (!empty($title))
  122.             {
  123.                 $otherItems[] = $item = array(
  124.                     'id' => $fieldName,
  125.                     'title' => $entityFieldsTitles[$fieldName]
  126.                 );
  127.                 $i++;
  128.             }
  129.         }
  130.     }
  131.     if ($i === 0 && $g === 1)
  132.     {
  133.         unset($otherItems[key($otherItems)]);
  134.     }
  135.     $result = array_merge($topItems, $items, $otherItems);
  136. }
  137.  
  138. echo "<pre>";
  139. print_r($result);
  140. echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement