<?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
/*
* Copyright 2014 Osclass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class CommentForm extends Form
{
static public function primary_input_hidden($comment = null)
{
$commentId = null;
if( isset($comment['pk_i_id']) ) {
$commentId = $comment['pk_i_id'];
}
if(Session::newInstance()->_getForm('commentId') != '') {
$commentId = Session::newInstance()->_getForm('commentId');
}
if( !is_null($commentId) ) {
parent::generic_input_hidden("id", $commentId);
}
}
static public function title_input_text($comment = null, $class = '')
{
$commentTitle = '';
if( isset($comment['s_title']) ) {
$commentTitle = $comment['s_title'];
}
if(Session::newInstance()->_getForm('commentTitle') != '') {
$commentTitle = Session::newInstance()->_getForm('commentTitle');
}
parent::generic_input_text("title", $commentTitle, null, false, false, $class);
}
static public function author_input_text($comment = null, $class = '')
{
$commentAuthorName = '';
if( isset($comment['s_author_name']) ) {
$commentAuthorName = $comment['s_author_name'];
}
if(Session::newInstance()->_getForm('commentAuthorName') != '') {
$commentAuthorName = Session::newInstance()->_getForm('commentAuthorName');
}
parent::generic_input_text("authorName", $commentAuthorName, null, false, false, $class);
}
static public function email_input_text($comment = null, $class = '')
{
$commentAuthorEmail = '';
if( isset($comment['s_author_email']) ) {
$commentAuthorEmail = $comment['s_author_email'];
}
if(Session::newInstance()->_getForm('commentAuthorEmail') != '') {
$commentAuthorEmail = Session::newInstance()->_getForm('commentAuthorEmail');
}
parent::generic_input_text("authorEmail", $commentAuthorEmail, null, false, false, $class);
}
static public function body_input_textarea($comment = null, $class = '')
{
$commentBody = '';
if( isset($comment['s_body']) ) {
$commentBody = $comment['s_body'];
}
if(Session::newInstance()->_getForm('commentBody') != '') {
$commentBody = Session::newInstance()->_getForm('commentBody');
}
parent::generic_textarea("body", $commentBody, $class);
}
static public function js_validation($admin = false) {
?>
<script type="text/javascript">
$(document).ready(function(){
$('a[data-comment-action="edit-comment"]').click(function() {
var _this = $(this);
var comment_id = parseInt(_this.attr('data-comment-id'));
$.ajax({
type: 'POST',
url: "<?php echo osc_base_url(true) . "?page=ajax&action=get_comment"; ?>",
data: {'comment_id' : comment_id},
success: function(data){
var json = JSON.parse(data);
if(!$('form[name="comment_form"] input[name="comment_id"]').length) {
$('form[name="comment_form"]').prepend('<input type="hidden" name="comment_id" value="' + comment_id + '">');
$('form[name="comment_form"] input[type="submit"], form[name="comment_form"] button[type="submit"]').parent().append('<a rel="nofollow" data-comment-action="cancel-edit-comment" href="javascript:;" title="<?php _e('Cancel editing a comment'); ?>"><?php _e('Cancel'); ?></a>');
} else {
$('form[name="comment_form"] input[name="comment_id"]').val(comment_id);
}
$('input[name="action"]').val('edit_comment');
$('input[name="title"]').val(json.title);
$('textarea[name="body"]').val(json.text);
$('html, body').animate({ scrollTop: $('form[name="comment_form"]').offset().top}, 600);
}
});
});
$('body').on('click', 'a[data-comment-action="cancel-edit-comment"]', function() {
var _this = $(this);
$('form[name="comment_form"]').trigger('reset');
$('form[name="comment_form"] input[name="comment_id"]').remove();
$('input[name="action"]').val('add_comment');
_this.remove();
});
// Code for form validation
$("form[name=comment_form]").validate({
rules: {
body: {
required: true,
minlength: 1
},
authorEmail: {
required: true,
email: true
}
},
messages: {
authorEmail: {
required: "<?php _e("Email: this field is required"); ?>.",
email: "<?php _e("Invalid email address"); ?>."
},
body: {
required: "<?php _e("Comment: this field is required"); ?>.",
minlength: "<?php _e("Comment: this field is required"); ?>."
}
},
wrapper: "li",
<?php if($admin) { ?>
errorLabelContainer: "#error_list",
invalidHandler: function(form, validator) {
$('html,body').animate({ scrollTop: $('h1').offset().top }, { duration: 250, easing: 'swing'});
},
submitHandler: function(form){
$('button[type=submit], input[type=submit]').attr('disabled', 'disabled');
form.submit();
}
<?php } else { ?>
errorLabelContainer: "#comment_error_list",
invalidHandler: function(form, validator) {
$('html,body').animate({ scrollTop: $('#comment_error_list').offset().top }, { duration: 250, easing: 'swing'});
},
submitHandler: function(form){
$('button[type=submit], input[type=submit]').attr('disabled', 'disabled');
form.submit();
}
<?php }; ?>
});
});
</script>
<?php
}
}
?>