csvimport.php 43 KB

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