X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Fprofile%2Fdecos.inc.php;h=3adbafaf7eec08d3bcc0ce83e147685ccfe88eec;hb=f036c8966e64d9f2bb3923bbeb278353bc7e1083;hp=df971474a0727199929ca92fc57d48a8ded3f1af;hpb=301e3543efce642475e428cfab02a8a38b4ed3f8;p=platal.git diff --git a/modules/profile/decos.inc.php b/modules/profile/decos.inc.php index df97147..3adbafa 100644 --- a/modules/profile/decos.inc.php +++ b/modules/profile/decos.inc.php @@ -1,6 +1,6 @@ $b['grade']; + } + return $a['id'] > $b['id']; + } + + public function value(ProfilePage $page, $field, $value, &$success) { $success = true; if (is_null($value)) { // Fetch already attributed medals - $res = XDB::iterRow("SELECT m.id AS id, s.gid AS grade - FROM profile_medals_sub AS s - INNER JOIN profile_medals AS m ON ( s.mid = m.id ) - WHERE s.uid = {?}", - S::i('uid')); - $value = array(); - while (list($id, $grade) = $res->next()) { - $value[$id] = array('grade' => $grade, - 'valid' => '1'); - } + $value = XDB::fetchAllAssoc('SELECT mid AS id, gid AS grade, 1 AS valid + FROM profile_medals + WHERE pid = {?}', + $page->pid()); // Fetch not yet validated medals - require_once('validations.inc.php'); - $medals = Validate::get_typed_requests(S::i('uid'), 'medal'); + $medals = ProfileValidate::get_typed_requests($page->pid(), 'medal'); foreach ($medals as &$medal) { - $value[$medal->mid] = array('grade' => $medal->gid, - 'valid' => '0'); + $value[] = array( + 'id' => $medal->mid, + 'grade' => $medal->gid, + 'valid' => '0' + ); } - } else if (!is_array($value)) { + } elseif (!is_array($value)) { $value = array(); } - ksort($value); + usort($value, 'self::compareMedals'); return $value; } - public function save(ProfilePage &$page, $field, $value) + public function save(ProfilePage $page, $field, $value) { - require_once('validations.inc.php'); + $original =& $page->orig[$field]; - $orig =& $page->orig[$field]; + $i = $j = 0; + $total_original = count($original); + $total_value = count($value); - // Remove old ones - foreach ($orig as $id=>&$val) { - if (!isset($value[$id]) || $val['grade'] != $value[$id]['grade']) { - if ($val['valid']) { - XDB::execute("DELETE FROM profile_medals_sub - WHERE uid = {?} AND mid = {?}", - S::i('uid'), $id); + while ($i < $total_original || $j < $total_value) { + if (isset($value[$j]) && (!isset($original[$i]) || self::compareMedals($original[$i], $value[$j]))) { + $req = new MedalReq(S::user(), $page->profile, $value[$j]['id'], $value[$j]['grade']); + $req->submit(); + sleep(1); + ++$j; + } elseif (isset($original[$i]) && (!isset($value[$j]) || self::compareMedals($value[$j], $original[$i]))) { + if ($original[$i]['valid']) { + XDB::execute('DELETE FROM profile_medals + WHERE pid = {?} AND mid = {?} AND gid = {?}', + $page->pid(), $original[$i]['id'], $original[$i]['grade']); } else { - $req = MedalReq::get_request(S::i('uid'), $id); + $req = MedalReq::get_request($page->pid(), $original[$i]['id'], $original[$i]['grade']); if ($req) { $req->clean(); } } + ++$i; + } else { + ++$i; + ++$j; } } + } - // Add new ones - foreach ($value as $id=>&$val) { - if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) { - $req = new MedalReq(S::i('uid'), $id, $val['grade']); - $req->submit(); - } + public function getText($value) { + $medalsList = DirEnum::getOptions(DirEnum::MEDALS); + $medals = array(); + foreach ($value as $id => $medal) { + $medals[] = $medalsList[$id]; } + return implode(', ', $medals); } } -class ProfileDecos extends ProfilePage +class ProfilePageDecos extends ProfilePage { protected $pg_template = 'profile/deco.tpl'; - public function __construct(PlWizard &$wiz) + public function __construct(PlWizard $wiz) { parent::__construct($wiz); - $this->settings['medals'] = new ProfileDeco(); - $this->settings['medals_pub'] = new ProfilePub(); + $this->settings['medals'] = new ProfileSettingDeco(); + $this->settings['medals_pub'] = new ProfileSettingPub(); $this->watched['medals'] = true; } protected function _fetchData() { - $res = XDB::query("SELECT profile_medals_pub - FROM auth_user_quick - WHERE user_id = {?}", - S::i('uid')); + $res = XDB::query("SELECT medals_pub + FROM profiles + WHERE pid = {?}", + $this->pid()); $this->values['medals_pub'] = $res->fetchOneCell(); } protected function _saveData() { if ($this->changed['medals_pub']) { - XDB::execute("UPDATE auth_user_quick - SET profile_medals_pub = {?} - WHERE user_id = {?}", - $this->values['medals_pub'], S::i('uid')); + XDB::execute("UPDATE profiles + SET medals_pub = {?} + WHERE pid = {?}", + $this->values['medals_pub'], $this->pid()); } } - public function _prepare(PlPage &$page, $id) + public function _prepare(PlPage $page, $id) { - $res = XDB::iterator("SELECT *, FIND_IN_SET('validation', flags) AS validate - FROM profile_medals - ORDER BY type, text"); - $mlist = array(); + $res = XDB::iterator('SELECT *, FIND_IN_SET(\'validation\', flags) AS validate + FROM profile_medal_enum + ORDER BY type, text'); + $mlist = array(); while ($tmp = $res->next()) { $mlist[$tmp['type']][] = $tmp; } $page->assign('medal_list', $mlist); - $trad = Array('ordre' => 'Ordres', - 'croix' => 'Croix', - 'militaire' => 'Médailles militaires', - 'honneur' => 'Médailles d\'honneur', - 'resistance' => 'Médailles de la résistance', - 'prix' => 'Prix'); - $page->assign('trad', $trad); + $fullType = array( + 'ordre' => 'Ordres', + 'croix' => 'Croix', + 'militaire' => 'Médailles militaires', + 'honneur' => 'Médailles d\'honneur', + 'resistance' => 'Médailles de la résistance', + 'prix' => 'Prix', + 'sport' => 'Médailles sportives' + ); + $page->assign('fullType', $fullType); } }