1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\Criteria;
14: use Xoops\Core\Kernel\CriteriaCompo;
15: use Xoops\Core\Kernel\CriteriaElement;
16: use Xoops\Core\Kernel\XoopsObject;
17: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
18:
19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34:
35: class AvatarsAvatar extends XoopsObject
36: {
37: 38: 39:
40: private $userCount;
41:
42: 43: 44:
45: public function __construct()
46: {
47: $this->initVar('avatar_id', XOBJ_DTYPE_INT, null, false);
48: $this->initVar('avatar_file', XOBJ_DTYPE_OTHER, null, false, 30);
49: $this->initVar('avatar_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
50: $this->initVar('avatar_mimetype', XOBJ_DTYPE_OTHER, null, false);
51: $this->initVar('avatar_created', XOBJ_DTYPE_INT, null, false);
52: $this->initVar('avatar_display', XOBJ_DTYPE_INT, 1, false);
53: $this->initVar('avatar_weight', XOBJ_DTYPE_INT, 0, false);
54: $this->initVar('avatar_type', XOBJ_DTYPE_OTHER, 0, false);
55: }
56:
57: 58: 59: 60: 61: 62: 63:
64: public function id($format = 'n')
65: {
66: return $this->getVar('avatar_id', $format);
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76: public function avatar_id($format = '')
77: {
78: return $this->getVar('avatar_id', $format);
79: }
80:
81: 82: 83: 84: 85: 86: 87:
88: public function avatar_file($format = '')
89: {
90: return $this->getVar('avatar_file', $format);
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public function avatar_name($format = '')
101: {
102: return $this->getVar('avatar_name', $format);
103: }
104:
105: 106: 107: 108: 109: 110: 111:
112: public function avatar_mimetype($format = '')
113: {
114: return $this->getVar('avatar_mimetype', $format);
115: }
116:
117: 118: 119: 120: 121: 122: 123:
124: public function avatar_created($format = '')
125: {
126: return $this->getVar('avatar_created', $format);
127: }
128:
129: 130: 131: 132: 133: 134: 135:
136: public function avatar_display($format = '')
137: {
138: return $this->getVar('avatar_display', $format);
139: }
140:
141: 142: 143: 144: 145: 146: 147:
148: public function avatar_weight($format = '')
149: {
150: return $this->getVar('avatar_weight', $format);
151: }
152:
153: 154: 155: 156: 157: 158: 159:
160: public function avatar_type($format = '')
161: {
162: return $this->getVar('avatar_type', $format);
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function setUserCount($value)
173: {
174: $this->userCount = (int)($value);
175: }
176:
177: 178: 179: 180: 181:
182: public function getUserCount()
183: {
184: return $this->userCount;
185: }
186: }
187:
188: 189: 190: 191: 192:
193: class AvatarsAvatarHandler extends XoopsPersistableObjectHandler
194: {
195: 196: 197: 198: 199:
200: public function __construct(Connection $db = null)
201: {
202: parent::__construct($db, 'avatars_avatar', 'AvatarsAvatar', 'avatar_id', 'avatar_name');
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212:
213: public function getObjectsWithCount(CriteriaElement $criteria = null, $id_as_key = false)
214: {
215: $ret = array();
216: if ($criteria === null) {
217: $criteria = new Criteria('');
218: }
219: $criteria->setGroupby('a.avatar_id');
220: $criteria->setSort('avatar_weight, avatar_id');
221: $qb = $this->db2->createXoopsQueryBuilder();
222: $qb ->select('a.*', 'COUNT(u.user_id) AS count')
223: ->fromPrefix('avatars_avatar', 'a')
224: ->leftJoinPrefix('l', 'avatars_user_link', 'u', 'u.avatar_id=a.avatar_id');
225: $criteria->renderQb($qb);
226: $result = $qb->execute();
227: if (!$result) {
228: return $ret;
229: }
230: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
231: $avatar = new AvatarsAvatar();
232: $avatar->assignVars($myrow);
233: $avatar->setUserCount($myrow['count']);
234: if (!$id_as_key) {
235: $ret[] = $avatar;
236: } else {
237: $ret[$myrow['avatar_id']] = $avatar;
238: }
239: unset($avatar);
240: }
241: return $ret;
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251:
252: public function addUser($avatar_id, $user_id)
253: {
254: $avatar_id = (int)($avatar_id);
255: $user_id = (int)($user_id);
256: if ($avatar_id < 1 || $user_id < 1) {
257: return false;
258: }
259:
260: $qb = $this->db2->createXoopsQueryBuilder();
261: $qb ->deletePrefix('avatars_user_link', 'l')
262: ->where('l.user_id = :uid')
263: ->setParameter(':uid', $user_id, \PDO::PARAM_INT);
264: $result = $qb->execute();
265: if ($result) {
266: return false;
267: }
268:
269: $qb = $this->db2->createXoopsQueryBuilder();
270: $qb ->insertPrefix('avatars_user_link')
271: ->values(
272: array(
273: 'avatar_id' => ':aid',
274: 'user_id' => ':uid'
275: )
276: )
277: ->setParameter(':aid', $avatar_id, \PDO::PARAM_INT)
278: ->setParameter(':uid', $user_id, \PDO::PARAM_INT);
279: $result = $qb->execute();
280: if ($result) {
281: return false;
282: }
283:
284: return true;
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: public function getUser(AvatarsAvatar $avatar)
295: {
296: $ret = array();
297: $qb = $this->db2->createXoopsQueryBuilder();
298: $qb ->select('user_id')
299: ->fromPrefix('avatars_user_link', 'l')
300: ->where('l.avatar_id = :bid')
301: ->setParameter(':bid', $avatar->getVar('avatar_id'), \PDO::PARAM_INT);
302: $result = $qb->execute();
303: if (!$result) {
304: return $ret;
305: }
306: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
307: $ret[] = $myrow['user_id'];
308: }
309: return $ret;
310: }
311:
312: 313: 314: 315: 316: 317: 318: 319:
320: public function getListByType($avatar_type = null, $avatar_display = null)
321: {
322: $criteria = new CriteriaCompo();
323: if (isset($avatar_type)) {
324: $avatar_type = ($avatar_type === 'C') ? 'C' : 'S';
325: $criteria->add(new Criteria('avatar_type', $avatar_type));
326: }
327: if (isset($avatar_display)) {
328: $criteria->add(new Criteria('avatar_display', (int)($avatar_display)));
329: }
330: $avatars = $this->getObjects($criteria, true);
331: $ret = array(
332: 'avatars/blank.gif' => XoopsLocale::NONE
333: );
334: foreach (array_keys($avatars) as $i) {
335: $ret[$avatars[$i]->getVar('avatar_file')] = $avatars[$i]->getVar('avatar_name');
336: }
337: return $ret;
338: }
339: }
340: