csvimport.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * CSV Import Page
  18. * Wizard to import CSV (or TSV) data into the database
  19. *
  20. * @author Erwan Taloc <erwan.taloc@combodo.com>
  21. * @author Romain Quetiez <romain.quetiez@combodo.com>
  22. * @author Denis Flaven <denis.flaven@combodo.com>
  23. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  24. */
  25. ini_set('memory_limit', '256M');
  26. require_once('../application/application.inc.php');
  27. require_once('../application/itopwebpage.class.inc.php');
  28. require_once('../application/startup.inc.php');
  29. require_once('../application/loginwebpage.class.inc.php');
  30. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  31. $oAppContext = new ApplicationContext();
  32. $currentOrganization = utils::ReadParam('org_id', 1);
  33. $iStep = utils::ReadParam('step', 1);
  34. $oPage = new iTopWebPage(Dict::S('UI:Title:BulkImport'), $currentOrganization);
  35. /**
  36. * Helper function to build a select from the list of valid classes for a given action
  37. * @param string $sName The name of the select in the HTML form
  38. * @param string $sDefaulfValue The defaut value (i.e the value selected by default)
  39. * @param integer $iWidthPx The width (in pixels) of the drop-down list
  40. * @param integer $iActionCode The ActionCode (from UserRights) to check for authorization for the classes
  41. * @return string The HTML fragment corresponding to the select tag
  42. */
  43. function GetClassesSelect($sName, $sDefaultValue, $iWidthPx, $iActionCode = null)
  44. {
  45. $sHtml = "<select id=\"select_$sName\" name=\"$sName\">";
  46. $sHtml .= "<option tyle=\"width: ".$iWidthPx."px;\" title=\"Select the class you want to load\" value=\"\">".Dict::S('UI:CSVImport:ClassesSelectOne')."</option>\n";
  47. $aValidClasses = array();
  48. $aClassCategories = array('bizmodel');
  49. if (UserRights::IsAdministrator())
  50. {
  51. $aClassCategories = array('bizmodel', 'application', 'addon/authentication');
  52. }
  53. foreach($aClassCategories as $sClassCategory)
  54. {
  55. foreach(MetaModel::GetClasses($sClassCategory) as $sClassName)
  56. {
  57. if ( (is_null($iActionCode) || UserRights::IsActionAllowed($sClassName, $iActionCode)) &&
  58. (!MetaModel::IsAbstract($sClassName)) )
  59. {
  60. $sSelected = ($sClassName == $sDefaultValue) ? " selected" : "";
  61. $sDescription = MetaModel::GetClassDescription($sClassName);
  62. $sDisplayName = MetaModel::GetName($sClassName);
  63. $aValidClasses[$sDisplayName] = "<option style=\"width: ".$iWidthPx."px;\" title=\"$sDescription\" value=\"$sClassName\"$sSelected>$sDisplayName</option>";
  64. }
  65. }
  66. }
  67. ksort($aValidClasses);
  68. $sHtml .= implode("\n", $aValidClasses);
  69. $sHtml .= "</select>";
  70. return $sHtml;
  71. }
  72. /**
  73. * Helper to 'check' an input in an HTML form if the current value equals the value given
  74. * @param mixed $sCurrentValue The current value to be chacked against the value of the input
  75. * @param mixed $sProposedValue The value of the input
  76. * @param bool $bInverseCondition Set to true to perform the reversed comparison
  77. * @return string Either ' checked' or an empty string
  78. */
  79. function IsChecked($sCurrentValue, $sProposedValue, $bInverseCondition = false)
  80. {
  81. $bCondition = ($sCurrentValue == $sProposedValue);
  82. return ($bCondition xor $bInverseCondition) ? ' checked' : '';
  83. }
  84. /**
  85. * Get the user friendly name for an 'extended' attribute code i.e 'name', becomes 'Name' and 'org_id->name' becomes 'Organization->Name'
  86. * @param string $sClassName The name of the class
  87. * @param string $sAttCodeEx Either an attribute code of ext_key_name->att_code
  88. * @return string A user friendly format of the string: AttributeName or AttributeName->ExtAttributeName
  89. */
  90. function GetFriendlyAttCodeName($sClassName, $sAttCodeEx)
  91. {
  92. $sFriendlyName = '';
  93. if (preg_match('/(.+)->(.+)/', $sAttCodeEx, $aMatches) > 0)
  94. {
  95. $sAttribute = $aMatches[1];
  96. $sField = $aMatches[2];
  97. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttribute);
  98. if ($oAttDef->IsExternalKey())
  99. {
  100. $sTargetClass = $oAttDef->GetTargetClass();
  101. $oTargetAttDef = MetaModel::GetAttributeDef($sTargetClass, $sField);
  102. $sFriendlyName = $oAttDef->GetLabel().'->'.$oTargetAttDef->GetLabel();
  103. }
  104. else
  105. {
  106. // hum, hum... should never happen, we'd better raise an exception
  107. throw(new Exception(Dict::Format('UI:CSVImport:ErrorExtendedAttCode', $sAttCodeEx, $sAttribute, $sClassName)));
  108. }
  109. }
  110. else
  111. {
  112. if ($sAttCodeEx == 'id')
  113. {
  114. $sFriendlyName = Dict::S('UI:CSVImport:idField');
  115. }
  116. else
  117. {
  118. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCodeEx);
  119. $sFriendlyName = $oAttDef->GetLabel();
  120. }
  121. }
  122. return $sFriendlyName;
  123. }
  124. /**
  125. * Returns the number of occurences of each char from the set in the specified string
  126. * @param string $sString The input data
  127. * @param array $aSet The set of characters to count
  128. * @return hash 'char' => nb of occurences
  129. */
  130. function CountCharsFromSet($sString, $aSet)
  131. {
  132. $aResult = array();
  133. $aCount = count_chars($sString);
  134. foreach($aSet as $sChar)
  135. {
  136. $aResult[$sChar] = isset($aCount[ord($sChar)]) ? $aCount[ord($sChar)] : 0;
  137. }
  138. return $aResult;
  139. }
  140. /**
  141. * Return the most frequent (and regularly occuring) character among the given set, in the specified lines
  142. * @param array $aCSVData The input data, one entry per line
  143. * @param array $aPossibleSeparators The list of characters to count
  144. * @return string The most frequent character from the set
  145. */
  146. function GuessFromFrequency($aCSVData, $aPossibleSeparators)
  147. {
  148. $iLine = 0;
  149. $iMaxLine = 20; // Process max 20 lines to guess the parameters
  150. foreach($aPossibleSeparators as $sSep)
  151. {
  152. $aGuesses[$sSep]['total'] = $aGuesses[$sSep]['max'] = 0;
  153. $aGuesses[$sSep]['min'] = 999;
  154. }
  155. $aStats = array();
  156. while(($iLine < count($aCSVData)) && ($iLine < $iMaxLine) )
  157. {
  158. if (strlen($aCSVData[$iLine]) > 0)
  159. {
  160. $aStats[$iLine] = CountCharsFromSet($aCSVData[$iLine], $aPossibleSeparators);
  161. }
  162. $iLine++;
  163. }
  164. $iLine = 1;
  165. foreach($aStats as $aLineStats)
  166. {
  167. foreach($aPossibleSeparators as $sSep)
  168. {
  169. $aGuesses[$sSep]['total'] += $aLineStats[$sSep];
  170. if ($aLineStats[$sSep] > $aGuesses[$sSep]['max']) $aGuesses[$sSep]['max'] = $aLineStats[$sSep];
  171. if ($aLineStats[$sSep] < $aGuesses[$sSep]['min']) $aGuesses[$sSep]['min'] = $aLineStats[$sSep];
  172. }
  173. $iLine++;
  174. }
  175. $aScores = array();
  176. foreach($aGuesses as $sSep => $aData)
  177. {
  178. $aScores[$sSep] = $aData['total'] + $aData['max'] - $aData['min'];
  179. }
  180. arsort($aScores, SORT_NUMERIC); // Sort the array, higher scores first
  181. $aKeys = array_keys($aScores);
  182. $sSeparator = $aKeys[0]; // Take the first key, the one with the best score
  183. return $sSeparator;
  184. }
  185. /**
  186. * Try to predict the CSV parameters based on the input data
  187. * @param string $sCSVData The input data
  188. * @return hash 'separator' => the_guessed_separator, 'qualifier' => the_guessed_text_qualifier
  189. */
  190. function GuessParameters($sCSVData)
  191. {
  192. $aData = explode("\n", $sCSVData);
  193. $sSeparator = GuessFromFrequency($aData, array("\t", ',', ';', '|')); // Guess the most frequent (and regular) character on each line
  194. $sQualifier = GuessFromFrequency($aData, array('"', "'")); // Guess the most frequent (and regular) character on each line
  195. return array('separator' => $sSeparator, 'qualifier' => $sQualifier);
  196. }
  197. /**
  198. * Process the CSV data, for real or as a simulation
  199. * @param WebPage $oPage The page used to display the wizard
  200. * @param bool $bSimulate Whether or not to simulate the data load
  201. * @return array The CSV lines in error that were rejected from the load (with the header line - if any) or null
  202. */
  203. function ProcessCSVData(WebPage $oPage, $bSimulate = true)
  204. {
  205. $aResult = array();
  206. $sCSVData = utils::ReadParam('csvdata', '');
  207. $sCSVDataTruncated = utils::ReadParam('csvdata_truncated', '');
  208. $sSeparator = utils::ReadParam('separator', ',');
  209. $sTextQualifier = utils::ReadParam('text_qualifier', '"');
  210. $bHeaderLine = (utils::ReadParam('header_line', '0') == 1);
  211. $iRealSkippedLines = $iSkippedLines = utils::ReadParam('nb_skipped_lines', '0');
  212. $sClassName = utils::ReadParam('class_name', '');
  213. $aFieldsMapping = utils::ReadParam('field', array());
  214. $aSearchFields = utils::ReadParam('search_field', array());
  215. $iCurrentStep = $bSimulate ? 4 : 5;
  216. $bAdvanced = utils::ReadParam('advanced', 0);
  217. $sEncoding = utils::ReadParam('encoding', 'UTF-8');
  218. // Parse the data set
  219. $oCSVParser = new CSVParser($sCSVData, $sSeparator, $sTextQualifier);
  220. $aData = $oCSVParser->ToArray($iSkippedLines);
  221. if ($bHeaderLine)
  222. {
  223. $aResult[] = $sTextQualifier.implode($sTextQualifier.$sSeparator.$sTextQualifier, array_shift($aData)).$sTextQualifier; // Remove the first line and store it in case of error
  224. $iRealSkippedLines++;
  225. }
  226. // Format for the line numbers
  227. $sMaxLen = (strlen(''.count($aData)) < 3) ? 3 : strlen(''.count($aData)); // Pad line numbers to the appropriate number of chars, but at least 3
  228. // Compute the list of search/reconciliation criteria
  229. $aSearchKeys = array();
  230. foreach($aSearchFields as $index => $sDummy)
  231. {
  232. $sSearchField = $aFieldsMapping[$index];
  233. $aMatches = array();
  234. if (preg_match('/(.+)->(.+)/', $sSearchField, $aMatches) > 0)
  235. {
  236. $sSearchField = $aMatches[1];
  237. $aSearchKeys[$aMatches[1]] = '';
  238. }
  239. else
  240. {
  241. $aSearchKeys[$sSearchField] = '';
  242. }
  243. if (!MetaModel::IsValidFilterCode($sClassName, $sSearchField))
  244. {
  245. // Remove invalid or unmapped search fields
  246. $aSearchFields[$index] = null;
  247. unset($aSearchKeys[$sSearchField]);
  248. }
  249. }
  250. // Compute the list of fields and external keys to process
  251. $aExtKeys = array();
  252. $aAttributes = array();
  253. $aExternalKeysByColumn = array();
  254. foreach($aFieldsMapping as $iNumber => $sAttCode)
  255. {
  256. $iIndex = $iNumber-1;
  257. if (!empty($sAttCode) && ($sAttCode != ':none:') && ($sAttCode != 'finalclass'))
  258. {
  259. if (preg_match('/(.+)->(.+)/', $sAttCode, $aMatches) > 0)
  260. {
  261. $sAttribute = $aMatches[1];
  262. $sField = $aMatches[2];
  263. $aExtKeys[$sAttribute][$sField] = $iIndex;
  264. $aExternalKeysByColumn[$iIndex] = $sAttribute;
  265. }
  266. else
  267. {
  268. if ($sAttCode == 'id')
  269. {
  270. $aAttributes['id'] = $iIndex;
  271. }
  272. else
  273. {
  274. $oAttDef = MetaModel::GetAttributeDef($sClassName, $sAttCode);
  275. if ($oAttDef->IsExternalKey())
  276. {
  277. $aExtKeys[$sAttCode]['id'] = $iIndex;
  278. $aExternalKeysByColumn[$iIndex] = $sAttCode;
  279. }
  280. else
  281. {
  282. $aAttributes[$sAttCode] = $iIndex;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. $oMyChange = null;
  289. if (!$bSimulate)
  290. {
  291. // We're doing it for real, let's create a change
  292. $oMyChange = MetaModel::NewObject("CMDBChange");
  293. $oMyChange->Set("date", time());
  294. if (UserRights::IsImpersonated())
  295. {
  296. $sUserString = Dict::Format('UI:Archive_User_OnBehalfOf_User', UserRights::GetRealUser(), UserRights::GetUser());
  297. }
  298. else
  299. {
  300. $sUserString = UserRights::GetUser();
  301. }
  302. $oMyChange->Set("userinfo", $sUserString);
  303. $iChangeId = $oMyChange->DBInsert();
  304. }
  305. $oBulk = new BulkChange(
  306. $sClassName,
  307. $aData,
  308. $aAttributes,
  309. $aExtKeys,
  310. array_keys($aSearchKeys)
  311. );
  312. $oPage->add('<input type="hidden" name="csvdata_truncated" id="csvdata_truncated" value="'.htmlentities($sCSVDataTruncated, ENT_QUOTES, 'UTF-8').'"/>');
  313. $aRes = $oBulk->Process($oMyChange);
  314. $sHtml = '<table id="bulk_preview">';
  315. $sHtml .= '<tr><th>Line</th>';
  316. $sHtml .= '<th>Status</th>';
  317. $sHtml .= '<th>Object</th>';
  318. foreach($aFieldsMapping as $iNumber => $sAttCode)
  319. {
  320. if (!empty($sAttCode) && ($sAttCode != ':none:') && ($sAttCode != 'finalclass'))
  321. {
  322. $sHtml .= "<th>".GetFriendlyAttCodeName($sClassName, $sAttCode)."</th>";
  323. }
  324. }
  325. $sHtml .= '<th>Message</th>';
  326. $sHtml .= '</tr>';
  327. $iLine = 0;
  328. $iErrors = 0;
  329. $iCreated = 0;
  330. $iModified = 0;
  331. $iUnchanged = 0;
  332. foreach($aData as $aRow)
  333. {
  334. $oStatus = $aRes[$iLine]['__STATUS__'];
  335. $sUrl = '';
  336. $sMessage = '';
  337. $sCSSRowClass = '';
  338. $sCSSMessageClass = 'cell_ok';
  339. switch(get_class($oStatus))
  340. {
  341. case 'RowStatus_NoChange':
  342. $iUnchanged++;
  343. $sFinalClass = $aRes[$iLine]['finalclass'];
  344. $oObj = MetaModel::GetObject($sFinalClass, $aRes[$iLine]['id']->GetValue());
  345. $sUrl = $oObj->GetHyperlink();
  346. $sStatus = '<img src="../images/unchanged.png" title="Unchanged">';
  347. $sCSSRowClass = 'row_unchanged';
  348. break;
  349. case 'RowStatus_Modify':
  350. $iModified++;
  351. $sFinalClass = $aRes[$iLine]['finalclass'];
  352. $oObj = MetaModel::GetObject($sFinalClass, $aRes[$iLine]['id']->GetValue());
  353. $sUrl = $oObj->GetHyperlink();
  354. $sStatus = '<img src="../images/modified.png" title="Modified">';
  355. $sCSSRowClass = 'row_modified';
  356. break;
  357. case 'RowStatus_NewObj':
  358. $iCreated++;
  359. $sFinalClass = $aRes[$iLine]['finalclass'];
  360. $sStatus = '<img src="../images/added.png" title="Created">';
  361. $sCSSRowClass = 'row_added';
  362. if ($bSimulate)
  363. {
  364. $sMessage = 'Object will be created';
  365. }
  366. else
  367. {
  368. $sFinalClass = $aRes[$iLine]['finalclass'];
  369. $oObj = MetaModel::GetObject($sFinalClass, $aRes[$iLine]['id']->GetValue());
  370. $sUrl = $oObj->GetHyperlink();
  371. $sMessage = 'Object created';
  372. }
  373. break;
  374. case 'RowStatus_Issue':
  375. $iErrors++;
  376. $sMessage .= $oPage->GetP($oStatus->GetDescription());
  377. $sStatus = '<img src="../images/error.png" title="Error">';
  378. $sCSSMessageClass = 'cell_error';
  379. $sCSSRowClass = 'row_error';
  380. $aResult[] = $sTextQualifier.implode($sTextQualifier.$sSeparator.$sTextQualifier,$aRow).$sTextQualifier; // Remove the first line and store it in case of error
  381. break;
  382. }
  383. $sHtml .= '<tr class="'.$sCSSRowClass.'">';
  384. $sHtml .= "<td>".sprintf("%0{$sMaxLen}d", 1+$iLine+$iRealSkippedLines)."</td>";
  385. $sHtml .= "<td>$sStatus</td>";
  386. $sHtml .= "<td>$sUrl</td>";
  387. foreach($aFieldsMapping as $iNumber => $sAttCode)
  388. {
  389. if (!empty($sAttCode) && ($sAttCode != ':none:') && ($sAttCode != 'finalclass'))
  390. {
  391. $oCellStatus = $aRes[$iLine][$iNumber -1];
  392. $sCellMessage = '';
  393. if (isset($aExternalKeysByColumn[$iNumber -1]))
  394. {
  395. $sExtKeyName = $aExternalKeysByColumn[$iNumber -1];
  396. $oExtKeyCellStatus = $aRes[$iLine][$sExtKeyName];
  397. switch(get_class($oExtKeyCellStatus))
  398. {
  399. case 'CellStatus_Issue':
  400. $sCellMessage .= $oPage->GetP($oExtKeyCellStatus->GetDescription());
  401. break;
  402. case 'CellStatus_Ambiguous':
  403. $sCellMessage .= $oPage->GetP($oExtKeyCellStatus->GetDescription());
  404. break;
  405. default:
  406. // Do nothing
  407. }
  408. }
  409. switch(get_class($oCellStatus))
  410. {
  411. case 'CellStatus_Issue':
  412. $sCellMessage .= $oPage->GetP($oCellStatus->GetDescription());
  413. $sHtml .= '<td class="cell_error">ERROR: '.htmlentities($aData[$iLine][$iNumber-1], ENT_QUOTES, 'UTF-8').$sCellMessage.'</td>';
  414. break;
  415. case 'CellStatus_Ambiguous':
  416. $sCellMessage .= $oPage->GetP($oCellStatus->GetDescription());
  417. $sHtml .= '<td class="cell_error">AMBIGUOUS: '.htmlentities($aData[$iLine][$iNumber-1], ENT_QUOTES, 'UTF-8').$sCellMessage.'</td>';
  418. break;
  419. case 'CellStatus_Modify':
  420. $sHtml .= '<td class="cell_modified"><b>'.htmlentities($aData[$iLine][$iNumber-1], ENT_QUOTES, 'UTF-8').'</b></td>';
  421. break;
  422. default:
  423. $sHtml .= '<td class="cell_ok">'.htmlentities($aData[$iLine][$iNumber-1], ENT_QUOTES, 'UTF-8').$sCellMessage.'</td>';
  424. }
  425. }
  426. }
  427. $sHtml .= "<td class=\"$sCSSMessageClass\">$sMessage</td>";
  428. $iLine++;
  429. $sHtml .= '</tr>';
  430. }
  431. $sHtml .= '</table>';
  432. $oPage->add('<div class="wizContainer">');
  433. $oPage->add('<form enctype="multipart/form-data" id="wizForm" method="post" onSubmit="return CheckValues()">');
  434. $oPage->add('<input type="hidden" name="step" value="'.($iCurrentStep+1).'"/>');
  435. $oPage->add('<input type="hidden" name="separator" value="'.htmlentities($sSeparator, ENT_QUOTES, 'UTF-8').'"/>');
  436. $oPage->add('<input type="hidden" name="text_qualifier" value="'.htmlentities($sTextQualifier, ENT_QUOTES, 'UTF-8').'"/>');
  437. $oPage->add('<input type="hidden" name="header_line" value="'.$bHeaderLine.'"/>');
  438. $oPage->add('<input type="hidden" name="box_skiplines" value="'.(($iSkippedLines > 0) ? 1 : 0).'"/>');
  439. $oPage->add('<input type="hidden" name="nb_skipped_lines" value="'.$iSkippedLines.'"/>');
  440. $oPage->add('<input type="hidden" name="csvdata" value="'.htmlentities($sCSVData, ENT_QUOTES, 'UTF-8').'"/>');
  441. $oPage->add('<input type="hidden" name="csvdata_truncated" value="'.htmlentities($sCSVDataTruncated, ENT_QUOTES, 'UTF-8').'"/>');
  442. $oPage->add('<input type="hidden" name="class_name" value="'.$sClassName.'"/>');
  443. $oPage->add('<input type="hidden" name="advanced" value="'.$bAdvanced.'"/>');
  444. $oPage->add('<input type="hidden" name="encoding" value="'.$sEncoding.'"/>');
  445. foreach($aFieldsMapping as $iNumber => $sAttCode)
  446. {
  447. $oPage->add('<input type="hidden" name="field['.$iNumber.']" value="'.$sAttCode.'"/>');
  448. }
  449. foreach($aSearchFields as $index => $sDummy)
  450. {
  451. $oPage->add('<input type="hidden" name="search_field['.$index.']" value="1"/>');
  452. }
  453. $aFieldsMapping = utils::ReadParam('field', array());
  454. $aSearchFields = utils::ReadParam('search_field', array());
  455. $aDisplayFilters = array();
  456. if ($bSimulate)
  457. {
  458. $aDisplayFilters['unchanged'] = Dict::S('UI:CSVImport:ObjectsWillStayUnchanged');
  459. $aDisplayFilters['modified'] = Dict::S('UI:CSVImport:ObjectsWillBeModified');
  460. $aDisplayFilters['added'] = Dict::S('UI:CSVImport:ObjectsWillBeAdded');
  461. $aDisplayFilters['errors'] = Dict::S('UI:CSVImport:ObjectsWillHaveErrors');
  462. }
  463. else
  464. {
  465. $aDisplayFilters['unchanged'] = Dict::S('UI:CSVImport:ObjectsRemainedUnchanged');
  466. $aDisplayFilters['modified'] = Dict::S('UI:CSVImport:ObjectsWereModified');
  467. $aDisplayFilters['added'] = Dict::S('UI:CSVImport:ObjectsWereAdded');
  468. $aDisplayFilters['errors'] = Dict::S('UI:CSVImport:ObjectsHadErrors');
  469. }
  470. $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;');
  471. $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;');
  472. $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;');
  473. $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>');
  474. $oPage->add('<div style="overflow-y:auto" class="white">');
  475. $oPage->add($sHtml);
  476. $oPage->add('</div> <!-- end of preview -->');
  477. $oPage->add('<p><input type="button" value="'.Dict::S('UI:Button:Back').'" onClick="CSVGoBack()"/>&nbsp;&nbsp;');
  478. if ($bSimulate)
  479. {
  480. $oPage->add('<input type="submit" value="'.Dict::S('UI:Button:DoImport').'"/></p>');
  481. }
  482. else
  483. {
  484. $oPage->add('<input type="submit" value="'.Dict::S('UI:Button:Done').'"/></p>');
  485. }
  486. $oPage->add('</form>');
  487. $oPage->add('</div> <!-- end of wizForm -->');
  488. $oPage->add_script(
  489. <<< EOF
  490. function CSVGoBack()
  491. {
  492. $('input[name=step]').val($iCurrentStep-1);
  493. $('#wizForm').submit();
  494. }
  495. function ToggleRows(sCSSClass)
  496. {
  497. $('.'+sCSSClass).toggle();
  498. }
  499. EOF
  500. );
  501. if ($iErrors > 0)
  502. {
  503. return $aResult;
  504. }
  505. else
  506. {
  507. return null;
  508. }
  509. }
  510. /**
  511. * Perform the actual load of the CSV data and display the results
  512. * @param WebPage $oPage The web page to display the wizard
  513. * @return void
  514. */
  515. function LoadData(WebPage $oPage)
  516. {
  517. $oPage->add('<h2>'.Dict::S('UI:Title:CSVImportStep5').'</h2>');
  518. $aResult = ProcessCSVData($oPage, false /* simulate = false */);
  519. if (is_array($aResult))
  520. {
  521. $oPage->StartCollapsibleSection(Dict::S('UI:CSVImport:LinesNotImported'), false);
  522. $oPage->p(Dict::S('UI:CSVImport:LinesNotImported+'));
  523. $oPage->add('<textarea rows="30" cols="100">');
  524. $oPage->add(htmlentities(implode("\n", $aResult), ENT_QUOTES, 'UTF-8'));
  525. $oPage->add('</textarea>');
  526. $oPage->EndCollapsibleSection();
  527. }
  528. }
  529. /**
  530. * Simulate the load of the CSV data and display the results
  531. * @param WebPage $oPage The web page to display the wizard
  532. * @return void
  533. */
  534. function Preview(WebPage $oPage)
  535. {
  536. $oPage->add('<h2>'.Dict::S('UI:Title:CSVImportStep4').'</h2>');
  537. ProcessCSVData($oPage, true /* simulate */);
  538. }
  539. /**
  540. * Select the mapping between the CSV column and the fields of the objects
  541. * @param WebPage $oPage The web page to display the wizard
  542. * @return void
  543. */
  544. function SelectMapping(WebPage $oPage)
  545. {
  546. $sCSVData = utils::ReadParam('csvdata', '');
  547. $sCSVDataTruncated = utils::ReadParam('csvdata_truncated', '');;
  548. $sSeparator = utils::ReadParam('separator', ',');
  549. if ($sSeparator == 'tab') $sSeparator = "\t";
  550. if ($sSeparator == 'other')
  551. {
  552. $sSeparator = utils::ReadParam('other_separator', ',');
  553. }
  554. $sTextQualifier = utils::ReadParam('text_qualifier', '"');
  555. if ($sTextQualifier == 'other')
  556. {
  557. $sTextQualifier = utils::ReadParam('other_qualifier', '"');
  558. }
  559. $bHeaderLine = (utils::ReadParam('header_line', '0') == 1);
  560. $iSkippedLines = 0;
  561. if (utils::ReadParam('box_skiplines', '0') == 1)
  562. {
  563. $iSkippedLines = utils::ReadParam('nb_skipped_lines', '0');
  564. }
  565. $sClassName = utils::ReadParam('class_name', '');
  566. $bAdvanced = utils::ReadParam('advanced', 0);
  567. $sEncoding = utils::ReadParam('encoding', 'UTF-8');
  568. $oPage->add('<h2>'.Dict::S('UI:Title:CSVImportStep3').'</h2>');
  569. $oPage->add('<div class="wizContainer">');
  570. $oPage->add('<form enctype="multipart/form-data" id="wizForm" method="post" onSubmit="return CheckValues()"><table style="width:100%" class="transparent"><tr><td>'.Dict::S('UI:CSVImport:SelectClass').' ');
  571. $oPage->add(GetClassesSelect('class_name', $sClassName, 300, UR_ACTION_BULK_MODIFY));
  572. $oPage->add('</td><td style="text-align:right"><input type="checkbox" name="advanced" value="1" '.IsChecked($bAdvanced, 1).' onChange="DoMapping()">&nbsp;'.Dict::S('UI:CSVImport:AdvancedMode').'</td></tr></table>');
  573. $oPage->add('<div style="padding:1em;display:none" id="advanced_help" style="display:none">'.Dict::S('UI:CSVImport:AdvancedMode+').'</div>');
  574. $oPage->add('<div id="mapping" class="white"><p style="text-align:center;width:100%;font-size:1.5em;padding:1em;">'.Dict::S('UI:CSVImport:SelectAClassFirst').'<br/></p></div>');
  575. $oPage->add('<input type="hidden" name="step" value="4"/>');
  576. $oPage->add('<input type="hidden" name="separator" value="'.htmlentities($sSeparator, ENT_QUOTES, 'UTF-8').'"/>');
  577. $oPage->add('<input type="hidden" name="text_qualifier" value="'.htmlentities($sTextQualifier, ENT_QUOTES, 'UTF-8').'"/>');
  578. $oPage->add('<input type="hidden" name="header_line" value="'.$bHeaderLine.'"/>');
  579. $oPage->add('<input type="hidden" name="box_skiplines" value="'.(($iSkippedLines > 0) ? 1 : 0).'"/>');
  580. $oPage->add('<input type="hidden" name="nb_skipped_lines" value="'.$iSkippedLines.'"/>');
  581. $oPage->add('<input type="hidden" name="csvdata_truncated" id="csvdata_truncated" value="'.htmlentities($sCSVDataTruncated, ENT_QUOTES, 'UTF-8').'"/>');
  582. $oPage->add('<input type="hidden" name="csvdata" value="'.htmlentities($sCSVData, ENT_QUOTES, 'UTF-8').'"/>');
  583. $oPage->add('<input type="hidden" name="encoding" value="'.$sEncoding.'">');
  584. $oPage->add('<p><input type="button" value="'.Dict::S('UI:Button:Back').'" onClick="CSVGoBack()"/>&nbsp;&nbsp;');
  585. $oPage->add('<input type="submit" value="'.Dict::S('UI:Button:SimulateImport').'"/></p>');
  586. $oPage->add('</form>');
  587. $oPage->add('</div>');
  588. $sAlertIncompleteMapping = Dict::S('UI:CSVImport:AlertIncompleteMapping');
  589. $sAlertNoSearchCriteria = Dict::S('UI:CSVImport:AlertNoSearchCriteria');
  590. $oPage->add_ready_script(
  591. <<<EOF
  592. $('#select_class_name').change( DoMapping );
  593. EOF
  594. );
  595. if ($sClassName != '')
  596. {
  597. $oPage->add_ready_script("DoMapping();"); // There is already a class selected, run the mapping
  598. }
  599. $oPage->add_script(
  600. <<<EOF
  601. var aDefaultKeys = new Array();
  602. function CSVGoBack()
  603. {
  604. $('input[name=step]').val(2);
  605. $('#wizForm').submit();
  606. }
  607. var ajax_request = null;
  608. function DoMapping()
  609. {
  610. var class_name = $('select[name=class_name]').val();
  611. var advanced = $('input[name=advanced]:checked').val();
  612. if (advanced != 1)
  613. {
  614. $('#advanced_help').hide();
  615. }
  616. else
  617. {
  618. $('#advanced_help').show();
  619. }
  620. if (class_name != '')
  621. {
  622. var separator = $('input[name=separator]').val();
  623. var text_qualifier = $('input[name=text_qualifier]').val();
  624. var header_line = $('input[name=header_line]').val();
  625. var nb_lines_skipped = $('input[name=nb_skipped_lines]').val();
  626. var csv_data = $('input[name=csvdata]').val();
  627. var encoding = $('input[name=encoding]').val();
  628. if (advanced != 1)
  629. {
  630. advanced = 0;
  631. }
  632. $('#mapping').block();
  633. // Make sure that we cancel any pending request before issuing another
  634. // since responses may arrive in arbitrary order
  635. if (ajax_request != null)
  636. {
  637. ajax_request.abort();
  638. ajax_request = null;
  639. }
  640. ajax_request = $.post('ajax.csvimport.php',
  641. { operation: 'display_mapping_form', enctype: 'multipart/form-data', csvdata: csv_data, separator: separator,
  642. qualifier: text_qualifier, nb_lines_skipped: nb_lines_skipped, header_line: header_line, class_name: class_name,
  643. advanced: advanced, encoding: encoding },
  644. function(data) {
  645. $('#mapping').empty();
  646. $('#mapping').append(data);
  647. $('#mapping').unblock();
  648. }
  649. );
  650. }
  651. }
  652. function CheckValues()
  653. {
  654. bResult = true;
  655. bMappingOk = true;
  656. bSearchOk = false;
  657. $('select[name^=field]').each( function() {
  658. if ($(this).val() == '')
  659. {
  660. $(this).parent().css({'border': '2px #D81515 solid'});
  661. bMappingOk = false;
  662. bResult = false;
  663. }
  664. else
  665. {
  666. $(this).parent().css({'border': '0'});
  667. }
  668. });
  669. // At least one search field must be checked
  670. $('input[name^=search]:checked').each( function() {
  671. bSearchOk = true;
  672. });
  673. if (!bMappingOk)
  674. {
  675. alert("$sAlertIncompleteMapping");
  676. }
  677. if (!bSearchOk)
  678. {
  679. bResult = false;
  680. alert("$sAlertNoSearchCriteria");
  681. }
  682. if (bResult)
  683. {
  684. $('#mapping').block();
  685. // Re-enable all search_xxx checkboxes so that their value gets posted
  686. $('input[name^=search]').each(function() {
  687. $(this).attr('disabled', false);
  688. });
  689. }
  690. return bResult;
  691. }
  692. function DoCheckMapping()
  693. {
  694. // Check if there is a field mapped to 'id'
  695. // In which case, it's the only possible search key
  696. var idSelected = 0;
  697. var nbSearchKeys = $('input[name^=search]:checked').length;
  698. var nbMappings = $('select[name^=field]').length;
  699. for(index=1; index <= nbMappings; index++)
  700. {
  701. var selectedValue = $('#mapping_'+index).val();
  702. if (selectedValue == 'id')
  703. {
  704. idSelected = index;
  705. }
  706. }
  707. for (index=1; index <= nbMappings; index++)
  708. {
  709. sMappingValue = $('#mapping_'+index).val();
  710. if ((sMappingValue == '') || (sMappingValue == ':none:'))
  711. {
  712. // Non-mapped field, uncheck and disabled
  713. $('#search_'+index).attr('checked', false);
  714. $('#search_'+index).attr('disabled', true);
  715. }
  716. else if (index == idSelected)
  717. {
  718. // The 'id' field was mapped, it's the only possible reconciliation key
  719. $('#search_'+index).attr('checked', true);
  720. $('#search_'+index).attr('disabled', true);
  721. }
  722. else
  723. {
  724. if (idSelected > 0)
  725. {
  726. // The 'id' field was mapped, it's the only possible reconciliation key
  727. $('#search_'+index).attr('checked', false);
  728. $('#search_'+index).attr('disabled', true);
  729. }
  730. else
  731. {
  732. $('#search_'+index).attr('disabled', false);
  733. if (nbSearchKeys == 0)
  734. {
  735. // No search key was selected, select the default ones
  736. for(j =0; j < aDefaultKeys.length; j++)
  737. {
  738. if (sMappingValue == aDefaultKeys[j])
  739. {
  740. $('#search_'+index).attr('checked', true);
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. }
  748. EOF
  749. );
  750. }
  751. /**
  752. * Select the options of the CSV load and check for CSV parsing errors
  753. * @param WebPage $oPage The current web page
  754. * @return void
  755. */
  756. function SelectOptions(WebPage $oPage)
  757. {
  758. $sOperation = utils::ReadParam('operation', 'csv_data', 'post');
  759. $sCSVData = '';
  760. switch($sOperation)
  761. {
  762. case 'file_upload':
  763. $oDocument = utils::ReadPostedDocument('csvdata');
  764. if (!$oDocument->IsEmpty())
  765. {
  766. $sCSVData = $oDocument->GetData();
  767. }
  768. break;
  769. default:
  770. $sCSVData = utils::ReadParam('csvdata', '', 'post');
  771. }
  772. $sEncoding = utils::ReadParam('encoding', 'UTF-8');
  773. // Compute a subset of the data set, now that we know the charset
  774. if ($sEncoding == 'UTF-8')
  775. {
  776. $sUTF8Data = $sCSVData;
  777. }
  778. else
  779. {
  780. $sUTF8Data = iconv($sEncoding, 'UTF-8//IGNORE//TRANSLIT', $sCSVData);
  781. }
  782. $aGuesses = GuessParameters($sUTF8Data); // Try to predict the parameters, based on the input data
  783. $sSeparator = utils::ReadParam('separator', '');
  784. if ($sSeparator == '') // May be set to an empty value by the previous page
  785. {
  786. $sSeparator = $aGuesses['separator'];
  787. }
  788. $iSkippedLines = utils::ReadParam('nb_skipped_lines', '');
  789. $bBoxSkipLines = utils::ReadParam('box_skiplines', 0);
  790. if ($sSeparator == 'tab') $sSeparator = "\t";
  791. $sOtherSeparator = in_array($sSeparator, array(',', ';', "\t")) ? '' : $sSeparator;
  792. $sTextQualifier = utils::ReadParam('text_qualifier', '');
  793. if ($sTextQualifier == '') // May be set to an empty value by the previous page
  794. {
  795. $sTextQualifier = $aGuesses['qualifier'];
  796. }
  797. $sOtherTextQualifier = in_array($sTextQualifier, array('"', "'")) ? '' : $sTextQualifier;
  798. $bHeaderLine = utils::ReadParam('header_line', 0);
  799. $sClassName = utils::ReadParam('class_name', '');
  800. $bAdvanced = utils::ReadParam('advanced', 0);
  801. // Create a truncated version of the data used for the fast preview
  802. // Take about 20 lines of data... knowing that some lines may contain carriage returns
  803. $iMaxLines = 20;
  804. $iMaxLen = strlen($sUTF8Data);
  805. $iCurPos = true;
  806. while ( ($iCurPos > 0) && ($iMaxLines > 0))
  807. {
  808. $pos = strpos($sUTF8Data, "\n", $iCurPos);
  809. if ($pos !== false)
  810. {
  811. $iCurPos = 1+$pos;
  812. }
  813. else
  814. {
  815. $iCurPos = strlen($sUTF8Data);
  816. $iMaxLines = 1;
  817. }
  818. $iMaxLines--;
  819. }
  820. $sCSVDataTruncated = substr($sUTF8Data, 0, $iCurPos);
  821. $oPage->add('<h2>'.Dict::S('UI:Title:CSVImportStep2').'</h2>');
  822. $oPage->add('<div class="wizContainer">');
  823. $oPage->add('<table><tr><td style="vertical-align:top;padding-right:50px;">');
  824. $oPage->add('<form enctype="multipart/form-data" id="wizForm" method="post" id="csv_options">');
  825. $oPage->add('<h3>'.Dict::S('UI:CSVImport:SeparatorCharacter').'</h3>');
  826. $oPage->add('<p><input type="radio" name="separator" value="," onChange="DoPreview()"'.IsChecked($sSeparator, ',').'/> '.Dict::S('UI:CSVImport:SeparatorComma+').'<br/>');
  827. $oPage->add('<input type="radio" name="separator" value=";" onChange="DoPreview()"'.IsChecked($sSeparator, ';').'/> '.Dict::S('UI:CSVImport:SeparatorSemicolon+').'<br/>');
  828. $oPage->add('<input type="radio" name="separator" value="tab" onChange="DoPreview()"'.IsChecked($sSeparator, "\t").'/> '.Dict::S('UI:CSVImport:SeparatorTab+').'<br/>');
  829. $oPage->add('<input type="radio" name="separator" value="other" onChange="DoPreview()"'.IsChecked($sOtherSeparator, '', true).'/> '.Dict::S('UI:CSVImport:SeparatorOther').' <input type="text" size="3" maxlength="1" name="other_separator" id="other_separator" value="'.$sOtherSeparator.'" onChange="DoPreview()"/>');
  830. $oPage->add('</p>');
  831. $oPage->add('</td><td style="vertical-align:top;padding-right:50px;">');
  832. $oPage->add('<h3>'.Dict::S('UI:CSVImport:TextQualifierCharacter').'</h3>');
  833. $oPage->add('<p><input type="radio" name="text_qualifier" value="&#34;" onChange="DoPreview()"'.IsChecked($sTextQualifier, '"').'/> '.Dict::S('UI:CSVImport:QualifierDoubleQuote+').'<br/>');
  834. $oPage->add('<input type="radio" name="text_qualifier" value="&#39;" onChange="DoPreview()"'.IsChecked($sTextQualifier, "'").'/> '.Dict::S('UI:CSVImport:QualifierSimpleQuote+').'<br/>');
  835. $oPage->add('<input type="radio" name="text_qualifier" value="other" onChange="DoPreview()"'.IsChecked($sOtherTextQualifier, '', true).'/> '.Dict::S('UI:CSVImport:QualifierOther').' <input type="text" size="3" maxlength="1" name="other_qualifier" value="'.htmlentities($sOtherTextQualifier, ENT_QUOTES, 'UTF-8').'" onChange="DoPreview()"/>');
  836. $oPage->add('</p>');
  837. $oPage->add('</td><td style="vertical-align:top;">');
  838. $oPage->add('<h3>'.Dict::S('UI:CSVImport:CommentsAndHeader').'</h3>');
  839. $oPage->add('<p><input type="checkbox" name="header_line" id="box_header" value="1" onChange="DoPreview()"'.IsChecked($bHeaderLine, 1).'/> '.Dict::S('UI:CSVImport:TreatFirstLineAsHeader').'<p>');
  840. $oPage->add('<p><input type="checkbox" name="box_skiplines" value="1" id="box_skiplines" onChange="DoPreview()"'.IsChecked($bBoxSkipLines, 1).'/> '.Dict::Format('UI:CSVImport:Skip_N_LinesAtTheBeginning', '<input type="text" size=2 name="nb_skipped_lines" id="nb_skipped_lines" onChange="DoPreview()" value="'.$iSkippedLines.'">').'<p>');
  841. $oPage->add('</td></tr></table>');
  842. $oPage->add('<input type="hidden" name="csvdata_truncated" id="csvdata_truncated" value="'.htmlentities($sCSVDataTruncated, ENT_QUOTES, 'UTF-8').'"/>');
  843. $oPage->add('<input type="hidden" name="csvdata" id="csvdata" value="'.htmlentities($sUTF8Data, ENT_QUOTES, 'UTF-8').'"/>');
  844. $oPage->add('<input type="hidden" name="class_name" value="'.$sClassName.'"/>');
  845. $oPage->add('<input type="hidden" name="advanced" value="'.$bAdvanced.'"/>');
  846. $oPage->add('<input type="hidden" name="step" value="3"/>');
  847. $oPage->add('<div id="preview">');
  848. $oPage->add('<p style="text-align:center">'.Dict::S('UI:CSVImport:CSVDataPreview').'</p>');
  849. $oPage->add('</div>');
  850. $oPage->add('<input type="button" value="'.Dict::S('UI:Button:Back').'" onClick="GoBack()"/>');
  851. $oPage->add('<input type="submit" value="'.Dict::S('UI:Button:Next').'"/>');
  852. $oPage->add('</form>');
  853. $oPage->add('</div>');
  854. $oPage->add_script(
  855. <<<EOF
  856. function GoBack()
  857. {
  858. $('input[name=step]').val(1);
  859. $('#wizForm').submit();
  860. }
  861. var ajax_request = null;
  862. function DoPreview()
  863. {
  864. var separator = $('input[name=separator]:checked').val();
  865. if (separator == 'other')
  866. {
  867. separator = $('#other_separator').val();
  868. }
  869. var text_qualifier = $('input[name=text_qualifier]:checked').val();
  870. if (text_qualifier == 'other')
  871. {
  872. text_qualifier = $('#other_qualifier').val();
  873. }
  874. var nb_lines_skipped = 0;
  875. if ($('#box_skiplines:checked').val() != null)
  876. {
  877. nb_lines_skipped = $('#nb_skipped_lines').val();
  878. }
  879. var header_line = 0;
  880. if ($('#box_header:checked').val() != null)
  881. {
  882. header_line = 1;
  883. }
  884. var encoding = $('input[name=encoding]').val();
  885. $('#preview').block();
  886. // Make sure that we cancel any pending request before issuing another
  887. // since responses may arrive in arbitrary order
  888. if (ajax_request != null)
  889. {
  890. ajax_request.abort();
  891. ajax_request = null;
  892. }
  893. ajax_request = $.post('ajax.csvimport.php',
  894. { operation: 'parser_preview', enctype: 'multipart/form-data', csvdata: $("#csvdata_truncated").val(), separator: separator, qualifier: text_qualifier, nb_lines_skipped: nb_lines_skipped, header_line: header_line, encoding: encoding },
  895. function(data) {
  896. $('#preview').empty();
  897. $('#preview').append(data);
  898. $('#preview').unblock();
  899. }
  900. );
  901. }
  902. EOF
  903. );
  904. $oPage->add_ready_script('DoPreview();');
  905. }
  906. /**
  907. * Prompt for the data to be loaded (either via a file or a copy/paste)
  908. * @param WebPage $oPage The current web page
  909. * @return void
  910. */
  911. function Welcome(iTopWebPage $oPage)
  912. {
  913. // Encodings supported:
  914. // ICONV_CODE => Display Name
  915. // Each iconv installation supports different encodings
  916. // Some reasonably common and useful encodnings are listed here
  917. $aPossibleEncodings = array(
  918. 'UTF-8' => 'Unicode (UTF-8)',
  919. 'ISO-8859-1' => 'Western (ISO-8859-1)',
  920. 'WINDOWS-1251' => 'Cyrilic (Windows 1251)',
  921. 'WINDOWS-1252' => 'Western (Windows 1252)',
  922. 'ISO-8859-15' => 'Western (ISO-8859-15)',
  923. );
  924. // Some more encodings can be specified in the config file
  925. $aExtraCharsets = utils::GetConfig()->GetCSVImportCharsets();
  926. $aPossibleEncodings = array_merge($aPossibleEncodings, $aExtraCharsets);
  927. asort($aPossibleEncodings);
  928. $oPage->add("<div><p><h1>".Dict::S('UI:Title:BulkImport+')."</h1></p></div>\n");
  929. $oPage->AddTabContainer('tabs1');
  930. $sSeparator = utils::ReadParam('separator', '');
  931. $sTextQualifier = utils::ReadParam('text_qualifier', '');
  932. $bHeaderLine = utils::ReadParam('header_line', true);
  933. $iSkippedLines = utils::ReadParam('nb_skipped_lines', '');
  934. $sClassName = utils::ReadParam('class_name', '');
  935. $bAdvanced = utils::ReadParam('advanced', 0);
  936. $sEncoding = utils::ReadParam('encoding', 'UTF-8');
  937. $sFileLoadHtml = '<div><form enctype="multipart/form-data" method="post"><p>'.Dict::S('UI:CSVImport:SelectFile').'</p>'.
  938. '<p><input type="file" name="csvdata"/></p>';
  939. $sFileLoadHtml .= '<p>'.Dict::S('UI:CSVImport:Encoding').': ';
  940. $sFileLoadHtml .= '<select name="encoding" onChange="DoPreview()"/>';
  941. foreach($aPossibleEncodings as $sIconvCode => $sDisplayName )
  942. {
  943. $sSelected = '';
  944. if ($sEncoding == $sIconvCode)
  945. {
  946. $sSelected = ' selected';
  947. }
  948. $sFileLoadHtml .= '<option value="'.$sIconvCode.'"'.$sSelected.'>'.$sDisplayName.'</option>';
  949. }
  950. $sFileLoadHtml .= '</select></p>';
  951. $sFileLoadHtml .= '<p><input type="submit" value="'.Dict::S('UI:Button:Next').'"/></p>'.
  952. '<input type="hidden" name="step" value="2"/>'.
  953. '<input type="hidden" name="operation" value="file_upload"/>'.
  954. '<input type="hidden" name="header_line" value="'.$bHeaderLine.'"/>'.
  955. '<input type="hidden" name="nb_skipped_lines" value="'.$iSkippedLines.'"/>'.
  956. '<input type="hidden" name="class_name" value="'.$sClassName.'"/>'.
  957. '<input type="hidden" name="advanced" value="'.$bAdvanced.'"/>'.
  958. '</form></div>';
  959. $oPage->AddToTab('tabs1', Dict::S('UI:CSVImport:Tab:LoadFromFile'), $sFileLoadHtml);
  960. $sCSVData = utils::ReadParam('csvdata', '');
  961. $sPasteDataHtml = '<div><form enctype="multipart/form-data" method="post"><p>'.Dict::S('UI:CSVImport:PasteData').'</p>'.
  962. '<p><textarea cols="100" rows="30" name="csvdata">'.htmlentities($sCSVData, ENT_QUOTES, 'UTF-8').'</textarea></p>';
  963. $sPasteDataHtml .= '<p>'.Dict::S('UI:CSVImport:Encoding').': ';
  964. $sPasteDataHtml .= '<select name="encoding" onChange="DoPreview()"/>';
  965. foreach($aPossibleEncodings as $sIconvCode => $sDisplayName )
  966. {
  967. $sSelected = '';
  968. if ($sEncoding == $sIconvCode)
  969. {
  970. $sSelected = ' selected';
  971. }
  972. $sPasteDataHtml .= '<option value="'.$sIconvCode.'"'.$sSelected.'>'.$sDisplayName.'</option>';
  973. }
  974. $sPasteDataHtml .= '</select></p>'.
  975. '<p><input type="submit" value="'.Dict::S('UI:Button:Next').'"/></p>'.
  976. '<input type="hidden" name="step" value="2"/>'.
  977. '<input type="hidden" name="operation" value="csv_data"/>'.
  978. '<input type="hidden" name="separator" value="'.htmlentities($sSeparator, ENT_QUOTES, 'UTF-8').'"/>'.
  979. '<input type="hidden" name="text_qualifier" value="'.htmlentities($sTextQualifier, ENT_QUOTES, 'UTF-8').'"/>'.
  980. '<input type="hidden" name="header_line" value="'.$bHeaderLine.'"/>'.
  981. '<input type="hidden" name="nb_skipped_lines" value="'.$iSkippedLines.'"/>'.
  982. '<input type="hidden" name="class_name" value="'.$sClassName.'"/>'.
  983. '<input type="hidden" name="advanced" value="'.$bAdvanced.'"/>'.
  984. '</form></div>';
  985. $oPage->AddToTab('tabs1', Dict::S('UI:CSVImport:Tab:CopyPaste'), $sPasteDataHtml);
  986. if (!empty($sCSVData))
  987. {
  988. // When there are some data, activate the 'copy & paste' tab by default
  989. $oPage->SelectTab('tabs1', Dict::S('UI:CSVImport:Tab:CopyPaste'));
  990. }
  991. $sTemplateHtml = '<div><p>'.Dict::S('UI:CSVImport:PickClassForTemplate').' ';
  992. $sTemplateHtml .= GetClassesSelect('template_class', '', 300, UR_ACTION_BULK_MODIFY);
  993. $sTemplateHtml .= '</div>';
  994. $sTemplateHtml .= '<div id="template" style="text-align:center">';
  995. $sTemplateHtml .= '</div>';
  996. $oPage->AddToTab('tabs1', Dict::S('UI:CSVImport:Tab:Templates'), $sTemplateHtml);
  997. $oPage->add_script(
  998. <<<EOF
  999. var ajax_request = null;
  1000. function DisplayTemplate(sClassName) {
  1001. $('#template').block();
  1002. // Make sure that we cancel any pending request before issuing another
  1003. // since responses may arrive in arbitrary order
  1004. if (ajax_request != null)
  1005. {
  1006. ajax_request.abort();
  1007. ajax_request = null;
  1008. }
  1009. ajax_request = $.post('ajax.csvimport.php',
  1010. { operation: 'get_csv_template', class_name: sClassName },
  1011. function(data) {
  1012. $('#template').empty();
  1013. $('#template').append(data);
  1014. $('#template').unblock();
  1015. }
  1016. );
  1017. }
  1018. EOF
  1019. );
  1020. $oPage->add_ready_script(
  1021. <<<EOF
  1022. $('#select_template_class').change( function() {
  1023. DisplayTemplate(this.value);
  1024. });
  1025. EOF
  1026. );
  1027. }
  1028. switch($iStep)
  1029. {
  1030. case 5:
  1031. LoadData($oPage);
  1032. break;
  1033. case 4:
  1034. Preview($oPage);
  1035. break;
  1036. case 3:
  1037. SelectMapping($oPage);
  1038. break;
  1039. case 2:
  1040. SelectOptions($oPage);
  1041. break;
  1042. case 1:
  1043. case 6: // Loop back here when we are done
  1044. default:
  1045. Welcome($oPage);
  1046. }
  1047. $oPage->output();
  1048. ?>