<?php
/**
* Save comment
* @return void
* @param array  $aForm
* @param int    $iImage
*/
function addComment( $aForm, $iImage ){
  $oFF =& FlatFiles::getInstance( );

  $aForm = changeMassTxt( $aForm, 'HLen', Array( 'sContent', 'HBrLen' ) );
  $aForm['iComment'] = $oFF->throwLastId( DB_IMAGES_COMMENTS, 'iComment' ) + 1;
  $aForm['iTime'] = time( );
  $aForm['iImage'] = $iImage;
  $aForm['sIp'] = $_SERVER['REMOTE_ADDR'];
  $aForm['iStatus'] = 0;

  $oFF->save( DB_IMAGES_COMMENTS, $aForm, null, 'rsort' );

} // end function addComment

/**
* Save vote to image
* @return void
* @param int  $iImage
* @param int  $iVote
*/
function saveVote( $iImage, $iVote ){
  $oFF =& FlatFiles::getInstance( );
  $aVotes = getVotes( $iImage );
  if( isset( $aVotes ) ){
    $iVotesCount = $aVotes['iVotesCount'] + 1;
    $fVotesAvarage = sprintf( '%01.1f', ( ( $aVotes['fVotesAvarage'] * $aVotes['iVotesCount'] ) + $iVote ) / $iVotesCount );
    $oFF->save( DB_IMAGES_VOTES, Array( 'iImage' => $iImage, 'iVotesCount' => $iVotesCount, 'fVotesAvarage' => $fVotesAvarage ), 'iImage' );
  }
  else{
    $oFF->save( DB_IMAGES_VOTES, Array( 'iImage' => $iImage, 'iVotesCount' => 1, 'fVotesAvarage' => sprintf( '%01.1f', $iVote ) ) );
  }
} // end function saveVote

/**
* List comment for page
* @return string
* @param string  $sFile
* @param int  $iImage
* @param int  $iList
*/
function listComments( $sFile, $iImage = null, $iList = null ){

  if( !isset( $iList ) ){
    $iList = $GLOBALS['config']['comments_list'];
  }

  if( isset( $iImage ) && !is_numeric( $iImage ) ){
    $iImage = null;
  }

  $iStatus = defined( 'CUSTOMER_PAGE' ) ? 1 : 0;
  $sFunction = LANGUAGE.'_images_comments';
  
  $rFile = fopen( DB_IMAGES_COMMENTS, 'r' );
  $i = 0;
  while( ( $aExp = fgetcsv( $rFile, 50000, '$' ) ) !== FALSE ){
    if( $i > 0 ){
      if( $aExp[3] >= $iStatus ){
        if( isset( $iImage ) ){
          if( $iImage == $aExp[1] ){
            $aComments[] = $aExp;
          }
        }
        else{
          $aComments[] = $aExp;
        }
      }
    }
    $i++;
  }
  fclose( $rFile );

  if( isset( $aComments ) ){
    $oTpl =& TplParser::getInstance( );
    $oImage = $GLOBALS['oImage'];
    $content = null;

    $iCount = count( $aComments );
    $iComments = ceil( $iCount / $iList );
    $iPageNumber = isset( $_GET['iPage'] ) ? $_GET['iPage'] : 1;
    if( !isset( $iPageNumber ) || !is_numeric( $iPageNumber ) || $iPageNumber < 1 )
      $iPageNumber = 1;
    if( $iPageNumber > $iComments )
      $iPageNumber = $iComments;

    $iEnd   = $iPageNumber * $iList;
    $iStart = $iEnd - $iList;

    if( $iEnd > $iCount )
      $iEnd = $iCount;

    for( $i = $iStart; $i < $iEnd; $i++ ){
      $aData = $sFunction( $aComments[$i] );
      $aData['iStyle']  = ( $i % 2 ) ? 0: 1;
      $aData['sStyle']  = ( $i == ( $iCount - 1 ) ) ? 'L': $i + 1;
      $aData['sDate']   = date( 'Y-m-d H:i', $aData['iTime'] );
      $aData['sImageName'] = $oImage->aImages[$aData['iImage']]['sName'];

      $oTpl->setVariables( 'aData', $aData );
      $content .= $oTpl->tbHtml( $sFile, 'COMMENTS_LIST' );
    } // end for

    if( isset( $content ) ){
      $aData['sPages'] = countPagesClassic( $iCount, $iList, $iPageNumber, changeUri( $_SERVER['REQUEST_URI'] ) );
      $oTpl->setVariables( 'aData', $aData );
      return $oTpl->tbHtml( $sFile, 'COMMENTS_HEAD' ).$content.$oTpl->tbHtml( $sFile, 'COMMENTS_FOOT' );
    }
  }
} // end function listComments

/**
* Return images comments quantity
* @return mixed
* @param int  $iImage
*/
function getCommetsQuantity( $iImage = null ){
  $rFile = fopen( DB_IMAGES_COMMENTS, 'r' );
  $i = 0;
  while( ( $aExp = fgetcsv( $rFile, 50000, '$' ) ) !== FALSE ){
    if( $i > 0 && $aExp[3] == 1 ){
      if( !isset( $aCount[$aExp[1]] ) )
        $aCount[$aExp[1]] = 0;

      $aCount[$aExp[1]]++;
    }
    $i++;
  }
  fclose( $rFile );

  if( isset( $iImage ) ){
    if( isset( $aCount[$iImage] ) )
      return $aCount[$iImage];
    else
      return 0;
  }
  else{
    if( isset( $aCount ) )
      return $aCount;
  }
} // end function getCommetsQuantity

/**
* Return images votes
* @return mixed
* @param int  $iImage
*/
function getVotes( $iImage = null ){

  $aFile = file( DB_IMAGES_VOTES );
  $iCount = count( $aFile );
  for( $i = 1; $i < $iCount; $i++ ){
    $aExp = explode( '$', $aFile[$i] );
    $aVotes[$aExp[0]]['iVotesCount'] = $aExp[1];
    $aVotes[$aExp[0]]['fVotesAvarage'] = $aExp[2];
  } // end for
  
  if( isset( $iImage ) ){
    if( isset( $aVotes[$iImage] ) )
      return $aVotes[$iImage];
    else
      return null;
  }
  else{
    if( isset( $aVotes ) )
      return $aVotes;
  }
} // end function getVotes

?>