]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Make phone numbers save
authorZach Copley <zach@status.net>
Thu, 10 Mar 2011 22:14:21 +0000 (14:14 -0800)
committerZach Copley <zach@status.net>
Thu, 10 Mar 2011 22:14:21 +0000 (14:14 -0800)
plugins/ExtendedProfile/extendedprofile.php
plugins/ExtendedProfile/extendedprofilewidget.php
plugins/ExtendedProfile/profiledetailsettingsaction.php

index 5ad6db114fc87f5aa8f642c155bccd68eeaac531..908f0bc72146f00c080fd21d9dd27d07106e651d 100644 (file)
@@ -39,7 +39,7 @@ class ExtendedProfile
         $this->user     = $profile->getUser();
         $this->sections = $this->getSections();
         $this->fields   = $this->loadFields();
-        common_debug(var_export($this->fields, true));
+        //common_debug(var_export($this->fields, true));
     }
 
     /**
@@ -96,6 +96,21 @@ class ExtendedProfile
         }
     }
 
+
+    function getPhones()
+    {
+        return array(
+                'label' => _m('Phone'),
+                'type'  => 'phone',
+                'multi' => true,
+                'index' => 8123,
+                'rel'   => 'home',
+                'value' => '510-528-0079',
+                'vcard' => 'tel'
+
+        );
+    }
+
     /**
      *  Return all the sections of the extended profile
      *
@@ -140,12 +155,7 @@ class ExtendedProfile
             'contact' => array(
                 'label' => _m('Contact'),
                 'fields' => array(
-                    'phone' => array(
-                        'label' => _m('Phone'),
-                        'type' => 'phone',
-                        'multi' => true,
-                        'vcard' => 'tel',
-                    ),
+                    'phone' => $this->getPhones(),
                     'im' => array(
                         'label' => _m('IM'),
                         'type' => 'im',
index fbb3ff3c23602ff3d3fc664ec04315df8a43a7ed..f5685e9981a234204e90203f53e9e2173e682c35 100644 (file)
@@ -150,10 +150,55 @@ class ExtendedProfileWidget extends Form
         case 'tags':
             $this->out->text($this->ext->getTags());
             break;
+        case 'phone':
+            common_debug("GOT a PHONE!");
+            $this->showPhone($field);
+            break;
         default:
             $this->out->text("TYPE: $type");
         }
+    }
 
+    protected function showPhone($field)
+    {
+        $this->out->elementStart('div', array('class' => 'phone-display'));
+        $this->out->text($field['value']);
+        $this->out->text(' (' . $field['rel'] . ')');
+        $this->out->elementEnd('div');
+    }
+
+    protected function showEditablePhone($name, $field)
+    {
+        $index = $field['index'];
+        $id    = "extprofile-$name-$index";
+        $rel   = $id . '-rel';
+
+        $this->out->elementStart('div', array('class' => 'phone-edit'));
+        $this->out->input($id, null, $field['value']);
+        $this->out->dropdown(
+            $id . '-rel',
+            'Type',
+            array(
+                'office' => 'Office',
+                'mobile' => 'Mobile',
+                'home'   => 'Home',
+                'pager'  => 'Pager',
+                'other'  => 'Other'
+            ),
+            null,
+            false,
+            $field['rel']
+        );
+        if ($field['multi']) {
+            $this->out->element(
+                'a',
+                array(
+                    'name' => $name,
+                    'href' => 'javascript://'),
+                    '+'
+                );
+        }
+        $this->out->elementEnd('div');
     }
 
     /**
@@ -182,6 +227,9 @@ class ExtendedProfileWidget extends Form
         case 'tags':
             $out->input($id, null, $this->ext->getTags());
             break;
+        case 'phone':
+            $this->showEditablePhone($name, $field);
+            break;
         default:
             $out->input($id, null, "TYPE: $type");
         }
index 0f84dc0582039e3fd1225ccaddf7d10d33399342..c18732c0580cf66f47d2817266f561c47d1817f7 100644 (file)
@@ -101,51 +101,122 @@ class ProfileDetailSettingsAction extends SettingsAction
 
         foreach ($simpleFieldNames as $name) {
             $value = $this->trimmed('extprofile-' . $name);
-            $this->saveSimpleField($user, $name, $value);
+            $this->saveField($user, $name, $value);
         }
 
+        $this->savePhoneNumbers($user);
+
         $this->showForm(_('Details saved.'), true);
 
     }
 
-    function saveSimpleField($user, $name, $value)
+    function savePhoneNumbers($user) {
+        $phones = $this->findPhoneNumbers();
+
+        foreach ($phones as $phone) {
+            $this->saveField(
+                $user,
+                'phone',
+                $phone['value'],
+                $phone['rel'],
+                $phone['index']
+            );
+        }
+    }
+
+    function findPhoneNumbers() {
+        $phones      = array();
+        $phoneParams = $this->findMultiParams('phone');
+        ksort($phoneParams); // this sorts them into pairs
+        $phones = $this->arraySplit($phoneParams, sizeof($phoneParams) / 2);
+
+        $phoneTuples = array();
+
+        foreach($phones as $phone) {
+            $firstkey           = array_shift(array_keys($phone));
+            $index              = substr($firstkey, strrpos($firstkey, '-') + 1);
+            list($number, $rel) = array_values($phone);
+
+            $phoneTuples[] = array(
+                'value' => $number,
+                'index' => $index,
+                'rel'   => $rel
+            );
+
+            return $phoneTuples;
+        }
+
+        return $phones;
+    }
+
+    function arraySplit($array, $pieces)
     {
-        $profile = $user->getProfile();
+        if ($pieces < 2) {
+            return array($array);
+        }
+
+        $newCount = ceil(count($array) / $pieces);
+        $a = array_slice($array, 0, $newCount);
+        $b = array_split(array_slice($array, $newCount), $pieces - 1);
 
-        $detail = new Profile_detail();
+        return array_merge(array($a), $b);
+    }
+
+    function findMultiParams($type) {
+        $formVals = array();
+        $target   = $type;
+        foreach ($_POST as $key => $val) {
+            if (strrpos('extprofile-' . $key, $target) !== false) {
+                $formVals[$key] = $val;
+            }
+        }
+        return $formVals;
+    }
+
+    /**
+     * Save an extended profile field as a Profile_detail
+     *
+     * @param User   $user    the current user
+     * @param string $name    field name
+     * @param string $value   field value
+     * @param string $rel     field rel (type)
+     * @param int    $index   index (fields can have multiple values)
+     */
+    function saveField($user, $name, $value, $rel = null, $index = null)
+    {
+        $profile = $user->getProfile();
+        $detail  = new Profile_detail();
 
         $detail->profile_id  = $profile->id;
         $detail->field_name  = $name;
+        $detail->value_index = $index;
 
         $result = $detail->find(true);
 
-
         if (empty($result)) {
-
+            $detial->value_index = $index;
+            $detail->rel         = $rel;
             $detail->field_value = $value;
-
-            $detail->created = common_sql_now();
-
-            common_debug("XXXXXXXXXXXXXXX not found");
-
+            $detail->created     = common_sql_now();
             $result = $detail->insert();
-
             if (empty($result)) {
                 common_log_db_error($detail, 'INSERT', __FILE__);
                 $this->serverError(_m('Could not save profile details.'));
             }
-
         } else {
-
-            common_debug('zzzzz FOUND');
             $orig = clone($detail);
+
             $detail->field_value = $value;
-            $result = $detail->update($orig);
+            $detail->rel         = $rel;
 
+            $result = $detail->update($orig);
+            if (empty($result)) {
+                common_log_db_error($detail, 'UPDATE', __FILE__);
+                $this->serverError(_m('Could not save profile details.'));
+            }
         }
 
         $detail->free();
-
     }
 
     /**
@@ -189,7 +260,7 @@ class ProfileDetailSettingsAction extends SettingsAction
             || $location != $profile->location
             || !empty($newTags)
             || $bio      != $profile->bio) {
+
             $orig = clone($profile);
 
             $profile->nickname = $user->nickname;