csvimport.php 43 KB

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