public function makePointSet($jsonArray) { $arr = json_decode($jsonArray); $pointSet = PointSet::create(['X' => $arr[0], 'Y' => $arr[1], 'Z' => $arr[2]]); return $pointSet; }
public static function fromXml($xml) { $xpath = new DOMXpath($xml); $simulation = new static(); $simulationNode = $xpath->query('//simulationDefinition/simulation')->item(0); $simulation->Id = strtoupper($simulationNode->getAttribute('id')); $simulationAttributes = ['Caption', 'SegmentationType', 'Progress', 'State', 'Color', 'Active']; foreach ($simulationAttributes as $simulationAttribute) { $simulation->{$simulationAttribute} = $simulationNode->getAttribute(strtolower($simulationAttribute)); } //if (Simulation::find($simulation->Id)) // return [false, "Simulation with this ID already exists"]; $simulationNeedle = []; $combination = Combination::find($xpath->query('//simulationDefinition/combination/@id')->item(0)->value); if (!$combination) { throw new Exception("Cannot find combination (you may be able to work around this manually from the XML)"); } $patient = DB::table('ItemSet_Patient')->where('Id', '=', $xpath->query('//simulationDefinition/simulation/patient/@id')->item(0)->value)->get(); if (empty($patient)) { throw new Exception("Patient no longer exists"); } $patient = $patient[0]; $simulation->Combination_Id = $combination->Combination_Id; $simulation->Patient_Id = $patient->Id; $simulation->Id = null; $simulation->save(); $parameterNodes = $xpath->query('//simulationDefinition/parameters/parameter'); $parameters = []; foreach ($parameterNodes as $parameterNode) { $parameter = Parameter::whereName($parameterNode->getAttribute("name"))->first(); $simulation->Parameters()->attach($parameter, ["ValueSet" => $parameterNode->getAttribute("value")]); } $needleNodes = $xpath->query('//simulationDefinition/numericalModel/needles/needle'); foreach ($needleNodes as $needleNode) { $needle = Needle::find($needleNode->getAttribute("id")); if (!$needle) { throw new Exception("Needle not found"); } $simulationNeedle = new SimulationNeedle(); $simulationNeedle->Needle_Id = $needle->Id; $simulationNeedle->Simulation_Id = $simulation->Id; $simulationNeedle->save(); $parameterNodes = $xpath->query('//simulationDefinition/numericalModel/needles/needle/parameters/parameter'); $parameters = []; foreach ($parameterNodes as $parameterNode) { $parameter = Parameter::whereName($parameterNode->getAttribute("name"))->first(); switch ($parameter->Name) { case "NEEDLE_TIP_LOCATION": $target = PointSet::fromArray(json_decode($parameterNode->getAttribute("value"))); $target->save(); $simulationNeedle->Target_Id = $target->Id; break; case "NEEDLE_ENTRY_LOCATION": $entry = PointSet::fromArray(json_decode($parameterNode->getAttribute("value"))); $entry->save(); $simulationNeedle->Entry_Id = $entry->Id; break; default: $simulationNeedle->Parameters()->attach($parameter, ["ValueSet" => $parameterNode->getAttribute("value")]); } } $simulationNeedle->save(); } //foreach ($parameters as $sP) //{ // $sP->Simulation_Id = $simulation->Id; // $sP->save(); //} $simulation->save(); return $simulation; }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { $simulation = Simulation::find($id); if (Input::has('caption')) { $simulation->Caption = Input::get('caption'); } $simulation->save(); if (Input::has('Combination_Id')) { $simulation->Combination_Id = Input::get('Combination_Id'); $simulation->save(); $this->rebuild($simulation->Id); } foreach ($simulation->Parameters as $parameter) { if (Input::has('parameters-' . $parameter->Id)) { $parameter->pivot->ValueSet = Input::get('parameters-' . $parameter->Id); $parameter->pivot->save(); } } if (Input::get('new-parameter-name') && Input::get('new-parameter-value') != '') { $parameter = Parameter::whereName(Input::get('new-parameter-name'))->first(); if ($parameter) { $simulation->Parameters()->attach($parameter, ["ValueSet" => Input::get('new-parameter-value')]); $simulation->save(); } } foreach ($simulation->SimulationNeedles as $simulationNeedle) { foreach ($simulationNeedle->Parameters as $parameter) { if (Input::has('needle-parameters-' . $simulationNeedle->Id . '-' . $parameter->Id)) { $parameter->pivot->ValueSet = Input::get('needle-parameters-' . $simulationNeedle->Id . '-' . $parameter->Id); $parameter->pivot->save(); } } $newparameterprefix = 'needle-' . $simulationNeedle->Id . '-new-parameter-'; if (Input::get($newparameterprefix . 'name') && Input::get($newparameterprefix . 'value') != '') { $parameter = Parameter::whereName(Input::get($newparameterprefix . 'name'))->first(); if ($parameter) { $simulationNeedle->Parameters()->attach($parameter, ["ValueSet" => Input::get($newparameterprefix . 'value')]); $simulationNeedle->save(); } } } if (Input::get('removing')) { if (Input::get('simulation-needle-id')) { $simulationNeedle = SimulationNeedle::find(Input::get('simulation-needle-id')); $simulationNeedle->delete(); } if (Input::get('region-remove-id')) { $region = Region::find(Input::get('region-remove-id')); if (Input::get('region-remove-location')) { $simulation->Regions()->newPivotStatementForId($region->Id)->where('Location', '=', Input::get('region-remove-location'))->delete(); } else { $simulation->Regions()->newPivotStatementForId($region->Id)->whereNull('Location')->delete(); } } } else { if (Input::get('needle-id')) { $needle = Needle::find(Input::get('needle-id')); $simulationNeedle = new SimulationNeedle(); $simulationNeedle->Simulation_Id = $simulation->Id; $simulationNeedle->Needle_Id = $needle->Id; $target = json_decode(Input::get('needle-target')); $simulationNeedle->Target_Id = PointSet::create(['X' => $target[0], 'Y' => $target[1], 'Z' => $target[2]])->Id; $entry = json_decode(Input::get('needle-entry')); $simulationNeedle->Entry_Id = PointSet::create(['X' => $entry[0], 'Y' => $entry[1], 'Z' => $entry[2]])->Id; $simulationNeedle->save(); } if (Input::get('region-id')) { $region = Region::find(Input::get('region-id')); $simulation->Regions()->attach($region, ['Location' => Input::get('region-location')]); $simulation->save(); } } return Redirect::route('simulation.edit', $id); }