Exemple #1
0
	function testParsingWithExplicitHeaderRow() {
		/* If your CSV file doesn't have a header row */
		$csv = new CSVParser($this->getCurrentRelativePath() .'/CsvBulkLoaderTest_PlayersWithHeader.csv');
		
		$csv->provideHeaderRow(array('__fn','__bio','__bd','__reg'));
		
		$firstNames = $birthdays = $biographies = $registered = array();
		foreach($csv as $record) {
			/* Each row in the CSV file will be keyed with the header row that you gave */
			$this->assertEquals(array('__fn','__bio','__bd','__reg'), array_keys($record));
			$firstNames[] = $record['__fn'];
			$biographies[] = $record['__bio'];
			$birthdays[] = $record['__bd'];
			$registered[] = $record['__reg'];
		}
		
		/* And the first row will be returned in the data */		
		$this->assertEquals(array('FirstName','John','Jane','Jamie','Järg'), $firstNames);
		$this->assertEquals(array(
			'Biography',
			"He's a good guy",
			"She is awesome.\nSo awesome that she gets multiple rows and \"escaped\" strings in her biography",
			"Pretty old, with an escaped comma",
			"Unicode FTW"), $biographies);
		$this->assertEquals(array("Birthday","31/01/1988","31/01/1982","31/01/1882","31/06/1982"), $birthdays);
		$this->assertEquals(array('IsRegistered', '1', '0', '1', '1'), $registered);
	}
 /**
  * @param string $filepath
  * @param boolean $preview
  */
 protected function processAll($filepath, $preview = false)
 {
     $results = new BulkLoader_Result();
     $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure);
     // ColumnMap has two uses, depending on whether hasHeaderRow is set
     if ($this->columnMap) {
         // if the map goes to a callback, use the same key value as the map
         // value, rather than function name as multiple keys may use the
         // same callback
         foreach ($this->columnMap as $k => $v) {
             if (strpos($v, "->") === 0) {
                 $map[$k] = $k;
             } else {
                 $map[$k] = $v;
             }
         }
         if ($this->hasHeaderRow) {
             $csv->mapColumns($map);
         } else {
             $csv->provideHeaderRow($map);
         }
     }
     foreach ($csv as $row) {
         $this->processRecord($row, $this->columnMap, $results, $preview);
     }
     return $results;
 }
 function test_csv()
 {
     $desired_outputs = array(6, 5);
     $log = array();
     $options = array("opt_min_backlinks" => 1);
     $csvParser = new CSVParser($log, $options);
     foreach ($this->test_files as $key => $file) {
         $_FILES['ebn_import']['tmp_name'] = $file;
         $posts = $csvParser->parseUploadedMajesticFile();
         $this->assertEquals($desired_outputs[$key], count($posts));
     }
 }
 protected function processAll($filepath, $preview = false)
 {
     $results = new BulkLoader_Result();
     $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure);
     // ColumnMap has two uses, depending on whether hasHeaderRow is set
     if ($this->columnMap) {
         if ($this->hasHeaderRow) {
             $csv->mapColumns($this->columnMap);
         } else {
             $csv->provideHeaderRow($this->columnMap);
         }
     }
     foreach ($csv as $row) {
         $this->processRecord($row, $this->columnMap, $results, $preview);
     }
     return $results;
 }
 public function MakeValueFromString($sProposedValue, $bLocalizedValue = false, $sSepItem = null, $sSepAttribute = null, $sSepValue = null, $sAttributeQualifier = null)
 {
     if (is_null($sSepItem) || empty($sSepItem)) {
         $sSepItem = MetaModel::GetConfig()->Get('link_set_item_separator');
     }
     if (is_null($sSepAttribute) || empty($sSepAttribute)) {
         $sSepAttribute = MetaModel::GetConfig()->Get('link_set_attribute_separator');
     }
     if (is_null($sSepValue) || empty($sSepValue)) {
         $sSepValue = MetaModel::GetConfig()->Get('link_set_value_separator');
     }
     if (is_null($sAttributeQualifier) || empty($sAttributeQualifier)) {
         $sAttributeQualifier = MetaModel::GetConfig()->Get('link_set_attribute_qualifier');
     }
     $sTargetClass = $this->Get('linked_class');
     $sInput = str_replace($sSepItem, "\n", $sProposedValue);
     $oCSVParser = new CSVParser($sInput, $sSepAttribute, $sAttributeQualifier);
     $aInput = $oCSVParser->ToArray(0);
     $aLinks = array();
     foreach ($aInput as $aRow) {
         // 1st - get the values, split the extkey->searchkey specs, and eventually get the finalclass value
         $aExtKeys = array();
         $aValues = array();
         foreach ($aRow as $sCell) {
             $iSepPos = strpos($sCell, $sSepValue);
             if ($iSepPos === false) {
                 // Houston...
                 throw new CoreException('Wrong format for link attribute specification', array('value' => $sCell));
             }
             $sAttCode = trim(substr($sCell, 0, $iSepPos));
             $sValue = substr($sCell, $iSepPos + strlen($sSepValue));
             if (preg_match('/^(.+)->(.+)$/', $sAttCode, $aMatches)) {
                 $sKeyAttCode = $aMatches[1];
                 $sRemoteAttCode = $aMatches[2];
                 $aExtKeys[$sKeyAttCode][$sRemoteAttCode] = $sValue;
                 if (!MetaModel::IsValidAttCode($sTargetClass, $sKeyAttCode)) {
                     throw new CoreException('Wrong attribute code for link attribute specification', array('class' => $sTargetClass, 'attcode' => $sKeyAttCode));
                 }
                 $oKeyAttDef = MetaModel::GetAttributeDef($sTargetClass, $sKeyAttCode);
                 $sRemoteClass = $oKeyAttDef->GetTargetClass();
                 if (!MetaModel::IsValidAttCode($sRemoteClass, $sRemoteAttCode)) {
                     throw new CoreException('Wrong attribute code for link attribute specification', array('class' => $sRemoteClass, 'attcode' => $sRemoteAttCode));
                 }
             } else {
                 if (!MetaModel::IsValidAttCode($sTargetClass, $sAttCode)) {
                     throw new CoreException('Wrong attribute code for link attribute specification', array('class' => $sTargetClass, 'attcode' => $sAttCode));
                 }
                 $oAttDef = MetaModel::GetAttributeDef($sTargetClass, $sAttCode);
                 $aValues[$sAttCode] = $oAttDef->MakeValueFromString($sValue, $bLocalizedValue, $sSepItem, $sSepAttribute, $sSepValue, $sAttributeQualifier);
             }
         }
         // 2nd - Instanciate the object and set the value
         if (isset($aValues['finalclass'])) {
             $sLinkClass = $aValues['finalclass'];
             if (!is_subclass_of($sLinkClass, $sTargetClass)) {
                 throw new CoreException('Wrong class for link attribute specification', array('requested_class' => $sLinkClass, 'expected_class' => $sTargetClass));
             }
         } elseif (MetaModel::IsAbstract($sTargetClass)) {
             throw new CoreException('Missing finalclass for link attribute specification');
         } else {
             $sLinkClass = $sTargetClass;
         }
         $oLink = MetaModel::NewObject($sLinkClass);
         foreach ($aValues as $sAttCode => $sValue) {
             $oLink->Set($sAttCode, $sValue);
         }
         // 3rd - Set external keys from search conditions
         foreach ($aExtKeys as $sKeyAttCode => $aReconciliation) {
             $oKeyAttDef = MetaModel::GetAttributeDef($sTargetClass, $sKeyAttCode);
             $sKeyClass = $oKeyAttDef->GetTargetClass();
             $oExtKeyFilter = new DBObjectSearch($sKeyClass);
             $aReconciliationDesc = array();
             foreach ($aReconciliation as $sRemoteAttCode => $sValue) {
                 $oExtKeyFilter->AddCondition($sRemoteAttCode, $sValue, '=');
                 $aReconciliationDesc[] = "{$sRemoteAttCode}={$sValue}";
             }
             $oExtKeySet = new CMDBObjectSet($oExtKeyFilter);
             switch ($oExtKeySet->Count()) {
                 case 0:
                     $sReconciliationDesc = implode(', ', $aReconciliationDesc);
                     throw new CoreException("Found no match", array('ext_key' => $sKeyAttCode, 'reconciliation' => $sReconciliationDesc));
                     break;
                 case 1:
                     $oRemoteObj = $oExtKeySet->Fetch();
                     $oLink->Set($sKeyAttCode, $oRemoteObj->GetKey());
                     break;
                 default:
                     $sReconciliationDesc = implode(', ', $aReconciliationDesc);
                     throw new CoreException("Found several matches", array('ext_key' => $sKeyAttCode, 'reconciliation' => $sReconciliationDesc));
                     // Found several matches, ambiguous
             }
         }
         // Check (roughly) if such a link is valid
         $aErrors = array();
         foreach (MetaModel::ListAttributeDefs($sTargetClass) as $sAttCode => $oAttDef) {
             if ($oAttDef->IsExternalKey()) {
                 if ($oAttDef->GetTargetClass() == $this->GetHostClass() || is_subclass_of($this->GetHostClass(), $oAttDef->GetTargetClass())) {
                     continue;
                     // Don't check the key to self
                 }
             }
             if ($oAttDef->IsWritable() && $oAttDef->IsNull($oLink->Get($sAttCode)) && !$oAttDef->IsNullAllowed()) {
                 $aErrors[] = $sAttCode;
             }
         }
         if (count($aErrors) > 0) {
             throw new CoreException("Missing value for mandatory attribute(s): " . implode(', ', $aErrors));
         }
         $aLinks[] = $oLink;
     }
     $oSet = DBObjectSet::FromArray($sTargetClass, $aLinks);
     return $oSet;
 }
Exemple #6
0
 $oPage = new ajax_page("");
 $oPage->no_cache();
 $oPage->SetContentType('text/html');
 $sSeparator = utils::ReadParam('separator', ',', false, 'raw_data');
 $sTextQualifier = utils::ReadParam('qualifier', '"', false, 'raw_data');
 $iLinesToSkip = utils::ReadParam('do_skip_lines', 0);
 $bFirstLineAsHeader = utils::ReadParam('header_line', false);
 $sData = stripslashes(utils::ReadParam('csvdata', '', false, 'raw_data'));
 $sClassName = utils::ReadParam('class_name', '');
 $bAdvanced = utils::ReadParam('advanced', false);
 $sEncoding = utils::ReadParam('encoding', 'UTF-8');
 $sInitFieldMapping = utils::ReadParam('init_field_mapping', '', false, 'raw_data');
 $sInitSearchField = utils::ReadParam('init_search_field', '', false, 'raw_data');
 $aInitFieldMapping = empty($sInitFieldMapping) ? array() : json_decode($sInitFieldMapping, true);
 $aInitSearchField = empty($sInitSearchField) ? array() : json_decode($sInitSearchField, true);
 $oCSVParser = new CSVParser($sData, $sSeparator, $sTextQualifier);
 $aData = $oCSVParser->ToArray($iLinesToSkip);
 $iTarget = count($aData);
 if ($iTarget == 0) {
     $oPage->p(Dict::S('UI:CSVImport:NoData'));
 } else {
     $oPage->add("<table>");
     $aFirstLine = $aData[0];
     // Use the first row to determine the number of columns
     $iStartLine = 0;
     $iNbColumns = count($aFirstLine);
     if ($bFirstLineAsHeader) {
         $iStartLine = 1;
         foreach ($aFirstLine as $sField) {
             $aHeader[] = $sField;
         }
 }
 if ($sSynchronize == '1') {
     $bSynchronize = true;
 } else {
     $bSynchronize = false;
 }
 //////////////////////////////////////////////////
 //
 // Parse first line, check attributes, analyse the request
 //
 if ($sCharSet == 'UTF-8') {
     $sUTF8Data = $sCSVData;
 } else {
     $sUTF8Data = iconv($sCharSet, 'UTF-8//IGNORE//TRANSLIT', $sCSVData);
 }
 $oCSVParser = new CSVParser($sUTF8Data, $sSep, $sQualifier);
 $aInputColumns = $oCSVParser->ListFields();
 $iColCount = count($aInputColumns);
 // Check columns
 $aColumns = $oDataSource->GetSQLColumns();
 $aDateColumns = $oDataSource->GetDateSQLColumns();
 $aIsDateToTransform = array();
 $aDateToTransformReport = array();
 foreach ($aInputColumns as $iFieldId => $sInputColumn) {
     if (strlen($sDateFormat) > 0 && array_key_exists($sInputColumn, $aDateColumns)) {
         $aIsDateToTransform[$iFieldId] = true;
         $aDateToTransformReport[] = $sInputColumn;
     } else {
         $aIsDateToTransform[$iFieldId] = false;
     }
     if ($sInputColumn == 'primary_key') {
Exemple #8
0
 /**
  * Change writer type to convert to another format
  * 
  * @param    string  $filetype   Set the filetype of the file which will be written (XML/CSV/TSV/HTML/JSON)
  */
 public function convertTo($filetype)
 {
     $this->constructWriter($filetype);
     $this->writer->setData($this->parser->getField());
 }
<?php

require_once "lib/Magento.php";
$parseObj = new CSVParser();
$csv_directory = 'csv/attributes/';
$csv_file_name = $csv_directory . 'brands2jan16.csv';
$attribute_name = 'brands';
$item_rows = $parseObj->parseCSVFile($csv_file_name);
$arg_attribute = 'brand';
foreach ($item_rows as $item) {
    $brand = ucwords(strtolower($item['brands']));
    addAttributeValue($arg_attribute, $brand);
}
function addAttributeValue($arg_attribute, $arg_value)
{
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute = $attribute_model->load($attribute_code);
    $value['option'] = array($arg_value, $arg_value);
    $result = array('value' => $value);
    $attribute->setData('option', $result);
    $attribute->save();
    echo "Attribute '" . $arg_attribute . "' with \n        option '" . $arg_value . "' saved.<br />";
}
Exemple #10
0
 /**
  *
  * Handle POST submission
  *
  * @param array $options
  *
  * @return void
  *
  */
 private function handleUploadedData($options)
 {
     // Check if we have the correct nonce value for the form, else ignore the whole request
     if (!wp_verify_nonce($_POST['ebn_nonce'], 'majestic_importer_nonce')) {
         $this->log['error'][] = 'Authentication failed. Please refresh the page and try again.';
         $this->showLog();
         return;
     }
     if (empty($_FILES['ebn_import']['tmp_name'])) {
         $this->log['error'][] = 'No file uploaded, aborting.';
         $this->showLog();
         return;
     }
     if (!current_user_can('publish_pages')) {
         $this->log['error'][] = 'You don\'t have the permissions to publish pages. Please contact the blog\'s administrator.';
         $this->showLog();
         return;
     }
     $time_start = microtime(true);
     $csvParser = new CSVParser($this->log, $options);
     $imported_posts = $csvParser->parseUploadedMajesticFile();
     $imported = 0;
     foreach ($imported_posts as $post) {
         $id = $this->create_post($post, $options, get_home_url());
         if ($id) {
             $imported++;
         }
     }
     $exec_time = microtime(true) - $time_start;
     $this->log['notice'][] = sprintf("<b>Imported {$imported} pages in %.2f seconds.</b>", $exec_time);
     $this->showLog();
 }
 /**
  * ----------------------------------------------------------------------------------- 
  * Overload the processAll method of the CsvBulkLoader class.                          
  * Used so that the session vars can be cleared once they are no longer needed.        
  *                                                                                     
  * @return BulkLoader_Result See {@link self::processAll()}                            
  * ----------------------------------------------------------------------------------- 
  */
 protected function processAll($filepath, $preview = false)
 {
     $results = new BulkLoader_Result();
     // NOTE: CSVParser BUGFIX should be present in the remapHeader method to allow spaces before/after headings in a CSV file.
     $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure);
     // ColumnMap has two uses, depending on whether hasHeaderRow is set
     if ($this->columnMap) {
         if ($this->hasHeaderRow) {
             $csv->mapColumns($this->columnMap);
         } else {
             $csv->provideHeaderRow($this->columnMap);
         }
     }
     foreach ($csv as $row) {
         $this->processRecord($row, $this->columnMap, $results, $preview);
     }
     // Clear the session variables we set in the load method
     Session::clear('MenuPageID');
     Session::clear('SectionImageIDs');
     // Return the process results
     //die();
     return $results;
 }
 private function handlePOST_importOutlookCSV($request_data)
 {
     //    echo "<pre>" . print_r($request_data, 1) . "</pre>";
     //    echo "<pre>" . print_r($_FILES, 1) . "</pre>";
     $error = '';
     $html = '';
     if (!empty($_FILES['outlook_csv']['name']) && is_uploaded_file($_FILES['outlook_csv']['tmp_name'])) {
         $ext = strtolower(end(explode('.', $_FILES['outlook_csv']['name'])));
         if (!in_array($_FILES['outlook_csv']['type'], $this->valid_cvs_mime_types) || $ext != 'csv') {
             //            $html = htmlspecialchars("<pre>" . print_r($_FILES, 1) . "</pre>");
             $error = __('Invalid file type. Please select a valid Outlook CSV file. ');
         } else {
             try {
                 $content = file_get_contents($_FILES['outlook_csv']['tmp_name']);
                 $csv_parser = new CSVParser($content);
                 $contacts = $csv_parser->getCSVContacts('CSVDataMapper', true);
                 if (!$contacts) {
                     $error = $csv_parser->lastError;
                 }
                 $this->deleteWithoutEmail($contacts, $csv_parser->mapped_contacts);
                 $templ = PA::$blockmodule_path . '/' . get_class($this) . "/outlook_list.tpl";
                 $html_gen = new Template($templ);
                 $html_gen->set('contacts', $contacts);
                 $html_gen->set('mapped_contacts', $csv_parser->mapped_contacts);
                 $html = $html_gen->fetch();
             } catch (Exception $e) {
                 $error = $e->getMessage();
             }
         }
     } else {
         $error = __('Please, select a valid Outlook CSV file.');
     }
     /*
           echo "Error: $error <br />";
           echo "Contacts: <pre>" . print_r($contacts, 1) . "</pre>";
           echo "Mapped Contacts: <pre>" . print_r($csv_parser->mapped_contacts, 1) . "</pre>";
     */
     echo "{";
     echo "error: '" . $error . "',\n";
     echo "content: '" . base64_encode(htmlspecialchars($html)) . "'\n";
     echo "}";
     exit;
 }
Exemple #13
0
 public function setRecipients($a)
 {
     $parser = new CSVParser($a);
     $this->recipients = $parser->parse();
 }
Exemple #14
0
 public function testDict()
 {
     $parser = new CSVParser('dict.csv');
     $dict = [['col1' => 'a', 'col2' => 'b', 'col3' => 'c']];
     $this->assertEquals($dict, $parser->toDictionary());
 }
        fclose($this->fp);
    }
    function getFlotrData($lines_count = 1000000)
    {
        $data = array();
        $ticks = array();
        $this->nextLine();
        $x = 0;
        while (($line = $this->nextLine()) && $lines_count--) {
            $d = array($x);
            $i = 1;
            while ($i < 5 && ($value = $line[$i++])) {
                $d[] = floatval($value);
            }
            $data[] = $d;
            $ticks[] = array($x, $line[0]);
            $x++;
        }
        return array('ticks' => $ticks, 'data' => $data);
    }
    function reset()
    {
        rewind($this->fp);
    }
    function nextLine()
    {
        return fgetcsv($this->fp, null, $this->delimiter, $this->enclosure);
    }
}
$parser = new CSVParser('http://ichart.finance.yahoo.com/table.csv?s=AAPL&a=00&b=1&c=1999&d=11&e=31&f=2020&g=m&ignore=.csv');
echo json_encode($parser->getFlotrData(30));
Exemple #16
0
 //////////////////////////////////////////////////
 //
 // Parse first line, check attributes, analyse the request
 //
 if ($sCharSet == 'UTF-8') {
     // Remove the BOM if any
     if (substr($sCSVData, 0, 3) == UTF8_BOM) {
         $sCSVData = substr($sCSVData, 3);
     }
     // Clean the input
     // Todo: warn the user if some characters are lost/substituted
     $sUTF8Data = iconv('UTF-8', 'UTF-8//IGNORE//TRANSLIT', $sCSVData);
 } else {
     $sUTF8Data = iconv($sCharSet, 'UTF-8//IGNORE//TRANSLIT', $sCSVData);
 }
 $oCSVParser = new CSVParser($sUTF8Data, $sSep, $sQualifier);
 // Limitation: as the attribute list is in the first line, we can not match external key by a third-party attribute
 $aRawFieldList = $oCSVParser->ListFields();
 $iColCount = count($aRawFieldList);
 // Translate into internal names
 $aFieldList = array();
 foreach ($aRawFieldList as $iFieldId => $sFieldName) {
     $sFieldName = trim($sFieldName);
     $aMatches = array();
     if (preg_match('/^(.+)\\*$/', $sFieldName, $aMatches)) {
         // Ignore any trailing "star" (*) that simply indicates a mandatory field
         $sFieldName = $aMatches[1];
     } else {
         if (preg_match('/^(.+)\\*->(.+)$/', $sFieldName, $aMatches)) {
             // Remove any trailing "star" character before the arrow (->)
             // A star character at the end can be used to indicate a mandatory field
 protected function DoExecute()
 {
     $sLogin = '******' . time();
     $oParser = new CSVParser("login,contactid->name,password,profile_list\n\t\t_1_{$sLogin},Picasso,secret1,profileid:10;reason:service manager|profileid->name:Problem Manager;'reason:toto;problem manager'\n\t\t_2_{$sLogin},Picasso,secret2,\n\t\t", ',', '"');
     $aData = $oParser->ToArray(1, array('_login', '_contact_name', '_password', '_profiles'));
     self::DumpVariable($aData);
     $oUser = new UserLocal();
     $oUser->Set('login', 'patator');
     $oUser->Set('password', 'patator');
     //$oUser->Set('contactid', 0);
     //$oUser->Set('language', $sLanguage);
     $aProfiles = array(array('profileid' => 10, 'reason' => 'service manager'), array('profileid->name' => 'Problem Manager', 'reason' => 'problem manager'));
     $oBulk = new BulkChange('UserLocal', $aData, array('login' => '_login', 'password' => '_password', 'profile_list' => '_profiles'), array('contactid' => array('name' => '_contact_name')), array('login'), "SELECT UserLocal", array('password' => 'terminated', 'login' => 'terminated' . time()));
     if (false) {
         $oMyChange = MetaModel::NewObject("CMDBChange");
         $oMyChange->Set("date", time());
         $oMyChange->Set("userinfo", "Testor");
         $iChangeId = $oMyChange->DBInsert();
         //		echo "Created new change: $iChangeId</br>";
     }
     echo "<h3>Planned for loading...</h3>";
     $aRes = $oBulk->Process();
     self::DumpVariable($aRes);
     if (false) {
         echo "<h3>Go for loading...</h3>";
         $aRes = $oBulk->Process($oMyChange);
         self::DumpVariable($aRes);
     }
     return;
 }
 public function parser() : CSVParser
 {
     return CSVParser::create($this);
 }
Exemple #19
0
    /**
     * Process the CSV data, for real or as a simulation
     * @param WebPage $oPage The page used to display the wizard
     * @param bool $bSimulate Whether or not to simulate the data load
     * @return array The CSV lines in error that were rejected from the load (with the header line - if any) or null
     */
    function ProcessCSVData(WebPage $oPage, $bSimulate = true)
    {
        $aResult = array();
        $sCSVData = utils::ReadParam('csvdata', '', false, 'raw_data');
        $sCSVDataTruncated = utils::ReadParam('csvdata_truncated', '', false, 'raw_data');
        $sSeparator = utils::ReadParam('separator', ',', false, 'raw_data');
        $sTextQualifier = utils::ReadParam('text_qualifier', '"', false, 'raw_data');
        $bHeaderLine = utils::ReadParam('header_line', '0') == 1;
        $iSkippedLines = 0;
        if (utils::ReadParam('box_skiplines', '0') == 1) {
            $iSkippedLines = utils::ReadParam('nb_skipped_lines', '0');
        }
        $sClassName = utils::ReadParam('class_name', '', false, 'class');
        $aFieldsMapping = utils::ReadParam('field', array(), false, 'raw_data');
        $aSearchFields = utils::ReadParam('search_field', array(), false, 'field_name');
        $iCurrentStep = $bSimulate ? 4 : 5;
        $bAdvanced = utils::ReadParam('advanced', 0);
        $sEncoding = utils::ReadParam('encoding', 'UTF-8');
        $sSynchroScope = utils::ReadParam('synchro_scope', '', false, 'raw_data');
        if (!empty($sSynchroScope)) {
            $oSearch = DBObjectSearch::FromOQL($sSynchroScope);
            $sClassName = $oSearch->GetClass();
            // If a synchronization scope is set, then the class is fixed !
            $oSet = new DBObjectSet($oSearch);
            $iCount = $oSet->Count();
            DisplaySynchroBanner($oPage, $sClassName, $iCount);
            $sClassesSelect = "<select id=\"select_class_name\" name=\"class_name\"><option value=\"{$sClassName}\" selected>" . MetaModel::GetName($sClassName) . "</option>";
            $aSynchroUpdate = utils::ReadParam('synchro_update', array());
        } else {
            $sSynchroScope = '';
            $aSynchroUpdate = null;
        }
        // Parse the data set
        $oCSVParser = new CSVParser($sCSVData, $sSeparator, $sTextQualifier);
        $aData = $oCSVParser->ToArray($iSkippedLines);
        $iRealSkippedLines = $iSkippedLines;
        if ($bHeaderLine) {
            $aResult[] = $sTextQualifier . implode($sTextQualifier . $sSeparator . $sTextQualifier, array_shift($aData)) . $sTextQualifier;
            // Remove the first line and store it in case of error
            $iRealSkippedLines++;
        }
        // Format for the line numbers
        $sMaxLen = strlen('' . count($aData)) < 3 ? 3 : strlen('' . count($aData));
        // Pad line numbers to the appropriate number of chars, but at least 3
        // Compute the list of search/reconciliation criteria
        $aSearchKeys = array();
        foreach ($aSearchFields as $index => $sDummy) {
            $sSearchField = $aFieldsMapping[$index];
            $aMatches = array();
            if (preg_match('/(.+)->(.+)/', $sSearchField, $aMatches) > 0) {
                $sSearchField = $aMatches[1];
                $aSearchKeys[$aMatches[1]] = '';
            } else {
                $aSearchKeys[$sSearchField] = '';
            }
            if (!MetaModel::IsValidFilterCode($sClassName, $sSearchField)) {
                // Remove invalid or unmapped search fields
                $aSearchFields[$index] = null;
                unset($aSearchKeys[$sSearchField]);
            }
        }
        // Compute the list of fields and external keys to process
        $aExtKeys = array();
        $aAttributes = array();
        $aExternalKeysByColumn = array();
        foreach ($aFieldsMapping as $iNumber => $sAttCode) {
            $iIndex = $iNumber - 1;
            if (!empty($sAttCode) && $sAttCode != ':none:' && $sAttCode != 'finalclass') {
                if (preg_match('/(.+)->(.+)/', $sAttCode, $aMatches) > 0) {
                    $sAttribute = $aMatches[1];
                    $sField = $aMatches[2];
                    $aExtKeys[$sAttribute][$sField] = $iIndex;
                    $aExternalKeysByColumn[$iIndex] = $sAttribute;
                } else {
                    if ($sAttCode == 'id') {
                        $aAttributes['id'] = $iIndex;
                    } else {
                        $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
                        if ($oAttDef->IsExternalKey()) {
                            $aExtKeys[$sAttCode]['id'] = $iIndex;
                            $aExternalKeysByColumn[$iIndex] = $sAttCode;
                        } else {
                            $aAttributes[$sAttCode] = $iIndex;
                        }
                    }
                }
            }
        }
        $oMyChange = null;
        if (!$bSimulate) {
            // We're doing it for real, let's create a change
            $sUserString = CMDBChange::GetCurrentUserName() . ' (CSV)';
            CMDBObject::SetTrackInfo($sUserString);
            CMDBObject::SetTrackOrigin('csv-interactive');
            $oMyChange = CMDBObject::GetCurrentChange();
        }
        $oBulk = new BulkChange($sClassName, $aData, $aAttributes, $aExtKeys, array_keys($aSearchKeys), empty($sSynchroScope) ? null : $sSynchroScope, $aSynchroUpdate, null, true);
        $oBulk->SetReportHtml();
        $oPage->add('<input type="hidden" name="csvdata_truncated" id="csvdata_truncated" value="' . htmlentities($sCSVDataTruncated, ENT_QUOTES, 'UTF-8') . '"/>');
        $aRes = $oBulk->Process($oMyChange);
        $sHtml = '<table id="bulk_preview" style="border-collapse: collapse;">';
        $sHtml .= '<tr><th style="padding:2px;border-right: 2px #fff solid;">Line</th>';
        $sHtml .= '<th style="padding:2px;border-right: 2px #fff solid;">Status</th>';
        $sHtml .= '<th style="padding:2px;border-right: 2px #fff solid;">Object</th>';
        foreach ($aFieldsMapping as $iNumber => $sAttCode) {
            if (!empty($sAttCode) && $sAttCode != ':none:' && $sAttCode != 'finalclass') {
                $sHtml .= "<th style=\"padding:2px;border-right: 2px #fff solid;\">" . MetaModel::GetLabel($sClassName, $sAttCode) . "</th>";
            }
        }
        $sHtml .= '<th>Message</th>';
        $sHtml .= '</tr>';
        $iErrors = 0;
        $iCreated = 0;
        $iModified = 0;
        $iUnchanged = 0;
        foreach ($aRes as $iLine => $aResRow) {
            $oStatus = $aResRow['__STATUS__'];
            $sUrl = '';
            $sMessage = '';
            $sCSSRowClass = '';
            $sCSSMessageClass = 'cell_ok';
            switch (get_class($oStatus)) {
                case 'RowStatus_NoChange':
                    $iUnchanged++;
                    $sFinalClass = $aResRow['finalclass'];
                    $oObj = MetaModel::GetObject($sFinalClass, $aResRow['id']->GetPureValue());
                    $sUrl = $oObj->GetHyperlink();
                    $sStatus = '<img src="../images/unchanged.png" title="' . Dict::S('UI:CSVReport-Icon-Unchanged') . '">';
                    $sCSSRowClass = 'row_unchanged';
                    break;
                case 'RowStatus_Modify':
                    $iModified++;
                    $sFinalClass = $aResRow['finalclass'];
                    $oObj = MetaModel::GetObject($sFinalClass, $aResRow['id']->GetPureValue());
                    $sUrl = $oObj->GetHyperlink();
                    $sStatus = '<img src="../images/modified.png" title="' . Dict::S('UI:CSVReport-Icon-Modified') . '">';
                    $sCSSRowClass = 'row_modified';
                    break;
                case 'RowStatus_Disappeared':
                    $iModified++;
                    $sFinalClass = $aResRow['finalclass'];
                    $oObj = MetaModel::GetObject($sFinalClass, $aResRow['id']->GetPureValue());
                    $sUrl = $oObj->GetHyperlink();
                    $sStatus = '<img src="../images/delete.png" title="' . Dict::S('UI:CSVReport-Icon-Missing') . '">';
                    $sCSSRowClass = 'row_modified';
                    if ($bSimulate) {
                        $sMessage = Dict::S('UI:CSVReport-Object-MissingToUpdate');
                    } else {
                        $sMessage = Dict::S('UI:CSVReport-Object-MissingUpdated');
                    }
                    break;
                case 'RowStatus_NewObj':
                    $iCreated++;
                    $sFinalClass = $aResRow['finalclass'];
                    $sStatus = '<img src="../images/added.png" title="' . Dict::S('UI:CSVReport-Icon-Created') . '">';
                    $sCSSRowClass = 'row_added';
                    if ($bSimulate) {
                        $sMessage = Dict::S('UI:CSVReport-Object-ToCreate');
                    } else {
                        $sFinalClass = $aResRow['finalclass'];
                        $oObj = MetaModel::GetObject($sFinalClass, $aResRow['id']->GetPureValue());
                        $sUrl = $oObj->GetHyperlink();
                        $sMessage = Dict::S('UI:CSVReport-Object-Created');
                    }
                    break;
                case 'RowStatus_Issue':
                    $iErrors++;
                    $sMessage .= $oPage->GetP($oStatus->GetDescription());
                    $sStatus = '<img src="../images/error.png" title="' . Dict::S('UI:CSVReport-Icon-Error') . '">';
                    //translate
                    $sCSSMessageClass = 'cell_error';
                    $sCSSRowClass = 'row_error';
                    if (array_key_exists($iLine, $aData)) {
                        $aRow = $aData[$iLine];
                        $aResult[] = $sTextQualifier . implode($sTextQualifier . $sSeparator . $sTextQualifier, $aRow) . $sTextQualifier;
                        // Remove the first line and store it in case of error
                    }
                    break;
            }
            $sHtml .= '<tr class="' . $sCSSRowClass . '">';
            $sHtml .= "<td style=\"background-color:#f1f1f1;border-right:2px #fff solid;\">" . sprintf("%0{$sMaxLen}d", 1 + $iLine + $iRealSkippedLines) . "</td>";
            $sHtml .= "<td style=\"text-align:center;background-color:#f1f1f1;border-right:2px #fff solid;\">{$sStatus}</td>";
            $sHtml .= "<td style=\"text-align:center;background-color:#f1f1f1;\">{$sUrl}</td>";
            foreach ($aFieldsMapping as $iNumber => $sAttCode) {
                if (!empty($sAttCode) && $sAttCode != ':none:' && $sAttCode != 'finalclass') {
                    $oCellStatus = $aResRow[$iNumber - 1];
                    $sCellMessage = '';
                    if (isset($aExternalKeysByColumn[$iNumber - 1])) {
                        $sExtKeyName = $aExternalKeysByColumn[$iNumber - 1];
                        $oExtKeyCellStatus = $aResRow[$sExtKeyName];
                        switch (get_class($oExtKeyCellStatus)) {
                            case 'CellStatus_Issue':
                            case 'CellStatus_SearchIssue':
                            case 'CellStatus_NullIssue':
                                $sCellMessage .= $oPage->GetP($oExtKeyCellStatus->GetDescription());
                                break;
                            case 'CellStatus_Ambiguous':
                                $sCellMessage .= $oPage->GetP($oExtKeyCellStatus->GetDescription());
                                break;
                            default:
                                // Do nothing
                        }
                    }
                    $sHtmlValue = $oCellStatus->GetDisplayableValue();
                    switch (get_class($oCellStatus)) {
                        case 'CellStatus_Issue':
                            $sCellMessage .= $oPage->GetP($oCellStatus->GetDescription());
                            $sHtml .= '<td class="cell_error" style="border-right:1px #eee solid;">' . Dict::Format('UI:CSVReport-Object-Error', $sHtmlValue) . $sCellMessage . '</td>';
                            break;
                        case 'CellStatus_SearchIssue':
                            $sCellMessage .= $oPage->GetP($oCellStatus->GetDescription());
                            $sHtml .= '<td class="cell_error">ERROR: ' . $sHtmlValue . $sCellMessage . '</td>';
                            break;
                        case 'CellStatus_Ambiguous':
                            $sCellMessage .= $oPage->GetP($oCellStatus->GetDescription());
                            $sHtml .= '<td class="cell_error" style="border-right:1px #eee solid;">' . Dict::Format('UI:CSVReport-Object-Ambiguous', $sHtmlValue) . $sCellMessage . '</td>';
                            break;
                        case 'CellStatus_Modify':
                            $sHtml .= '<td class="cell_modified" style="border-right:1px #eee solid;"><b>' . $sHtmlValue . '</b></td>';
                            break;
                        default:
                            $sHtml .= '<td class="cell_ok" style="border-right:1px #eee solid;">' . $sHtmlValue . $sCellMessage . '</td>';
                    }
                }
            }
            $sHtml .= "<td class=\"{$sCSSMessageClass}\" style=\"background-color:#f1f1f1;\">{$sMessage}</td>";
            $sHtml .= '</tr>';
        }
        $iUnchanged = count($aRes) - $iErrors - $iModified - $iCreated;
        $sHtml .= '</table>';
        $oPage->add('<div class="wizContainer" style="width:auto;display:inline-block;">');
        $oPage->add('<form enctype="multipart/form-data" id="wizForm" method="post">');
        $oPage->add('<input type="hidden" name="step" value="' . ($iCurrentStep + 1) . '"/>');
        $oPage->add('<input type="hidden" name="separator" value="' . htmlentities($sSeparator, ENT_QUOTES, 'UTF-8') . '"/>');
        $oPage->add('<input type="hidden" name="text_qualifier" value="' . htmlentities($sTextQualifier, ENT_QUOTES, 'UTF-8') . '"/>');
        $oPage->add('<input type="hidden" name="header_line" value="' . $bHeaderLine . '"/>');
        $oPage->add('<input type="hidden" name="nb_skipped_lines" value="' . utils::ReadParam('nb_skipped_lines', '0') . '"/>');
        $oPage->add('<input type="hidden" name="box_skiplines" value="' . utils::ReadParam('box_skiplines', '0') . '"/>');
        $oPage->add('<input type="hidden" name="csvdata" value="' . htmlentities($sCSVData, ENT_QUOTES, 'UTF-8') . '"/>');
        $oPage->add('<input type="hidden" name="csvdata_truncated" value="' . htmlentities($sCSVDataTruncated, ENT_QUOTES, 'UTF-8') . '"/>');
        $oPage->add('<input type="hidden" name="class_name" value="' . $sClassName . '"/>');
        $oPage->add('<input type="hidden" name="advanced" value="' . $bAdvanced . '"/>');
        $oPage->add('<input type="hidden" name="encoding" value="' . $sEncoding . '"/>');
        $oPage->add('<input type="hidden" name="synchro_scope" value="' . $sSynchroScope . '"/>');
        if (!empty($sSynchroScope)) {
            foreach ($aSynchroUpdate as $sKey => $value) {
                $oPage->add('<input type="hidden" name="synchro_update[' . $sKey . ']" value="' . $value . '"/>');
            }
        }
        foreach ($aFieldsMapping as $iNumber => $sAttCode) {
            $oPage->add('<input type="hidden" name="field[' . $iNumber . ']" value="' . $sAttCode . '"/>');
        }
        foreach ($aSearchFields as $index => $sDummy) {
            $oPage->add('<input type="hidden" name="search_field[' . $index . ']" value="1"/>');
        }
        $aDisplayFilters = array();
        if ($bSimulate) {
            $aDisplayFilters['unchanged'] = Dict::S('UI:CSVImport:ObjectsWillStayUnchanged');
            $aDisplayFilters['modified'] = Dict::S('UI:CSVImport:ObjectsWillBeModified');
            $aDisplayFilters['added'] = Dict::S('UI:CSVImport:ObjectsWillBeAdded');
            $aDisplayFilters['errors'] = Dict::S('UI:CSVImport:ObjectsWillHaveErrors');
        } else {
            $aDisplayFilters['unchanged'] = Dict::S('UI:CSVImport:ObjectsRemainedUnchanged');
            $aDisplayFilters['modified'] = Dict::S('UI:CSVImport:ObjectsWereModified');
            $aDisplayFilters['added'] = Dict::S('UI:CSVImport:ObjectsWereAdded');
            $aDisplayFilters['errors'] = Dict::S('UI:CSVImport:ObjectsHadErrors');
        }
        $oPage->add('<p><input type="checkbox" checked id="show_unchanged" onClick="ToggleRows(\'row_unchanged\')"/>&nbsp;<img src="../images/unchanged.png">&nbsp;' . sprintf($aDisplayFilters['unchanged'], $iUnchanged) . '&nbsp&nbsp;');
        $oPage->add('<input type="checkbox" checked id="show_modified" onClick="ToggleRows(\'row_modified\')"/>&nbsp;<img src="../images/modified.png">&nbsp;' . sprintf($aDisplayFilters['modified'], $iModified) . '&nbsp&nbsp;');
        $oPage->add('<input type="checkbox" checked id="show_created" onClick="ToggleRows(\'row_added\')"/>&nbsp;<img src="../images/added.png">&nbsp;' . sprintf($aDisplayFilters['added'], $iCreated) . '&nbsp&nbsp;');
        $oPage->add('<input type="checkbox" checked id="show_errors" onClick="ToggleRows(\'row_error\')"/>&nbsp;<img src="../images/error.png">&nbsp;' . sprintf($aDisplayFilters['errors'], $iErrors) . '</p>');
        $oPage->add('<div class="white" style="display:inline-block">');
        $oPage->add($sHtml);
        $oPage->add('</div> <!-- end of preview -->');
        $oPage->add('<p>');
        if ($bSimulate) {
            $oPage->add('<input type="button" value="' . Dict::S('UI:Button:Restart') . '" onClick="CSVRestart()"/>&nbsp;&nbsp;');
        }
        $oPage->add('<input type="button" value="' . Dict::S('UI:Button:Back') . '" onClick="CSVGoBack()"/>&nbsp;&nbsp;');
        $bShouldConfirm = false;
        if ($bSimulate) {
            // if there are *too many* changes, we should ask the user for a confirmation
            if (count($aRes) >= MetaModel::GetConfig()->Get('csv_import_min_object_confirmation')) {
                $fErrorsPercentage = 100.0 * $iErrors / count($aRes);
                if ($fErrorsPercentage >= MetaModel::GetConfig()->Get('csv_import_errors_percentage')) {
                    $sMessage = Dict::Format('UI:CSVReport-Stats-Errors', $fErrorsPercentage);
                    $bShouldConfirm = true;
                }
                $fCreatedPercentage = 100.0 * $iCreated / count($aRes);
                if ($fCreatedPercentage >= MetaModel::GetConfig()->Get('csv_import_creations_percentage')) {
                    $sMessage = Dict::Format('UI:CSVReport-Stats-Created', $fCreatedPercentage);
                    $bShouldConfirm = true;
                }
                $fModifiedPercentage = 100.0 * $iModified / count($aRes);
                if ($fModifiedPercentage >= MetaModel::GetConfig()->Get('csv_import_modifications_percentage')) {
                    $sMessage = Dict::Format('UI:CSVReport-Stats-Modified', $fModifiedPercentage);
                    $bShouldConfirm = true;
                }
            }
            $iCount = count($aRes);
            //$oPage->add('<input type="submit" value="'.Dict::S('UI:Button:DoImport').'" onClick="$(\'#wizForm\').block();"/></p>');
            $sConfirm = $bShouldConfirm ? 'true' : 'false';
            $oPage->add('<input type="button" value="' . Dict::S('UI:Button:DoImport') . "\" onClick=\"return DoSubmit({$sConfirm});\"/></p>");
        } else {
            $oPage->add('<input type="submit" value="' . Dict::S('UI:Button:Done') . '"/></p>');
        }
        $oPage->add('</form>');
        $oPage->add('</div> <!-- end of wizForm -->');
        if ($bShouldConfirm) {
            $sYesButton = Dict::S('UI:Button:Ok');
            $sNoButton = Dict::S('UI:Button:Cancel');
            $oPage->add('<div id="dlg_confirmation" title="' . htmlentities(Dict::S('UI:CSVImportConfirmTitle'), ENT_QUOTES, 'UTF-8') . '">');
            $oPage->add('<p style="text-align:center"><b>' . $sMessage . '</b></p>');
            $oPage->add('<p style="text-align:center">' . htmlentities(Dict::S('UI:CSVImportConfirmMessage'), ENT_QUOTES, 'UTF-8') . '</p>');
            $oPage->add('<div id="confirmation_chart"></div>');
            $oPage->add('</div> <!-- end of dlg_confirmation -->');
            $oPage->add_ready_script(<<<EOF
\t\$('#dlg_confirmation').dialog( 
\t\t{
\t\t\theight: 'auto',
\t\t\twidth: 500,
\t\t\tmodal:true, 
\t\t\tautoOpen: false, 
\t\t\tbuttons:
\t\t\t{
\t\t\t\t'{$sYesButton}': RunImport,
\t\t\t\t'{$sNoButton}': CancelImport 
\t\t\t} 
\t\t});
\t\tswfobject.embedSWF(\t"../images/open-flash-chart.swf", 
\t\t\t\t\t\t\t"confirmation_chart", 
\t\t\t\t\t\t\t"100%", "300","9.0.0",
\t\t\t\t\t\t\t"expressInstall.swf",
\t\t\t\t\t\t\t{}, 
\t\t\t\t\t\t\t{'wmode': 'transparent'}
\t\t\t\t\t\t);
EOF
);
        }
        $sErrors = addslashes(Dict::Format('UI:CSVImportError_items', $iErrors));
        $sCreated = addslashes(Dict::Format('UI:CSVImportCreated_items', $iCreated));
        $sModified = addslashes(Dict::Format('UI:CSVImportModified_items', $iModified));
        $sUnchanged = addslashes(Dict::Format('UI:CSVImportUnchanged_items', $iUnchanged));
        $oPage->add_script(<<<EOF
function CSVGoBack()
{
\t\$('input[name=step]').val({$iCurrentStep}-1);
\t\$('#wizForm').submit();
\t
}

function CSVRestart()
{
\t\$('input[name=step]').val(1);
\t\$('#wizForm').submit();
\t
}

function ToggleRows(sCSSClass)
{
\t\$('.'+sCSSClass).toggle();
}

function DoSubmit(bConfirm)
{
\tif (bConfirm) //Ask for a confirmation
\t{
\t\t\$('#dlg_confirmation').dialog('open');
\t}
\telse
\t{
\t\t// Submit the form
\t\t\$('#wizForm').block();
\t\t\$('#wizForm').submit();
\t}
\treturn false;
}

function CancelImport()
{
\t\$('#dlg_confirmation').dialog('close');
}

function RunImport()
{
\t\$('#dlg_confirmation').dialog('close');
\t// Submit the form
\t\$('#wizForm').block();
\t\$('#wizForm').submit();
}

function open_flash_chart_data()
{
\tvar iErrors = {$iErrors};
\tvar iModified = {$iModified};
\tvar iCreated = {$iCreated};
\tvar iUnchanged = {$iUnchanged};
\tvar fAlpha = 0.9;
\t
\tvar oResult = {
\t\t"elements": [
\t\t\t{
\t\t\t\t"type": "pie",
\t\t\t\t"tip": "#label# (#percent#)",
\t\t\t\t"gradient-fill": true,
\t\t\t\t"font-size": 14,
\t\t\t\t"colours":[],
\t\t\t\t"values": [],
\t\t\t\t"animate":[
\t\t\t        {
\t\t\t          "type": "fade"
\t\t\t        }
\t\t        ]
\t\t\t}
\t\t],
\t\t"x_axis": null,
\t\t"font-size": 14,
\t\t"bg_colour": "#EEEEEE"
\t};

\tif (iErrors > 0)
\t{
\t\tvar oErrors =
\t\t{
\t\t\t"value":  iErrors,
\t\t\t"label": "{$sErrors}",
\t\t\t"alpha": fAlpha,
\t\t\t"label-colour": "#CC3333",
\t\t};
\t\toResult.elements[0].values.push(oErrors);
\t\toResult.elements[0].colours.push('#FF6666');
\t}
\tif (iModified > 0)
\t{
\t\tvar oModified =
\t\t{
\t\t\t"value":  iModified,
\t\t\t"label": "{$sModified}",
\t\t\t"alpha": fAlpha,
\t\t\t"label-colour": "#3333CC",
\t\t};
\t\toResult.elements[0].values.push(oModified);
\t\toResult.elements[0].colours.push('#6666FF');
\t}
\tif (iCreated > 0)
\t{
\t\tvar oCreated =
\t\t{
\t\t\t"value":  iCreated,
\t\t\t"label": "{$sCreated}",
\t\t\t"alpha": fAlpha,
\t\t\t"label-colour": "#33CC33",
\t\t\t
\t\t};
\t\toResult.elements[0].values.push(oCreated);
\t\toResult.elements[0].colours.push('#66FF66');
\t}
\tif (iUnchanged > 0)
\t{
\t\tvar oUnchanged =
\t\t{
\t\t\t"value":  iUnchanged,
\t\t\t"label": "{$sUnchanged}",
\t\t\t"alpha": fAlpha,
\t\t\t"label-colour": "#333333",
\t\t\t
\t\t};
\t\toResult.elements[0].values.push(oUnchanged);
\t\toResult.elements[0].colours.push('#666666');
\t}

\treturn JSON.stringify(oResult);
}
EOF
);
        if ($iErrors > 0) {
            return $aResult;
        } else {
            return null;
        }
    }
Exemple #20
0
/**
 * Parse CSV files into a two dimensional array.
 * (Convenience function for CSVParser::parse().)
 *
 * @param Iterator $lineIterator
 *   An Iterator object that yields line strings, e.g. CSVFileLineIterator.
 * @return
 *   Two dimensional array that contains the data in the CSV file.
 */
function csv_parse(Iterator $lineIterator, $delimiter = ',', $skipFirstLine = FALSE)
{
    $parser = new CSVParser();
    $parser->setDelimiter($delimiter);
    $parser->setSkipFirstLine($skipFirstLine);
    return $parser->parse($lineIterator);
}
<?php

require_once 'classes/CSVParser.php';
$csv = new CSVParser('cliente.csv', ';');
$csv->parse();
while ($row = $csv->fetch()) {
    print $row['Cliente'] . ' - ';
    print $row['Cidade'] . "<br>\n";
}
 /**
  * Process every record in the file
  *
  * @param string  $filepath Absolute path to the file we're importing (with UTF8 content)
  * @param boolean $preview  If true, we'll just output a summary of changes but not actually do anything
  *
  * @return int
  *
  * @author Sascha Koehler <*****@*****.**>
  * @since 16.08.2011
  */
 public function processAll($filepath, $preview = false)
 {
     $pluginResult = SilvercartPlugin::call($this, 'overwriteProcessAll', array($filepath, $preview), false, 'DataObject');
     if ($pluginResult) {
         return $pluginResult;
     }
     $results = new BulkLoader_Result();
     $result = 0;
     $currPointer = 0;
     $csvParser = new CSVParser($filepath, $this->delimiter, $this->enclosure);
     $this->Log('product import start ---------------------------------------------------------------------');
     // --------------------------------------------------------------------
     // Insert header row if configured so
     // --------------------------------------------------------------------
     if ($this->columnMap) {
         if ($this->hasHeaderRow) {
             $csvParser->mapColumns($this->columnMap);
         } else {
             $csvParser->provideHeaderRow($this->columnMap);
         }
     }
     // --------------------------------------------------------------------
     // Process data range
     // --------------------------------------------------------------------
     foreach ($csvParser as $row) {
         $status = $this->processRecord($row, $this->columnMap, $results, $preview);
         if ($status) {
             $results->addCreated($status);
         }
         $currPointer++;
         usleep(1000);
     }
     $this->Log('product import end ---------------------------------------------------------------------');
     return $results;
 }
Exemple #23
0
<?php

include_once "Source/constant.php";
include_once "Source/CSVParser.php";
include_once "Source/Controllers/UserController.php";
include_once "Source/Models/DbUser.php";
include_once "Source/Models/Logger.php";
include_once "Source/Database/Database.php";
include_once "Source/Factories/DatabaseFactory.php";
$csvParser = new CSVParser();
$logger = new Logger();
$fileUri = "db.csv";
$loggerUri = "Log/Log";
// functie maken die fout in bestand set write2errorfile(dataArray)
// controler vast tel nummer toevoegen
$csvParser->setFileUri($fileUri);
// Kijkt dat file bestaat
if ($csvParser->checkIfFileExist()) {
    // Lees regel CSV bestand
    $row = 0;
    $dataArray = [];
    if ($csvParser->setHandle($csvParser->openFile("r")) !== FALSE) {
        // Controleert of data is gevonden
        while (($data = $csvParser->lineCSV($csvParser->getHandle())) !== FALSE) {
            if ($row == 0) {
                $row++;
                continue;
            }
            $num = count($data);
            // Zet regel op het scherm
            echo "<p> {$num} velden in lijn {$row}: <br /></p>\n";