Location: PHPKode > scripts > DABAJAX Toolkit > dabajax-toolkit/AjaxControlFiller.class.php
<?php
   require_once( "AjaxQueries.class.php" );
   require_once( "AjaxFormat.class.php" );
   require_once( "AjaxFilter.class.php" );      
   
   /**
     * AjaxControlFiller - this is the main class that arranges the SQL statement with filters,
     * builds the result, and treats options.This class should be used by a controller that receives
     * the data from client and aplies json enconding and decoding.
   */
   class AjaxControlFiller {
      private $oracon  = null;
      private $ajaxf   = null;
      private $options = array();
      private $errors  = array();
      private $optionValidator = null;
      private $lang = 'en';
      
      private $babel = array( 'pt', 'en', 'br', 'de', 'it' );
      
      function __construct( $con, $lang = 'en' ) {
         array_push( $this->errors, "error" );
         $this->oracon = $con;
         
         $this->lang = $lang;
         
         $this->babel['en'] = array();
         $this->babel['en'][0] = "Invalid Query";
         $this->babel['en'][1] = "Invalid SQL statement";
         $this->babel['en'][2] = "Query could not be performed. Check for SQL errors.";
         $this->babel['en'][3] = "There is no values";

         $this->babel['br'] = array();
         $this->babel['br'][0] = "Consulta invalida";
         $this->babel['br'][1] = "Comando SQL invalido";
         $this->babel['br'][2] = "A consulta nao pode ser executada. Verifique erros SQL.";
         $this->babel['br'][3] = "Não foram encontrados resultados";
         
      }

      public function setOptionValidator( $o ) {
         $this->optionValidator = $o;
      }
      
      public function getErrors() {
         return $this->errors;
      }
      
      public function setAjaxf( $ajaxFilter ) {
         $this->ajaxf = $ajaxFilter;
      }
      
      public function setOptions( $options ) {
         if ( is_array( $options ) )
            $this->options = $options; 
      }
      
      public function getResults( $optionsValidator = "" ) {
      
         $mffiltro = $this->ajaxf;
         $ajaxq    = new AjaxFormat();
         
			// validations
			if ( is_object( $this->optionValidator ) ) {
			   if ( !$this->optionValidator->isValid( $this->options ) ) {
			      array_push( $this->errors, $this->optionValidator->getMessage() );
			      return false;
			   }
			} 
         
         // retrieving the filters 
         $criterias = array();
         foreach( $mffiltro->filters as $filter ) {
	         $vfilter = explode( ";@;", $filter );
	         $name  = $vfilter[ 0 ];
	         $value = $vfilter[ 1 ];
	         $type  = $vfilter[ 2 ];
	         
	         if ( $type >= AjaxFormat::$CONTAINS && $type <= AjaxFormat::$ENDS_WITH )
	            array_push( $criterias, $name . " LIKE " . $ajaxq->formatValue( $value, $type ) ); 
	         else
	            array_push( $criterias, $name . " = " . $ajaxq->formatValue( $value, $type ) );
         }

         $crit = implode( " AND ", $criterias );

         // building SQL
         $sql = $mffiltro->sql;
         $vsql = explode(":", $sql);
         if ( count( $vsql ) != 2 ) {
	         array_push( $this->errors, $this->babel[ $this->lang ][ 0 ] );
	         return false;
         }

         // retrieving a query if it would be valid 
         $ajaxq = new AjaxQueries();
         $xsql  = $ajaxq->getQuery( $vsql[ 0 ], $vsql[ 1 ] );
         if ( count( $criterias ) > 0  )
         
            if ( strpos( strtoupper( $xsql ), "WHERE" ) === false )
               $xsql .= " WHERE " . $crit;
            else
               $xsql .= " AND " . $crit;

			if ( $mffiltro->rownum == 1 ) {
			   if ( count( $criterias ) > 0 )
			      $xsql .= " AND ROWNUM < 2 ";
			   else 
			      $xsql .= " WHERE ROWNUM < 2 ";
			}

		// querying database 
         $stmt = @ociparse( $this->oracon, $xsql );
         if ( !$stmt ) {
            array_push( $this->errors, $this->babel[ $this->lang ][ 1 ] );
            return false;
         }
			
         $exec = @ociexecute( $stmt );
         if (!$exec ) {
	         array_push( $this->errors, $this->babel[ $this->lang ][ 2 ] );
	         return false;
         }

         if ( ocifetchstatement( $stmt, $reg ) ) {
		      // building the result 
	         $r = array();
            foreach( $mffiltro->results as $result ) {
              array_push( $r, $reg[ strtoupper( $result ) ][ 0 ] );	
            }
            return $r;
	
         } else {
            array_push( $this->errors, $this->babel[ $this->lang ][ 3 ] );
            return false;
         }
     }
  }
?>
Return current item: DABAJAX Toolkit