com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_counters.php

Documentation is available at lib_counters.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains everything that is connected to counters.
  9. * @author Andreas Launila
  10. * @version $Revision: 1.11 $
  11. * @package com.lokorin.lokorin.lib
  12. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13. */
  14.  
  15. /**
  16. * For access to common constants and functions.
  17. */
  18. require_once('common.php');
  19.  
  20. /**
  21. * Gets the descriptions of all available counters.
  22. * @return array An array with the counters' unique identifiers as key and the
  23. * corresponding descriptions as value.
  24. * @access public
  25. */
  26. function getAllCounterDescriptions() {
  27. global $db;
  28. $fields = array('id');
  29. $rows = $db->querySelect(TABLE_COUNTERS, $fields);
  30. $descriptions = array();
  31. foreach($rows as $row) {
  32. $counter = new Counter($row['id']);
  33. $descriptions[$row['id']] = $counter->getDescription();
  34. }
  35. return $descriptions;
  36. }
  37.  
  38. /**
  39. * Describes a counter which maintains a value that can be incremented by one.
  40. * @author Andreas Launila
  41. * @package com.lokorin.lokorin.lib
  42. */
  43. class Counter extends TableEntry{
  44. /**
  45. * The number of hits that the counter has recorded.
  46. * @var integer
  47. */
  48. private $hits;
  49. /**
  50. * The unix timestamp of the last hit that the counter recorded.
  51. * @var integer
  52. */
  53. private $lastTimestamp;
  54. /**
  55. * The description of what the counter records.
  56. * @var string
  57. */
  58. private $description;
  59. /**
  60. * Constructor for Counter.
  61. * @param integer $id The unique identifier for the counter. This parameter
  62. * is optional, if it's not given then a new counter is created.
  63. */
  64. public function __construct($id = 0) {
  65. if($id === 0) {
  66. //Create a new counter.
  67. global $db;
  68. $vars = array(
  69. 'hits' => 0,
  70. 'latest_timestamp' => 0,
  71. 'description' => ''
  72. );
  73. $db->queryInsert(TABLE_COUNTERS, $vars);
  74. $id = $db->getLastInsertId();
  75. }
  76. //Get the counter's information.
  77. $fields = array(
  78. 'hits',
  79. 'latest_timestamp',
  80. 'description'
  81. );
  82. parent::__construct(TABLE_COUNTERS, $id, $fields);
  83. }
  84. /**
  85. * @see com.lokorin.dfcrafters.TableEntry.readRow($row)
  86. */
  87. protected function readRow($row) {
  88. $this->hits = $row['hits'];
  89. $this->lastTimestamp = $row['latest_timestamp'];
  90. $this->description = $row['description'];
  91. }
  92. /**
  93. * Records that a hit has occured. Increments the counter.
  94. * @access public
  95. */
  96. public function recordHit() {
  97. $this->hits++;
  98. $this->lastTimestamp = time();
  99. $this->save();
  100. }
  101. /**
  102. * Saves the current information held by the counter. This public function should
  103. * be concidered private.
  104. * @access private
  105. */
  106. public function save() {
  107. global $db;
  108. $vars = array(
  109. 'hits' => $this->hits,
  110. 'latest_timestamp' => $this->lastTimestamp,
  111. 'description' => $this->description
  112. );
  113. $idFields = array(
  114. 'id' => parent::$this->getId()
  115. );
  116. $db->queryUpdate(TABLE_COUNTERS, $vars, $db->buildsqlSelector($idFields));
  117. }
  118. /**
  119. * Sets the counters description to a new value.
  120. * @param string $newDescription The new description that should be used
  121. * for the counter. This description should describe what the counter
  122. * records.
  123. * @access public
  124. */
  125. public function setDescription($newDescription) {
  126. $this->description = $newDescription;
  127. $this->save();
  128. }
  129. /**
  130. * Gets the counter's description. The description describes what the
  131. * counter is recording.
  132. * @return string A description.
  133. * @access public
  134. */
  135. public function getDescription() {
  136. return $this->description;
  137. }
  138. /**
  139. * Gets the number of hits that have been recorded by the counter.
  140. * @return integer A non-negative number describing the number of recorded
  141. * hits.
  142. * @access public
  143. */
  144. function getHits() {
  145. return $this->hits;
  146. }
  147. }
  148. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:13 +0200 by phpDocumentor 1.3.0RC4