* @package Questions/Core * @version 1.0.0 * @since 1.0.0 * @license GPL 2 Copyright 2015 awesome.ug (support@awesome.ug) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ if ( !defined( 'ABSPATH' ) ) exit; abstract class Questions_FormElement { /** * ID of instanced Element * @since 1.0.0 */ var $id = NULL; /** * Contains the Survey ID of the element * @since 1.0.0 */ var $survey_id; /** * Slug of element * @since 1.0.0 */ var $slug; /** * Title of element which will be shown in admin * @since 1.0.0 */ var $title; /** * Description of element * @since 1.0.0 */ var $description; /** * Icon URl of element * @since 1.0.0 */ var $icon_url; /** * Question text * @since 1.0.0 */ var $question; /** * Sort number where to display element * @since 1.0.0 */ var $sort = 0; /** * Contains if this element a question or is this element for anything else (like description element or something else) * @since 1.0.0 */ var $is_question = TRUE; /** * If value is true, Questions will try to create charts from results * @since 1.0.0 */ var $is_analyzable = FALSE; /** * Does this elements has own answers? For example on multiple choice or one choice has answers. * @since 1.0.0 */ var $has_answers = FALSE; var $splits_form = FALSE; /** * Sections for answers * @since 1.0.0 */ var $sections = array(); /** * If elements has answers they will be stored here after populating object. * @since 1.0.0 */ var $answers = array(); /** * Contains users response of element after submitting * @since 1.0.0 */ var $response; /** * The settings field setup * @since 1.0.0 */ var $settings_fields = array(); /** * Contains all settings of the element * @since 1.0.0 */ var $settings = array(); /** * HTML template for answer * @since 1.0.0 */ var $create_answer_syntax; /** * Parameters which have to be added on answer * @since 1.0.0 */ var $create_answer_params = array(); var $answer_is_multiple = FALSE; /** * Control variable if element is already initialized * @since 1.0.0 */ var $initialized = FALSE; /** * Constructor * @param int $id ID of the element * @since 1.0.0 */ public function __construct( $id = NULL ) { if ( NULL != $id && '' != $id ) { $this->populate( $id ); } $this->settings_fields(); } /** * Function to register element in Questions * * After registerung was successfull the new element will be shown in the elements list. * * @return boolean $is_registered Returns TRUE if registering was succesfull, FALSE if not * @since 1.0.0 */ public function _register() { global $questions_global; if ( TRUE == $this->initialized ) { return FALSE; } if ( ! is_object( $questions_global ) ) { return FALSE; } if ( '' == $this->slug ) { $this->slug = get_class( $this ); } if ( '' == $this->title ) { $this->title = ucwords( get_class( $this ) ); } if ( '' == $this->description ) { $this->description = esc_attr__( 'This is a Questions Survey Element.', 'questions-locale' ); } if ( array_key_exists( $this->slug, $questions_global->element_types ) ) { return FALSE; } if ( ! is_array( $questions_global->element_types ) ) { $questions_global->element_types = array(); } $this->initialized = TRUE; return $questions_global->add_form_element( $this->slug, $this ); } /** * Populating element object with data * @param int $id Element id * @since 1.0.0 */ private function populate( $id ) { global $wpdb, $questions_global; $this->question = ''; $this->answers = array(); $sql = $wpdb->prepare( "SELECT * FROM {$questions_global->tables->questions} WHERE id = %s", $id ); $row = $wpdb->get_row( $sql ); $this->id = $id; $this->set_question( $row->question ); $this->questions_id = $row->questions_id; $this->sort = $row->sort; $sql = $wpdb->prepare( "SELECT * FROM {$questions_global->tables->answers} WHERE question_id = %s ORDER BY sort ASC", $id ); $results = $wpdb->get_results( $sql ); if ( is_array( $results ) ): foreach ( $results AS $result ): $this->add_answer( $result->answer, $result->sort, $result->id, $result->section ); endforeach; endif; $sql = $wpdb->prepare( "SELECT * FROM {$questions_global->tables->settings} WHERE question_id = %s", $id ); $results = $wpdb->get_results( $sql ); if ( is_array( $results ) ): foreach ( $results AS $result ): $this->add_setting( $result->name, $result->value ); endforeach; endif; } /** * Setting question for element * @param string $question Question text * @since 1.0.0 */ private function set_question( $question, $order = NULL ) { if ( '' == $question ) { return FALSE; } if ( NULL != $order ) { $this->sort = $order; } $this->question = $question; return TRUE; } /** * Adding answer to object data * @param string $text Answer text * @param int $sort Sort number * @param int $id Answer ID from DB * @param string $section Section of answer * @return boolean $is_added TRUE if answer was added, False if not * @since 1.0.0 */ private function add_answer( $text, $sort = FALSE, $id = NULL, $section = NULL ) { if ( '' == $text ) { return FALSE; } $this->answers[ $id ] = array( 'id' => $id, 'text' => $text, 'sort' => $sort, 'section' => $section ); return TRUE; } /** * Add setting to object data * @param string $name Name of setting * @param string $value Value of setting * @since 1.0.0 */ private function add_setting( $name, $value ) { $this->settings[ $name ] = $value; } public function settings_fields() { } /** * Validate user input - Have to be overwritten by child classes if element needs validation * @return bool * @since 1.0.0 */ public function validate( $input ) { return TRUE; } /** * Drawing Element on frontend * @return string $html Element HTML * @since 1.0.0 */ public function draw() { global $questions_response_errors; $errors = ''; if ( is_array( $questions_response_errors ) && array_key_exists( $this->id, $questions_response_errors ) ) { $errors = $questions_response_errors[ $this->id ]; } $html = ''; $html = apply_filters( 'questions_draw_element_outer_start', $html, $this ); $element_classes = array( 'survey-element', 'survey-element-' . $this->id ); $element_classes = apply_filters( 'questions_element_classes', $element_classes, $this ); $html .= '
' . esc_attr__( 'You donĀ“t entered any answers. Please add some to display answers here.', 'questions-locale' ) . '
'; }else { $html .= $this->input_html(); } $html .= '' . esc_attr__( 'No HTML for Element given. Please check element sourcecode.', 'questions-locale' ) . '
'; } /** * Returns the widget id which will be used in HTML * @return string $widget_id The widget id * @since 1.0.0 */ private function admin_get_widget_id() { // Getting Widget ID if ( NULL == $this->id ): // New Element $widget_id = 'widget_formelement_XXnrXX'; else: // Existing Element $widget_id = 'widget_formelement_' . $this->id; endif; return $widget_id; } /** * Draws element box in Admin * @return string $html The admin element HTML code * @since 1.0.0 */ public function draw_admin() { // Getting id string if ( NULL == $this->id ): // New Element $id_name = ' id="widget_formelement_XXnrXX"'; else: // Existing Element $id_name = ' id="widget_formelement_' . $this->id . '"'; endif; /** * Widget */ $html = ' '; return $html; } /** * Content of the question tab * @since 1.0.0 */ private function admin_widget_question_tab() { $widget_id = $this->admin_get_widget_id(); // Question $html = ''; // Answers if ( $this->has_answers ): // Answers have sections if ( property_exists( $this, 'sections' ) && is_array( $this->sections ) && count( $this->sections ) > 0 ): foreach ( $this->sections as $section_key => $section_name ): $html .= '
' . $section_name . '
'; $html .= $this->admin_widget_question_answers( $section_key ); $html .= ''; $html .= '' . esc_attr__( 'Answer/s:', 'questions-locale' ) . '
'; $html .= $this->admin_widget_question_answers(); endif; endif; $html .= ''; return $html; } /** * Content of the answers under the question * @param string $section Name of the section * @return string $html The answers HTML * @since 1.0.0 */ private function admin_widget_question_answers( $section = NULL ) { $widget_id = $this->admin_get_widget_id(); $html = ''; if ( is_array( $this->answers ) ): $html .= '