/** * Store a Chart * 1. store Chart * * @param Chart * @return Response */ public function store($org_id = null, $branch_id = null) { //check branch $branch = \App\ThunderID\OrganisationManagementV1\Models\Branch::id($branch_id)->organisationid($org_id)->first(); if (!$branch) { \App::abort(404); } if (!Input::has('chart')) { return new JSend('error', (array) Input::all(), 'Tidak ada data Chart.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate Chart Parameter $chart = Input::get('chart'); if (is_null($chart['id'])) { $is_new = true; } else { $is_new = false; } $chart_rules = ['branch_id' => 'exists:hrom_branches,id|' . ($is_new ? '' : 'in:' . $branch_id), 'name' => 'required|max:255', 'department' => 'required|max:255']; //1a. Get original data $chart_data = \App\ThunderID\OrganisationManagementV1\Models\Chart::findornew($chart['id']); //1b. Validate Basic Chart Parameter $validator = Validator::make($chart, $chart_rules); if (!$validator->passes()) { $errors->add('Chart', $validator->errors()); } else { //if validator passed, save Chart $chart['branch_id'] = $branch_id; if (isset($chart['path'])) { unset($chart['path']); } $chart_data = $chart_data->fill($chart); if (!$chart_data->save()) { $errors->add('Chart', $chart_data->getError()); } } //End of validate Chart if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); $final_Chart = \App\ThunderID\OrganisationManagementV1\Models\Chart::id($chart_data['id'])->with(['chart'])->first()->toArray(); return new JSend('success', (array) $final_Chart); }