Location: PHPKode > projects > Quick.Cart - free and simple to use shopping cart script > core/products.php
<?php
class Products
{

  public $aProducts = null;
  public $aProductsPages = null;
  public $aPages = null;
  protected $mData = null;
  protected $aFields = null;
  private static $oInstance = null;

  public static function getInstance( ){  
    if( !isset( self::$oInstance ) ){  
      self::$oInstance = new Products( );  
    }  
    return self::$oInstance;  
  } // end function getInstance

  /**
  * Constructor
  * @return void
  */
  private function __construct( ){
    $this->generateCache( );
  } // end function __construct

  /**
  * Generate cache variables
  * @return void
  */
  public function generateCache( ){

    if( !is_file( DB_PRODUCTS ) )
      return null;

    $this->aFields = $GLOBALS['aProductsFields'];

    $oPage = Pages::getInstance( );
    $oFFS = FlatFilesSerialize::getInstance( );
    
    $aPages = $oFFS->getData( DB_PRODUCTS_PAGES );
    $aData = $oFFS->getData( DB_PRODUCTS );

    if( !is_array( $aData ) || ( is_array( $aData ) && count( $aData ) == 0 ) )
      return null;

    $iStatus = throwStatus( );
    $sLanguageUrl = ( LANGUAGE_IN_URL == true ) ? LANGUAGE.LANGUAGE_SEPARATOR : null;
    $this->aProducts = null;
    $this->aProductsPages = null;

    foreach( $aData as $iKey => $aValue ){
      if( isset( $aValue['iStatus'] ) && $aValue['iStatus'] >= $iStatus && ( !defined( 'CUSTOMER_PAGE' ) || isset( $aPages[$aValue['iProduct']] ) ) ){
        if( !isset( $aValue['mPrice'] ) )
          $aValue['mPrice'] = null;
        $this->aProducts[$aValue['iProduct']] = $aValue;
        $sUrlName = $sLanguageUrl.change2Url( !empty( $this->aProducts[$aValue['iProduct']]['sNameUrl'] ) ? $this->aProducts[$aValue['iProduct']]['sNameUrl'] : $this->aProducts[$aValue['iProduct']]['sName'] );
        if( is_numeric( $sUrlName ) )
          $sUrlName .= '-';
        $this->aProducts[$aValue['iProduct']]['sLinkName'] = '?'.$aValue['iProduct'].','.$sUrlName;
        $this->aProductsPages[$aValue['iProduct']] = isset( $aPages[$aValue['iProduct']] ) ? $aPages[$aValue['iProduct']] : null;
      }
    } // end foreach
  } // end function generateCache


  /**
  * List products
  * @return string
  * @param int $iPage
  * @param int $iList
  * @param array $aProducts
  */
  public function listProducts( $iPage, $iList = null, $aProducts = null ){
    global $config, $lang;

    $oFile = Files::getInstance( );
    $oPage = Pages::getInstance( );
    $content= null;
    $sUrlExt= null;
    $this->aPages = null;

    if( !isset( $aProducts ) ){
      if( isset( $GLOBALS['sPhrase'] ) && !empty( $GLOBALS['sPhrase'] ) ){
        $aProducts = $this->generateProductsSearchListArray( $GLOBALS['sPhrase'] );
        $sUrlExt .= '&amp;sPhrase='.$GLOBALS['sPhrase'];
      }
      else{
        if( DISPLAY_SUBCATEGORY_PRODUCTS === true ){
          // return all pages and subpages
          $oPage->mData = null;
          $aData = $oPage->throwAllChildrens( $iPage );
          if( isset( $aData ) ){
            foreach( $aData as $iValue ){
              $this->aPages[$iValue] = $iValue;
            }
          }
        }
        $this->aPages[$iPage] = $iPage;
        $aProducts = $this->generateProductsListArray( );
      }
    }

    if( isset( $aProducts ) ){

      $sSort = isset( $_GET['sSort'] ) ? $_GET['sSort'] : null;
      if( isset( $sSort ) && !empty( $sSort ) && in_array( $sSort, Array( 'price', 'name' ) ) ){
        $aProducts = $this->sortProducts( $aProducts, $sSort );
        $sUrlExt .= '&amp;sSort='.$sSort;
      }
      else
        $sSort = null;

      $sBasketPage = ( !empty( $config['basket_page'] ) && isset( $oPage->aPages[$config['basket_page']] ) ) ? $oPage->aPages[$config['basket_page']]['sLinkName'] : null;

      $iCount = count( $aProducts );
      if( !isset( $iList ) ){
        $iList = $config['products_list'];
      }

      $aKeys = countPageNumber( $iCount, ( isset( $GLOBALS['aActions']['o2'] ) ? $GLOBALS['aActions']['o2'] : null ), $iList );
      $this->mData = null;

      for( $i = $aKeys['iStart']; $i < $aKeys['iEnd']; $i++ ){
        $aData = $this->aProducts[$aProducts[$i]];
        $sDescription = null;
        $sImage = null;

        if( !empty( $aData['sDescriptionShort'] ) ){
          $aData['sDescriptionShort'] = changeTxt( $aData['sDescriptionShort'], 'nlNds' );
          $sDescription = '<div class="description">'.$aData['sDescriptionShort'].'</div>';
        }

        if( isset( $oFile ) ){
          $sImage = $oFile->getDefaultImage( $aData['iProduct'], 2, true, $aData['sLinkName'] );
        }

        $content .= '<li class="l'.( ( $i == ( $iCount - 1 ) ) ? 'L': $i + 1 ).' i'.( ( $i % 2 ) ? 0: 1 ).' column'.( ( $i % 3 ) ? 0: 1 ).'">
          <h2><a href="'.$aData['sLinkName'].'">'.$aData['sName'].'</a></h2>
          '.( isset( $GLOBALS['aDisplayPagesTreeInProductsList'][$iPage] ) ? '<h3>'.$this->throwProductsPagesTree( $aData['iProduct'] ).'</h3>' : null ).$sImage.$sDescription;
        
        if( is_numeric( $aData['mPrice'] ) ){
          if( isset( $sBasketPage ) ){
            $content .= '<div class="basket"><a href="'.$sBasketPage.'&amp;iProductAdd='.$aData['iProduct'].'&amp;iQuantity=1" rel="nofollow" title="'.$lang['Basket_add'].': '.$aData['sName'].'">'.$lang['Basket_add'].'</a></div>';
          }
          $content .= '<div class="price"><em>'.$lang['Price'].':</em><strong>'.displayPrice( $aData['mPrice'] ).'</strong><span>'.$config['currency_symbol'].'</span></div>';
        }
        else{
          $content .= '<div class="noPrice"><strong>'.$aData['mPrice'].'</strong></div>';
        }          

        $content .= '</li>';
      } // end for

      if( isset( $content ) ){
        if( $iCount > $iList ){
          $aData['sPages'] = countPages( $iCount, $iList, $aKeys['iPageNumber'], ( isset( $oPage->aPages[$iPage]['sLinkNameHome'] ) ? $oPage->aPages[$iPage]['sLinkNameHome'] : $oPage->aPages[$iPage]['sLinkName'] ), $sUrlExt );
          $aData['sHidePages'] = null;
        }
        else{
          $aData['sPages'] = '<li></li>';
          $aData['sHidePages'] = ' hide';
        }

        if( $config['sorting_products'] === true && isset( $oPage->aPages[$iPage] ) ){
          $sSortingLink = ( isset( $oPage->aPages[$iPage]['sLinkNameHome'] ) ? $oPage->aPages[$iPage]['sLinkNameHome'] : $oPage->aPages[$iPage]['sLinkName'] ).( isset( $GLOBALS['sPhrase'] ) && !empty( $GLOBALS['sPhrase'] ) ? '&amp;sPhrase='.$GLOBALS['sPhrase'].'&amp;' : '&amp;' ).( isset( $GLOBALS['bViewAll'] ) ? '&amp;bViewAll=true&amp;' : null );
          $sSortingLink = str_replace( '&amp;&amp;', '&amp;', $sSortingLink );
        }

        return '<div id="products" class="productsList">'.( isset( $sSortingLink ) ? '<div class="sort">'.$lang['Sort_by'].'<div><a href="'.$sSortingLink.'sSort=">'.$lang['Default'].'</a></div>|<div><a href="'.$sSortingLink.'sSort=name">'.$lang['Name'].'</a></div>|<div><a href="'.$sSortingLink.'sSort=price">'.$lang['Price'].'</a></div></div>' : null ).'<div class="pages'.$aData['sHidePages'].'" id="pagesBefore"><a href="'.$_SERVER['REQUEST_URI'].$config['before_amp'].'&amp;bViewAll=true">'.$lang['View_all'].'</a> | '.$lang['Pages'].': <ul>'.$aData['sPages'].'</ul></div><ul class="list">'.$content.'</ul><div class="pages'.$aData['sHidePages'].'" id="pagesAfter"><a href="'.$_SERVER['REQUEST_URI'].$config['before_amp'].'&amp;bViewAll=true">'.$lang['View_all'].'</a> | '.$lang['Pages'].': <ul>'.$aData['sPages'].'</ul></div></div>';
      }
    }
    else{
      if( $iPage == $config['page_search'] )
        echo '<div class="message" id="error"><h3>'.$lang['Data_not_found'].'</h3></div>';
    }
  } // end function listProducts

  /**
  * Return page data
  * @return array
  * @param int  $iProduct
  */
  public function throwProduct( $iProduct ){
    if( isset( $this->aProducts[$iProduct] ) ){
      $aData = $this->aProducts[$iProduct];
      $aData['aCategories'] = $this->aProductsPages[$iProduct];
      $aData['sPrice'] = is_numeric( $this->aProducts[$iProduct]['mPrice'] ) ? displayPrice( $this->aProducts[$iProduct]['mPrice'] ) : $this->aProducts[$iProduct]['mPrice'];
      if( !isset( $aData['sDescriptionFull'] ) ){
        $aData['sDescriptionFull'] = getFullDescription( DIR_DATABASE_PRODUCTS, $iProduct );
        if( !empty( $aData['sDescriptionFull'] ) )
          $aData['bDescriptionFromFile'] = true;
      }

      return $aData;
    }
    else
      return null;
  } // end function throwProduct

  /**
  * Return array with products
  * @return array
  * @param bool $bKeysInverted
  */
  protected function generateProductsListArray( $bKeysInverted = null ){
    if( isset( $this->aProducts ) ){
      foreach( $this->aProductsPages as $iProduct => $aData ){
        foreach( $this->aPages as $iValue ){
          if( isset( $aData[$iValue] ) && !isset( $aProducts[$iProduct] ) ){
            $aReturn[] = $iProduct;
            $aProducts[$iProduct] = true;
          }
        } // end foreach
      } // end foreach

      if( isset( $aReturn ) ){
        if( isset( $bKeysInverted ) )
          return $aProducts;
        else
          return $aReturn;
      }
    }
  } // end function generateProductsListArray

  /**
  * Return array with products
  * @return array
  * @param string $sPhrase
  */
  public function generateProductsSearchListArray( $sPhrase ){
    if( isset( $this->aProducts ) ){
      $aWords = getWordsFromPhrase( $sPhrase );
      $iCount = count( $aWords );

      if( isset( $this->aPages ) ){
        $aProductsPages = $this->generateProductsListArray( true );
      }

      foreach( $this->aProducts as $iProduct => $aProduct ){
        $iFound = 0;
        $bSearch = null;

        if( isset( $this->aPages ) ){
          if( isset( $aProductsPages[$iProduct] ) )
            $bSearch = true;
        }
        else
          $bSearch = true;

        if( isset( $bSearch ) ){
          $sSearchData = implode( ' ', $aProduct );
          for( $i = 0; $i < $iCount; $i++ ){
            if( preg_match( '/'.$aWords[$i].'/ui', $sSearchData ) )
              $iFound++;
          } // end for
        }

        if( $iFound == $iCount ){
          $aProducts[] = $iProduct;
        }
      }

      if( isset( $aProducts ) )
        return $aProducts;
    }
  } // end function generateProductsSearchListArray

  /**
  * Return products pages tree
  * @return string
  * @param int  $iProduct
  */
  public function throwProductsPagesTree( $iProduct ){
    global $oPage;
    if( isset( $this->aProductsPages[$iProduct] ) ){
      $content = null;
      $oPage->mData = null;
      foreach( $this->aProductsPages[$iProduct] as $iPage ){
        if( isset( $content ) )
          $content .= '<em>|</em>';
        if( isset( $this->mData[$iPage] ) ){
          $content .= $this->mData[$iPage];
        }
        else{
          $sTree = $oPage->throwPagesTree( $iPage );
          if( empty( $sTree ) && isset( $oPage->aPages[$iPage] ) )
            $sTree .= '<a href="'.$oPage->aPages[$iPage]['sLinkName'].'">'.$oPage->aPages[$iPage]['sName'].'</a>';
          $content .= $this->mData[$iPage] = $sTree;
        }
      }

      return $content;
    }
  } // end function throwProductsPagesTree

  /**
  * Sort products
  * @return array
  * @param array  $aProducts
  * @param string $sSort
  */
  public function sortProducts( $aProducts, $sSort = null ){
    $iCount = count( $aProducts );
    $sFunctionSort = 'sort';

    if( $sSort == 'status' ){
      $sKey = 'iStatus';
    }
    elseif( $sSort == 'price' ){
      $sKey = 'mPrice';    
    }
    elseif( $sSort == 'name' ){
      $sKey = 'sName';    
    }
    else{
      $sFunctionSort = 'rsort';
      $sKey = 'iProduct';    
    }

    for( $i = 0; $i < $iCount; $i++ ){
      $mValue = $this->aProducts[$aProducts[$i]][$sKey]; 
      $aSort[$i][0] = $mValue;
      $aSort[$i][1] = $aProducts[$i];
    } // end for

    $sFunctionSort( $aSort );

    for( $i = 0; $i < $iCount; $i++ ){
      $aProducts[$i] = $aSort[$i][1];
    } // end for    
    return $aProducts;
  } // end function sortProducts 
}
?>
Return current item: Quick.Cart - free and simple to use shopping cart script