From 8675176bb675044c4020d0b91469029a207e8625 Mon Sep 17 00:00:00 2001 From: TLRZ Seyfferth Date: Wed, 29 Jan 2020 14:09:22 +0100 Subject: [PATCH] [TASK] updated contact-form-layout --- plugins/PicoContact/src/P01contact_Field.php | 165 ++++--- plugins/PicoContact/src/P01contact_Form.php | 490 ++++++++++--------- 2 files changed, 362 insertions(+), 293 deletions(-) diff --git a/plugins/PicoContact/src/P01contact_Field.php b/plugins/PicoContact/src/P01contact_Field.php index 9286546..2f08bbe 100644 --- a/plugins/PicoContact/src/P01contact_Field.php +++ b/plugins/PicoContact/src/P01contact_Field.php @@ -1,17 +1,16 @@ value)) { $this->value = htmlentities($new_value, ENT_COMPAT, 'UTF-8', false); + return; } // multiples-values (checkbox, radio, select) if (!is_array($new_value)) { - $new_value = array($new_value); + $new_value = [$new_value]; } foreach ($new_value as $i) { $this->selected_values[intval($i)] = true; @@ -57,11 +58,11 @@ class P01contactField } /** - * Reset the selected values by finding ones who starts or end with ":" + * Reset the selected values by finding ones who starts or end with ":". */ public function resetSelectedValues() { - $this->selected_values = array(); + $this->selected_values = []; foreach ($this->value as $i => $val) { $value = preg_replace('`(^\s*:|:\s*$)`', '', $val, -1, $count); if ($count) { @@ -73,32 +74,38 @@ class P01contactField /** * Check field value. - * @return boolean + * + * @return bool */ public function validate() { // empty and required if (empty($this->value) && $this->required) { $this->error = 'field_required'; + return false; } // value blacklisted or not in whitelist if ($reason = $this->isBlacklisted()) { - $this->error = 'field_' . $reason; + $this->error = 'field_'.$reason; + return false; } // not empty but not valid if (!empty($this->value) && !$this->isValid()) { - $this->error = 'field_' . $this->type; + $this->error = 'field_'.$this->type; + return false; } + return true; } /** * Check if field value is valid - * Mean different things depending on field type - * @return boolean + * Mean different things depending on field type. + * + * @return bool */ public function isValid() { @@ -107,6 +114,7 @@ class P01contactField return filter_var($this->value, FILTER_VALIDATE_EMAIL); case 'tel': $pattern = '`^\+?[-0-9(). ]{6,}$$`i'; + return preg_match($pattern, $this->value); case 'url': return filter_var($this->value, FILTER_VALIDATE_URL); @@ -122,8 +130,11 @@ class P01contactField } /** - * Check if reCaptcha is valid - * @return boolean + * Check if reCaptcha is valid. + * + * @param mixed $answer + * + * @return bool */ public function reCaptchaValidity($answer) { @@ -131,10 +142,10 @@ class P01contactField return false; } $params = [ - 'secret' => $this->form->config('recaptcha_secret_key'), - 'response' => $answer + 'secret' => $this->form->config('recaptcha_secret_key'), + 'response' => $answer, ]; - $url = "https://www.google.com/recaptcha/api/siteverify?" . http_build_query($params); + $url = 'https://www.google.com/recaptcha/api/siteverify?'.http_build_query($params); if (function_exists('curl_version')) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); @@ -154,12 +165,12 @@ class P01contactField } /** - * Check if field value is blacklisted + * Check if field value is blacklisted. * * Search for every comma-separated entry of every checklist * in value, and define if it should or should not be there. * - * @return boolean + * @return bool */ public function isBlacklisted() { @@ -173,14 +184,15 @@ class P01contactField } $content = array_filter(explode(',', $cl->content)); foreach ($content as $avoid) { - $found = preg_match("`$avoid`", $this->value); - $foundBlacklisted = $found && $cl->type == 'blacklist'; - $notFoundWhitelisted = !$found && $cl->type == 'whitelist'; + $found = preg_match("`{$avoid}`", $this->value); + $foundBlacklisted = $found && 'blacklist' == $cl->type; + $notFoundWhitelisted = !$found && 'whitelist' == $cl->type; if ($foundBlacklisted || $notFoundWhitelisted) { return $cl->type; } } } + return false; } @@ -192,8 +204,8 @@ class P01contactField */ public function html() { - $id = 'p01-contact' . $this->form->getId() . '_field' . $this->id; - $name = 'p01-contact_fields[' . $this->id . ']'; + $id = 'p01-contact'.$this->form->getId().'_field'.$this->id; + $name = 'p01-contact_fields['.$this->id.']'; $type = $this->getGeneralType(); $orig = $type != $this->type ? $this->type : ''; $value = $this->value; @@ -201,71 +213,80 @@ class P01contactField $required = $this->required ? ' required ' : ''; $placeholder = $this->placeholder ? ' placeholder="'.$this->placeholder.'"' : ''; - $is_single_option = is_array($this->value) && count($this->value) == 1; - if ($is_single_option) { - $html = "
"; - } else { - $html = "
"; + $is_single_option = is_array($this->value) && 1 == count($this->value) ? 'inline' : ''; + $html = "
"; + + $html .= '
'; + if ('' === $is_single_option) { $html .= $this->htmlLabel($id); } + $html .= '
'; + $html .= '
'; switch ($type) { case 'textarea': - $html .= ''; + break; case 'captcha': $key = $this->form->config('recaptcha_public_key'); if (!$key) { break; } - if ($this->form->getId() == 1) { + if (1 == $this->form->getId()) { $html .= ''; } - $html .='
'; - $html .=""; + $html .= '
'; + $html .= ""; + break; case 'checkbox': case 'radio': - $html .= '
'; + $html .= '
'; foreach ($this->value as $i => $v) { $selected = $this->isSelected($i) ? ' checked' : ''; $v = !empty($v) ? $v : 'Default'; - $html .= '
'; + break; case 'select': - $html .= ""; foreach ($this->value as $i => $v) { $value = !empty($v) ? $v : 'Default'; $selected = $this->isSelected($i) ? ' selected="selected"' : ''; - $html .= "'; + $html .= "'; } - $html.= ''; + $html .= ''; + break; default: - $html .= ''; + $html .= ''; + break; } + $html .= '
'; + $html .= '
'; + return $html; } - /* - * Return a html presentation of the field value. - */ + + // Return a html presentation of the field value. public function htmlMail() { $gen_type = $this->getGeneralType(); - $properties = array(); + $properties = []; $html = ''; @@ -281,7 +302,7 @@ class P01contactField // properties $html .= ''; $html .= "\n\n"; // value if (!$this->value) { - return $html . '
'; if (!$this->value) { - $html .= $this->form->lang('empty') . ' '; + $html .= $this->form->lang('empty').' '; } if ($this->title) { $properties[] = $this->type; @@ -289,21 +310,21 @@ class P01contactField if ($gen_type != $this->type) { $properties[] = $gen_type; } - foreach (array('locked', 'required') as $property) { - if ($this->$property) { + foreach (['locked', 'required'] as $property) { + if ($this->{$property}) { $properties[] = $this->form->lang($property); } } if (count($properties)) { - $html .= '(' . implode(', ', $properties) . ') '; + $html .= '('.implode(', ', $properties).') '; } - $html .= '#' . $this->id; + $html .= '#'.$this->id; $html .= '
'; + return $html.''; } $html .= ''; $html .= '
'; @@ -323,15 +344,18 @@ class P01contactField $html .= empty($v) ? 'Default' : $v; $html .= "
\n"; } + break; default: $address = '~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~'; $val = nl2br(preg_replace($address, '\\0', $this->value)); - $html .= "

$val

"; + $html .= "

{$val}

"; + break; } $html .= '
'; + return $html; } @@ -347,19 +371,20 @@ class P01contactField */ private function htmlLabel($for) { - $html = '