1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**
21: * A Avatar
22: *
23: * @author Kazumi Ono <onokazu@xoops.org>
24: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
25: *
26: * @package kernel
27: */
28: class XoopsAvatar extends XoopsObject
29: {
30: public $_userCount;
31:
32: //PHP 8.2 Dynamic properties deprecated
33: public $avatar_id;
34: public $avatar_file;
35: public $avatar_name;
36: public $avatar_mimetype;
37: public $avatar_created;
38: public $avatar_display;
39: public $avatar_weight;
40: public $avatar_type;
41:
42: /**
43: * Constructor
44: */
45: public function __construct()
46: {
47: parent::__construct();
48: $this->initVar('avatar_id', XOBJ_DTYPE_INT, null, false);
49: $this->initVar('avatar_file', XOBJ_DTYPE_OTHER, null, false, 30);
50: $this->initVar('avatar_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
51: $this->initVar('avatar_mimetype', XOBJ_DTYPE_OTHER, null, false);
52: $this->initVar('avatar_created', XOBJ_DTYPE_INT, null, false);
53: $this->initVar('avatar_display', XOBJ_DTYPE_INT, 1, false);
54: $this->initVar('avatar_weight', XOBJ_DTYPE_INT, 0, false);
55: $this->initVar('avatar_type', XOBJ_DTYPE_OTHER, 0, false);
56: }
57:
58: /**
59: * Returns Class Base Variable avatar_id
60: * @param string $format
61: * @return mixed
62: */
63: public function id($format = 'N')
64: {
65: return $this->getVar('avatar_id', $format);
66: }
67:
68: /**
69: * Returns Class Base Variable avatar_id
70: * @param string $format
71: * @return mixed
72: */
73: public function avatar_id($format = '')
74: {
75: return $this->getVar('avatar_id', $format);
76: }
77:
78: /**
79: * Returns Class Base Variable avatar_file
80: * @param string $format
81: * @return mixed
82: */
83: public function avatar_file($format = '')
84: {
85: return $this->getVar('avatar_file', $format);
86: }
87:
88: /**
89: * Returns Class Base Variable avatar_name
90: * @param string $format
91: * @return mixed
92: */
93: public function avatar_name($format = '')
94: {
95: return $this->getVar('avatar_name', $format);
96: }
97:
98: /**
99: * Returns Class Base Variable avatar_mimetype
100: * @param string $format
101: * @return mixed
102: */
103: public function avatar_mimetype($format = '')
104: {
105: return $this->getVar('avatar_mimetype', $format);
106: }
107:
108: /**
109: * Returns Class Base Variable avatar_created
110: * @param string $format
111: * @return mixed
112: */
113: public function avatar_created($format = '')
114: {
115: return $this->getVar('avatar_created', $format);
116: }
117:
118: /**
119: * Returns Class Base Variable avatar_display
120: * @param string $format
121: * @return mixed
122: */
123: public function avatar_display($format = '')
124: {
125: return $this->getVar('avatar_display', $format);
126: }
127:
128: /**
129: * Returns Class Base Variable avatar_weight
130: * @param string $format
131: * @return mixed
132: */
133: public function avatar_weight($format = '')
134: {
135: return $this->getVar('avatar_weight', $format);
136: }
137:
138: /**
139: * Returns Class Base Variable avatar_type
140: * @param string $format
141: * @return mixed
142: */
143: public function avatar_type($format = '')
144: {
145: return $this->getVar('avatar_type', $format);
146: }
147:
148: /**
149: * Set User Count
150: *
151: * @param int $value
152: */
153: public function setUserCount($value)
154: {
155: $this->_userCount = (int)$value;
156: }
157:
158: /**
159: * Get User Count
160: *
161: * @return int
162: */
163: public function getUserCount()
164: {
165: return $this->_userCount;
166: }
167: }
168:
169: /**
170: * XOOPS avatar handler class. (Singelton)
171: *
172: * This class is responsible for providing data access mechanisms to the data source
173: * of XOOPS block class objects.
174: *
175: * @author Kazumi Ono <onokazu@xoops.org>
176: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
177: * @package kernel
178: * @subpackage block
179: */
180: class XoopsAvatarHandler extends XoopsObjectHandler
181: {
182: /**
183: * Create new Object
184: *
185: * @param bool $isNew
186: * @return XoopsAvatar
187: */
188: public function create($isNew = true)
189: {
190: $avatar = new XoopsAvatar();
191: if ($isNew) {
192: $avatar->setNew();
193: }
194:
195: return $avatar;
196: }
197:
198: /**
199: * Egt Object
200: *
201: * @param int $id
202: * @return XoopsAvatar|false
203: */
204: public function get($id)
205: {
206: $avatar = false;
207: $id = (int)$id;
208: if ($id > 0) {
209: $sql = 'SELECT * FROM ' . $this->db->prefix('avatar') . ' WHERE avatar_id=' . $id;
210: $result = $this->db->query($sql);
211: if (!$this->db->isResultSet($result)) {
212: return false;
213: }
214: $numrows = $this->db->getRowsNum($result);
215: if ($numrows == 1) {
216: $avatar = new XoopsAvatar();
217: $avatar->assignVars($this->db->fetchArray($result));
218:
219: return $avatar;
220: }
221: }
222:
223: return $avatar;
224: }
225:
226: /**
227: * Insert and Object into the database
228: *
229: * @param XoopsObject|XoopsAvatar $avatar a XoopsAvatar object
230: *
231: * @return bool true on success, otherwise false
232: */
233: public function insert(XoopsObject $avatar)
234: {
235: $className = 'XoopsAvatar';
236: if (!($avatar instanceof $className)) {
237: return false;
238: }
239: if (!$avatar->isDirty()) {
240: return true;
241: }
242: if (!$avatar->cleanVars()) {
243: return false;
244: }
245: foreach ($avatar->cleanVars as $k => $v) {
246: ${$k} = $v;
247: }
248: if ($avatar->isNew()) {
249: $avatar_id = $this->db->genId('avatar_avatar_id_seq');
250: $sql = sprintf('INSERT INTO %s (avatar_id, avatar_file, avatar_name, avatar_created, avatar_mimetype, avatar_display, avatar_weight, avatar_type) VALUES (%u, %s, %s, %u, %s, %u, %u, %s)', $this->db->prefix('avatar'), $avatar_id, $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), time(), $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type));
251: } else {
252: $sql = sprintf('UPDATE %s SET avatar_file = %s, avatar_name = %s, avatar_created = %u, avatar_mimetype= %s, avatar_display = %u, avatar_weight = %u, avatar_type = %s WHERE avatar_id = %u', $this->db->prefix('avatar'), $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), $avatar_created, $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type), $avatar_id);
253: }
254: if (!$result = $this->db->query($sql)) {
255: return false;
256: }
257: if (empty($avatar_id)) {
258: $avatar_id = $this->db->getInsertId();
259: }
260: $avatar->assignVar('avatar_id', $avatar_id);
261:
262: return true;
263: }
264:
265: /**
266: * Delete an object from the database
267: *
268: * @param XoopsObject|XoopsAvatar $avatar a XoopsAvatar object
269: *
270: * @return bool true on success, otherwise false
271: */
272: public function delete(XoopsObject $avatar)
273: {
274: $className = 'XoopsAvatar';
275: if (!($avatar instanceof $className)) {
276: return false;
277: }
278:
279: $id = $avatar->getVar('avatar_id');
280: $sql = sprintf('DELETE FROM %s WHERE avatar_id = %u', $this->db->prefix('avatar'), $id);
281: if (!$result = $this->db->query($sql)) {
282: return false;
283: }
284: $sql = sprintf('DELETE FROM %s WHERE avatar_id = %u', $this->db->prefix('avatar_user_link'), $id);
285: $result = $this->db->query($sql);
286:
287: return true;
288: }
289:
290: /**
291: * Fetch a row of objects from the database
292: *
293: * @param CriteriaElement|CriteriaCompo $criteria
294: * @param bool $id_as_key
295: * @return array
296: */
297: public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false)
298: {
299: $ret = array();
300: $limit = $start = 0;
301: $sql = 'SELECT a.*, COUNT(u.user_id) AS count FROM ' . $this->db->prefix('avatar') . ' a LEFT JOIN ' . $this->db->prefix('avatar_user_link') . ' u ON u.avatar_id=a.avatar_id';
302: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
303: $sql .= ' ' . $criteria->renderWhere();
304: $sql .= ' GROUP BY a.avatar_id ORDER BY avatar_weight, avatar_id';
305: $limit = $criteria->getLimit();
306: $start = $criteria->getStart();
307: }
308: $result = $this->db->query($sql, $limit, $start);
309: if (!$this->db->isResultSet($result)) {
310: throw new \RuntimeException(
311: \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
312: );
313: }
314: /** @var array $myrow */
315: while (false !== ($myrow = $this->db->fetchArray($result))) {
316: $avatar = new XoopsAvatar();
317: $avatar->assignVars($myrow);
318: $avatar->setUserCount($myrow['count']);
319: if (!$id_as_key) {
320: $ret[] = &$avatar;
321: } else {
322: $ret[$myrow['avatar_id']] = &$avatar;
323: }
324: unset($avatar);
325: }
326:
327: return $ret;
328: }
329:
330: /**
331: * Get count
332: *
333: * @param CriteriaElement|CriteriaCompo $criteria
334: * @return int
335: */
336: public function getCount(CriteriaElement $criteria = null)
337: {
338:
339:
340: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('avatar');
341: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
342: $sql .= ' ' . $criteria->renderWhere();
343: }
344: $result = $this->db->query($sql);
345: if (!$this->db->isResultSet($result)) {
346: return 0;
347: }
348:
349: list($count) = $this->db->fetchRow($result);
350:
351: return (int)$count;
352: }
353:
354: /**
355: * Add user
356: *
357: * @param int $avatar_id
358: * @param int $user_id
359: * @return bool
360: */
361: public function addUser($avatar_id, $user_id)
362: {
363: $avatar_id = (int)$avatar_id;
364: $user_id = (int)$user_id;
365: if ($avatar_id < 1 || $user_id < 1) {
366: return false;
367: }
368: $sql = sprintf('DELETE FROM %s WHERE user_id = %u', $this->db->prefix('avatar_user_link'), $user_id);
369: $this->db->query($sql);
370: $sql = sprintf('INSERT INTO %s (avatar_id, user_id) VALUES (%u, %u)', $this->db->prefix('avatar_user_link'), $avatar_id, $user_id);
371: if (!$result = $this->db->query($sql)) {
372: return false;
373: }
374:
375: return true;
376: }
377:
378: /**
379: * Get User
380: *
381: * @param XoopsAvatar $avatar
382: * @return array|false
383: */
384: public function getUser(XoopsAvatar $avatar)
385: {
386: $ret = array();
387: /**
388: * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
389: */
390: if (!is_a($avatar, 'xoopsavatar')) {
391: return false;
392: }
393: $sql = 'SELECT user_id FROM ' . $this->db->prefix('avatar_user_link') . ' WHERE avatar_id=' . $avatar->getVar('avatar_id');
394: $result = $this->db->query($sql);
395: if (!$this->db->isResultSet($result)) {
396: return $ret;
397: }
398: /** @var array $myrow */
399: while (false !== ($myrow = $this->db->fetchArray($result))) {
400: $ret[] = &$myrow['user_id'];
401: }
402:
403: return $ret;
404: }
405:
406: /**
407: * Get a list of Avatars
408: *
409: * @param string|null $avatar_type 'S' for system, 'C' for custom
410: * @param bool|null $avatar_display null lists all, bool respects avatar_display
411: * @return array
412: */
413: public function getList($avatar_type = null, $avatar_display = null)
414: {
415: $criteria = new CriteriaCompo();
416: if (isset($avatar_type)) {
417: $avatar_type = ($avatar_type === 'C') ? 'C' : 'S';
418: $criteria->add(new Criteria('avatar_type', $avatar_type));
419: }
420: if (isset($avatar_display)) {
421: $criteria->add(new Criteria('avatar_display', (int)$avatar_display));
422: }
423: $avatars = &$this->getObjects($criteria, true);
424: $ret = array('blank.gif' => _NONE);
425: foreach (array_keys($avatars) as $i) {
426: $ret[$avatars[$i]->getVar('avatar_file')] = $avatars[$i]->getVar('avatar_name');
427: }
428:
429: return $ret;
430: }
431: }
432: