[TASK] updated contact-form-layout
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* p01-contact - A simple contact forms manager
|
||||
* p01-contact - A simple contact forms manager.
|
||||
*
|
||||
* @see https://github.com/nliautaud/p01contact
|
||||
*
|
||||
* @link https://github.com/nliautaud/p01contact
|
||||
* @author Nicolas Liautaud
|
||||
* @package p01contact
|
||||
*/
|
||||
|
||||
namespace P01C;
|
||||
|
||||
class P01contactField
|
||||
{
|
||||
private $form;
|
||||
|
||||
public $id;
|
||||
public $type;
|
||||
public $title;
|
||||
@@ -22,10 +21,11 @@ class P01contactField
|
||||
public $required;
|
||||
public $locked;
|
||||
public $error;
|
||||
private $form;
|
||||
|
||||
/**
|
||||
* @param Form $form the container form
|
||||
* @param int $id the field id
|
||||
* @param Form $form the container form
|
||||
* @param int $id the field id
|
||||
* @param string $type the field type
|
||||
*/
|
||||
public function __construct($form, $id, $type)
|
||||
@@ -36,7 +36,7 @@ class P01contactField
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the field value or selected value
|
||||
* Set the field value or selected value.
|
||||
*
|
||||
* @param mixed $new_value the value, or an array of selected values ids
|
||||
*/
|
||||
@@ -45,11 +45,12 @@ class P01contactField
|
||||
// simple value
|
||||
if (!is_array($this->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 = "<div class=\"field inline $type $orig $required\">";
|
||||
} else {
|
||||
$html = "<div class=\"field $type $orig $required\">";
|
||||
$is_single_option = is_array($this->value) && 1 == count($this->value) ? 'inline' : '';
|
||||
$html = "<div class=\"row field {$is_single_option} {$type} {$orig} {$required}\">";
|
||||
|
||||
$html .= '<div class="col-sm-12 col-md-3">';
|
||||
if ('' === $is_single_option) {
|
||||
$html .= $this->htmlLabel($id);
|
||||
}
|
||||
$html .= '</div>';
|
||||
|
||||
$html .= '<div class="col-sm-12 col-md">';
|
||||
switch ($type) {
|
||||
case 'textarea':
|
||||
$html .= '<textarea id="' . $id . '" rows="10" ';
|
||||
$html .= 'name="' . $name . '"' . $disabled.$required.$placeholder;
|
||||
$html .= '>' . $value . '</textarea>';
|
||||
$html .= '<textarea id="'.$id.'" rows="10" ';
|
||||
$html .= 'name="'.$name.'"'.$disabled.$required.$placeholder;
|
||||
$html .= '>'.$value.'</textarea>';
|
||||
|
||||
break;
|
||||
case 'captcha':
|
||||
$key = $this->form->config('recaptcha_public_key');
|
||||
if (!$key) {
|
||||
break;
|
||||
}
|
||||
if ($this->form->getId() == 1) {
|
||||
if (1 == $this->form->getId()) {
|
||||
$html .= '<script src="https://www.google.com/recaptcha/api.js"></script>';
|
||||
}
|
||||
$html .='<div class="g-recaptcha" id="'.$id.'" data-sitekey="'.$key.'"></div>';
|
||||
$html .="<input type=\"hidden\" id=\"$id\" name=\"$name\" value=\"trigger\">";
|
||||
$html .= '<div class="g-recaptcha" id="'.$id.'" data-sitekey="'.$key.'"></div>';
|
||||
$html .= "<input type=\"hidden\" id=\"{$id}\" name=\"{$name}\" value=\"trigger\">";
|
||||
|
||||
break;
|
||||
case 'checkbox':
|
||||
case 'radio':
|
||||
$html .= '<div class="options">';
|
||||
$html .= '<div class="options row">';
|
||||
foreach ($this->value as $i => $v) {
|
||||
$selected = $this->isSelected($i) ? ' checked' : '';
|
||||
$v = !empty($v) ? $v : 'Default';
|
||||
$html .= '<label class="option">';
|
||||
$html .= '<label class="option col-sm-12">';
|
||||
$html .= "<input id=\"{$id}_option{$i}\"";
|
||||
$html .= " type=\"$type\" class=\"$type\" name=\"{$name}\"";
|
||||
$html .= " value=\"$i\"$disabled$required$selected />$v";
|
||||
$html .= " type=\"{$type}\" class=\"{$type}\" name=\"{$name}\"";
|
||||
$html .= " value=\"{$i}\"{$disabled}{$required}{$selected} />{$v}";
|
||||
$html .= '</label>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
|
||||
break;
|
||||
case 'select':
|
||||
$html .= "<select id=\"$id\" name=\"$name\"$disabled$required>";
|
||||
$html .= "<select id=\"{$id}\" name=\"{$name}\"{$disabled}{$required}>";
|
||||
foreach ($this->value as $i => $v) {
|
||||
$value = !empty($v) ? $v : 'Default';
|
||||
$selected = $this->isSelected($i) ? ' selected="selected"' : '';
|
||||
$html .= "<option id=\"{$id}_option{$i}\" value=\"$i\"$selected>";
|
||||
$html .= $value . '</option>';
|
||||
$html .= "<option id=\"{$id}_option{$i}\" value=\"{$i}\"{$selected}>";
|
||||
$html .= $value.'</option>';
|
||||
}
|
||||
$html.= '</select>';
|
||||
$html .= '</select>';
|
||||
|
||||
break;
|
||||
default:
|
||||
$html .= '<input id="' . $id . '" ';
|
||||
$html .= 'name="' . $name . '" type="'.$type.'" ';
|
||||
$html .= 'value="' . $value . '"' . $disabled.$required.$placeholder . ' />';
|
||||
$html .= '<input id="'.$id.'" ';
|
||||
$html .= 'name="'.$name.'" type="'.$type.'" ';
|
||||
$html .= 'value="'.$value.'"'.$disabled.$required.$placeholder.' />';
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$html .= '</div>';
|
||||
$html .= '</div>';
|
||||
|
||||
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 = '<table style="width: 100%; margin: 1em 0; border-collapse: collapse;">';
|
||||
|
||||
@@ -281,7 +302,7 @@ class P01contactField
|
||||
// properties
|
||||
$html .= '<td style="padding:.5em 1em; text-transform:lowercase; text-align:right; font-size:.875em; color:#888888; vertical-align: middle"><em>';
|
||||
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 .= '</em></td></tr>';
|
||||
$html .= "\n\n";
|
||||
|
||||
// value
|
||||
if (!$this->value) {
|
||||
return $html . '</table>';
|
||||
return $html.'</table>';
|
||||
}
|
||||
$html .= '<tr><td colspan=2 style="padding:0">';
|
||||
$html .= '<div style="padding:.5em 1.5em;border:1px solid #ccc">';
|
||||
@@ -323,15 +344,18 @@ class P01contactField
|
||||
$html .= empty($v) ? 'Default' : $v;
|
||||
$html .= "</div>\n";
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
$address = '~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~';
|
||||
$val = nl2br(preg_replace($address, '<a href="\\0">\\0</a>', $this->value));
|
||||
$html .= "<p style=\"margin:0\">$val</p>";
|
||||
$html .= "<p style=\"margin:0\">{$val}</p>";
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$html .= '</div></td></tr></table>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
@@ -347,19 +371,20 @@ class P01contactField
|
||||
*/
|
||||
private function htmlLabel($for)
|
||||
{
|
||||
$html = '<label for="' . $for . '">';
|
||||
$html .= '<label for="'.$for.'" class="doc">';
|
||||
if ($this->title) {
|
||||
$html .= $this->title;
|
||||
} else {
|
||||
$html .= ucfirst($this->form->lang($this->type));
|
||||
}
|
||||
if ($this->description) {
|
||||
$html .= ' <em class="description">' . $this->description . '</em>';
|
||||
$html .= ' <em class="description">'.$this->description.'</em>';
|
||||
}
|
||||
if ($this->error) {
|
||||
$html .= ' <span class="error-msg">' . $this->form->lang($this->error) . '</span>';
|
||||
$html .= ' <span class="error-msg">'.$this->form->lang($this->error).'</span>';
|
||||
}
|
||||
$html .= '</label>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
@@ -368,22 +393,23 @@ class P01contactField
|
||||
*/
|
||||
private function getGeneralType()
|
||||
{
|
||||
$types = array(
|
||||
'name' => 'text',
|
||||
$types = [
|
||||
'name' => 'text',
|
||||
'subject' => 'text',
|
||||
'message' => 'textarea',
|
||||
'askcopy' => 'checkbox'
|
||||
);
|
||||
'askcopy' => 'checkbox',
|
||||
];
|
||||
if (isset($types[$this->type])) {
|
||||
return $types[$this->type];
|
||||
}
|
||||
|
||||
return $this->type;
|
||||
}
|
||||
}
|
||||
|
||||
function preint($arr, $return = false)
|
||||
{
|
||||
$out = '<pre class="test" style="white-space:pre-wrap;">' . print_r(@$arr, true) . '</pre>';
|
||||
$out = '<pre class="test" style="white-space:pre-wrap;">'.print_r(@$arr, true).'</pre>';
|
||||
if ($return) {
|
||||
return $out;
|
||||
}
|
||||
@@ -402,5 +428,6 @@ function unset_r($a, $i)
|
||||
unset($a[$k][$i]);
|
||||
}
|
||||
}
|
||||
|
||||
return $a;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user