Index: trunk/core/kernel/db/db_event_handler.php =================================================================== diff -u -N -r6833 -r7391 --- trunk/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 6833) +++ trunk/core/kernel/db/db_event_handler.php (.../db_event_handler.php) (revision 7391) @@ -113,8 +113,8 @@ 'OnMassMoveUp' => Array('self' => 'advanced:move_up|edit', 'subitem' => 'advanced:move_up|add|edit'), 'OnMassMoveDown' => Array('self' => 'advanced:move_down|edit', 'subitem' => 'advanced:move_down|add|edit'), - 'OnPreCreate' => Array('self' => 'add|add.pending'), - 'OnEdit' => Array('self' => 'edit|edit.pending'), + 'OnPreCreate' => Array('self' => 'add|add.pending', 'subitem' => 'edit|edit.pending'), + 'OnEdit' => Array('self' => 'edit|edit.pending', 'subitem' => 'edit|edit.pending'), 'OnExport' => Array('self' => 'view|advanced:export'), 'OnExportBegin' => Array('self' => 'view|advanced:export'), @@ -123,6 +123,7 @@ // theese event do not harm, but just in case check them too :) 'OnCancelEdit' => Array('self' => true, 'subitem' => true), 'OnCancel' => Array('self' => true, 'subitem' => true), + 'OnReset' => Array('self' => true, 'subitem' => true), 'OnSetSorting' => Array('self' => true, 'subitem' => true), 'OnSetSortingDirect' => Array('self' => true, 'subitem' => true), @@ -180,7 +181,7 @@ if($ret) return $ret; // recall selected ids array and use the first one - $ids=$this->Application->GetVar($event->getPrefixSpecial().'_selected_ids'); + $ids = $this->Application->GetVar($event->getPrefixSpecial().'_selected_ids'); if ($ids != '') { $ids=explode(',',$ids); if($ids) $ret=array_shift($ids); @@ -199,10 +200,21 @@ * id passed in get/post as prefix_id * * @param kEvent $event + * @param Array $ids + * * @return Array ids stored */ - function StoreSelectedIDs(&$event) + function StoreSelectedIDs(&$event, $ids = null) { + $wid = $this->Application->GetTopmostWid($event->Prefix); + $session_name = rtrim($event->getPrefixSpecial().'_selected_ids_'.$wid, '_'); + + if (isset($ids)) { + // save ids directly if they given + $this->Application->StoreVar($session_name, implode(',', $ids)); + return $ids; + } + $ret = Array(); // May be we don't need this part: ? @@ -228,8 +240,8 @@ $ret = array_unique(array_merge($ret, $ids)); - $this->Application->SetVar($event->getPrefixSpecial().'_selected_ids',implode(',',$ret)); - $this->Application->LinkVar($event->getPrefixSpecial().'_selected_ids'); + $this->Application->SetVar($event->getPrefixSpecial().'_selected_ids', implode(',',$ret)); + $this->Application->LinkVar($event->getPrefixSpecial().'_selected_ids', $session_name); // This is critical - otherwise getPassedID will return last ID stored in session! (not exactly true) // this smells... needs to be refactored @@ -248,11 +260,21 @@ * Returns stored selected ids as an array * * @param kEvent $event + * @param bool $from_session return ids from session (written, when editing was started) * @return array */ - function getSelectedIDs(&$event) + function getSelectedIDs(&$event, $from_session = false) { - return explode(',', $this->Application->GetVar($event->getPrefixSpecial().'_selected_ids')); + if ($from_session) { + $wid = $this->Application->GetTopmostWid($event->Prefix); + $var_name = rtrim($event->getPrefixSpecial().'_selected_ids_'.$wid, '_'); + $ret = $this->Application->RecallVar($var_name); + } + else { + $ret = $this->Application->GetVar($event->getPrefixSpecial().'_selected_ids'); + } + + return explode(',', $ret); } /** @@ -278,10 +300,14 @@ function clearSelectedIDs(&$event) { $prefix_special = $event->getPrefixSpecial(); - $ids = $this->Application->RecallVar($prefix_special.'_selected_ids'); + + $ids = implode(',', $this->getSelectedIDs($event, true)); $event->setEventParam('ids', $ids); - $this->Application->RemoveVar($prefix_special.'_selected_ids'); + $wid = $this->Application->GetTopmostWid($event->Prefix); + $session_name = rtrim($prefix_special.'_selected_ids_'.$wid, '_'); + + $this->Application->RemoveVar($session_name); $this->Application->SetVar($prefix_special.'_selected_ids', ''); $this->Application->SetVar($prefix_special.'_id', ''); // $event->getPrefixSpecial(true).'_id' too may be @@ -358,6 +384,8 @@ function OnTempHandlerBuild(&$event) { $object =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + /* @var $object kTempTablesHandler */ + $object->BuildTables( $event->Prefix, $this->getSelectedIDs($event) ); } @@ -372,13 +400,23 @@ $object = &$event->getObject(); $top_prefix = $this->Application->GetTopmostPrefix($event->Prefix); - return ( - $this->Application->GetVar(rtrim($top_prefix.'_'.$event->Special, '_')) == 't' - || - $this->Application->GetVar(rtrim($top_prefix.'.'.$event->Special, '.')) == 't' - || - $this->Application->GetVar($top_prefix.'_mode') == 't' - ); + $var_names = Array ( + $top_prefix, + rtrim($top_prefix.'_'.$event->Special, '_'), + rtrim($top_prefix.'.'.$event->Special, '.'), + ); + $var_names = array_unique($var_names); + + $temp_mode = false; + foreach ($var_names as $var_name) { + $value = $this->Application->GetVar($var_name.'_mode'); + if (substr($value, 0, 1) == 't') { + $temp_mode = true; + break; + } + } + + return $temp_mode; } /** @@ -390,7 +428,7 @@ */ function TablePrefix(&$event) { - return $this->UseTempTables($event) ? $this->Application->GetTempTablePrefix().TABLE_PREFIX : TABLE_PREFIX; + return $this->UseTempTables($event) ? $this->Application->GetTempTablePrefix('prefix:'.$event->Prefix).TABLE_PREFIX : TABLE_PREFIX; } /** @@ -485,7 +523,10 @@ function OnSetPerPage(&$event) { $per_page = $this->Application->GetVar($event->getPrefixSpecial(true).'_PerPage'); - $this->Application->StoreVar( $event->getPrefixSpecial().'_PerPage', $per_page ); + $this->Application->StoreVar($event->getPrefixSpecial().'_PerPage', $per_page); + + $view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view'); + $this->Application->StorePersistentVar($event->getPrefixSpecial().'_PerPage.'.$view_name, $per_page); } /** @@ -544,17 +585,24 @@ $object->SetPage($page); } + /** + * Returns current per-page setting for list + * + * @param kEvent $event + * @return int + */ function getPerPage(&$event) { + // 1. per-page is passed as tag parameter to PrintList, InitList, etc. $per_page = $event->getEventParam('per_page'); - /* if ($per_page == 'list_next') { + /*if ($per_page == 'list_next') { $per_page = ''; }*/ + // 2. per-page variable name is store into config variable $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping'); - - if ( $config_mapping ) { + if ($config_mapping) { switch ( $per_page ){ case 'short_list' : $per_page = $this->Application->ConfigValue($config_mapping['ShortListPerPage']); @@ -565,21 +613,27 @@ } } - if(!$per_page) - { - $per_page_var = $event->getPrefixSpecial().'_PerPage'; + if (!$per_page) { + // per-page is stored to persistent session + $view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view'); + $per_page = $this->Application->RecallPersistentVar($event->getPrefixSpecial().'_PerPage.'.$view_name); - $per_page = $this->Application->RecallVar($per_page_var); - if(!$per_page) - { + if (!$per_page) { + // per-page is stored to current session + $per_page = $this->Application->RecallVar($event->getPrefixSpecial().'_PerPage'); + } + if (!$per_page) { if ($config_mapping) { if (!isset($config_mapping['PerPage'])) { trigger_error('Incorrect mapping of PerPage key in config for prefix '.$event->Prefix.'', E_USER_WARNING); } $per_page = $this->Application->ConfigValue($config_mapping['PerPage']); } - if(!$per_page) $per_page = 10; + if (!$per_page) { + // none of checked above per-page locations are useful, then try default value + $per_page = 10; + } } } @@ -611,8 +665,13 @@ $tag_sort_by = $event->getEventParam('sort_by'); if ($tag_sort_by) { + if ($tag_sort_by == 'random') { + $by = 'RAND()'; + $dir = ''; + } + else { list($by, $dir) = explode(',', $tag_sort_by); - if ($by == 'random') $by = 'RAND()'; + } $object->AddOrderField($by, $dir); } @@ -654,26 +713,6 @@ } /** - * Adds filters found in session to object - * - * @param kEvent $event - * @param string $session_var variable name in session, where filter is stored - * @param string $filter_class filter class, e.g. FLT_SYSTEM, FLT_NORMAL, etc. - */ - function addStoredFilter(&$event, $session_var, $filter_class) - { - $filter_data = $this->Application->RecallVar($event->getPrefixSpecial().'_'.$session_var); - if ($filter_data) { - $object =& $event->getObject(); - $filter_data = unserialize($filter_data); - foreach ($filter_data as $filter_field => $filter_params) { - $filter_type = ($filter_params['type'] == 'having') ? HAVING_FILTER : WHERE_FILTER; - $object->addFilter($filter_field, $filter_params['value'], $filter_type, $filter_sub_type); - } - } - } - - /** * Add filters found in session * * @param kEvent $event @@ -682,16 +721,19 @@ { $object =& $event->getObject(); + $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 = unserialize($filter_data); foreach ($filter_data as $filter_field => $filter_params) { $filter_type = ($filter_params['type'] == 'having') ? HAVING_FILTER : WHERE_FILTER; - $object->addFilter($filter_field, $filter_params['value'], $filter_type, FLT_SEARCH); + $filter_value = str_replace(EDIT_MARK, $edit_mark, $filter_params['value']); + $object->addFilter($filter_field, $filter_value, $filter_type, FLT_SEARCH); } } - + // add custom filter $view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view'); $custom_filters = $this->Application->RecallPersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name); @@ -701,9 +743,10 @@ 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 ($field_options['value']) { + if (isset($field_options['value']) && $field_options['value']) { $filter_type = ($field_options['sql_filter_type'] == 'having') ? HAVING_FILTER : WHERE_FILTER; - $object->addFilter($field_name, $field_options['value'], $filter_type, FLT_CUSTOM); + $filter_value = str_replace(EDIT_MARK, $edit_mark, $field_options['value']); + $object->addFilter($field_name, $filter_value, $filter_type, FLT_CUSTOM); } } } @@ -999,9 +1042,9 @@ $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); - $this->StoreSelectedIDs($event); + $ids = $this->StoreSelectedIDs($event); - $event->setEventParam('ids', $this->getSelectedIDs($event) ); + $event->setEventParam('ids', $ids); $this->customProcessing($event, 'before'); $ids = $event->getEventParam('ids'); @@ -1013,16 +1056,33 @@ } /** + * Sets window id (of first opened edit window) to temp mark in uls + * + * @param kEvent $event + */ + function setTempWindowID(&$event) + { + $mode = $this->Application->GetVar($event->Prefix.'_mode'); + if ($mode == 't') { + $wid = $this->Application->GetVar('m_wid'); + $this->Application->SetVar($event->Prefix.'_mode', 't'.$wid); + } + } + + /** * Prepare temp tables and populate it * with items selected in the grid * * @param kEvent $event */ function OnEdit(&$event) { + $this->setTempWindowID($event); $this->StoreSelectedIDs($event); $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler'); + /* @var $temp kTempTablesHandler */ + $temp->PrepareEdit(); $event->redirect=false; @@ -1046,7 +1106,7 @@ 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->Application->StoreVar($event->getPrefixSpecial().'_selected_ids', implode(',', $live_ids)); + $this->StoreSelectedIDs($event, $live_ids); } } $this->clearSelectedIDs($event); @@ -1085,8 +1145,11 @@ function isNewItemCreate(&$event) { $event->setEventParam('raise_warnings', 0); - $item_id = $this->getPassedID($event); - return ($item_id == '') ? true : false; + $object =& $event->getObject(); + return !$object->IsLoaded(); + +// $item_id = $this->getPassedID($event); +// return ($item_id == '') ? true : false; } /** @@ -1187,13 +1250,17 @@ */ function OnPreCreate(&$event) { + $this->setTempWindowID($event); $this->clearSelectedIDs($event); + $object =& $event->getObject( Array('skip_autoload' => true) ); $temp =& $this->Application->recallObject($event->Prefix.'_TempHandler', 'kTempTablesHandler'); $temp->PrepareEdit(); $object->setID(0); + $this->Application->SetVar($event->getPrefixSpecial().'_id',0); + $this->Application->SetVar($event->getPrefixSpecial().'_PreCreate', 1); $event->redirect=false; } @@ -1229,6 +1296,17 @@ } + function OnReset(&$event) + { + //do nothing - should reset :) + if ($this->isNewItemCreate($event)) { + // just reset id to 0 in case it was create + $object =& $event->getObject( Array('skip_autoload' => true) ); + $object->setID(0); + $this->Application->SetVar($event->getPrefixSpecial().'_id',0); + } + } + /** * Apply same processing to each item beeing selected in grid * @@ -1509,8 +1587,12 @@ */ function finalizePopup(&$event) { - $event->redirect = 'incs/close_popup'; + $event->SetRedirectParam('opener', 'u'); + + + /*return ; + // 2. substitute opener $opener_stack = $this->Application->RecallVar('opener_stack'); $opener_stack = $opener_stack ? unserialize($opener_stack) : Array(); @@ -1529,10 +1611,10 @@ $new_level = 'index.php|'.ltrim($this->Application->BuildEnv($t, $redirect_params, 'all', $pass_events), ENV_VAR_NAME.'='); array_push($opener_stack, $new_level); - $this->Application->StoreVar('opener_stack', serialize($opener_stack)); + $this->Application->StoreVar('opener_stack', serialize($opener_stack));*/ } - + /** * Create search filters based on search query * @@ -1542,7 +1624,7 @@ function OnSearch(&$event) { $event->setPseudoClass('_List'); - + $search_helper =& $this->Application->recallObject('SearchHelper'); $search_helper->performSearch($event); } @@ -1775,7 +1857,7 @@ $this->RemoveRequiredFields($object); $is_new = !$object->isLoaded(); - $is_main = $this->Application->GetVar($event->Prefix.'_mode') == 't'; + $is_main = substr($this->Application->GetVar($event->Prefix.'_mode'), 0, 1) == 't'; if ($is_new) { $new_event = $is_main ? 'OnPreCreate' : 'OnNew'; @@ -1826,7 +1908,11 @@ $export_helper =& $this->Application->recallObject('CatItemExportHelper'); $event->redirect = $export_t ? $export_t : $export_helper->getModuleFolder($event).'/export'; - $redirect_params = Array( 'm_opener' => 'd', + list($index_file, $env) = explode('|', $this->Application->RecallVar('last_template')); + $finish_url = $this->Application->BaseURL('/admin').$index_file.'?'.ENV_VAR_NAME.'='.$env; + $this->Application->StoreVar('export_finish_url', $finish_url); + + $redirect_params = Array( $this->Prefix.'.export_event' => 'OnNew', 'pass' => 'all,'.$this->Prefix.'.export'); @@ -1870,6 +1956,7 @@ function OnExportBegin(&$event) { $export_helper =& $this->Application->recallObject('CatItemExportHelper'); + /* @var $export_helper kCatDBItemExportHelper */ $export_helper->OnExportBegin($event); } @@ -1924,6 +2011,20 @@ } } + /** + * Saves changes & changes language + * + * @param kEvent $event + */ + function OnPreSaveAndChangeLanguage(&$event) + { + $event->CallSubEvent('OnPreSave'); + + if ($event->status == erSUCCESS) { + $this->Application->SetVar('m_lang', $this->Application->GetVar('language')); + } + } + }