1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
19: |
|
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: |
|
28: | class XoopsAvatar extends XoopsObject
|
29: | {
|
30: | public $_userCount;
|
31: |
|
32: |
|
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: | |
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: | |
60: | |
61: | |
62: |
|
63: | public function id($format = 'N')
|
64: | {
|
65: | return $this->getVar('avatar_id', $format);
|
66: | }
|
67: |
|
68: | |
69: | |
70: | |
71: | |
72: |
|
73: | public function avatar_id($format = '')
|
74: | {
|
75: | return $this->getVar('avatar_id', $format);
|
76: | }
|
77: |
|
78: | |
79: | |
80: | |
81: | |
82: |
|
83: | public function avatar_file($format = '')
|
84: | {
|
85: | return $this->getVar('avatar_file', $format);
|
86: | }
|
87: |
|
88: | |
89: | |
90: | |
91: | |
92: |
|
93: | public function avatar_name($format = '')
|
94: | {
|
95: | return $this->getVar('avatar_name', $format);
|
96: | }
|
97: |
|
98: | |
99: | |
100: | |
101: | |
102: |
|
103: | public function avatar_mimetype($format = '')
|
104: | {
|
105: | return $this->getVar('avatar_mimetype', $format);
|
106: | }
|
107: |
|
108: | |
109: | |
110: | |
111: | |
112: |
|
113: | public function avatar_created($format = '')
|
114: | {
|
115: | return $this->getVar('avatar_created', $format);
|
116: | }
|
117: |
|
118: | |
119: | |
120: | |
121: | |
122: |
|
123: | public function avatar_display($format = '')
|
124: | {
|
125: | return $this->getVar('avatar_display', $format);
|
126: | }
|
127: |
|
128: | |
129: | |
130: | |
131: | |
132: |
|
133: | public function avatar_weight($format = '')
|
134: | {
|
135: | return $this->getVar('avatar_weight', $format);
|
136: | }
|
137: |
|
138: | |
139: | |
140: | |
141: | |
142: |
|
143: | public function avatar_type($format = '')
|
144: | {
|
145: | return $this->getVar('avatar_type', $format);
|
146: | }
|
147: |
|
148: | |
149: | |
150: | |
151: | |
152: |
|
153: | public function setUserCount($value)
|
154: | {
|
155: | $this->_userCount = (int)$value;
|
156: | }
|
157: |
|
158: | |
159: | |
160: | |
161: | |
162: |
|
163: | public function getUserCount()
|
164: | {
|
165: | return $this->_userCount;
|
166: | }
|
167: | }
|
168: |
|
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: |
|
180: | class XoopsAvatarHandler extends XoopsObjectHandler
|
181: | {
|
182: | |
183: | |
184: | |
185: | |
186: | |
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: | |
200: | |
201: | |
202: | |
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: | |
228: | |
229: | |
230: | |
231: | |
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: | |
267: | |
268: | |
269: | |
270: | |
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: | |
292: | |
293: | |
294: | |
295: | |
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: |
|
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: | |
332: | |
333: | |
334: | |
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: | |
356: | |
357: | |
358: | |
359: | |
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: | |
380: | |
381: | |
382: | |
383: |
|
384: | public function getUser(XoopsAvatar $avatar)
|
385: | {
|
386: | $ret = array();
|
387: | |
388: | |
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: |
|
399: | while (false !== ($myrow = $this->db->fetchArray($result))) {
|
400: | $ret[] = &$myrow['user_id'];
|
401: | }
|
402: |
|
403: | return $ret;
|
404: | }
|
405: |
|
406: | |
407: | |
408: | |
409: | |
410: | |
411: | |
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: | |