diff --git a/wire/modules/Fieldtype/FieldtypeComments/CommentArray.php b/wire/modules/Fieldtype/FieldtypeComments/CommentArray.php index 0c6e0390..03af23b1 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/CommentArray.php +++ b/wire/modules/Fieldtype/FieldtypeComments/CommentArray.php @@ -5,11 +5,11 @@ * * Maintains an array of multiple Comment instances. * Serves as the value referenced when a FieldtypeComment field is reference from a Page. - * - * ProcessWire 2.x - * Copyright (C) 2010 by Ryan Cramer + * + * ProcessWire 2.x + * Copyright (C) 2010 by Ryan Cramer * Licensed under GNU/GPL v2, see LICENSE.TXT - * + * * http://www.processwire.com * http://www.ryancramer.com * @@ -18,10 +18,10 @@ class CommentArray extends PaginatedArray implements WirePaginatable { /** - * Page that owns these comments, required to use the renderForm() or getCommentForm() methods. + * Page that owns these comments, required to use the renderForm() or getCommentForm() methods. * */ - protected $page = null; + protected $page = null; /** * Field object associated with this CommentArray @@ -31,14 +31,14 @@ class CommentArray extends PaginatedArray implements WirePaginatable { /** * Total number of comments, including those here and others that aren't, but may be here in pagination. - * + * * @var int - * + * */ protected $numTotal = 0; /** - * If this CommentArray is a partial representation of a larger set, this will contain the max number + * If this CommentArray is a partial representation of a larger set, this will contain the max number * of comments allowed to be present/loaded in the CommentArray at once. * * May vary from count() when on the last page of a result set. @@ -46,16 +46,16 @@ class CommentArray extends PaginatedArray implements WirePaginatable { * Applicable for paginated result sets. This number is not enforced for adding items to this CommentArray. * * @var int - * + * */ protected $numLimit = 0; /** - * If this CommentArray is a partial representation of a larger set, this will contain the starting result + * If this CommentArray is a partial representation of a larger set, this will contain the starting result * number if previous results preceded it. * * @var int - * + * */ protected $numStart = 0; @@ -65,9 +65,9 @@ class CommentArray extends PaginatedArray implements WirePaginatable { */ public function isValidItem($item) { if($item instanceof Comment) { - if($this->page) $item->setPage($this->page); - if($this->field) $item->setField($this->field); - return true; + if($this->page) $item->setPage($this->page); + if($this->field) $item->setField($this->field); + return true; } else { return false; } @@ -83,13 +83,14 @@ public function isValidItem($item) { */ public function render(array $options = array()) { $defaultOptions = array( + 'useImageField' => ($this->field ? $this->field->useImageField : ''), 'useGravatar' => ($this->field ? $this->field->useGravatar : ''), - 'useVotes' => ($this->field ? $this->field->useVotes : 0), - 'depth' => ($this->field ? (int) $this->field->depth : 0), - 'dateFormat' => 'relative', + 'useVotes' => ($this->field ? $this->field->useVotes : 0), + 'depth' => ($this->field ? (int) $this->field->depth : 0), + 'dateFormat' => 'relative', ); $options = array_merge($defaultOptions, $options); - $commentList = $this->getCommentList($options); + $commentList = $this->getCommentList($options); return $commentList->render(); } @@ -105,20 +106,20 @@ public function renderForm(array $options = array()) { $defaultOptions = array( 'depth' => ($this->field ? (int) $this->field->depth : 0) ); - $options = array_merge($defaultOptions, $options); - $form = $this->getCommentForm($options); + $options = array_merge($defaultOptions, $options); + $form = $this->getCommentForm($options); return $form->render(); } /** * Render all comments and a comments form below it - * + * * @param array $options * @return string - * + * */ public function renderAll(array $options = array()) { - return $this->render($options) . $this->renderForm($options); + return $this->render($options) . $this->renderForm($options); } /** @@ -126,7 +127,7 @@ public function renderAll(array $options = array()) { * */ public function getCommentList(array $options = array()) { - return new CommentList($this, $options); + return new CommentList($this, $options); } /** @@ -135,35 +136,35 @@ public function getCommentList(array $options = array()) { * @param array $options * @return CommentForm * @throws WireException - * + * */ public function getCommentForm(array $options = array()) { - if(!$this->page) throw new WireException("You must set a page to this CommentArray before using it i.e. \$ca->setPage(\$page)"); - return new CommentForm($this->page, $this, $options); + if(!$this->page) throw new WireException("You must set a page to this CommentArray before using it i.e. \$ca->setPage(\$page)"); + return new CommentForm($this->page, $this, $options); } /** - * Set the page that these comments are on + * Set the page that these comments are on * - */ + */ public function setPage(Page $page) { - $this->page = $page; + $this->page = $page; } /** - * Set the Field that these comments are on + * Set the Field that these comments are on * - */ + */ public function setField(Field $field) { - $this->field = $field; + $this->field = $field; } - + /** * Get the page that these comments are on * */ - public function getPage() { - return $this->page; + public function getPage() { + return $this->page; } /** diff --git a/wire/modules/Fieldtype/FieldtypeComments/CommentForm.php b/wire/modules/Fieldtype/FieldtypeComments/CommentForm.php index ccc80930..c1ebbe2c 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/CommentForm.php +++ b/wire/modules/Fieldtype/FieldtypeComments/CommentForm.php @@ -3,15 +3,15 @@ /** * ProcessWire CommentFormInterface and CommentForm * - * Defines the CommentFormInterface and provides a base/example of this interface with the CommentForm class. + * Defines the CommentFormInterface and provides a base/example of this interface with the CommentForm class. * - * Use of this is optional, and it's primarily here for example purposes. - * You can make your own markup/output for the form directly in your own templates. - * - * ProcessWire 2.x - * Copyright (C) 2014 by Ryan Cramer + * Use of this is optional, and it's primarily here for example purposes. + * You can make your own markup/output for the form directly in your own templates. + * + * ProcessWire 2.x + * Copyright (C) 2014 by Ryan Cramer * Licensed under GNU/GPL v2, see LICENSE.TXT - * + * * http://processwire.com * */ @@ -22,13 +22,13 @@ */ interface CommentFormInterface { public function __construct(Page $page, CommentArray $comments, $options = array()); - public function render(); + public function render(); public function processInput(); } /** * Default/example implementation of the CommentFormInterface - * + * * Generates a user input form for comments, processes comment input, and saves to the page * * @see CommentArray::renderForm() @@ -40,13 +40,13 @@ class CommentForm extends Wire implements CommentFormInterface { * Page object where the comment is being submitted * */ - protected $page; + protected $page; /** * Reference to the Field object used by this CommentForm * */ - protected $commentsField; + protected $commentsField; /** * Instance of CommentArray, containing all Comment instances for this Page @@ -62,8 +62,8 @@ class CommentForm extends Wire implements CommentFormInterface { 'cite' => '', 'email' => '', 'text' => '', - 'notify' => '', - ); + 'notify' => '', + ); protected $postedComment = null; @@ -76,11 +76,11 @@ class CommentForm extends Wire implements CommentFormInterface { 'successMessage' => '', // Thank you, your submission has been saved 'pendingMessage' => '', // Your comment has been submitted and will appear once approved by the moderator. 'errorMessage' => '', // Your submission was not saved due to one or more errors. Try again. - 'processInput' => true, - 'encoding' => 'UTF-8', + 'processInput' => true, + 'encoding' => 'UTF-8', 'attrs' => array( - 'id' => 'CommentForm', - 'action' => './', + 'id' => 'CommentForm', + 'action' => './', 'method' => 'post', 'class' => '', 'rows' => 5, @@ -98,25 +98,31 @@ class CommentForm extends Wire implements CommentFormInterface { // note that using presets is NOT cache safe: do not use for non-logged-in users if output caches are active 'presets' => array( 'cite' => null, - 'email' => null, + '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. + // applies only for preset values that are not null. 'presetsEditable' => false, // the name of a field that must be set (and have any non-blank value), typically set in Javascript to keep out spammers // to use it, YOU must set this with a field from your own javascript, somewhere in the form - 'requireSecurityField' => '', + 'requireSecurityField' => '', // should a redirect be performed immediately after a comment is successfully posted? 'redirectAfterPost' => null, // null=unset (must be set to true to enable) - + + // whether to use custom fields to create Your Name (cite) value + 'yourNameFields' => null, + + // should user fields (Your Name and Your E-mail) be hidden when logged in + 'hideUserFieldsLoggedIn' => 0, + // use threaded comments? - 'depth' => 0, - + 'depth' => 0, + // When a comment is saved to a page, avoid updating the modified time/user 'quietSave' => false, @@ -134,74 +140,78 @@ class CommentForm extends Wire implements CommentFormInterface { public function __construct(Page $page, CommentArray $comments, $options = array()) { $this->page = $page; - $this->comments = $comments; + $this->comments = $comments; // default messages $h3 = $this->_('h3'); // Headline tag $this->options['headline'] = "<$h3>" . $this->_('Post Comment') . "$h3>"; // Form headline - $this->options['successMessage'] = "
" . $this->_('Thank you, your submission has been saved.') . "
"; - $this->options['pendingMessage'] = "" . $this->_('Your comment has been submitted and will appear once approved by the moderator.') . "
"; - $this->options['errorMessage'] = "" . $this->_('Your submission was not saved due to one or more errors. Please check that you have completed all fields before submitting again.') . "
"; + $this->options['successMessage'] = "" . $this->_('Thank you, your submission has been saved.') . "
"; + $this->options['pendingMessage'] = "" . $this->_('Your comment has been submitted and will appear once approved by the moderator.') . "
"; + $this->options['errorMessage'] = "" . $this->_('Your submission was not saved due to one or more errors. Please check that you have completed all fields before submitting again.') . "
"; // default labels - $this->options['labels']['cite'] = $this->_('Your Name'); - $this->options['labels']['email'] = $this->_('Your E-Mail'); - $this->options['labels']['website'] = $this->_('Your Website URL'); - $this->options['labels']['text'] = $this->_('Comments'); - $this->options['labels']['submit'] = $this->_('Submit'); + $this->options['labels']['cite'] = $this->_('Your Name'); + $this->options['labels']['email'] = $this->_('Your E-Mail'); + $this->options['labels']['website'] = $this->_('Your Website URL'); + $this->options['labels']['text'] = $this->_('Comments'); + $this->options['labels']['submit'] = $this->_('Submit'); if(isset($options['labels'])) { - $this->options['labels'] = array_merge($this->options['labels'], $options['labels']); - unset($options['labels']); + $this->options['labels'] = array_merge($this->options['labels'], $options['labels']); + unset($options['labels']); } if(isset($options['attrs'])) { - $this->options['attrs'] = array_merge($this->options['attrs'], $options['attrs']); - unset($options['attrs']); + $this->options['attrs'] = array_merge($this->options['attrs'], $options['attrs']); + unset($options['attrs']); } - $this->options = array_merge($this->options, $options); + $this->options = array_merge($this->options, $options); // determine which field on the page is the commentsField and save the Field instance foreach($this->wire('fields') as $field) { - if(!$field->type instanceof FieldtypeComments) continue; - $value = $this->page->get($field->name); + if(!$field->type instanceof FieldtypeComments) continue; + $value = $this->page->get($field->name); if($value === $this->comments) { $this->commentsField = $field; break; } } - // populate the vlaue of redirectAfterPost + // populate the value of redirectAfterPost if($this->commentsField && is_null($this->options['redirectAfterPost'])) { $this->options['redirectAfterPost'] = (bool) $this->commentsField->redirectAfterPost; } if($this->commentsField && $this->commentsField->quietSave) { - $this->options['quietSave'] = true; + $this->options['quietSave'] = true; + } + // populate the value of yourNameFields + if($this->commentsField && is_null($this->options['yourNameFields'])) { + $this->options['yourNameFields'] = $this->commentsField->yourNameFields; } } public function setAttr($attr, $value) { - $this->options['attrs'][$attr] = $value; + $this->options['attrs'][$attr] = $value; } public function setLabel($label, $value) { - $this->options['labels'][$label] = $value; + $this->options['labels'][$label] = $value; } /** * Replaces the output of the render() method when a Comment is posted * * A success message is shown rather than the form. - * + * * @param Comment|null $comment * @return string * */ protected function renderSuccess(Comment $comment = null) { - $pageID = (int) $this->wire('input')->post->page_id; - + $pageID = (int) $this->wire('input')->post->page_id; + if($pageID && $this->options['redirectAfterPost']) { // redirectAfterPost option - $page = $this->wire('pages')->get($pageID); + $page = $this->wire('pages')->get($pageID); if(!$page->viewable() || !$page->id) $page = $this->wire('page'); $url = $page->id ? $page->url : './'; $url .= "?comment_success=1"; @@ -214,15 +224,15 @@ protected function renderSuccess(Comment $comment = null) { $this->wire('session')->redirect($url); return ''; } - + if(!$this->commentsField || $this->commentsField->moderate == FieldtypeComments::moderateAll) { // all comments are moderated $message = $this->options['pendingMessage']; - + } else if($this->commentsField->moderate == FieldtypeComments::moderateNone) { // no moderation in service $message = $this->options['successMessage']; - + } else if($comment && $comment->status > Comment::statusPending) { // comment is approved $message = $this->options['successMessage']; @@ -230,13 +240,13 @@ protected function renderSuccess(Comment $comment = null) { } else if($this->wire('input')->get('comment_approved') == 1) { // comment was approved in previous request $message = $this->options['successMessage']; - + } else { // other/comment still pending $message = $this->options['pendingMessage']; } - - return "" . - "\n\t\t " . + $out = + "\n\t
" . + "\n\t\t " . "\n\t\t "; - + foreach($options as $value => $label) { - $label = str_replace(' ', ' ', $label); + $label = str_replace(' ', ' ', $label); $out .= "\n\t\t "; } $out .= "\n\t
"; } - - return $out; + + return $out; } /** * Process a submitted CommentForm, insert the Comment, and save the Page - * + * * @return Comment|bool * */ public function processInput() { - $data = $this->wire('input')->post; - if(!count($data)) return false; + $data = $this->wire('input')->post; + if(!count($data)) return false; if($key = $this->options['requireSecurityField']) { - if(empty($data[$key])) return false; + if(empty($data[$key])) return false; } - $comment = new Comment(); - $comment->user_agent = $_SERVER['HTTP_USER_AGENT']; + $comment = new Comment(); + $comment->user_agent = $_SERVER['HTTP_USER_AGENT']; $comment->ip = $this->wire('session')->getIP(); - $comment->created_users_id = $this->user->id; - $comment->sort = count($this->comments)+1; - $comment->parent_id = (int) $data->parent_id; + $comment->created_users_id = $this->user->id; + $comment->sort = count($this->comments)+1; + $comment->parent_id = (int) $data->parent_id; $errors = array(); - // $sessionData = array(); + // $sessionData = array(); foreach(array('cite', 'email', 'website', 'text') as $key) { - if($key == 'website' && (!$this->commentsField || !$this->commentsField->useWebsite)) continue; + if($key == 'website' && (!$this->commentsField || !$this->commentsField->useWebsite)) continue; if($this->options['presetsEditable'] || !isset($this->options['presets'][$key]) || $this->options['presets'][$key] === null) { $comment->$key = $data->$key; // Comment performs sanitization/validation } else { @@ -473,7 +495,7 @@ public function processInput() { } if($key != 'website' && !$comment->$key) $errors[] = $key; $this->inputValues[$key] = $comment->$key; - //if($key != 'text') $sessionData[$key] = $comment->$key; + //if($key != 'text') $sessionData[$key] = $comment->$key; } $flags = 0; @@ -494,15 +516,15 @@ public function processInput() { if(!count($errors)) { if($this->comments->add($comment) && $this->commentsField) { - $outputFormatting = $this->page->outputFormatting; + $outputFormatting = $this->page->outputFormatting; $this->page->setOutputFormatting(false); $saveOptions = array(); - if($this->options['quietSave']) $saveOptions['quiet'] = true; - $this->page->save($this->commentsField->name, $saveOptions); - $this->page->setOutputFormatting($outputFormatting); - $this->postedComment = $comment; + if($this->options['quietSave']) $saveOptions['quiet'] = true; + $this->page->save($this->commentsField->name, $saveOptions); + $this->page->setOutputFormatting($outputFormatting); + $this->postedComment = $comment; // $this->wire('session')->set('CommentForm', $sessionData); - return $comment; + return $comment; } } @@ -514,6 +536,6 @@ public function processInput() { * */ public function getPostedComment() { - return $this->postedComment; + return $this->postedComment; } } diff --git a/wire/modules/Fieldtype/FieldtypeComments/CommentList.php b/wire/modules/Fieldtype/FieldtypeComments/CommentList.php index 2f052f5a..7602db31 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/CommentList.php +++ b/wire/modules/Fieldtype/FieldtypeComments/CommentList.php @@ -4,16 +4,16 @@ * ProcessWire CommentListInterface and CommentList * * CommentListInterface defines an interface for CommentLists. - * CommentList provides the default implementation of this interface. + * CommentList provides the default implementation of this interface. * - * Use of these is not required. These are just here to provide output for a FieldtypeComments field. + * Use of these is not required. These are just here to provide output for a FieldtypeComments field. * Typically you would iterate through the field and generate your own output. But if you just need - * something simple, or are testing, then this may fit your needs. - * - * ProcessWire 2.x - * Copyright (C) 2010 by Ryan Cramer + * something simple, or are testing, then this may fit your needs. + * + * ProcessWire 2.x + * Copyright (C) 2010 by Ryan Cramer * Licensed under GNU/GPL v2, see LICENSE.TXT - * + * * http://www.processwire.com * http://www.ryancramer.com * @@ -24,23 +24,23 @@ * */ interface CommentListInterface { - public function __construct(CommentArray $comments, $options = array()); + public function __construct(CommentArray $comments, $options = array()); public function render(); public function renderItem(Comment $comment); } /** - * CommentList provides the default implementation of the CommentListInterface interface. + * CommentList provides the default implementation of the CommentListInterface interface. * */ class CommentList extends Wire implements CommentListInterface { - + /** * Reference to CommentsArray provided in constructor * */ protected $comments = null; - + protected $page; protected $field; @@ -49,24 +49,25 @@ class CommentList extends Wire implements CommentListInterface { * */ protected $options = array( - 'headline' => '', // '$header
" . - "\n\t\t$text
" . + + $out = + "\n\t$header
" . + "\n\t\t$text
" . "\n\t\t