getObject(); $top_prefix = $this->Application->GetTopmostPrefix($event->Prefix); $item_type = $this->Application->getUnitOption($top_prefix, 'ItemType'); $item_resource_id = $this->GetParentResourceId($event); // there is no ResourceId when creating new item, but we need some id for SQL to work if (!$item_resource_id) $item_resource_id = 0; $sql = str_replace('#ITEM_RESOURCE_ID#',$item_resource_id,$object->SelectClause); $object->setSelectSQL($sql); $object->removeFilter('parent_filter'); $object->addFilter('itemtype_filter', 'Type = '.$item_type); if ($event->Special=='general') $object->addFilter('generaltab_filter','OnGeneralTab = 1'); $object->SetPerPage(-1); } /** * Enter description here... * * @param kEvent $event * @return unknown */ function GetParentResourceId(&$event) { $object = &$event->getObject(); $top_object = $this->Application->RecallObject( $this->Application->GetTopmostPrefix($event->Prefix) ); return $top_object->GetDBField('ResourceId'); } function OnPreSave(&$event) { $object =& $event->getObject(); $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); if($items_info) { $lang_id = $this->Application->GetVar('m_lang'); foreach($items_info as $custom_field_id => $values) { $set_values = Array( 'CustomFieldId' => $custom_field_id, 'l'.$lang_id.'_Value' => $values['l'.$lang_id.'_Value'], 'ResourceId' => $values['ResourceId'] != 0 ? $values['ResourceId'] : $this->GetParentResourceId($event), ); // ResourceId will be -1 if we are creating new product, then we get ID directly, // because it should be already set by parent' event, as we are in hAFTER hook $id = $values['CustomDataId']; if ($id) { //if it's already existing custom field record - update id $object->SetDefaultValues(); $object->Load($id); $object->SetFieldsFromHash($set_values); if( $object->Update($id) ) { $event->status=erSUCCESS; } else { $event->status=erFAIL; $event->redirect=false; break; } } else { //new custom field record - create it and set temp id! $object->SetDefaultValues(); $object->SetFieldsFromHash($set_values); if( $object->Create(null, true) ) { $object->setTempID(); $event->status=erSUCCESS; } } } } // this is critical, as the object here is Item, but if something fails, the template could use the same name as grid // the problem appeared on products general tab, when validation does not go through for product, // and the templates is getting parsed $this->Application->removeObject($event->Prefix_Special); } } ?>