<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('QuestionsUlp')):
class QuestionsUlp extends CustomPostTypeUlp{
	/*
	 * @var string
	 */
	protected $post_type_slug = 'ulp_question';
	/*
	 * @var int
	 */
	protected $menu_position = 8;
	/**
	 * @var array
	 */
	protected $supports = array('editor', 'thumbnail', 'author');
	public function __construct(){
		$this->labels = array(
								    'name'               => __('Questions', 'ulp'),
								    'singular_name'      => __('Question', 'ulp'),
								    'add_new'            => __('Add new Question', 'ulp'),
								    'add_new_item'       => __('Add new Question', 'ulp'),
								    'edit_item'          => __('Edit Question', 'ulp'),
								    'new_item'           => __('New Question', 'ulp'),
								    'all_items'          => __('All Questions', 'ulp'),
								    'view_item'          => __('View Question', 'ulp'),
								    'search_items'       => __('Search Question', 'ulp'),
								    'not_found'          => '',
								    'not_found_in_trash' => '',
								    'parent_item_colon'  => '',
								    'menu_name'          => __('Questions', 'ulp'),
		);
		$this->taxonomy_labels = array(
									'name'              => __('Question Categories', 'ulp'),
									'singular_name'     => __('Question Category', 'ulp'),
									'search_items'      => __('Search Question Category', 'ulp'),
									'all_items'         => __('Question Categories', 'ulp'),
									'parent_item'       => '',
									'parent_item_colon' => '',
									'edit_item'         => __('Edit', 'ulp'),
									'update_item'       => __('Update', 'ulp'),
									'add_new_item'      => __('Add new question category', 'ulp'),
									'new_item_name'     => __('Add new question category', 'ulp'),
									'menu_name'         => __('Question Categories', 'ulp'),
		);
		$this->taxonomy_slug = 'ulp_question_categories';
		$this->metaBoxes[] = array(
												'slug' => 'answer_settings',
												'title' => __('Answer Settings', 'ulp'),
												'callback' => array($this, 'answerMetaBox'),
												'context' => 'normal', // normal || side || advanced
												'priority' => 'high', /// high || low
		);
		$this->run(); /// from parent class
	}
	/**
	 * @param int
	 * @return none
	 */
	public function afterSavePost($post_id=0){
			$meta_keys = DbUlp::getPostMetaGroup($post_id, 'answer_settings', FALSE);
			foreach ($meta_keys as $type=>$value){
				if (isset($_POST[$type])){
					update_post_meta($post_id, $type, $_POST[$type]);
				}
			}
			/// initiate special settings if the posts has been created
			$post_got_special_settings = DbUlp::does_post_meta_exists($post_id, 'ulp_question_points');
			if ($post_got_special_settings===FALSE){
					$defaults = DbUlp::getPostMetaGroup($post_id, 'questions_special_settings', TRUE);
					DbUlp::update_post_meta_group('questions_special_settings', $post_id, $defaults );
			}
	}
	/**
	 * @param object
	 * @return string
	 */
	public function answerMetaBox($post=null){
		$post_id = isset($post->ID) ? $post->ID : 0;
		$meta_data = DbUlp::getPostMetaGroup($post_id, 'answer_settings', TRUE);
		/// output
		$view = new ViewUlp();
		$view->setTemplate(ULP_PATH . 'views/admin/meta_boxes/questions_answer.php');
		$view->setContentData($meta_data);
		echo $view->getOutput();
	}
}
endif;
