Index: branches/unlabeled/unlabeled-1.2.2/core/units/custom_data/custom_data_event_handler.php =================================================================== diff -u -r6786 -r8242 --- branches/unlabeled/unlabeled-1.2.2/core/units/custom_data/custom_data_event_handler.php (.../custom_data_event_handler.php) (revision 6786) +++ branches/unlabeled/unlabeled-1.2.2/core/units/custom_data/custom_data_event_handler.php (.../custom_data_event_handler.php) (revision 8242) @@ -2,6 +2,139 @@ class CustomDataEventHandler extends kDBEventHandler { + /** + * [HOOK] Allows to apply custom fields functionality to specific config + * When main item is created, then cdata config is cloned + * + * @param kEvent $event + */ + function OnDefineCustomFields(&$event) + { + // 1. clone customdata table + $clones = $this->Application->getUnitOption('cdata', 'Clones'); + $clones[$event->MasterEvent->Prefix.'-cdata'] = Array ( + 'ParentPrefix' => $event->MasterEvent->Prefix, + 'TableName' => $this->Application->getUnitOption($event->MasterEvent->Prefix, 'TableName').'CustomData', + ); + $this->Application->setUnitOption('cdata', 'Clones', $clones); + + // 2. add custom field information to main item + $this->createCustomFields($event->MasterEvent->Prefix); + } + + function scanCustomFields($prefix) + { + if (defined('IS_INSTALL') && IS_INSTALL && !$this->Application->TableFound('CustomField')) { + return false; + } + + if (!$prefix) { + // prefix not specified + return false; + } + + $item_type = $this->Application->getUnitOption($prefix, 'ItemType'); + if (!$item_type) { + // no main config of such type + return false; + } + // get custom field information + $sql = 'SELECT * + FROM '.TABLE_PREFIX.'CustomField + WHERE Type = '.$item_type.' + ORDER BY CustomFieldId'; + $custom_fields = $this->Conn->Query($sql, 'CustomFieldId'); + if (!$custom_fields) { + // config doesn't have custom fields + return false; + } + + return $custom_fields; + } + + /** + * Fills cloned cdata config with data from it's parent + * + * @param kEvent $event + */ + function OnAfterConfigRead(&$event) + { + $main_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix'); + + $custom_fields = $this->scanCustomFields($main_prefix); + if (!$custom_fields) { + return false; + } + + // 2. create fields (for customdata item) + $fields = $this->Application->getUnitOption($event->Prefix, 'Fields', Array()); + $field_options = Array('type' => 'string', 'formatter' => 'kMultiLanguage', 'db_type' => 'text', 'default' => ''); + foreach ($custom_fields as $custom_id => $custom_params) { + if (isset($fields['cust_'.$custom_id])) continue; + $fields['cust_'.$custom_id] = $field_options; + } + $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); + } + + /** + * Creates "cust_" virtual fields for main item + * + * @param string $prefix + */ + function createCustomFields($prefix) + { + $custom_fields = $this->scanCustomFields($prefix); + if (!$custom_fields) { + return false; + } + + $calculated_fields = Array(); + $virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array()); + + $cf_helper =& $this->Application->recallObject('InpCustomFieldsHelper'); + $field_options = Array('type' => 'string', 'not_null' => 1, 'default' => ''); + $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); + + foreach ($custom_fields as $custom_id => $custom_params) { + switch ($custom_params['ElementType']) { + case 'date': + case 'datetime': + unset($field_options['options']); + $field_options['formatter'] = 'kDateFormatter'; + break; + + case 'select': + case 'multiselect': + case 'radio': + if ($custom_params['ValueList']) { + $field_options['options'] = $cf_helper->GetValuesHash($custom_params['ValueList']); + $field_options['formatter'] = 'kOptionsFormatter'; + } + break; + + default: + unset($field_options['options'], $field_options['formatter']); + break; + } + + $custom_name = $custom_params['FieldName']; + $calculated_fields['cust_'.$custom_name] = 'cust.'.$ml_formatter->LangFieldName('cust_'.$custom_id); + if (!isset($virtual_fields['cust_'.$custom_name])) { + $virtual_fields['cust_'.$custom_name] = Array(); + } + $virtual_fields['cust_'.$custom_name] = array_merge_recursive2($field_options, $virtual_fields['cust_'.$custom_name]); + $custom_fields[$custom_id] = $custom_name; + } + + $config_calculated_fields = $this->Application->getUnitOption($prefix, 'CalculatedFields', Array()); + foreach ($config_calculated_fields as $special => $special_fields) { + $config_calculated_fields[$special] = array_merge_recursive2($config_calculated_fields[$special], $calculated_fields); + } + $this->Application->setUnitOption($prefix, 'CalculatedFields', $config_calculated_fields); + + $this->Application->setUnitOption($prefix, 'CustomFields', $custom_fields); + $this->Application->setUnitOption($prefix, 'VirtualFields', $virtual_fields); + } } ?> \ No newline at end of file