Application->getUnitConfig('cdata')->addClones(Array ( $event->MasterEvent->Prefix . '-cdata' => Array ( 'ParentPrefix' => $event->MasterEvent->Prefix, 'TableName' => $this->_getDataTable($event->MasterEvent), ) )); // 2. add custom field information to main item $this->createCustomFields($event->MasterEvent->Prefix); } /** * Returns custom data table name for given event * * @param kEvent $event * @return string * @access protected */ protected function _getDataTable(kEvent $event) { $config = $event->getUnitConfig(); $ret = $config->getCustomDataTableName(); return $ret ? $ret : $config->getTableName() . 'CustomData'; } /** * Returns list of custom fields for a given $prefix * * @param $prefix * * @return Array|bool * @access protected */ protected function scanCustomFields($prefix) { static $custom_fields = Array (); if ( defined('IS_INSTALL') && IS_INSTALL && !$this->Application->TableFound('CustomFields', true) ) { return false; } if ( !$prefix ) { // prefix not specified return false; } $item_type = $this->Application->getUnitConfig($prefix)->getItemType(); if ( !$item_type ) { // no main config of such type return false; } $no_caching = (defined('IS_INSTALL') && IS_INSTALL) || (defined('CUSTOM_FIELD_ADDED') && CUSTOM_FIELD_ADDED); if ( !$custom_fields || $no_caching ) { // query all custom fields at once -> saves 4 sqls queries if ( $no_caching ) { $all_custom_fields = $this->getCustomFields(); } else { $cache_key = 'all_custom_fields[%CfSerial%][%ModSerial%]'; $all_custom_fields = $this->Application->getCache($cache_key, false); if ( $all_custom_fields === false ) { $this->Conn->nextQueryCachable = true; $all_custom_fields = $this->getCustomFields(); $this->Application->setCache($cache_key, $all_custom_fields); } } foreach ($all_custom_fields as $custom_field_id => $custom_field_data) { $cf_type = $custom_field_data['Type']; if ( !array_key_exists($cf_type, $custom_fields) ) { $custom_fields[$cf_type] = Array (); } $custom_fields[$cf_type][$custom_field_id] = $custom_field_data; } } return array_key_exists($item_type, $custom_fields) ? $custom_fields[$item_type] : false; } /** * Returns sorted list of all custom fields * * @return Array */ function getCustomFields() { $sql = 'SELECT * FROM '.TABLE_PREFIX.'CustomFields'; $ret = $this->Conn->Query($sql, 'CustomFieldId'); ksort($ret); return $ret; } /** * Fills cloned cdata config with data from it's parent * * @param kEvent $event * @return void * @access protected */ protected function OnAfterConfigRead(kEvent $event) { parent::OnAfterConfigRead($event); $config = $event->getUnitConfig(); $main_prefix = $config->getParentPrefix(); if ( !$main_prefix ) { return; } $custom_fields = $this->scanCustomFields($main_prefix); if ( !$custom_fields ) { return; } // 2. create fields (for custom data item) $field_options = Array ( 'type' => 'string', 'formatter' => 'kMultiLanguage', 'db_type' => 'text', 'default' => '' ); foreach ($custom_fields as $custom_id => $custom_params) { $field_name = 'cust_' . $custom_id; if ( $config->getFieldByName($field_name) ) { continue; } $field_options['force_primary'] = !$custom_params['MultiLingual']; $config->addFields($field_options, $field_name); } } /** * Creates "cust_" virtual fields for main item * * @param string $prefix * @return void * @access protected */ protected function createCustomFields($prefix) { $custom_fields = $this->scanCustomFields($prefix); if ( !$custom_fields ) { return; } $calculated_fields = Array (); $config = $this->Application->getUnitConfig($prefix); /** @var InpCustomFieldsHelper $cf_helper */ $cf_helper = $this->Application->recallObject('InpCustomFieldsHelper'); $is_install = defined('IS_INSTALL') && IS_INSTALL; foreach ($custom_fields as $custom_id => $custom_params) { $custom_name = $custom_params['FieldName']; $field_options = Array ('type' => 'string', 'default' => $custom_params['DefaultValue']); // raises warnings during 4.3.9 -> 5.0.0 upgrade, no fatal sqls though if ( $custom_params['IsRequired'] ) { $field_options['required'] = 1; } $calculated_fields['cust_' . $custom_name] = 'cust.l' . $this->Application->GetDefaultLanguageId() . '_cust_' . $custom_id; switch ( $custom_params['ElementType'] ) { case 'date': unset($field_options['options']); $field_options['formatter'] = 'kDateFormatter'; $field_options['input_time_format'] = ''; $field_options['time_format'] = ''; break; case 'datetime': unset($field_options['options']); $field_options['formatter'] = 'kDateFormatter'; break; case 'select': case 'multiselect': case 'radio': if ( $custom_params['ValueList'] ) { // $is_install check prevents 335 bad phrase sql errors on upgrade to 5.1.0 $field_options['options'] = $is_install ? Array () : $cf_helper->GetValuesHash($custom_params['ValueList']); $field_options['formatter'] = 'kOptionsFormatter'; $field_options['multiple'] = $custom_params['ElementType'] == 'multiselect'; } break; default: if ( $custom_params['MultiLingual'] ) { $field_options['formatter'] = 'kMultiLanguage'; $calculated_fields['cust_' . $custom_name] = 'cust.l%2$s_cust_' . $custom_id; } break; } $field_name = 'cust_' . $custom_name; $old_field_options = $config->getVirtualFieldByName($field_name, Array ()); $config->addVirtualFields(Array ( $field_name => kUtil::array_merge_recursive($field_options, $old_field_options) )); $custom_fields[$custom_id] = $custom_name; } foreach ($config->getCalculatedFieldSpecials() as $special) { if ( $special == '-virtual' ) { continue; } $config->addCalculatedFieldsBySpecial($special, $calculated_fields); } $config->setCustomFields($custom_fields); } }