Initialise the import reader and locate the column indexes.

@return bool false is a failed import
179997
public function init() {
        if ($this->csvreader == null) {
            $this->csvreader = new csv_import_reader($this->importid, 'assignfeedback_offline');
        }
        $this->csvreader->init();

        $columns = $this->csvreader->get_columns();

        $strgrade = get_string('grade');
        $strid = get_string('recordid', 'assign');
        $strmodified = get_string('lastmodifiedgrade', 'assign');

        foreach ($this->assignment->get_feedback_plugins() as $plugin) {
            if ($plugin->is_enabled() && $plugin->is_visible()) {
                foreach ($plugin->get_editor_fields() as $field => $description) {
                    $this->feedbackcolumnindexes[$description] = array('plugin'=>$plugin,
                                                                       'field'=>$field,
                                                                       'description'=>$description);
                }
            }
        }

        if ($columns) {
            foreach ($columns as $index => $column) {
                if (isset($this->feedbackcolumnindexes[$column])) {
                    $this->feedbackcolumnindexes[$column]['index'] = $index;
                }
                if ($column == $strgrade) {
                    $this->gradeindex = $index;
                }
                if ($column == $strid) {
                    $this->idindex = $index;
                }
                if ($column == $strmodified) {
                    $this->modifiedindex = $index;
                }
            }
        }

        if ($this->idindex < 0 || $this->gradeindex < 0 || $this->modifiedindex < 0) {
            return false;
        }

        $groupmode = groups_get_activity_groupmode($this->assignment->get_course_module());
        // All users.
        $groupid = 0;
        $groupname = '';
        if ($groupmode) {
            $groupid = groups_get_activity_group($this->assignment->get_course_module(), true);
            $groupname = groups_get_group_name($groupid).'-';
        }
        $this->validusers = $this->assignment->list_participants($groupid, false);
        return true;
    }
168408
public function initElements()
    {

        if(!isset($this->options['class_properties'])){
    		throw new Exception('No class properties found');
    	}
    	if(!isset($this->options['csv_column'])){
    		throw new Exception('No csv columns found');
    	}

        $columnsOptions = array();
        $columnsOptionsLabels = array();
    	$columnsOptionsLiteral =  array();
    	$columnsOptionsLiteral['csv_select'] = ' --- ' . __('Select') . ' --- ';
    	$columnsOptionsLiteral['csv_null']  = ' --- ' . __("Don't set");
        
        $columnsOptionsRanged = array();
        $columnsOptionsRanged['csv_select'] = ' --- ' . __('Select') . ' --- ';
        $columnsOptionsRanged['csv_null'] = ' --- ' . __('Use default value');
    	
    	// We build the list of CSV columns that can be mapped to
    	// the target class properties. 
    	if (isset($this->options[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES])
    	    && $this->options[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES]){
	    	foreach($this->options['csv_column'] as $i => $column){
	    		$columnsOptions[$i.tao_models_classes_import_CsvImporter::OPTION_POSTFIX] = __('Column') . ' ' . ($i + 1) . ' : ' . $column;
                $columnsOptionsLabels[$i.tao_models_classes_import_CsvImporter::OPTION_POSTFIX] = $this->prepareString($column);
	    	}
    	} else {
    		// We do not know column so we display more neutral information
    		// about columns to the end user.
    		for ($i = 0; $i < count($this->options['csv_column']); $i++){
				$columnsOptions[$i.tao_models_classes_import_CsvImporter::OPTION_POSTFIX] = __('Column') . ' ' . ($i + 1);
	    	}
    	}

    	foreach($this->options['class_properties'] as $propertyUri => $propertyLabel){

    		$propElt = tao_helpers_form_FormFactory::getElement($propertyUri, 'Combobox');
    		$propElt->setDescription($propertyLabel);

            // literal or ranged?
            $options = array_key_exists($propertyUri, $this->options['ranged_properties'])
                ? array_merge($columnsOptionsRanged, $columnsOptions)
                : array_merge($columnsOptionsLiteral, $columnsOptions);

			$value = 'csv_select';
			if (isset($_POST[$propertyUri]) && isset($options[$_POST[$propertyUri]])) {
				$value = $_POST[$propertyUri];
			}

            // We trying compare current label with option labels from file and set most suitable
            $label = $this->prepareString($propertyLabel);
            $value = key(preg_grep("/^$label$/", $columnsOptionsLabels)) ?: $value;

            $propElt->setOptions($options);
            $propElt->setValue($value);

			/** Add mandatory label */
			if (tao_helpers_Uri::decode($propertyUri)==OntologyRdfs::RDFS_LABEL) {
				$elementEqualsToCsvSelect = new tao_helpers_form_elements_xhtml_Hidden();
				$elementEqualsToCsvSelect->setValue('csv_select');
				$elementEqualsToCsvSelect->setDescription(' to null');
				$propElt->addValidator(tao_helpers_form_FormFactory::getValidator(
					'Equals', array(
						'reference' => $elementEqualsToCsvSelect,
						'invert' => true
					)
				));
			}

    		$this->form->addElement($propElt);
    	}
        if(count($this->options['class_properties']) > 0){
            $this->form->createGroup('property_mapping', __('Map the properties to the CSV columns'), array_keys($this->options['class_properties']));
        }
    	$ranged = array();
    	foreach($this->options['ranged_properties'] as $propertyUri => $propertyLabel){
    		$property = new core_kernel_classes_Property(tao_helpers_Uri::decode($propertyUri));
    		$propElt = tao_helpers_form_GenerisFormFactory::elementMap($property);
    		if(!is_null($propElt)){
    			$defName = tao_helpers_Uri::encode($property->getUri()) . self::DEFAULT_VALUES_SUFFIX;
    			$propElt->setName($defName);
				if ($this->form->getElement($propertyUri)->getRawValue()=='csv_null') {
					$propElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
				}
    			$this->form->addElement($propElt);
    			$ranged[$defName] = $propElt;
    		}
    	}

    	if(count($this->options['ranged_properties']) > 0){
    		$this->form->createGroup('ranged_property', __('Define the default values'), array_keys($ranged));
		}
    	
    	$importFileEle = tao_helpers_form_FormFactory::getElement('importFile', 'Hidden');
    	$this->form->addElement($importFileEle);
    	
    	$optDelimiter = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_DELIMITER, 'Hidden');
		$this->form->addElement($optDelimiter);
		
		$optEncloser = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_ENCLOSER, 'Hidden');
		$this->form->addElement($optEncloser);
		
		$optMulti = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::MULTI_VALUES_DELIMITER, 'Hidden');
		$this->form->addElement($optMulti);

		if (isset($this->options[tao_models_classes_import_CsvUploadForm::IS_OPTION_FIRST_COLUMN_ENABLE])){
		    if ($this->options[tao_models_classes_import_CsvUploadForm::IS_OPTION_FIRST_COLUMN_ENABLE] === true){
                $optFirstColumn = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, 'Hidden');
                $this->form->addElement($optFirstColumn);
            }
        }else{
            $optFirstColumn = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, 'Hidden');
            $this->form->addElement($optFirstColumn);
        }
    }
186547
public static function __initialize():bool
    {
        $env = (new ModuleManager())->getCurrentEnv();
        $base =  realpath(__DIR__.'/../../../../../../').DIRECTORY_SEPARATOR;
        self::__setPaths($base, $env, '', true);
        FEnv::set(
            "framework.fcm.config.commands.file",
            $base."vendor/iumio/framework/Core/Additional/Manager/Configs/commands.json"
        );
        return (true);
    }
408587
public function ImportForm()
    {
        $modelSNG = singleton($this->modelClass);
        $modelName = $modelSNG->i18n_singular_name();
        // check if a import form should be generated
        if (!$this->showImportForm ||
            (is_array($this->showImportForm) && !in_array($this->modelClass, $this->showImportForm))
        ) {
            return false;
        }

        $importers = $this->getModelImporters();
        if (!$importers || !isset($importers[$this->modelClass])) {
            return false;
        }

        if (!$modelSNG->canCreate(Security::getCurrentUser())) {
            return false;
        }

        $fields = new FieldList(
            new HiddenField('ClassName', false, $this->modelClass),
            new FileField('_CsvFile', false)
        );

        // get HTML specification for each import (column names etc.)
        $importerClass = $importers[$this->modelClass];
        /** @var BulkLoader $importer */
        $importer = new $importerClass($this->modelClass);
        $spec = $importer->getImportSpec();
        $specFields = new ArrayList();
        foreach ($spec['fields'] as $name => $desc) {
            $specFields->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
        }
        $specRelations = new ArrayList();
        foreach ($spec['relations'] as $name => $desc) {
            $specRelations->push(new ArrayData(array('Name' => $name, 'Description' => $desc)));
        }
        $specHTML = $this->customise(array(
            'ClassName' => $this->sanitiseClassName($this->modelClass),
            'ModelName' => Convert::raw2att($modelName),
            'Fields' => $specFields,
            'Relations' => $specRelations,
        ))->renderWith($this->getTemplatesWithSuffix('_ImportSpec'));

        $fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
        $fields->push(
            new CheckboxField(
                'EmptyBeforeImport',
                _t('SilverStripe\\Admin\\ModelAdmin.EMPTYBEFOREIMPORT', 'Replace data'),
                false
            )
        );

        $actions = new FieldList(
            FormAction::create('import', _t('SilverStripe\\Admin\\ModelAdmin.IMPORT', 'Import from CSV'))
                ->addExtraClass('btn btn-outline-secondary font-icon-upload')
        );

        $form = new Form(
            $this,
            "ImportForm",
            $fields,
            $actions
        );
        $form->setFormAction(
            Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'ImportForm')
        );

        $this->extend('updateImportForm', $form);

        return $form;
    }
1211224
protected static function initConnection()
    {
        $readConfig = new DBConfig();
        self::$conn_list = $readConfig->getAllConnection();
        self::$conn_list['db'] = $readConfig->getDefaultConnection();
        self::$conn_name_list = array_keys(self::$conn_list);
    }
461311
public function InitializeIndexer()
    {
        $bWorkToDo = true;
        if (!$this->bRegenerateCompleteIndex) {
            // drop index
            $query = "UPDATE shop_search_reindex_queue SET `processing` = '1'";
            MySqlLegacySupport::getInstance()->query($query);
            $query = "SELECT COUNT(*) AS reccount FROM shop_search_reindex_queue WHERE `processing` = '1'";
            $aCount = MySqlLegacySupport::getInstance()->fetch_assoc(MySqlLegacySupport::getInstance()->query($query));
            if ($aCount['reccount'] > 0) {
                $query = "SHOW TABLES LIKE '_index_%'";
                $tRes = MySqlLegacySupport::getInstance()->query($query);
                while ($aIndexTable = MySqlLegacySupport::getInstance()->fetch_row($tRes)) {
                    $query = "DELETE FROM {$aIndexTable[0]} USING {$aIndexTable[0]}, shop_search_reindex_queue WHERE {$aIndexTable[0]}.`shop_article_id`= shop_search_reindex_queue.object_id AND `shop_search_reindex_queue`.`processing`='1' ";
                    MySqlLegacySupport::getInstance()->query($query);
                }
                $sQuery = "DELETE FROM shop_search_reindex_queue WHERE `processing` = '1' AND `action` = 'delete'";
                MySqlLegacySupport::getInstance()->query($sQuery);

                $query = "SELECT COUNT(*) AS reccount FROM shop_search_reindex_queue WHERE `processing` = '1'";
                $aCount = MySqlLegacySupport::getInstance()->fetch_assoc(MySqlLegacySupport::getInstance()->query($query));
                if (0 == $aCount['reccount']) {
                    $bWorkToDo = false;
                }
            } else {
                $bWorkToDo = false;
            }
        }
        if ($bWorkToDo) {
            $this->aTablesToProcess = array();
            $aData = $this->sqlData;
            $aData['started'] = date('Y-m-d H:i:s');
            $aData['completed'] = date('0000-00-00 00:00:00');
            $iTotalRows = 0;
            $query = 'SELECT shop_search_query.*
                    FROM shop_search_query
              INNER JOIN shop_search_field_weight ON shop_search_query.id = shop_search_field_weight.shop_search_query_id
                GROUP BY shop_search_query.id
                 ';
            $oIndexQueries = &TdbShopSearchQueryList::GetList($query);
            while ($oIndexQuery = &$oIndexQueries->Next()) {
                $oIndexQuery->StartIndex($this->bRegenerateCompleteIndex);
                $iNumberOfItems = $oIndexQuery->NumberOfRecordsLeftToIndex();
                if ($iNumberOfItems > 0) {
                    $iTotalRows += $iNumberOfItems;
                    $this->aTablesToProcess[] = $oIndexQuery->id;
                }
            }
            $aData['total_rows_to_process'] = $iTotalRows;
            $sTmp = serialize($this->aTablesToProcess);
            $aData['processdata'] = $sTmp;

            $this->LoadFromRow($aData);
            $this->AllowEditByAll(true);
            $this->Save();

            $this->CommitProcessData();

            $this->CreateIndexTables();
        }
    }
1362198
protected function read()
    {
        $handle = fopen($this->config['path'], "r");

        if (!$handle)
            throw new ImportException('Unable to open file, or file not exist');

        if (!$this->import->CheckIfFileIsCML($this->config['path'])) {
            throw new ImportException('File is not valid');
        }

        $this->xml->DropTemporaryTables();
        $this->xml->CreateTemporaryTables();
        $this->xml->ReadXMLToDatabase($handle, $this->session, $this->config['interval']);
        $this->xml->IndexTemporaryTables();
    }
1084413
public function init() {
		if ($this->initialized)
			return $this;
		$this->initialized = true;
		$this->check_tables();
		return $this;
	}
285795
protected function initIndexesInfo()
    {
        $this->_indexNames = [];
        $this->_indexTypes = [];
        $indexes = $this->findIndexes();
        foreach ($indexes as $index) {
            $indexName = $index['Index'];
            $this->_indexNames[] = $indexName;
            $this->_indexTypes[$indexName] = $index['Type'];
        }
    }
1097330
public function init()
    {
        Profiler::start('init', 'init');
        $this->initTime = microtime(true);
        $this->init = true;
        Log::d('Initializing framework...');
//        $this->register_error_handler();
        date_default_timezone_set('Europe/Madrid');
        $this->setupLanguage();

        Profiler::stop('init');
        $this->manager = new ModuleManager($this->config, $this->directory);
        $this->manager->init();
        Log::d('Module manager initialized');
        $this->inited = true;

//        Log::d('Modules initialized: ' . count($this->manager->getModules()));
//        Log::d(array_map(function ($a) {
//            return $a->title;
//        }, $this->manager->getModules()));
    }
990470
protected function initHaltHooks()
    {

        // error handler
        set_error_handler(function ($level, $message, $file = null, $line = null) {
            /** @var $this Application|Console */
            $event = $this->getApplicationEvent();
            $this->emit($event->setName($this::EVENT_HANDLE_ERROR), $level, $message, $file, $line);
        });

        // exception handler
        set_exception_handler(function ($exception) {
            /** @var $this Application|Console */
            /** @var \Exception|\Throwable $exception */
            // Convert throwable to exception for backwards compatibility
            if (!($exception instanceof \Exception)) {
                $throwable = $exception;
                $exception = new \ErrorException(
                    $throwable->getMessage(),
                    $throwable->getCode(),
                    E_ERROR,
                    $throwable->getFile(),
                    $throwable->getLine()
                );
            }

            $event = $this->getApplicationEvent();
            $this->emit($event->setName($this::EVENT_SYSTEM_EXCEPTION), $exception);
        });

        if (method_exists($this, 'shutdown')) {
            // shutdown function
            register_shutdown_function([$this, 'shutdown']);
        }
    }
111802
private static function init()
    {
        if (self::$reader) {
            return self::$reader;
        }

        $reader      = new self();
        $databaseSrc = __DIR__ . '/' . $reader->database;
        if (is_readable($databaseSrc) === FALSE) {
            throw new \InvalidArgumentException("The IP Database file \"{$databaseSrc}\" does not exist or is not readable.");
        }
        $reader->file = @fopen($databaseSrc, 'rb');
        if ($reader->file === FALSE) {
            throw new \InvalidArgumentException("IP Database File opening \"{$databaseSrc}\".");
        }
        $reader->fileSize = @filesize($databaseSrc);
        if ($reader->fileSize === FALSE) {
            throw new \UnexpectedValueException("Error determining the size of \"{$databaseSrc}\".");
        }

        $metaLength = unpack('N', fread($reader->file, 4))[1];
        $text       = fread($reader->file, $metaLength);

        $reader->meta = (array)json_decode($text, true);

        if (!isset($reader->meta['fields']) || !isset($reader->meta['languages'])) {
            throw new \Exception('IP Database metadata error.');
        }

        $fileSize = 4 + $metaLength + $reader->meta['total_size'];
        if ($fileSize != $reader->fileSize) {
            throw  new \Exception('IP Database size error.');
        }

        $reader->nodeCount  = $reader->meta['node_count'];
        $reader->nodeOffset = 4 + $metaLength;

        self::$reader = $reader;

        return $reader;
    }
196588
public function doFinalInitialization()
    {
        // Heavy indexing must wait until after all columns composing
        // a table's primary key have been parsed.
        if ($this->heavyIndexing) {
            $this->doHeavyIndexing();
        }

        // if idMethod is "native" and in fact there are no autoIncrement
        // columns in the table, then change it to "none"
        $anyAutoInc = false;
        foreach ($this->columns as $column) {
            if ($column->isAutoIncrement()) {
                $anyAutoInc = true;
            }
        }
        if (IdMethod::NATIVE === $this->getIdMethod() && !$anyAutoInc) {
            $this->setIdMethod(IdMethod::NO_ID_METHOD);
        }
    }
613234
private function importSeeds()
	{
		$databaseFolder = base_path( 'resources/database/seeds/' );
		if( ! file_exists( $databaseFolder ) ) {
			return $this->line( 'no seeds to move' );
		}
		$this->moveFilesToDatabaseSeeds( $databaseFolder );
	}
1223881
protected function setupReader()
    {
        require_once 'ExcelReader/reader.php';
        $this->reader = new \Spreadsheet_Excel_Reader();
        $this->reader->setOutputEncoding( $this->encoding );
        $this->reader->read( $this->file_path );

    }
244313
public function import()
    {
        $result = [
            'languages' => ['new' => 0, 'updated' => 0],
            'languageSources' => ['new' => 0, 'updated' => 0],
            'languageTranslations' => ['new' => 0, 'updated' => 0],
        ];

        $data = $this->parseImportFile();

        /** @var Language[] $languages */
        $languages = Language::find()->indexBy('language_id')->all();

        foreach ($data['languages'] as $importedLanguage) {
            if (isset($languages[$importedLanguage['language_id']])) {
                $language = $languages[$importedLanguage['language_id']];
            } else {
                $language = new Language();
            }

            //cast integers
            $importedLanguage['status'] = (int) $importedLanguage['status'];

            $language->attributes = $importedLanguage;
            if (count($language->getDirtyAttributes())) {
                $saveType = $language->isNewRecord ? 'new' : 'updated';
                if ($language->save()) {
                    ++$result['languages'][$saveType];
                } else {
                    $this->throwInvalidModelException($language);
                }
            }
        }

        /** @var LanguageSource[] $languageSources */
        $languageSources = LanguageSource::find()->indexBy('id')->all();

        /** @var LanguageTranslate[] $languageTranslations */
        $languageTranslations = LanguageTranslate::find()->all();

        /*
         *  Create 2 dimensional array for current and imported translation, first index by LanguageSource->id
         *  and than indexed by LanguageTranslate->language.
         *  E.g.: [
         *      id => [
         *          language => LanguageTranslate (for $languageTranslations) / Array (for $importedLanguageTranslations)
         *          ...
         *      ]
         *      ...
         * ]
         */
        $languageTranslations = ArrayHelper::map($languageTranslations, 'language', function ($languageTranslation) {
            return $languageTranslation;
        }, 'id');
        $importedLanguageTranslations = ArrayHelper::map($data['languageTranslations'], 'language', function ($languageTranslation) {
            return $languageTranslation;
        }, 'id');

        foreach ($data['languageSources'] as $importedLanguageSource) {
            $languageSource = null;

            //check if id exist and if category and messages are matching
            if (isset($languageSources[$importedLanguageSource['id']]) &&
                ($languageSources[$importedLanguageSource['id']]->category == $importedLanguageSource['category']) &&
                ($languageSources[$importedLanguageSource['id']]->message == $importedLanguageSource['message'])
            ) {
                $languageSource = $languageSources[$importedLanguageSource['id']];
            }

            if (is_null($languageSource)) {
                //no match by id, search by message
                foreach ($languageSources as $languageSourceSearch) {
                    if (($languageSourceSearch->category == $importedLanguageSource['category']) &&
                        ($languageSourceSearch->message == $importedLanguageSource['message'])
                    ) {
                        $languageSource = $languageSourceSearch;
                        break;
                    }
                }
            }

            if (is_null($languageSource)) {
                //still no match, create new
                $languageSource = new LanguageSource([
                    'category' => $importedLanguageSource['category'],
                    'message' => $importedLanguageSource['message'],
                ]);

                if ($languageSource->save()) {
                    ++$result['languageSources']['new'];
                } else {
                    $this->throwInvalidModelException($languageSource);
                }
            }

            //do we have translations for the current source?
            if (isset($importedLanguageTranslations[$importedLanguageSource['id']])) {
                //loop through the translations for the current source
                foreach ($importedLanguageTranslations[$importedLanguageSource['id']] as $importedLanguageTranslation) {
                    $languageTranslate = null;

                    //is there already a translation for this souce
                    if (isset($languageTranslations[$languageSource->id]) &&
                        isset($languageTranslations[$languageSource->id][$importedLanguageTranslation['language']])
                    ) {
                        $languageTranslate = $languageTranslations[$languageSource->id][$importedLanguageTranslation['language']];
                    }

                    //no translation found, create a new one
                    if (is_null($languageTranslate)) {
                        $languageTranslate = new LanguageTranslate();
                    }

                    $languageTranslate->attributes = $importedLanguageTranslation;

                    //overwrite the id because the $languageSource->id might be different from the $importedLanguageTranslation['id']
                    $languageTranslate->id = $languageSource->id;

                    if (count($languageTranslate->getDirtyAttributes())) {
                        $saveType = $languageTranslate->isNewRecord ? 'new' : 'updated';
                        if ($languageTranslate->save()) {
                            ++$result['languageTranslations'][$saveType];
                        } else {
                            $this->throwInvalidModelException($languageTranslate);
                        }
                    }
                }
            }
        }

        return $result;
    }
907547
public function initialize()
    {
        $initializationQuery = 'INSERT INTO tx_solr_indexqueue_item (root, item_type, item_uid, indexing_configuration, indexing_priority, changed, errors) '
            . $this->buildSelectStatement() . ',"" '
            . 'FROM ' . $this->type . ' '
            . 'WHERE '
            . $this->buildPagesClause()
            . $this->buildTcaWhereClause()
            . $this->buildUserWhereClause();
        $logData = ['query' => $initializationQuery];

        try {
            $logData['rows'] = $this->queueItemRepository->initializeByNativeSQLStatement($initializationQuery);
        } catch (DBALException $DBALException) {
            $logData['error'] = $DBALException->getCode() . ': ' . $DBALException->getMessage();
        }

        $this->logInitialization($logData);
        return true;
    }