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