OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxcounter.php
Go to the documentation of this file.
1 <?php
2 
7 class oxCounter
8 {
16  public function getNext( $sIdent )
17  {
18  $oDb = oxDb::getDb();
19  $oDb->startTransaction();
20 
21  $sQ = "SELECT `oxcount` FROM `oxcounters` WHERE `oxident` = " . $oDb->quote( $sIdent ) . " FOR UPDATE";
22 
23  if ( ( $iCnt = $oDb->getOne( $sQ, false, false ) ) === false ) {
24  $sQ = "INSERT INTO `oxcounters` (`oxident`, `oxcount`) VALUES (?, '0')";
25  $oDb->execute( $sQ, array( $sIdent ) );
26  }
27 
28  $iCnt = ( (int) $iCnt ) + 1;
29  $sQ = "UPDATE `oxcounters` SET `oxcount` = ? WHERE `oxident` = ?";
30  $oDb->execute( $sQ, array( $iCnt, $sIdent ) );
31 
32  $oDb->commitTransaction();
33 
34  return $iCnt;
35  }
36 
46  public function update( $sIdent, $iCount )
47  {
48  $oDb = oxDb::getDb();
49  $oDb->startTransaction();
50 
51  $sQ = "SELECT `oxcount` FROM `oxcounters` WHERE `oxident` = " . $oDb->quote( $sIdent ) . " FOR UPDATE";
52 
53  if ( ( $iCnt = $oDb->getOne( $sQ, false, false ) ) === false ) {
54  $sQ = "INSERT INTO `oxcounters` (`oxident`, `oxcount`) VALUES (?, ?)";
55  $blResult = $oDb->execute( $sQ, array( $sIdent, $iCount ) );
56  } else {
57  $sQ = "UPDATE `oxcounters` SET `oxcount` = ? WHERE `oxident` = ? AND `oxcount` < ?";
58  $blResult = $oDb->execute( $sQ, array( $iCount, $sIdent, $iCount ) );
59  }
60 
61  $oDb->commitTransaction();
62  return $blResult;
63  }
64 
65 
66 }