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:  34: 
 35:     public function __construct()
 36:     {
 37:         parent::__construct();
 38:         $this->initVar('avatar_id', XOBJ_DTYPE_INT, null, false);
 39:         $this->initVar('avatar_file', XOBJ_DTYPE_OTHER, null, false, 30);
 40:         $this->initVar('avatar_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
 41:         $this->initVar('avatar_mimetype', XOBJ_DTYPE_OTHER, null, false);
 42:         $this->initVar('avatar_created', XOBJ_DTYPE_INT, null, false);
 43:         $this->initVar('avatar_display', XOBJ_DTYPE_INT, 1, false);
 44:         $this->initVar('avatar_weight', XOBJ_DTYPE_INT, 0, false);
 45:         $this->initVar('avatar_type', XOBJ_DTYPE_OTHER, 0, false);
 46:     }
 47: 
 48:      49:  50:  51:  52: 
 53:     public function id($format = 'N')
 54:     {
 55:         return $this->getVar('avatar_id', $format);
 56:     }
 57: 
 58:      59:  60:  61:  62: 
 63:     public function avatar_id($format = '')
 64:     {
 65:         return $this->getVar('avatar_id', $format);
 66:     }
 67: 
 68:      69:  70:  71:  72: 
 73:     public function avatar_file($format = '')
 74:     {
 75:         return $this->getVar('avatar_file', $format);
 76:     }
 77: 
 78:      79:  80:  81:  82: 
 83:     public function avatar_name($format = '')
 84:     {
 85:         return $this->getVar('avatar_name', $format);
 86:     }
 87: 
 88:      89:  90:  91:  92: 
 93:     public function avatar_mimetype($format = '')
 94:     {
 95:         return $this->getVar('avatar_mimetype', $format);
 96:     }
 97: 
 98:      99: 100: 101: 102: 
103:     public function avatar_created($format = '')
104:     {
105:         return $this->getVar('avatar_created', $format);
106:     }
107: 
108:     109: 110: 111: 112: 
113:     public function avatar_display($format = '')
114:     {
115:         return $this->getVar('avatar_display', $format);
116:     }
117: 
118:     119: 120: 121: 122: 
123:     public function avatar_weight($format = '')
124:     {
125:         return $this->getVar('avatar_weight', $format);
126:     }
127: 
128:     129: 130: 131: 132: 
133:     public function avatar_type($format = '')
134:     {
135:         return $this->getVar('avatar_type', $format);
136:     }
137: 
138:     139: 140: 141: 142: 
143:     public function setUserCount($value)
144:     {
145:         $this->_userCount = (int)$value;
146:     }
147: 
148:     149: 150: 151: 152: 
153:     public function getUserCount()
154:     {
155:         return $this->_userCount;
156:     }
157: }
158: 
159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 
170: class XoopsAvatarHandler extends XoopsObjectHandler
171: {
172:     173: 174: 175: 176: 177: 
178:     public function create($isNew = true)
179:     {
180:         $avatar = new XoopsAvatar();
181:         if ($isNew) {
182:             $avatar->setNew();
183:         }
184: 
185:         return $avatar;
186:     }
187: 
188:     189: 190: 191: 192: 193: 
194:     public function get($id)
195:     {
196:         $avatar = false;
197:         $id     = (int)$id;
198:         if ($id > 0) {
199:             $sql = 'SELECT * FROM ' . $this->db->prefix('avatar') . ' WHERE avatar_id=' . $id;
200:             if (!$result = $this->db->query($sql)) {
201:                 return false;
202:             }
203:             $numrows = $this->db->getRowsNum($result);
204:             if ($numrows == 1) {
205:                 $avatar = new XoopsAvatar();
206:                 $avatar->assignVars($this->db->fetchArray($result));
207: 
208:                 return $avatar;
209:             }
210:         }
211: 
212:         return $avatar;
213:     }
214: 
215:     216: 217: 218: 219: 220: 221: 
222:     public function insert(XoopsObject $avatar)
223:     {
224:         $className = 'XoopsAvatar';
225:         if (!($avatar instanceof $className)) {
226:             return false;
227:         }
228:         if (!$avatar->isDirty()) {
229:             return true;
230:         }
231:         if (!$avatar->cleanVars()) {
232:             return false;
233:         }
234:         foreach ($avatar->cleanVars as $k => $v) {
235:             ${$k} = $v;
236:         }
237:         if ($avatar->isNew()) {
238:             $avatar_id = $this->db->genId('avatar_avatar_id_seq');
239:             $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));
240:         } else {
241:             $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);
242:         }
243:         if (!$result = $this->db->query($sql)) {
244:             return false;
245:         }
246:         if (empty($avatar_id)) {
247:             $avatar_id = $this->db->getInsertId();
248:         }
249:         $avatar->assignVar('avatar_id', $avatar_id);
250: 
251:         return true;
252:     }
253: 
254:     255: 256: 257: 258: 259: 260: 
261:     public function delete(XoopsObject $avatar)
262:     {
263:         $className = 'XoopsAvatar';
264:         if (!($avatar instanceof $className)) {
265:             return false;
266:         }
267: 
268:         $id  = $avatar->getVar('avatar_id');
269:         $sql = sprintf('DELETE FROM %s WHERE avatar_id = %u', $this->db->prefix('avatar'), $id);
270:         if (!$result = $this->db->query($sql)) {
271:             return false;
272:         }
273:         $sql    = sprintf('DELETE FROM %s WHERE avatar_id = %u', $this->db->prefix('avatar_user_link'), $id);
274:         $result = $this->db->query($sql);
275: 
276:         return true;
277:     }
278: 
279:     280: 281: 282: 283: 284: 285: 
286:     public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false)
287:     {
288:         $ret   = array();
289:         $limit = $start = 0;
290:         $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';
291:         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
292:             $sql .= ' ' . $criteria->renderWhere();
293:             $sql .= ' GROUP BY a.avatar_id ORDER BY avatar_weight, avatar_id';
294:             $limit = $criteria->getLimit();
295:             $start = $criteria->getStart();
296:         }
297:         $result = $this->db->query($sql, $limit, $start);
298:         if (!$result) {
299:             return $ret;
300:         }
301:         while ($myrow = $this->db->fetchArray($result)) {
302:             $avatar = new XoopsAvatar();
303:             $avatar->assignVars($myrow);
304:             $avatar->setUserCount($myrow['count']);
305:             if (!$id_as_key) {
306:                 $ret[] = &$avatar;
307:             } else {
308:                 $ret[$myrow['avatar_id']] = &$avatar;
309:             }
310:             unset($avatar);
311:         }
312: 
313:         return $ret;
314:     }
315: 
316:     317: 318: 319: 320: 321: 
322:     public function getCount(CriteriaElement $criteria = null)
323:     {
324: 
325: 
326:         $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('avatar');
327:         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
328:             $sql .= ' ' . $criteria->renderWhere();
329:         }
330:         if (!$result = $this->db->query($sql)) {
331:             return 0;
332:         }
333:         list($count) = $this->db->fetchRow($result);
334: 
335:         return $count;
336:     }
337: 
338:     339: 340: 341: 342: 343: 344: 
345:     public function addUser($avatar_id, $user_id)
346:     {
347:         $avatar_id = (int)$avatar_id;
348:         $user_id   = (int)$user_id;
349:         if ($avatar_id < 1 || $user_id < 1) {
350:             return false;
351:         }
352:         $sql = sprintf('DELETE FROM %s WHERE user_id = %u', $this->db->prefix('avatar_user_link'), $user_id);
353:         $this->db->query($sql);
354:         $sql = sprintf('INSERT INTO %s (avatar_id, user_id) VALUES (%u, %u)', $this->db->prefix('avatar_user_link'), $avatar_id, $user_id);
355:         if (!$result = $this->db->query($sql)) {
356:             return false;
357:         }
358: 
359:         return true;
360:     }
361: 
362:     363: 364: 365: 366: 367: 
368:     public function getUser(XoopsAvatar $avatar)
369:     {
370:         $ret = array();
371:         372: 373: 
374:         if (!is_a($avatar, 'xoopsavatar')) {
375:             return false;
376:         }
377:         $sql = 'SELECT user_id FROM ' . $this->db->prefix('avatar_user_link') . ' WHERE avatar_id=' . $avatar->getVar('avatar_id');
378:         if (!$result = $this->db->query($sql)) {
379:             return $ret;
380:         }
381:         while ($myrow = $this->db->fetchArray($result)) {
382:             $ret[] = &$myrow['user_id'];
383:         }
384: 
385:         return $ret;
386:     }
387: 
388:     389: 390: 391: 392: 393: 394: 
395:     public function getList($avatar_type = null, $avatar_display = null)
396:     {
397:         $criteria = new CriteriaCompo();
398:         if (isset($avatar_type)) {
399:             $avatar_type = ($avatar_type === 'C') ? 'C' : 'S';
400:             $criteria->add(new Criteria('avatar_type', $avatar_type));
401:         }
402:         if (isset($avatar_display)) {
403:             $criteria->add(new Criteria('avatar_display', (int)$avatar_display));
404:         }
405:         $avatars = &$this->getObjects($criteria, true);
406:         $ret     = array(
407:             'blank.gif' => _NONE);
408:         foreach (array_keys($avatars) as $i) {
409:             $ret[$avatars[$i]->getVar('avatar_file')] = $avatars[$i]->getVar('avatar_name');
410:         }
411: 
412:         return $ret;
413:     }
414: }
415: