Fixes misplaced assignation.
[platal.git] / modules / profile / general.inc.php
index 4bae6dd..93302c9 100644 (file)
@@ -96,10 +96,12 @@ class ProfileSettingSearchNames implements ProfileSetting
             foreach ($value['public_names'] as $key => $name) {
                 $value['public_names'][$key] = trim($name);
             }
-            foreach ($value['private_names'] as $key => $name) {
-                $value['private_names'][$key]['name'] = trim($name['name']);
-                if ($value['private_names'][$key]['name'] == '') {
-                    unset($value['private_names'][$key]);
+            if (isset($value['private_names'])) {
+                foreach ($value['private_names'] as $key => $name) {
+                    $value['private_names'][$key]['name'] = trim($name['name']);
+                    if ($value['private_names'][$key]['name'] == '') {
+                        unset($value['private_names'][$key]);
+                    }
                 }
             }
 
@@ -110,8 +112,13 @@ class ProfileSettingSearchNames implements ProfileSetting
 
         require_once 'name.func.inc.php';
         $public_name = build_first_name($value['public_names']) . ' ' . build_full_last_name($value['public_names'], $page->profile->isFemale());
-        $private_name_end = build_private_name($value['private_names']);
-        $private_name = $public_name . $private_name_end;
+        if (isset($value['private_names'])) {
+            $private_name_end = build_private_name($value['private_names']);
+            $private_name = $public_name . $private_name_end;
+        } else {
+            $value['private_names'] = array();
+            $private_name = $public_name;
+        }
 
         Platal::page()->assign('public_name', $public_name);
         Platal::page()->assign('private_name', $private_name);
@@ -142,8 +149,10 @@ class ProfileSettingSearchNames implements ProfileSetting
                      $page->pid());
         $values = array();
         $nickname = $lastname = $firstname = 0;
-        foreach ($value['private_names'] as $name) {
-            $values[] = XDB::format('({?}, {?}, {?}, {?})', $page->pid(), $name['type'], $$name['type']++, $name['name']);
+        if (isset($value['private_names'])) {
+            foreach ($value['private_names'] as $name) {
+                $values[] = XDB::format('({?}, {?}, {?}, {?})', $page->pid(), $name['type'], $$name['type']++, $name['name']);
+            }
         }
         if (count($values)) {
             XDB::rawExecute('INSERT INTO  profile_private_names (pid, type, id, name)
@@ -153,7 +162,8 @@ class ProfileSettingSearchNames implements ProfileSetting
         if ($has_diff) {
             update_display_names($page->profile, $old, $value['private_names']);
         } else {
-            update_display_names($page->profile, $value['public_names'], $value['private_names']);
+            update_display_names($page->profile,
+                                 $value['public_names'], (isset($value['private_names']) ? $value['private_names'] : null));
         }
     }
 
@@ -165,7 +175,7 @@ class ProfileSettingSearchNames implements ProfileSetting
             }
         }
 
-        if (count($value['private_names'])) {
+        if (isset($value['private_names']) && count($value['private_names'])) {
             $private_names = array();
             foreach ($value['private_names'] as $name) {
                 $private_names[] = $name['name'];
@@ -338,11 +348,11 @@ class ProfileSettingMainEdu implements ProfileSetting
         $educations = array();
         foreach ($value as $item) {
             $details = array($this->cycles[$item['degreeid']]);
-            if ($education['program']) {
-                $details[] = '« ' . $education['program'] . ' »';
+            if ($item['program']) {
+                $details[] = '« ' . $item['program'] . ' »';
             }
-            if ($education['fieldid']) {
-                $details[] = $fieldsList[$education['fieldid']];
+            if ($item['fieldid']) {
+                $details[] = $fieldsList[$item['fieldid']];
             }
         }
         return implode(', ', $educations);
@@ -460,6 +470,79 @@ class ProfileSettingNetworking implements ProfileSetting
     }
 }
 
+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);
+    }
+}
+
+
 class ProfileSettingPromo implements ProfileSetting
 {
     public function __construct(){}
@@ -557,6 +640,8 @@ class ProfilePageGeneral extends ProfilePage
         $this->settings['edus'] = new ProfileSettingEdu();
         $this->settings['main_edus'] = new ProfileSettingMainEdu();
         $this->settings['promo']  = new ProfileSettingPromo();
+        $this->settings['networking'] = new ProfileSettingNetworking();
+        $this->settings['hobbies'] = new ProfileSettingHobby();
         $this->watched = array('tels' => true,
                                'networking' => true, 'edus' => true,
                                'nationality1' => true, 'nationality2' => true,
@@ -573,7 +658,6 @@ class ProfilePageGeneral extends ProfilePage
         }
         if (S::user()->checkPerms('directory_private')
             || S::user()->isMyProfile($this->owner)) {
-            $this->settings['networking'] = new ProfileSettingNetworking();
             $this->settings['freetext'] = null;
             $this->settings['freetext_pub'] = $this->settings['photo_pub']
                                             = new ProfileSettingPub();