Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/units/custom_fields/custom_fields_event_handler.php
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | |||||
* @return void | * @return void | ||||
* @access protected | * @access protected | ||||
* @see kDBEventHandler::OnListBuild() | * @see kDBEventHandler::OnListBuild() | ||||
*/ | */ | ||||
protected function SetCustomQuery(kEvent $event) | protected function SetCustomQuery(kEvent $event) | ||||
{ | { | ||||
parent::SetCustomQuery($event); | parent::SetCustomQuery($event); | ||||
/** @var kDBList $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBList */ | |||||
$item_type = $this->Application->GetVar('cf_type'); | $item_type = $this->Application->GetVar('cf_type'); | ||||
if ( !$item_type ) { | if ( !$item_type ) { | ||||
$prefix = $event->getEventParam('SourcePrefix'); | $prefix = $event->getEventParam('SourcePrefix'); | ||||
$item_type = $this->Application->getUnitOption($prefix, 'ItemType'); | $item_type = $this->Application->getUnitOption($prefix, 'ItemType'); | ||||
} | } | ||||
if ( $event->Special == 'general' ) { | if ( $event->Special == 'general' ) { | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Line(s) | |||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function _getHiddenFields($event) | protected function _getHiddenFields($event) | ||||
{ | { | ||||
$prefix = $this->_getSourcePrefix($event); | $prefix = $this->_getSourcePrefix($event); | ||||
$hidden_fields = Array (); | $hidden_fields = Array (); | ||||
$virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array ()); | $virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array ()); | ||||
/** @var Array $custom_fields */ | |||||
$custom_fields = $this->Application->getUnitOption($prefix, 'CustomFields', Array ()); | $custom_fields = $this->Application->getUnitOption($prefix, 'CustomFields', Array ()); | ||||
/* @var $custom_fields Array */ | |||||
foreach ($custom_fields as $custom_field) { | foreach ($custom_fields as $custom_field) { | ||||
$check_field = 'cust_' . $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; | $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)) ) { | if ( ($show_mode === false) || (($show_mode === smDEBUG) && !(defined('DEBUG_MODE') && DEBUG_MODE)) ) { | ||||
$hidden_fields[] = $custom_field; | $hidden_fields[] = $custom_field; | ||||
} | } | ||||
} | } | ||||
return $hidden_fields; | return $hidden_fields; | ||||
} | } | ||||
/** | /** | ||||
* Prevents from duplicate item creation | * Prevents from duplicate item creation | ||||
* | * | ||||
* @param kEvent $event | * @param kEvent $event | ||||
* @return void | * @return void | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function OnBeforeItemCreate(kEvent $event) | protected function OnBeforeItemCreate(kEvent $event) | ||||
{ | { | ||||
parent::OnBeforeItemCreate($event); | parent::OnBeforeItemCreate($event); | ||||
/** @var kDBItem $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBItem */ | |||||
$sql = 'SELECT COUNT(*) | $sql = 'SELECT COUNT(*) | ||||
FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' | FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' | ||||
WHERE FieldName = ' . $this->Conn->qstr($object->GetDBField('FieldName')) . ' AND Type = ' . $object->GetDBField('Type'); | WHERE FieldName = ' . $this->Conn->qstr($object->GetDBField('FieldName')) . ' AND Type = ' . $object->GetDBField('Type'); | ||||
$found = $this->Conn->GetOne($sql); | $found = $this->Conn->GetOne($sql); | ||||
if ( $found ) { | if ( $found ) { | ||||
$event->status = kEvent::erFAIL; | $event->status = kEvent::erFAIL; | ||||
$object->SetError('FieldName', 'duplicate', 'la_error_CustomExists'); | $object->SetError('FieldName', 'duplicate', 'la_error_CustomExists'); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Occurs after deleting item, id of deleted item | * Occurs after deleting item, id of deleted item | ||||
* is stored as 'id' param of event | * is stored as 'id' param of event | ||||
* | * | ||||
* @param kEvent $event | * @param kEvent $event | ||||
* @return void | * @return void | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function OnAfterItemDelete(kEvent $event) | protected function OnAfterItemDelete(kEvent $event) | ||||
{ | { | ||||
parent::OnAfterItemDelete($event); | parent::OnAfterItemDelete($event); | ||||
/** @var kDBItem $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBItem */ | |||||
$main_prefix = $this->getPrefixByItemType($object->GetDBField('Type')); | $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type')); | ||||
/** @var kMultiLanguageHelper $ml_helper */ | |||||
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper'); | $ml_helper = $this->Application->recallObject('kMultiLanguageHelper'); | ||||
/* @var $ml_helper kMultiLanguageHelper */ | |||||
// call main item config to clone cdata table | // call main item config to clone cdata table | ||||
$this->Application->getUnitOption($main_prefix, 'TableName'); | $this->Application->getUnitOption($main_prefix, 'TableName'); | ||||
$ml_helper->deleteField($main_prefix . '-cdata', $event->getEventParam('id')); | $ml_helper->deleteField($main_prefix . '-cdata', $event->getEventParam('id')); | ||||
} | } | ||||
/** | /** | ||||
* Get config prefix based on item type | * Get config prefix based on item type | ||||
Show All 19 Lines | |||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function OnSaveCustomField($event) | protected function OnSaveCustomField($event) | ||||
{ | { | ||||
if ( $event->MasterEvent->status != kEvent::erSUCCESS ) { | if ( $event->MasterEvent->status != kEvent::erSUCCESS ) { | ||||
return ; | return ; | ||||
} | } | ||||
/** @var kDBItem $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBItem */ | |||||
$main_prefix = $this->getPrefixByItemType($object->GetDBField('Type')); | $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type')); | ||||
/** @var kMultiLanguageHelper $ml_helper */ | |||||
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper'); | $ml_helper = $this->Application->recallObject('kMultiLanguageHelper'); | ||||
/* @var $ml_helper kMultiLanguageHelper */ | |||||
// call main item config to clone cdata table | // call main item config to clone cdata table | ||||
define('CUSTOM_FIELD_ADDED', 1); // used in cdata::scanCustomFields method | define('CUSTOM_FIELD_ADDED', 1); // used in cdata::scanCustomFields method | ||||
$this->Application->getUnitOption($main_prefix, 'TableName'); | $this->Application->getUnitOption($main_prefix, 'TableName'); | ||||
$ml_helper->createFields($main_prefix . '-cdata'); | $ml_helper->createFields($main_prefix . '-cdata'); | ||||
} | } | ||||
/** | /** | ||||
Show All 20 Lines | |||||
* @param kEvent $event | * @param kEvent $event | ||||
* @return void | * @return void | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function OnPreCreate(kEvent $event) | protected function OnPreCreate(kEvent $event) | ||||
{ | { | ||||
parent::OnPreCreate($event); | parent::OnPreCreate($event); | ||||
/** @var kDBItem $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBItem */ | |||||
$object->SetDBField('Type', $this->Application->GetVar('cf_type')); | $object->SetDBField('Type', $this->Application->GetVar('cf_type')); | ||||
} | } | ||||
/** | /** | ||||
* Prepares ValueList field's value as xml for editing | * Prepares ValueList field's value as xml for editing | ||||
* | * | ||||
* @param kEvent $event | * @param kEvent $event | ||||
* @return void | * @return void | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function OnAfterItemLoad(kEvent $event) | protected function OnAfterItemLoad(kEvent $event) | ||||
{ | { | ||||
parent::OnAfterItemLoad($event); | parent::OnAfterItemLoad($event); | ||||
/** @var kDBItem $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBItem */ | |||||
if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) { | if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) { | ||||
return ; | return ; | ||||
} | } | ||||
/** @var InpCustomFieldsHelper $custom_field_helper */ | |||||
$custom_field_helper = $this->Application->recallObject('InpCustomFieldsHelper'); | $custom_field_helper = $this->Application->recallObject('InpCustomFieldsHelper'); | ||||
/* @var $custom_field_helper InpCustomFieldsHelper */ | |||||
$options = $custom_field_helper->GetValuesHash($object->GetDBField('ValueList'), VALUE_LIST_SEPARATOR, false); | $options = $custom_field_helper->GetValuesHash($object->GetDBField('ValueList'), VALUE_LIST_SEPARATOR, false); | ||||
$records = Array (); | $records = Array (); | ||||
$option_key = key($options); | $option_key = key($options); | ||||
if ( $option_key === '' || $option_key == 0 ) { | if ( $option_key === '' || $option_key == 0 ) { | ||||
// remove 1st empty option, and add it later, when options will be saved, but allow string option keys | // 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! | unset($options[$option_key]); // keep index, don't use array_unshift! | ||||
} | } | ||||
foreach ($options as $option_key => $option_title) { | foreach ($options as $option_key => $option_title) { | ||||
$records[] = Array ('OptionKey' => $option_key, 'OptionTitle' => $option_title); | $records[] = Array ('OptionKey' => $option_key, 'OptionTitle' => $option_title); | ||||
} | } | ||||
/** @var MInputHelper $minput_helper */ | |||||
$minput_helper = $this->Application->recallObject('MInputHelper'); | $minput_helper = $this->Application->recallObject('MInputHelper'); | ||||
/* @var $minput_helper MInputHelper */ | |||||
$xml = $minput_helper->prepareMInputXML($records, Array ('OptionKey', 'OptionTitle')); | $xml = $minput_helper->prepareMInputXML($records, Array ('OptionKey', 'OptionTitle')); | ||||
$object->SetDBField('Options', $xml); | $object->SetDBField('Options', $xml); | ||||
} | } | ||||
/** | /** | ||||
* Returns custom field element types, that will use minput control | * Returns custom field element types, that will use minput control | ||||
* | * | ||||
Show All 11 Lines | |||||
* @param kEvent $event | * @param kEvent $event | ||||
* @return void | * @return void | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected function OnBeforeItemUpdate(kEvent $event) | protected function OnBeforeItemUpdate(kEvent $event) | ||||
{ | { | ||||
parent::OnBeforeItemUpdate($event); | parent::OnBeforeItemUpdate($event); | ||||
/** @var kDBItem $object */ | |||||
$object = $event->getObject(); | $object = $event->getObject(); | ||||
/* @var $object kDBItem */ | |||||
if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) { | if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) { | ||||
return ; | return ; | ||||
} | } | ||||
/** @var MInputHelper $minput_helper */ | |||||
$minput_helper = $this->Application->recallObject('MInputHelper'); | $minput_helper = $this->Application->recallObject('MInputHelper'); | ||||
/* @var $minput_helper MInputHelper */ | |||||
$ret = $object->GetDBField('ElementType') == 'select' ? Array ('' => '=+') : Array (); | $ret = $object->GetDBField('ElementType') == 'select' ? Array ('' => '=+') : Array (); | ||||
$records = $minput_helper->parseMInputXML($object->GetDBField('Options')); | $records = $minput_helper->parseMInputXML($object->GetDBField('Options')); | ||||
if ( $object->GetDBField('SortValues') ) { | if ( $object->GetDBField('SortValues') ) { | ||||
usort($records, Array (&$this, '_sortValues')); | usort($records, Array (&$this, '_sortValues')); | ||||
ksort($records); | ksort($records); | ||||
} | } | ||||
Show All 10 Lines | |||||
$object->SetDBField('ValueList', implode(VALUE_LIST_SEPARATOR, $ret)); | $object->SetDBField('ValueList', implode(VALUE_LIST_SEPARATOR, $ret)); | ||||
} | } | ||||
function _sortValues($record_a, $record_b) | function _sortValues($record_a, $record_b) | ||||
{ | { | ||||
return strcasecmp($record_a['OptionTitle'], $record_b['OptionTitle']); | return strcasecmp($record_a['OptionTitle'], $record_b['OptionTitle']); | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |