]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Extended profile - finished basic pattern for adding/removing/saving multiple complex...
authorZach Copley <zach@status.net>
Mon, 14 Mar 2011 08:49:46 +0000 (01:49 -0700)
committerZach Copley <zach@status.net>
Mon, 14 Mar 2011 08:49:46 +0000 (01:49 -0700)
plugins/ExtendedProfile/extendedprofile.php
plugins/ExtendedProfile/extendedprofilewidget.php
plugins/ExtendedProfile/js/profiledetail.js
plugins/ExtendedProfile/profiledetailsettingsaction.php

index 94a5ef482680ddf359000b2d4a0941799c7e0f2d..711fdcf1bb59a1aaba8669804b95b863f4cdd9d0 100644 (file)
@@ -100,7 +100,7 @@ class ExtendedProfile
 
     function getPhones()
     {
-        $phones  = $this->fields['phone'];
+        $phones = (isset($this->fields['phone'])) ? $this->fields['phone'] : null;
         $pArrays = array();
 
         if (empty($phones)) {
@@ -109,22 +109,20 @@ class ExtendedProfile
                 'index' => 0,
                 'type'  => 'phone',
                 'vcard' => 'tel',
-                'multi' => true
+                'rel'   => 'office',
+                'value' => null
             );
         } else {
             for ($i = 0; $i < sizeof($phones); $i++) {
                 $pa = array(
                     'label' => _m('Phone'),
                     'type'  => 'phone',
-                    'index' => intva($phones[$i]->value_index),
+                    'index' => intval($phones[$i]->value_index),
                     'rel'   => $phones[$i]->rel,
                     'value' => $phones[$i]->field_value,
                     'vcard' => 'tel'
                 );
-                // Last phone record should allow adding more
-                if ($i == sizeof($phones) - 1) {
-                    $pa['multi'] = true;
-                }
+
                $pArrays[] = $pa;
             }
         }
index 7eb195e369f90cfde2c67e2c4e919e2b8b7445bd..d8c42df6a3d39ba3ee0806e2fc18ca59defda59a 100644 (file)
@@ -156,7 +156,7 @@ class ExtendedProfileWidget extends Form
             $this->out->text($this->ext->getTags());
             break;
         case 'phone':
-            $this->showPhone($field);
+            $this->showPhone($name, $field);
             break;
         default:
             $this->out->text("TYPE: $type");
@@ -169,7 +169,7 @@ class ExtendedProfileWidget extends Form
         }
     }
 
-    protected function showPhone($field)
+    protected function showPhone($name, $field)
     {
         $this->out->elementStart('div', array('class' => 'phone-display'));
         $this->out->text($field['value']);
@@ -181,7 +181,7 @@ class ExtendedProfileWidget extends Form
 
     protected function showEditablePhone($name, $field)
     {
-        $index = $field['index'];
+        $index = isset($field['index']) ? $field['index'] : 0;
         $id    = "extprofile-$name-$index";
         $rel   = $id . '-rel';
         $this->out->elementStart(
@@ -190,7 +190,11 @@ class ExtendedProfileWidget extends Form
                 'class' => 'phone-edit'
             )
         );
-        $this->out->input($id, null, $field['value']);
+        $this->out->input(
+            $id,
+            null,
+            isset($field['value']) ? $field['value'] : null
+        );
         $this->out->dropdown(
             $id . '-rel',
             'Type',
@@ -203,26 +207,29 @@ class ExtendedProfileWidget extends Form
             ),
             null,
             false,
-            $field['rel']
+            isset($field['rel']) ? $field['rel'] : null
         );
-        if ($field['multi']) {
-            $this->out->element(
-                'a',
-                array(
-                    'class' => 'add_row',
-                    'href' => 'javascript://'),
-                    '+'
-                );
-            $this->out->element(
-                'a',
-                array(
-                    'class' => 'remove_row',
-                    'href' => 'javascript://',
-                    'style' => 'display: none; '
-                    ),
-                    '-'
-                );
-        }
+
+        $this->out->element(
+            'a',
+            array(
+                'class' => 'add_row',
+                'href' => 'javascript://',
+                'style' => 'display: none; '
+            ),
+            '+'
+        );
+
+        $this->out->element(
+            'a',
+            array(
+                'class' => 'remove_row',
+                'href' => 'javascript://',
+                'style' => 'display: none; '
+            ),
+            '-'
+        );
+
         $this->out->elementEnd('div');
     }
 
index a021a32645b6e2ac45d20390ccd75c4ba6329dcd..7d7eceddc107ebe4db27eea85d6c5498618046a3 100644 (file)
@@ -1,43 +1,35 @@
-var removeRow = function() {
-    var cnt = rowCount(this);
-    var table = $(this).closest('table');
-    console.log("row count = " + cnt);
-    if (cnt > 1) {
-        var target = $(this).closest('tr');
-        target.remove();
-        reorder(table);
-    }
-};
-
-var rowCount = function(row) {
-    var top = $(row).closest('table');
-    var trs = $(top).find('tr');
-    return trs.length - 1; // exclude th section header row
-};
+var reorder = function(class) {
+    console.log("QQQ Enter reorder");
 
-var reorder = function(table) {
-    var trs = $(table).find('tr').has('td');
+    var divs = $.find('div[class=' + class + ']');
+    console.log('divs length = ' + divs.length);
 
-    $(trs).find('a').hide();
+    $(divs).find('a').hide();
 
-    $(trs).each(function(i, tr) {
+    $(divs).each(function(i, div) {
         console.log("ROW " + i);
-        $(tr).find('a.remove_row').show();
-        replaceIndex(rowIndex(tr), i);
+        $(div).find('a.remove_row').show();
+        replaceIndex(rowIndex(div), i);
     });
 
-    $(trs).last().find('a.add_row').show();
+    $(divs).last().find('a.add_row').show();
 
-    if (trs.length == 1) {
-        $(trs).find('a.remove_row').hide();
+    if (divs.length == 1) {
+        $(divs).find('a.remove_row').hide();
     }
 
 };
 
-var rowIndex = function(elem) {
-    var idStr = $(elem).find('div').attr('id');
-    var id = idStr.match(/\d+/);
+var rowIndex = function(div) {
+    var idstr = $(div).attr('id');
+    var id = idstr.match(/\d+/);
     console.log("id = " + id);
+    return id;
+};
+
+var rowCount = function(class) {
+    var divs = $.find('div[class=' + class + ']');
+    return divs.length;
 };
 
 var replaceIndex = function(elem, oldIndex, newIndex) {
@@ -46,7 +38,7 @@ var replaceIndex = function(elem, oldIndex, newIndex) {
             var regexp = /extprofile-.*-\d.*/;
             var value = attrib.value;
             var match = value.match(regexp);
-            if (match != null) {
+            if (match !== null) {
                 attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
                 console.log('match: oldIndex = ' + oldIndex + ' newIndex = ' + newIndex + ' name = ' + attrib.name + ' value = ' + attrib.value);
             }
@@ -60,22 +52,42 @@ var resetRow = function(elem) {
 }
 
 var addRow = function() {
-    var divId = $(this).closest('div').attr('id');
-    var index = divId.match(/\d+/);
-    console.log("Current row = " + index);
+    var div = $(this).closest('div');
+    var id = $(div).attr('id');
+    var class = $(div).attr('class');
+    var index = id.match(/\d+/);
+    console.log("Current row = " + index + ', class = ' + class);
     var tr = $(this).closest('tr');
     var newtr = $(tr).clone();
     var newIndex = parseInt(index) + 1;
     replaceIndex(newtr, index, newIndex);
     resetRow(newtr);
     $(tr).after(newtr);
-    console.log("number of rows: " + rowCount(tr));
-    reorder($(this).closest('table'));
+    reorder(class);
 };
 
+var removeRow = function() {
+    var div = $(this).closest('div');
+    var id = $(div).attr('id');
+    var class = $(div).attr('class');
+
+    cnt = rowCount(class);
+    console.debug("removeRow - cnt = " + cnt);
+    if (cnt > 1) {
+        var target = $(this).closest('tr');
+        target.remove();
+        reorder(class);
+    }
+};
+
+var init = function() {
+    reorder('phone-edit');
+}
+
 $(document).ready(
 
 function() {
+    init();
     $('.add_row').live('click', addRow);
     $('.remove_row').live('click', removeRow);
 }
index ee450118c3d7f6eba3dbfd50181da19b6ed14649..89b8d61cf9af267be88822a84c6ae09c7fec23f0 100644 (file)
@@ -107,7 +107,9 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
 
         foreach ($simpleFieldNames as $name) {
             $value = $this->trimmed('extprofile-' . $name);
-            $this->saveField($user, $name, $value);
+            if (!empty($value)) {
+                $this->saveField($user, $name, $value);
+            }
         }
 
         $this->savePhoneNumbers($user);
@@ -118,15 +120,19 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
 
     function savePhoneNumbers($user) {
         $phones = $this->findPhoneNumbers();
-
-        foreach ($phones as $phone) {
-            $this->saveField(
-                $user,
-                'phone',
-                $phone['value'],
-                $phone['rel'],
-                $phone['index']
-            );
+        $this->removeAll($user, 'phone');
+        $i = 0;
+        foreach($phones as $phone) {
+            if (!empty($phone['value'])) {
+                ++$i;
+                $this->saveField(
+                    $user,
+                    'phone',
+                    $phone['value'],
+                    $phone['rel'],
+                    $i
+                );
+            }
         }
     }
 
@@ -223,6 +229,16 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
         $detail->free();
     }
 
+    function removeAll($user, $name)
+    {
+        $profile = $user->getProfile();
+        $detail  = new Profile_detail();
+        $detail->profile_id  = $profile->id;
+        $detail->field_name  = $name;
+        $detail->delete();
+        $detail->free();
+    }
+
     /**
      * Save fields that should be stored in the main profile object
      *