Ejemplo n.º 1
0
 protected function migrateCustomElementProperties($iblockId, $entityNewName)
 {
     $iblockId = (int) $iblockId;
     $VERSION = \CIBlockElement::GetIBVersion($iblockId);
     if ($VERSION == 2) {
         $strTable = "b_iblock_element_prop_m" . $iblockId;
     } else {
         $strTable = "b_iblock_element_property";
     }
     $tableNameUf = "b_utm_" . strtolower($entityNewName);
     $tableNameSingleUf = "b_uts_" . strtolower($entityNewName);
     $sqlHelper = $this->connection->getSqlHelper();
     $listElementAll = array();
     $objectQuery = $this->connection->query("\n\t\t\tSELECT obj.ID, prop.VALUE, prop.IBLOCK_PROPERTY_ID\n\t\t\tFROM b_disk_object obj\n\t\t\tINNER JOIN {$strTable} prop ON obj.WEBDAV_ELEMENT_ID = prop.IBLOCK_ELEMENT_ID\n\t\t\tWHERE obj.WEBDAV_IBLOCK_ID = {$iblockId} AND obj.TYPE = 3\n\t\t");
     while ($listObject = $objectQuery->fetch()) {
         $listElementAll[$listObject['ID']][$listObject['IBLOCK_PROPERTY_ID']]['FIELD_VALUE'][] = $listObject['VALUE'];
     }
     $listElement = array();
     foreach ($this->getIblockProperties($iblockId) as $prop) {
         $propId = $prop['ID'];
         $mappedUfType = $this->mapTypeElementPropertyToUfType($prop);
         if (!$mappedUfType) {
             $this->log(array('Unknown property of element', $prop));
             continue;
         }
         $userTypeEntity = new \CUserTypeEntity();
         $symbolicName = empty($prop['CODE']) ? $propId : strtoupper($prop['CODE']);
         $xmlId = empty($prop['CODE']) ? $propId : $prop['CODE'];
         $fieldName = substr('UF_' . $symbolicName, 0, 20);
         if ($mappedUfType == 'iblock_section' || $mappedUfType == 'iblock_element') {
             $settingsArray = array('IBLOCK_ID' => $prop['LINK_IBLOCK_ID'], 'DISPLAY' => 'LIST');
         } else {
             $settingsArray = array();
         }
         $id = $userTypeEntity->add(array('ENTITY_ID' => $entityNewName, 'FIELD_NAME' => $fieldName, 'USER_TYPE_ID' => $mappedUfType, 'XML_ID' => 'PROPERTY_' . $xmlId, 'MULTIPLE' => $prop['MULTIPLE'], 'MANDATORY' => $prop['IS_REQUIRED'], 'SHOW_FILTER' => 'N', 'SHOW_IN_LIST' => null, 'EDIT_IN_LIST' => null, 'IS_SEARCHABLE' => $prop['SEARCHABLE'], 'SETTINGS' => $settingsArray, 'EDIT_FORM_LABEL' => array('en' => $prop['NAME'], 'ru' => $prop['NAME'], 'de' => $prop['NAME'])));
         if ($id) {
             if ($mappedUfType == 'enumeration') {
                 $i = 0;
                 $enumValues = array();
                 $queryEnum = \CIBlockPropertyEnum::getlist(array("SORT" => "ASC", "VALUE" => "ASC"), array('PROPERTY_ID' => $propId));
                 while ($queryEnum && ($rowEnum = $queryEnum->fetch())) {
                     $enumValues['n' . $i] = array('SORT' => $rowEnum['SORT'], 'VALUE' => $rowEnum['VALUE'], 'XML_ID' => $rowEnum['XML_ID'], 'DEF' => $rowEnum['DEF']);
                     $i++;
                 }
                 $userTypeEnum = new \CUserFieldEnum();
                 $userTypeEnum->setEnumValues($id, $enumValues);
             }
             foreach ($listElementAll as $newId => $propArray) {
                 if (array_key_exists($propId, $propArray)) {
                     $listElement[$newId][$propId]['FIELD_VALUE'] = $listElementAll[$newId][$propId]['FIELD_VALUE'];
                     $listElement[$newId][$propId]['FIELD_NAME'] = $fieldName;
                     if ($prop['MULTIPLE'] == 'Y') {
                         $listElement[$newId][$propId]['FIELD_ID'] = $id;
                         $listElement[$newId][$propId]['PROPERTY_TYPE'] = $mappedUfType;
                     }
                 }
             }
         }
     }
     if (!empty($listElement)) {
         foreach ($listElement as $newId => $propArray) {
             $fieldArray = array();
             $valueArray = array();
             foreach ($propArray as $prop) {
                 $fieldArray[] = $prop['FIELD_NAME'];
                 if (count($prop['FIELD_VALUE']) > 1) {
                     $valueArray[] = "'" . $sqlHelper->forSql(serialize($prop['FIELD_VALUE'])) . "'";
                     foreach ($prop['FIELD_VALUE'] as $utmValue) {
                         if ($prop['PROPERTY_TYPE'] == 'integer') {
                             $utmValue = (int) $utmValue;
                             $this->connection->queryExecute("\n\t\t\t\t\t\t\t\t\tINSERT INTO {$tableNameUf} (VALUE_ID, FIELD_ID, VALUE_INT)\n\t\t\t\t\t\t\t\t\tVALUES ({$newId}, {$prop['FIELD_ID']}, {$utmValue})\n\t\t\t\t\t\t\t\t");
                         } else {
                             $utmValueStr = "'" . $sqlHelper->forSql($utmValue) . "'";
                             $this->connection->queryExecute("\n\t\t\t\t\t\t\t\t\tINSERT INTO {$tableNameUf} (VALUE_ID, FIELD_ID, VALUE)\n\t\t\t\t\t\t\t\t\tVALUES ({$newId}, {$prop['FIELD_ID']}, {$utmValueStr})\n\t\t\t\t\t\t\t\t");
                         }
                     }
                 } else {
                     $valueArray[] = "'" . $sqlHelper->forSql($prop['FIELD_VALUE'][0]) . "'";
                 }
             }
             if (!empty($fieldArray)) {
                 if ($this->isMysql) {
                     $sql = "\n\t\t\t\t\t\t\tINSERT IGNORE INTO {$tableNameSingleUf} (VALUE_ID, " . implode(', ', $fieldArray) . ")\n\t\t\t\t\t\t\tVALUES ({$newId}, " . implode(', ', $valueArray) . ")\n\t\t\t\t\t\t";
                 } elseif ($this->isOracle) {
                     $sql = "\n\t\t\t\t\t\t\tINSERT INTO {$tableNameSingleUf} (VALUE_ID, " . implode(', ', $fieldArray) . ")\n\t\t\t\t\t\t\tSELECT {$newId}, " . implode(', ', $valueArray) . " FROM dual\n\t\t\t\t\t\t\tWHERE NOT EXISTS(SELECT 'x' FROM {$tableNameSingleUf} WHERE VALUE_ID = {$newId})\n\t\t\t\t\t\t";
                 } elseif ($this->isMssql) {
                     $sql = "\n\t\t\t\t\t\t\tINSERT INTO {$tableNameSingleUf} (VALUE_ID, " . implode(', ', $fieldArray) . ")\n\t\t\t\t\t\t\tSELECT * FROM (SELECT {$newId}, " . implode(', ', $valueArray) . ") AS tmp\n\t\t\t\t\t\t\tWHERE NOT EXISTS(SELECT 'x' FROM {$tableNameSingleUf} WHERE VALUE_ID = {$newId})\n\t\t\t\t\t\t";
                 }
                 $this->connection->queryExecute($sql);
             }
         }
     }
 }