[ExtendedProfile] Clean up of missing array indices handling

This commit is contained in:
Alexei Sorokin 2020-08-07 11:59:17 +03:00 committed by Diogo Peralta Cordeiro
parent 5196b45e62
commit 83ba7030cc

View File

@ -1,30 +1,29 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/* /*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2011, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET')) {
exit(1);
}
/**
* Class for outputting a widget to display or edit * Class for outputting a widget to display or edit
* extended profiles * extended profiles
*
* @copyright 2011 StatusNet, Inc.
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
defined('GNUSOCIAL') || die();
class ExtendedProfileWidget extends Form class ExtendedProfileWidget extends Form
{ {
const EDITABLE = true; const EDITABLE = true;
@ -164,7 +163,7 @@ class ExtendedProfileWidget extends Form
// combine // combine
protected function showPhone($name, $field) protected function showPhone($name, $field)
{ {
$this->out->elementStart('div', array('class' => 'phone-display')); $this->out->elementStart('div', ['class' => 'phone-display']);
if (!empty($field['value'])) { if (!empty($field['value'])) {
$this->out->text($field['value']); $this->out->text($field['value']);
if (!empty($field['rel'])) { if (!empty($field['rel'])) {
@ -178,44 +177,43 @@ class ExtendedProfileWidget extends Form
protected function showIm($name, $field) protected function showIm($name, $field)
{ {
$this->out->elementStart('div', array('class' => 'im-display')); $this->out->elementStart('div', ['class' => 'im-display']);
$this->out->text($field['value'] ?? ''); if (!empty($field['value'])) {
if (!empty($field['rel'])) { $this->out->text($field['value']);
// TRANS: Value between parentheses (phone number, website, or IM address). if (!empty($field['rel'])) {
$outtext = sprintf(_m('(%s)'), $field['rel']); // TRANS: Value between parentheses (phone number, website, or IM address).
$this->out->text(' ' . $outtext); $outtext = sprintf(_m('(%s)'), $field['rel']);
$this->out->text(' ' . $outtext);
}
} }
$this->out->elementEnd('div'); $this->out->elementEnd('div');
} }
protected function showWebsite($name, $field) protected function showWebsite($name, $field)
{ {
$this->out->elementStart('div', array('class' => 'website-display')); $this->out->elementStart('div', ['class' => 'website-display']);
if (!empty($field['value'])) {
$url = $field['value'];
$url = $field['value']; $this->out->element('a', [
$this->out->element(
"a",
array(
'href' => $url, 'href' => $url,
'class' => 'extended-profile-link', 'class' => 'extended-profile-link',
'target' => "_blank" 'target' => '_blank',
), ], $url);
$url
);
if (!empty($field['rel'])) { if (!empty($field['rel'])) {
// TRANS: Value between parentheses (phone number, website, or IM address). // TRANS: Value between parentheses (phone number, website, or IM address).
$outtext = sprintf(_m('(%s)'), $field['rel']); $outtext = sprintf(_m('(%s)'), $field['rel']);
$this->out->text(' ' . $outtext); $this->out->text(' ' . $outtext);
}
} }
$this->out->elementEnd('div'); $this->out->elementEnd('div');
} }
protected function showEditableIm($name, $field) protected function showEditableIm($name, $field)
{ {
$index = isset($field['index']) ? $field['index'] : 0; $index = (int) ($field['index'] ?? 0);
$id = "extprofile-$name-$index"; $id = "extprofile-{$name}-{$index}";
$rel = $id . '-rel'; $rel = $id . '-rel';
$this->out->elementStart( $this->out->elementStart(
'div', 'div',
@ -227,7 +225,7 @@ class ExtendedProfileWidget extends Form
$this->out->input( $this->out->input(
$id, $id,
null, null,
isset($field['value']) ? $field['value'] : null ($field['value'] ?? null)
); );
$this->out->dropdown( $this->out->dropdown(
$id . '-rel', $id . '-rel',
@ -243,7 +241,7 @@ class ExtendedProfileWidget extends Form
), ),
null, null,
false, false,
isset($field['rel']) ? $field['rel'] : null ($field['rel'] ?? null)
); );
$this->showMultiControls(); $this->showMultiControls();
@ -252,8 +250,8 @@ class ExtendedProfileWidget extends Form
protected function showEditablePhone($name, $field) protected function showEditablePhone($name, $field)
{ {
$index = isset($field['index']) ? $field['index'] : 0; $index = (int) ($field['index'] ?? 0);
$id = "extprofile-$name-$index"; $id = "extprofile-{$name}-{$index}";
$rel = $id . '-rel'; $rel = $id . '-rel';
$this->out->elementStart( $this->out->elementStart(
'div', 'div',
@ -265,7 +263,7 @@ class ExtendedProfileWidget extends Form
$this->out->input( $this->out->input(
$id, $id,
null, null,
isset($field['value']) ? $field['value'] : null ($field['value'] ?? null)
); );
$this->out->dropdown( $this->out->dropdown(
$id . '-rel', $id . '-rel',
@ -279,7 +277,7 @@ class ExtendedProfileWidget extends Form
), ),
null, null,
false, false,
isset($field['rel']) ? $field['rel'] : null ($field['rel'] ?? null)
); );
$this->showMultiControls(); $this->showMultiControls();
@ -288,8 +286,8 @@ class ExtendedProfileWidget extends Form
protected function showEditableWebsite($name, $field) protected function showEditableWebsite($name, $field)
{ {
$index = isset($field['index']) ? $field['index'] : 0; $index = (int) ($field['index'] ?? 0);
$id = "extprofile-$name-$index"; $id = "extprofile-{$name}-{$index}";
$rel = $id . '-rel'; $rel = $id . '-rel';
$this->out->elementStart( $this->out->elementStart(
'div', 'div',
@ -301,7 +299,7 @@ class ExtendedProfileWidget extends Form
$this->out->input( $this->out->input(
$id, $id,
null, null,
isset($field['value']) ? $field['value'] : null ($field['value'] ?? null)
); );
$this->out->dropdown( $this->out->dropdown(
$id . '-rel', $id . '-rel',
@ -317,7 +315,7 @@ class ExtendedProfileWidget extends Form
), ),
null, null,
false, false,
isset($field['rel']) ? $field['rel'] : null ($field['rel'] ?? null)
); );
$this->showMultiControls(); $this->showMultiControls();
@ -368,8 +366,8 @@ class ExtendedProfileWidget extends Form
protected function showEditableExperience($name, $field) protected function showEditableExperience($name, $field)
{ {
$index = isset($field['index']) ? $field['index'] : 0; $index = (int) ($field['index'] ?? 0);
$id = "extprofile-$name-$index"; $id = "extprofile-{$name}-{$index}";
$this->out->elementStart( $this->out->elementStart(
'div', 'div',
array( array(
@ -383,7 +381,7 @@ class ExtendedProfileWidget extends Form
$this->out->input( $this->out->input(
$id, $id,
null, null,
isset($field['company']) ? $field['company'] : null ($field['company'] ?? null)
); );
// TRANS: Field label in extended profile (when did one start a position or education). // TRANS: Field label in extended profile (when did one start a position or education).
@ -458,8 +456,8 @@ class ExtendedProfileWidget extends Form
protected function showEditableEducation($name, $field) protected function showEditableEducation($name, $field)
{ {
$index = isset($field['index']) ? $field['index'] : 0; $index = (int) ($field['index'] ?? 0);
$id = "extprofile-$name-$index"; $id = "extprofile-{$name}-{$index}";
$this->out->elementStart( $this->out->elementStart(
'div', 'div',
array( array(
@ -472,7 +470,7 @@ class ExtendedProfileWidget extends Form
$this->out->input( $this->out->input(
$id, $id,
null, null,
isset($field['school']) ? $field['school'] : null ($field['school'] ?? null)
); );
// TRANS: Field label in extended profile for specifying an academic degree. // TRANS: Field label in extended profile for specifying an academic degree.
@ -480,7 +478,7 @@ class ExtendedProfileWidget extends Form
$this->out->input( $this->out->input(
$id . '-degree', $id . '-degree',
null, null,
isset($field['degree']) ? $field['degree'] : null ($field['degree'] ?? null)
); );
// TRANS: Field label in education edit area of extended profile. // TRANS: Field label in education edit area of extended profile.
@ -489,7 +487,7 @@ class ExtendedProfileWidget extends Form
$this->out->textarea( $this->out->textarea(
$id . '-description', $id . '-description',
null, null,
isset($field['description']) ? $field['description'] : null ($field['description'] ?? null)
); );
// TRANS: Field label in extended profile (when did one start a position or education). // TRANS: Field label in extended profile (when did one start a position or education).
@ -546,7 +544,7 @@ class ExtendedProfileWidget extends Form
*/ */
protected function showFieldValue($name, $field) protected function showFieldValue($name, $field)
{ {
$type = strval(@$field['type']); $type = (string) ($field['type'] ?? '');
switch ($type) { switch ($type) {
case '': case '':
@ -557,7 +555,7 @@ class ExtendedProfileWidget extends Form
break; break;
case 'custom-text': case 'custom-text':
case 'custom-textarea': case 'custom-textarea':
$this->out->text(isset($field['value']) ? $field['value'] : null); $this->out->text($field['value'] ?? '');
break; break;
case 'date': case 'date':
$value = $this->ext->getDateValue($name); $value = $this->ext->getDateValue($name);
@ -603,7 +601,7 @@ class ExtendedProfileWidget extends Form
{ {
$out = $this->out; $out = $this->out;
$type = strval(@$field['type']); $type = (string) ($field['type'] ?? '');
$id = "extprofile-" . $name; $id = "extprofile-" . $name;
$value = 'placeholder'; $value = 'placeholder';
@ -616,7 +614,7 @@ class ExtendedProfileWidget extends Form
break; break;
case 'custom-text': case 'custom-text':
case 'custom-textarea': case 'custom-textarea':
$out->input($id, null, isset($field['value']) ? $field['value'] : null); $out->input($id, null, ($field['value'] ?? null));
break; break;
case 'date': case 'date':
$value = $this->ext->getDateValue($name); $value = $this->ext->getDateValue($name);