getPrefixSpecial(true) instead of * $event->Prefix_Special as usual. This is due PHP * is converting "." symbols in variable names during * submit info "_". $event->getPrefixSpecial optional * 1st parameter returns correct corrent Prefix_Special * for variables beeing submitted such way (e.g. variable * name that will be converted by PHP: "users.read_only_id" * will be submitted as "users_read_only_id". * * 2. When using $this->Application-LinkVar on variables submitted * from form which contain $Prefix_Special then note 1st item. Example: * LinkVar($event->getPrefixSpecial(true).'_varname',$event->Prefix_Special.'_varname') * */ /** * EventHandler that is used to process * any database related events * */ class kDBEventHandler extends kEventHandler { /** * Get's ID of item to be edited. * Returns 1st id in case if many * items were selected. * * @param kEvent $event * @return int */ function getPassedID(&$event) { $ret=$this->Application->GetVar($event->getPrefixSpecial().'_id'); if($ret===false) { $ids=$this->Application->GetVar($event->getPrefixSpecial().'_selected_ids'); $ids=explode(',',$ids); if($ids) $ret=array_shift($ids); } return $ret; } /** * Builds item (loads if needed) * * @param kEvent $event * @access protected */ function OnItemBuild(&$event) { $object =& $this->createObject(&$event); $this->dbBuild(&$object,&$event); $sql=$this->getSelectSQL($event,'OnItemPrepareQuery'); $object->setSelectSQL($sql); // 1. set from config what needed $fields = $this->Application->getUnitOption($event->Prefix,'Fields'); $object->setConfigFields( array_keys($fields) ); // 2. loads if allowed $auto_load = $this->Application->getUnitOption($event->Prefix,'AutoLoad'); if($auto_load) { $id = $this->getPassedID(&$event); $object->Load($id); } $this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnUpdate'); } /** * Builds list * * @param kEvent $event * @access protected */ function OnListBuild(&$event) { $event->setPseudoClass('_List'); $object =& $this->createObject(&$event); $this->dbBuild(&$object,&$event); $sql=$this->getSelectSQL($event,'OnListPrepareQuery'); $object->setSelectSQL($sql); $t=$this->Application->GetVar('t'); $this->Application->StoreVar('redirect_to',$t); $this->SetPagination(&$event); $this->SetSorting(&$event); } /** * Set's correct page for list * based on data provided with event * * @param kEvent $event * @access private * @see OnListBuild */ function SetPagination(&$event) { $per_page = $event->getEventParam('per_page'); if(!$per_page) { $per_page=$this->Application->RecallVar($event->Prefix_Special.'_PerPage'); if(!$per_page) { $per_page=10; } } $event->setPseudoClass('_List'); $object =& $this->createObject(&$event); $object->SetPerPage($per_page); $object->CountRecs(); $object->SetPage( $this->Application->GetLinkedVar( $event->Prefix_Special.'_Page' ) ); } /** * Set's correct sorting for list * based on data provided with event * * @param kEvent $event * @access private * @see OnListBuild */ function SetSorting(&$event) { $event->setPseudoClass('_List'); $object =& $this->createObject(&$event); $cur_sort1 = $this->Application->RecallVar($event->Prefix_Special.'_Sort1'); $cur_sort1_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort1_Dir'); $cur_sort2 = $this->Application->RecallVar($event->Prefix_Special.'_Sort2'); $cur_sort2_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort2_Dir'); //Use default if not specified /*if ( $cur_sort1 === false || $cur_sort1_dir == false ) { $cur_sort1 = $this->Application->ConfigOption($event->Prefix_Special.'_Sort1'); $cur_sort1_dir = $this->Application->ConfigOption($event->Prefix_Special.'_Sort1_Dir'); $cur_sort2 = $this->Application->ConfigOption($event->Prefix_Special.'_Sort2'); $cur_sort2_dir = $this->Application->ConfigOption($event->Prefix_Special.'_Sort2_Dir'); }*/ if($cur_sort1 != '' && $cur_sort1_dir != '') { $object->AddOrderField($cur_sort1, $cur_sort1_dir); } if($cur_sort2 != '' && $cur_sort2_dir != '') { $object->AddOrderField($cur_sort2, $cur_sort2_dir); } } /** * Some kind of filter processing stuff. * Not in use by now * */ function AddFilters() { } /** * Set's new sorting for list * * @param kEvent $event * @access protected */ function OnSetSorting(&$event) { $this->Application->LinkVar($event->getPrefixSpecial(true).'_Sort1',$event->Prefix_Special.'_Sort1'); $this->Application->LinkVar($event->getPrefixSpecial(true).'_Sort1_Dir',$event->Prefix_Special.'_Sort1_Dir'); //$event->setPseudoClass('_List'); //$object =& $this->createObject(&$event); } /** * Common builder part for Item & List * * @param Object $object * @access private */ function dbBuild(&$object,&$event) { // set Item & List common parameters from config $table = $this->Application->getUnitOption($event->Prefix,'TableName'); $object->setTableName($table); $id_field = $this->Application->getUnitOption($event->Prefix,'IDField'); $object->setIDField($id_field); // get selected ids from post & save them to session $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); if($items_info) { $ids=array_keys($items_info); $this->Application->SetVar($event->getPrefixSpecial().'_selected_ids',implode(',',$ids)); } $this->Application->LinkVar($event->getPrefixSpecial().'_selected_ids'); // set's any possible hidden fields needed for both Item & List $current_event = $this->Application->GetVar($event->Prefix_Special.'_event'); $this->Application->setEvent($event->Prefix_Special,$current_event); } /** * Returns select query for loading item/list * * @param kEvent $event * @param string $event_name * @return string * @access protected */ function getSelectSQL(&$event,$event_name) { $new_event =& $this->inheritEvent(&$event); $new_event->Name=$event_name; $this->Application->HandleEvent(&$new_event); return $event->getEventParam('SQLQuery'); } /** * Creates needed sql query to load item, * if no query is defined in config for * special requested, then use default * query * * @param kEvent $event * @access protected */ function OnItemPrepareQuery(&$event) { $sqls = $this->Application->getUnitOption($event->Prefix,'ItemSQLs'); $sql = isset($sqls[$event->Special])?$sqls[$event->Special]:$sqls['']; $event->MasterEvent->setEventParam('SQLQuery',$sql); } /** * Creates needed sql query to load list, * if no query is defined in config for * special requested, then use default * query * * @param kEvent $event * @access protected */ function OnListPrepareQuery(&$event) { $sqls = $this->Application->getUnitOption($event->Prefix,'ListSQLs'); $sql = isset($sqls[$event->Special])?$sqls[$event->Special]:$sqls['']; $event->MasterEvent->setEventParam('SQLQuery',$sql); } /** * Creates new kDBItem * * @param kEvent $event * @access protected */ function OnCreate(&$event) { $this->Application->setUnitOption($this->getPrefixSpecial(),'AutoLoad',false); $object =& $this->createObject(&$event); $this->prepareObject(&$object,&$event); $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); if($items_info) $field_values = array_shift($items_info); $object->SetFieldsFromHash($field_values); if( $object->Create() ) { $event->status=erSUCCESS; } else { $event->status=erFATAL; $event->redirect=false; } } /** * Updates kDBItem * * @param kEvent $event * @access protected */ function OnUpdate(&$event) { $this->Application->setUnitOption($this->getPrefixSpecial(),'AutoLoad',false); $object =& $this->createObject(&$event); $this->prepareObject(&$object,&$event); $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); if($items_info) { foreach($items_info as $id => $field_values) { //$object->Load($id); $object->SetFieldsFromHash($field_values); if( $object->Update($id) ) { $event->status=erSUCCESS; } else { $event->status=erFATAL; $event->redirect=false; break; } } } } /** * Delete's kDBItem object * * @param kEvent $event * @access protected */ function OnDelete(&$event) { $this->Application->setUnitOption($this->getPrefixSpecial(),'AutoLoad',false); $object =& $this->createObject(&$event); $object->ID=$this->Application->GetVar($event->Prefix_Special.'_id'); if( $object->Delete() ) { $event->status=erSUCCESS; } else { $event->status=erFATAL; $event->redirect=false; break; } } /** * Prepares new kDBItem object * * @param kEvent $event * @access protected */ function OnNew(&$event) { $this->Application->setUnitOption($this->getPrefixSpecial(),'AutoLoad',false); $object =& $this->createObject(&$event); $this->prepareObject(&$object,&$event); $object->setID(0); $this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnCreate'); $event->redirect=false; } /** * Cancel's kDBItem Editing/Creation * * @param kEvent $event * @access protected */ function OnCancel(&$event) { } } ?>