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

use com\cminds\maplocations\model\Settings;

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

class SettingsView extends SettingsViewAbstract {
	
	public function renderSubcategory($category, $subcategory) {
		$cls = '';
		$empty = '';
		if($subcategory == 'rest' || $subcategory == 'usertrack' || $subcategory == 'templocation' || $subcategory == 'temppolygon' || $subcategory == 'temppolygonps' || $subcategory == 'units' || $subcategory == 'icons' || $subcategory == 'geolocation' || $subcategory == 'share' || $subcategory == 'other' || $subcategory == 'index_map' || $subcategory == 'zip' || $subcategory == 'infowindow' || $subcategory == 'shape' || $subcategory == 'moderation' || $subcategory == 'notifications' || $subcategory == 'access' || $subcategory == 'taxonomies' || $subcategory == 'section') {
			$cls = 'onlyinpro';
		}
		if($subcategory == 'navigation' || $subcategory == 'temppolygonps' || $subcategory == 'other' || $subcategory == 'infowindow' || $subcategory == 'location_map' || $subcategory == 'shape' || $subcategory == 'notifications' || $subcategory == 'access' || $subcategory == 'locationform' || $subcategory == 'taxonomies' || $subcategory == 'section') {
			$empty = '<table><tr><td><div class="onlyinpro_empty" style="display:none;"><p style="margin:20px 0px 0px 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('cmloc_settings_pages', Settings::$categories);
	}
	
	protected function getSubcategories() {
		return apply_filters('cmloc_settings_pages_groups', Settings::$subcategories);
	}
	
}