From 83088a1b26d03648f4e972e1509c7b198b0d576d Mon Sep 17 00:00:00 2001 From: sanjeev Date: Sat, 8 Jan 2022 21:07:00 +0530 Subject: [PATCH] JSONquestionfilter recommitted : ps --- .../Template_Management_Controller.php | 146 ++++++------------ 1 file changed, 46 insertions(+), 100 deletions(-) diff --git a/api/application/controllers/Template_Management_Controller.php b/api/application/controllers/Template_Management_Controller.php index c9515805..9ff49ecc 100644 --- a/api/application/controllers/Template_Management_Controller.php +++ b/api/application/controllers/Template_Management_Controller.php @@ -1319,78 +1319,28 @@ class Template_Management_Controller extends REST_Controller { $json_table = $this->Template_Management_Model->selectCustomRecords($fields,$where_condition_array,TEMPLATE_JSON_V2); if(count($json_table)>0){ $json = json_decode($json_table[0]['json'],true); - // print_r($json['questions']);die(); $quest = $json['questions']; + if($form_id == 19) $quest = $json['questions'][0]['question']; + // ch(1,2,22,20,11,13) + // print_r($quest);die(); $filter_arr = array(); - $key_arr = array(); - //try 1; - // for($i= 0;$i $value) { - // // $filter_arr[$i] = $value['question']; - // // $key_arr[$i] = $value['question_key']; - // $onchange_properties = isset($value['onchange_properties'][0]['form_controls']) ? $value['onchange_properties'][0]['form_controls'] : array(); - // $count = isset($onchange_properties) ? count($onchange_properties) : 0 ; - // if($count>0){ - // foreach ($onchange_properties as $key1 => $value1) { - // // print_r($onchange_properties); - // $question_index = $value1['insert_index']; - // if($value1['insert_index'] == $value['question_key']){ - // $sub_quest = $value1['questions']; - // $filter_sub_arr = array(); - // for($l= 0;$l$filter_sub_arr); - // } - // } - // } - // }else{ - // $filter_arr[] = $value['question']; - // } - // // $i++; - // }} - //try 3; - $arr =array(); + //try 4 if(!empty($quest)){ - $a1 = $this->fns1($quest);//filter only question and question_key - $arr = $this->fns2($quest,$a1); // filter the onchange; + $arr1 = $this->call_arr1($quest); + $arr2 = $this->call_arr2($quest); + $filter_arr = $this->merged_arr($arr1,$arr2); } - - - //end - if(!empty($arr)){ + + if(!empty($filter_arr)){ $data['dataStatus'] = true; $data['status'] = REST_Controller::HTTP_OK; - $data['available_question'] = $arr; + $data['available_question'] = $filter_arr; $data['json'] = $quest; $this->response($data,REST_Controller::HTTP_OK); }else{ $data['dataStatus'] = false; $data['status'] = REST_Controller::HTTP_NOT_MODIFIED; - $data['msg'] = 'Something Went Wrong! Try again Later'; + $data['msg'] = 'There is no JSON template. try again'; $this->response($data,REST_Controller::HTTP_OK); } } @@ -1404,39 +1354,27 @@ class Template_Management_Controller extends REST_Controller { } } - // public function Auto($quest){ - // // return 1; - // $return_arr = array(); - // if(!empty($quest)){ - // foreach ($quest as $key => $value) { - // $return_arr = $value['question'] && $var['question_key']; - // } - // } - // return $return_arr; - // } - - public function fns1($quest){ - // return $key == 'question' && $key == 'question_key'; - // $new_array = array_filter($quest, function($var) use ($filter){ - // return ($var['question'] && $var['question_key']); - // }); - // $filtered = array_filter($quest, function($v) { return ($v['question']); }); + //Step 1 : Question Filter Only + public function call_arr1($quest){ $return_arr = array(); $i = 0 ; if(!empty($quest)){ foreach ($quest as $key => $value) { - $return_arr[$i] = array('question'=> $value['question'],'question_key'=>$value['question_key']); + // $return_arr[$i] = array('question'=> $value['question'],'question_key'=>$value['question_key']); // $return_arr[$i]['question'] = $value['question']; // $return_arr[$i]['question_key'] = $value['question_key']; + $return_arr[$i] = $value['question']; $i++; } } return $return_arr; } - public function fns2($quest,$arr1){ - $arr2 = array(); - $i = 0; + //Step 2 : Onchange fns - Question Filter + public function call_arr2($quest){ + $return_arr = array(); + $i = 0 ; + if(!empty($quest)){ foreach ($quest as $key => $value) { if((isset($value['onchange_properties'])) && (!empty($value['onchange_properties'])) && ($value['onchange_properties'] != null)){ $onchange_properties = isset($value['onchange_properties'][0]['form_controls']) ? $value['onchange_properties'][0]['form_controls'] : array(); @@ -1444,33 +1382,41 @@ class Template_Management_Controller extends REST_Controller { if($count>0 && !empty($onchange_properties)){ foreach ($onchange_properties as $key1 => $value1) { if($value1['insert_index'] == $value['question_key']){ - // $arr2[$i]['child'] = $this->fns1($value1['questions']); - $try2 = $this->fns1($value1['questions']); - $arr2[$i]['child'] = self::fns2($value1,$try2); + $temp[$i] = $this->call_arr1($value1['questions']); + $arr2 = $this->call_arr2($value1['questions']); + // $arr1 = $this->call_arr1($value1['questions']); + // $arr2[$i]['child'] = $this->call_arr2($temp[$i],$arr2); + $return_arr[$i] = $this->merged_arr($temp[$i],$arr2); + // foreach($value1['question'] as $key2 => $value2){ + // $temp2[$i] = $this->call_arr1($value2['question']); + // $arr3 = $this->call_arr2($value2['question']); + // $return_arr[$i] = $this->merged_arr($temp2[$i],$arr3); + // } } } } } - // else{} - $i++; + $i++; } - // $arr1 = $this->fns1($quest); + } + return $return_arr; + } + + //Step 3 : Mapping Here for single-array using 2-array(arr1,arr2) + public function merged_arr($arr1,$arr2){ $final_result = array(); foreach($arr1 as $key=>$val){ // Loop though one array if (array_key_exists($key, $arr2)) - { - $val2 = $arr2[$key]; // Get the values from the other array - $final_result[$key] = $val + $val2; // combine 'em - } - else - { - $final_result[$key] = $val; - } + { + $val2 = $arr2[$key]; // Get the values from the other array + $final_result[$val] = $val2; // combine here (mapping as key) + // $final_result[$key] = $val + $val2; + // $final_result[$key][$val] = $val2; + } + else{ + $final_result[$key] = $val; // combine here (mapping as key) + } } return $final_result; - // } - // $return_arr = $this->fns1($quest); - // return $return_arr; - } } \ No newline at end of file