tabularbulkexport.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Bulk export: Tabular export: abstract base class for all "tabular" exports.
  20. * Provides the user interface for selecting the column to be exported
  21. *
  22. * @copyright Copyright (C) 2015 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. abstract class TabularBulkExport extends BulkExport
  26. {
  27. public function EnumFormParts()
  28. {
  29. return array_merge(parent::EnumFormParts(), array('tabular_fields' => array('fields')));
  30. }
  31. public function DisplayFormPart(WebPage $oP, $sPartId)
  32. {
  33. switch($sPartId)
  34. {
  35. case 'tabular_fields':
  36. $sFields = utils::ReadParam('fields', '', true, 'raw_data');
  37. $sSuggestedFields = utils::ReadParam('suggested_fields', null, true, 'raw_data');
  38. if (($sSuggestedFields !== null) && ($sSuggestedFields !== ''))
  39. {
  40. $aSuggestedFields = explode(',', $sSuggestedFields);
  41. $sFields = implode(',', $this->SuggestFields($aSuggestedFields));
  42. }
  43. $oP->add('<input id="tabular_fields" type="hidden" size="50" name="fields" value="'.htmlentities($sFields, ENT_QUOTES, 'UTF-8').'"></input>');
  44. break;
  45. default:
  46. return parent::DisplayFormPart($oP, $sPartId);
  47. }
  48. }
  49. protected function SuggestFields($aSuggestedFields)
  50. {
  51. $aRet = array();
  52. // By defaults all fields are Ok, nothing gets translated but
  53. // you can overload this method if some fields are better exported
  54. // (in a given format) by using an alternate field, for example id => friendlyname
  55. $aAliases = $this->oSearch->GetSelectedClasses();
  56. foreach($aSuggestedFields as $idx => $sField)
  57. {
  58. if (preg_match('/^([^\\.]+)\\.(.+)$/', $sField, $aMatches))
  59. {
  60. $sAlias = $aMatches[1];
  61. $sAttCode = $aMatches[2];
  62. $sClass = $aAliases[$sAlias];
  63. }
  64. else
  65. {
  66. $sAlias = '';
  67. $sAttCode = $sField;
  68. $sClass = reset($aAliases);
  69. }
  70. $sMostRelevantField = $this->SuggestField($sClass, $sAttCode);
  71. $sAttCodeEx = $this->NormalizeFieldSpec($sClass, $sMostRelevantField);
  72. // Remove the aliases (if any) from the field names to make them compatible
  73. // with the 'short' notation used in this case by the widget
  74. if (count($aAliases) > 1)
  75. {
  76. $sAttCodeEx = $sAlias.'.'.$sAttCodeEx;
  77. }
  78. $aRet[] = $sAttCodeEx;
  79. }
  80. return $aRet;
  81. }
  82. protected function SuggestField($sClass, $sAttCode)
  83. {
  84. return $sAttCode;
  85. }
  86. /**
  87. * Given a field spec, get the most relevant (unique) representation
  88. * Examples for a user request:
  89. * - friendlyname => ref
  90. * - org_name => org_id->name
  91. * - org_id_friendlyname => org_id=>name
  92. * - caller_name => caller_id->name
  93. * - caller_id_friendlyname => caller_id->friendlyname
  94. */
  95. protected function NormalizeFieldSpec($sClass, $sField)
  96. {
  97. $sRet = $sField;
  98. if ($sField == 'id')
  99. {
  100. $sRet = 'id';
  101. }
  102. elseif ($sField == 'friendlyname')
  103. {
  104. $sFriendlyNameAttCode = MetaModel::GetFriendlyNameAttributeCode($sClass);
  105. if (!is_null($sFriendlyNameAttCode))
  106. {
  107. // The friendly name is made of a single attribute
  108. $sRet = $sFriendlyNameAttCode;
  109. }
  110. }
  111. else
  112. {
  113. $oAttDef = MetaModel::GetAttributeDef($sClass, $sField);
  114. if ($oAttDef instanceof AttributeFriendlyName)
  115. {
  116. $oKeyAttDef = MetaModel::GetAttributeDef($sClass, $oAttDef->GetKeyAttCode());
  117. $sRemoteClass = $oKeyAttDef->GetTargetClass();
  118. $sFriendlyNameAttCode = MetaModel::GetFriendlyNameAttributeCode($sRemoteClass);
  119. if (is_null($sFriendlyNameAttCode))
  120. {
  121. // The friendly name is made of several attributes
  122. $sRet = $oAttDef->GetKeyAttCode().'->friendlyname';
  123. }
  124. else
  125. {
  126. // The friendly name is made of a single attribute
  127. $sRet = $oAttDef->GetKeyAttCode().'->'.$sFriendlyNameAttCode;
  128. }
  129. }
  130. elseif ($oAttDef->IsExternalField())
  131. {
  132. $sRet = $oAttDef->GetKeyAttCode().'->'.$oAttDef->GetExtAttCode();
  133. }
  134. }
  135. return $sRet;
  136. }
  137. protected function IsSubAttribute($sClass, $sAttCode, $oAttDef)
  138. {
  139. return (($oAttDef instanceof AttributeFriendlyName) || ($oAttDef instanceof AttributeExternalField) || ($oAttDef instanceof AttributeSubItem));
  140. }
  141. protected function GetSubAttributes($sClass, $sAttCode, $oAttDef)
  142. {
  143. $aResult = array();
  144. switch(get_class($oAttDef))
  145. {
  146. case 'AttributeExternalKey':
  147. case 'AttributeHierarchicalKey':
  148. $bAddFriendlyName = true;
  149. $oKeyAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  150. $sRemoteClass = $oKeyAttDef->GetTargetClass();
  151. $sFriendlyNameAttCode = MetaModel::GetFriendlyNameAttributeCode($sRemoteClass);
  152. if (!is_null($sFriendlyNameAttCode))
  153. {
  154. // The friendly name is made of a single attribute, check if that attribute is present as an external field
  155. foreach(MetaModel::ListAttributeDefs($sClass) as $sSubAttCode => $oSubAttDef)
  156. {
  157. if ($oSubAttDef instanceof AttributeExternalField)
  158. {
  159. if (($oSubAttDef->GetKeyAttCode() == $sAttCode) && ($oSubAttDef->GetExtAttCode() == $sFriendlyNameAttCode))
  160. {
  161. $bAddFriendlyName = false;
  162. }
  163. }
  164. }
  165. }
  166. $aResult[$sAttCode] = array('code' => $sAttCode, 'unique_label' => $oAttDef->GetLabel(), 'label' => Dict::S('UI:CSVImport:idField'), 'attdef' => $oAttDef);
  167. if ($bAddFriendlyName)
  168. {
  169. if ($this->IsExportableField($sClass, $sAttCode.'->friendlyname'))
  170. {
  171. $aResult[$sAttCode.'->friendlyname'] = array('code' => $sAttCode.'->friendlyname', 'unique_label' => $oAttDef->GetLabel().'->'.Dict::S('Core:FriendlyName-Label'), 'label' => Dict::S('Core:FriendlyName-Label'), 'attdef' => MetaModel::GetAttributeDef($sClass, $sAttCode.'_friendlyname'));
  172. }
  173. }
  174. foreach(MetaModel::ListAttributeDefs($sClass) as $sSubAttCode => $oSubAttDef)
  175. {
  176. if ($oSubAttDef instanceof AttributeExternalField)
  177. {
  178. if ($this->IsExportableField($sClass, $sSubAttCode, $oSubAttDef))
  179. {
  180. if ($oSubAttDef->GetKeyAttCode() == $sAttCode)
  181. {
  182. $sAttCodeEx = $sAttCode.'->'.$oSubAttDef->GetExtAttCode();
  183. $aResult[$sAttCodeEx] = array('code' => $sAttCodeEx, 'unique_label' => $oAttDef->GetLabel().'->'.$oSubAttDef->GetExtAttDef()->GetLabel(), 'label' => MetaModel::GetLabel($sRemoteClass, $oSubAttDef->GetExtAttCode()), 'attdef' => $oSubAttDef);
  184. }
  185. }
  186. }
  187. }
  188. // Add the reconciliation keys
  189. foreach(MetaModel::GetReconcKeys($sRemoteClass) as $sRemoteAttCode)
  190. {
  191. $sAttCodeEx = $sAttCode.'->'.$sRemoteAttCode;
  192. if (!array_key_exists($sAttCodeEx, $aResult))
  193. {
  194. $oRemoteAttDef = MetaModel::GetAttributeDef($sRemoteClass, $sRemoteAttCode);
  195. if ($this->IsExportableField($sRemoteClass, $sRemoteAttCode, $oRemoteAttDef))
  196. {
  197. $aResult[$sAttCodeEx] = array('code' => $sAttCodeEx, 'unique_label' => $oAttDef->GetLabel().'->'.$oRemoteAttDef->GetLabel(), 'label' => MetaModel::GetLabel($sRemoteClass, $sRemoteAttCode), 'attdef' => $oRemoteAttDef);
  198. }
  199. }
  200. }
  201. break;
  202. case 'AttributeStopWatch':
  203. foreach(MetaModel::ListAttributeDefs($sClass) as $sSubAttCode => $oSubAttDef)
  204. {
  205. if ($oSubAttDef instanceof AttributeSubItem)
  206. {
  207. if ($oSubAttDef->GetParentAttCode() == $sAttCode)
  208. {
  209. if ($this->IsExportableField($sClass, $sSubAttCode, $oSubAttDef))
  210. {
  211. $aResult[$sSubAttCode] = array('code' => $sSubAttCode, 'unique_label' => $oSubAttDef->GetLabel(), 'label' => $oSubAttDef->GetLabel(), 'attdef' => $oSubAttDef);
  212. }
  213. }
  214. }
  215. }
  216. break;
  217. }
  218. return $aResult;
  219. }
  220. protected function GetInteractiveFieldsWidget(WebPage $oP, $sWidgetId)
  221. {
  222. $oSet = new DBObjectSet($this->oSearch);
  223. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  224. $aAuthorizedClasses = array();
  225. foreach($aSelectedClasses as $sAlias => $sClassName)
  226. {
  227. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  228. {
  229. $aAuthorizedClasses[$sAlias] = $sClassName;
  230. }
  231. }
  232. $aAllFieldsByAlias = array();
  233. $aAllAttCodes = array();
  234. foreach($aAuthorizedClasses as $sAlias => $sClass)
  235. {
  236. $aAllFields = array();
  237. if (count($aAuthorizedClasses) > 1 )
  238. {
  239. $sShortAlias = $sAlias.'.';
  240. }
  241. else
  242. {
  243. $sShortAlias = '';
  244. }
  245. if ($this->IsExportableField($sClass, 'id'))
  246. {
  247. $sFriendlyNameAttCode = MetaModel::GetFriendlyNameAttributeCode($sClass);
  248. if (is_null($sFriendlyNameAttCode))
  249. {
  250. // The friendly name is made of several attribute
  251. $aSubAttr = array(
  252. array('attcodeex' => 'id', 'code' => $sShortAlias.'id', 'unique_label' => $sShortAlias.Dict::S('UI:CSVImport:idField'), 'label' => $sShortAlias.'id'),
  253. array('attcodeex' => 'friendlyname', 'code' => $sShortAlias.'friendlyname', 'unique_label' => $sShortAlias.Dict::S('Core:FriendlyName-Label'), 'label' => $sShortAlias.Dict::S('Core:FriendlyName-Label')),
  254. );
  255. }
  256. else
  257. {
  258. // The friendly name has no added value
  259. $aSubAttr = array();
  260. }
  261. $aAllFields[] = array('attcodeex' => 'id', 'code' => $sShortAlias.'id', 'unique_label' => $sShortAlias.Dict::S('UI:CSVImport:idField'), 'label' => Dict::S('UI:CSVImport:idField'), 'subattr' => $aSubAttr);
  262. }
  263. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  264. {
  265. if($this->IsSubAttribute($sClass, $sAttCode, $oAttDef)) continue;
  266. if ($this->IsExportableField($sClass, $sAttCode, $oAttDef))
  267. {
  268. $sShortLabel = $oAttDef->GetLabel();
  269. $sLabel = $sShortAlias.$oAttDef->GetLabel();
  270. $aSubAttr = $this->GetSubAttributes($sClass, $sAttCode, $oAttDef);
  271. $aValidSubAttr = array();
  272. foreach($aSubAttr as $aSubAttDef)
  273. {
  274. $aValidSubAttr[] = array('attcodeex' => $aSubAttDef['code'], 'code' => $sShortAlias.$aSubAttDef['code'], 'label' => $aSubAttDef['label'], 'unique_label' => $sShortAlias.$aSubAttDef['unique_label']);
  275. }
  276. $aAllFields[] = array('attcodeex' => $sAttCode, 'code' => $sShortAlias.$sAttCode, 'label' => $sShortLabel, 'unique_label' => $sLabel, 'subattr' => $aValidSubAttr);
  277. }
  278. }
  279. usort($aAllFields, array(get_class($this), 'SortOnLabel'));
  280. if (count($aAuthorizedClasses) > 1)
  281. {
  282. $sKey = MetaModel::GetName($sClass).' ('.$sAlias.')';
  283. }
  284. else
  285. {
  286. $sKey = MetaModel::GetName($sClass);
  287. }
  288. $aAllFieldsByAlias[$sKey] = $aAllFields;
  289. foreach ($aAllFields as $aFieldSpec)
  290. {
  291. $sAttCode = $aFieldSpec['attcodeex'];
  292. if (count($aFieldSpec['subattr']) > 0)
  293. {
  294. foreach ($aFieldSpec['subattr'] as $aSubFieldSpec)
  295. {
  296. $aAllAttCodes[$sAlias][] = $aSubFieldSpec['attcodeex'];
  297. }
  298. }
  299. else
  300. {
  301. $aAllAttCodes[$sAlias][] = $sAttCode;
  302. }
  303. }
  304. }
  305. $oP->add('<div id="'.$sWidgetId.'"></div>');
  306. $JSAllFields = json_encode($aAllFieldsByAlias);
  307. // First, fetch only the ids - the rest will be fetched by an object reload
  308. $oSet = new DBObjectSet($this->oSearch);
  309. $iCount = $oSet->Count();
  310. foreach ($this->oSearch->GetSelectedClasses() as $sAlias => $sClass)
  311. {
  312. $aColumns[$sAlias] = array();
  313. }
  314. $oSet->OptimizeColumnLoad($aColumns);
  315. $iPreviewLimit = 3;
  316. $oSet->SetLimit($iPreviewLimit);
  317. $aSampleData = array();
  318. while($aRow = $oSet->FetchAssoc())
  319. {
  320. $aSampleRow = array();
  321. foreach($aAuthorizedClasses as $sAlias => $sClass)
  322. {
  323. if (count($aAuthorizedClasses) > 1 )
  324. {
  325. $sShortAlias = $sAlias.'.';
  326. }
  327. else
  328. {
  329. $sShortAlias = '';
  330. }
  331. foreach ($aAllAttCodes[$sAlias] as $sAttCodeEx)
  332. {
  333. $oObj = $aRow[$sAlias];
  334. $aSampleRow[$sShortAlias.$sAttCodeEx] = $oObj ? $this->GetSampleData($oObj, $sAttCodeEx) : '';
  335. }
  336. }
  337. $aSampleData[] = $aSampleRow;
  338. }
  339. $sJSSampleData = json_encode($aSampleData);
  340. $aLabels = array(
  341. 'preview_header' => Dict::S('Core:BulkExport:DragAndDropHelp'),
  342. 'empty_preview' => Dict::S('Core:BulkExport:EmptyPreview'),
  343. 'columns_order' => Dict::S('Core:BulkExport:ColumnsOrder'),
  344. 'columns_selection' => Dict::S('Core:BulkExport:AvailableColumnsFrom_Class'),
  345. 'check_all' => Dict::S('Core:BulkExport:CheckAll'),
  346. 'uncheck_all' => Dict::S('Core:BulkExport:UncheckAll'),
  347. 'no_field_selected' => Dict::S('Core:BulkExport:NoFieldSelected'),
  348. );
  349. $sJSLabels = json_encode($aLabels);
  350. $oP->add_ready_script(
  351. <<<EOF
  352. $('#$sWidgetId').tabularfieldsselector({fields: $JSAllFields, value_holder: '#tabular_fields', advanced_holder: '#tabular_advanced', sample_data: $sJSSampleData, total_count: $iCount, preview_limit: $iPreviewLimit, labels: $sJSLabels });
  353. EOF
  354. );
  355. }
  356. static public function SortOnLabel($aItem1, $aItem2)
  357. {
  358. return strcmp($aItem1['label'], $aItem2['label']);
  359. }
  360. /**
  361. * Tells if the specified field can be exported
  362. * @param unknown $sClass
  363. * @param unknown $sAttCode
  364. * @param AttributeDefinition $oAttDef Can be null in case the attribute definition has not been fetched by the caller
  365. * @return boolean
  366. */
  367. protected function IsExportableField($sClass, $sAttCode, $oAttDef = null)
  368. {
  369. if ($sAttCode == 'id') return true;
  370. if (is_null($oAttDef))
  371. {
  372. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  373. }
  374. if ($oAttDef instanceof AttributeLinkedSet) return false;
  375. return true; //$oAttDef->IsScalar();
  376. }
  377. protected function GetSampleData($oObj, $sAttCode)
  378. {
  379. if ($sAttCode == 'id') return $oObj->GetKey();
  380. return $oObj->GetEditValue($sAttCode);
  381. }
  382. public function ReadParameters()
  383. {
  384. parent::ReadParameters();
  385. $sQueryId = utils::ReadParam('query', null, true);
  386. $sFields = utils::ReadParam('fields', null, true, 'raw_data');
  387. if ((($sFields === null) || ($sFields === '')) && ($sQueryId === null))
  388. {
  389. throw new BulkExportMissingParameterException('fields');
  390. }
  391. else if(($sQueryId !== null) && ($sQueryId !== null))
  392. {
  393. $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId));
  394. $oQueries = new DBObjectSet($oSearch);
  395. if ($oQueries->Count() > 0)
  396. {
  397. $oQuery = $oQueries->Fetch();
  398. if (($sFields === null) || ($sFields === ''))
  399. {
  400. // No 'fields' parameter supplied, take the fields from the query phrasebook definition
  401. $sFields = trim($oQuery->Get('fields'));
  402. if ($sFields === '')
  403. {
  404. throw new BulkExportMissingParameterException('fields');
  405. }
  406. }
  407. }
  408. else
  409. {
  410. throw BulkExportException('Invalid value for the parameter: query. There is no Query Phrasebook with id = '.$sQueryId, Dict::Format('Core:BulkExport:InvalidParameter_Query', $sQueryId));
  411. }
  412. }
  413. // Interpret (and check) the list of fields
  414. //
  415. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  416. $aAliases = array_keys($aSelectedClasses);
  417. $aAuthorizedClasses = array();
  418. foreach($aSelectedClasses as $sAlias => $sClassName)
  419. {
  420. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ) == UR_ALLOWED_YES)
  421. {
  422. $aAuthorizedClasses[$sAlias] = $sClassName;
  423. }
  424. }
  425. $aFields = explode(',', $sFields);
  426. $this->aStatusInfo['fields'] = array();
  427. foreach($aFields as $sFieldSpec)
  428. {
  429. // Trim the values since it's natural to write: fields=name, first_name, org_name instead of fields=name,first_name,org_name
  430. $sExtendedAttCode = trim($sFieldSpec);
  431. if (preg_match('/^([^\.]+)\.(.+)$/', $sExtendedAttCode, $aMatches))
  432. {
  433. $sAlias = $aMatches[1];
  434. $sAttCode = $aMatches[2];
  435. }
  436. else
  437. {
  438. $sAlias = reset($aAliases);
  439. $sAttCode = $sExtendedAttCode;
  440. }
  441. if (!array_key_exists($sAlias, $aSelectedClasses))
  442. {
  443. throw new Exception("Invalid alias '$sAlias' for the column '$sExtendedAttCode'. Availables aliases: '".implode("', '", $aAliases)."'");
  444. }
  445. $sClass = $aSelectedClasses[$sAlias];
  446. if (!array_key_exists($sAlias, $aAuthorizedClasses))
  447. {
  448. throw new Exception("You do not have enough permissions to bulk read data of class '$sClass' (alias: $sAlias)");
  449. }
  450. if ($this->bLocalizeOutput)
  451. {
  452. try
  453. {
  454. $sLabel = MetaModel::GetLabel($sClass, $sAttCode);
  455. }
  456. catch (Exception $e)
  457. {
  458. throw new Exception("Wrong field specification '$sFieldSpec': ".$e->getMessage());
  459. }
  460. }
  461. else
  462. {
  463. $sLabel = $sAttCode;
  464. }
  465. if (count($aAuthorizedClasses) > 1)
  466. {
  467. $sColLabel = $sAlias.'.'.$sLabel;
  468. }
  469. else
  470. {
  471. $sColLabel = $sLabel;
  472. }
  473. $this->aStatusInfo['fields'][] = array(
  474. 'sFieldSpec' => $sExtendedAttCode,
  475. 'sAlias' => $sAlias,
  476. 'sClass' => $sClass,
  477. 'sAttCode' => $sAttCode,
  478. 'sLabel' => $sLabel,
  479. 'sColLabel' => $sColLabel
  480. );
  481. }
  482. }
  483. /**
  484. * Prepare the given object set with the list of fields as read into $this->aStatusInfo['fields']
  485. */
  486. protected function OptimizeColumnLoad(DBObjectSet $oSet)
  487. {
  488. $aColumnsToLoad = array();
  489. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  490. {
  491. $sClass = $aFieldSpec['sClass'];
  492. $sAlias = $aFieldSpec['sAlias'];
  493. $sAttCode = $aFieldSpec['sAttCode'];
  494. if (!array_key_exists($sAlias, $aColumnsToLoad))
  495. {
  496. $aColumnsToLoad[$sAlias] = array();
  497. }
  498. // id is not a real attribute code and, moreover, is always loaded
  499. if ($sAttCode != 'id')
  500. {
  501. // Extended attributes are not recognized by DBObjectSet::OptimizeColumnLoad
  502. if (($iPos = strpos($sAttCode, '->')) === false)
  503. {
  504. $aColumnsToLoad[$sAlias][] = $sAttCode;
  505. $sClass = '???';
  506. }
  507. else
  508. {
  509. $sExtKeyAttCode = substr($sAttCode, 0, $iPos);
  510. $sRemoteAttCode = substr($sAttCode, $iPos + 2);
  511. // Load the external key to avoid an object reload!
  512. $aColumnsToLoad[$sAlias][] = $sExtKeyAttCode;
  513. // Load the external field (if any) to avoid getting the remote object (see DBObject::Get that does the same)
  514. $oExtFieldAtt = MetaModel::FindExternalField($sClass, $sExtKeyAttCode, $sRemoteAttCode);
  515. if (!is_null($oExtFieldAtt))
  516. {
  517. $aColumnsToLoad[$sAlias][] = $oExtFieldAtt->GetCode();
  518. }
  519. }
  520. }
  521. }
  522. // Add "always loaded attributes"
  523. //
  524. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  525. foreach ($aSelectedClasses as $sAlias => $sClass)
  526. {
  527. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  528. {
  529. if ($oAttDef->AlwaysLoadInTables())
  530. {
  531. $aColumnsToLoad[$sAlias][] = $sAttCode;
  532. }
  533. }
  534. }
  535. $oSet->OptimizeColumnLoad($aColumnsToLoad);
  536. }
  537. }