query.class.inc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // Copyright (C) 2010-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. * Persistent class Event and derived
  20. * Application internal events
  21. * There is also a file log
  22. *
  23. * @copyright Copyright (C) 2010-2015 Combodo SARL
  24. * @license http://opensource.org/licenses/AGPL-3.0
  25. */
  26. abstract class Query extends cmdbAbstractObject
  27. {
  28. public static function Init()
  29. {
  30. $aParams = array
  31. (
  32. "category" => "core/cmdb,view_in_gui,application",
  33. "key_type" => "autoincrement",
  34. "name_attcode" => "name",
  35. "state_attcode" => "",
  36. "reconc_keys" => array(),
  37. "db_table" => "priv_query",
  38. "db_key_field" => "id",
  39. "db_finalclass_field" => "realclass",
  40. "display_template" => "",
  41. );
  42. MetaModel::Init_Params($aParams);
  43. //MetaModel::Init_InheritAttributes();
  44. MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  45. MetaModel::Init_AddAttribute(new AttributeText("description", array("allowed_values"=>null, "sql"=>"description", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  46. MetaModel::Init_AddAttribute(new AttributeText("fields", array("allowed_values"=>null, "sql"=>"fields", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
  47. // Display lists
  48. MetaModel::Init_SetZListItems('details', array('name', 'description', 'fields')); // Attributes to be displayed for the complete details
  49. MetaModel::Init_SetZListItems('list', array('description')); // Attributes to be displayed for a list
  50. // Search criteria
  51. MetaModel::Init_SetZListItems('standard_search', array('name', 'description', 'fields')); // Criteria of the std search form
  52. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  53. }
  54. }
  55. class QueryOQL extends Query
  56. {
  57. public static function Init()
  58. {
  59. $aParams = array
  60. (
  61. "category" => "core/cmdb,view_in_gui,application",
  62. "key_type" => "autoincrement",
  63. "name_attcode" => "name",
  64. "state_attcode" => "",
  65. "reconc_keys" => array(),
  66. "db_table" => "priv_query_oql",
  67. "db_key_field" => "id",
  68. "db_finalclass_field" => "",
  69. "display_template" => "",
  70. );
  71. MetaModel::Init_Params($aParams);
  72. MetaModel::Init_InheritAttributes();
  73. MetaModel::Init_AddAttribute(new AttributeOQL("oql", array("allowed_values"=>null, "sql"=>"oql", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  74. // Display lists
  75. MetaModel::Init_SetZListItems('details', array('name', 'description', 'oql', 'fields')); // Attributes to be displayed for the complete details
  76. MetaModel::Init_SetZListItems('list', array('description')); // Attributes to be displayed for a list
  77. // Search criteria
  78. MetaModel::Init_SetZListItems('standard_search', array('name', 'description', 'fields', 'oql')); // Criteria of the std search form
  79. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  80. }
  81. function DisplayBareProperties(WebPage $oPage, $bEditMode = false, $sPrefix = '', $aExtraParams = array())
  82. {
  83. $aFieldsMap = parent::DisplayBareProperties($oPage, $bEditMode, $sPrefix, $aExtraParams);
  84. if (!$bEditMode)
  85. {
  86. $sFields = trim($this->Get('fields'));
  87. $bExportV1Recommended = ($sFields == '');
  88. if ($bExportV1Recommended)
  89. {
  90. $oFieldAttDef = MetaModel::GetAttributeDef('QueryOQL', 'fields');
  91. $oPage->add('<div class="message message_error" style="padding-left: 30px;"><div style="padding: 10px;">'.Dict::Format('UI:Query:UrlV1', $oFieldAttDef->GetLabel()).'</div></div>');
  92. $sUrl = utils::GetAbsoluteUrlAppRoot().'webservices/export.php?format=spreadsheet&login_mode=basic&query='.$this->GetKey();
  93. }
  94. else
  95. {
  96. $sUrl = utils::GetAbsoluteUrlAppRoot().'webservices/export-v2.php?format=spreadsheet&login_mode=basic&date_format='.urlencode((string)AttributeDateTime::GetFormat()).'&query='.$this->GetKey();
  97. }
  98. $sOql = $this->Get('oql');
  99. $sMessage = null;
  100. try
  101. {
  102. $oSearch = DBObjectSearch::FromOQL($sOql);
  103. $aParameters = $oSearch->GetQueryParams();
  104. foreach($aParameters as $sParam => $val)
  105. {
  106. $sUrl .= '&arg_'.$sParam.'=["'.$sParam.'"]';
  107. }
  108. $oPage->p(Dict::S('UI:Query:UrlForExcel').':<br/><textarea cols="80" rows="3" READONLY>'.$sUrl.'</textarea>');
  109. if (count($aParameters) == 0)
  110. {
  111. $oBlock = new DisplayBlock($oSearch, 'list');
  112. $aExtraParams = array(
  113. //'menu' => $sShowMenu,
  114. 'table_id' => 'query_preview_'.$this->getKey(),
  115. );
  116. $sBlockId = 'block_query_preview_'.$this->GetKey(); // make a unique id (edition occuring in the same DOM)
  117. $oBlock->Display($oPage, $sBlockId, $aExtraParams);
  118. }
  119. }
  120. catch (OQLException $e)
  121. {
  122. $sMessage = '<div class="message message_error" style="padding-left: 30px;"><div style="padding: 10px;">'.Dict::Format('UI:RunQuery:Error', $e->getHtmlDesc()).'</div></div>';
  123. $oPage->p($sMessage);
  124. }
  125. }
  126. return $aFieldsMap;
  127. }
  128. }
  129. ?>