tabularbulkexport.class.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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:BulkExport:Friendlyname'), 'label' => Dict::S('Core:BulkExport:Friendlyname'), '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:BulkExport:Friendlyname'), 'label' => $sShortAlias.Dict::S('Core:BulkExport:Friendlyname')),
  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. /**
  252. * Tells if the specified field is part of the "advanced" fields
  253. * @param unknown $sClass
  254. * @param unknown $sAttCode
  255. * @param AttributeDefinition $oAttDef Can be null when $sAttCode == 'id'
  256. * @return boolean
  257. */
  258. protected function IsAdvancedValidField($sClass, $sAttCode, $oAttDef = null)
  259. {
  260. return (($sAttCode == 'id') || ($oAttDef instanceof AttributeExternalKey));
  261. }
  262. protected function GetSampleData(DBObject $oObj, $sAttCode)
  263. {
  264. if ($oObj == null) return '';
  265. return $oObj->GetEditValue($sAttCode);
  266. }
  267. protected function GetSampleKey(DBObject $oObj)
  268. {
  269. if ($oObj == null) return '';
  270. return $oObj->GetKey();
  271. }
  272. public function ReadParameters()
  273. {
  274. parent::ReadParameters();
  275. $sQueryId = utils::ReadParam('query', null, true);
  276. $sFields = utils::ReadParam('fields', null, true, 'raw_data');
  277. if ((($sFields === null) || ($sFields === '')) && ($sQueryId === null))
  278. {
  279. throw new BulkExportMissingParameterException('fields');
  280. }
  281. else if(($sQueryId !== null) && ($sQueryId !== null))
  282. {
  283. $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId));
  284. $oQueries = new DBObjectSet($oSearch);
  285. if ($oQueries->Count() > 0)
  286. {
  287. $oQuery = $oQueries->Fetch();
  288. $sFields = trim($oQuery->Get('fields'));
  289. if ($sFields === '')
  290. {
  291. throw new BulkExportMissingParameterException('fields');
  292. }
  293. }
  294. else
  295. {
  296. throw BulkExportException('Invalid value for the parameter: query. There is no Query Phrasebook with id = '.$sQueryId, Dict::Format('Core:BulkExport:InvalidParameter_Query', $sQueryId));
  297. }
  298. }
  299. $this->aStatusInfo['fields'] = explode(',', $sFields);
  300. }
  301. }