Habari\Post::comment_form PHP Method

comment_form() public method

Manage this post's comment form
public comment_form ( string $context = 'public' ) : FormUI
$context string The context in which the form is used, used to facilitate plugin alteration of the comment form in different circumstances
return FormUI The comment form for this post
    public function comment_form($context = 'public')
    {
        // Handle comment submissions and default commenter id values
        $cookie = 'comment_' . Options::get('public-GUID');
        $commenter_name = '';
        $commenter_email = '';
        $commenter_url = '';
        $commenter_content = '';
        $user = User::identify();
        if ($user->loggedin) {
            $commenter_name = $user->displayname;
            $commenter_email = $user->email;
            $commenter_url = Site::get_url('habari');
        } elseif (isset($_COOKIE[$cookie])) {
            // limit to 3 elements so a # in the URL stays appended
            $commenter = explode('#', $_COOKIE[$cookie], 3);
            // make sure there are always at least 3 elements
            $commenter = array_pad($commenter, 3, null);
            list($commenter_name, $commenter_email, $commenter_url) = $commenter;
        }
        // Now start the form.
        $form = new FormUI('comment-' . $context, 'comment');
        $form->add_class($context);
        $form->add_class('commentform');
        $form->set_wrap_each('<div>%s</div>');
        $form->set_setting('use_session_errors', true);
        // Enforce commenting rules
        if (Options::get('comments_disabled')) {
            $form->append(new FormControlStatic('message', _t('Comments are disabled site-wide.')));
            $form->class[] = 'comments_disabled';
            $form->set_properties(array('action' => '/'));
        } elseif ($this->info->comments_disabled) {
            $form->append(new FormControlStatic('message', _t('Comments for this post are disabled.')));
            $form->class[] = 'comments_disabled';
            $form->set_properties(array('action' => '/'));
        } elseif (Options::get('comments_require_logon') && !$user->loggedin) {
            $form->append(new FormControlStatic('message', _t('Commenting on this site requires authentication.')));
            $form->class[] = 'comments_require_logon';
            $form->set_properties(array('action' => '/'));
        } elseif (!$user->can('comment')) {
            $form->append(new FormControlStatic('message', _t('You do not have permission to comment on this site.')));
            $form->class[] = 'comments_require_permission';
            $form->set_properties(array('action' => '/'));
        } else {
            $form->set_properties(array('action' => URL::get('submit_feedback', array('id' => $this->id))));
            // Create the Name field
            $form->append(FormControlLabel::wrap(_t('Name <span class="required">*Required</span>'), FormControlText::create('cf_commenter', 'null:null', array('id' => 'comment_name', 'required' => 'required'))->add_validator('validate_required', _t('The Name field value is required'))));
            // Create the Email field
            $form->append($cf_email = FormControlText::create('cf_email', 'null:null', array('id' => 'comment_email', 'type' => 'email'))->add_validator('validate_email', _t('The Email field value must be a valid email address')));
            if (Options::get('comments_require_id') == 1) {
                $cf_email->add_validator('validate_required', _t('The Email field value must be a valid email address'));
                $cf_email->label(_t('Email <span class="required">*Required</span>'));
                $cf_email->set_property("required", "required");
            } else {
                $cf_email->label(_t('Email'));
            }
            $cf_email->set_value($commenter_email);
            // Create the URL field
            $form->append(FormControlLabel::wrap(_t('Website'), FormControlText::create('cf_url', 'null:null', array('id' => 'comment_url', 'type' => 'url')))->add_validator('validate_url', _t('The Website field value must be a valid URL')));
            $form->cf_url->value = $commenter_url;
            // Create the Comment field
            $form->append(FormControlTextArea::create('cf_content', 'null:null', array('id' => 'comment_content', 'required' => 'required'))->add_validator('validate_required', _t('The Comment field value is required'))->label(_t('Content')));
            $form->cf_content->value = $commenter_content;
            // Create the Submit button
            $form->append(FormControlSubmit::create('cf_submit')->set_properties(array('value' => _t('Submit'))));
            // Let plugins alter this form
            Plugins::act('form_comment', $form, $this, $context);
        }
        // Return the form object
        return $form;
    }