run_query.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Tools to design OQL queries and test them
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/application.inc.php');
  25. require_once('../application/itopwebpage.class.inc.php');
  26. require_once('../application/startup.inc.php');
  27. require_once('../application/loginwebpage.class.inc.php');
  28. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  29. function ShowExamples($oP, $sExpression)
  30. {
  31. $bUsingExample = false;
  32. $aExamples = array(
  33. 'Pedagogic examples' => array(
  34. "Applications" => "SELECT bizApplication",
  35. "Person having an 'A' in their name" => "SELECT bizPerson AS B WHERE B.name LIKE '%A%'",
  36. "Changes planned on new year's day" => "SELECT bizChangeTicket AS ch WHERE ch.start_date >= '2009-12-31' AND ch.end_date <= '2010-01-01'",
  37. "IPs in a range" => "SELECT bizDevice AS dev WHERE INET_ATON(dev.mgmt_ip) > INET_ATON('10.22.32.33') AND INET_ATON(dev.mgmt_ip) < INET_ATON('10.22.32.40')"
  38. ),
  39. 'Usefull examples' => array(
  40. "NW interfaces of equipment in production for customer 'Demo'" => "SELECT bizInterface AS if JOIN bizDevice AS dev ON if.device_id = dev.id WHERE if.status = 'production' AND dev.status = 'production' AND dev.org_name = 'Demo' AND if.physical_type = 'ethernet'",
  41. "My tickets" => "SELECT bizIncidentTicket AS i WHERE i.agent_id = :current_contact_id",
  42. "People being owner of an active ticket" => "SELECT bizPerson AS p JOIN bizIncidentTicket AS i ON i.agent_id = p.id WHERE i.ticket_status != 'Closed'",
  43. "Contracts terminating in the next thirty days" => "SELECT bizContract AS c WHERE c.end_prod > NOW() AND c.end_prod < DATE_ADD(NOW(), INTERVAL 30 DAY)",
  44. "Orphan tickets (opened one hour ago, still not assigned)" => "SELECT bizIncidentTicket AS i WHERE i.start_date < DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND i.ticket_status = 'New'",
  45. "Long lasting incidents (duration > 8 hours)" => "SELECT bizIncidentTicket AS i WHERE i.end_date > DATE_ADD(i.start_date, INTERVAL 8 HOUR)",
  46. ),
  47. );
  48. $aDisplayData = array();
  49. foreach($aExamples as $sTopic => $aQueries)
  50. {
  51. foreach($aQueries as $sDescription => $sOql)
  52. {
  53. $sHighlight = '';
  54. $sDisable = '';
  55. if ($sOql == $sExpression)
  56. {
  57. // this one is currently being tested, highlight it
  58. $sHighlight = "background-color:yellow;";
  59. $sDisable = 'disabled';
  60. // and remember we are testing a query of the list
  61. $bUsingExample = true;
  62. }
  63. //$aDisplayData[$sTopic][] = array(
  64. $aDisplayData[Dict::S('UI:RunQuery:QueryExamples')][] = array(
  65. 'desc' => "<div style=\"$sHighlight\">".htmlentities($sDescription)."</div>",
  66. 'oql' => "<div style=\"$sHighlight\">".htmlentities($sOql)."</div>",
  67. 'go' => "<form method=\"get\"><input type=\"hidden\" name=\"expression\" value=\"$sOql\"><input type=\"submit\" value=\"".Dict::S('UI:Button:Test')."\" $sDisable></form>\n",
  68. );
  69. }
  70. }
  71. $aDisplayConfig = array();
  72. $aDisplayConfig['desc'] = array('label' => Dict::S('UI:RunQuery:HeaderPurpose'), 'description' => Dict::S('UI:RunQuery:HeaderPurpose+'));
  73. $aDisplayConfig['oql'] = array('label' => Dict::S('UI:RunQuery:HeaderOQLExpression'), 'description' => Dict::S('UI:RunQuery:HeaderOQLExpression+'));
  74. $aDisplayConfig['go'] = array('label' => '', 'description' => '');
  75. foreach ($aDisplayData as $sTopic => $aQueriesDisplayData)
  76. {
  77. $bShowOpened = $bUsingExample;
  78. $oP->StartCollapsibleSection($sTopic, $bShowOpened);
  79. $oP->table($aDisplayConfig, $aQueriesDisplayData);
  80. $oP->EndCollapsibleSection();
  81. }
  82. }
  83. $sOperation = utils::ReadParam('operation', 'menu');
  84. $oContext = new UserContext();
  85. $oAppContext = new ApplicationContext();
  86. $iActiveNodeId = utils::ReadParam('menu', -1);
  87. $currentOrganization = utils::ReadParam('org_id', '');
  88. $oP = new iTopWebPage(Dict::S('UI:RunQuery:Title'), $currentOrganization);
  89. // Main program
  90. $sExpression = utils::ReadParam('expression', '');
  91. $sEncoding = utils::ReadParam('encoding', 'oql');
  92. ShowExamples($oP, $sExpression);
  93. try
  94. {
  95. if ($sEncoding == 'crypted')
  96. {
  97. // Translate $sExpression into a oql expression
  98. $sClearText = base64_decode($sExpression);
  99. echo "<strong>FYI: '$sClearText'</strong><br/>\n";
  100. $oFilter = DBObjectSearch::unserialize($sExpression);
  101. $sExpression = $oFilter->ToOQL();
  102. exit;
  103. }
  104. else
  105. {
  106. // leave $sExpression as is
  107. }
  108. $oP->add("<form method=\"get\">\n");
  109. $oP->add(Dict::S('UI:RunQuery:ExpressionToEvaluate')."<br/>\n");
  110. $oP->add("<textarea cols=\"120\" rows=\"8\" name=\"expression\">$sExpression</textarea>\n");
  111. $oP->add("<input type=\"submit\" value=\"".Dict::S('UI:Button:Evaluate')."\">\n");
  112. $oP->add("</form>\n");
  113. if (!empty($sExpression))
  114. {
  115. $oFilter = DBObjectSearch::FromOQL($sExpression);
  116. if ($oFilter)
  117. {
  118. $oP->add("<h3>Query results</h3>\n");
  119. $oResultBlock = new DisplayBlock($oFilter, 'list', false);
  120. $oResultBlock->Display($oP, 1);
  121. $oP->p('');
  122. $oP->StartCollapsibleSection(Dict::S('UI:RunQuery:MoreInfo'), false);
  123. $oP->p(Dict::S('UI:RunQuery:DevelopedQuery').$oFilter->ToOQL());
  124. $oP->p(Dict::S('UI:RunQuery:SerializedFilter').$oFilter->serialize());
  125. $oP->EndCollapsibleSection();
  126. }
  127. }
  128. }
  129. catch(CoreException $e)
  130. {
  131. $oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getHtmlDesc()).'</b>');
  132. }
  133. catch(Exception $e)
  134. {
  135. $oP->p('<b>'.Dict::Format('UI:RunQuery:Error', $e->getMessage()).'</b>');
  136. }
  137. $oP->output();
  138. ?>