[ PHPXref.com ] [ Generated: Sun Jul 20 19:12:48 2008 ] [ OSC 2.0.5 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> QueryView.php (source)

   1  <?php
   2  /*******************************************************************************
   3   *
   4   *  filename    : QueryView.php
   5   *  last change : 2003-06-09
   6   *
   7   *  http://osc.sourceforge.net
   8   *
   9   *  This product is based upon work previously done by Infocentral (infocentral.org)
  10   *  on their PHP version Church Management Software that they discontinued
  11   *  and we have taken over.  We continue to improve and build upon this product
  12   *  in the direction of excellence.
  13   * 
  14   *  OpenSourceChurch (OSC) is free software; you can redistribute it and/or modify
  15   *  it under the terms of the GNU General Public License as published by
  16   *  the Free Software Foundation; either version 2 of the License, or
  17   *  (at your option) any later version.
  18   * 
  19   *  Any changes to the software must be submitted back to the OpenSourceChurch project
  20   *  for review and possible inclusion.
  21   *
  22   *  Copyright 2001, 2002 Deane Barker
  23   ******************************************************************************/
  24  
  25  //Include the function library
  26  require  "Include/Config.php";
  27  require  "Include/Functions.php";
  28  
  29  //Set the page title
  30  $sPageTitle = gettext("Query View");
  31  
  32  //Get the QueryID from the querystring
  33  $iQueryID = FilterInput($_GET["QueryID"],'int');
  34  
  35  $aFinanceQueries = explode(',', $aFinanceQueries);
  36  
  37  if (!$_SESSION['bFinance'] && in_array($iQueryID,$aFinanceQueries))
  38  {
  39      Redirect("Menu.php");
  40      exit;
  41  }
  42  
  43  //Include the header
  44  require  "Include/Header.php";
  45  
  46  //Get the query information
  47  $sSQL = "SELECT * FROM query_qry WHERE qry_ID = " . $iQueryID . " AND chu_Church_ID=" . $_SESSION['iChurchID'];
  48  $rsSQL = RunQuery($sSQL);
  49  extract(mysql_fetch_array($rsSQL));
  50  
  51  //Get the parameters for this query
  52  $sSQL = "SELECT * FROM queryparameters_qrp WHERE qrp_qry_ID = " . $iQueryID . " AND chu_Church_ID=" . $_SESSION['iChurchID'];
  53  $rsParameters = RunQuery($sSQL);
  54  
  55  //If the form was submitted or there are no parameters, run the query
  56  if (isset($_POST["Submit"]) || mysql_num_rows($rsParameters) == 0)
  57  {
  58      //Check that all validation rules were followed
  59      ValidateInput();
  60  
  61      //Any errors?
  62      if (count($aErrorText) == 0)
  63      {
  64          //No errors; process the SQL, run the query, and display the results
  65          ProcessSQL();
  66          DoQuery();
  67      }
  68      else
  69      {
  70          //Yes, there were errors; re-display the parameter form (the DisplayParameterForm function will
  71          //pick up and display any error messages)
  72          DisplayQueryInfo();
  73          DisplayParameterForm();
  74      }
  75  }
  76  else
  77  {
  78      //Display the parameter form
  79      DisplayQueryInfo();
  80      DisplayParameterForm();
  81  }
  82  
  83  
  84  //Loops through all the parameters and ensures validation rules have been followed
  85  function ValidateInput()
  86  {
  87      global $rsParameters;
  88      global $_POST;
  89      global $vPOST;
  90      global $aErrorText;
  91  
  92      //Initialize the validated post array, error text array, and the error flag
  93       $vPOST = array();
  94      $aErrorText = array();
  95      $bError = false;
  96  
  97      //Are there any parameters to loop through?
  98      if (mysql_num_rows($rsParameters)) { mysql_data_seek($rsParameters,0); }
  99      while ($aRow = mysql_fetch_array($rsParameters))
 100      {
 101          extract($aRow);
 102  
 103          //Is the value required?
 104          if ($qrp_Required && strlen(trim($_POST[$qrp_Alias])) < 1)
 105          {
 106              $bError = true;
 107              $aErrorText[$qrp_Alias] = gettext("This value is required.");
 108          }
 109  
 110          //Assuming there was no error above...
 111          else
 112          {
 113              //Validate differently depending on the contents of the qrp_Validation field
 114              switch ($qrp_Validation)
 115              {
 116                  //Numeric validation
 117                  case "n":
 118  
 119                      //Is it a number?
 120                      if (!is_numeric($_POST[$qrp_Alias]))
 121                      {
 122                          $bError = true;
 123                          $aErrorText[$qrp_Alias] = gettext("This value must be numeric.");
 124                      }
 125                      else
 126                      {
 127                          //Is it more than the minimum?
 128                          if ($_POST[$qrp_Alias] < $qrp_NumericMin)
 129                          {
 130                              $bError = true;
 131                              $aErrorText[$qrp_Alias] = gettext("This value must be at least ") . $qrp_NumericMin;
 132                          }
 133                          //Is it less than the maximum?
 134                          elseif ($_POST[$qrp_Alias] > $qrp_NumericMax)
 135                          {
 136                              $bError = true;
 137                              $aErrorText[$qrp_Alias] = gettext("This value cannot be more than ") . $qrp_NumericMax;
 138                          }
 139                      }
 140  
 141                      $vPOST[$qrp_Alias] = FilterInput($_POST[$qrp_Alias],'int');
 142                      break;
 143  
 144                  //Alpha validation
 145                  case "a":
 146  
 147                      //Is the length less than the maximum?
 148                      if (strlen($_POST[$qrp_Alias]) > $qrp_AlphaMaxLength)
 149                      {
 150                          $bError = true;
 151                          $aErrorText[$qrp_Alias] = gettext("This value cannot be more than ") . $qrp_AlphaMaxLength . gettext(" characters long");
 152                      }
 153                      //is the length more than the minimum?
 154                      elseif (strlen($_POST[$qrp_Alias]) < $qrp_AlphaMinLength)
 155                      {
 156                          $bError = true;
 157                          $aErrorText[$qrp_Alias] = gettext("This value cannot be less than ") . $qrp_AlphaMinLength . gettext(" characters long");
 158                      }
 159  
 160                      $vPOST[$qrp_Alias] = FilterInput($_POST[$qrp_Alias]);
 161                      break;
 162  
 163                  default:
 164                      $vPOST[$qrp_Alias] = FilterInput($_POST[$qrp_Alias]);
 165                      break;
 166              }
 167          }
 168      }
 169  }
 170  
 171  
 172  //Loops through the list of parameters and replaces their alias in the SQL with the value given for the parameter
 173  function ProcessSQL()
 174  {
 175      global $vPOST;
 176      global $qry_SQL;
 177      global $rsParameters;
 178  
 179      //Loop through the list of parameters
 180      if (mysql_num_rows($rsParameters)) {mysql_data_seek($rsParameters,0); }
 181      while ($aRow = mysql_fetch_array($rsParameters))
 182      {
 183          extract($aRow);
 184  
 185          //Debugging code
 186          //echo "--" . $qry_SQL . "<br>--" . "~" . $qrp_Alias . "~" . "<br>--" . $vPOST[$qrp_Alias] . "<p>";
 187  
 188          //Replace the placeholder with the parameter value
 189          $qry_SQL = str_replace("~" . $qrp_Alias . "~",$vPOST[$qrp_Alias],$qry_SQL);
 190      }
 191  }
 192  
 193  
 194  //Checks if a count is to be displayed, and displays it if required
 195  function DisplayRecordCount()
 196  {
 197      global $qry_Count;
 198      global $rsQueryResults;
 199  
 200      //Are we supposed to display a count for this query?
 201      if ($qry_Count == 1)
 202      {
 203          //Display the count of the recordset
 204          echo "<p align=\"center\">";
 205          echo mysql_num_rows($rsQueryResults) . gettext(" record(s) returned");
 206          echo "</p>";
 207      }
 208  }
 209  
 210  
 211  //Runs the parameterized SQL and display the results
 212  function DoQuery()
 213  {
 214      global $cnInfoCentral;
 215      global $aRowClass;
 216      global $rsQueryResults;
 217      global $qry_SQL;
 218      global $iQueryID;
 219  
 220      //Run the SQL
 221      $rsQueryResults = RunQuery($qry_SQL);
 222  
 223      //Set the first row style
 224      $sRowClass = "RowColorA";
 225  
 226      //Check for a count display
 227      DisplayRecordCount();
 228  
 229      //Start the table and the header row
 230      echo "<table align=\"center\" cellpadding=\"5\" cellspacing=\"0\">";
 231      echo "<tr class=\"TableHeader\">";
 232  
 233      //Loop through the fields and write the header row
 234      for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
 235      {
 236          //If this field is called "AddToCart", don't display this field...
 237          if (mysql_field_name($rsQueryResults,$iCount) != "AddToCart")
 238          {
 239              echo "<td>" . mysql_field_name($rsQueryResults,$iCount) . "</td>";
 240          }
 241      }
 242  
 243      //Close the header row
 244      echo "</tr>";
 245  
 246      //Loop through the recordset
 247      while($aRow = mysql_fetch_array($rsQueryResults))
 248      {
 249          //Alternate the background color of the row
 250          $sRowClass = AlternateRowStyle($sRowClass);
 251  
 252          //Begin the row
 253          echo "<tr class=\"" . $sRowClass . "\">";
 254  
 255          //Loop through the fields and write each one
 256          for ($iCount = 0; $iCount < mysql_num_fields($rsQueryResults); $iCount++)
 257          {
 258              //If this field is called "AddToCart", add this to the hidden form field...
 259              if (mysql_field_name($rsQueryResults,$iCount) == "AddToCart")
 260              {
 261                  $aHiddenFormField[] = $aRow[$iCount];
 262              }
 263              //...otherwise just render the field
 264              else
 265              {
 266                  //Write the actual value of this row
 267                  echo "<td>" . $aRow[$iCount] . "&nbsp;</td>";
 268              }
 269          }
 270  
 271          //Close the row
 272          echo "</tr>";
 273      }
 274  
 275      //Close the table and allow a link to run the query again
 276      echo "</table>";
 277      echo "<p align=\"center\">";
 278  
 279      if (is_array($aHiddenFormField) && count($aHiddenFormField) > 0)
 280      {
 281          ?>
 282          <form method="post" action="CartView.php"><p align="center">
 283              <input type="hidden" value="<?php echo join(",",$aHiddenFormField); ?>" name="BulkAddToCart">
 284              <input type="submit" class="icButton" name="AddToCartSubmit" value="<?php echo gettext("Add Results To Cart");?>">&nbsp;
 285              <input type="submit" class="icButton" name="AndToCartSubmit" value="<?php echo gettext("Intersect Results With Cart");?>">&nbsp;
 286              <input type="submit" class="icButton" name="NotToCartSubmit" value="<?php echo gettext("Remove Results From Cart");?>">
 287          </p></form>
 288          <?php
 289      }
 290  
 291      echo "<p align=\"center\"><a href=\"QueryView.php?QueryID=" . $iQueryID . "\">" . gettext("Run Query Again") . "</a></p>";
 292      echo "<p align=\"center\"><a href=\"QueryList.php\">". gettext("Return to Query Menu") . "</a></p>";
 293  
 294      //Print the SQL to make debugging easier
 295      echo "<br><p class=\"ShadedBox\"><span class=\"SmallText\">" . str_replace(Chr(13),"<br>",htmlspecialchars($qry_SQL)) . "</span></p>";
 296  }
 297  
 298  
 299  //Displays the name and description of the query
 300  function DisplayQueryInfo()
 301  {
 302  
 303      global $rsSQL;
 304      global $qry_Name;
 305      global $qry_Description;
 306  
 307      //Display the information about this query
 308      echo "<p align=\"center\">";
 309      echo "<b>" . $qry_Name . "</b><br>" . $qry_Description;
 310      echo "</p>";
 311  
 312  }
 313  
 314  
 315  //Displays a form to enter values for each parameter, creating INPUT boxes and SELECT drop-downs as necessary
 316  function DisplayParameterForm()
 317  {
 318      global $rsParameters;
 319      global $iQueryID;
 320      global $aErrorText;
 321      global $cnInfoCentral;
 322  
 323      //Start the form and the table
 324      echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "?QueryID=" . $iQueryID . "\">";
 325      echo "<table align=\"center\" cellpadding=\"5\" cellspacing=\"1\" border=\"0\">";
 326  
 327      //Loop through the parameters and display an entry box for each one
 328      if (mysql_num_rows($rsParameters)) {mysql_data_seek($rsParameters,0); }
 329      while ($aRow = mysql_fetch_array($rsParameters))
 330      {
 331          extract($aRow);
 332  
 333          //Begin the row, giving the name of the parameter
 334          echo "<tr>";
 335          echo "<td class=\"LabelColumn\">" . $qrp_Name . ":</td>";
 336  
 337          //Determine the type of parameter we're dealing with
 338          switch ($qrp_Type)
 339          {
 340              //Standard INPUT box
 341              case 0:
 342                  //Begin the table cell, disoplay the INPUT tag, close the table cell
 343                  echo "<td class=\"TextColumn\">";
 344                  echo "<input size=\"" . $qrp_InputBoxSize . "\" name=\"" . $qrp_Alias . "\" type=\"text\" value=\"" . $qrp_Default . "\">";
 345                  echo "</td>";
 346                  break;
 347  
 348              //SELECT box with OPTION tags supplied in the queryparameteroptions_qpo table
 349              case 1:
 350                  //Get the query parameter options for this parameter
 351                  $sSQL = "SELECT * FROM queryparameteroptions_qpo WHERE qpo_qrp_ID = " . $qrp_ID . " AND chu_Church_ID=" . $_SESSION['iChurchID'];
 352                  $rsParameterOptions = RunQuery($sSQL);
 353  
 354                  //Begin the table cell and the SELECT tag
 355                  echo "<td class=\"TextColumn\">";
 356                  echo "<select name=\"" . $qrp_Alias . "\">";
 357  
 358                  //Loop through the parameter options
 359                  while ($ThisRow = mysql_Fetch_array($rsParameterOptions))
 360                  {
 361                      extract($ThisRow);
 362  
 363                      //Display the OPTION tag
 364                      echo "<option value=\"" . $qpo_Value . "\">" . $qpo_Display . "</option>";
 365                  }
 366  
 367                  //Close the SELECT tag and table cell
 368                  echo "</select>";
 369                  echo "</td>";
 370                  break;
 371  
 372              //SELECT box with OPTION tags provided via a SQL query
 373              case 2:
 374                  //Run the SQL to get the options
 375                  $rsParameterOptions = RunQuery($qrp_OptionSQL);
 376  
 377                  echo "<td class=\"TextColumn\">";
 378                  echo "<select name=\"" . $qrp_Alias . "\">";
 379  
 380                  while ($ThisRow = mysql_Fetch_array($rsParameterOptions))
 381                  {
 382                      extract($ThisRow);
 383                      echo "<option value=\"" . $Value . "\">" . $Display . "</option>";
 384                  }
 385  
 386                  //Close the SELECT tag and table cell
 387                  echo "</select>";
 388                  echo "</td>";
 389                  break;
 390          }
 391  
 392          //Display the query description and close the row
 393          echo "<td  valign=\"top\" class=\"SmallText\">" . $qrp_Description . "</td>";
 394          echo "</tr>";
 395  
 396          //If we are re-rendering this form due to a validation error, display the error
 397          if (isset($aErrorText[$qrp_Alias]))
 398          { 
 399              echo "<tr><td colspan=\"3\" style=\"color: red;\">" . $aErrorText[$qrp_Alias] . "</td></tr>";
 400          }
 401          
 402      }
 403  
 404      ?>
 405      
 406      <td colspan="3" align="center">
 407          <br>
 408          <input class="icButton" type="Submit" value="<?php echo gettext("Execute Query"); ?>" name="Submit">
 409      </p>
 410  
 411      </table>
 412  
 413      </form>
 414  
 415      <?php
 416  
 417  }
 418  
 419  require  "Include/Footer.php";
 420  
 421  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]