| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | class Group |
| 23 | { |
| 24 | public $id; |
| 25 | public $shortname; |
| 26 | private $data = array(); |
| 27 | |
| 28 | private function __construct(array $data) |
| 29 | { |
| 30 | foreach ($data as $key=>$value) { |
| 31 | $this->data[$key] = $value; |
| 32 | } |
| 33 | $this->id = intval($this->data['id']); |
| 34 | $this->shortname = $this->data['diminutif']; |
| 35 | } |
| 36 | |
| 37 | public function __get($name) |
| 38 | { |
| 39 | if (property_exists($this, $name)) { |
| 40 | return $this->$name; |
| 41 | } |
| 42 | |
| 43 | if (isset($this->data[$name])) { |
| 44 | return $this->data[$name]; |
| 45 | } |
| 46 | |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | public function __isset($name) |
| 51 | { |
| 52 | return property_exists($this, $name) || isset($this->data[$name]); |
| 53 | } |
| 54 | |
| 55 | private function getUF($admin = false, $extra_cond = null, $sort = null) |
| 56 | { |
| 57 | $cond = new UFC_Group($this->id, $admin); |
| 58 | if (!is_null($extra_cond)) { |
| 59 | $cond = new PFC_And($cond, $extra_cond); |
| 60 | } |
| 61 | return new UserFilter($cond, $sort); |
| 62 | } |
| 63 | |
| 64 | public function getMembersFilter($extra_cond = null, $sort = null) |
| 65 | { |
| 66 | return $this->getUF(false, $extra_cond, $sort); |
| 67 | } |
| 68 | |
| 69 | public function getAdminsFilter($extra_cond = null, $sort = null) |
| 70 | { |
| 71 | return $this->getUF(true, $extra_cond, $sort); |
| 72 | } |
| 73 | |
| 74 | public function iterMembers($extra_cond = null, $sort = null, $limit = null) |
| 75 | { |
| 76 | $uf = $this->getMembersFilter($extra_cond, $sort); |
| 77 | return $uf->iterUsers($limit); |
| 78 | } |
| 79 | |
| 80 | public function iterAdmins($extra_cond = null, $sort = null, $limit = null) |
| 81 | { |
| 82 | $uf = $this->getAdminsFilter($extra_cond, $sort); |
| 83 | return $uf->iterUsers($limit); |
| 84 | } |
| 85 | |
| 86 | public function getLogo($fallback = true) |
| 87 | { |
| 88 | if (!empty($this->logo)) { |
| 89 | return PlImage::fromData($this->logo, $this->logo_mime); |
| 90 | } else if ($fallback) { |
| 91 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/dflt_carre.jpg', 'image/jpeg'); |
| 92 | } |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | static public function get($id) |
| 97 | { |
| 98 | if (!$id) { |
| 99 | return null; |
| 100 | } |
| 101 | if (is_int($id) || ctype_digit($id)) { |
| 102 | $where = XDB::format('id = {?}', $id); |
| 103 | } else { |
| 104 | $where = XDB::format('diminutif = {?}', $id); |
| 105 | } |
| 106 | $res = XDB::query('SELECT a.*, d.nom AS domnom, |
| 107 | FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc, |
| 108 | FIND_IN_SET(\'notif_unsub\', a.flags) AS notif_unsub |
| 109 | FROM groups AS a |
| 110 | LEFT JOIN group_dom AS d ON d.id = a.dom |
| 111 | WHERE ' . $where); |
| 112 | if ($res->numRows() != 1) { |
| 113 | return null; |
| 114 | } |
| 115 | return new Group($res->fetchOneAssoc()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 120 | ?> |