Index: branches/5.2.x/core/kernel/db/db_event_handler.php =================================================================== diff -u -r14585 -r14596 --- branches/5.2.x/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 14585) +++ branches/5.2.x/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 14596) @@ -1,6 +1,6 @@ setEventParam('top_prefix', $this->Application->GetTopmostPrefix($event->Prefix, true)); - $object->Configure( $event->getEventParam('populate_ml_fields') || $this->Application->getUnitOption($event->Prefix, 'PopulateMlFields') ); + if ( $event->getEventParam('form_name') !== false ) { + $form_name = $event->getEventParam('form_name'); + } + else { + $request_forms = $this->Application->GetVar('forms', Array ()); + $form_name = (string)getArrayValue( $request_forms, $object->getPrefixSpecial() ); + } + + $object->Configure( $event->getEventParam('populate_ml_fields') || $this->Application->getUnitOption($event->Prefix, 'PopulateMlFields'), $form_name ); $this->PrepareObject($object, $event); // force live table if specified or is original item @@ -492,8 +502,10 @@ function OnItemBuild(&$event) { $object =& $event->getObject(); - $this->dbBuild($object,$event); + /* @var $object kDBItem */ + $this->dbBuild($object, $event); + $sql = $this->ItemPrepareQuery($event); $sql = $this->Application->ReplaceLanguageTags($sql); $object->setSelectSQL($sql); @@ -549,9 +561,11 @@ } $actions =& $this->Application->recallObject('kActions'); - $actions->Set($event->getPrefixSpecial().'_GoTab', ''); + /* @var $actions Params */ + $actions->Set($event->getPrefixSpecial().'_GoTab', ''); $actions->Set($event->getPrefixSpecial().'_GoId', ''); + $actions->Set('forms[' . $event->getPrefixSpecial() . ']', $object->getFormName()); } /** @@ -597,22 +611,26 @@ * Load item if id is available * * @param kEvent $event + * @return void + * @access protected */ - function LoadItem(&$event) + protected function LoadItem(&$event) { $object =& $event->getObject(); /* @var $object kDBItem */ $id = $this->getPassedID($event); - if ($object->isLoaded() && !is_array($id) && ($object->GetID() == $id)) { + if ( $object->isLoaded() && !is_array($id) && ($object->GetID() == $id) ) { // object is already loaded by same id return ; } - if ($object->Load($id)) { + if ( $object->Load($id) ) { $actions =& $this->Application->recallObject('kActions'); - $actions->Set($event->getPrefixSpecial().'_id', $object->GetID() ); + /* @var $actions Params */ + + $actions->Set($event->getPrefixSpecial() . '_id', $object->GetID()); } else { $object->setID($id); @@ -632,17 +650,17 @@ $object =& $event->getObject(); /* @var $object kDBList */ - $this->dbBuild($object,$event); + $this->dbBuild($object, $event); - if (!$object->isMainList() && $event->getEventParam('main_list')) { + if ( !$object->isMainList() && $event->getEventParam('main_list') ) { // once list is set to main, then even "requery" parameter can't remove that /*$passed = $this->Application->GetVar('passed'); $this->Application->SetVar('passed', $passed . ',' . $event->Prefix);*/ $object->becameMain(); } - $object->setGridName( $event->getEventParam('grid') ); + $object->setGridName($event->getEventParam('grid')); $sql = $this->ListPrepareQuery($event); $sql = $this->Application->ReplaceLanguageTags($sql); @@ -653,13 +671,15 @@ $object->linkToParent( $this->getMainSpecial($event) ); $this->AddFilters($event); - $this->SetCustomQuery($event); // new!, use this for dynamic queries based on specials for ex. + $this->SetCustomQuery($event); // new!, use this for dynamic queries based on specials for ex. $this->SetPagination($event); $this->SetSorting($event); $actions =& $this->Application->recallObject('kActions'); - $actions->Set('remove_specials['.$event->getPrefixSpecial().']', '0'); - $actions->Set($event->getPrefixSpecial().'_GoTab', ''); + /* @var $actions Params */ + + $actions->Set('remove_specials[' . $event->getPrefixSpecial() . ']', '0'); + $actions->Set($event->getPrefixSpecial() . '_GoTab', ''); } /** @@ -691,10 +711,11 @@ * Apply any custom changes to list's sql query * * @param kEvent $event + * @return void * @access protected - * @see OnListBuild + * @see kDBEventHandler::OnListBuild() */ - function SetCustomQuery(&$event) + protected function SetCustomQuery(&$event) { } @@ -1001,16 +1022,17 @@ } } - // always add forced sorting before any user sortings + // always add forced sorting before any user sorting fields $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting'); - + /* @var $forced_sorting Array */ + if ($forced_sorting) { foreach ($forced_sorting as $field => $dir) { $object->AddOrderField($field, $dir); } } - // add user sortings + // add user sorting fields if ($cur_sort1 != '' && $cur_sort1_dir != '') { $object->AddOrderField($cur_sort1, $cur_sort1_dir); } @@ -1071,13 +1093,16 @@ function AddFilters(&$event) { $object =& $event->getObject(); + /* @var $object kDBList */ - $edit_mark = rtrim($this->Application->GetSID().'_'.$this->Application->GetTopmostWid($event->Prefix), '_'); + $edit_mark = rtrim($this->Application->GetSID() . '_' . $this->Application->GetTopmostWid($event->Prefix), '_'); // add search filter - $filter_data = $this->Application->RecallVar($event->getPrefixSpecial().'_search_filter'); - if ($filter_data) { + $filter_data = $this->Application->RecallVar($event->getPrefixSpecial() . '_search_filter'); + + if ( $filter_data ) { $filter_data = unserialize($filter_data); + foreach ($filter_data as $filter_field => $filter_params) { $filter_type = ($filter_params['type'] == 'having') ? kDBList::HAVING_FILTER : kDBList::WHERE_FILTER; $filter_value = str_replace(EDIT_MARK, $edit_mark, $filter_params['value']); @@ -1086,15 +1111,18 @@ } // add custom filter - $view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view'); - $custom_filters = $this->Application->RecallPersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name); - if ($custom_filters) { + $view_name = $this->Application->RecallVar($event->getPrefixSpecial() . '_current_view'); + $custom_filters = $this->Application->RecallPersistentVar($event->getPrefixSpecial() . '_custom_filter.' . $view_name); + + if ( $custom_filters ) { $grid_name = $event->getEventParam('grid'); $custom_filters = unserialize($custom_filters); - if (isset($custom_filters[$grid_name])) { + + if ( isset($custom_filters[$grid_name]) ) { foreach ($custom_filters[$grid_name] as $field_name => $field_options) { list ($filter_type, $field_options) = each($field_options); - if (isset($field_options['value']) && $field_options['value']) { + + if ( isset($field_options['value']) && $field_options['value'] ) { $filter_type = ($field_options['sql_filter_type'] == 'having') ? kDBList::HAVING_FILTER : kDBList::WHERE_FILTER; $filter_value = str_replace(EDIT_MARK, $edit_mark, $field_options['value']); $object->addFilter($field_name, $filter_value, $filter_type, kDBList::FLT_CUSTOM); @@ -1103,29 +1131,34 @@ } } - $view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter'); - if($view_filter) - { + $view_filter = $this->Application->RecallVar($event->getPrefixSpecial() . '_view_filter'); + + if ( $view_filter ) { $view_filter = unserialize($view_filter); + $temp_filter =& $this->Application->makeClass('kMultipleFilter'); - $filter_menu = $this->Application->getUnitOption($event->Prefix,'FilterMenu'); + /* @var $temp_filter kMultipleFilter */ + + $filter_menu = $this->Application->getUnitOption($event->Prefix, 'FilterMenu'); - $group_key = 0; $group_count = count($filter_menu['Groups']); - while($group_key < $group_count) - { + $group_key = 0; + $group_count = count($filter_menu['Groups']); + + while ( $group_key < $group_count ) { $group_info = $filter_menu['Groups'][$group_key]; - $temp_filter->setType( constant('kDBList::FLT_TYPE_'.$group_info['mode']) ); + $temp_filter->setType(constant('kDBList::FLT_TYPE_' . $group_info['mode'])); $temp_filter->clearFilters(); - foreach ($group_info['filters'] as $flt_id) - { - $sql_key = getArrayValue($view_filter,$flt_id) ? 'on_sql' : 'off_sql'; - if ($filter_menu['Filters'][$flt_id][$sql_key] != '') - { - $temp_filter->addFilter('view_filter_'.$flt_id, $filter_menu['Filters'][$flt_id][$sql_key]); + + foreach ($group_info['filters'] as $flt_id) { + $sql_key = getArrayValue($view_filter, $flt_id) ? 'on_sql' : 'off_sql'; + + if ( $filter_menu['Filters'][$flt_id][$sql_key] != '' ) { + $temp_filter->addFilter('view_filter_' . $flt_id, $filter_menu['Filters'][$flt_id][$sql_key]); } } - $object->addFilter('view_group_'.$group_key, $temp_filter, $group_info['type'] , kDBList::FLT_VIEW); + + $object->addFilter('view_group_' . $group_key, $temp_filter, $group_info['type'], kDBList::FLT_VIEW); $group_key++; } } @@ -1277,15 +1310,15 @@ */ function ItemPrepareQuery(&$event) { - $sqls = $this->Application->getUnitOption($event->Prefix, 'ItemSQLs', Array ()); - $special = array_key_exists($event->Special, $sqls) ? $event->Special : ''; + $object =& $event->getObject(); + /* @var $object kDBItem */ - if (!array_key_exists($special, $sqls)) { - // preferred special not found in ItemSQLs -> use analog from ListSQLs - return $this->ListPrepareQuery($event); - } + $sqls = $object->getFormOption('ItemSQLs', Array ()); + $special = isset($sqls[$event->Special]) ? $event->Special : ''; - return $sqls[$special]; + // preferred special not found in ItemSQLs -> use analog from ListSQLs + + return isset($sqls[$special]) ? $sqls[$special] : $this->ListPrepareQuery($event); } /** @@ -1299,16 +1332,23 @@ */ function ListPrepareQuery(&$event) { - $sqls = $this->Application->getUnitOption($event->Prefix, 'ListSQLs', Array ()); + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $sqls = $object->getFormOption('ListSQLs', Array ()); + return $sqls[ array_key_exists($event->Special, $sqls) ? $event->Special : '' ]; } /** * Apply custom processing to item * * @param kEvent $event + * @param string $type + * @return void + * @access protected */ - function customProcessing(&$event, $type) + protected function customProcessing(&$event, $type) { } @@ -1356,19 +1396,21 @@ */ function OnUpdate(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; return; } - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if ($items_info) { + + if ( $items_info ) { foreach ($items_info as $id => $field_values) { $object->Load($id); - $object->SetFieldsFromHash($field_values); - $this->customProcessing($event, 'before'); + $object->SetFieldsFromHash($field_values); + $this->customProcessing($event, 'before'); if ( $object->Update($id) ) { $this->customProcessing($event, 'after'); @@ -1451,64 +1493,70 @@ } /** - * Cancel's kDBItem Editing/Creation + * Cancels kDBItem Editing/Creation * * @param kEvent $event + * @return void * @access protected */ - function OnCancel(&$event) + protected function OnCancel(&$event) { - $object =& $event->getObject(Array('skip_autoload' => true)); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ $items_info = $this->Application->GetVar($event->getPrefixSpecial(true)); - if ($items_info) { - $delete_ids = Array(); - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + + if ( $items_info ) { + $delete_ids = Array (); + + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + foreach ($items_info as $id => $field_values) { $object->Load($id); // record created for using with selector (e.g. Reviews->Select User), and not validated => Delete it - if ($object->isLoaded() && !$object->Validate() && ($id <= 0) ) { + if ( $object->isLoaded() && !$object->Validate() && ($id <= 0) ) { $delete_ids[] = $id; } } - if ($delete_ids) { - $temp->DeleteItems($event->Prefix, $event->Special, $delete_ids); + if ( $delete_ids ) { + $temp_handler->DeleteItems($event->Prefix, $event->Special, $delete_ids); } } - $event->setRedirectParams(Array('opener'=>'u'), true); + $event->SetRedirectParam('opener', 'u'); } - /** * 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 */ - function OnMassDelete(&$event) + protected function OnMassDelete(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; - return; + return ; } - $event->status=kEvent::erSUCCESS; + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $ids = $this->StoreSelectedIDs($event); $event->setEventParam('ids', $ids); $this->customProcessing($event, 'before'); $ids = $event->getEventParam('ids'); - if($ids) - { - $temp->DeleteItems($event->Prefix, $event->Special, $ids); + if ( $ids ) { + $temp_handler->DeleteItems($event->Prefix, $event->Special, $ids); } + $this->clearSelectedIDs($event); } @@ -1563,47 +1611,54 @@ * redirects to event' default redirect (normally grid template) * * @param kEvent $event + * @return void + * @access protected */ - function OnSave(&$event) + protected function OnSave(&$event) { $event->CallSubEvent('OnPreSave'); - if ($event->status == kEvent::erSUCCESS) { - $skip_master = false; - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $changes_var_name = $this->Prefix.'_changes_'.$this->Application->GetTopmostWid($this->Prefix); + if ($event->status != kEvent::erSUCCESS) { + return ; + } - if (!$this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { - $live_ids = $temp->SaveEdit($event->getEventParam('master_ids') ? $event->getEventParam('master_ids') : Array()); - if ($live_ids === false) { - // coping from table failed, because we have another coping process to same table, that wasn't finished - $event->status = kEvent::erFAIL; - return ; - } + $skip_master = false; + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ - if ($live_ids) { - // ensure, that newly created item ids are avalable as if they were selected from grid - // NOTE: only works if main item has subitems !!! - $this->StoreSelectedIDs($event, $live_ids); - } + $changes_var_name = $this->Prefix . '_changes_' . $this->Application->GetTopmostWid($this->Prefix); - $object =& $event->getObject(); - /* @var $object kDBItem */ + if ( !$this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { + $live_ids = $temp_handler->SaveEdit($event->getEventParam('master_ids') ? $event->getEventParam('master_ids') : Array ()); - $this->SaveLoggedChanges($changes_var_name, $object->ShouldLogChanges()); - } - else { + if ( $live_ids === false ) { + // coping from table failed, because we have another coping process to same table, that wasn't finished $event->status = kEvent::erFAIL; + return ; } - $this->clearSelectedIDs($event); + if ( $live_ids ) { + // ensure, that newly created item ids are available as if they were selected from grid + // NOTE: only works if main item has sub-items !!! + $this->StoreSelectedIDs($event, $live_ids); + } - $event->setRedirectParams(Array('opener' => 'u'), true); - $this->Application->RemoveVar($event->getPrefixSpecial().'_modified'); + $object =& $event->getObject(); + /* @var $object kDBItem */ - // all temp tables are deleted here => all after hooks should think, that it's live mode now - $this->Application->SetVar($event->Prefix.'_mode', ''); + $this->SaveLoggedChanges($changes_var_name, $object->ShouldLogChanges()); } + else { + $event->status = kEvent::erFAIL; + } + + $this->clearSelectedIDs($event); + + $event->setRedirectParams(Array ('opener' => 'u'), true); + $this->Application->RemoveVar($event->getPrefixSpecial() . '_modified'); + + // all temp tables are deleted here => all after hooks should think, that it's live mode now + $this->Application->SetVar($event->Prefix . '_mode', ''); } function SaveLoggedChanges($changes_var_name, $save = true) @@ -1701,21 +1756,24 @@ * Removes all temp tables and clears selected ids * * @param kEvent $event + * @return void + * @access protected */ - function OnCancelEdit(&$event) + protected function OnCancelEdit(&$event) { - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $temp->CancelEdit(); + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + $temp_handler->CancelEdit(); + $this->clearSelectedIDs($event); - $event->setRedirectParams(Array('opener'=>'u'), true); - $this->Application->RemoveVar($event->getPrefixSpecial().'_modified'); + $event->setRedirectParams(Array ('opener' => 'u'), true); + $this->Application->RemoveVar($event->getPrefixSpecial() . '_modified'); $changes_var_name = $this->Prefix . '_changes_' . $this->Application->GetTopmostWid($this->Prefix); $this->Application->RemoveVar($changes_var_name); } - /** * Allows to determine if we are creating new item or editing already created item * @@ -1725,47 +1783,49 @@ function isNewItemCreate(&$event) { $object =& $event->getObject( Array ('raise_warnings' => 0) ); - return !$object->IsLoaded(); + /* @var $object kDBItem */ -// $item_id = $this->getPassedID($event); -// return ($item_id == '') ? true : false; + return !$object->isLoaded(); } /** * Saves edited item into temp table * If there is no id, new item is created in temp table * * @param kEvent $event + * @return void + * @access protected */ - function OnPreSave(&$event) + protected function OnPreSave(&$event) { - //$event->redirect = false; // if there is no id - it means we need to create an item - if (is_object($event->MasterEvent)) { - $event->MasterEvent->setEventParam('IsNew',false); + if ( is_object($event->MasterEvent) ) { + $event->MasterEvent->setEventParam('IsNew', false); } - if ($this->isNewItemCreate($event)) { + if ( $this->isNewItemCreate($event) ) { $event->CallSubEvent('OnPreSaveCreated'); - if (is_object($event->MasterEvent)) { - $event->MasterEvent->setEventParam('IsNew',true); + + if ( is_object($event->MasterEvent) ) { + $event->MasterEvent->setEventParam('IsNew', true); } - return; + + return ; } - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject( Array ('skip_autoload' => true) ); /* @var $object kDBItem */ $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if ($items_info) { + if ( $items_info ) { foreach ($items_info as $id => $field_values) { $object->Load($id); - $object->SetFieldsFromHash($field_values); - $this->customProcessing($event, 'before'); - if( $object->Update($id) ) - { + $object->SetFieldsFromHash($field_values); + $this->customProcessing($event, 'before'); + + if ( $object->Update($id) ) { $this->customProcessing($event, 'after'); - $event->status=kEvent::erSUCCESS; + $event->status = kEvent::erSUCCESS; } else { $event->status = kEvent::erFAIL; @@ -1777,7 +1837,7 @@ } /** - * [HOOK] Saves subitem + * [HOOK] Saves sub-item * * @param kEvent $event */ @@ -1851,21 +1911,26 @@ * done in OnPreSaveCreated * * @param kEvent $event + * @return void + * @access protected */ - function OnPreCreate(&$event) + protected function OnPreCreate(&$event) { $this->setTempWindowID($event); $this->clearSelectedIDs($event); $this->Application->SetVar('m_lang', $this->Application->GetDefaultLanguageId()); - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject( Array ('skip_autoload' => true) ); + /* @var $object kDBItem */ - $temp =& $this->Application->recallObject($event->Prefix.'_TempHandler', 'kTempTablesHandler'); - $temp->PrepareEdit(); + $temp_handler =& $this->Application->recallObject($event->Prefix . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ + $temp_handler->PrepareEdit(); + $object->setID(0); - $this->Application->SetVar($event->getPrefixSpecial().'_id', 0); - $this->Application->SetVar($event->getPrefixSpecial().'_PreCreate', 1); + $this->Application->SetVar($event->getPrefixSpecial() . '_id', 0); + $this->Application->SetVar($event->getPrefixSpecial() . '_PreCreate', 1); $changes_var_name = $this->Prefix . '_changes_' . $this->Application->GetTopmostWid($this->Prefix); $this->Application->RemoveVar($changes_var_name); @@ -1875,74 +1940,83 @@ /** * Creates a new item in temp table and - * stores item id in App vars and Session on succsess + * stores item id in App vars and Session on success * * @param kEvent $event + * @return void + * @access protected */ - function OnPreSaveCreated(&$event) + protected function OnPreSaveCreated(&$event) { - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if($items_info) $field_values = array_shift($items_info); - $object =& $event->getObject( Array('skip_autoload' => true) ); - $object->SetFieldsFromHash($field_values); + /* @var $object kDBItem */ + $object->SetFieldsFromHash( $this->getSubmittedFields($event) ); + $this->customProcessing($event, 'before'); - if( $object->Create() ) - { + if ( $object->Create() ) { $this->customProcessing($event, 'after'); - $event->SetRedirectParam($event->getPrefixSpecial(true).'_id', $object->GetId()); - $event->status=kEvent::erSUCCESS; + $event->SetRedirectParam($event->getPrefixSpecial(true) . '_id', $object->GetID()); } - else - { - $event->status=kEvent::erFAIL; - $event->redirect=false; + else { + $event->status = kEvent::erFAIL; + $event->redirect = false; $object->setID(0); } - } - function OnReset(&$event) + /** + * Reloads form to loose all changes made during item editing + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnReset(&$event) { //do nothing - should reset :) - if ($this->isNewItemCreate($event)) { + if ( $this->isNewItemCreate($event) ) { // just reset id to 0 in case it was create - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject( Array ('skip_autoload' => true) ); + /* @var $object kDBItem */ + $object->setID(0); - $this->Application->SetVar($event->getPrefixSpecial().'_id',0); + $this->Application->SetVar($event->getPrefixSpecial() . '_id', 0); } } /** - * Apply same processing to each item beeing selected in grid + * Apply same processing to each item being selected in grid * * @param kEvent $event - * @access private + * @return void + * @access protected */ - function iterateItems(&$event) + protected function iterateItems(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; - return; + return ; } - $object =& $event->getObject( Array('skip_autoload' => true) ); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ + $ids = $this->StoreSelectedIDs($event); - if ($ids) { - $status_field = array_shift( $this->Application->getUnitOption($event->Prefix,'StatusField') ); - $order_field = $this->Application->getUnitOption($event->Prefix,'OrderField'); + if ( $ids ) { + $status_field = array_shift( $this->Application->getUnitOption($event->Prefix, 'StatusField') ); + $order_field = $this->Application->getUnitOption($event->Prefix, 'OrderField'); - if (!$order_field) { + if ( !$order_field ) { $order_field = 'Priority'; } foreach ($ids as $id) { $object->Load($id); - switch ($event->Name) { + switch ( $event->Name ) { case 'OnMassApprove': $object->SetDBField($status_field, 1); break; @@ -1960,7 +2034,7 @@ break; } - if ($object->Update()) { + if ( $object->Update() ) { $event->status = kEvent::erSUCCESS; } else { @@ -1981,19 +2055,18 @@ */ function OnMassClone(&$event) { - if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) { + if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) { $event->status = kEvent::erFAIL; - return; + return ; } - $event->status = kEvent::erSUCCESS; + $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler'); + /* @var $temp_handler kTempTablesHandler */ - $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $ids = $this->StoreSelectedIDs($event); - if ($ids) { - $temp->CloneItems($event->Prefix, $event->Special, $ids); + if ( $ids ) { + $temp_handler->CloneItems($event->Prefix, $event->Special, $ids); } $this->clearSelectedIDs($event); @@ -2009,13 +2082,22 @@ return false; } - function OnPreSavePopup(&$event) + /** + * Saves data from editing form to database without checking required fields + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnPreSavePopup(&$event) { $object =& $event->getObject(); + /* @var $object kDBItem */ + $this->RemoveRequiredFields($object); $event->CallSubEvent('OnPreSave'); - $this->finalizePopup($event); + $event->SetRedirectParam('opener', 'u'); } @@ -2036,35 +2118,38 @@ } /** - * Occurse after loading item, 'id' parameter + * Occurs after loading item, 'id' parameter * allows to get id of item that was loaded * * @param kEvent $event - * @access public + * @return void + * @access protected */ - function OnAfterItemLoad(&$event) + protected function OnAfterItemLoad(&$event) { } /** - * Occurse before creating item + * Occurs before creating item * * @param kEvent $event - * @access public + * @return void + * @access protected */ - function OnBeforeItemCreate(&$event) + protected function OnBeforeItemCreate(&$event) { } /** - * Occurse after creating item + * Occurs after creating item * * @param kEvent $event - * @access public + * @return void + * @access protected */ - function OnAfterItemCreate(&$event) + protected function OnAfterItemCreate(&$event) { $object =& $event->getObject(); /* @var $object kDBItem */ @@ -2075,18 +2160,19 @@ } /** - * Occurse before updating item + * Occurs before updating item * * @param kEvent $event - * @access public + * @return void + * @access protected */ - function OnBeforeItemUpdate(&$event) + protected function OnBeforeItemUpdate(&$event) { } /** - * Occurse after updating item + * Occurs after updating item * * @param kEvent $event * @access public @@ -2157,13 +2243,15 @@ } /** - * Occures before an item is deleted from live table when copying from temp + * Occurs before an item is deleted from live table when copying from temp * (temp handler deleted all items from live and then copy over all items from temp) * Id of item being deleted is passed as event' 'id' param * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeDeleteFromLive(&$event) + protected function OnBeforeDeleteFromLive(&$event) { } @@ -2227,13 +2315,14 @@ } /** - * Occures before an item is cloneded - * Id of ORIGINAL item is passed as event' 'id' param - * Do not call object' Update method in this event, just set needed fields! + * Occurs before an item has been cloned + * Id of newly created item is passed as event' 'id' param * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeClone(&$event) + protected function OnBeforeClone(&$event) { } @@ -2265,9 +2354,11 @@ * passed * * @param kEvent $event - * @access public + * @return void + * @access protected + * @deprecated */ - function finalizePopup(&$event) + protected function finalizePopup(&$event) { $event->SetRedirectParam('opener', 'u'); } @@ -2320,20 +2411,31 @@ $this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) ); } - function OnSetFilterPattern(&$event) + /** + * Sets view filter based on request + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnSetFilterPattern(&$event) { - $filters = $this->Application->GetVar($event->getPrefixSpecial(true).'_filters'); - if (!$filters) return ; + $filters = $this->Application->GetVar($event->getPrefixSpecial(true) . '_filters'); + if ( !$filters ) { + return; + } - $view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter'); - $view_filter = $view_filter ? unserialize($view_filter) : Array(); + $view_filter = $this->Application->RecallVar($event->getPrefixSpecial() . '_view_filter'); + $view_filter = $view_filter ? unserialize($view_filter) : Array (); $filters = explode(',', $filters); + foreach ($filters as $a_filter) { list($id, $value) = explode('=', $a_filter); $view_filter[$id] = $value; } - $this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) ); + + $this->Application->StoreVar($event->getPrefixSpecial() . '_view_filter', serialize($view_filter)); $event->redirect = false; } @@ -2369,43 +2471,54 @@ * Enter description here... * * @param kEvent $event + * @access protected */ - function OnPreSaveAndOpenTranslator(&$event) + protected function OnPreSaveAndOpenTranslator(&$event) { $this->Application->SetVar('allow_translation', true); + $object =& $event->getObject(); + /* @var $object kDBItem */ + $this->RemoveRequiredFields($object); $event->CallSubEvent('OnPreSave'); - if ($event->status == kEvent::erSUCCESS) { - + if ( $event->status == kEvent::erSUCCESS ) { $resource_id = $this->Application->GetVar('translator_resource_id'); - if ($resource_id) { + + if ( $resource_id ) { $t_prefixes = explode(',', $this->Application->GetVar('translator_prefixes')); - $cdata =& $this->Application->recallObject($t_prefixes[1], null, Array('skip_autoload' => true)); + $cdata =& $this->Application->recallObject($t_prefixes[1], null, Array ('skip_autoload' => true)); + /* @var $cdata kDBItem */ + $cdata->Load($resource_id, 'ResourceId'); - if (!$cdata->isLoaded()) { + + if ( !$cdata->isLoaded() ) { $cdata->SetDBField('ResourceId', $resource_id); $cdata->Create(); } - $this->Application->SetVar($cdata->getPrefixSpecial().'_id', $cdata->GetID()); + + $this->Application->SetVar($cdata->getPrefixSpecial() . '_id', $cdata->GetID()); } $event->redirect = $this->Application->GetVar('translator_t'); - $event->setRedirectParams(Array ( + + $redirect_params = Array ( 'pass' => 'all,trans,' . $this->Application->GetVar('translator_prefixes'), 'opener' => 's', $event->getPrefixSpecial(true) . '_id' => $object->GetID(), 'trans_event' => 'OnLoad', 'trans_prefix' => $this->Application->GetVar('translator_prefixes'), 'trans_field' => $this->Application->GetVar('translator_field'), 'trans_multi_line' => $this->Application->GetVar('translator_multi_line'), - ), true); + ); + $event->setRedirectParams($redirect_params, true); + // 1. SAVE LAST TEMPLATE TO SESSION (really needed here, because of tweaky redirect) $last_template = $this->Application->RecallVar('last_template'); - preg_match('/index4\.php\|'.$this->Application->GetSID().'-(.*):/U', $last_template, $rets); + preg_match('/index4\.php\|' . $this->Application->GetSID() . '-(.*):/U', $last_template, $rets); $this->Application->StoreVar('return_template', $this->Application->GetVar('t')); } } @@ -2418,9 +2531,9 @@ function RemoveRequiredFields(&$object) { // making all field non-required to achieve successful presave - $fields = $object->getFields(); + $fields = array_keys( $object->getFields() ); - foreach ($fields as $field => $options) { + foreach ($fields as $field) { if ( $object->isRequired($field) ) { $object->setRequired($field, false); } @@ -2434,34 +2547,37 @@ */ function OnSelectUser(&$event) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + $items_info = $this->Application->GetVar('u'); - if ($items_info) { + + if ( $items_info ) { $user_id = array_shift( array_keys($items_info) ); - $object =& $event->getObject(); $this->RemoveRequiredFields($object); $is_new = !$object->isLoaded(); - $is_main = substr($this->Application->GetVar($event->Prefix.'_mode'), 0, 1) == 't'; + $is_main = substr($this->Application->GetVar($event->Prefix . '_mode'), 0, 1) == 't'; - if ($is_new) { + if ( $is_new ) { $new_event = $is_main ? 'OnPreCreate' : 'OnNew'; $event->CallSubEvent($new_event); $event->redirect = true; } $object->SetDBField($this->Application->RecallVar('dst_field'), $user_id); - if ($is_new) { + if ( $is_new ) { $object->Create(); } else { $object->Update(); } } - $event->SetRedirectParam($event->getPrefixSpecial().'_id', $object->GetID()); - $this->finalizePopup($event); + $event->SetRedirectParam($event->getPrefixSpecial() . '_id', $object->GetID()); + $event->SetRedirectParam('opener', 'u'); } @@ -2502,18 +2618,18 @@ } /** - * Apply some special processing to - * object beeing recalled before using - * it in other events that call prepareObject + * Apply some special processing to object being + * recalled before using it in other events that + * call prepareObject * - * @param Object $object + * @param kDBItem|kDBList $object * @param kEvent $event + * @return void * @access protected */ - function prepareObject(&$object, &$event) + protected function prepareObject(&$object, &$event) { - if ($event->Special == 'export' || $event->Special == 'import') - { + if ( $event->Special == 'export' || $event->Special == 'import' ) { $export_helper =& $this->Application->recallObject('CatItemExportHelper'); /* @var $export_helper kCatDBItemExportHelper */ @@ -2564,34 +2680,47 @@ } - function OnDeleteExportPreset(&$event) + /** + * Deletes export preset + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnDeleteExportPreset(&$event) { - $object =& $event->GetObject(); + $field_values = $this->getSubmittedFields($event); - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if($items_info) - { - list($id,$field_values) = each($items_info); - $preset_key = $field_values['ExportPresets']; + if ( !$field_values ) { + return ; + } - $export_settings = $this->Application->RecallPersistentVar('export_settings'); - if (!$export_settings) return ; - $export_settings = unserialize($export_settings); - if (!isset($export_settings[$event->Prefix])) return ; + $preset_key = $field_values['ExportPresets']; + $export_settings = $this->Application->RecallPersistentVar('export_settings'); - $to_delete = ''; - $export_presets = array(''=>''); - foreach ($export_settings[$event->Prefix] as $key => $val) { - if (implode('|', $val['ExportColumns']) == $preset_key) { - $to_delete = $key; - break; - } + if ( !$export_settings ) { + return ; + } + + $export_settings = unserialize($export_settings); + + if ( !isset($export_settings[$event->Prefix]) ) { + return ; + } + + $to_delete = ''; + + foreach ($export_settings[$event->Prefix] as $key => $val) { + if ( implode('|', $val['ExportColumns']) == $preset_key ) { + $to_delete = $key; + break; } - if ($to_delete) { - unset($export_settings[$event->Prefix][$to_delete]); - $this->Application->StorePersistentVar('export_settings', serialize($export_settings)); - } } + + if ( $to_delete ) { + unset($export_settings[$event->Prefix][$to_delete]); + $this->Application->StorePersistentVar('export_settings', serialize($export_settings)); + } } /** @@ -2620,8 +2749,10 @@ * Used to save files uploaded via swfuploader * * @param kEvent $event + * @return void + * @access protected */ - function OnUploadFile(&$event) + protected function OnUploadFile(&$event) { $event->status = kEvent::erSTOP; // define('DBG_SKIP_REPORTING', 0); @@ -2630,7 +2761,7 @@ if (!$this->Application->HttpQuery->Post) { // Variables {field, id, flashsid} are always submitted through POST! // When file size is larger, then "upload_max_filesize" (in php.ini), - // then theese variables also are not submitted -> handle such case. + // then these variables also are not submitted -> handle such case. header('HTTP/1.0 413 File size exceeds allowed limit'); echo $default_msg; return ; @@ -2902,13 +3033,13 @@ { $event->status = kEvent::erSTOP; - $lang =& $this->Application->recallObject('lang.current'); -// header('Content-type: text/xml; charset='.$lang->GetDBField('Charset')); + /*$lang =& $this->Application->recallObject('lang.current'); + header('Content-type: text/xml; charset=' . $lang->GetDBField('Charset'));*/ $picker_helper =& $this->Application->RecallObject('ColumnPickerHelper'); /* @var $picker_helper kColumnPickerHelper */ - $picker_helper->PreparePicker($event->getPrefixSpecial(), $this->Application->GetVar('grid_name')); + $picker_helper->PreparePicker($event->getPrefixSpecial(), $this->Application->GetVar('grid_name')); $picker_helper->SaveWidths($event->getPrefixSpecial(), $this->Application->GetVar('widths')); echo 'OK';