Index: branches/5.2.x/core/units/configuration/configuration_event_handler.php =================================================================== diff -u -N -r15460 -r15471 --- branches/5.2.x/core/units/configuration/configuration_event_handler.php (.../configuration_event_handler.php) (revision 15460) +++ branches/5.2.x/core/units/configuration/configuration_event_handler.php (.../configuration_event_handler.php) (revision 15471) @@ -1,6 +1,6 @@ getObject(); + /* @var $object kDBItem */ + + $object->SetDBField('Section', $this->Application->GetVar('section')); + $object->SetDBField('ModuleOwner', $this->Application->GetVar('module')); + } + + /** * Sets custom validation * * @param kEvent $event @@ -113,66 +131,31 @@ } /** - * Performs validation of configuration variable value + * Performs custom validation * * @param kEvent $event * @return void * @access protected */ - protected function OnBeforeItemUpdate(kEvent $event) + protected function OnBeforeItemValidate(kEvent $event) { - parent::OnBeforeItemUpdate($event); + parent::OnBeforeItemValidate($event); $object = $event->getObject(); /* @var $object kDBItem */ // if password field is empty, then don't update if ( $object->GetDBField('ElementType') == 'password' ) { - if ( trim($object->GetDBField('VariableValue')) == '' ) { - $field_options = $object->GetFieldOptions('VariableValue'); - $field_options['skip_empty'] = 1; - $object->SetFieldOptions('VariableValue', $field_options); - } - else { + if ( trim($object->GetDBField('VariableValue')) != '' ) { $password_formatter = $this->Application->recallObject('kPasswordFormatter'); /* @var $password_formatter kPasswordFormatter */ $object->SetDBField('VariableValue', $password_formatter->EncryptPassword($object->GetDBField('VariableValue'), 'b38')); } } - $field_name = $object->GetDBField('VariableName'); - $field_values = $this->Application->GetVar($event->getPrefixSpecial(true)); - $state_country_hash = Array ('Comm_State' => 'Comm_Country', 'Comm_Shipping_State' => 'Comm_Shipping_Country'); + $this->_processCountryState($event); - if ( array_key_exists($field_name, $state_country_hash) ) { - // if this is state field - $sql = 'SELECT VariableId - FROM ' . $this->Application->getUnitOption('conf', 'TableName') . ' - WHERE VariableName = "' . $state_country_hash[$field_name] . '"'; - $country_variable_id = $this->Conn->GetOne($sql); - - $check_state = $object->GetDBField('VariableValue'); - $check_country = $field_values[$country_variable_id]['VariableValue']; - - if ( !$check_country || !$check_state ) { - return; - } - - $cs_helper = $this->Application->recallObject('CountryStatesHelper'); - /* @var $cs_helper kCountryStatesHelper */ - - $state_iso = $cs_helper->getStateIso($check_state, $check_country); - - if ( $state_iso !== false ) { - $object->SetDBField('VariableValue', $state_iso); - } - else { - // selected state doesn't belong to selected country - $object->SetError('VariableValue', 'invalid_state', 'la_InvalidState'); - } - } - $variable_name = $object->GetDBField('VariableName'); $new_value = $object->GetDBField('VariableValue'); @@ -187,17 +170,111 @@ $compile = $event->MasterEvent->getEventParam('compile_maintenance_template'); $compile = $compile || $new_value != $object->GetOriginalField('VariableValue'); - if ( $compile && !$this->generateMaintenancePage($new_value) ) { + if ( $compile && !$this->_generateMaintenancePage($new_value) ) { $object->SetError('VariableValue', 'template_file_missing', 'la_error_TemplateFileMissing'); } } elseif ( $variable_name == 'DefaultEmailRecipients' ) { $email_event_data = $this->Application->GetVar('emailevents_' . $event->Prefix); $object->SetDBField('VariableValue', $email_event_data[0]['Recipients']); } + + $sections_helper = $this->Application->recallObject('SectionsHelper'); + /* @var $sections_helper kSectionsHelper */ + + $section = $object->GetDBField('Section'); + + if ( $section && !$sections_helper->getSectionData($section) ) { + $object->SetError('Section', 'unknown_section'); + } } /** + * Checks, that state belongs to selected country + * + * @param kEvent $event + * @access protected + */ + protected function _processCountryState(kEvent $event) + { + $object = $event->getObject(); + /* @var $object kDBItem */ + + $country_iso = $this->_getCountryByState($event); + $state_name = $object->GetDBField('VariableValue'); + + if ( !$country_iso || !$state_name ) { + return; + } + + $cs_helper = $this->Application->recallObject('CountryStatesHelper'); + /* @var $cs_helper kCountryStatesHelper */ + + $state_iso = $cs_helper->getStateIso($state_name, $country_iso); + + if ( $state_iso !== false ) { + $object->SetDBField('VariableValue', $state_iso); + } + else { + // selected state doesn't belong to selected country + $object->SetError('VariableValue', 'invalid_state', 'la_InvalidState'); + } + } + + /** + * Returns country iso code, that matches current state variable name + * + * @param kEvent $event + * @return bool + * @access protected + */ + protected function _getCountryByState(kEvent $event) + { + $object = $event->getObject(); + /* @var $object kDBItem */ + + $variable_name = $object->GetDBField('VariableName'); + + $state_country_hash = Array ( + 'Comm_State' => 'Comm_Country', + 'Comm_Shipping_State' => 'Comm_Shipping_Country' + ); + + if ( !array_key_exists($variable_name, $state_country_hash) ) { + return false; + } + + $field_values = $this->Application->GetVar($event->getPrefixSpecial(true)); + + $sql = 'SELECT VariableId + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + WHERE VariableName = ' . $this->Conn->qstr($state_country_hash[$variable_name]); + $country_variable_id = $this->Conn->GetOne($sql); + + return $field_values[$country_variable_id]['VariableValue']; + } + + /** + * Does custom password setting processong + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnBeforeItemUpdate(kEvent $event) + { + parent::OnBeforeItemUpdate($event); + + $object = $event->getObject(); + /* @var $object kDBItem */ + + // if password field is empty, then don't update + if ( $object->GetDBField('ElementType') == 'password' && trim($object->GetDBField('VariableValue')) == '' ) { + $object->SetFieldOption('VariableValue', 'skip_empty', 1); + } + } + + /** * Occurs after updating item * * @param kEvent $event @@ -213,12 +290,8 @@ $object = $event->getObject(); /* @var $object kDBItem */ - if ( $object->GetDBField('ElementType') == 'password' ) { - if ( trim($object->GetDBField('VariableValue')) == '' ) { - $field_options = $object->GetFieldOptions('VariableValue'); - unset($field_options['skip_empty']); - $object->SetFieldOptions('VariableValue', $field_options); - } + if ( $object->GetDBField('ElementType') == 'password' && trim($object->GetDBField('VariableValue')) == '' ) { + $object->SetFieldOption('VariableValue', 'skip_empty', 0); } // allows to check if variable's value was changed now @@ -371,7 +444,7 @@ * @return bool * @access protected */ - protected function generateMaintenancePage($template = null) + protected function _generateMaintenancePage($template = null) { if ( !isset($template) ) { $template = $this->Application->ConfigValue('HardMaintenanceTemplate'); @@ -390,4 +463,100 @@ return false; } + + /** + * Returns auto-complete values for ajax-dropdown + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnSuggestValues(kEvent $event) + { + if ( !$this->Application->isAdminUser ) { + // very careful here, because this event allows to + // view every object field -> limit only to logged-in admins + return; + } + + $event->status = kEvent::erSTOP; + + $field = $this->Application->GetVar('field'); + $cur_value = $this->Application->GetVar('cur_value'); + + $object = $event->getObject(); + /* @var $object kDBItem */ + + if ( !$field || !$cur_value || !$object->isField($field) ) { + return; + } + + $limit = $this->Application->GetVar('limit'); + if ( !$limit ) { + $limit = 20; + } + + $sql = 'SELECT DISTINCT ' . $field . ', ModuleOwner + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + WHERE ' . $field . ' LIKE ' . $this->Conn->qstr('%' . $cur_value . '%') . ' + ORDER BY ' . $field . ' ASC'; + $raw_suggestions = $this->Conn->Query($sql); + + $suggestions = Array (); + $this->Application->XMLHeader(); + + foreach ($raw_suggestions as $raw_suggestion) { + $suggestion = $raw_suggestion[$field]; + + if ( !isset($suggestions[$suggestion]) ) { + $suggestions[$suggestion] = Array (); + } + + $suggestions[$suggestion][] = $raw_suggestion['ModuleOwner']; + } + + array_splice($suggestions, $limit); + + echo ''; + $of_label = $this->Application->Phrase('la_From', false); + + foreach ($suggestions as $suggestion_value => $suggestion_modules) { + $suggestion_module = in_array('In-Portal', $suggestion_modules) ? 'In-Portal' : implode(', ', $suggestion_modules); + $suggestion_title = $suggestion_value . ' ' . $of_label . ' ' . $suggestion_module . ''; + + echo '' . htmlspecialchars($suggestion_title) . ''; + } + + echo ''; + } + + /** + * Prefills module dropdown + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnAfterConfigRead(kEvent $event) + { + parent::OnAfterConfigRead($event); + + $options = Array (); + + foreach ($this->Application->ModuleInfo as $module_name => $module_info) { + if ( $module_name == 'Core' ) { + continue; + } + + $options[$module_name] = $module_name; + + if ( $module_name == 'In-Portal' ) { + $options['In-Portal:Users'] = 'In-Portal:Users'; + } + } + + $fields = $this->Application->getUnitOption($event->Prefix, 'Fields'); + $fields['ModuleOwner']['options'] = $options; + $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); + } } \ No newline at end of file