Subversion Repositories php_library

Rev

Rev 14 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

   class QueryParameters {
      protected $parameter;
      protected $prompt;
      protected $query;
      protected $displayQuery;
      protected $value;
      private $parametersDelimiter = '++';

      function __constructor ( $parameter = '', $prompt = '', $query = '' ) {
         $this->parameter = $parameter;
         $this->prompt = $prompt;
         $this->query = $query;
      }
      
      function stringToParameter( $string ) {
         $temp = explode( $this->parametersDelimiter, $string );
         $this->parameter = $temp[0];
         $this->prompt = $temp[1];
         $this->query = $temp[2];
         $this->displayQuery = $temp[3];
      }
      
      function name ( $newParameter = '' ) {
         $oldParameter = $this->parameter;
         if ($newParameter) {
            $this->parameter = $newParameter;
         }
         return $oldParameter;
      }
      
      function value ( $newValue = '' ) {
         $oldValue = $this->value;
         if ($newValue) {
            $this->value = $newValue;
         }
         return $oldValue;
      }

      function prompt ( $newPrompt = '' ) {
         $oldPrompt = $this->prompt;
         if ($newPrompt) {
            $this->prompt = $newPrompt;
         }
         return $oldPrompt;
      }
   
      function query ( $newQuery = '' ) {
         $oldQuery = $this->query;
         if ($newQuery) {
            $this->query = $newQuery;
         }
         return $oldQuery;
      }
      
      function displayQuery ( $newQuery = '' ) {
         $oldQuery = $this->displayQuery;
         if ($newQuery) {
            $this->displayQuery = $newQuery;
         }
         return $oldQuery;
      }
      
      function toHTML () {
         $result = "<td>$this->prompt</td><td>";
         if ( $this->query ) {
            $result .= "<select name='report_param_$this->parameter'>" . queryToSelect( $this->query ) . '</select>';
         } else {
            $result .= "<input type='text' name='$this->parameter'>";
         }
         $result .= '</td>';
         return $result;
      }
   } // class QueryParameters

   class Report {
      protected $name;
      protected $title;
      protected $query;
      protected $parameters = array();
      private $tableName = 'report';
      private $nameField = 'name';
      private $queryField = 'query';
      private $parametersField = 'parameters';
      private $idField = 'report_id';

      
      static public function listAllReports( $tableName = 'report', $nameField = 'name', $idField = 'report_id' ) {
         return queryToSelect( "select $idField, $nameField from $tableName" );
      }

      function __constructor ($name = '', $title = '', $query = '', $parameters = array() ) {
         $this->name = $name;
         $this->title = $title;
         $this->query = $query;
         $this->parameters = $parameters;
      }
      
      function loadFromDatabase ( $id ) {
         $sql = "select $this->nameField, $this->queryField, $this->parametersField from $this->tableName where $this->idField = $id";
         $result = queryDatabaseExtended( $sql );
         $result = $result['data'][0];
         $this->name = $result[$this->nameField];
         $this->title = $result[$this->nameField];
         $this->query = $result[$this->queryField];
         $parameters = $result[$this->parametersField];
         $parameters = explode("\n", $parameters);
         foreach ( $parameters as $thisParam ) {
            if (! $thisParam ) {
               continue;
            }
            $temp = new QueryParameters();
            $temp->stringToParameter($thisParam);
            $this->parameters[] = $temp;
         }
      }

      function name ( $newName = '' ) {
         $oldName = $this->name;
         if ($newName) {
            $this->name = $newName;
         }
         return $oldName;
      }

      function query ( $newQuery = '' ) {
         $oldQuery = $this->query;
         if ($newQuery) {
            $this->query = $newQuery;
         }
         return $oldQuery;
      }

      function parameters ( $newParameters = array() ) {
         $oldParameters = $this->parameters;
         if ($newParameters) {
            $this->parameters = $newParameters;
         }
         return $oldParameters;
      }

      function addParameter ( $newParameter = array() ) {
         $this->parameters[] = $newParameter;
      }
      
      function run( $parameters = '') {
         $conditions = array();
         if ( ! $parameters ) { // try to get them from the POST line
            foreach ( $this->parameters as $parameter ) {
               if ($_POST['report_param_' . $parameter->name()]) {
                  $parameter->value($_POST['report_param_' . $parameter->name()]);
               }
            }
         }
         // print "<pre>" . print_r( $this ); print "</pre>";
         foreach ( $this->parameters as $parameter ) {
            $toFind = '<' . $parameter->name() . '>';
            $this->query = preg_replace( "/$toFind/", $parameter->value(), $this->query );
            if ( $parameter->displayQuery() ) {
               //$result = preg_replace( '/<value>/', $parameter->value(), $parameter->displayQuery() );
               $result = queryDatabaseExtended(preg_replace( '/<value>/', $parameter->value(), $parameter->displayQuery() ), false);
               //print "<pre>" . print_r( $result ); print "</pre>";
               $conditions[] = $parameter->name() . ' - '  . $result['data'][0][0];
            } else {
               $conditions[] = $parameter->name() . ' - ' . $parameter->value();
            }
         }
         return '<h1>' . $this->title . '</h1>' . '<h2>' . implode('</h2><h2>', $conditions) . '</h2>' . queryToTable($this->query);
      }
      
      function toHTML( ) {
         $result = "<table border='1'>\n";
         $result .= "<tr><td colspan='2'>$this->name</td></tr>";
         foreach ( $this->parameters as $parameter ) {
            $result .= '<tr>' . $parameter->toHTML() . '</tr>';
         }
         return $result . '<table>';
      }

   } // class Report

?>

Generated by GNU Enscript 1.6.5.90.