<?php
/***************************************************************************
- * Copyright (C) 2003-2010 Polytechnique.org *
+ * Copyright (C) 2003-2011 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
class ProfileSettingDeco implements ProfileSetting
{
- public function value(ProfilePage &$page, $field, $value, &$success)
+ private static function compareMedals(array $a, array $b)
+ {
+ if ($a['id'] == $b['id']) {
+ return $a['grade'] > $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 AS s
- INNER JOIN profile_medal_enum AS m ON ( s.mid = m.id )
- WHERE s.pid = {?}",
- $page->pid());
- $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 = 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
- WHERE pid = {?} AND mid = {?}",
- $page->pid(), $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($page->pid(), $id);
+ $req = MedalReq::get_request($page->pid(), $original[$i]['id'], $original[$i]['grade']);
if ($req) {
$req->clean();
}
}
- }
- }
-
- // Add new ones
- foreach ($value as $id=>&$val) {
- if (!isset($orig[$id]) || $orig[$id]['grade'] != $val['grade']) {
- $req = new MedalReq(S::user(), $page->profile, $id, $val['grade']);
- $req->submit();
- sleep(1);
+ ++$i;
+ } else {
+ ++$i;
+ ++$j;
}
}
}
}
}
-class ProfileSettingDecos 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 ProfileSettingDeco();
}
}
- 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_medal_enum
- 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);
}
}