addElement('text','txtToken','Token Name: '); $rate_ele = $form->addElement('text','txtCallrate','Callrate : '); $form->addElement('text','txtEffectiveDate','Effective Date : '); $metPhil = array( 'M'=>'Flat rate on answer', 'S'=>'Step rate for Cell, first 60 sec and then every 30 sec', 'I'=>'Per minute billing', 'T'=>'VTI based on time duration only', 'V'=>'VTI based on time duration and distance involved' ); $form->addElement('select','selMeteringPhilosophy','Metering Philosophy : ',$metPhil); $form->addElement('text','txtMinCost','Minimum Cost : '); $form->addElement('text','txtCost1','Cost 1: '); $form->addElement('text','txtCost2','Cost 2: '); $form->addElement('text','txtCost3','Cost 3: '); $form->addElement('submit','btnSubmit',ucfirst($_GET['action'])); //}}} //{{{ create rules $form->addRule('txtToken','*required','required'); $form->addRule('txtCallrate','*required','required'); $form->addRule('txtEffectiveDate','*required','required'); //}}} //{{{ --- MAIN CODE --- switch($_GET['action']) { case 'add' : $form->addElement('header','hdrTop','Add New Rate'); break; case 'delete' : case 'edit' : $rate_id = intval($_POST['rid']); $smarty_obj->assign('rid',$rate_id); //{{{ assign defaults from db $r_query="SELECT * FROM `rate` WHERE `rt_id`=?"; $r_st = $db->prepare($r_query); $r_res =$db->execute($r_st,array($rate_id)); if (handleError($r_res)) { $r_res->fetchInto($r_row); } $defaults = array( 'txtToken'=>$r_row['rt_channel_out'], 'txtCallrate'=>$r_row['rt_callrate'], 'txtEffectiveDate'=>$r_row['rt_effective'], 'selMeteringPhilosophy'=>$r_row['rt_metering'], 'txtMinCost'=>$r_row['rt_min_cost'], 'txtCost1'=>$r_row['rt_rate1_cost'], 'txtCost2'=>$r_row['rt_rate2_cost'], 'txtCost3'=>$r_row['rt_rate3_cost'] ); $form->setDefaults($defaults); //}}} if($_GET['action']=='delete') { $form->addElement('header','hdrTop','Delete Rate Entry'); $form->freeze(); } else { $form->addElement('header','hdrTop','Edit Existing Rate'); $token_ele->freeze(); $rate_ele->freeze(); } break; default : $smarty_obj = new SmartyQFWrapper('access_denied.tpl'); }//end of switch if($form->validate()) { $result= $form->process('process_data',false); $form->addElement('text','txtResult',$result); } $smarty_obj->display($form); //}}} /** * process_data * * callback function do all the processing for the form */ function process_data($args) { global $form; global $db; if ('Add'==$args['btnSubmit']) { $sel_query = "SELECT * FROM `rate` WHERE `rt_channel_out` = ? AND `rt_callrate` = ? AND `rt_effective` = ?"; $data = array( $args['txtToken'], $args['txtCallrate'], $args['txtEffectiveDate'] ); $sel_st = $db->prepare($sel_query); $sel_res = $db->execute($sel_st,$data); if(handleError($sel_res)) { if(0==$sel_res->numRows()) { $ins_query = "INSERT INTO `rate` (`rt_channel_out`, `rt_callrate`, `rt_effective`, `rt_metering`, `rt_min_cost`, `rt_rate1_cost`, `rt_rate2_cost`, `rt_rate3_cost`) VALUES (?,?,?,?,?,?,?,?)"; $data = array( $args['txtToken'], $args['txtCallrate'], $args['txtEffectiveDate'], $args['selMeteringPhilosophy'], $args['txtMinCost'], $args['txtCost1'], $args['txtCost2'], $args['txtCost3'] ); $ins_st = $db->prepare($ins_query); $ins_res= $db->execute($ins_st,$data); if(handleError($ins_res)) { return "Rate inserted successfully"; } else { return "Unable to insert Rate!"; } } else { return "There is already a rate with those credentials."; } } else { return "Unable to check if rate already exists"; } } if ('Edit'==$args['btnSubmit']) { $sel_query = "SELECT * FROM `rate` WHERE `rt_channel_out` = ? AND `rt_callrate` = ? AND `rt_effective` = ? AND `rt_id` <> ?"; $data = array( $args['txtToken'], $args['txtCallrate'], $args['txtEffectiveDate'], $args['rid'] ); $sel_st = $db->prepare($sel_query); $sel_res = $db->execute($sel_st,$data); if(handleError($sel_res)) { if(0==$sel_res->numRows()) { $upd_query = "UPDATE rate SET `rt_effective` = ?, `rt_metering` = ?, `rt_min_cost` = ?, `rt_rate1_cost` = ?, `rt_rate2_cost` = ?, `rt_rate3_cost` = ? WHERE rt_id = ?"; $data = array( $args['txtEffectiveDate'], $args['selMeteringPhilosophy'], $args['txtMinCost'], $args['txtCost1'], $args['txtCost2'], $args['txtCost3'], $args['rid'] ); $upd_st = $db->prepare($upd_query); $upd_res = $db->execute($upd_st,$data); if(handleError($upd_res)) { return "Rate updated successfully"; } else { return "Unable to update Rate!"; } } else { return "There is already a rate with those credentials."; } } else { return "Unable to check if rate already exists"; } } if ('Delete' == $args['btnSubmit']) { $del_query = "DELETE FROM rate WHERE rt_id = ?"; $del_st = $db->prepare($del_query); $del_res= $db->execute($del_st,array($args['rid'])); if(handleError($del_res)) { return "Rate deleted successfully"; } else { return "Unable to delete rate"; } } } // end of function process_data ?>