/**
  * Store a newly created resource in storage.
  *
  * @param  Request  $request
  * @return Response
  */
 public function store(Request $request)
 {
     $this->validate($request, ['prompt' => 'required|max:100|unique_with:forms, formgroup = form_group_id, formtype = form_type_id', 'formgroup' => 'required|not_in:0', 'formtype' => 'required|not_in:0']);
     \DB::beginTransaction();
     try {
         $form = new Form();
         $form->form_group_id = $request->formgroup;
         $form->form_type_id = $request->formtype;
         $form->prompt = $request->prompt;
         $form->required = $request->required == '1' ? 1 : 0;
         $form->expected_answer = $request->expected_answer == '1' ? 1 : 0;
         $form->exempt = $request->exempt == '1' ? 1 : 0;
         $form->save();
         if ($request->formtype == 9) {
             $multiData = array();
             foreach ($request->multiselect as $option) {
                 $multiData[] = array('form_id' => $form->id, 'multi_select_id' => $option);
             }
             if (count($multiData) > 0) {
                 FormMultiSelect::insert($multiData);
             }
         }
         if ($request->formtype == 10) {
             $singleData = array();
             foreach ($request->singleselect as $option) {
                 $singleData[] = array('form_id' => $form->id, 'single_select_id' => $option);
             }
             if (count($singleData) > 0) {
                 FormSingleSelect::insert($singleData);
             }
         }
         if ($request->formtype == 11) {
             if ($request->has('formula')) {
                 $formformula = new FormFormula();
                 $formformula->form_id = $form->id;
                 $formformula->formula = $request->formula;
                 $formformula->save();
             }
         }
         \DB::commit();
         Session::flash('flash_message', 'Form successfully added!');
         return redirect()->route("form.index");
     } catch (Exception $e) {
         DB::rollBack();
         return redirect()->back();
     }
 }
 public function storeform(Request $request, $id)
 {
     // dd($request->all());
     $this->validate($request, ['category' => 'required', 'group' => 'required', 'prompt' => 'required', 'formtype' => 'required']);
     \DB::beginTransaction();
     try {
         $template = AuditTemplate::find($id);
         $form_type = FormType::find($request->formtype);
         $prompt = $request->prompt;
         foreach ($request->category as $cat_id) {
             $category = FormCategory::find($cat_id);
             $cat_order = 1;
             $a_cat_id = 0;
             $clast_cnt = AuditTemplateCategory::getLastOrder($template->id);
             if (empty($clast_cnt)) {
                 $a_cat = AuditTemplateCategory::create(['category_order' => $cat_order, 'audit_template_id' => $template->id, 'category_id' => $category->id]);
                 $a_cat_id = $a_cat->id;
             } else {
                 $cat = AuditTemplateCategory::categoryExist($template->id, $category->id);
                 if (empty($cat)) {
                     $cat_order = $clast_cnt->category_order + 1;
                     $a_cat = AuditTemplateCategory::create(['category_order' => $cat_order, 'audit_template_id' => $template->id, 'category_id' => $category->id]);
                     $a_cat_id = $a_cat->id;
                 } else {
                     $a_cat_id = $cat->id;
                 }
             }
             foreach ($request->group as $grp_id) {
                 $group = FormGroup::find($grp_id);
                 $grp_order = 1;
                 $a_grp_id = 0;
                 $glast_cnt = AuditTemplateGroup::getLastOrder($a_cat_id);
                 if (empty($glast_cnt)) {
                     $a_grp = AuditTemplateGroup::create(['group_order' => $grp_order, 'audit_template_category_id' => $a_cat_id, 'form_group_id' => $group->id]);
                     $a_grp_id = $a_grp->id;
                 } else {
                     $grp = AuditTemplateGroup::categoryExist($a_cat_id, $group->id);
                     if (empty($grp)) {
                         $grp_order = $glast_cnt->group_order + 1;
                         $a_grp = AuditTemplateGroup::create(['group_order' => $grp_order, 'audit_template_category_id' => $a_cat_id, 'form_group_id' => $group->id]);
                         $a_grp_id = $a_grp->id;
                     } else {
                         $a_grp_id = $grp->id;
                     }
                 }
                 $form = Form::create(array('audit_template_id' => $template->id, 'form_type_id' => $form_type->id, 'prompt' => strtoupper($prompt), 'required' => $request->required == '1' ? 1 : 0, 'expected_answer' => $request->expected_answer == '1' ? 1 : 0, 'exempt' => $request->exempt == '1' ? 1 : 0));
                 if ($request->formtype == 9) {
                     $multiData = array();
                     foreach ($request->multiselect as $option) {
                         $multiData[] = array('form_id' => $form->id, 'multi_select_id' => $option);
                     }
                     if (count($multiData) > 0) {
                         FormMultiSelect::insert($multiData);
                     }
                 }
                 if ($request->formtype == 10) {
                     $singleData = array();
                     foreach ($request->singleselect as $option) {
                         $singleData[] = array('form_id' => $form->id, 'single_select_id' => $option);
                     }
                     if (count($singleData) > 0) {
                         FormSingleSelect::insert($singleData);
                     }
                 }
                 if ($request->formtype == 11) {
                     if ($request->has('formula')) {
                         $text = $request->formula;
                         preg_match_all('/:(.*?):/', $text, $matches);
                         $index = array();
                         foreach ($matches[1] as $value) {
                             $split_up = explode('_', $value);
                             $last_item = $split_up[count($split_up) - 1];
                             $index[] = $last_item;
                         }
                         $formula1 = $text;
                         foreach ($matches[1] as $key => $a) {
                             $formula1 = str_replace(':' . $a . ':', $index[$key], $formula1);
                         }
                         $formformula = new FormFormula();
                         $formformula->form_id = $form->id;
                         $formformula->formula = $formula1;
                         $formformula->formula_desc = $request->formula;
                         $formformula->save();
                     }
                 }
                 if ($request->formtype == 12) {
                     if ($request->has('condition')) {
                     }
                 }
                 $order = 1;
                 $a_frm_id = 0;
                 $last_cnt = AuditTemplateForm::getLastOrder($a_grp_id);
                 if (!empty($last_cnt)) {
                     $order = $last_cnt->order + 1;
                 }
                 AuditTemplateForm::insert(array('audit_template_group_id' => $a_grp_id, 'order' => $order, 'audit_template_id' => $template->id, 'form_id' => $form->id));
             }
         }
         \DB::commit();
         Session::flash('flash_message', 'Template successfully added!');
         return redirect()->route("audittemplate.form", $id);
     } catch (Exception $e) {
         DB::rollBack();
         Session::flash('flash_message', 'An error occured in adding form!');
         return redirect()->back();
     }
 }