Conn->qstr( $this->Application->GetVar('cf_type') ); $main_prefix = $this->Conn->GetOne($sql); $section = $this->Application->getUnitOption($main_prefix.'.custom', 'PermSection'); $event->setEventParam('PermSection', $section); return parent::CheckPermission($event); } /** * Apply any custom changes to list's sql query * * @param kEvent $event * @return void * @access protected * @see kDBEventHandler::OnListBuild() */ protected function SetCustomQuery(kEvent $event) { parent::SetCustomQuery($event); $object =& $event->getObject(); /* @var $object kDBList */ $item_type = $this->Application->GetVar('cf_type'); if ( !$item_type ) { $prefix = $event->getEventParam('SourcePrefix'); $item_type = $this->Application->getUnitOption($prefix, 'ItemType'); } if ( $event->Special == 'general' ) { $object->addFilter('generaltab_filter', '%1$s.OnGeneralTab = 1'); } if ( $item_type ) { $hidden_fields = $this->Conn->qstrArray($this->_getHiddenFields($event)); if ( $hidden_fields ) { $object->addFilter('hidden_filter', '%1$s.FieldName NOT IN (' . implode(',', $hidden_fields) . ')'); } $object->addFilter('itemtype_filter', '%1$s.Type = ' . $item_type); } if ( !($this->Application->isDebugMode() && $this->Application->isAdminUser) ) { $object->addFilter('user_filter', '%1$s.IsSystem = 0'); } } /** * Returns prefix, that custom fields are printed for * * @param kEvent $event * @return string */ function _getSourcePrefix($event) { $prefix = $event->getEventParam('SourcePrefix'); if (!$prefix) { $sql = 'SELECT Prefix FROM ' . TABLE_PREFIX . 'ItemTypes WHERE ItemType = ' . $this->Conn->qstr( $this->Application->GetVar('cf_type') ); $prefix = $this->Conn->GetOne($sql); } return $prefix; } /** * Get custom fields, that should no be shown anywhere * * @param kEvent $event * @return Array * @access protected */ protected function _getHiddenFields($event) { $prefix = $this->_getSourcePrefix($event); $hidden_fields = Array (); $virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array ()); $custom_fields = $this->Application->getUnitOption($prefix, 'CustomFields', Array ()); /* @var $custom_fields Array */ foreach ($custom_fields as $custom_field) { $check_field = 'cust_' . $custom_field; $show_mode = array_key_exists('show_mode', $virtual_fields[$check_field]) ? $virtual_fields[$check_field]['show_mode'] : true; if ( ($show_mode === false) || (($show_mode === smDEBUG) && !(defined('DEBUG_MODE') && DEBUG_MODE)) ) { $hidden_fields[] = $custom_field; } } return $hidden_fields; } /** * Prevents from duplicate item creation * * @param kEvent $event * @return void * @access protected */ protected function OnBeforeItemCreate(kEvent $event) { parent::OnBeforeItemCreate($event); $object =& $event->getObject(); /* @var $object kDBItem */ $sql = 'SELECT COUNT(*) FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' WHERE FieldName = ' . $this->Conn->qstr($object->GetDBField('FieldName')) . ' AND Type = ' . $object->GetDBField('Type'); $found = $this->Conn->GetOne($sql); if ( $found ) { $event->status = kEvent::erFAIL; $object->SetError('FieldName', 'duplicate', 'la_error_CustomExists'); } } /** * Occurs after deleting item, id of deleted item * is stored as 'id' param of event * * @param kEvent $event * @return void * @access protected */ protected function OnAfterItemDelete(kEvent $event) { parent::OnAfterItemDelete($event); $object =& $event->getObject(); /* @var $object kDBItem */ $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type')); $ml_helper = $this->Application->recallObject('kMultiLanguageHelper'); /* @var $ml_helper kMultiLanguageHelper */ // call main item config to clone cdata table $this->Application->getUnitOption($main_prefix, 'TableName'); $ml_helper->deleteField($main_prefix . '-cdata', $event->getEventParam('id')); } /** * Get config prefix based on item type * * @param int $item_type * @return string * @access protected */ protected function getPrefixByItemType($item_type) { $sql = 'SELECT Prefix FROM ' . TABLE_PREFIX . 'ItemTypes WHERE ItemType = ' . $item_type; return $this->Conn->GetOne($sql); } /** * Creates new database columns, once custom field is successfully created * * @param kEvent $event * @return void * @access protected */ protected function OnSaveCustomField($event) { if ( $event->MasterEvent->status != kEvent::erSUCCESS ) { return ; } $object =& $event->getObject(); /* @var $object kDBItem */ $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type')); $ml_helper = $this->Application->recallObject('kMultiLanguageHelper'); /* @var $ml_helper kMultiLanguageHelper */ // call main item config to clone cdata table define('CUSTOM_FIELD_ADDED', 1); // used in cdata::scanCustomFields method $this->Application->getUnitOption($main_prefix, 'TableName'); $ml_helper->createFields($main_prefix . '-cdata'); } /** * Deletes all selected items. * Automatically recurse into sub-items using temp handler, and deletes sub-items * by calling its Delete method if sub-item has AutoDelete set to true in its config file * * @param kEvent $event * @return void * @access protected */ protected function OnMassDelete(kEvent $event) { parent::OnMassDelete($event); $event->SetRedirectParam('opener', 's'); } /** * Prepare temp tables for creating new item * but does not create it. Actual create is * done in OnPreSaveCreated * * @param kEvent $event * @return void * @access protected */ protected function OnPreCreate(kEvent $event) { parent::OnPreCreate($event); $object =& $event->getObject(); /* @var $object kDBItem */ $object->SetDBField('Type', $this->Application->GetVar('cf_type')); } /** * Prepares ValueList field's value as xml for editing * * @param kEvent $event * @return void * @access protected */ protected function OnAfterItemLoad(kEvent $event) { parent::OnAfterItemLoad($event); $object =& $event->getObject(); /* @var $object kDBItem */ if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) { return ; } $custom_field_helper = $this->Application->recallObject('InpCustomFieldsHelper'); /* @var $custom_field_helper InpCustomFieldsHelper */ $options = $custom_field_helper->GetValuesHash($object->GetDBField('ValueList'), VALUE_LIST_SEPARATOR, false); $records = Array (); $option_key = key($options); if ( $option_key === '' || $option_key == 0 ) { // remove 1st empty option, and add it later, when options will be saved, but allow string option keys unset($options[$option_key]); // keep index, don't use array_unshift! } foreach ($options as $option_key => $option_title) { $records[] = Array ('OptionKey' => $option_key, 'OptionTitle' => $option_title); } $minput_helper = $this->Application->recallObject('MInputHelper'); /* @var $minput_helper MInputHelper */ $xml = $minput_helper->prepareMInputXML($records, Array ('OptionKey', 'OptionTitle')); $object->SetDBField('Options', $xml); } /** * Returns custom field element types, that will use minput control * * @return Array * @access protected */ protected function _getMultiElementTypes() { return Array ('select', 'multiselect', 'radio'); } /** * Saves minput content to ValueList field * * @param kEvent $event * @return void * @access protected */ protected function OnBeforeItemUpdate(kEvent $event) { parent::OnBeforeItemUpdate($event); $object =& $event->getObject(); /* @var $object kDBItem */ if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) { return ; } $minput_helper = $this->Application->recallObject('MInputHelper'); /* @var $minput_helper MInputHelper */ $ret = $object->GetDBField('ElementType') == 'select' ? Array ('' => '=+') : Array (); $records = $minput_helper->parseMInputXML($object->GetDBField('Options')); if ( $object->GetDBField('SortValues') ) { usort($records, Array (&$this, '_sortValues')); ksort($records); } foreach ($records as $record) { if ( substr($record['OptionKey'], 0, 3) == 'SQL' ) { $ret[] = $record['OptionTitle']; } else { $ret[] = $record['OptionKey'] . '=' . $record['OptionTitle']; } } $object->SetDBField('ValueList', implode(VALUE_LIST_SEPARATOR, $ret)); } function _sortValues($record_a, $record_b) { return strcasecmp($record_a['OptionTitle'], $record_b['OptionTitle']); } }