<?php
namespace com\cminds\registration\view;

use com\cminds\registration\model\Settings;

require_once dirname(__FILE__) . '/SettingsViewAbstract.php';

class SettingsView extends SettingsViewAbstract {
	
	public function renderSubcategory($category, $subcategory) {
		
		$cls = '';
		$empty = '';
		if($subcategory == 'api' || $subcategory == 'editprofileform' || $subcategory == 'appearance' || $subcategory == 'toastmessage' || $subcategory == 'adminbar' || $subcategory == 'selfregister' || $subcategory == 'access' || $subcategory == 'social-login' || $subcategory == 'limit' || $subcategory == 'resetpasswordlimit' || $subcategory == 'ip' || $subcategory == 'redirect_role' || $subcategory == 'multi_step' || $subcategory == 'subscribe' || $subcategory == 'verification' || $subcategory == 'register_ip' || $subcategory == 's2member' || $subcategory == 'age_verification' || $subcategory == 'invitations' || $subcategory == 'email' || $subcategory == 'automatic_creation' || $subcategory == 'password_email' || $subcategory == 'welcome' || $subcategory == 'activation' || $subcategory == 'invite_friend' || $subcategory == 'account_deleted' || $subcategory == 'reset_password_email' || $subcategory == 'self_register_email' || $subcategory == 'edit_profile_confirm_email' || $subcategory == 'password_email_by_admin' || $subcategory == 'navigation' || $subcategory == 'dashboard_appearance' || $subcategory == 'tabs') {
			$cls = 'onlyinpro';
			if($subcategory == 'selfregister' || $subcategory == 'redirect_role' || $subcategory =='age_verification' || $subcategory == 'automatic_creation' || $subcategory == 'password_email_by_admin' || $subcategory == 'tabs') {
				$empty = '<br><table><tr><td><div class="onlyinpro_empty" style="display:none;"><p style="margin:0px;font-weight:normal;">Pro options are hidden. Click the button <span style="font-weight:bold;color:#00cd00;">"Show/hide Pro options"</span> to see them.</p></div></td></tr></table>';
			}
		}
		$content = parent::renderSubcategory($category, $subcategory);
		if (strlen(strip_tags($content)) > 0) {
			return sprintf('<table class="'.$cls.'"><caption>%s</caption>%s</table>'.$empty,
				esc_html($this->getSubcategoryTitle($category, $subcategory)),
				$content
			);
		}
	}
	
	public function renderOption($name, array $option = array()) {
		if(strpos($name, 'onlyinpro') == 1) {
			return sprintf('<tr class="onlyinpro">%s</tr>', parent::renderOption($name, $option));
		} else {
			return sprintf('<tr>%s</tr>', parent::renderOption($name, $option));
		}
	}
	
	public function renderOptionTitle($option) {
		return sprintf('<th scope="row" class="option-title">%s:</th>', parent::renderOptionTitle($option));
	}
	
	public function renderOptionControls($name, array $option = array()) {
		return sprintf('<td class="option-controls">%s</td>', parent::renderOptionControls($name, $option));
	}
	
	public function renderOptionDescription($option) {
		return sprintf('<td class="option-description">%s</td>', parent::renderOptionDescription($option));
	}
	
	protected function getSubcategoryTitle($category, $subcategory) {
		$subcategories = $this->getSubcategories();
		if (isset($subcategories[$category]) AND isset($subcategories[$category][$subcategory])) {
			return __($subcategories[$category][$subcategory]);
		} else {
			return $subcategory;
		}
	}
	
	protected function getCategories() {
		return apply_filters('cmreg_settings_pages', Settings::$categories);
	}
	
	protected function getSubcategories() {
		return apply_filters('cmreg_settings_pages_groups', Settings::$subcategories);
	}
	
}