XOOPS  2.6.0
avatar.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
18 
36 {
40  private $userCount;
41 
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 
64  public function id($format = 'n')
65  {
66  return $this->getVar('avatar_id', $format);
67  }
68 
76  public function avatar_id($format = '')
77  {
78  return $this->getVar('avatar_id', $format);
79  }
80 
88  public function avatar_file($format = '')
89  {
90  return $this->getVar('avatar_file', $format);
91  }
92 
100  public function avatar_name($format = '')
101  {
102  return $this->getVar('avatar_name', $format);
103  }
104 
112  public function avatar_mimetype($format = '')
113  {
114  return $this->getVar('avatar_mimetype', $format);
115  }
116 
124  public function avatar_created($format = '')
125  {
126  return $this->getVar('avatar_created', $format);
127  }
128 
136  public function avatar_display($format = '')
137  {
138  return $this->getVar('avatar_display', $format);
139  }
140 
148  public function avatar_weight($format = '')
149  {
150  return $this->getVar('avatar_weight', $format);
151  }
152 
160  public function avatar_type($format = '')
161  {
162  return $this->getVar('avatar_type', $format);
163  }
164 
172  public function setUserCount($value)
173  {
174  $this->userCount = intval($value);
175  }
176 
182  public function getUserCount()
183  {
184  return $this->userCount;
185  }
186 }
187 
194 {
200  public function __construct(Connection $db = null)
201  {
202  parent::__construct($db, 'avatars_avatar', 'AvatarsAvatar', 'avatar_id', 'avatar_name');
203  }
204 
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 
252  public function addUser($avatar_id, $user_id)
253  {
254  $avatar_id = intval($avatar_id);
255  $user_id = intval($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 
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 
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', intval($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 }
getUser(AvatarsAvatar $avatar)
Definition: avatar.php:294
avatar_display($format= '')
Definition: avatar.php:136
$i
Definition: dialog.php:68
avatar_type($format= '')
Definition: avatar.php:160
setUserCount($value)
Definition: avatar.php:172
avatar_id($format= '')
Definition: avatar.php:76
__construct(Connection $db=null)
Definition: avatar.php:200
$result
Definition: pda.php:33
id($format= 'n')
Definition: avatar.php:64
addUser($avatar_id, $user_id)
Definition: avatar.php:252
avatar_name($format= '')
Definition: avatar.php:100
getListByType($avatar_type=null, $avatar_display=null)
Definition: avatar.php:320
getVar($key, $format= 's')
avatar_file($format= '')
Definition: avatar.php:88
avatar_created($format= '')
Definition: avatar.php:124
avatar_weight($format= '')
Definition: avatar.php:148
getObjects(CriteriaElement $criteria=null, $id_as_key=false, $as_object=true)
avatar_mimetype($format= '')
Definition: avatar.php:112
$avatar
Definition: userinfo.php:94
$criteria
getObjectsWithCount(CriteriaElement $criteria=null, $id_as_key=false)
Definition: avatar.php:213
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options= '')
$user_id
Definition: update.php:57