tabularbulkexport.class.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. // By defaults all fields are Ok, nothing gets translated but
  52. // you can overload this method if some fields are better exported
  53. // (in a given format) by using an alternate field, for example id => friendlyname
  54. $aAliases = $this->oSearch->GetSelectedClasses();
  55. foreach($aSuggestedFields as $idx => $sField)
  56. {
  57. if (preg_match('/^([^\\.]+)\\.(.+)$/', $sField, $aMatches))
  58. {
  59. $sAlias = $aMatches[1];
  60. $sAttCode = $aMatches[2];
  61. $sClass = $aAliases[$sAlias];
  62. }
  63. else
  64. {
  65. $sAlias = '';
  66. $sAttCode = $sField;
  67. $sClass = reset($aAliases);
  68. }
  69. $aSuggestedFields[$idx] = $this->SuggestField($aAliases, $sClass, $sAlias, $sAttCode);
  70. }
  71. return $aSuggestedFields;
  72. }
  73. protected function SuggestField($aAliases, $sClass, $sAlias, $sAttCode)
  74. {
  75. // Remove the aliases (if any) from the field names to make them compatible
  76. // with the 'short' notation used in this case by the widget
  77. if (count($aAliases) == 1)
  78. {
  79. return $sAttCode;
  80. }
  81. return $sAlias.'.'.$sAttCode;
  82. }
  83. protected function IsSubAttribute($sClass, $sAttCode, $oAttDef)
  84. {
  85. return (($oAttDef instanceof AttributeFriendlyName) || ($oAttDef instanceof AttributeExternalField) || ($oAttDef instanceof AttributeSubItem));
  86. }
  87. protected function GetSubAttributes($sClass, $sAttCode, $oAttDef)
  88. {
  89. $aResult = array();
  90. switch(get_class($oAttDef))
  91. {
  92. case 'AttributeExternalKey':
  93. case 'AttributeHierarchicalKey':
  94. $aResult[] = array('code' => $sAttCode, 'unique_label' => $oAttDef->GetLabel(), 'label' => Dict::S('Core:BulkExport:Identifier'), 'attdef' => $oAttDef);
  95. $aResult[] = 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'));
  96. foreach(MetaModel::ListAttributeDefs($sClass) as $sSubAttCode => $oSubAttDef)
  97. {
  98. if ($oSubAttDef instanceof AttributeExternalField)
  99. {
  100. if ($oSubAttDef->GetKeyAttCode() == $sAttCode)
  101. {
  102. $aResult[] = array('code' => $sSubAttCode, 'unique_label' => $oAttDef->GetLabel().'->'.$oSubAttDef->GetExtAttDef()->GetLabel(), 'label' => $oSubAttDef->GetExtAttDef()->GetLabel(), 'attdef' => $oSubAttDef);
  103. }
  104. }
  105. }
  106. break;
  107. case 'AttributeStopWatch':
  108. foreach(MetaModel::ListAttributeDefs($sClass) as $sSubAttCode => $oSubAttDef)
  109. {
  110. if ($oSubAttDef instanceof AttributeSubItem)
  111. {
  112. if ($oSubAttDef->GetParentAttCode() == $sAttCode)
  113. {
  114. $aResult[] = array('code' => $sSubAttCode, 'unique_label' => $oSubAttDef->GetLabel(), 'label' => $oSubAttDef->GetLabel(), 'attdef' => $oSubAttDef);
  115. }
  116. }
  117. }
  118. break;
  119. }
  120. return $aResult;
  121. }
  122. protected function GetInteractiveFieldsWidget(WebPage $oP, $sWidgetId)
  123. {
  124. $oSet = new DBObjectSet($this->oSearch);
  125. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  126. $aAuthorizedClasses = array();
  127. foreach($aSelectedClasses as $sAlias => $sClassName)
  128. {
  129. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  130. {
  131. $aAuthorizedClasses[$sAlias] = $sClassName;
  132. }
  133. }
  134. $aAllFieldsByAlias = array();
  135. foreach($aAuthorizedClasses as $sAlias => $sClass)
  136. {
  137. $aAllFields = array();
  138. if (count($aAuthorizedClasses) > 1 )
  139. {
  140. $sShortAlias = $sAlias.'.';
  141. }
  142. else
  143. {
  144. $sShortAlias = '';
  145. }
  146. if ($this->IsValidField($sClass, 'id'))
  147. {
  148. $aAllFields[] = array('code' => $sShortAlias.'id', 'unique_label' => $sShortAlias.Dict::S('Core:BulkExport:Identifier'), 'label' => $sShortAlias.'id', 'subattr' => array(
  149. array('code' => $sShortAlias.'id', 'unique_label' => $sShortAlias.Dict::S('Core:BulkExport:Identifier'), 'label' => $sShortAlias.'id'),
  150. array('code' => $sShortAlias.'friendlyname', 'unique_label' => $sShortAlias.Dict::S('Core:FriendlyName-Label'), 'label' => $sShortAlias.Dict::S('Core:FriendlyName-Label')),
  151. ));
  152. }
  153. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  154. {
  155. if($this->IsSubAttribute($sClass, $sAttCode, $oAttDef)) continue;
  156. if ($this->IsValidField($sClass, $sAttCode, $oAttDef))
  157. {
  158. $sShortLabel = $oAttDef->GetLabel();
  159. $sLabel = $sShortAlias.$oAttDef->GetLabel();
  160. $aSubAttr = $this->GetSubAttributes($sClass, $sAttCode, $oAttDef);
  161. $aValidSubAttr = array();
  162. foreach($aSubAttr as $aSubAttDef)
  163. {
  164. if ($this->IsValidField($sClass, $aSubAttDef['code'], $aSubAttDef['attdef']))
  165. {
  166. $aValidSubAttr[] = array('code' => $sShortAlias.$aSubAttDef['code'], 'label' => $aSubAttDef['label'], 'unique_label' => $aSubAttDef['unique_label']);
  167. }
  168. }
  169. $aAllFields[] = array('code' => $sShortAlias.$sAttCode, 'label' => $sShortLabel, 'unique_label' => $sLabel, 'subattr' => $aValidSubAttr);
  170. }
  171. }
  172. usort($aAllFields, array(get_class($this), 'SortOnLabel'));
  173. if (count($aAuthorizedClasses) > 1)
  174. {
  175. $sKey = MetaModel::GetName($sClass).' ('.$sAlias.')';
  176. }
  177. else
  178. {
  179. $sKey = MetaModel::GetName($sClass);
  180. }
  181. $aAllFieldsByAlias[$sKey] = $aAllFields;
  182. }
  183. $oP->add('<div id="'.$sWidgetId.'"></div>');
  184. $JSAllFields = json_encode($aAllFieldsByAlias);
  185. $oSet = new DBObjectSet($this->oSearch);
  186. $iCount = $oSet->Count();
  187. $iPreviewLimit = 3;
  188. $oSet->SetLimit($iPreviewLimit);
  189. $aSampleData = array();
  190. while($aRow = $oSet->FetchAssoc())
  191. {
  192. $aSampleRow = array();
  193. foreach($aAuthorizedClasses as $sAlias => $sClass)
  194. {
  195. if (count($aAuthorizedClasses) > 1 )
  196. {
  197. $sShortAlias = $sAlias.'.';
  198. }
  199. else
  200. {
  201. $sShortAlias = '';
  202. }
  203. if ($this->IsValidField($sClass, 'id'))
  204. {
  205. $aSampleRow[$sShortAlias.'id'] = $this->GetSampleKey($aRow[$sAlias]);
  206. }
  207. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  208. {
  209. if ($this->IsValidField($sClass, $sAttCode, $oAttDef))
  210. {
  211. $aSampleRow[$sShortAlias.$sAttCode] = $this->GetSampleData($aRow[$sAlias], $sAttCode);
  212. }
  213. }
  214. }
  215. $aSampleData[] = $aSampleRow;
  216. }
  217. $sJSSampleData = json_encode($aSampleData);
  218. $aLabels = array(
  219. 'preview_header' => Dict::S('Core:BulkExport:DragAndDropHelp'),
  220. 'empty_preview' => Dict::S('Core:BulkExport:EmptyPreview'),
  221. 'columns_order' => Dict::S('Core:BulkExport:ColumnsOrder'),
  222. 'columns_selection' => Dict::S('Core:BulkExport:AvailableColumnsFrom_Class'),
  223. 'check_all' => Dict::S('Core:BulkExport:CheckAll'),
  224. 'uncheck_all' => Dict::S('Core:BulkExport:UncheckAll'),
  225. 'no_field_selected' => Dict::S('Core:BulkExport:NoFieldSelected'),
  226. );
  227. $sJSLabels = json_encode($aLabels);
  228. $oP->add_ready_script(
  229. <<<EOF
  230. $('#$sWidgetId').tabularfieldsselector({fields: $JSAllFields, value_holder: '#tabular_fields', advanced_holder: '#tabular_advanced', sample_data: $sJSSampleData, total_count: $iCount, preview_limit: $iPreviewLimit, labels: $sJSLabels });
  231. EOF
  232. );
  233. }
  234. static public function SortOnLabel($aItem1, $aItem2)
  235. {
  236. return strcmp($aItem1['label'], $aItem2['label']);
  237. }
  238. /**
  239. * Tells if the specified field can be exported
  240. * @param unknown $sClass
  241. * @param unknown $sAttCode
  242. * @param AttributeDefinition $oAttDef Can be null when $sAttCode == 'id'
  243. * @return boolean
  244. */
  245. protected function IsValidField($sClass, $sAttCode, $oAttDef = null)
  246. {
  247. if ($sAttCode == 'id') return true;
  248. if ($oAttDef instanceof AttributeLinkedSet) return false;
  249. return true; //$oAttDef->IsScalar();
  250. }
  251. protected function GetSampleData($oObj, $sAttCode)
  252. {
  253. if ($oObj == null) return '';
  254. return $oObj->GetEditValue($sAttCode);
  255. }
  256. protected function GetSampleKey($oObj)
  257. {
  258. if ($oObj == null) return '';
  259. return $oObj->GetKey();
  260. }
  261. public function ReadParameters()
  262. {
  263. parent::ReadParameters();
  264. $sQueryId = utils::ReadParam('query', null, true);
  265. $sFields = utils::ReadParam('fields', null, true, 'raw_data');
  266. if ((($sFields === null) || ($sFields === '')) && ($sQueryId === null))
  267. {
  268. throw new BulkExportMissingParameterException('fields');
  269. }
  270. else if(($sQueryId !== null) && ($sQueryId !== null))
  271. {
  272. $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId));
  273. $oQueries = new DBObjectSet($oSearch);
  274. if ($oQueries->Count() > 0)
  275. {
  276. $oQuery = $oQueries->Fetch();
  277. if (($sFields === null) || ($sFields === ''))
  278. {
  279. // No 'fields' parameter supplied, take the fields from the query phrasebook definition
  280. $sFields = trim($oQuery->Get('fields'));
  281. if ($sFields === '')
  282. {
  283. throw new BulkExportMissingParameterException('fields');
  284. }
  285. }
  286. }
  287. else
  288. {
  289. throw BulkExportException('Invalid value for the parameter: query. There is no Query Phrasebook with id = '.$sQueryId, Dict::Format('Core:BulkExport:InvalidParameter_Query', $sQueryId));
  290. }
  291. }
  292. // Interpret (and check) the list of fields
  293. //
  294. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  295. $aAliases = array_keys($aSelectedClasses);
  296. $aAuthorizedClasses = array();
  297. foreach($aSelectedClasses as $sAlias => $sClassName)
  298. {
  299. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ) == UR_ALLOWED_YES)
  300. {
  301. $aAuthorizedClasses[$sAlias] = $sClassName;
  302. }
  303. }
  304. $aFields = explode(',', $sFields);
  305. $this->aStatusInfo['fields'] = array();
  306. foreach($aFields as $sFieldSpec)
  307. {
  308. // Trim the values since it's natural to write: fields=name, first_name, org_name instead of fields=name,first_name,org_name
  309. $sExtendedAttCode = trim($sFieldSpec);
  310. if (preg_match('/^([^\.]+)\.(.+)$/', $sExtendedAttCode, $aMatches))
  311. {
  312. $sAlias = $aMatches[1];
  313. $sAttCode = $aMatches[2];
  314. }
  315. else
  316. {
  317. $sAlias = reset($aAliases);
  318. $sAttCode = $sExtendedAttCode;
  319. }
  320. if (!array_key_exists($sAlias, $aSelectedClasses))
  321. {
  322. throw new Exception("Invalid alias '$sAlias' for the column '$sExtendedAttCode'. Availables aliases: '".implode("', '", $aAliases)."'");
  323. }
  324. $sClass = $aSelectedClasses[$sAlias];
  325. if (!array_key_exists($sAlias, $aAuthorizedClasses))
  326. {
  327. throw new Exception("You do not have enough permissions to bulk read data of class '$sClass' (alias: $sAlias)");
  328. }
  329. if ($this->bLocalizeOutput)
  330. {
  331. try
  332. {
  333. $sLabel = MetaModel::GetLabel($sClass, $sAttCode);
  334. }
  335. catch (Exception $e)
  336. {
  337. throw new Exception("Wrong field specification '$sFieldSpec': ".$e->getMessage());
  338. }
  339. }
  340. else
  341. {
  342. $sLabel = $sAttCode;
  343. }
  344. if (count($aAuthorizedClasses) > 1)
  345. {
  346. $sColLabel = $sAlias.'.'.$sLabel;
  347. }
  348. else
  349. {
  350. $sColLabel = $sLabel;
  351. }
  352. $this->aStatusInfo['fields'][] = array(
  353. 'sFieldSpec' => $sExtendedAttCode,
  354. 'sAlias' => $sAlias,
  355. 'sClass' => $sClass,
  356. 'sAttCode' => $sAttCode,
  357. 'sLabel' => $sLabel,
  358. 'sColLabel' => $sColLabel
  359. );
  360. }
  361. }
  362. /**
  363. * Prepare the given object set with the list of fields as read into $this->aStatusInfo['fields']
  364. */
  365. protected function OptimizeColumnLoad(DBObjectSet $oSet)
  366. {
  367. $aColumnsToLoad = array();
  368. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  369. {
  370. $sClass = $aFieldSpec['sClass'];
  371. $sAlias = $aFieldSpec['sAlias'];
  372. $sAttCode = $aFieldSpec['sAttCode'];
  373. if (!array_key_exists($sAlias, $aColumnsToLoad))
  374. {
  375. $aColumnsToLoad[$sAlias] = array();
  376. }
  377. // id is not a real attribute code and, moreover, is always loaded
  378. if ($sAttCode != 'id')
  379. {
  380. // Extended attributes are not recognized by DBObjectSet::OptimizeColumnLoad
  381. if (($iPos = strpos($sAttCode, '->')) === false)
  382. {
  383. $aColumnsToLoad[$sAlias][] = $sAttCode;
  384. $sClass = '???';
  385. }
  386. else
  387. {
  388. $sExtKeyAttCode = substr($sAttCode, 0, $iPos);
  389. $sRemoteAttCode = substr($sAttCode, $iPos + 2);
  390. // Load the external key to avoid an object reload!
  391. $aColumnsToLoad[$sAlias][] = $sExtKeyAttCode;
  392. // Load the external field (if any) to avoid getting the remote object (see DBObject::Get that does the same)
  393. $oExtFieldAtt = MetaModel::FindExternalField($sClass, $sExtKeyAttCode, $sRemoteAttCode);
  394. if (!is_null($oExtFieldAtt))
  395. {
  396. $aColumnsToLoad[$sAlias][] = $oExtFieldAtt->GetCode();
  397. }
  398. }
  399. }
  400. }
  401. // Add "always loaded attributes"
  402. //
  403. $aSelectedClasses = $this->oSearch->GetSelectedClasses();
  404. foreach ($aSelectedClasses as $sAlias => $sClass)
  405. {
  406. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  407. {
  408. if ($oAttDef->AlwaysLoadInTables())
  409. {
  410. $aColumnsToLoad[$sAlias][] = $sAttCode;
  411. }
  412. }
  413. }
  414. $oSet->OptimizeColumnLoad($aColumnsToLoad);
  415. }
  416. }