/** * This will be called for this controller, always. * * @param $parameters Parameters * @param $form array * @param $files array * @param $class_name string * @return mixed */ public function run(Parameters $parameters, $form, $files, $class_name) { set_time_limit(900); $import = Import_Builder_Form::build($form, Session::current()->get(Files::class)->files); $import->class_name = $class_name; $parameters->getMainObject($import); $parameters = $parameters->getObjects(); foreach ($import->worksheets as $worksheet) { $array = $worksheet->file->getCsvContent(); $import_array = new Import_Array($worksheet->settings, $import->class_name); $import_array->importArray($array); } $parameters[Template::TEMPLATE] = 'importDone'; return View::run($parameters, $form, $files, $class_name, Feature::F_IMPORT); }
/** * @param $data array * @param $properties string[] */ public function __construct($data = null, $properties = null) { if (isset($properties)) { $this->properties = $properties; } if (isset($data)) { $this->data = $data; $constants = Import_Array::getConstantsFromArray($data); $row = current($data); if (!isset($this->properties)) { foreach ($row as $column_number => $property_path) { if (!isset($constants[$property_path])) { $this->properties[$column_number] = $property_path; } } next($data); } // next row is the first row (in 1..n keys instead of 0..n of the $data array) $this->first_row = key($data) + 1; $this->last_row = min($this->first_row + 10, count($this->data)); $this->last_column = count($this->properties); } }
/** * @param $constants string[] key is the property path (can be translated or alias) */ public function setConstants($constants) { $class_name = $this->getClassName(); $properties_alias = Import_Array::getPropertiesAlias($class_name); $use_reverse_translation = Locale::current() ? true : false; foreach ($constants as $property_path => $value) { $property_path = Import_Array::propertyPathOf($class_name, $property_path, $use_reverse_translation, $properties_alias); $property_name = ($i = strrpos($property_path, DOT)) === false ? $property_path : substr($property_path, $i + 1); $master_path = substr($property_path, 0, $i); if (isset($this->classes[$master_path])) { $this->classes[$master_path]->constants[$property_name] = new Reflection_Property_Value($this->classes[$master_path]->class_name, $property_name, $value, true); } } }
/** * @param $parameters Parameters * @param $form array * @param $files array * @param $class_name string * @return mixed * @todo factorize */ public function run(Parameters $parameters, $form, $files, $class_name) { // convert form files to worksheets and session files if ($files) { /** @var $import Import */ $import = $parameters->getMainObject(Import::class); $import->class_name = $class_name; $form = (new Post_Files())->appendToForm($form, $files); foreach ($form as $file) { if ($file instanceof File) { if (!isset($session_files)) { /** @var $session_files Files */ $session_files = Builder::create(Files::class); } $excel = Spreadsheet_File::fileToArray($file->temporary_file_name, $errors); $worksheet_number = 0; foreach ($excel as $temporary_file_name => $worksheet) { if (filesize($temporary_file_name) > 1) { $import_worksheet = Builder::create(Import_Worksheet::class, [$worksheet_number++, Import_Settings_Builder::buildArray($worksheet, $class_name), $csv_file = Builder::create(File::class, [$temporary_file_name])]); $import_worksheet->errors = $errors; $session_files->files[] = $csv_file; $import->worksheets[] = $import_worksheet; } } // only one file once break; } } if (isset($session_files)) { Session::current()->set($session_files); } } else { /** @var $files File[] */ $files = Session::current()->get(Files::class)->files; $parameters->unshift($import = Import_Builder_Form::build($form, $files)); $import->class_name = $class_name; } // prepare parameters $parameters = $parameters->getObjects(); $general_buttons = $this->getGeneralButtons($class_name); if (isset($parameters['constant_remove']) && strtoupper($parameters['constant_remove'][0]) === $parameters['constant_remove'][0]) { $parameters['constant_remove'] = rParse($parameters['constant_remove'], DOT); } foreach ($import->worksheets as $worksheet) { // apply controller parameters if (isset($parameters['constant_add']) && isset($worksheet->settings->classes[$parameters['constant_add']])) { $worksheet->settings->classes[$parameters['constant_add']]->addConstant(); } if (isset($parameters['constant_remove']) && isset($worksheet->settings->classes[lLastParse($parameters['constant_remove'], DOT, 1, false)])) { $worksheet->settings->classes[lLastParse($parameters['constant_remove'], DOT, 1, false)]->removeConstant(rLastParse($parameters['constant_remove'], DOT, 1, true)); } Custom_Settings_Controller::applyParametersToCustomSettings($worksheet->settings, array_merge($form, $parameters)); } // recover empty Import_Settings (after loading empty one) /** @var $files File[] */ $files = Session::current()->get(Files::class)->files; foreach ($import->worksheets as $worksheet_number => $worksheet) { if (empty($worksheet->settings->classes)) { $file = $files[$worksheet_number]; $array = $file->getCsvContent(); $import->worksheets[$worksheet_number] = new Import_Worksheet($worksheet_number, Import_Settings_Builder::buildArray($array, $class_name), $file); } } // get general buttons and customized import settings foreach ($import->worksheets as $worksheet_number => $worksheet) { $customized_import_settings = $worksheet->settings->getCustomSettings(); $worksheet_general_buttons = $general_buttons; if (!isset($customized_import_settings[$worksheet->settings->name])) { unset($worksheet_general_buttons['delete']); } $parameters['custom'][$worksheet_number] = new StdClass(); $parameters['custom'][$worksheet_number]->customized_lists = $customized_import_settings; $parameters['custom'][$worksheet_number]->general_buttons = $worksheet_general_buttons; $parameters['custom'][$worksheet_number]->settings = $worksheet->settings; $parameters['custom'][$worksheet_number]->aliases_property = Import_Array::getPropertiesAlias($worksheet->settings->getClassName()); $parameters['custom'][$worksheet_number]->properties_alias = array_flip($parameters['custom'][$worksheet_number]->aliases_property); } // view $parameters[Template::TEMPLATE] = 'importPreview'; return View::run($parameters, $form, $files, $class_name, Feature::F_IMPORT); }
/** * Builds import settings using a data array * * First line must contain the class name (can be a short name, namespace will automatically be found) * Second line must contain the fields paths, relative to the class * Other liens contain data, and are not used * * @param $array array two dimensional array (keys are row, col) * @param $class_name string default class name (if not found into array) * @return Import_Settings */ public static function buildArray(&$array, $class_name = null) { $class_name = Import_Array::getClassNameFromArray($array) ?: $class_name; $settings = new Import_Settings($class_name); /** @var $classes Import_Class[] */ $classes = []; $properties_path = Import_Array::getPropertiesFromArray($array, $class_name); $auto_identify = self::autoIdentify($class_name, $properties_path); foreach ($properties_path as $property_path) { $sub_class = $class_name; $last_identify = false; $class_path = ''; $property_path_for_class = []; foreach (explode(DOT, $property_path) as $pos => $property_name) { $identify = substr($property_name, -1) !== '*'; if (!$identify) { $property_name = substr($property_name, 0, -1); } $class_key = join(DOT, $property_path_for_class); if (!isset($classes[$class_key])) { $classes[$class_key] = new Import_Class($sub_class, $property_path_for_class, $last_identify ? 'tell_it_and_stop_import' : 'create_new_value'); } $class = $classes[$class_key]; $import_property = new Import_Property($sub_class, $property_name); try { $property = new Reflection_Property($sub_class, $property_name); if ($identify && !$auto_identify || isset($auto_identify[$property_path]) && isset($auto_identify[$property_path][$pos])) { $class->identify_properties[$property_name] = $import_property; } else { $class->write_properties[$property_name] = $import_property; } $sub_class = $property->getType()->getElementTypeAsString(); $class_path .= $sub_class . DOT; } catch (ReflectionException $exception) { $class->ignore_properties[$property_name] = $import_property; $class->unknown_properties[$property_name] = $import_property; } $last_identify = $identify; $property_path_for_class[] = $property_name; } } $settings->classes = $classes; $settings->setConstants(Import_Array::getConstantsFromArray($array)); return $settings; }