1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-23 23:02:58 +02:00

Update the FieldtypeComments classes to support for more output configuration options for CommentForm and CommentList

This commit is contained in:
Ryan Cramer
2018-10-19 11:12:58 -04:00
parent faaf8adec9
commit f5cda0acdb
6 changed files with 213 additions and 61 deletions

View File

@@ -127,6 +127,12 @@ class Comment extends WireData {
*/
protected $quiet = false;
/**
* @var CommentArray|null
*
*/
protected $pageComments = null;
/**
* Construct a Comment and set defaults
*
@@ -253,6 +259,9 @@ class Comment extends WireData {
/**
* Clean a comment string by issuing several filters
*
* @param string $str
* @return string
*
*/
public function cleanCommentString($str) {
$str = strip_tags(trim($str));
@@ -364,11 +373,13 @@ class Comment extends WireData {
*
*/
public function children() {
$field = $this->getField();
$comments = $this->getPage()->get($field->name);
/** @var CommentArray $comments */
$comments = $this->getPageComments();
$children = $comments->makeNew();
$children->setPage($this->getPage());
$children->setField($field);
$page = $this->getPage();
$field = $this->getField();
if($page) $children->setPage($this->getPage());
if($field) $children->setField($this->getField());
$id = $this->id;
foreach($comments as $comment) {
if(!$comment->parent_id) continue;
@@ -377,6 +388,35 @@ class Comment extends WireData {
return $children;
}
/**
* Get array that holds all the comments for the current Page/Field
*
* #pw-internal
*
* @param bool $autoDetect Autodetect from Page and Field if not already set? (default=true)
* @return CommentArray|null
*
*/
public function getPageComments($autoDetect = true) {
if($autoDetect && !$this->pageComments) {
$field = $this->getField();
$this->pageComments = $this->getPage()->get($field->name);
}
return $this->pageComments;
}
/**
* Set the CommentArray that holds all comments for the curent Page/Field
*
* #pw-internal
*
* @param CommentArray $pageComments
*
*/
public function setPageComments(CommentArray $pageComments) {
$this->pageComments = $pageComments;
}
/**
* Render stars markup
*

View File

@@ -86,14 +86,6 @@ class CommentArray extends PaginatedArray implements WirePaginatable {
*
*/
public function render(array $options = array()) {
$defaultOptions = array(
'useGravatar' => ($this->field ? $this->field->get('useGravatar') : ''),
'useVotes' => ($this->field ? $this->field->get('useVotes') : 0),
'useStars' => ($this->field ? $this->field->get('useStars') : 0),
'depth' => ($this->field ? (int) $this->field->get('depth') : 0),
'dateFormat' => ($this->field ? $this->field->get('dateFormat') : 'relative'),
);
$options = array_merge($defaultOptions, $options);
$commentList = $this->getCommentList($options);
return $commentList->render();
}
@@ -148,6 +140,21 @@ class CommentArray extends PaginatedArray implements WirePaginatable {
*
*/
public function getCommentList(array $options = array()) {
$field = $this->field;
if($field) {
$defaults = array(
'useGravatar' => $field->get('useGravatar'),
'useVotes' => $field->get('useVotes'),
'useStars' => $field->get('useStars'),
'depth' => $field->get('depth'),
'dateFormat' => $field->get('dateFormat'),
);
} else {
$defaults = array(
'dateFormat' => 'relative'
);
}
$options = array_merge($defaults, $options);
return $this->wire(new CommentList($this, $options));
}
@@ -274,6 +281,7 @@ class CommentArray extends PaginatedArray implements WirePaginatable {
if($getCount) return array($stars, $count);
return $stars;
}
/**
* Render combined star rating for all comments in this CommentsArray
*
@@ -304,6 +312,18 @@ class CommentArray extends PaginatedArray implements WirePaginatable {
if($showCount) $out .= $commentStars->renderCount((int) $count, $stars, $options['schema']);
return $out;
}
/**
* Track an item added
*
* @param Comment $item
* @param int|string $key
*
*/
protected function trackAdd($item, $key) {
parent::trackAdd($item, $key);
if(!$item->getPageComments(false)) $item->setPageComments($this);
}
}

View File

@@ -82,7 +82,9 @@ class CommentForm extends Wire implements CommentFormInterface {
'class' => '',
'rows' => 5,
'cols' => 50,
'form' => '', // any extra attributes for form element
),
'inputWrapTag' => 'p', // tag that wraps label/inputs
'labels' => array(
'cite' => '', // Your Name
'email' => '', // Your E-Mail
@@ -91,7 +93,28 @@ class CommentForm extends Wire implements CommentFormInterface {
'text' => '', // Comments
'submit' => '', // Submit
'starsRequired' => '', // Please select a star rating
),
),
'classes' => array(
'form' => '',
'label' => '',
'labelSpan' => '',
'cite' => 'CommentFormCite {id}_cite',
'citeInput' => 'required',
'email' => 'CommentFormEmail {id}_email',
'emailInput' => 'required email',
'text' => 'CommentFormText {id}_text',
'textInput' => 'required',
'website' => 'CommentFormWebsite {id}_website',
'websiteInput' => 'website',
'stars' => 'CommentFormStars {id}_stars',
'starsRequired' => 'CommentFormStarsRequired',
'honeypot' => 'CommentFormHP {id}_hp',
'notify' => 'CommentFormNotify',
'radioLabel' => '',
'radioInput' => '',
'submit' => 'CommentFormSubmit {id}_submit',
'submitButton' => '',
),
// values that will be already set, perhaps pulled from a user profile for instance (null = ignore)
// note that using presets is NOT cache safe: do not use for non-logged-in users if output caches are active
@@ -100,7 +123,7 @@ class CommentForm extends Wire implements CommentFormInterface {
'email' => null,
'website' => null,
'text' => null,
),
),
// whether or not eht preset values above will be changeable by the user
// applies only for preset values that are not null.
@@ -191,6 +214,14 @@ class CommentForm extends Wire implements CommentFormInterface {
$this->options['labels'][$label] = $value;
}
public function getOptions() {
return $this->options;
}
public function setOptions(array $options) {
$this->options = array_merge($this->options, $options);
}
/**
* Replaces the output of the render() method when a Comment is posted
*
@@ -402,48 +433,68 @@ class CommentForm extends Wire implements CommentFormInterface {
protected function renderFormThread($id, $class, $attrs, $labels, $inputValues) {
$classes = $this->options['classes'];
$classes['form'] .= " $class CommentFormThread";
foreach($classes as $key => $value) {
$classes[$key] = trim(str_replace('{id}', $id, $value));
}
$labelClass = $classes['label'];
$labelClass = $labelClass ? " class='$labelClass'" : "";
$labelSpanClass = $classes['labelSpan'];
$labelSpanClass = $labelSpanClass ? " class='$labelSpanClass'" : "";
$tag = $this->options['inputWrapTag'];
$formAttrs = trim(
"class='$classes[form]' " .
"action='$attrs[action]#$id' " .
"method='$attrs[method]' " .
$attrs['form']
);
$form =
"\n<form class='$class CommentFormThread' action='$attrs[action]#$id' method='$attrs[method]'>" .
"\n\t<p class='CommentFormCite {$id}_cite'>" .
"\n\t\t<label>" .
"\n\t\t\t<span>$labels[cite]</span> " .
"\n\t\t\t<input type='text' name='cite' class='required' required='required' value='$inputValues[cite]' maxlength='128' />" .
"\n<form $formAttrs>" .
"\n\t<$tag class='$classes[cite]'>" .
"\n\t\t<label$labelClass>" .
"\n\t\t\t<span$labelSpanClass>$labels[cite]</span> " .
"\n\t\t\t<input type='text' name='cite' class='$classes[citeInput]' required='required' value='$inputValues[cite]' maxlength='128' />" .
"\n\t\t</label> " .
"\n\t</p>" .
"\n\t<p class='CommentFormEmail {$id}_email'>" .
"\n\t\t<label>" .
"\n\t\t\t<span>$labels[email]</span> " .
"\n\t\t\t<input type='email' name='email' class='required email' required='required' value='$inputValues[email]' maxlength='255' />" .
"\n\t</$tag>" .
"\n\t<$tag class='$classes[email]'>" .
"\n\t\t<label$labelClass>" .
"\n\t\t\t<span$labelSpanClass>$labels[email]</span> " .
"\n\t\t\t<input type='email' name='email' class='$classes[emailInput]' required='required' value='$inputValues[email]' maxlength='255' />" .
"\n\t\t</label>" .
"\n\t</p>";
"\n\t</$tag>";
if($this->commentsField && $this->commentsField->useWebsite && $this->commentsField->schemaVersion > 0) {
$form .=
"\n\t<p class='CommentFormWebsite {$id}_website'>" .
"\n\t\t<label>" .
"\n\t\t\t<span>$labels[website]</span> " .
"\n\t\t\t<input type='text' name='website' class='website' value='$inputValues[website]' maxlength='255' />" .
"\n\t<$tag class='$classes[website]'>" .
"\n\t\t<label$labelClass>" .
"\n\t\t\t<span$labelSpanClass>$labels[website]</span> " .
"\n\t\t\t<input type='text' name='website' class='$classes[websiteInput]' value='$inputValues[website]' maxlength='255' />" .
"\n\t\t</label>" .
"\n\t</p>";
"\n\t</$tag>";
}
if($this->commentsField->useStars && $this->commentsField->schemaVersion > 5) {
$commentStars = new CommentStars();
$starsClass = 'CommentFormStars';
$starsClass = $classes['stars'];
if($this->commentsField->useStars > 1) {
$starsNote = $labels['starsRequired'];
$starsClass .= ' CommentFormStarsRequired';
$starsClass .= " $classes[starsRequired]";
} else {
$starsNote = '';
}
$form .=
"\n\t<p class='$starsClass {$id}_stars' data-note='$starsNote'>" .
"\n\t\t<label>" .
"\n\t\t\t<span>$labels[stars]</span>" .
"\n\t<$tag class='$starsClass' data-note='$starsNote'>" .
"\n\t\t<label$labelClass>" .
"\n\t\t\t<span$labelSpanClass>$labels[stars]</span>" .
"\n\t\t\t<input type='number' name='stars' value='$inputValues[stars]' min='0' max='5' />" .
"\n\t\t\t" . $commentStars->render(0, true) .
"\n\t\t</label>" .
"\n\t</p>";
"\n\t</$tag>";
}
// do we need to show the honeypot field?
@@ -452,34 +503,36 @@ class CommentForm extends Wire implements CommentFormInterface {
$honeypotLabel = isset($labels[$honeypot]) ? $labels[$honeypot] : '';
$honeypotValue = isset($inputValues[$honeypot]) ? $inputValues[$honeypot] : '';
$form .=
"\n\t<p class='CommentFormHP {$id}_hp'>" .
"\n\t<$tag class='$classes[honeypot]'>" .
"\n\t\t<label><span>$honeypotLabel</span>" .
"\n\t\t<input type='text' name='$honeypot' value='$honeypotValue' size='3' />" .
"\n\t\t</label>" .
"\n\t</p>";
"\n\t</$tag>";
}
$form .=
"\n\t<p class='CommentFormText {$id}_text'>" .
"\n\t\t<label>" .
"\n\t\t\t<span>$labels[text]</span>" .
"\n\t\t\t<textarea name='text' class='required' required='required' rows='$attrs[rows]' cols='$attrs[cols]'>$inputValues[text]</textarea>" .
"\n\t<$tag class='$classes[text]'>" .
"\n\t\t<label$labelClass>" .
"\n\t\t\t<span$labelSpanClass>$labels[text]</span>" .
"\n\t\t\t<textarea name='text' class='$classes[textInput]' required='required' rows='$attrs[rows]' cols='$attrs[cols]'>$inputValues[text]</textarea>" .
"\n\t\t</label>" .
"\n\t</p>" .
"\n\t</$tag>" .
$this->renderNotifyOptions() .
"\n\t<p class='CommentFormSubmit {$id}_submit'>" .
"\n\t\t<button type='submit' name='{$id}_submit' value='1'>$labels[submit]</button>" .
"\n\t<$tag class='$classes[submit]'>" .
"\n\t\t<button type='submit' class='$classes[submitButton]' name='{$id}_submit' value='1'>$labels[submit]</button>" .
"\n\t\t<input type='hidden' name='page_id' value='{$this->page->id}' />" .
"\n\t\t<input type='hidden' class='CommentFormParent' name='parent_id' value='0' />" .
"\n\t</p>" .
"\n\t</$tag>" .
"\n</form>";
return $form;
}
protected function renderNotifyOptions() {
if(!$this->commentsField->useNotify) return '';
$out = '';
$tag = $this->options['inputWrapTag'];
$options = array();
@@ -487,22 +540,33 @@ class CommentForm extends Wire implements CommentFormInterface {
$options['2'] = $this->_('Replies');
}
if($this->commentsField->useNotify == Comment::flagNotifyAll) {
$options['4'] = $this->_('All');
}
$classes = array(
'notify' => $this->options['classes']['notify'],
'label' => $this->options['classes']['label'],
'labelSpan' => $this->options['classes']['labelSpan'],
'radioLabel' => $this->options['classes']['radioLabel'],
'radioInput' => $this->options['classes']['radioInput'],
);
foreach($classes as $key => $value) {
$classes[$key] = $value ? " class='" . trim($value) . "'" : "";
}
if(count($options)) {
$out =
"\n\t<p class='CommentFormNotify'>" .
"\n\t\t<label>" . $this->_('E-Mail Notifications:') . "</label> " .
"\n\t\t<label><input type='radio' name='notify' checked='checked' value='0'>" . $this->_('Off') . "</label> ";
"\n\t<$tag$classes[notify]>" .
"\n\t\t<label$classes[label]><span$classes[labelSpan]>" . $this->_('E-Mail Notifications:') . "</span></label> " .
"\n\t\t<label$classes[radioLabel]><input$classes[radioInput] type='radio' name='notify' checked='checked' value='0' /> " . $this->_('Off') . "</label> ";
foreach($options as $value => $label) {
$label = str_replace(' ', '&nbsp;', $label);
$out .= "\n\t\t<label><input type='radio' name='notify' value='$value'>$label</label> ";
$out .= "\n\t\t<label$classes[radioLabel]><input$classes[radioInput] type='radio' name='notify' value='$value' /> $label</label> ";
}
$out .= "\n\t</p>";
$out .= "\n\t</$tag>";
}
return $out;

View File

@@ -315,11 +315,21 @@ class CommentList extends Wire implements CommentListInterface {
* It also populates a session variable 'CommentApprovalMessage' with
* a text message of what occurred.
*
* @param array $options
* @return string
*
*/
public function renderCheckActions() {
public function renderCheckActions(array $options = array()) {
$defaults = array(
'messageIdAttr' => 'CommentApprovalMessage',
'messageMarkup' => "<p id='{id}' class='{class}'><strong>{message}</strong></p>",
'linkMarkup' => "<a href='{href}'>{label}</a>",
'successClass' => 'success',
'errorClass' => 'error',
);
$options = array_merge($defaults, $options);
$action = $this->wire('input')->get('comment_success');
if(empty($action) || $action === "1") return '';
@@ -327,11 +337,22 @@ class CommentList extends Wire implements CommentListInterface {
$message = $this->wire('session')->get('CommentApprovalMessage');
if($message) {
$this->wire('session')->remove('CommentApprovalMessage');
$class = $action === '2' ? 'success' : 'error';
$class = $action === '2' ? $options['successClass'] : $options['errorClass'];
$commentID = (int) $this->wire('input')->get('comment_id');
$message = $this->wire('sanitizer')->entities($message);
if($commentID) $message = str_replace($commentID, "<a href='#Comment$commentID'>$commentID</a>", $message);
return "<p id='CommentApprovalMessage' class='$class'><strong>$message</strong></p>";
if($commentID) {
$link = str_replace(
array('{href}', '{label}'),
array("#Comment$commentID", $commentID),
$options['linkMarkup']
);
$message = str_replace($commentID, $link, $message);
}
return str_replace(
array('{id}', '{class}', '{message}'),
array($options['messageIdAttr'], $class, $message),
$options['messageMarkup']
);
}
}
@@ -345,7 +366,7 @@ class CommentList extends Wire implements CommentListInterface {
if($info['commentID']) $url .= "comment_id=$info[commentID]&";
$url .= "comment_success=" . ($info['success'] ? '2' : '3');
$this->wire('session')->set('CommentApprovalMessage', $info['message']);
$this->wire('session')->redirect($url . '#CommentApprovalMessage');
$this->wire('session')->redirect($url . '#' . $options['messageIdAttr']);
}
return '';

View File

@@ -421,13 +421,16 @@ class InputfieldSelect extends Inputfield {
/**
* Render the given options
*
* @param array $options
* @param bool $allowBlank
* Note: method was protected prior to 3.0.116
*
* @param array|null $options Options array or omit (null) to use already specified options
* @param bool $allowBlank Allow first item to be blank when supported? (default=tru)
* @return string
*
*/
protected function renderOptions($options, $allowBlank = true) {
public function renderOptions($options = null, $allowBlank = true) {
if($options === null) $options = $this->options;
$out = '';
reset($options);
$key = key($options);

View File

@@ -77,10 +77,12 @@ class ProcessPagesExportImport extends Process {
return $form->render();
}
} else {
/*
$this->warning(
'Please note this is a development version of pages export/import ' .
'and not yet recommended for production use.'
);
*/
$form = $this->buildForm();
return $form->render();
}
@@ -307,6 +309,8 @@ class ProcessPagesExportImport extends Process {
'changeStatus' => in_array('status', $fieldNames),
'changeSort' => in_array('sort', $fieldNames),
'filesPath' => $this->wire('session')->getFor($this, 'filesPath'),
'originalHost' => isset($a['host']) ? $a['host'] : $this->wire('config')->httpHost,
'originalRootUrl' => isset($a['url']) ? $a['url'] : $this->wire('config')->urls->root,
);
foreach($a['pages'] as $key => $item) {