+class ProfileSettingHobby implements ProfileSetting
+{
+ private $pub;
+ static private $type = array('Sport', 'Loisir', 'Hobby');
+
+ public function __construct()
+ {
+ $this->pub = new ProfileSettingPub();
+ }
+
+ public function value(ProfilePage $page, $field, $value, &$success)
+ {
+ $success = true;
+ if (is_null($value)) {
+ $value = XDB::fetchAllAssoc('SELECT type, text, pub
+ FROM profile_hobby
+ WHERE pid = {?}',
+ $page->pid());
+ }
+ if (!is_array($value)) {
+ return array();
+ }
+ foreach($value as $i => &$hobby) {
+ $hobby['text'] = trim($hobby['text']);
+ if (!$hobby['text'] ||!in_array($hobby['type'], self::$type)) {
+ unset($value[$i]);
+ } else {
+ if (!isset($hobby['pub'])) {
+ $hobby['pub'] = 'private';
+ }
+ $s = true;
+ $hobby['pub'] = $this->pub->value($page, 'pub', $hobby['pub'], $s);
+ $success = $success && $s;
+ }
+ }
+ return $value;
+ }
+
+ public function save(ProfilePage $page, $field, $value)
+ {
+ XDB::execute('DELETE FROM profile_hobby
+ WHERE pid = {?}',
+ $page->pid());
+ if (!count($value)) {
+ return;
+ }
+ foreach ($value as $id => $hobby) {
+ XDB::execute("INSERT INTO profile_hobby (pid, id, type, text, pub)
+ VALUES ({?}, {?}, {?}, {?}, {?})",
+ $page->pid(), $id, $hobby['type'], $hobby['text'], $hobby['pub']);
+ }
+ }
+
+ public function getText($value) {
+ static $pubs = array('public' => 'publique', 'private' => 'privé');
+ $hobbies = array();
+ foreach (self::$type as $type) {
+ $hobbies[$type] = array();
+ }
+ foreach ($value as $hobby) {
+ $hobbies[$hobby['type']][] = $hobby['text'] . ' (affichage ' . $pubs[$hobby['pub']] . ')';
+ }
+ $text = array();
+ foreach (self::$type as $type) {
+ if (!empty($hobbies[$type])) {
+ $text[] = $hobbies[$type] . ' : ' . implode(', ' , $hobbies[$type]);
+ }
+ }
+ return implode(', ' , $text);
+ }
+}
+
+