/** * Default run method for default 'write-typed' controller * * Save data from the posted form into the first parameter object using standard method. * Create a new instance of this object if no identifier was given. * * @param $parameters Parameters * @param $form array * @param $files array * @param $class_name string * @return string * @throws Exception */ public function run(Parameters $parameters, $form, $files, $class_name) { $object = $parameters->getMainObject($class_name); Dao::begin(); try { $builder = new Post_Files(); $form = $builder->appendToForm($form, $files); $builder = new Object_Builder_Array(); $builder->null_if_empty_sub_objects = true; $builder->build($form, $object); $write_objects = []; foreach ($builder->getBuiltObjects() as $write_object) { if ($write_object == $object || Dao::getObjectIdentifier($write_object)) { $write_objects[] = $write_object; } } $write_error = false; foreach ($write_objects as $write_object) { if (!Dao::write($write_object)) { $write_error = true; break; } } $write_error ? Dao::rollback() : Dao::commit(); } catch (Exception $exception) { Dao::rollback(); throw $exception; } $parameters = $this->getViewParameters($parameters, $class_name, $write_error); return View::run($parameters, $form, $files, $class_name, Feature::F_WRITE); }
/** * @param $array * @param $object * @return array */ private function initLinkObject(&$array, &$object) { /** @var $link Class_\Link_Annotation */ $link = $this->class->getAnnotation('link'); if ($link->value) { $id_property_value = null; $linked_class_name = null; $link_properties = $link->getLinkProperties(); $search = []; foreach ($link_properties as $property) { if ($property->getType()->isClass()) { $property_name = $property->getName(); $id_property_name = 'id_' . $property_name; if (isset($array[$id_property_name]) && $array[$id_property_name]) { $search[$property_name] = $array[$id_property_name]; } $property_class_name = $property->getType()->asString(); if (is_a($property_class_name, $link->value, true)) { $id_property_value = isset($array[$id_property_name]) ? $array[$id_property_name] : null; $linked_class_name = $property_class_name; if (!isset($array[$id_property_name]) && !isset($array[$property_name])) { $linked_array = $array; foreach (array_keys($link_properties) as $link_property_name) { unset($linked_array[$link_property_name]); } $builder = new Object_Builder_Array($property_class_name, $this->from_form); $array[$property_name] = $builder->build($linked_array); } } } } if (count($search) >= 2) { $object = Dao::searchOne($search, $this->class->name); } if ($id_property_value && !$object) { $object = Builder::createClone(Dao::read($id_property_value, $linked_class_name), $this->class->name); } return $search; } return null; }
/** * Store the row into the data store * * @param $row array * @param $data_store List_Data|array[]|object[] * @return boolean false if the callback returned false to stop the read process */ private function store($row, &$data_store) { $result = true; if ($data_store instanceof List_Data) { $id = array_pop($row); $data_store->add($data_store->newRow($this->class_name, $id, $row)); } else { // calculate index $index = []; foreach ($this->key as $key) { if (isset($row[$key])) { $index[] = $row[$key]; } } $index = join(DOT, $index); // store if (isset($this->class_name)) { if ($index !== '') { if (isset($data_store[$index])) { $data_store[$index] = $this->object_builder->build($row, $data_store[$index]); } else { $result = $this->doCallback($data_store); $data_store[$index] = $this->object_builder->build($row); } } else { $result = $this->doCallback($data_store); $data_store[] = $this->object_builder->build($row); } } elseif ($index !== '') { $result = $this->doCallback($data_store); $data_store[$index] = $row; } else { $result = $this->doCallback($data_store); $data_store[] = $row; } } return $result; }
/** * @param $object object * @param $null_if_empty boolean * @return mixed */ public function buildValue($object, $null_if_empty) { $builder = new Object_Builder_Array(); return $builder->buildMap($this->value, $this->property->getType()->getElementTypeAsString()); }