1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35:
36: class XoopsAvatar extends XoopsObject
37: {
38: var $_userCount;
39:
40: function XoopsAvatar()
41: {
42: $this->XoopsObject();
43: $this->initVar('avatar_id', XOBJ_DTYPE_INT, null, false);
44: $this->initVar('avatar_file', XOBJ_DTYPE_OTHER, null, false, 30);
45: $this->initVar('avatar_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
46: $this->initVar('avatar_mimetype', XOBJ_DTYPE_OTHER, null, false);
47: $this->initVar('avatar_created', XOBJ_DTYPE_INT, null, false);
48: $this->initVar('avatar_display', XOBJ_DTYPE_INT, 1, false);
49: $this->initVar('avatar_weight', XOBJ_DTYPE_INT, 0, false);
50: $this->initVar('avatar_type', XOBJ_DTYPE_OTHER, 0, false);
51: }
52:
53: function setUserCount($value)
54: {
55: $this->_userCount = (int)$value;
56: }
57:
58: function getUserCount()
59: {
60: return $this->_userCount;
61: }
62: }
63:
64:
65: 66: 67: 68: 69: 70: 71: 72:
73:
74: class XoopsAvatarHandler extends XoopsObjectHandler
75: {
76:
77: function &create($isNew = true)
78: {
79: $avatar =new XoopsAvatar();
80: if ($isNew) {
81: $avatar->setNew();
82: }
83: return $avatar;
84: }
85:
86: function &get($id)
87: {
88: $ret = false;
89: $id = (int)$id;
90: if ($id > 0) {
91: $sql = 'SELECT * FROM '.$this->db->prefix('avatar').' WHERE avatar_id='.$id;
92: if ($result = $this->db->query($sql)) {
93: $numrows = $this->db->getRowsNum($result);
94: if ($numrows == 1) {
95: $avatar =new XoopsAvatar();
96: $avatar->assignVars($this->db->fetchArray($result));
97: $ret =& $avatar;
98: }
99: }
100: }
101: return $ret;
102: }
103:
104: function insert(&$avatar)
105: {
106: if (strtolower(get_class($avatar)) != 'xoopsavatar') {
107: return false;
108: }
109: if (!$avatar->isDirty()) {
110: return true;
111: }
112: if (!$avatar->cleanVars()) {
113: return false;
114: }
115: foreach ($avatar->cleanVars as $k => $v) {
116: ${$k} = $v;
117: }
118: if ($avatar->isNew()) {
119: $avatar_id = $this->db->genId('avatar_avatar_id_seq');
120: $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));
121: } else {
122: $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);
123: }
124: if (!$result = $this->db->query($sql)) {
125: return false;
126: }
127: if (empty($avatar_id)) {
128: $avatar_id = $this->db->getInsertId();
129: }
130: $avatar->assignVar('avatar_id', $avatar_id);
131: return true;
132: }
133:
134: function delete(&$avatar)
135: {
136: if (strtolower(get_class($avatar)) != 'xoopsavatar') {
137: return false;
138: }
139: $id = $avatar->getVar('avatar_id');
140: $sql = sprintf("DELETE FROM %s WHERE avatar_id = %u", $this->db->prefix('avatar'), $id);
141: if (!$result = $this->db->query($sql)) {
142: return false;
143: }
144: $sql = sprintf("DELETE FROM %s WHERE avatar_id = %u", $this->db->prefix('avatar_user_link'), $id);
145: $result = $this->db->query($sql);
146: return true;
147: }
148:
149: function &getObjects($criteria = null, $id_as_key = false)
150: {
151: $ret = array();
152: $limit = $start = 0;
153: $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';
154: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
155: $sql .= ' '.$criteria->renderWhere();
156: $sql .= ' GROUP BY a.avatar_id ORDER BY avatar_weight, avatar_id';
157: $limit = $criteria->getLimit();
158: $start = $criteria->getStart();
159: }
160: $result = $this->db->query($sql, $limit, $start);
161: if (!$result) {
162: return $ret;
163: }
164: while ($myrow = $this->db->fetchArray($result)) {
165: $avatar =new XoopsAvatar();
166: $avatar->assignVars($myrow);
167: $avatar->setUserCount($myrow['count']);
168: if (!$id_as_key) {
169: $ret[] =& $avatar;
170: } else {
171: $ret[$myrow['avatar_id']] =& $avatar;
172: }
173: unset($avatar);
174: }
175: return $ret;
176: }
177:
178: function getCount($criteria = null)
179: {
180: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('avatar');
181: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
182: $sql .= ' '.$criteria->renderWhere();
183: }
184: if (!$result =& $this->db->query($sql)) {
185: return 0;
186: }
187: list($count) = $this->db->fetchRow($result);
188: return $count;
189: }
190:
191: function addUser($avatar_id, $user_id){
192: $avatar_id = (int)$avatar_id;
193: $user_id = (int)$user_id;
194: if ($avatar_id < 1 || $user_id < 1) {
195: return false;
196: }
197: $sql = sprintf("DELETE FROM %s WHERE user_id = %u", $this->db->prefix('avatar_user_link'), $user_id);
198: $this->db->query($sql);
199: $sql = sprintf("INSERT INTO %s (avatar_id, user_id) VALUES (%u, %u)", $this->db->prefix('avatar_user_link'), $avatar_id, $user_id);
200: if (!$result =& $this->db->query($sql)) {
201: return false;
202: }
203: return true;
204: }
205:
206: function &getUser(&$avatar){
207: $ret = array();
208: if (strtolower(get_class($avatar)) != 'xoopsavatar') {
209: return $ret;
210: }
211: $sql = 'SELECT user_id FROM '.$this->db->prefix('avatar_user_link').' WHERE avatar_id='.$avatar->getVar('avatar_id');
212: if (!$result = $this->db->query($sql)) {
213: return $ret;
214: }
215: while ($myrow = $this->db->fetchArray($result)) {
216: $ret[] =& $myrow['user_id'];
217: }
218: return $ret;
219: }
220:
221: function &getList($avatar_type = null, $avatar_display = null)
222: {
223: $criteria = new CriteriaCompo();
224: if (isset($avatar_type)) {
225: $avatar_type = ($avatar_type == 'C') ? 'C' : 'S';
226: $criteria->add(new Criteria('avatar_type', $avatar_type));
227: }
228: if (isset($avatar_display)) {
229: $criteria->add(new Criteria('avatar_display', (int)$avatar_display));
230: }
231: $avatars =& $this->getObjects($criteria, true);
232: $ret = array('blank.gif' => _NONE);
233: foreach (array_keys($avatars) as $i) {
234: $ret[$avatars[$i]->getVar('avatar_file')] = $avatars[$i]->getVar('avatar_name');
235: }
236: return $ret;
237: }
238: }
239: ?>
240: