1: <?php
2:
3: /**
4: * This file contains the stat collection and item class.
5: *
6: * @package Core
7: * @subpackage GenericDB_Model
8: * @author Murat Purc <murat@purc.de>
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Statistic collection
19: *
20: * @package Core
21: * @subpackage GenericDB_Model
22: */
23: class cApiStatCollection extends ItemCollection {
24: /**
25: * Constructor to create an instance of this class.
26: *
27: * @throws cInvalidArgumentException
28: */
29: public function __construct() {
30: global $cfg;
31: parent::__construct($cfg['tab']['stat'], 'idstat');
32: $this->_setItemClass('cApiStat');
33:
34: // set the join partners so that joins can be used via link() method
35: $this->_setJoinPartner('cApiCategoryArticleCollection');
36: $this->_setJoinPartner('cApiLanguageCollection');
37: $this->_setJoinPartner('cApiClientCollection');
38: }
39:
40: /**
41: * Tracks a visit.
42: * Increments a existing entry or creates a new one.
43: *
44: * @param int $iIdCatArt
45: * @param int $iIdLang
46: * @param int $iIdClient
47: *
48: * @throws cDbException
49: * @throws cException
50: * @throws cInvalidArgumentException
51: */
52: public function trackVisit($iIdCatArt, $iIdLang, $iIdClient) {
53: $oStat = $this->fetchByCatArtAndLang($iIdCatArt, $iIdLang);
54: if (is_object($oStat)) {
55: $oStat->increment();
56: } else {
57: $this->create($iIdCatArt, $iIdLang, $iIdClient);
58: }
59: }
60:
61: /**
62: * Creates a stat entry.
63: *
64: * @param int $iIdCatArt
65: * @param int $iIdLang
66: * @param int $iIdClient
67: * @param int $iVisited [optional]
68: *
69: * @return cApiStat
70: * @throws cDbException
71: * @throws cException
72: * @throws cInvalidArgumentException
73: */
74: public function create($iIdCatArt, $iIdLang, $iIdClient, $iVisited = 1) {
75: $oItem = $this->createNewItem();
76:
77: $oItem->set('visited', $iVisited);
78: $oItem->set('idcatart', $iIdCatArt);
79: $oItem->set('idlang', $iIdLang);
80: $oItem->set('idclient', $iIdClient);
81: $oItem->store();
82:
83: return $oItem;
84: }
85:
86: /**
87: * Returns a stat entry by category article and language.
88: *
89: * @param int $iIdCatArt
90: * @param int $iIdLang
91: * @return cApiStat|NULL
92: * @throws cDbException
93: * @throws cException
94: */
95: public function fetchByCatArtAndLang($iIdCatArt, $iIdLang) {
96: $this->select('idcatart=' . (int) $iIdCatArt . ' AND idlang=' . (int) $iIdLang);
97: return $this->next();
98: }
99:
100: /**
101: * Deletes statistics entries by category article id and language id.
102: *
103: * @param int $idcatart
104: * @param int $idlang
105: * @return int
106: * Number of deleted items
107: * @throws cDbException
108: * @throws cInvalidArgumentException
109: */
110: public function deleteByCategoryArticleAndLanguage($idcatart, $idlang) {
111: $where = 'idcatart = ' . (int) $idcatart . ' AND idlang = ' . (int) $idlang;
112: return $this->deleteByWhereClause($where);
113: }
114: }
115:
116: /**
117: * Statistic item
118: *
119: * @package Core
120: * @subpackage GenericDB_Model
121: */
122: class cApiStat extends Item
123: {
124: /**
125: * Constructor to create an instance of this class.
126: *
127: * @param mixed $mId [optional]
128: * Specifies the ID of item to load
129: *
130: * @throws cDbException
131: * @throws cException
132: */
133: public function __construct($mId = false) {
134: global $cfg;
135: parent::__construct($cfg['tab']['stat'], 'idstat');
136: $this->setFilters(array(), array());
137: if ($mId !== false) {
138: $this->loadByPrimaryKey($mId);
139: }
140: }
141:
142: /**
143: * Increment and store property 'visited'.
144: *
145: * @throws cDbException
146: * @throws cInvalidArgumentException
147: */
148: public function increment() {
149: $this->set('visited', $this->get('visited') + 1);
150: $this->store();
151: }
152:
153: /**
154: * Userdefined setter for stat fields.
155: *
156: * @param string $name
157: * @param mixed $value
158: * @param bool $bSafe [optional]
159: * Flag to run defined inFilter on passed value
160: * @return bool
161: */
162: public function setField($name, $value, $bSafe = true) {
163: switch ($name) {
164: case 'visited':
165: $value = (int) $value;
166: break;
167: case 'idcatart':
168: $value = (int) $value;
169: break;
170: case 'idlang':
171: $value = (int) $value;
172: break;
173: case 'idclient':
174: $value = (int) $value;
175: break;
176: }
177:
178: return parent::setField($name, $value, $bSafe);
179: }
180:
181: }
182: